Commit 470888ffb0 for qemu.org

commit 470888ffb0220911a0ab58f9ffd41d95f36e4a22
Author: Eric Blake <eblake@redhat.com>
Date:   Thu Nov 12 10:59:58 2026 -0600

    nbd/client: Don't leak uninitialized memory on malicious server

    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_=165, 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(NBDClient *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 = nbd_co_send_iov(client, iov, 2, errp);
    |          } else {
    |              ret = blk_co_pread(exp->common.blk, offset + progress, pnum,

    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 <bea1e@proton.me>
    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 <eblake@redhat.com>
    Message-ID: <20261113223742.2546619-9-eblake@redhat.com>
    Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
    [eblake: Commit message enhanced with reproducer]
    Signed-off-by: Eric Blake <eblake@redhat.com>

diff --git a/block/nbd.c b/block/nbd.c
index d873892c19..fb4295bb23 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 = NULL;
     Error *local_err = NULL;
+    uint64_t highwater = offset;

     NBD_FOREACH_REPLY_CHUNK(s, iter, cookie,
                             s->info.mode >= NBD_MODE_STRUCTURED,
@@ -1158,6 +1159,7 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t cookie,
     {
         int ret;
         NBDStructuredReplyChunk *chunk = &reply.structured;
+        NBDStructuredReadHole *slice = 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 = payload;
             break;
         case NBD_REPLY_TYPE_OFFSET_HOLE:
             ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload,
@@ -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 = payload;
             }
             break;
         default:
@@ -1187,10 +1193,26 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_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 = MAX(highwater, slice->offset + slice->length);
+        }
+
         g_free(payload);
         payload = NULL;
     }

+    /* Initialize any trailing region not touched by server. */
+    if (iter.only_structured && iter.ret == 0 &&
+        highwater < offset + qiov->size) {
+        qemu_iovec_memset(qiov, highwater - offset, 0,
+                          offset + qiov->size - highwater);
+    }
+
     error_propagate(errp, iter.err);
     *request_ret = iter.request_ret;
     return iter.ret;