From nobody Thu May 16 16:31:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1586977443; cv=none; d=zohomail.com; s=zohoarc; b=BgmCs1n5KrlyO/f9XbrMrgfrUwtoDsfsfo5qFqJY4A5Lw5u/hBckPW4ONoLaAkix6OaOJWQtgPTpNXNI3aXbvj0/KDvSbm3MeU0xf2UZnDdCEIGyzhSeq+uquNjM3VGlV6pO/R7s123EnE+oX60exIo5mhTm5g1nPjpOX7G6tN4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586977443; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=KfYvThIihR3Lk8FkZLDUEk5qUchvsn1nH/dYX8v+CTg=; b=GSwwKjn7N34/PkVVc0HgEFb1dme+fyTaM4/lvhwKoH1HIAigBZ89wxTM/75pyGWWMTyMENJFx+6KRsr//B7X80//he6TBVl7aNbtLiagUTk3jIABBTi/fQ0OdS4nt3nZficfjwcO2pmJzY5PM5UySG963SbEtu/1Ymsk1dg3UmQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586977442811868.167502792554; Wed, 15 Apr 2020 12:04:02 -0700 (PDT) Received: from localhost ([::1]:54134 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jOnKK-000698-Nn for importer@patchew.org; Wed, 15 Apr 2020 15:04:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55580) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jOnJ9-0005cZ-AY for qemu-devel@nongnu.org; Wed, 15 Apr 2020 15:02:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jOnJ7-0003Yb-Ue for qemu-devel@nongnu.org; Wed, 15 Apr 2020 15:02:47 -0400 Received: from fanzine.igalia.com ([178.60.130.6]:49459) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jOnJ7-0003Ps-BS; Wed, 15 Apr 2020 15:02:45 -0400 Received: from [81.0.35.113] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1jOnIn-0005dC-E9; Wed, 15 Apr 2020 21:02:25 +0200 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1jOnIZ-0005VO-HR; Wed, 15 Apr 2020 21:02:11 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From; bh=KfYvThIihR3Lk8FkZLDUEk5qUchvsn1nH/dYX8v+CTg=; b=iXnGv/q0ocm1qMoppKBHlj9ypBu8KHvDa8AZCf4RIxm5G8YUZbMrLRHSor8O9i2hz3rhdrF9IXU0dw+spjeC36XiwRdlhScUV7dDlTubBJ08skIWVbWs7MunPHz4NZHc6fNQYBpVufS9YabqRAIcUfCV9Nv2jQVS9l3iRUbPRXKVQsULWc39yK53t1JQVcs7RusoRxBvM0hCI/tKWYVtt5yfPyx8GjkFjDb4ALHwi6AVWVyFksH/RbvOm7Rdh7HVHjmrNV8vQ7AEQHGX2ERS7Pa6gFLYkTHMKyubCRMrt4eSfCgG7SWjbygdVlr5y9At1gtu1zKhoaTDxcbKKe1yyQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH for-5.1] qcow2: Don't open images with a backing file and the data-file-raw bit Date: Wed, 15 Apr 2020 21:02:07 +0200 Message-Id: <20200415190207.21118-1-berto@igalia.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 178.60.130.6 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 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) Content-Type: text/plain; charset="utf-8" Although we cannot create these images with qemu-img it is still possible to do it using an external tool. QEMU should refuse to open them until the data-file-raw bit is cleared with 'qemu-img check'. Signed-off-by: Alberto Garcia Reviewed-by: Eric Blake --- block/qcow2.c | 39 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/244 | 13 +++++++++++++ tests/qemu-iotests/244.out | 14 ++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/block/qcow2.c b/block/qcow2.c index b524b0c53f..fcc3810391 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -588,16 +588,49 @@ static void qcow2_add_check_result(BdrvCheckResult *o= ut, } } =20 +static int qcow2_check_header(BlockDriverState *bs, BdrvCheckResult *resul= t, + BdrvCheckMode fix) +{ + BDRVQcow2State *s =3D bs->opaque; + int ret; + + if (bs->backing && data_file_is_raw(bs)) { + fprintf(stderr, "%s header: data-file-raw cannot be set " + "when there is a backing file.\n", + fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR in"); + if (fix & BDRV_FIX_ERRORS) { + s->autoclear_features &=3D ~QCOW2_AUTOCLEAR_DATA_FILE_RAW; + ret =3D qcow2_update_header(bs); + if (ret < 0) { + result->check_errors++; + return ret; + } + result->corruptions_fixed++; + } else { + result->corruptions++; + } + } + + return 0; +} + static int coroutine_fn qcow2_co_check_locked(BlockDriverState *bs, BdrvCheckResult *result, BdrvCheckMode fix) { + BdrvCheckResult header_res =3D {}; BdrvCheckResult snapshot_res =3D {}; BdrvCheckResult refcount_res =3D {}; int ret; =20 memset(result, 0, sizeof(*result)); =20 + ret =3D qcow2_check_header(bs, &header_res, fix); + qcow2_add_check_result(result, &header_res, false); + if (ret < 0) { + return ret; + } + ret =3D qcow2_check_read_snapshot_table(bs, &snapshot_res, fix); if (ret < 0) { qcow2_add_check_result(result, &snapshot_res, false); @@ -1604,6 +1637,12 @@ static int coroutine_fn qcow2_do_open(BlockDriverSta= te *bs, QDict *options, =20 /* read the backing file name */ if (header.backing_file_offset !=3D 0) { + if (data_file_is_raw(bs) && (!(flags & BDRV_O_CHECK))) { + error_setg(errp, "data-file-raw cannot be set when " + "there is a backing file"); + ret =3D -EINVAL; + goto fail; + } len =3D header.backing_file_size; if (len > MIN(1023, s->cluster_size - header.backing_file_offset) = || len >=3D sizeof(bs->backing_file)) { diff --git a/tests/qemu-iotests/244 b/tests/qemu-iotests/244 index 2ec1815e6f..159941acd4 100755 --- a/tests/qemu-iotests/244 +++ b/tests/qemu-iotests/244 @@ -211,6 +211,19 @@ $QEMU_IMG amend -f $IMGFMT -o "data_file=3Dblkdebug::$= TEST_IMG.data" "$TEST_IMG" $QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG" $QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG" =20 +echo +echo "=3D=3D=3D Using and repairing an image with backing file and the dat= a_file_raw bit =3D=3D=3D" +echo + +# Create an image with a backing file and an external data file +TEST_IMG_FILE=3D"$TEST_IMG.base" _make_test_img 1M +_make_test_img -o "data_file=3D$TEST_IMG.data" -b "$TEST_IMG.base" +# Set 'data_file_raw' directly on the header (qemu-img amend won't let us) +poke_file "$TEST_IMG" 95 "\x02" +# Trying to open the image should produce an error +$QEMU_IMG info "$TEST_IMG" 2>&1 | _filter_testdir +_check_test_img -r all + # success, all done echo "*** done" rm -f $seq.full diff --git a/tests/qemu-iotests/244.out b/tests/qemu-iotests/244.out index 56329deb4b..cab367dfb5 100644 --- a/tests/qemu-iotests/244.out +++ b/tests/qemu-iotests/244.out @@ -128,4 +128,18 @@ Offset Length Mapped to File Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D67108864 data_file=3DT= EST_DIR/t.IMGFMT.data Images are identical. Images are identical. + +=3D=3D=3D Using and repairing an image with backing file and the data_file= _raw bit =3D=3D=3D + +Formatting 'TEST_DIR/t.IMGFMT.base', fmt=3DIMGFMT size=3D1048576 +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D1048576 backing_file= =3DTEST_DIR/t.IMGFMT.base data_file=3DTEST_DIR/t.IMGFMT.data +qemu-img: Could not open 'TEST_DIR/t.qcow2': data-file-raw cannot be set w= hen there is a backing file +Repairing header: data-file-raw cannot be set when there is a backing file. +The following inconsistencies were found and repaired: + + 0 leaked clusters + 1 corruptions + +Double checking the fixed image now... +No errors were found on the image. *** done --=20 2.20.1