From nobody Fri Oct 24 13:14:17 2025 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 1519922480035921.1359946849007; Thu, 1 Mar 2018 08:41:20 -0800 (PST) Received: from localhost ([::1]:58008 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erRGh-0001eo-0P for importer@patchew.org; Thu, 01 Mar 2018 11:41:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41351) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erR46-0008BU-HI for qemu-devel@nongnu.org; Thu, 01 Mar 2018 11:28:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1erR41-0004Jc-Ff for qemu-devel@nongnu.org; Thu, 01 Mar 2018 11:28:18 -0500 Received: from fanzine.igalia.com ([91.117.99.155]:54922) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1erR40-0003uP-TJ; Thu, 01 Mar 2018 11:28:13 -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 1erR3P-0003Da-IF; Thu, 01 Mar 2018 17:27:35 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1erR36-0004OU-Qr; Thu, 01 Mar 2018 18:27:16 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=uVZAwB2g/CoxGOAF7C3FcUPBisvUQ9solApNHKy/PgM=; b=SlnqMfU1toqidaIs8PGODMD/8FWmklIOIhGOlj1i+xbFJ0BKw8vmLBL2UzBxmhvI6HDGgSoy6P80UAIJKTF6r54YTVWqF/ZQ6fLcll3hbqlbxJLyLBZBKpPQ4bPRdPgAIKBdwu0yPP2UELMmMrlq2JTVpFDMKJ8HlGr0SZGtBIvWGWEv5dZtnbs8AKwLUgBNh3bSRrrFmchqjpYd3+Rdq9HZZRO/yurcsdXM2PEkR1hFR+r1PjLnmeYMcmS3Tr3x+CUIjELfQ4w6zu8POLI+POkM/cNJcEQhceyeej6WOqheTq1zKzHhdOwyRY6p4u5LbQo6BQIL484yZCwtgIvWMw==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Thu, 1 Mar 2018 18:27:09 +0200 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: 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 3/7] qcow2: Check L1 table parameters in qcow2_expand_zero_clusters() 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" This function iterates over all snapshots of a qcow2 file in order to expand all zero clusters, but it does not validate the snapshots' L1 tables first. We now have a function to take care of this, so let's use it. We can also take the opportunity to replace the sector-based bdrv_read() with bdrv_pread(). Signed-off-by: Alberto Garcia Reviewed-by: Eric Blake --- block/qcow2-cluster.c | 20 +++++++++++++------- tests/qemu-iotests/080 | 2 ++ tests/qemu-iotests/080.out | 2 ++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index e406b0f3b9..40167ac09c 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -2092,11 +2092,18 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs, } =20 for (i =3D 0; i < s->nb_snapshots; i++) { - int l1_sectors =3D DIV_ROUND_UP(s->snapshots[i].l1_size * - sizeof(uint64_t), BDRV_SECTOR_SIZE); + int l1_size2; + uint64_t *new_l1_table; =20 - uint64_t *new_l1_table =3D - g_try_realloc(l1_table, l1_sectors * BDRV_SECTOR_SIZE); + ret =3D qcow2_validate_table(bs, s->snapshots[i].l1_table_offset, + s->snapshots[i].l1_size, sizeof(uint64_= t), + QCOW_MAX_L1_SIZE, "", NULL); + if (ret < 0) { + return ret; + } + + l1_size2 =3D s->snapshots[i].l1_size * sizeof(uint64_t); + new_l1_table =3D g_try_realloc(l1_table, l1_size2); =20 if (!new_l1_table) { ret =3D -ENOMEM; @@ -2105,9 +2112,8 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs, =20 l1_table =3D new_l1_table; =20 - ret =3D bdrv_read(bs->file, - s->snapshots[i].l1_table_offset / BDRV_SECTOR_SIZE, - (void *)l1_table, l1_sectors); + ret =3D bdrv_pread(bs->file, s->snapshots[i].l1_table_offset, + l1_table, l1_size2); if (ret < 0) { goto fail; } diff --git a/tests/qemu-iotests/080 b/tests/qemu-iotests/080 index 6a10e7defa..5622604f83 100755 --- a/tests/qemu-iotests/080 +++ b/tests/qemu-iotests/080 @@ -177,6 +177,7 @@ _make_test_img 64M { $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _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 +{ $QEMU_IMG amend -o compat=3D0.10 $TEST_IMG; } 2>&1 | _filter_testdir =20 echo echo "=3D=3D Invalid snapshot L1 table size =3D=3D" @@ -185,6 +186,7 @@ _make_test_img 64M { $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir poke_file "$TEST_IMG" "$offset_snap1_l1_size" "\x10\x00\x00\x00" { $QEMU_IMG convert -s test $TEST_IMG $TEST_IMG.snap; } 2>&1 | _filter_tes= tdir +{ $QEMU_IMG amend -o compat=3D0.10 $TEST_IMG; } 2>&1 | _filter_testdir =20 # success, all done echo "*** done" diff --git a/tests/qemu-iotests/080.out b/tests/qemu-iotests/080.out index f0d9038d55..5d9030ab93 100644 --- a/tests/qemu-iotests/080.out +++ b/tests/qemu-iotests/080.out @@ -64,10 +64,12 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D671= 08864 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 offset invalid +qemu-img: Error while amending options: Invalid argument =20 =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: Failed to load snapshot: Snapshot L1 table too large +qemu-img: Error while amending options: File too large *** done --=20 2.11.0