From nobody Thu Apr 25 10:22:34 2024 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; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1570702238; cv=none; d=zoho.com; s=zohoarc; b=LuBOBlvDoTumzvloKrFZlhlfEar3VnKFPoKPw7psn1iBSksKYB8qOKsX2ANSOFqXAlpqWlFau54ZgjVICzHgeMczGjc3F0nLa4B/DHz4R8FC2Qqm/ETFmY57pLtRu7+7PBmurxihGiwuh3EVsJjrRTbx30QXzP3HxLapM7tXI6o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1570702238; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ynFMyZ7owRvkjBsy9a4enYNlMOvALvIm+wzYAK8wWlc=; b=MgwtlT9lBDJc7Bgtvg/N80iv6UIsSSnZZX0Ed5F2LBB0JbEGFoxIebvObXFcvETycHfwSwEE5BkgpnJ66ufZInitPf5AN9a51z+mwyxpXNL6Kjqhh1X5wIb02Djr+I50JwBAxxAEuqYuvKdPWNiXOTaImqnLEAncAN5sdxSIxW4= 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; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1570702238016345.73973745102455; Thu, 10 Oct 2019 03:10:38 -0700 (PDT) Received: from localhost ([::1]:35758 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iIVP3-0005Oy-0q for importer@patchew.org; Thu, 10 Oct 2019 06:10:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41082) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iIVNc-0003uU-8r for qemu-devel@nongnu.org; Thu, 10 Oct 2019 06:09:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iIVNb-00069x-5K for qemu-devel@nongnu.org; Thu, 10 Oct 2019 06:09:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40224) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iIVNY-00067q-Rg; Thu, 10 Oct 2019 06:09:04 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1C787308FBA6; Thu, 10 Oct 2019 10:09:04 +0000 (UTC) Received: from localhost (unknown [10.36.118.5]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8FC4D5C223; Thu, 10 Oct 2019 10:09:03 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Subject: [PATCH 1/2] qcow2: Limit total allocation range to INT_MAX Date: Thu, 10 Oct 2019 12:08:57 +0200 Message-Id: <20191010100858.1261-2-mreitz@redhat.com> In-Reply-To: <20191010100858.1261-1-mreitz@redhat.com> References: <20191010100858.1261-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Thu, 10 Oct 2019 10:09:04 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 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 , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" When the COW areas are included, the size of an allocation can exceed INT_MAX. This is kind of limited by handle_alloc() in that it already caps avail_bytes at INT_MAX, but the number of clusters still reflects the original length. This can have all sorts of effects, ranging from the storage layer write call failing to image corruption. (If there were no image corruption, then I suppose there would be data loss because the .cow_end area is forced to be empty, even though there might be something we need to COW.) Fix all of it by limiting nb_clusters so the equivalent number of bytes will not exceed INT_MAX. Cc: qemu-stable@nongnu.org Signed-off-by: Max Reitz Reviewed-by: Eric Blake Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- block/qcow2-cluster.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 8d5fa1539c..8982b7b762 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -1330,6 +1330,9 @@ static int handle_alloc(BlockDriverState *bs, uint64_= t guest_offset, nb_clusters =3D MIN(nb_clusters, s->l2_slice_size - l2_index); assert(nb_clusters <=3D INT_MAX); =20 + /* Limit total allocation byte count to INT_MAX */ + nb_clusters =3D MIN(nb_clusters, INT_MAX >> s->cluster_bits); + /* Find L2 entry for the first involved cluster */ ret =3D get_cluster_table(bs, guest_offset, &l2_slice, &l2_index); if (ret < 0) { @@ -1412,7 +1415,7 @@ static int handle_alloc(BlockDriverState *bs, uint64_= t guest_offset, * request actually writes to (excluding COW at the end) */ uint64_t requested_bytes =3D *bytes + offset_into_cluster(s, guest_off= set); - int avail_bytes =3D MIN(INT_MAX, nb_clusters << s->cluster_bits); + int avail_bytes =3D nb_clusters << s->cluster_bits; int nb_bytes =3D MIN(requested_bytes, avail_bytes); QCowL2Meta *old_m =3D *m; =20 --=20 2.21.0 From nobody Thu Apr 25 10:22:34 2024 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; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1570702235; cv=none; d=zoho.com; s=zohoarc; b=L9KTe1bAFdx6eGqkrDCLRFq4Mq9TdYMeA3/NfMdzt4yDMMxEpAKN5cayxanuHzpsXmO6z8q2I+VKjcB7E3Rl2mWeDIbIs2xbYVlnh5VUBj6ORbXN6q3Rbk5z2W5QjlVoXSOnICi57UAQAIKVBXasdTtqdYhdilkRwAX6EfcspdE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1570702235; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=GBOJsGQy18P17PW7Umrv2Uxd6mXcc8meLsm2BttgJB4=; b=TPONkFbT9YYUH0sL11k2DNU9i6mO7D61yO3lzS8YfNjs/hGEWRKB9ZMLhG160QUoDCpeG4qGj5rkuGlEBfn9pAdBELMmEje8OXctsad0aswBPRBGEJVjnMKbXe2c8BMOf/4Jr1LFzT3qtIg7HUEdWCr5IB0JNP85nniwaUSXVdg= 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; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1570702235933319.64218585852757; Thu, 10 Oct 2019 03:10:35 -0700 (PDT) Received: from localhost ([::1]:35756 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iIVP0-0005NV-KI for importer@patchew.org; Thu, 10 Oct 2019 06:10:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41106) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iIVNg-00042k-R1 for qemu-devel@nongnu.org; Thu, 10 Oct 2019 06:09:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iIVNf-0006BG-5l for qemu-devel@nongnu.org; Thu, 10 Oct 2019 06:09:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60730) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iIVNb-00069u-KL; Thu, 10 Oct 2019 06:09:07 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DAD2C30BCBA0; Thu, 10 Oct 2019 10:09:06 +0000 (UTC) Received: from localhost (unknown [10.36.118.5]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 60C1319D70; Thu, 10 Oct 2019 10:09:06 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Subject: [PATCH 2/2] iotests: Test large write request to qcow2 file Date: Thu, 10 Oct 2019 12:08:58 +0200 Message-Id: <20191010100858.1261-3-mreitz@redhat.com> In-Reply-To: <20191010100858.1261-1-mreitz@redhat.com> References: <20191010100858.1261-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Thu, 10 Oct 2019 10:09:06 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 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 , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Without HEAD^, the following happens when you attempt a large write request to a qcow2 file such that the number of bytes covered by all clusters involved in a single allocation will exceed INT_MAX: (A) handle_alloc_space() decides to fill the whole area with zeroes and fails because bdrv_co_pwrite_zeroes() fails (the request is too large). (B) If handle_alloc_space() does not do anything, but merge_cow() decides that the requests can be merged, it will create a too long IOV that later cannot be written. (C) Otherwise, all parts will be written separately, so those requests will work. In either B or C, though, qcow2_alloc_cluster_link_l2() will have an overflow: We use an int (i) to iterate over nb_clusters, and then calculate the L2 entry based on "i << s->cluster_bits" -- which will overflow if the range covers more than INT_MAX bytes. This then leads to image corruption because the L2 entry will be wrong (it will be recognized as a compressed cluster). Even if that were not the case, the .cow_end area would be empty (because handle_alloc() will cap avail_bytes and nb_bytes at INT_MAX, so their difference (which is the .cow_end size) will be 0). So this test checks that on such large requests, the image will not be corrupted. Unfortunately, we cannot check whether COW will be handled correctly, because that data is discarded when it is written to null-co (but we have to use null-co, because writing 2 GB of data in a test is not quite reasonable). Signed-off-by: Max Reitz Reviewed-by: Eric Blake --- tests/qemu-iotests/268 | 83 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/268.out | 9 +++++ tests/qemu-iotests/group | 1 + 3 files changed, 93 insertions(+) create mode 100755 tests/qemu-iotests/268 create mode 100644 tests/qemu-iotests/268.out diff --git a/tests/qemu-iotests/268 b/tests/qemu-iotests/268 new file mode 100755 index 0000000000..b9a12b908c --- /dev/null +++ b/tests/qemu-iotests/268 @@ -0,0 +1,83 @@ +#!/usr/bin/env bash +# +# Test large write to a qcow2 image +# +# Copyright (C) 2019 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 . +# + +seq=3D$(basename "$0") +echo "QA output created by $seq" + +status=3D1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +# This is a qcow2 regression test +_supported_fmt qcow2 +_supported_proto file +_supported_os Linux + +# We use our own external data file and our own cluster size, and we +# require v3 images +_unsupported_imgopts data_file cluster_size 'compat=3D0.10' + + +# We need a backing file so that handle_alloc_space() will not do +# anything. (If it were to do anything, it would simply fail its +# write-zeroes request because the request range is too large.) +TEST_IMG=3D"$TEST_IMG.base" _make_test_img 4G +$QEMU_IO -c 'write 0 512' "$TEST_IMG.base" | _filter_qemu_io + +# (Use .orig because _cleanup_test_img will remove that file) +# We need a large cluster size, see below for why (above the $QEMU_IO +# invocation) +_make_test_img -o cluster_size=3D2M,data_file=3D"$TEST_IMG.orig" \ + -b "$TEST_IMG.base" 4G + +# We want a null-co as the data file, because it allows us to quickly +# "write" 2G of data without using any space. +# (qemu-img create does not like it, though, because null-co does not +# support image creation.) +$QEMU_IMG amend -o data_file=3D"json:{'driver':'null-co',,'size':'42949672= 96'}" \ + "$TEST_IMG" + +# This gives us a range of: +# 2^31 - 512 + 768 - 1 =3D 2^31 + 255 > 2^31 +# until the beginning of the end COW block. (The total allocation +# size depends on the cluster size, but all that is important is that +# it exceeds INT_MAX.) +# +# 2^31 - 512 is the maximum request size. We want this to result in a +# single allocation, and because the qcow2 driver splits allocations +# on L2 boundaries, we need large L2 tables; hence the cluster size of +# 2 MB. (Anything from 256 kB should work, though, because then one L2 +# table covers 8 GB.) +$QEMU_IO -c "write 768 $((2 ** 31 - 512))" "$TEST_IMG" | _filter_qemu_io + +_check_test_img + +# success, all done +echo "*** done" +rm -f $seq.full +status=3D0 diff --git a/tests/qemu-iotests/268.out b/tests/qemu-iotests/268.out new file mode 100644 index 0000000000..35d4f9e3e9 --- /dev/null +++ b/tests/qemu-iotests/268.out @@ -0,0 +1,9 @@ +QA output created by 268 +Formatting 'TEST_DIR/t.IMGFMT.base', fmt=3DIMGFMT size=3D4294967296 +wrote 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D4294967296 backing_fil= e=3DTEST_DIR/t.IMGFMT.base data_file=3DTEST_DIR/t.IMGFMT.orig +wrote 2147483136/2147483136 bytes at offset 768 +2 GiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +No errors were found on the image. +*** done diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index 5805a79d9e..4c37b8acec 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -278,3 +278,4 @@ 265 rw auto quick 266 rw quick 267 rw auto quick snapshot +268 rw backing quick --=20 2.21.0