thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
0a122b
From cac9ac8f0173f95893fbc62fa67fcf04e4c76f5f Mon Sep 17 00:00:00 2001
0a122b
From: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
Date: Tue, 25 Mar 2014 14:23:15 +0100
0a122b
Subject: [PATCH 08/49] size off-by-one
0a122b
0a122b
RH-Author: Kevin Wolf <kwolf@redhat.com>
0a122b
Message-id: <1395753835-7591-9-git-send-email-kwolf@redhat.com>
0a122b
Patchwork-id: n/a
0a122b
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 08/48] block/cloop: fix offsets[] size off-by-one
0a122b
Bugzilla: 1066691
0a122b
RH-Acked-by: Jeff Cody <jcody@redhat.com>
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
0a122b
0a122b
From: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
0a122b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1066691
0a122b
Upstream status: Series embargoed
0a122b
0a122b
cloop stores the number of compressed blocks in the n_blocks header
0a122b
field. The file actually contains n_blocks + 1 offsets, where the extra
0a122b
offset is the end-of-file offset.
0a122b
0a122b
The following line in cloop_read_block() results in an out-of-bounds
0a122b
offsets[] access:
0a122b
0a122b
uint32_t bytes = s->offsets[block_num + 1] - s->offsets[block_num];
0a122b
0a122b
This patch allocates and loads the extra offset so that
0a122b
cloop_read_block() works correctly when the last block is accessed.
0a122b
0a122b
Notice that we must free s->offsets[] unconditionally now since there is
0a122b
always an end-of-file offset.
0a122b
0a122b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
---
0a122b
 block/cloop.c              |   12 +++++-------
0a122b
 tests/qemu-iotests/075     |    5 +++++
0a122b
 tests/qemu-iotests/075.out |    4 ++++
0a122b
 3 files changed, 14 insertions(+), 7 deletions(-)
0a122b
0a122b
diff --git a/block/cloop.c b/block/cloop.c
0a122b
index 55a804f..b6ad50f 100644
0a122b
--- a/block/cloop.c
0a122b
+++ b/block/cloop.c
0a122b
@@ -99,14 +99,14 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
     s->n_blocks = be32_to_cpu(s->n_blocks);
0a122b
 
0a122b
     /* read offsets */
0a122b
-    if (s->n_blocks > UINT32_MAX / sizeof(uint64_t)) {
0a122b
+    if (s->n_blocks > (UINT32_MAX - 1) / sizeof(uint64_t)) {
0a122b
         /* Prevent integer overflow */
0a122b
         error_setg(errp, "n_blocks %u must be %zu or less",
0a122b
                    s->n_blocks,
0a122b
-                   UINT32_MAX / sizeof(uint64_t));
0a122b
+                   (UINT32_MAX - 1) / sizeof(uint64_t));
0a122b
         return -EINVAL;
0a122b
     }
0a122b
-    offsets_size = s->n_blocks * sizeof(uint64_t);
0a122b
+    offsets_size = (s->n_blocks + 1) * sizeof(uint64_t);
0a122b
     if (offsets_size > 512 * 1024 * 1024) {
0a122b
         /* Prevent ridiculous offsets_size which causes memory allocation to
0a122b
          * fail or overflows bdrv_pread() size.  In practice the 512 MB
0a122b
@@ -123,7 +123,7 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
         goto fail;
0a122b
     }
0a122b
 
0a122b
-    for(i=0;i<s->n_blocks;i++) {
0a122b
+    for (i = 0; i < s->n_blocks + 1; i++) {
0a122b
         uint64_t size;
0a122b
 
0a122b
         s->offsets[i] = be64_to_cpu(s->offsets[i]);
0a122b
@@ -243,9 +243,7 @@ static coroutine_fn int cloop_co_read(BlockDriverState *bs, int64_t sector_num,
0a122b
 static void cloop_close(BlockDriverState *bs)
0a122b
 {
0a122b
     BDRVCloopState *s = bs->opaque;
0a122b
-    if (s->n_blocks > 0) {
0a122b
-        g_free(s->offsets);
0a122b
-    }
0a122b
+    g_free(s->offsets);
0a122b
     g_free(s->compressed_block);
0a122b
     g_free(s->uncompressed_block);
0a122b
     inflateEnd(&s->zstream);
0a122b
diff --git a/tests/qemu-iotests/075 b/tests/qemu-iotests/075
0a122b
index d74fb33..40032c5 100755
0a122b
--- a/tests/qemu-iotests/075
0a122b
+++ b/tests/qemu-iotests/075
0a122b
@@ -52,6 +52,11 @@ _use_sample_img simple-pattern.cloop.bz2
0a122b
 $QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
0a122b
 
0a122b
 echo
0a122b
+echo "== check that the last sector can be read =="
0a122b
+_use_sample_img simple-pattern.cloop.bz2
0a122b
+$QEMU_IO -c "read $((1024 * 1024 - 512)) 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
0a122b
+
0a122b
+echo
0a122b
 echo "== block_size must be a multiple of 512 =="
0a122b
 _use_sample_img simple-pattern.cloop.bz2
0a122b
 poke_file "$TEST_IMG" "$block_size_offset" "\x00\x00\x02\x01"
0a122b
diff --git a/tests/qemu-iotests/075.out b/tests/qemu-iotests/075.out
0a122b
index 911cd3b..5f1d6c1 100644
0a122b
--- a/tests/qemu-iotests/075.out
0a122b
+++ b/tests/qemu-iotests/075.out
0a122b
@@ -4,6 +4,10 @@ QA output created by 075
0a122b
 read 512/512 bytes at offset 0
0a122b
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
0a122b
 
0a122b
+== check that the last sector can be read ==
0a122b
+read 512/512 bytes at offset 1048064
0a122b
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
0a122b
+
0a122b
 == block_size must be a multiple of 512 ==
0a122b
 qemu-io: can't open device TEST_DIR/simple-pattern.cloop: block_size 513 must be a multiple of 512
0a122b
 no file open, try 'help open'
0a122b
-- 
0a122b
1.7.1
0a122b