|
|
9ae3a8 |
From c85b3af7587973944c28b859dfefa91c21852971 Mon Sep 17 00:00:00 2001
|
|
|
9ae3a8 |
From: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
Date: Tue, 6 Aug 2013 15:44:51 +0800
|
|
|
9ae3a8 |
Subject: [PATCH 05/13] vmdk: check granularity field in opening
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Message-id: <1377573001-27070-6-git-send-email-famz@redhat.com>
|
|
|
9ae3a8 |
Patchwork-id: 53785
|
|
|
9ae3a8 |
O-Subject: [RHEL-7 qemu-kvm PATCH 05/13] vmdk: check granularity field in opening
|
|
|
9ae3a8 |
Bugzilla: 995866
|
|
|
9ae3a8 |
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Granularity is used to calculate the cluster size and allocate r/w
|
|
|
9ae3a8 |
buffer. Check the value from image before using it, so we don't abort()
|
|
|
9ae3a8 |
for unbounded memory allocation.
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
9ae3a8 |
(cherry picked from commit 8aa1331c09a9b899f48d97f097bb49b7d458be1c)
|
|
|
9ae3a8 |
Signed-off-by: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
---
|
|
|
9ae3a8 |
block/vmdk.c | 40 +++++++++++++++++++++++++++++++---------
|
|
|
9ae3a8 |
tests/qemu-iotests/059 | 8 +++++++-
|
|
|
9ae3a8 |
tests/qemu-iotests/059.out | 6 ++++++
|
|
|
9ae3a8 |
3 files changed, 44 insertions(+), 10 deletions(-)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
diff --git a/block/vmdk.c b/block/vmdk.c
|
|
|
9ae3a8 |
index 78ea480..8f59697 100644
|
|
|
9ae3a8 |
--- a/block/vmdk.c
|
|
|
9ae3a8 |
+++ b/block/vmdk.c
|
|
|
9ae3a8 |
@@ -385,15 +385,22 @@ static int vmdk_parent_open(BlockDriverState *bs)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
/* Create and append extent to the extent array. Return the added VmdkExtent
|
|
|
9ae3a8 |
* address. return NULL if allocation failed. */
|
|
|
9ae3a8 |
-static VmdkExtent *vmdk_add_extent(BlockDriverState *bs,
|
|
|
9ae3a8 |
+static int vmdk_add_extent(BlockDriverState *bs,
|
|
|
9ae3a8 |
BlockDriverState *file, bool flat, int64_t sectors,
|
|
|
9ae3a8 |
int64_t l1_offset, int64_t l1_backup_offset,
|
|
|
9ae3a8 |
uint32_t l1_size,
|
|
|
9ae3a8 |
- int l2_size, unsigned int cluster_sectors)
|
|
|
9ae3a8 |
+ int l2_size, uint64_t cluster_sectors,
|
|
|
9ae3a8 |
+ VmdkExtent **new_extent)
|
|
|
9ae3a8 |
{
|
|
|
9ae3a8 |
VmdkExtent *extent;
|
|
|
9ae3a8 |
BDRVVmdkState *s = bs->opaque;
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+ if (cluster_sectors > 0x200000) {
|
|
|
9ae3a8 |
+ /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
|
|
|
9ae3a8 |
+ error_report("invalid granularity, image may be corrupt");
|
|
|
9ae3a8 |
+ return -EINVAL;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
s->extents = g_realloc(s->extents,
|
|
|
9ae3a8 |
(s->num_extents + 1) * sizeof(VmdkExtent));
|
|
|
9ae3a8 |
extent = &s->extents[s->num_extents];
|
|
|
9ae3a8 |
@@ -416,7 +423,10 @@ static VmdkExtent *vmdk_add_extent(BlockDriverState *bs,
|
|
|
9ae3a8 |
extent->end_sector = extent->sectors;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
bs->total_sectors = extent->end_sector;
|
|
|
9ae3a8 |
- return extent;
|
|
|
9ae3a8 |
+ if (new_extent) {
|
|
|
9ae3a8 |
+ *new_extent = extent;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ return 0;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent)
|
|
|
9ae3a8 |
@@ -475,12 +485,17 @@ static int vmdk_open_vmdk3(BlockDriverState *bs,
|
|
|
9ae3a8 |
if (ret < 0) {
|
|
|
9ae3a8 |
return ret;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
- extent = vmdk_add_extent(bs,
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret = vmdk_add_extent(bs,
|
|
|
9ae3a8 |
bs->file, false,
|
|
|
9ae3a8 |
le32_to_cpu(header.disk_sectors),
|
|
|
9ae3a8 |
le32_to_cpu(header.l1dir_offset) << 9,
|
|
|
9ae3a8 |
0, 1 << 6, 1 << 9,
|
|
|
9ae3a8 |
- le32_to_cpu(header.granularity));
|
|
|
9ae3a8 |
+ le32_to_cpu(header.granularity),
|
|
|
9ae3a8 |
+ &extent);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ return ret;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
ret = vmdk_init_tables(bs, extent);
|
|
|
9ae3a8 |
if (ret) {
|
|
|
9ae3a8 |
/* free extent allocated by vmdk_add_extent */
|
|
|
9ae3a8 |
@@ -580,13 +595,17 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
|
|
|
9ae3a8 |
if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
|
|
|
9ae3a8 |
l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
- extent = vmdk_add_extent(bs, file, false,
|
|
|
9ae3a8 |
+ ret = vmdk_add_extent(bs, file, false,
|
|
|
9ae3a8 |
le64_to_cpu(header.capacity),
|
|
|
9ae3a8 |
le64_to_cpu(header.gd_offset) << 9,
|
|
|
9ae3a8 |
l1_backup_offset,
|
|
|
9ae3a8 |
l1_size,
|
|
|
9ae3a8 |
le32_to_cpu(header.num_gtes_per_gte),
|
|
|
9ae3a8 |
- le64_to_cpu(header.granularity));
|
|
|
9ae3a8 |
+ le64_to_cpu(header.granularity),
|
|
|
9ae3a8 |
+ &extent);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ return ret;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
extent->compressed =
|
|
|
9ae3a8 |
le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
|
|
|
9ae3a8 |
extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
|
|
|
9ae3a8 |
@@ -702,8 +721,11 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
|
|
|
9ae3a8 |
/* FLAT extent */
|
|
|
9ae3a8 |
VmdkExtent *extent;
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
- extent = vmdk_add_extent(bs, extent_file, true, sectors,
|
|
|
9ae3a8 |
- 0, 0, 0, 0, sectors);
|
|
|
9ae3a8 |
+ ret = vmdk_add_extent(bs, extent_file, true, sectors,
|
|
|
9ae3a8 |
+ 0, 0, 0, 0, sectors, &extent);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ return ret;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
extent->flat_start_offset = flat_offset << 9;
|
|
|
9ae3a8 |
} else if (!strcmp(type, "SPARSE")) {
|
|
|
9ae3a8 |
/* SPARSE extent */
|
|
|
9ae3a8 |
diff --git a/tests/qemu-iotests/059 b/tests/qemu-iotests/059
|
|
|
9ae3a8 |
index 9dc7f64..9545e82 100755
|
|
|
9ae3a8 |
--- a/tests/qemu-iotests/059
|
|
|
9ae3a8 |
+++ b/tests/qemu-iotests/059
|
|
|
9ae3a8 |
@@ -43,7 +43,13 @@ _supported_fmt vmdk
|
|
|
9ae3a8 |
_supported_proto generic
|
|
|
9ae3a8 |
_supported_os Linux
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
-granularity_offset=16
|
|
|
9ae3a8 |
+granularity_offset=20
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+echo "=== Testing invalid granularity ==="
|
|
|
9ae3a8 |
+echo
|
|
|
9ae3a8 |
+_make_test_img 64M
|
|
|
9ae3a8 |
+poke_file "$TEST_IMG" "$granularity_offset" "\xff\xff\xff\xff\xff\xff\xff\xff"
|
|
|
9ae3a8 |
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
# success, all done
|
|
|
9ae3a8 |
echo "*** done"
|
|
|
9ae3a8 |
diff --git a/tests/qemu-iotests/059.out b/tests/qemu-iotests/059.out
|
|
|
9ae3a8 |
index 4ca7f29..380ca3d 100644
|
|
|
9ae3a8 |
--- a/tests/qemu-iotests/059.out
|
|
|
9ae3a8 |
+++ b/tests/qemu-iotests/059.out
|
|
|
9ae3a8 |
@@ -1,2 +1,8 @@
|
|
|
9ae3a8 |
QA output created by 059
|
|
|
9ae3a8 |
+=== Testing invalid granularity ===
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
|
|
|
9ae3a8 |
+invalid granularity, image may be corrupt
|
|
|
9ae3a8 |
+qemu-io: can't open device TEST_DIR/t.vmdk
|
|
|
9ae3a8 |
+no file open, try 'help open'
|
|
|
9ae3a8 |
*** done
|
|
|
9ae3a8 |
--
|
|
|
9ae3a8 |
1.7.1
|
|
|
9ae3a8 |
|