From nobody Mon Nov 23 04:34:28 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=1794609543; cv=none; d=zohomail.com; s=zohoarc; b=BkSC61lh0MBqmfKcxF55HchO1NPcjZOI68R3hKOzorM1Wrgf9taV89AOkaENK5aDGwrb98Ng2ZItXyEdRa3n0HYOS6Eff+hH9dzpccX0DQns8YwLzXybFfHtUSXD7YW1baXb4O0UwQs9ktOUwdCaOOIdJFAGkBT9rMNM7jzReYw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794609543; 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=I6M4dyUT6VoAgxlcqAl38LDA2igusI77Z1konCH45Dk=; b=GRECIN+PTprmRcGI7aSxR3HJyKPMRSXZC/2dwmro3Hl18Pj8z5ifQC5/Er9eD51sQy32UuqZf97ru/1ESEUT5GTGyiNAyG39komhprJtVzyPlBHGhxFmo54PVBcFJY3Wrg6h3s9+WiiZs98cGGOMupQSLY/Rg3O3z8x3Ea5cqeE= 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 1794609543170598.8197667037232; Fri, 13 Nov 2026 14:39:03 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRzu8-0003te-LD; Fri, 13 Nov 2026 17:37:56 -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 1xRzu7-0003t3-It for qemu-devel@nongnu.org; Fri, 13 Nov 2026 17:37:55 -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 1xRzu6-0007pu-3U for qemu-devel@nongnu.org; Fri, 13 Nov 2026 17:37:55 -0500 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-323-DI2YITCfPZ-L6sBNncJLVg-1; Fri, 13 Nov 2026 17:37:50 -0500 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 B826A1800607; Fri, 13 Nov 2026 22:37:48 +0000 (UTC) Received: from eblake.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 34FF21956086; Fri, 13 Nov 2026 22:37:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794609473; 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=I6M4dyUT6VoAgxlcqAl38LDA2igusI77Z1konCH45Dk=; b=QiagMsKwW+7G8z1BV8QV7E0XOC2yqTEsDe9Cjhm2SMYhUhNB2GL56ukrz4Y/wWYM5IS0Zo +T/5O89plFdF0czj4GVVsIh6FCFkJPvkPi1VEzuVOSk5P1owz/kcIyknai4mQtVkxG8CR7 d29QwK8Gpqil8cor6VN/9QRgm4NWJrM= X-MC-Unique: DI2YITCfPZ-L6sBNncJLVg-1 X-Mimecast-MFC-AGG-ID: DI2YITCfPZ-L6sBNncJLVg_1794609469 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, qemu-stable@nongnu.org, bea1e@proton.me, Vladimir Sementsov-Ogievskiy , Kevin Wolf , Hanna Reitz Subject: [PATCH 1/4] nbd: Refactor structured payload interpretation Date: Fri, 13 Nov 2026 16:34:49 -0600 Message-ID: <20261113223742.2546619-7-eblake@redhat.com> In-Reply-To: <20261113223742.2546619-6-eblake@redhat.com> References: <20261113223742.2546619-6-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 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: 1794609545579158501 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 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.0 From nobody Mon Nov 23 04:34:28 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=1794609528; cv=none; d=zohomail.com; s=zohoarc; b=SpENqULgHLQUdtjdApU5oDFUlcq2VEYwFzZvTvHQ0kpjgwN9zBnzBQg4r8raWHbTFyneVYVRBHU8G5QwDgPDiuYgdL/jnnwEBqwFI0lHhxolLYH8ArRWdjXcqaMNh/dC//lo7AzOpjE7NpsCWLJJ5zfYQLK/5lBCB+ucCCfM+/I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794609528; 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=t/Znd9wlesz1u2841EKG9p41Qq1ARoc4hNagujGnXE4=; b=Y7vHeK8iJxv/T+GAljP+vPUNDT7+aFAXLUQiKfym1+ihqbzJp/W3yehQoIwm8Gb5CNj57oYtiHN1KGZO6kg6gp076TJ0p3Pw7Vv3V31zyI3KYq3cIM3pP4q1qKWezaOZ2JGEysrwFRhPIKX8Y+XQwfawK2kRhm/5QOeS+InOZtY= 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 179460952850620.36748002975628; Fri, 13 Nov 2026 14:38:48 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRzuA-0003vX-Ni; Fri, 13 Nov 2026 17:37:58 -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 1xRzu9-0003v5-T9 for qemu-devel@nongnu.org; Fri, 13 Nov 2026 17:37:57 -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 1xRzu8-0007qH-6i for qemu-devel@nongnu.org; Fri, 13 Nov 2026 17:37:57 -0500 Received: from mx-prod-mc-03.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-695-Y-5AAJ22M3u4aVKJjQ7eEw-1; Fri, 13 Nov 2026 17:37:52 -0500 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E26A9195606F; Fri, 13 Nov 2026 22:37:50 +0000 (UTC) Received: from eblake.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1665E1956086; Fri, 13 Nov 2026 22:37:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794609475; 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=t/Znd9wlesz1u2841EKG9p41Qq1ARoc4hNagujGnXE4=; b=AmUhtq3z1Q/FAgDWuUmP19BsH/iyLKhL24kgjkjetHkepBr6fQHnbsqqZQl7QGEPaYjsEx TDSBCGK8G63KXt6MGDAgYfaBh8lxaO997Zmd67X4iZINjr/N3t5EdBna+UcOuKo6QOjVe3 LQpujqRfLY/e+g9MU1gbXt660rf5XrE= X-MC-Unique: Y-5AAJ22M3u4aVKJjQ7eEw-1 X-Mimecast-MFC-AGG-ID: Y-5AAJ22M3u4aVKJjQ7eEw_1794609471 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, qemu-stable@nongnu.org, bea1e@proton.me, Vladimir Sementsov-Ogievskiy , Kevin Wolf , Hanna Reitz Subject: [PATCH 2/4] nbd: Pass back offset/length for all structured read replies Date: Fri, 13 Nov 2026 16:34:50 -0600 Message-ID: <20261113223742.2546619-8-eblake@redhat.com> In-Reply-To: <20261113223742.2546619-6-eblake@redhat.com> References: <20261113223742.2546619-6-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 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: 1794609531473158500 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 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.0 From nobody Mon Nov 23 04:34:28 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=1794609528; cv=none; d=zohomail.com; s=zohoarc; b=AHyfJHiADLSus5zEdgJ5283cvXP4Uk88EKEbFPJWZmN3OcZZ9AkRWWjWuibA2EUacuhp+9uHNfQqhSDiQl5TIawENYh2DwZfFfO3RcDLE8LGCIRBwgzE6Cn3/O3W/xbd5xqjY5NBCUEM126sYDhKaTms4iVm2QYp56gEWL6F5sQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794609528; 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=mr34AFhT/zINqrCH1Z2lz59myyc5HktKLPX9hSFRZT8=; b=aOnm9hmbIAG/vblGQJWHz7lUALkp82B/a0z0YEo61kclKGgtM4H4WazZSZW5OcCBjHBvAcLY7Z9oWnhljwswaZw3MBxkF8zrb2bhYLVT7f0/RAHs4CMYw0Zj/3V2ytFFS1N3D86UYOD5JifbZBDWGET4JGA6MTzXfqLLG/gyAAU= 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 1794609527967958.3988893278212; Fri, 13 Nov 2026 14:38:47 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRzuD-00043G-7O; Fri, 13 Nov 2026 17:38:01 -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 1xRzuB-0003vn-9C for qemu-devel@nongnu.org; Fri, 13 Nov 2026 17:37:59 -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 1xRzu9-0007qV-5r for qemu-devel@nongnu.org; Fri, 13 Nov 2026 17:37:58 -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-35-t2jmLqLRPFiFz-2Y7xlNRw-1; Fri, 13 Nov 2026 17:37:54 -0500 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 2FB7319560BF; Fri, 13 Nov 2026 22:37:53 +0000 (UTC) Received: from eblake.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3EA161956086; Fri, 13 Nov 2026 22:37:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794609477; 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=mr34AFhT/zINqrCH1Z2lz59myyc5HktKLPX9hSFRZT8=; b=SzN+d/+3HH+laY2LvUHT6WK/wxZMihvhx6zNeN0TCiFZLNBAY1r3ctVb1RUHkKRVG9b83M f2mKHsM/0kV69z0UvhzMOpeZSModccqPr+gBPV104BFNUaJ9ugavQWwcPf95PsXiTTNcW4 zGFmgw36BmB1GYrPH7Bs2Q0yK0DQ5tE= X-MC-Unique: t2jmLqLRPFiFz-2Y7xlNRw-1 X-Mimecast-MFC-AGG-ID: t2jmLqLRPFiFz-2Y7xlNRw_1794609473 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, qemu-stable@nongnu.org, bea1e@proton.me, Vladimir Sementsov-Ogievskiy , Kevin Wolf , Hanna Reitz Subject: [PATCH 3/4] nbd/client: Don't leak uninitialized memory on malicious server Date: Fri, 13 Nov 2026 16:34:51 -0600 Message-ID: <20261113223742.2546619-9-eblake@redhat.com> In-Reply-To: <20261113223742.2546619-6-eblake@redhat.com> References: <20261113223742.2546619-6-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 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: 1794609529647158500 Content-Type: text/plain; charset="utf-8" The NBD spec 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 0xa5 bytes from a qemu 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. 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, was sufficient to guarantee that data left uninitialized by the server is at least deterministic of showing as all zero in the guest's view. 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 Reviewed-by: Stefan Hajnoczi --- 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.0 From nobody Mon Nov 23 04:34:28 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=1794609588; cv=none; d=zohomail.com; s=zohoarc; b=aVhZq0QkPxbRbwj3UZXG570nSoT2Zbtagxbsf8VChj7xGnR/SHKhoFcwqvCnmRx3w+cF1nBuq5Bgo1ZAlni/e0G22RjggWRCIg8Ct3FshIwpEPKk5YM4Fzp1q9/hivP17zyky8RhibheIctAWeeEC5QrOGUPf9kYIwoAhd1RxD8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794609588; 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=DuRjv5uyDKgngYsIV7UMpZFoPvwBZ0IH89zulFVjNA4=; b=LApaZnbBkmyWKq0ORdSOI6JTLL32MWjsVw66rIBi0eSbeIafgcycP4xT3ayGYlw6Nm/NaWjX+g3mM9PwWGKxzg7JZ0Rq3ynvLyZuAI/KZSyUJxu2pBukaBD3+Awy1qe6LPHG6r214bDNacsSZEsLvjf0JvccA2xol0smOQSiPpY= 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 1794609588523159.27400853366362; Fri, 13 Nov 2026 14:39:48 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRzuG-0004Bt-H7; Fri, 13 Nov 2026 17:38:04 -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 1xRzuF-00044h-3N for qemu-devel@nongnu.org; Fri, 13 Nov 2026 17:38:03 -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 1xRzuC-0007rU-TO for qemu-devel@nongnu.org; Fri, 13 Nov 2026 17:38:02 -0500 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-311-HB19iSxjMmGza9e5dXniYw-1; Fri, 13 Nov 2026 17:37:56 -0500 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 829CB1800613; Fri, 13 Nov 2026 22:37:55 +0000 (UTC) Received: from eblake.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 88FD61956086; Fri, 13 Nov 2026 22:37:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794609480; 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=DuRjv5uyDKgngYsIV7UMpZFoPvwBZ0IH89zulFVjNA4=; b=gOdhfGiHdTSap7Mvl71DFYknLbugmaTmW42SMjLQgXEIdvTwm58oKpUyc0ObLeVmu78Rql Mv4JUNIGrB287dwa6PK4AM+k/qbi8/n73xb4pbOaPOscLRJgwWqYEg/e2zf6+e9mL5yR5A gCAAa5eFVP1Vtp3EbXMztKe01GdObYo= X-MC-Unique: HB19iSxjMmGza9e5dXniYw-1 X-Mimecast-MFC-AGG-ID: HB19iSxjMmGza9e5dXniYw_1794609475 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, qemu-stable@nongnu.org, bea1e@proton.me, Vladimir Sementsov-Ogievskiy , Kevin Wolf , Hanna Reitz Subject: [PATCH 4/4] nbd: Add iotest exposing previous CVE fix Date: Fri, 13 Nov 2026 16:34:52 -0600 Message-ID: <20261113223742.2546619-10-eblake@redhat.com> In-Reply-To: <20261113223742.2546619-6-eblake@redhat.com> References: <20261113223742.2546619-6-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 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: 1794609589874158500 Content-Type: text/plain; charset="utf-8" Feifan Qian posted a sample script generated with the help of AI that created a known-broken NBD server that intentionally replies to a structured read with only a subset of bytes, to expose the leak of heap data from qemu as an NBD client into an emulated IDE drive. Take the idea of that test, but simplify it into a qemu-io test (much lighter-weight NBD client, and easier access to see what bytes the NBD client code is passing on) that more closely resembles paradigms in my recent nbd-robustness iotest. Also, expand the test to cover multiple scenarios: in addition to the original test demonstrating a prefix of 20 bytes with unwritten tail, I expanded it to cover out-of-order replies, a hole-only reply, and a completely empty reply. Reported-by: Feifan Qian Based-on: https://gitlab.com/qemu-project/qemu/-/work_items/3815 AI-used-for: tests Signed-off-by: Eric Blake --- .../tests/nbd-structured-read-gaps | 215 ++++++++++++++++++ .../tests/nbd-structured-read-gaps.out | 5 + 2 files changed, 220 insertions(+) create mode 100755 tests/qemu-iotests/tests/nbd-structured-read-gaps create mode 100644 tests/qemu-iotests/tests/nbd-structured-read-gaps.out diff --git a/tests/qemu-iotests/tests/nbd-structured-read-gaps b/tests/qemu= -iotests/tests/nbd-structured-read-gaps new file mode 100755 index 00000000000..9f8b3b8b20b --- /dev/null +++ b/tests/qemu-iotests/tests/nbd-structured-read-gaps @@ -0,0 +1,215 @@ +#!/usr/bin/env python3 +# group: rw quick +# +# Test that NBD client zero-initializes any regions omitted by a server +# sending incomplete or out-of-order structured read replies. +# +# Copyright (C) 2026 Red Hat, Inc. +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import os +import socket +import struct +import threading +import iotests + +NBD_INIT_MAGIC =3D 0x4E42444D41474943 +NBD_OPTS_MAGIC =3D 0x49484156454F5054 +NBD_REP_MAGIC =3D 0x0003E889045565A9 +NBD_REQUEST_MAGIC =3D 0x25609513 +NBD_STRUCTURED_REPLY_MAGIC =3D 0x668E33EF + +NBD_FLAG_FIXED_NEWSTYLE =3D 1 << 0 +NBD_FLAG_NO_ZEROES =3D 1 << 1 +NBD_FLAG_HAS_FLAGS =3D 1 << 0 + +NBD_OPT_ABORT =3D 2 +NBD_OPT_GO =3D 7 +NBD_OPT_STRUCTURED_REPLY =3D 8 + +NBD_REP_ACK =3D 1 +NBD_REP_INFO =3D 3 +NBD_REP_ERR_UNSUP =3D (1 << 31) | 1 +NBD_INFO_EXPORT =3D 0 + +NBD_CMD_READ =3D 0 +NBD_CMD_DISC =3D 2 + +NBD_REPLY_FLAG_DONE =3D 1 << 0 +NBD_REPLY_TYPE_NONE =3D 0 +NBD_REPLY_TYPE_OFFSET_DATA =3D 1 +NBD_REPLY_TYPE_OFFSET_HOLE =3D 2 + + +def recvall(conn, size): + data =3D bytearray() + while len(data) < size: + chunk =3D conn.recv(size - len(data)) + if not chunk: + raise EOFError("unexpected EOF") + data.extend(chunk) + return bytes(data) + + +def send_option_reply(conn, option, reply_type, payload=3Db""): + conn.sendall(struct.pack(">QII", NBD_REP_MAGIC, option, reply_type)) + conn.sendall(struct.pack(">I", len(payload)) + payload) + + +def send_chunk(conn, flags, chunk_type, cookie, payload=3Db""): + hdr =3D struct.pack(">IHHQI", NBD_STRUCTURED_REPLY_MAGIC, + flags, chunk_type, cookie, len(payload)) + conn.sendall(hdr + payload) + + +# Emulate a simple NBD server that only knows how to respond to 512-byte +# structured reads from fixed offsets. +class StructuredReadServer(threading.Thread): + def __init__(self, sock_path): + super().__init__(daemon=3DTrue) + self.sock_path =3D sock_path + self.ready =3D threading.Event() + self.error =3D None + + def run(self): + try: + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as srv: + srv.bind(self.sock_path) + srv.listen(1) + self.ready.set() + conn, _ =3D srv.accept() + with conn: + conn.settimeout(10) + self.negotiate(conn) + self.transmission(conn) + except (OSError, EOFError, AssertionError, struct.error) as exc: + self.error =3D exc + + def negotiate(self, conn): + conn.sendall(struct.pack(">QQH", NBD_INIT_MAGIC, NBD_OPTS_MAGIC, + NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZER= OES)) + recvall(conn, 4) # client flags + + while True: + magic, option, length =3D struct.unpack(">QII", recvall(conn, = 16)) + assert magic =3D=3D NBD_OPTS_MAGIC + if length: + recvall(conn, length) + + if option =3D=3D NBD_OPT_STRUCTURED_REPLY: + send_option_reply(conn, option, NBD_REP_ACK) + elif option =3D=3D NBD_OPT_GO: + info =3D struct.pack(">HQH", NBD_INFO_EXPORT, 65536, + NBD_FLAG_HAS_FLAGS) + send_option_reply(conn, option, NBD_REP_INFO, info) + send_option_reply(conn, option, NBD_REP_ACK) + return + elif option =3D=3D NBD_OPT_ABORT: + send_option_reply(conn, option, NBD_REP_ACK) + return + else: + send_option_reply(conn, option, NBD_REP_ERR_UNSUP) + + def transmission(self, conn): + """Trigger several non-compliant server behaviors based on the + client's offset. A compliant server would supply all 512 bytes.""" + while True: + try: + hdr =3D recvall(conn, 28) + except EOFError: + return + magic, _flags, cmd, cookie, offset, length =3D struct.unpack( + ">IHHQQI", hdr) + assert magic =3D=3D NBD_REQUEST_MAGIC + + if cmd =3D=3D NBD_CMD_DISC: + return + assert cmd =3D=3D NBD_CMD_READ and length =3D=3D 512 + + if offset =3D=3D 0: + # Case 1: Short prefix with FLAG_DONE + payload =3D struct.pack(">Q", offset) + b"QEMU_NBD_PREFIX_= DATA" + send_chunk(conn, NBD_REPLY_FLAG_DONE, + NBD_REPLY_TYPE_OFFSET_DATA, cookie, payload) + elif offset =3D=3D 512: + # Case 2: Out-of-order chunks with gaps + NONE|DONE + send_chunk(conn, 0, NBD_REPLY_TYPE_OFFSET_DATA, cookie, + struct.pack(">Q", 768) + (b"\x11" * 128)) + send_chunk(conn, 0, NBD_REPLY_TYPE_OFFSET_DATA, cookie, + struct.pack(">Q", 576) + (b"\x22" * 64)) + send_chunk(conn, NBD_REPLY_FLAG_DONE, + NBD_REPLY_TYPE_NONE, cookie) + elif offset =3D=3D 1024: + # Case 3: Leading & trailing gaps around OFFSET_HOLE + send_chunk(conn, NBD_REPLY_FLAG_DONE, + NBD_REPLY_TYPE_OFFSET_HOLE, cookie, + struct.pack(">QI", 1280, 128)) + elif offset =3D=3D 1536: + # Case 4: Immediate NONE|DONE with zero chunks + send_chunk(conn, NBD_REPLY_FLAG_DONE, + NBD_REPLY_TYPE_NONE, cookie) + + +class TestNBDStructuredReadGaps(iotests.QMPTestCase): + def setUp(self): + self.sock_path =3D os.path.join(iotests.sock_dir, "nbd.sock") + self.server =3D StructuredReadServer(self.sock_path) + self.server.start() + self.assertTrue(self.server.ready.wait(5)) + + def tearDown(self): + self.server.join(timeout=3D5) + self.assertIsNone(self.server.error) + if os.path.exists(self.sock_path): + os.unlink(self.sock_path) + + @staticmethod + def parse_read_v(out): + """Reconstruct raw bytes from `qemu-io -c 'read -v ...'` output.""" + buf =3D bytearray() + for line in out.splitlines(): + # Format: "00000000: 51 45 4d 55 ... QEMU_NBD_PREFIX_" + if ": " not in line: + continue + hex_col =3D line.split(": ", 1)[1][:49] + buf.extend(int(b, 16) for b in hex_col.split()) + return bytes(buf) + + def test_incomplete_and_out_of_order_reads(self): + # Force heap perturbation so uninitialized bytes show up (if not + # already set in caller, this favors values of 0x5A in unused heap) + os.environ.setdefault("MALLOC_PERTURB_", "165") + nbd_uri =3D f"nbd+unix:///?socket=3D{self.sock_path}" + out =3D iotests.qemu_io( + "-f", "raw", + "-c", "read -v 0 512", + "-c", "read -v 512 512", + "-c", "read -v 1024 512", + "-c", "read -v 1536 512", + nbd_uri + ) + data =3D self.parse_read_v(out.stdout) + self.assertEqual(len(data), 2048) + + # Case 1 [0..512): 20-byte prefix + 492 zeroed bytes + self.assertEqual(data[0:20], b"QEMU_NBD_PREFIX_DATA") + self.assertEqual(data[20:512], b"\x00" * 492) + + # Case 2 [512..1024): 0x22 at [576..640), 0x11 at [768..896), + # zeroes elsewhere + self.assertEqual(data[512:576], b"\x00" * 64) + self.assertEqual(data[576:640], b"\x22" * 64) + self.assertEqual(data[640:768], b"\x00" * 128) + self.assertEqual(data[768:896], b"\x11" * 128) + self.assertEqual(data[896:1024], b"\x00" * 128) + + # Case 3 [1024..1536): OFFSET_HOLE + leading/trailing gaps all zer= oed + self.assertEqual(data[1024:1536], b"\x00" * 512) + + # Case 4 [1536..2048): empty NONE|DONE all zeroed + self.assertEqual(data[1536:2048], b"\x00" * 512) + + +if __name__ =3D=3D "__main__": + iotests.main(supported_protocols=3D["nbd"]) diff --git a/tests/qemu-iotests/tests/nbd-structured-read-gaps.out b/tests/= qemu-iotests/tests/nbd-structured-read-gaps.out new file mode 100644 index 00000000000..ae1213e6f86 --- /dev/null +++ b/tests/qemu-iotests/tests/nbd-structured-read-gaps.out @@ -0,0 +1,5 @@ +. +---------------------------------------------------------------------- +Ran 1 tests + +OK --=20 2.56.0