From nobody Sun Jul 26 10:12:23 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1783591658; cv=none; d=zohomail.com; s=zohoarc; b=Xj50tuEXl59adD3i+rteRfKO58x3JTuhqaUTN6i6Kv2WK17jDGJWW3PIGV58Ug6ZTB709gAKURU6LNbgWR9v3fn3YDOB64tI/RTP948S+MS5LfALt0fLRatHQEmJFZlEBknnghqR1VJIUizK754xxeuuK+y2i3JZR2wuxw+6aOA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783591658; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=EEPfUffLyiCczr+0f4C8m/x6y6IhzudNuvSeKa4CCYE=; b=QfFCgX3rLcc0+IqQI/BP3ok6/WBedyw1FFIf8lnhTUE2AGlEkcqmVf9H30L+nUQC47y6L2iN2H/urSxIK+DTQBmCazgAp1Sx1XuyO8i8Xf/Q7Ri7D2d072Jog18fCwuOMcJGIVkCn1RtuzZhYKC6GcPqTcquaBrSc2MULkeipZY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1783591658718675.1251023088575; Thu, 9 Jul 2026 03:07:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whlem-0005GT-9l; Thu, 09 Jul 2026 06:07:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whlek-0005Cm-Ga for qemu-devel@nongnu.org; Thu, 09 Jul 2026 06:06:58 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whleg-0003hD-9I for qemu-devel@nongnu.org; Thu, 09 Jul 2026 06:06:57 -0400 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-346-iM1BZif9PDO0zoeaza4SKg-1; Thu, 09 Jul 2026 06:06:51 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0B4311800581; Thu, 9 Jul 2026 10:06:50 +0000 (UTC) Received: from lenovo-t14s.redhat.corp (headnet03.pony-001.prod.iad2.dc.redhat.com [10.2.32.114]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 573F43000C34; Thu, 9 Jul 2026 10:06:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1783591612; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=EEPfUffLyiCczr+0f4C8m/x6y6IhzudNuvSeKa4CCYE=; b=Ir3dmmynPpQgOU8kX8haW1yIOWPTz8FMBVG/HfrgCNCsaO1YDoBPIVLm7c1Ep6nv5j5H53 +oYbjme/ZAJJGDcIe3giGmsH1IaQ38axCTnm0zad4qG3zR9jp97YqyezRTR7su4sVUXCgi raUZOZibgIHhN6TcLEY6e2itphxhhaM= X-MC-Unique: iM1BZif9PDO0zoeaza4SKg-1 X-Mimecast-MFC-AGG-ID: iM1BZif9PDO0zoeaza4SKg_1783591610 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Hanna Reitz , Amit Shah , Laurent Vivier , "Michael S. Tsirkin" , Stefan Hajnoczi , Fam Zheng , Kevin Wolf , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , qemu-stable@nongnu.org Subject: [PATCH] hw/virtio: return NULL from qemu_get_virtqueue_element() on invalid state Date: Thu, 9 Jul 2026 12:06:44 +0200 Message-ID: <20260709100644.1878369-1-lvivier@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=lvivier@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.445, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1783591660952158501 Content-Type: text/plain; charset="utf-8" qemu_get_virtqueue_element() uses assert() to check that the in_num and out_num fields deserialized from the migration stream do not exceed VIRTQUEUE_MAX_SIZE. A crafted migration stream can set these fields to invalid values, hitting the assertion and aborting the destination QEMU process. Replace the assertions with a bounds check that returns NULL on failure. Update all callers (virtio-serial-bus, virtio-blk, virtio-scsi) to handle the NULL return and fail the migration gracefully. Cc: qemu-stable@nongnu.org Fixes: 6bdc21c050a2 ("virtio: fix up max size checks") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3802 Signed-off-by: Laurent Vivier --- hw/block/virtio-blk.c | 4 ++++ hw/char/virtio-serial-bus.c | 3 +++ hw/scsi/virtio-scsi.c | 4 ++++ hw/virtio/virtio.c | 11 ++++------- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index 6b92066aff4c..42c0f2553f18 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -1384,6 +1384,10 @@ static int virtio_blk_load_device(VirtIODevice *vdev= , QEMUFile *f, } =20 req =3D qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq)= ); + if (!req) { + return -EINVAL; + } + virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req); =20 WITH_QEMU_LOCK_GUARD(&s->rq_lock) { diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c index c1973f0248fc..879df158608a 100644 --- a/hw/char/virtio-serial-bus.c +++ b/hw/char/virtio-serial-bus.c @@ -763,6 +763,9 @@ static int fetch_active_ports_list(QEMUFile *f, =20 port->elem =3D qemu_get_virtqueue_element(vdev, f, sizeof(VirtQueueElemen= t)); + if (!port->elem) { + return -EINVAL; + } =20 /* * Port was throttled on source machine. Let's diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 6c7376801190..8dd0b88a30c4 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -274,6 +274,10 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCS= IRequest *sreq) assert(n < vs->conf.num_queues); req =3D qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOSCSIReq) + vs->cdb_size); + if (!req) { + return NULL; + } + virtio_scsi_init_req(s, vs->cmd_vqs[n], req); =20 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index f4d86a365530..7bccfdde33e7 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -2167,13 +2167,10 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev= , QEMUFile *f, size_t sz) =20 qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld)); =20 - /* TODO: teach all callers that this can fail, and return failure inst= ead - * of asserting here. - * This is just one thing (there are probably more) that must be - * fixed before we can allow NDEBUG compilation. - */ - assert(ARRAY_SIZE(data.in_addr) >=3D data.in_num); - assert(ARRAY_SIZE(data.out_addr) >=3D data.out_num); + if (data.in_num > ARRAY_SIZE(data.in_addr) || + data.out_num > ARRAY_SIZE(data.out_addr)) { + return NULL; + } =20 elem =3D virtqueue_alloc_element(sz, data.out_num, data.in_num); elem->index =3D data.index; --=20 2.54.0