From nobody Fri May 3 06:14:13 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1518176427425516.648613771746; Fri, 9 Feb 2018 03:40:27 -0800 (PST) Received: from localhost ([::1]:38665 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ek72T-00089V-F2 for importer@patchew.org; Fri, 09 Feb 2018 06:40:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47724) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ek70z-0007Df-9B for qemu-devel@nongnu.org; Fri, 09 Feb 2018 06:38:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ek70w-0005RE-5i for qemu-devel@nongnu.org; Fri, 09 Feb 2018 06:38:49 -0500 Received: from fanzine.igalia.com ([91.117.99.155]:56530) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ek70v-0004KM-QP; Fri, 09 Feb 2018 06:38:46 -0500 Received: from [194.100.51.2] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1ek70K-0005yG-9D; Fri, 09 Feb 2018 12:38:08 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1ek700-00035i-OX; Fri, 09 Feb 2018 13:37:48 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Message-Id:Date:Subject:Cc:To:From; bh=I/USPTRcGIV1UXUWnlL6PKjOnPXel0PP2JEPwmQmbvQ=; b=M6Acz6IogncE+01KSz8MtVMnLIdpCpZWskpbi42xD4W3jzbjN7ufRo5+RFUt68mNLaZdXtfjJbHaiNIGC2kEEwqB11Z8sNrn33iUBiiaOAO4ovI+1Vp6cPuIWf5ihwTFfamvAQupvIGn42QrsUh2klDvmsQ2/n54QwFiAy+ywLCct1RcjkbXM6VYDWwu1WToibO/nxNGuCQZ+LjHs43l29QP9JDvVaCsI2+Uqquh5CpamxS40kyD56dY59K5bdRrH4LIevGbWxxj9lZhHlqlXk3DUkefln3K3Ru2X85jroUEV4yd0CNE2ZQjrQnLIjzlGkoStMOaLtPV/ao91th0CQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Fri, 9 Feb 2018 13:37:44 +0200 Message-Id: <20180209113744.11842-1-berto@igalia.com> X-Mailer: git-send-email 2.11.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH] qcow2: Check the L1 table parameters from all internal snapshots X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The code that reads the qcow2 snapshot table takes the offset and size of all snapshots' L1 table without doing any kind of checks. Although qcow2_snapshot_load_tmp() does verify that the table size is valid, the table offset is not checked at all. On top of that there are several other code paths that deal with internal snapshots that don't use that function or do any other check. This patch verifies that all L1 tables are correctly aligned and the size does not exceed the maximum allowed valued. The check from qcow2_snapshot_load_tmp() is removed since it's now useless (opening an image will fail before that). Signed-off-by: Alberto Garcia --- block/qcow2-snapshot.c | 14 ++++++++++---- tests/qemu-iotests/080 | 10 +++++++++- tests/qemu-iotests/080.out | 10 ++++++++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 44243e0e95..b9d46d95fa 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -121,6 +121,16 @@ int qcow2_read_snapshots(BlockDriverState *bs) offset +=3D name_size; sn->name[name_size] =3D '\0'; =20 + if (offset_into_cluster(s, sn->l1_table_offset)) { + ret =3D -EINVAL; + goto fail; + } + + if (sn->l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) { + ret =3D -EFBIG; + goto fail; + } + if (offset - s->snapshots_offset > QCOW_MAX_SNAPSHOTS_SIZE) { ret =3D -EFBIG; goto fail; @@ -704,10 +714,6 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, sn =3D &s->snapshots[snapshot_index]; =20 /* Allocate and read in the snapshot's L1 table */ - if (sn->l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) { - error_setg(errp, "Snapshot L1 table too large"); - return -EFBIG; - } new_l1_bytes =3D sn->l1_size * sizeof(uint64_t); new_l1_table =3D qemu_try_blockalign(bs->file->bs, align_offset(new_l1_bytes, 512)); diff --git a/tests/qemu-iotests/080 b/tests/qemu-iotests/080 index 1c2bd85742..dec6749908 100755 --- a/tests/qemu-iotests/080 +++ b/tests/qemu-iotests/080 @@ -171,7 +171,15 @@ poke_file "$TEST_IMG" "$offset_l2_table_0" "\x80\x00\x= 00\xff\xff\xff\x00\x00" { $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter= _testdir =20 echo -echo "=3D=3D Invalid snapshot L1 table =3D=3D" +echo "=3D=3D Invalid snapshot L1 table offset =3D=3D" +_make_test_img 64M +{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_= testdir +{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter= _testdir +poke_file "$TEST_IMG" "$offset_snap1_l1_offset" "\x00\x00\x00\x00\x00\x40\= x02\x00" +{ $QEMU_IMG convert -s test $TEST_IMG $TEST_IMG.snap; } 2>&1 | _filter_tes= tdir + +echo +echo "=3D=3D Invalid snapshot L1 table size =3D=3D" _make_test_img 64M { $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_= testdir { $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir diff --git a/tests/qemu-iotests/080.out b/tests/qemu-iotests/080.out index 6a7fda1356..86ed9567b7 100644 --- a/tests/qemu-iotests/080.out +++ b/tests/qemu-iotests/080.out @@ -59,9 +59,15 @@ wrote 512/512 bytes at offset 0 qemu-img: Could not create snapshot 'test': -27 (File too large) qemu-img: Could not create snapshot 'test': -11 (Resource temporarily unav= ailable) =20 -=3D=3D Invalid snapshot L1 table =3D=3D +=3D=3D Invalid snapshot L1 table offset =3D=3D Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D67108864 wrote 512/512 bytes at offset 0 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) -qemu-img: Failed to load snapshot: Snapshot L1 table too large +qemu-img: Could not open 'TEST_DIR/t.qcow2': Could not read snapshots: Inv= alid argument + +=3D=3D Invalid snapshot L1 table size =3D=3D +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D67108864 +wrote 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +qemu-img: Could not open 'TEST_DIR/t.qcow2': Could not read snapshots: Fil= e too large *** done --=20 2.11.0