All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: Julio Faracco <jcfaracco@gmail.com>
Cc: qemu-devel@nongnu.org, Hanna Reitz <hreitz@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	qemu-block@nongnu.org
Subject: Re: [PATCH 2/2] tests/qemu-iotests: cover the dmg LZFSE short-decode fix
Date: Mon, 16 Nov 2026 17:49:33 +0100	[thread overview]
Message-ID: <avs0HYZSdJOg8Pef@redhat.com> (raw)
In-Reply-To: <20261112-dmg-short-decode-v1-2-f7f342097b60@gmail.com>

Am 12.11.2026 um 16:33 hat Julio Faracco geschrieben:
> Add a regression test for the LZFSE short-decode heap disclosure. It
> builds a minimal DMG (koly trailer, plist, BLKX block, LZFSE data fork)
> whose chunk declares two sectors of output while its LZFSE stream only
> decodes to one, and checks that reading the chunk now fails with an I/O
> error instead of leaking stale heap bytes.
> 
> A valid single-sector DMG is converted first; if that fails, the test
> is skipped as LZFSE support is not built in.
> 
> Signed-off-by: Julio Faracco <jcfaracco@gmail.com>

The last paragraph isn't completely accurate any more, the test checks
the specific error message now.

>  tests/qemu-iotests/tests/dmg-lzfse-short     | 142 +++++++++++++++++++++++++++
>  tests/qemu-iotests/tests/dmg-lzfse-short.out |   2 +
>  2 files changed, 144 insertions(+)
> 
> diff --git a/tests/qemu-iotests/tests/dmg-lzfse-short b/tests/qemu-iotests/tests/dmg-lzfse-short
> new file mode 100755
> index 0000000000..fc0b69cee3
> --- /dev/null
> +++ b/tests/qemu-iotests/tests/dmg-lzfse-short
> @@ -0,0 +1,142 @@
> +#!/usr/bin/env python3
> +# group: rw quick
> +#
> +# Regression test for the dmg LZFSE short-decode heap disclosure.
> +#
> +# Copyright (C) 2026 Red Hat, Inc.
> +#
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 2 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +#
> +# A crafted DMG can declare a chunk output size larger than its LZFSE
> +# stream actually produces.  dmg_uncompress_lzfse_do() used to accept
> +# such a short decode and hand the untouched tail of the (unzeroed)
> +# uncompressed chunk buffer to the guest -- a heap disclosure.  Reading
> +# such a chunk must now fail with an I/O error instead.
> +
> +import base64
> +import os
> +import struct
> +import sys
> +
> +import iotests
> +
> +ULFO = 0x80000007  # LZFSE compressed block type
> +SECTOR = 512
> +
> +
> +def lzfse_uncompressed_stream(payload: bytes) -> bytes:
> +    """A valid LZFSE stream: one uncompressed block + end-of-stream marker."""
> +    return (b'bvx-' + struct.pack('<I', len(payload))  # uncompressed block
> +            + payload
> +            + b'bvx$')                                   # end of stream
> +
> +
> +def build_dmg(stream: bytes, decl_sectors: int, total_sectors: int) -> bytes:
> +    """
> +    Build a minimal DMG with a single LZFSE chunk.  The chunk declares
> +    decl_sectors sectors of output; when that exceeds what 'stream'
> +    decodes to, the decode is short.
> +    """
> +    data_fork = stream
> +
> +    # BLKX ("mish") block: 204-byte header followed by one 40-byte entry.
> +    mish = bytearray(204)
> +    struct.pack_into('>I', mish, 0x00, 0x6d697368)        # 'mish' magic
> +    struct.pack_into('>I', mish, 0x04, 1)                 # version
> +    struct.pack_into('>Q', mish, 0x08, 0)                 # first sector number
> +    struct.pack_into('>Q', mish, 0x10, total_sectors)     # sector count
> +    struct.pack_into('>Q', mish, 0x18, 0)                 # data fork offset
> +
> +    entry = bytearray(40)
> +    struct.pack_into('>I', entry, 0x00, ULFO)             # entry type
> +    struct.pack_into('>Q', entry, 0x08, 0)               # sector number
> +    struct.pack_into('>Q', entry, 0x10, decl_sectors)    # sector count
> +    struct.pack_into('>Q', entry, 0x18, 0)               # compressed offset
> +    struct.pack_into('>Q', entry, 0x20, len(data_fork))  # compressed length
> +    mish += entry
> +
> +    xml = (b'<plist><dict><data>'
> +           + base64.b64encode(bytes(mish))
> +           + b'</data></dict></plist>')
> +
> +    buf = bytearray()
> +    buf += data_fork
> +    xml_off = len(buf)
> +    buf += xml
> +    koly_off = len(buf)
> +
> +    # UDIF trailer ("koly" block), 512 bytes, big-endian.
> +    koly = bytearray(512)
> +    struct.pack_into('>4s', koly, 0x000, b'koly')         # signature
> +    struct.pack_into('>I', koly, 0x004, 4)               # version
> +    struct.pack_into('>I', koly, 0x008, 512)             # header size
> +    struct.pack_into('>Q', koly, 0x018, 0)               # DataForkOffset
> +    struct.pack_into('>Q', koly, 0x020, len(data_fork))  # DataForkLength
> +    struct.pack_into('>Q', koly, 0x028, 0)               # RsrcForkOffset
> +    struct.pack_into('>Q', koly, 0x030, 0)               # RsrcForkLength
> +    struct.pack_into('>Q', koly, 0x0d8, xml_off)         # XMLOffset
> +    struct.pack_into('>Q', koly, 0x0e0, len(xml))        # XMLLength
> +    struct.pack_into('>q', koly, 0x1ec, total_sectors)   # SectorCount
> +    buf += koly
> +
> +    assert koly_off == len(buf) - 512
> +    return bytes(buf)
> +
> +
> +def convert_to_raw(dmg: bytes, name: str):
> +    dmg_path = os.path.join(iotests.test_dir, name)
> +    raw_path = dmg_path + '.raw'
> +    with open(dmg_path, 'wb') as f:
> +        f.write(dmg)
> +    res = iotests.qemu_img('convert', '-f', 'dmg', '-O', 'raw',
> +                           dmg_path, raw_path, check=False)
> +    return res, dmg_path, raw_path

