From nobody Sun Oct 5 21:10:02 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534301893137708.4650384526077; Tue, 14 Aug 2018 19:58:13 -0700 (PDT) Received: from localhost ([::1]:47001 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fpm0h-0002eF-MH for importer@patchew.org; Tue, 14 Aug 2018 22:58:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55026) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fplz1-0001hH-1N for qemu-devel@nongnu.org; Tue, 14 Aug 2018 22:56:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fplyz-00077u-L3 for qemu-devel@nongnu.org; Tue, 14 Aug 2018 22:56:26 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48662 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fplyw-000775-GJ; Tue, 14 Aug 2018 22:56:22 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1879640216EE; Wed, 15 Aug 2018 02:56:22 +0000 (UTC) Received: from red.redhat.com (ovpn-120-153.rdu2.redhat.com [10.10.120.153]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4F619105543D; Wed, 15 Aug 2018 02:56:20 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Tue, 14 Aug 2018 21:56:13 -0500 Message-Id: <20180815025614.53588-2-eblake@redhat.com> In-Reply-To: <20180815025614.53588-1-eblake@redhat.com> References: <20180815025614.53588-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Wed, 15 Aug 2018 02:56:22 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Wed, 15 Aug 2018 02:56:22 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'eblake@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 1/2] qemu-img: Fix dd with skip= and count= 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 , fullmanet@gmail.com, qemu-stable@nongnu.org, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When both skip=3D and count=3D are active, qemu-img dd was not copying enough data. It didn't help that the code made the same check for dd.flags & C_SKIP in two separate places. Compute 'size' as the amount of bytes to be read, and 'end' as the offset to end at, rather than trying to cram both meanings into a single variable (which only worked as long as we had at most one of those two limiting factors to worry about, but not both). Enhance the test to cover more combinations, and expose the problem. Signed-off-by: Eric Blake CC: qemu-stable@nongnu.org --- qemu-img.c | 39 ++++++++++++++++--------------------- tests/qemu-iotests/160 | 9 ++++++--- tests/qemu-iotests/160.out | 48 ++++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 70 insertions(+), 26 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 1acddf693c6..d72f0f0ec94 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -4398,7 +4398,7 @@ static int img_dd(int argc, char **argv) const char *out_fmt =3D "raw"; const char *fmt =3D NULL; int64_t size =3D 0; - int64_t block_count =3D 0, out_pos, in_pos; + int64_t block_count =3D 0, out_pos, in_pos, end; bool force_share =3D false; struct DdInfo dd =3D { .flags =3D 0, @@ -4559,19 +4559,23 @@ static int img_dd(int argc, char **argv) goto out; } + /* Overflow means the specified offset is beyond input image's size */ + if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || + size < in.bsz * in.offset)) { + size =3D 0; + error_report("%s: cannot skip to specified offset", in.filename); + } else { + size -=3D in.offset * in.bsz; + in_pos =3D in.offset * in.bsz; + } + if (dd.flags & C_COUNT && dd.count <=3D INT64_MAX / in.bsz && dd.count * in.bsz < size) { size =3D dd.count * in.bsz; } - /* Overflow means the specified offset is beyond input image's size */ - if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || - size < in.bsz * in.offset)) { - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); - } else { - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, - size - in.bsz * in.offset, &error_abort); - } + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort); + end =3D size + in_pos; ret =3D bdrv_create(drv, out.filename, opts, &local_err); if (ret < 0) { @@ -4595,24 +4599,13 @@ static int img_dd(int argc, char **argv) goto out; } - if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || - size < in.offset * in.bsz)) { - /* We give a warning if the skip option is bigger than the input - * size and create an empty output disk image (i.e. like dd(1)). - */ - error_report("%s: cannot skip to specified offset", in.filename); - in_pos =3D size; - } else { - in_pos =3D in.offset * in.bsz; - } - in.buf =3D g_new(uint8_t, in.bsz); - for (out_pos =3D 0; in_pos < size; block_count++) { + for (out_pos =3D 0; in_pos < end; block_count++) { int in_ret, out_ret; - if (in_pos + in.bsz > size) { - in_ret =3D blk_pread(blk1, in_pos, in.buf, size - in_pos); + if (in_pos + in.bsz > end) { + in_ret =3D blk_pread(blk1, in_pos, in.buf, end - in_pos); } else { in_ret =3D blk_pread(blk1, in_pos, in.buf, in.bsz); } diff --git a/tests/qemu-iotests/160 b/tests/qemu-iotests/160 index 5c910e5bfc1..48380a3aafc 100755 --- a/tests/qemu-iotests/160 +++ b/tests/qemu-iotests/160 @@ -44,6 +44,7 @@ _supported_os Linux TEST_SKIP_BLOCKS=3D"1 2 30 30K" for skip in $TEST_SKIP_BLOCKS; do + for count in '' 'count=3D1 '; do echo echo "=3D=3D Creating image =3D=3D" @@ -53,17 +54,19 @@ for skip in $TEST_SKIP_BLOCKS; do $QEMU_IO -c "write -P 0xa 24 512k" "$TEST_IMG" | _filter_qemu_io echo - echo "=3D=3D Converting the image with dd with skip=3D$skip =3D=3D" + echo "=3D=3D Converting the image with dd with ${count}skip=3D$skip = =3D=3D" - $QEMU_IMG dd if=3D"$TEST_IMG" of=3D"$TEST_IMG.out" skip=3D"$skip" -O "= $IMGFMT" \ + $QEMU_IMG dd if=3D"$TEST_IMG" of=3D"$TEST_IMG.out" $count skip=3D"$ski= p" -O "$IMGFMT" \ 2> /dev/null TEST_IMG=3D"$TEST_IMG.out" _check_test_img - dd if=3D"$TEST_IMG" of=3D"$TEST_IMG.out.dd" skip=3D"$skip" status=3Dno= ne + dd if=3D"$TEST_IMG" of=3D"$TEST_IMG.out.dd" $count skip=3D"$skip" stat= us=3Dnone echo echo "=3D=3D Compare the images with qemu-img compare =3D=3D" $QEMU_IMG compare "$TEST_IMG.out.dd" "$TEST_IMG.out" + rm "$TEST_IMG.out.dd" + done done echo diff --git a/tests/qemu-iotests/160.out b/tests/qemu-iotests/160.out index 9cedc803566..6147a8493d6 100644 --- a/tests/qemu-iotests/160.out +++ b/tests/qemu-iotests/160.out @@ -18,6 +18,18 @@ No errors were found on the image. wrote 524288/524288 bytes at offset 24 512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +=3D=3D Converting the image with dd with count=3D1 skip=3D1 =3D=3D +No errors were found on the image. + +=3D=3D Compare the images with qemu-img compare =3D=3D +Images are identical. + +=3D=3D Creating image =3D=3D +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D1048576 +No errors were found on the image. +wrote 524288/524288 bytes at offset 24 +512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + =3D=3D Converting the image with dd with skip=3D2 =3D=3D No errors were found on the image. @@ -30,6 +42,18 @@ No errors were found on the image. wrote 524288/524288 bytes at offset 24 512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +=3D=3D Converting the image with dd with count=3D1 skip=3D2 =3D=3D +No errors were found on the image. + +=3D=3D Compare the images with qemu-img compare =3D=3D +Images are identical. + +=3D=3D Creating image =3D=3D +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D1048576 +No errors were found on the image. +wrote 524288/524288 bytes at offset 24 +512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + =3D=3D Converting the image with dd with skip=3D30 =3D=3D No errors were found on the image. @@ -42,10 +66,34 @@ No errors were found on the image. wrote 524288/524288 bytes at offset 24 512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +=3D=3D Converting the image with dd with count=3D1 skip=3D30 =3D=3D +No errors were found on the image. + +=3D=3D Compare the images with qemu-img compare =3D=3D +Images are identical. + +=3D=3D Creating image =3D=3D +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D1048576 +No errors were found on the image. +wrote 524288/524288 bytes at offset 24 +512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + =3D=3D Converting the image with dd with skip=3D30K =3D=3D No errors were found on the image. =3D=3D Compare the images with qemu-img compare =3D=3D Images are identical. +=3D=3D Creating image =3D=3D +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D1048576 +No errors were found on the image. +wrote 524288/524288 bytes at offset 24 +512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +=3D=3D Converting the image with dd with count=3D1 skip=3D30K =3D=3D +No errors were found on the image. + +=3D=3D Compare the images with qemu-img compare =3D=3D +Images are identical. + *** done --=20 2.14.4