Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DA8F606A686 for ; Tue, 1 Dec 2026 14:19:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=f2EPsm6d Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xYOhC-0001KD-6v; Tue, 01 Dec 2026 09:19:02 -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 1xYOgz-000186-BY for qemu-devel@nongnu.org; Tue, 01 Dec 2026 09:18:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xYOgw-0002hP-VV for qemu-devel@nongnu.org; Tue, 01 Dec 2026 09:18:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796134726; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=81RY8t0poQHkhCsKFt0oS3w6G++WIG2iuVt3wXsjvJg=; b=f2EPsm6d6zDrQJN4Ki1+p3h02pbQ2Ihtnz+K/LoGHmCzJmi1JyKuw8x90JemwyGOPHosnA 1K1hsM7H/E/smwitORS1UK51uTnDdaJTmawzeFuLGWM4Ge2xRltmTTtgXdty6RCJYVDZnl n4RZ4hQrhDW0J5F486CEO09RiLwfEDg= 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-606-H7442ElcO_SS5CArgyq38g-1; Tue, 1 Dec 2026 14:18:41 +0000 X-MC-Unique: H7442ElcO_SS5CArgyq38g-1 X-Mimecast-MFC-AGG-ID: H7442ElcO_SS5CArgyq38g_1796134720 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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 CC7E01944DFB; Tue, 1 Dec 2026 14:18:40 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.44.32.40]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7B80C18011F1; Tue, 1 Dec 2026 14:18:39 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, richard.henderson@linaro.org, qemu-devel@nongnu.org Subject: [PULL 6/6] tests/qemu-iotests: cover the dmg LZFSE truncated-decode fix Date: Tue, 1 Dec 2026 15:18:26 +0100 Message-ID: <20261201141826.295840-7-kwolf@redhat.com> In-Reply-To: <20261201141826.295840-1-kwolf@redhat.com> References: <20261201141826.295840-1-kwolf@redhat.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Received-SPF: pass client-ip=170.10.133.124; envelope-from=kwolf@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_H3=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-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Julio Faracco Add a regression test for the LZFSE truncated-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 Input/output error instead of leaking stale heap bytes. A valid single-sector DMG is verified first with qemu-io; if the 'dmg-lzfse module is missing' error occurs, the test is skipped. Any other failure is treated as a regression. Signed-off-by: Julio Faracco Message-ID: <20261123-dmg-short-decode-v2-v2-2-e1dee5893891@gmail.com> [kwolf: Fixed up pylint errors] Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf --- tests/qemu-iotests/tests/dmg-lzfse-short | 141 +++++++++++++++++++ tests/qemu-iotests/tests/dmg-lzfse-short.out | 2 + 2 files changed, 143 insertions(+) create mode 100755 tests/qemu-iotests/tests/dmg-lzfse-short create mode 100644 tests/qemu-iotests/tests/dmg-lzfse-short.out diff --git a/tests/qemu-iotests/tests/dmg-lzfse-short b/tests/qemu-iotests/tests/dmg-lzfse-short new file mode 100755 index 00000000000..691f83f04b1 --- /dev/null +++ b/tests/qemu-iotests/tests/dmg-lzfse-short @@ -0,0 +1,141 @@ +#!/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 . +# +# 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 struct + +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(' 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 decoded output is truncated. + """ + 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'' + + base64.b64encode(bytes(mish)) + + b'') + + 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 main() -> None: + payload = b'\xab' * SECTOR + + with iotests.FilePath('good.dmg') as good_path, \ + iotests.FilePath('evil.dmg') as evil_path: + + # A well-formed single-sector chunk must be readable. Skip only + # when QEMU was built without the LZFSE module; treat any other + # failure as a real regression. + good = build_dmg(lzfse_uncompressed_stream(payload), + decl_sectors=1, total_sectors=1) + with open(good_path, 'wb') as f: + f.write(good) + + res = iotests.qemu_io('-f', 'dmg', '-r', '-c', 'read -P 0xab 0 512', + good_path, check=False) + 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 read') + iotests.log(res.stdout) + return + iotests.log('valid LZFSE chunk reads: ok') + + # Same 512-byte stream, but the chunk claims two sectors (1024 bytes). + # The decoded output is truncated and must be rejected with an I/O + # error. + evil = build_dmg(lzfse_uncompressed_stream(payload), + decl_sectors=2, total_sectors=2) + with open(evil_path, 'wb') as f: + f.write(evil) + + res = iotests.qemu_io('-f', 'dmg', '-r', '-c', 'read 0 1024', + evil_path, check=False) + if res.returncode == 0: + iotests.log('FAIL: truncated LZFSE decode was accepted') + return + if 'Input/output error' not in res.stdout: + iotests.log('FAIL: unexpected error reading truncated LZFSE chunk') + iotests.log(res.stdout) + return + iotests.log('truncated LZFSE decode rejected: ok') + + +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 00000000000..03014a6d28a --- /dev/null +++ b/tests/qemu-iotests/tests/dmg-lzfse-short.out @@ -0,0 +1,2 @@ +valid LZFSE chunk reads: ok +truncated LZFSE decode rejected: ok -- 2.56.1