From nobody Mon Nov 23 04:26:33 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=1795119883; cv=none; d=zohomail.com; s=zohoarc; b=XQM4JwEQklSTyEVqnspPXlBDwy+L1IPdVp66TeiJ894hO/F9HaSGGLKbvTqDjvCyql/zRmp5MVXA8y+Q/3g7lyoe4IXbpDpiEwgcu60kCqDxB53bb7tjdXLlxmzreSPWKmqcscCx4qdhOVQei+cXeX7tf/lJr0GYYe0TtM/ko5A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795119883; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To: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=n7ijLyDQT5N01imvJZvlansp62otmTWiU/aPlqnLi0w=; b=QqxwV7JOgSszswYbfPgFKCT25kXOUtZxI8C1eNclr33hYpf070df2xnyno6GfXw36EnDuo5HMxP0tGNosjYmbapC2BarFSezYPj7/YyHQvOwCTmsUMnasY68Wl3tPZ7bHniRWVEIolo8ZhNxcs0EGoaVuOXy6u8+2SmTXXSQL28= 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 1795119883774952.8114851447076; Thu, 19 Nov 2026 12:24:43 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU8fe-0008Sz-M7; Thu, 19 Nov 2026 15:23:50 -0500 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 1xU8fd-0008SW-Ar for qemu-devel@nongnu.org; Thu, 19 Nov 2026 15:23:49 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xU8fW-0003JC-Md for qemu-devel@nongnu.org; Thu, 19 Nov 2026 15:23:44 -0500 Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-655-y5GfXF3eMPCjdwnqXzUaCA-1; Thu, 19 Nov 2026 20:23:37 +0000 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A1A451955DA2; Thu, 19 Nov 2026 20:23:36 +0000 (UTC) Received: from eblake.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A30FD34C; Thu, 19 Nov 2026 20:23:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795119822; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n7ijLyDQT5N01imvJZvlansp62otmTWiU/aPlqnLi0w=; b=IhbHr/Qqthm0YeJs2pAwLPas6QJX8vDR5Sn8UX78qp7w3CjXVvF2LI5uSNJnwgbfoL7yO4 9cM28TwldAZM21Ww6Mc/ceWXK5YYbRajHzM0njkvargw9OwecEnZ+vmSpdvJTKJyTwhJcV eG2asjip40bk72ZnlCt4jvFuGnWc32A= X-MC-Unique: y5GfXF3eMPCjdwnqXzUaCA-1 X-Mimecast-MFC-AGG-ID: y5GfXF3eMPCjdwnqXzUaCA_1795119816 From: Eric Blake To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi , Vladimir Sementsov-Ogievskiy , Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org (open list:Network Block Dev...) Subject: [PULL 1/3] nbd: Refactor structured payload interpretation Date: Thu, 19 Nov 2026 14:23:11 -0600 Message-ID: <20261119202330.3853797-6-eblake@redhat.com> In-Reply-To: <20261119202330.3853797-5-eblake@redhat.com> References: <20261119202330.3853797-5-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 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.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 5 X-Spam_score: 0.5 X-Spam_bar: / X-Spam_report: (0.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no 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: 1795119885193158500 Content-Type: text/plain; charset="utf-8" An upcoming patch wants to reuse the payload of a structured reply chunk in further code for additional safety checks on the server's response. It will be easiest if the initial parse converts the payload from network order into host order along the way, so that I don't introduce subtle endian-specific bugs in that upcoming code. A quick audit shows that none of the current callers of payload_advance*() were reading a memory location within the payload after its initial parse. Signed-off-by: Eric Blake Message-ID: <20261113223742.2546619-7-eblake@redhat.com> Reviewed-by: Stefan Hajnoczi --- block/nbd.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index e5e16722ba2..052469b3547 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -578,24 +578,36 @@ err: return rc; } +/* Consume a field of payload, while swapping it in place to host-endian. = */ static inline uint16_t payload_advance16(uint8_t **payload) { + uint16_t value =3D lduw_be_p(*payload); + stw_he_p(*payload, value); *payload +=3D 2; - return lduw_be_p(*payload - 2); + return value; } static inline uint32_t payload_advance32(uint8_t **payload) { + uint32_t value =3D ldl_be_p(*payload); + stl_he_p(*payload, value); *payload +=3D 4; - return ldl_be_p(*payload - 4); + return value; } static inline uint64_t payload_advance64(uint8_t **payload) { + uint64_t value =3D ldq_be_p(*payload); + stq_he_p(*payload, value); *payload +=3D 8; - return ldq_be_p(*payload - 8); + return value; } +/* + * nbd_parse_offset_hole_payload: + * Parse a chunk for NBD_REPLY_TYPE_OFFSET_HOLE, updating payload in + * place to a host-endian description of the region described. + */ static int nbd_parse_offset_hole_payload(BDRVNBDState *s, NBDStructuredReplyChunk *chunk, uint8_t *payload, uint64_t orig_o= ffset, --=20 2.56.1 From nobody Mon Nov 23 04:26:33 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=1795119881; cv=none; d=zohomail.com; s=zohoarc; b=Xb8Uj847zoi4ilQn7NXPVJ6DA1PbNGilJid3b17A5DF8hyemF5CoyzM4mvs1sSXCCumSOVbyU57VJYAfvC3y/b+GZVNdt4BewtVJvrq4FU98dmCDS/hKzRm2vcdJox0ITS5xXfwF7hBYaOcE1bhVY0fEGf+ob8YvZqj1DmJ7ekg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795119881; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To: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=UOtdhRClmToIkuINOHRNiC72n4fmaONo8KvuB+D4MOA=; b=nIfKvEApnQW8GW/i4CuyG2QMR22VW0HacglKROFTzzY8YnbXD+8BEebkYtANmBEHuqnQ5FD4G11ER02ilghTHsImWOWv3HLAp2vdSn3uVfEUCGwOoRY4N2rN+1Wlnxxje6GtvBFA/c1CQ86ntK2a8bVzX82UkRSMEeT7WOFkghE= 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 1795119880998485.47295931927556; Thu, 19 Nov 2026 12:24:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU8fj-0008VL-4a; Thu, 19 Nov 2026 15:23:55 -0500 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 1xU8fg-0008U4-S4 for qemu-devel@nongnu.org; Thu, 19 Nov 2026 15:23:52 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xU8fd-0003Jb-7Q for qemu-devel@nongnu.org; Thu, 19 Nov 2026 15:23:52 -0500 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-635-JmhlUJMdPwKLhmbWCPNdQA-1; Thu, 19 Nov 2026 15:23:39 -0500 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 72FE6195D03D; Thu, 19 Nov 2026 20:23:38 +0000 (UTC) Received: from eblake.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2A5B734C; Thu, 19 Nov 2026 20:23:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795119825; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UOtdhRClmToIkuINOHRNiC72n4fmaONo8KvuB+D4MOA=; b=GZPptpc3tN1N6k961495oMzUd6+q1NoyoN2J+9XZDKz6m7WqeSrr3RpGFxtjcmYVY7zbZS IbLv6FZ4r9S2Bv1BQi/KVKhhuFRnstqQ8H5xmobfFHh6OpjMo4E0XMI78MuUqU3tQDfE/Z 80T7Eiq9NV8Z8TeU6nkdQjCwPSfwbYg= X-MC-Unique: JmhlUJMdPwKLhmbWCPNdQA-1 X-Mimecast-MFC-AGG-ID: JmhlUJMdPwKLhmbWCPNdQA_1795119818 From: Eric Blake To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi , Vladimir Sementsov-Ogievskiy , Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org (open list:Network Block Dev...) Subject: [PULL 2/3] nbd: Pass back offset/length for all structured read replies Date: Thu, 19 Nov 2026 14:23:12 -0600 Message-ID: <20261119202330.3853797-7-eblake@redhat.com> In-Reply-To: <20261119202330.3853797-5-eblake@redhat.com> References: <20261119202330.3853797-5-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 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.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 5 X-Spam_score: 0.5 X-Spam_bar: / X-Spam_report: (0.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no 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: 1795119883353158500 Content-Type: text/plain; charset="utf-8" The NBD spec allows a server that has negotiated structured replies to respond in any order among its various reply chunks. Although most known servers happen to give replies in sequential order, QEMU must be prepared for a server that intentionally returns out-of-order read chunks (a malicious server could exploit that in an effort to attempt to convince QEMU to expose uninitialized memory to a guest that is using the NBD client as storage). Although this patch does not actually add any new validation of the server's response chunks, it does make it easier for the next patch to do so by providing a centralized location in the code. Now, in nbd_co_receive_cmdread_reply(), payload contains a host-endian offset+length after either the nbd_co_receive_one_chunk for OFFSET_DATA or the nbd_parse_offset_hole_payload() for OFFSET_HOLE. Signed-off-by: Eric Blake Message-ID: <20261113223742.2546619-8-eblake@redhat.com> Reviewed-by: Stefan Hajnoczi --- block/nbd.c | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 052469b3547..d873892c197 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -786,9 +786,17 @@ static int nbd_parse_error_payload(NBDStructuredReplyC= hunk *chunk, return 0; } +/* + * nbd_co_receive_offset_data_payload: + * Helper to nbd_co_do_receive_one_chunk when server replies with + * structured reply to NBD_CMD_READ with an OFFSET_DATA chunk. On + * success, synthesize an NBDStructuredReadHole into @payload + * describing the same region that was populated into @qiov. + */ static int coroutine_fn nbd_co_receive_offset_data_payload(BDRVNBDState *s, uint64_t orig_offset, - QEMUIOVector *qiov, Error **errp) + QEMUIOVector *qiov, void **payload, + Error **errp) { QEMUIOVector sub_qiov; uint64_t offset; @@ -797,6 +805,7 @@ nbd_co_receive_offset_data_payload(BDRVNBDState *s, uin= t64_t orig_offset, NBDStructuredReplyChunk *chunk =3D &s->reply.structured; assert(nbd_reply_is_structured(&s->reply)); + assert(payload); /* The NBD spec requires at least one byte of payload */ if (chunk->length <=3D sizeof(offset)) { @@ -826,6 +835,18 @@ nbd_co_receive_offset_data_payload(BDRVNBDState *s, ui= nt64_t orig_offset, ret =3D qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, err= p); qemu_iovec_destroy(&sub_qiov); + if (ret =3D=3D 0) { + /* + * Give the caller a struct describing the same region we + * populated here, to allow for easier followup accounting + * between data and hole reads. + */ + NBDStructuredReadHole *synth =3D g_new(NBDStructuredReadHole, 1); + synth->offset =3D offset; + synth->length =3D data_size; + *payload =3D synth; + } + return ret < 0 ? -EIO : 0; } @@ -867,18 +888,21 @@ static coroutine_fn int nbd_co_receive_structured_pay= load( /* * nbd_co_do_receive_one_chunk + * @qiov is non-NULL for NBD_CMD_READ, NULL for all other commands. * for simple reply: * set request_ret to received reply error - * if qiov is not NULL: read payload to @qiov + * if qiov is not NULL: read data to @qiov + * either way, @payload stays NULL * for structured reply chunk: - * if error chunk: read payload, set @request_ret, do not set @payload - * else if offset_data chunk: read payload data to @qiov, do not set @pa= yload - * else: read payload to @payload + * if error chunk: read chunk payload, set @request_ret, @payload is NULL + * else if offset_data chunk and @qiov: read data to @qiov, allocate + * @payload as NBDStructuredReadHole describing region just populated + * else: read payload to @payload; caller checks size and contents * - * If function fails, @errp contains corresponding error message, and the - * connection with the server is suspect. If it returns 0, then the - * transaction succeeded (although @request_ret may be a negative errno - * corresponding to the server's error reply), and errp is unchanged. + * If function fails, @errp contains corresponding error message, @payload + * is NULL, and the connection with the server is suspect. If it returns = 0, + * then the transaction succeeded (although @request_ret may be a negative + * errno corresponding to the server's error reply), and errp is unchanged. */ static coroutine_fn int nbd_co_do_receive_one_chunk( BDRVNBDState *s, uint64_t cookie, bool only_structured, @@ -945,7 +969,7 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( return nbd_co_receive_offset_data_payload( s, nbd_request_by_cookie(s, cookie, &error_abort)->offset, - qiov, errp); + qiov, payload, errp); } if (nbd_reply_type_is_error(chunk->type)) { @@ -1093,6 +1117,7 @@ static bool coroutine_fn nbd_reply_chunk_iter_receive= (BDRVNBDState *s, return true; break_loop: + assert(payload =3D=3D NULL || *payload =3D=3D NULL); qemu_mutex_lock(&s->requests_lock); nbd_request_by_cookie(s, cookie, &error_abort)->coroutine =3D NULL; s->in_flight--; --=20 2.56.1 From nobody Mon Nov 23 04:26:33 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=1795119879; cv=none; d=zohomail.com; s=zohoarc; b=HMKZJ+JhYS+Q3Ou/LlFrqOyiudpDNKFc/td1+R+d6iJqC/KFpVGBUXvzfZpcFV+0txugo/a+SiTkH5tM1GogXw2wfbH1EtzvuFE6lXgwyUJq22H4TFGhv/QvockzSRen2VQIPf0APQSdAr2poAciD4WF3tmGgwVlv4SvkXnT/7I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795119879; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To: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=JRUWMAi1eNHlg4Ac9L7ARUkj1IaKjLqyCIjaOJ8wO60=; b=liy7zbu263RQprqXvajoOt7lfjlDjQGnHTOf7W5b6W1rVqWdiBAFjnJhB+nmg8KhzZEgEN0ApweZiNAhzKXWXXOMFeFO01F53oPJTBDKhEFTfQI5CQKX8vLax6Br0ndBIz6i5C9jbmgFj5X1m8yYzWfUXKsLwUTdvuDjroaNw1w= 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 1795119879956469.6120688989962; Thu, 19 Nov 2026 12:24:39 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU8fg-0008U1-HL; Thu, 19 Nov 2026 15:23:52 -0500 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 1xU8ff-0008TZ-Cd for qemu-devel@nongnu.org; Thu, 19 Nov 2026 15:23:51 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xU8fd-0003JY-7Q for qemu-devel@nongnu.org; Thu, 19 Nov 2026 15:23:51 -0500 Received: from mx-prod-mc-06.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-595-QgNkpGuAMcGvYH0m7dBoJg-1; Thu, 19 Nov 2026 20:23:41 +0000 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6C8811802144; Thu, 19 Nov 2026 20:23:40 +0000 (UTC) Received: from eblake.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CA99D34C; Thu, 19 Nov 2026 20:23:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795119825; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JRUWMAi1eNHlg4Ac9L7ARUkj1IaKjLqyCIjaOJ8wO60=; b=axrffaSjAlHck8PwZdvgMMbPFOLa8tjYAEfhSAO6c4rf7qAKQuBPOddwJr6YKS8ijtx7X6 ljaJp8NCSXudF6UCXqTQX10rz0BLKGh8WZYrn9LQf/wjTNhMB6RhJAboR1lJKkxQsWNFaF bUt2rGuLvl2Luo8ERXBXon/u9oDLV5c= X-MC-Unique: QgNkpGuAMcGvYH0m7dBoJg-1 X-Mimecast-MFC-AGG-ID: QgNkpGuAMcGvYH0m7dBoJg_1795119820 From: Eric Blake To: qemu-devel@nongnu.org Cc: Feifan Qian , qemu-stable@nongnu.org, Stefan Hajnoczi , Vladimir Sementsov-Ogievskiy , Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org (open list:Network Block Dev...) Subject: [PULL 3/3] nbd/client: Don't leak uninitialized memory on malicious server Date: Thu, 19 Nov 2026 14:23:13 -0600 Message-ID: <20261119202330.3853797-8-eblake@redhat.com> In-Reply-To: <20261119202330.3853797-5-eblake@redhat.com> References: <20261119202330.3853797-5-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 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.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=unavailable 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: 1795119881751158500 Content-Type: text/plain; charset="utf-8" The NBD spec[1] permits servers to send out-of-order replies for a structured read, which the client must re-assemble into the final buffer of its NBD_CMD_READ request. The standard states a server must send chunks covering the entire region, but leaves the burden on the client for double-checking whether a server actually sent sufficient reply chunks. If the client never checks for a server omitting chunks, or even sending multiple chunks that re-write the same portion of the buffer, then the NBD spec says behavior is unspecified. But ever since we added structured reads to QEMU, the actual behavior is that we would end up silently leaking QEMU host heap contents into the guest's view, which the guest blindly assumes is legitimate data sent by the server. A proof of concept reported by Feifan Qian with the help of AI analysis demonstrated the ability to leak 0x5a bytes from a qemu-kvm process with MALLOC_PERTURB_=3D165, which implies a malicious server can coerce QEMU into divulging other parts of its heap to a guest for exfiltration, where knowing unscrubbed heap contents may make further attacks against QEMU easier. [1] https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md In terms of the security risk, a guest cannot detect when a server does a short reply, and may misbehave if successive reads of the same portion of the disk come back with different contents, based on changes to QEMU's heap between those reads, making it easy for an attacker to attempt to provoke a silent form of data corruption to guests, even though the server cannot control the actual sequence of guest reads or QEMU heap contents between those reads. And although modern computing tends to isolate heap contents to prevent cross-process leaks, it is feasible that a determined attacker could try to set up a scenario in a multi-tenant environment where a guest under attacker control using a disk backed by an NBD server supplied by the attacker could then exfiltrate unrelated information learned from other guests on the same host. As a mitigating factor, QEMU recommends that NBD client connections utilize TLS traffic and x509 certificates to avoid connecting to untrusted NBD servers. See also CVE-2022-0485 for a similar issue with clients being made more robust to a non-compliant server in libnbd. It may be worth proposing a future enhancement to the NBD protocol to allow the client and server to negotiate an additional handshake guarantee that structured reads must occur in order (the client can detect a non-compliant server with less overhead, or even refuse to talk to a server that doesn't know the new protocol). But for now, tracking the highwater mark of what has been covered in the buffer, even if portions of the buffer get covered more than once, is sufficient to guarantee that data left uninitialized by the server is at least deterministic of showing as all zero in the guest's view. At the time I wrote this patch, I also had an AI-assisted unit test ready for qemu-iotests, but the policy update on LLM contributions was not yet ready. I also investigated modifying the existing tests/qemu-iotests/nbd-fault-injector.py to add a way for it to become a non-compliant structured read server, but that would itself require quite a bit of refactoring effort (the fault injector currently negotiates only simple replies, not structured), and would be rendered moot by later use of an AI-assisted unit test. So instead, I am documenting the following two-line patch to turn QEMU into a non-compliant server that transmits less data than normal for holes: | diff --git a/nbd/server.c b/nbd/server.c | index 8c45f9fc43f..5d754cefa29 100644 | --- a/nbd/server.c | +++ b/nbd/server.c | @@ -2251,7 +2251,8 @@ static int coroutine_fn nbd_co_send_sparse_read(NBD= Client *client, | final ? NBD_REPLY_FLAG_DONE : 0, | NBD_REPLY_TYPE_OFFSET_HOLE, request); | stq_be_p(&chunk.offset, offset + progress); | - stl_be_p(&chunk.length, pnum); | + /* HACK - truncate size of hole to test client response */ | + stl_be_p(&chunk.length, 1 /* pnum */); | ret =3D nbd_co_send_iov(client, iov, 2, errp); | } else { | ret =3D blk_co_pread(exp->common.blk, offset + progress, pnu= m, With that patch applied, you can see the difference in behavior by the following setup (recall that qemu-io hard-codes 0xab content after malloc() and before blk_pread(), so this behavior is unaffected by your choice of MALLOC_PERTURB_): $ qemu-img create -f qcow2 sample.qcow2 1m $ $hacked/qemu-nbd -f qcow2 sample.qcow2 & $ $old/qemu-io nbd://localhost:10809 -f raw -c 'read -v 0 16' 00000000: 00 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ................ $ $patched/qemu-io nbd://localhost:10809 -f raw -c 'read -v 0 16' 00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ $ kill %1 Reported-by: Feifan Qian Fixes: CVE-2026-126657 Fixes: https://gitlab.com/qemu-project/qemu/-/work_items/3815 Fixes: f140e300 ("nbd: Minimal structured read for client", v2.11.0) CC: qemu-stable@nongnu.org Signed-off-by: Eric Blake Message-ID: <20261113223742.2546619-9-eblake@redhat.com> Reviewed-by: Stefan Hajnoczi [eblake: Commit message enhanced with reproducer] Signed-off-by: Eric Blake --- block/nbd.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/block/nbd.c b/block/nbd.c index d873892c197..fb4295bb235 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -1151,6 +1151,7 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_= t cookie, NBDReply reply; void *payload =3D NULL; Error *local_err =3D NULL; + uint64_t highwater =3D offset; NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, s->info.mode >=3D NBD_MODE_STRUCTURED, @@ -1158,6 +1159,7 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_= t cookie, { int ret; NBDStructuredReplyChunk *chunk =3D &reply.structured; + NBDStructuredReadHole *slice =3D NULL; assert(nbd_reply_is_structured(&reply)); @@ -1165,8 +1167,9 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_= t cookie, case NBD_REPLY_TYPE_OFFSET_DATA: /* * special cased in nbd_co_receive_one_chunk, data is already - * in qiov + * in qiov, and payload is host-endian offset+length. */ + slice =3D payload; break; case NBD_REPLY_TYPE_OFFSET_HOLE: ret =3D nbd_parse_offset_hole_payload(s, &reply.structured, pa= yload, @@ -1174,6 +1177,9 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_= t cookie, if (ret < 0) { nbd_channel_error(s, ret); nbd_iter_channel_error(&iter, ret, &local_err); + } else { + /* Successful parse swapped payload to host-endian. */ + slice =3D payload; } break; default: @@ -1187,10 +1193,26 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint6= 4_t cookie, } } + /* Initialize any region skipped by a malicious server. */ + if (slice) { + if (slice->offset > highwater) { + qemu_iovec_memset(qiov, highwater - offset, 0, + slice->offset - highwater); + } + highwater =3D MAX(highwater, slice->offset + slice->length); + } + g_free(payload); payload =3D NULL; } + /* Initialize any trailing region not touched by server. */ + if (iter.only_structured && iter.ret =3D=3D 0 && + highwater < offset + qiov->size) { + qemu_iovec_memset(qiov, highwater - offset, 0, + offset + qiov->size - highwater); + } + error_propagate(errp, iter.err); *request_ret =3D iter.request_ret; return iter.ret; --=20 2.56.1