The test forgets to clean up after itself, i.e. remove the image files
it created. iotests.py has the FilePath context manager, or if that is
hard to integrate, the file_path() function to support this.

It's a common pattern to wrap a test in something like this:

    with iotests.FilePath('disk.img') as path:
        ...

> +
> +
> +def main() -> None:
> +    payload = b'\xab' * SECTOR
> +
> +    # A well-formed single-sector chunk must convert cleanly.  Skip only
> +    # when QEMU was built without the LZFSE module (so this would never
> +    # work); treat any other failure as a real regression.
> +    good = build_dmg(lzfse_uncompressed_stream(payload),
> +                     decl_sectors=1, total_sectors=1)
> +    res, _, raw_path = convert_to_raw(good, 'good.dmg')
> +    if res.returncode != 0:
> +        if 'dmg-lzfse module is missing' in res.stdout:
> +            iotests.notrun('LZFSE decompression not available')
> +        iotests.log('FAIL: valid LZFSE chunk failed to convert')
> +        iotests.log(res.stdout)
> +        sys.exit(1)
> +
> +    with open(raw_path, 'rb') as f:
> +        if f.read() != payload:
> +            iotests.log('FAIL: valid LZFSE chunk decoded incorrectly')
> +            return
> +    iotests.log('valid LZFSE chunk converts: ok')
> +
> +    # Same 512-byte stream, but the chunk claims two sectors (1024 bytes).
> +    # The decode is short and must be rejected with an I/O error.
> +    evil = build_dmg(lzfse_uncompressed_stream(payload),
> +                     decl_sectors=2, total_sectors=2)
> +    res, _, _ = convert_to_raw(evil, 'evil.dmg')
> +    if res.returncode == 0:
> +        iotests.log('FAIL: short LZFSE decode was accepted')
> +        return
> +    iotests.log('short LZFSE decode rejected: ok')

I wonder if we should check a more specific error message here, too. If
anything else goes wrong (can't open the image or whatever), we don't
necessarily want to count that as a success.

I'm also wondering why you chose to 'qemu-img convert' and then open the
raw image in Python. Is there a problem with the simpler way of just
verifying with qemu-io that 'read -P 0xab 0 512' directly on the dmg
image returns the expected result?

> +
> +
> +iotests.script_main(main, supported_fmts=['dmg'],
> +                    supported_protocols=['file'],
> +                    supported_platforms=['linux'])
> diff --git a/tests/qemu-iotests/tests/dmg-lzfse-short.out b/tests/qemu-iotests/tests/dmg-lzfse-short.out
> new file mode 100644
> index 0000000000..6802cf1a76
> --- /dev/null
> +++ b/tests/qemu-iotests/tests/dmg-lzfse-short.out
> @@ -0,0 +1,2 @@
> +valid LZFSE chunk converts: ok
> +short LZFSE decode rejected: ok

Kevin



      reply	other threads:[~2026-11-16 16:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 15:33 [PATCH 0/2] block/dmg-lzfse: fix short-decode heap disclosure and add regression test Julio Faracco
2026-11-12 15:33 ` [PATCH 1/2] block/dmg-lzfse: reject short LZFSE decodes Julio Faracco
2026-11-16 17:03   ` Kevin Wolf
2026-11-16 22:09   ` Stefan Hajnoczi
2026-11-12 15:33 ` [PATCH 2/2] tests/qemu-iotests: cover the dmg LZFSE short-decode fix Julio Faracco
2026-11-16 16:49   ` Kevin Wolf [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=avs0HYZSdJOg8Pef@redhat.com \
    --to=kwolf@redhat.com \
    --cc=hreitz@redhat.com \
    --cc=jcfaracco@gmail.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.