All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 19/25] qcow2: Unaligned zero cluster in handle_alloc()
Date: Fri, 17 Nov 2017 19:16:47 +0100	[thread overview]
Message-ID: <20171117181653.20651-20-kwolf@redhat.com> (raw)
In-Reply-To: <20171117181653.20651-1-kwolf@redhat.com>

From: Max Reitz <mreitz@redhat.com>

We should check whether the cluster offset we are about to use is
actually valid; that is, whether it is aligned to cluster boundaries.

Reported-by: R. Nageswara Sastry <nasastry@in.ibm.com>
Buglink: https://bugs.launchpad.net/qemu/+bug/1728643
Buglink: https://bugs.launchpad.net/qemu/+bug/1728657
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20171110203111.7666-3-mreitz@redhat.com
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Alberto Garcia <berto@igalia.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/qcow2-cluster.c      | 13 ++++++++++++-
 tests/qemu-iotests/060     | 16 ++++++++++++++++
 tests/qemu-iotests/060.out | 10 ++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 2e072ed155..a3fec27bf9 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -1308,10 +1308,21 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
         (!*host_offset ||
          start_of_cluster(s, *host_offset) == (entry & L2E_OFFSET_MASK)))
     {
+        int preallocated_nb_clusters;
+
+        if (offset_into_cluster(s, entry & L2E_OFFSET_MASK)) {
+            qcow2_signal_corruption(bs, true, -1, -1, "Preallocated zero "
+                                    "cluster offset %#llx unaligned (guest "
+                                    "offset: %#" PRIx64 ")",
+                                    entry & L2E_OFFSET_MASK, guest_offset);
+            ret = -EIO;
+            goto fail;
+        }
+
         /* Try to reuse preallocated zero clusters; contiguous normal clusters
          * would be fine, too, but count_cow_clusters() above has limited
          * nb_clusters already to a range of COW clusters */
-        int preallocated_nb_clusters =
+        preallocated_nb_clusters =
             count_contiguous_clusters(nb_clusters, s->cluster_size,
                                       &l2_table[l2_index], QCOW_OFLAG_COPIED);
         assert(preallocated_nb_clusters > 0);
diff --git a/tests/qemu-iotests/060 b/tests/qemu-iotests/060
index 56bdf1ee2e..49bc89df38 100755
--- a/tests/qemu-iotests/060
+++ b/tests/qemu-iotests/060
@@ -321,6 +321,22 @@ echo '--- Repairing ---'
 # because the image was already marked corrupt by that point
 _check_test_img -r all
 
+echo
+echo "=== Writing to an unaligned preallocated zero cluster ==="
+echo
+
+_make_test_img 64M
+
+# Allocate the L2 table
+$QEMU_IO -c "write 0 64k" -c "discard 0 64k" "$TEST_IMG" | _filter_qemu_io
+# Pretend there is a preallocated zero cluster somewhere inside the
+# image header
+poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x00\x2a\x01"
+# Let's write to it!
+$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
+
+# Can't repair this yet (TODO: We can just deallocate the cluster)
+
 # success, all done
 echo "*** done"
 rm -f $seq.full
diff --git a/tests/qemu-iotests/060.out b/tests/qemu-iotests/060.out
index f013fe73c0..c583076808 100644
--- a/tests/qemu-iotests/060.out
+++ b/tests/qemu-iotests/060.out
@@ -307,4 +307,14 @@ The following inconsistencies were found and repaired:
 
 Double checking the fixed image now...
 No errors were found on the image.
+
+=== Writing to an unaligned preallocated zero cluster ===
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
+wrote 65536/65536 bytes at offset 0
+64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+discard 65536/65536 bytes at offset 0
+64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+qcow2: Marking image as corrupt: Preallocated zero cluster offset 0x2a00 unaligned (guest offset: 0); further corruption events will be suppressed
+write failed: Input/output error
 *** done
-- 
2.13.6

  parent reply	other threads:[~2017-11-17 18:17 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-17 18:16 [Qemu-devel] [PULL 00/25] Block layer patches for 2.11.0-rc2 Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 01/25] replication: Fix replication open fail Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 02/25] qemu-iotests: Use -nographic in 182 Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 03/25] block: Fix error path in bdrv_backing_update_filename() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 04/25] qcow2: don't permit changing encryption parameters Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 05/25] block: Deprecate bdrv_set_read_only() and users Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 06/25] qcow2: fix image corruption after committing qcow2 image into base Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 07/25] block: Fix permissions in image activation Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 08/25] iotests: test clearing unknown autoclear_features by qcow2 Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 09/25] qcow2: fix image corruption on commit with persistent bitmap Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 10/25] qapi/qnull: Add own header Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 11/25] qapi/qlist: Add qlist_append_null() macro Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 12/25] qapi: Add qobject_is_equal() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 13/25] block: qobject_is_equal() in bdrv_reopen_prepare() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 14/25] iotests: Add test for non-string option reopening Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 15/25] tests: Add check-qobject for equality tests Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 16/25] iotests: Add test for failing qemu-img commit Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 17/25] qcow2: reject unaligned offsets in write compressed Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 18/25] qcow2: check_errors are fatal Kevin Wolf
2017-11-17 18:16 ` Kevin Wolf [this message]
2017-11-17 18:16 ` [Qemu-devel] [PULL 20/25] block: Guard against NULL bs->drv Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 21/25] qcow2: Add bounds check to get_refblock_offset() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 22/25] qcow2: Refuse to get unaligned offsets from cache Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 23/25] qcow2: Fix overly broad madvise() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 24/25] block: Make bdrv_next() keep strong references Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 25/25] iotests: Make 087 pass without AIO enabled Kevin Wolf
2017-11-20 14:53 ` [Qemu-devel] [PULL 00/25] Block layer patches for 2.11.0-rc2 Peter Maydell
2017-11-20 15:24   ` Kevin Wolf
2017-11-20 17:16     ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171117181653.20651-20-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.