dmg_uncompress_lzfse_do() treated any non-zero lzfse_decode_buffer()
return as success. A crafted DMG can declare a larger chunk output size
than its LZFSE stream decodes to, leaking the unzeroed tail of
uncompressed_chunk to the guest as sector data -- a heap disclosure.

lzfse_decode_buffer() returns the destination capacity both for an
exact-size and a too-small destination, so decode with one extra byte of
capacity (allocated in dmg_open()): an exact stream returns avail_out, a
short or oversized one does not. Require out_size == avail_out. Decoding
in place also avoids a per-chunk g_malloc()/memcpy() of up to 64 MiB in
the read path.

zlib and bzip2 chunk paths already enforce this; LZFSE did not.

Fixes: CVE-2026-116483
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4662
Reported-by: oxsignal <[email protected]>
Signed-off-by: Julio Faracco <[email protected]>
---
 block/dmg-lzfse.c | 27 +++++++++++++++++++--------
 block/dmg.c       |  8 +++++++-
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/block/dmg-lzfse.c b/block/dmg-lzfse.c
index 4ea0b9b20d..b1acd87135 100644
--- a/block/dmg-lzfse.c
+++ b/block/dmg-lzfse.c
@@ -33,16 +33,27 @@
 static int dmg_uncompress_lzfse_do(char *next_in, unsigned int avail_in,
                                    char *next_out, unsigned int avail_out)
 {
-    size_t out_size = lzfse_decode_buffer((uint8_t *) next_out, avail_out,
-                                          (uint8_t *) next_in, avail_in,
-                                          NULL);
+    size_t out_size;
 
-    /* We need to decode the single chunk only. */
-    /* So, out_size == avail_out is not an error here. */
-    if (out_size > 0) {
-        return out_size;
+    /*
+     * lzfse_decode_buffer() returns the destination capacity both on an
+     * exact decode and when the destination is too small, so on its own it
+     * cannot tell a full chunk from a short one. The caller allocates
+     * next_out one byte larger than avail_out (see dmg_open() in dmg.c);
+     * decoding with that extra byte of capacity lets an exact decode
+     * (out_size == avail_out) be distinguished from a short or oversized
+     * stream, without a per-chunk scratch allocation or memcpy.
+     *
+     * next_out MUST have room for avail_out + 1 bytes.
+     */
+    out_size = lzfse_decode_buffer((uint8_t *) next_out, avail_out + 1,
+                                   (uint8_t *) next_in, avail_in,
+                                   NULL);
+
+    if (out_size != avail_out) {
+        return -1;
     }
-    return -1;
+    return out_size;
 }
 
 __attribute__((constructor))
diff --git a/block/dmg.c b/block/dmg.c
index 412d4bfc35..aa0a0a2fe5 100644
--- a/block/dmg.c
+++ b/block/dmg.c
@@ -587,8 +587,14 @@ static int dmg_open(BlockDriverState *bs, QDict *options, 
int flags,
     /* initialize zlib engine */
     s->compressed_chunk = qemu_try_blockalign(bs->file->bs,
                                               ds.max_compressed_size + 1);
+    /*
+     * One extra byte so dmg-lzfse.c can decode a chunk in place with a
+     * capacity of avail_out + 1 and still tell an exact decode from a
+     * short or oversized LZFSE stream. See dmg_uncompress_lzfse_do().
+     */
     s->uncompressed_chunk = qemu_try_blockalign(bs->file->bs,
-                                                512 * 
ds.max_sectors_per_chunk);
+                                                512 * ds.max_sectors_per_chunk
+                                                + 1);
     if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
         ret = -ENOMEM;
         goto fail;

-- 
2.56.0


Reply via email to