From nobody Wed Nov 12 01:56:05 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; spf=pass (zoho.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=1567518525; cv=none; d=zoho.com; s=zohoarc; b=F8iMZr37qIAjBqaNkRQdkYezjeDv7e6LKlG6SxWWlE5BulIVUPNDSvxdmx+ao6/fGBnvSC1eZR4axis7ySlMuFbJ3GGuTK6qK7IMXCwIl8toaRakj43vtqMtdPVJ+GEprDSexQ/mRMK36KC3JCb4mTAphGsFuqtU+xLoY9r/Hd0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567518525; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=nv/aO7Oej6tpwTVE1kwcDwjxx+wBRF/44Uub+1pgSLM=; b=YXWh1/l35MLZWXbmgv1FA3pYn3k6JvnuZs9cK1VE1SApzjZpXnclixCSe7ljmYPzioDvSMdvMt/r5lzuLO4UqhpHLMLB4qOq/Pxph4FtM2/oGqujztX0u/QlGMEjQoIUhSc5tsQZ0tUPxoF1sfOoPXNIlC1sn1yndt+bGw3agwY= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1567518525337863.879333379447; Tue, 3 Sep 2019 06:48:45 -0700 (PDT) Received: from localhost ([::1]:46420 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59Ap-0008QV-0p for importer@patchew.org; Tue, 03 Sep 2019 09:48:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55593) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i58y8-0001PX-VL for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:35:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i58y7-0005oV-6G for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:35:36 -0400 Received: from kerio.kamp.de ([195.62.97.192]:47940) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i58y6-0005ik-Rh for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:35:35 -0400 Received: from submission.kamp.de ([195.62.97.28]) by kerio.kamp.de with ESMTPS (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)) for qemu-devel@nongnu.org; Tue, 3 Sep 2019 15:35:23 +0200 Received: (qmail 38703 invoked from network); 3 Sep 2019 13:35:26 -0000 Received: from lieven-pc.kamp-intra.net (HELO lieven-pc) (relay@kamp.de@::ffff:172.21.12.60) by submission.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted) ESMTPA; 3 Sep 2019 13:35:26 -0000 Received: by lieven-pc (Postfix, from userid 1060) id 4BD4513D85C; Tue, 3 Sep 2019 15:35:26 +0200 (CEST) X-Footer: a2FtcC5kZQ== From: Peter Lieven To: qemu-block@nongnu.org Date: Tue, 3 Sep 2019 15:35:24 +0200 Message-Id: <20190903133524.11755-1-pl@kamp.de> X-Mailer: git-send-email 2.17.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 195.62.97.192 Subject: [Qemu-devel] [PATCH V3] block/vhdx: add check for truncated image files 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: kwolf@redhat.com, codyprime@gmail.com, Peter Lieven , qemu-devel@nongnu.org, mreitz@redhat.com, jhf@kamp.de Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" qemu is currently not able to detect truncated vhdx image files. Add a basic check if all allocated blocks are reachable at open and report all errors during bdrv_co_check. Signed-off-by: Peter Lieven --- V3: - check for bdrv_getlength failure [Kevin] - use uint32_t for i [Kevin] - check for BAT entry overflow [Kevin] - break on !errcnt in second check V2: - add error reporting [Kevin] - use bdrv_getlength instead of bdrv_get_allocated_file_size [Kevin] - factor out BAT entry check and add error reporting for region overlaps - already check on vhdx_open block/vhdx.c | 102 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 85 insertions(+), 17 deletions(-) diff --git a/block/vhdx.c b/block/vhdx.c index 6a09d0a55c..253e32d524 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -24,6 +24,7 @@ #include "qemu/option.h" #include "qemu/crc32c.h" #include "qemu/bswap.h" +#include "qemu/error-report.h" #include "vhdx.h" #include "migration/blocker.h" #include "qemu/uuid.h" @@ -235,6 +236,9 @@ static int vhdx_region_check(BDRVVHDXState *s, uint64_t= start, uint64_t length) end =3D start + length; QLIST_FOREACH(r, &s->regions, entries) { if (!((start >=3D r->end) || (end <=3D r->start))) { + error_report("VHDX region %" PRIu64 "-%" PRIu64 " overlaps wit= h " + "region %" PRIu64 "-%." PRIu64, start, end, r->st= art, + r->end); ret =3D -EINVAL; goto exit; } @@ -877,6 +881,77 @@ static void vhdx_calc_bat_entries(BDRVVHDXState *s) =20 } =20 +static int vhdx_check_bat_entries(BlockDriverState *bs, int *errcnt) +{ + BDRVVHDXState *s =3D bs->opaque; + int64_t image_file_size =3D bdrv_getlength(bs->file->bs); + uint64_t payblocks =3D s->chunk_ratio; + uint32_t i; + int ret =3D 0; + + if (image_file_size < 0) { + error_report("Could not determinate VHDX image file size."); + return image_file_size; + } + + for (i =3D 0; i < s->bat_entries; i++) { + if ((s->bat[i] & VHDX_BAT_STATE_BIT_MASK) =3D=3D + PAYLOAD_BLOCK_FULLY_PRESENT) { + uint64_t offset =3D s->bat[i] & VHDX_BAT_FILE_OFF_MASK; + /* + * Check for BAT entry overflow. + */ + if (offset > INT64_MAX - s->block_size) { + error_report("VHDX BAT entry %" PRIu32 " offset overflow."= , i); + ret =3D -EINVAL; + if (!errcnt) { + break; + } + (*errcnt)++; + } + /* + * Check if fully allocated BAT entries do not reside after + * end of the image file. + */ + if (offset + s->block_size > image_file_size) { + error_report("VHDX BAT entry %" PRIu32 " offset points aft= er " + "end of file. Image has probably been truncat= ed.", + i); + ret =3D -EINVAL; + if (!errcnt) { + break; + } + (*errcnt)++; + } + + /* + * verify populated BAT field file offsets against + * region table and log entries + */ + if (payblocks--) { + /* payload bat entries */ + int ret2; + ret2 =3D vhdx_region_check(s, offset, s->block_size); + if (ret2 < 0) { + ret =3D -EINVAL; + if (!errcnt) { + break; + } + (*errcnt)++; + } + } else { + payblocks =3D s->chunk_ratio; + /* + * Once differencing files are supported, verify sector bi= tmap + * blocks here + */ + } + } + } + + return ret; +} + static void vhdx_close(BlockDriverState *bs) { BDRVVHDXState *s =3D bs->opaque; @@ -981,25 +1056,15 @@ static int vhdx_open(BlockDriverState *bs, QDict *op= tions, int flags, goto fail; } =20 - uint64_t payblocks =3D s->chunk_ratio; - /* endian convert, and verify populated BAT field file offsets against - * region table and log entries */ + /* endian convert populated BAT field entires */ for (i =3D 0; i < s->bat_entries; i++) { s->bat[i] =3D le64_to_cpu(s->bat[i]); - if (payblocks--) { - /* payload bat entries */ - if ((s->bat[i] & VHDX_BAT_STATE_BIT_MASK) =3D=3D - PAYLOAD_BLOCK_FULLY_PRESENT) { - ret =3D vhdx_region_check(s, s->bat[i] & VHDX_BAT_FILE_OFF= _MASK, - s->block_size); - if (ret < 0) { - goto fail; - } - } - } else { - payblocks =3D s->chunk_ratio; - /* Once differencing files are supported, verify sector bitmap - * blocks here */ + } + + if (!(flags & BDRV_O_CHECK)) { + ret =3D vhdx_check_bat_entries(bs, NULL); + if (ret < 0) { + goto fail; } } =20 @@ -2072,6 +2137,9 @@ static int coroutine_fn vhdx_co_check(BlockDriverStat= e *bs, if (s->log_replayed_on_open) { result->corruptions_fixed++; } + + vhdx_check_bat_entries(bs, &result->corruptions); + return 0; } =20 --=20 2.17.1