Skip to contentSkip to conversation

vmdk: check cluster size in vmdk_open

PATCH Superseded Fam Zheng 1 participant 7 messages Aug 2, 2013 1 message not in the archive Block layer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Fam Zheng

author
#1 lore raw

PATCH 1/7 vmdk: Make VMDK3Header QEMU_PACKED

Although the fields are all uint32_t, it's best to make it consistent that all on disk structures are QEMU_PACKED.

---
 block/vmdk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/vmdk.c +1 -1
index 3756333..8ff43b9 100644
@@ -62,7 +62,7 @@ typedef struct {
     uint32_t cylinders;
     uint32_t heads;
     uint32_t sectors_per_track;
-} VMDK3Header;
+} QEMU_PACKED VMDK3Header;
 
 typedef struct {
     uint32_t version;
Signature · 1 line
1.8.3.4
author
#2 lore raw

PATCH 2/7 vmdk: use unsigned values for on disk header fields

In reply to a message not in the archive

The size and offset fields are all non-negative values, use uint64_t for them to avoid getting negative in memory value by int overflow.

---
 block/vmdk.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
block/vmdk.c +8 -8
index 8ff43b9..3c6fa47 100644
@@ -67,14 +67,14 @@ typedef struct {
 typedef struct {
     uint32_t version;
     uint32_t flags;
-    int64_t capacity;
-    int64_t granularity;
-    int64_t desc_offset;
-    int64_t desc_size;
-    int32_t num_gtes_per_gte;
-    int64_t rgd_offset;
-    int64_t gd_offset;
-    int64_t grain_offset;
+    uint64_t capacity;
+    uint64_t granularity;
+    uint64_t desc_offset;
+    uint64_t desc_size;
+    uint32_t num_gtes_per_gte;
+    uint64_t rgd_offset;
+    uint64_t gd_offset;
+    uint64_t grain_offset;
     char filler[1];
     char check_bytes[4];
     uint16_t compressAlgorithm;
Signature · 1 line
1.8.3.4
author
#3 lore raw

PATCH 3/7 qemu-iotests: add empty test case for vmdk

In reply to a message not in the archive

Will add vmdk specific tests later here.

---
 tests/qemu-iotests/059     | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/059.out |  2 ++
 tests/qemu-iotests/group   |  1 +
 3 files changed, 54 insertions(+)
 create mode 100755 tests/qemu-iotests/059
 create mode 100644 tests/qemu-iotests/059.out
tests/qemu-iotests/059 new file +51 -0
new file mode 100755
index 0000000..996d85c
@@ -0,0 +1,51 @@
+#!/bin/bash
+#
+# Test case for vmdk
+#
+# Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
+#
+
+# creator
[email protected]
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# 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 tests qcow2-specific low-level functionality
+_supported_fmt vmdk
+_supported_proto generic
+_supported_os Linux
+
+granularity_offset=16
+
+# success, all done
+echo "*** done"
+rm -f $seq.full
+status=0
tests/qemu-iotests/059.out new file +2 -0
new file mode 100644
index 0000000..4ca7f29
@@ -0,0 +1,2 @@
+QA output created by 059
+*** done
tests/qemu-iotests/group +1 -0
index 1a1a30a..732dff8 100644
@@ -65,3 +65,4 @@
 056 rw auto backing
 057 rw auto
 058 rw auto
+059 rw auto
Signature · 1 line
1.8.3.4
author
#4 lore raw

PATCH 4/7 vmdk: check granularity field in opening

In reply to a message not in the archive

Granularity is used to calculate the cluster size and allocate r/w buffer. Check the value from image before using it, so we don't abort() for unbounded memory allocation.

---
 block/vmdk.c           | 13 +++++++++++++
 tests/qemu-iotests/059 |  6 ++++++
 2 files changed, 19 insertions(+)
block/vmdk.c +13 -0
index 3c6fa47..dbedff7 100644
@@ -475,6 +475,13 @@ static int vmdk_open_vmdk3(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+
+    if (le32_to_cpu(header.granularity) > 0x200000) {
+        /* 1GB for one cluster is unrealistic */
+        error_report("invalid granularity, image may be corrupt");
+        return -EINVAL;
+    }
+
     extent = vmdk_add_extent(bs,
                              bs->file, false,
                              le32_to_cpu(header.disk_sectors),
@@ -561,6 +568,12 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         header = footer.header;
     }
 
+    if (le32_to_cpu(header.granularity) > 0x200000) {
+        /* 1GB for one cluster is unrealistic */
+        error_report("invalid granularity, image may be corrupt");
+        return -EINVAL;
+    }
+
     if (le32_to_cpu(header.version) >= 3) {
         char buf[64];
         snprintf(buf, sizeof(buf), "VMDK version %d",
tests/qemu-iotests/059 +6 -0
index 996d85c..18807d3 100755
@@ -45,6 +45,12 @@ _supported_os Linux
 
 granularity_offset=16
 
+echo "=== Testing invalid granularity ==="
+echo
+_make_test_img 64M
+poke_file "$TEST_IMG" "$granularity_offset" "\xff\xff\xff\xff\xff\xff\xff\xff"
+$QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
+
 # success, all done
 echo "*** done"
 rm -f $seq.full
Signature · 1 line
1.8.3.4
author
#5 lore raw

PATCH 5/7 vmdk: check cluster size in vmdk_open

In reply to a message not in the archive

VMDK specification doesn't give upper limit of cluster size (header.grainSize). But we need to avoid unbounded memory allocation. Check the value when opening and refuse too big value (>512MB cluster size).

---
 block/vmdk.c               | 1 +
 tests/qemu-iotests/059     | 2 +-
 tests/qemu-iotests/059.out | 6 ++++++
 3 files changed, 8 insertions(+), 1 deletion(-)
block/vmdk.c +1 -0
index dbedff7..1d6bc39 100644
@@ -41,6 +41,7 @@
 #define VMDK4_GD_AT_END 0xffffffffffffffffULL
 
 #define VMDK_GTE_ZEROED 0x1
+#define VMDK_GRANULARITY_MAX 0x100000 /* limit of cluster_sectors */
 
 /* VMDK internal error codes */
 #define VMDK_OK      0
tests/qemu-iotests/059 +1 -1
index 18807d3..f14c41b 100755
@@ -43,7 +43,7 @@ _supported_fmt vmdk
 _supported_proto generic
 _supported_os Linux
 
-granularity_offset=16
+granularity_offset=20
 
 echo "=== Testing invalid granularity ==="
 echo
tests/qemu-iotests/059.out +6 -0
index 4ca7f29..380ca3d 100644
@@ -1,2 +1,8 @@
 QA output created by 059
+=== Testing invalid granularity ===
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
+invalid granularity, image may be corrupt
+qemu-io: can't open device TEST_DIR/t.vmdk
+no file open, try 'help open'
 *** done
Signature · 1 line
1.8.3.4
author
#6 lore raw

PATCH 6/7 vmdk: check l2 table size when opening

In reply to a message not in the archive

header.num_gtes_per_gte determines size for L2 table. Check for too big value before using it. Limit to 512M entries (2GB per one L2 table).

---
 block/vmdk.c               | 5 +++++
 tests/qemu-iotests/059     | 7 +++++++
 tests/qemu-iotests/059.out | 6 ++++++
 3 files changed, 18 insertions(+)
block/vmdk.c +5 -0
index 1d6bc39..e28bfc8 100644
@@ -584,6 +584,11 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         return -ENOTSUP;
     }
 
+    if (le64_to_cpu(header.num_gtes_per_gte) > 512 * 1024 * 1024) {
+        error_report("L2 table size too big");
+        return -EINVAL;
+    }
+
     l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gte)
                         * le64_to_cpu(header.granularity);
     if (l1_entry_sectors == 0) {
tests/qemu-iotests/059 +7 -0
index f14c41b..aa9eb57 100755
@@ -44,6 +44,7 @@ _supported_proto generic
 _supported_os Linux
 
 granularity_offset=20
+grain_table_size_offset=44
 
 echo "=== Testing invalid granularity ==="
 echo
@@ -51,6 +52,12 @@ _make_test_img 64M
 poke_file "$TEST_IMG" "$granularity_offset" "\xff\xff\xff\xff\xff\xff\xff\xff"
 $QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
 
+echo "=== Testing too big L2 table size ==="
+echo
+_make_test_img 64M
+poke_file "$TEST_IMG" "$grain_table_size_offset" "\xff\xff\xff\xff"
+$QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
+
 # success, all done
 echo "*** done"
 rm -f $seq.full
tests/qemu-iotests/059.out +6 -0
index 380ca3d..583955f 100644
@@ -5,4 +5,10 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
 invalid granularity, image may be corrupt
 qemu-io: can't open device TEST_DIR/t.vmdk
 no file open, try 'help open'
+=== Testing too big L2 table size ===
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
+L2 table size too big
+qemu-io: can't open device TEST_DIR/t.vmdk
+no file open, try 'help open'
 *** done
Signature · 1 line
1.8.3.4
author
#7 lore raw

PATCH 7/7 vmdk: check l1 size before opening image

In reply to a message not in the archive

L1 table size is calculated from capacity, granularity and l2 table size. If capacity is too big or later two are too small, the L1 table will be too big to allocate in memory. Limit it to a reasonable range.

---
 block/vmdk.c               | 8 ++++++++
 tests/qemu-iotests/059     | 8 ++++++++
 tests/qemu-iotests/059.out | 6 ++++++
 3 files changed, 22 insertions(+)
block/vmdk.c +8 -0
index e28bfc8..2114ef6 100644
@@ -596,6 +596,14 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     }
     l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
                 / l1_entry_sectors;
+    if (l1_size > 512 * 1024 * 1024) {
+        /* although with big capacity and small l1_entry_sectors, we can get a
+         * big l1_size, we don't want unbounded value to allocate the table.
+         * Limit it to 512M, which is 16PB for default cluster and L2 table
+         * size */
+        error_report("L1 size too big");
+        return -EFBIG;
+    }
     if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
         l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
     }
tests/qemu-iotests/059 +8 -0
index aa9eb57..e09e041 100755
@@ -43,6 +43,7 @@ _supported_fmt vmdk
 _supported_proto generic
 _supported_os Linux
 
+capacity_offset=16
 granularity_offset=20
 grain_table_size_offset=44
 
@@ -58,6 +59,13 @@ _make_test_img 64M
 poke_file "$TEST_IMG" "$grain_table_size_offset" "\xff\xff\xff\xff"
 $QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
 
+echo "=== Testing too big L1 table size ==="
+echo
+_make_test_img 64M
+poke_file "$TEST_IMG" "$capacity_offset" "\xff\xff\xff\xff"
+poke_file "$TEST_IMG" "$grain_table_size_offset" "\x01\x00\x00\x00"
+$QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
+
 # success, all done
 echo "*** done"
 rm -f $seq.full
tests/qemu-iotests/059.out +6 -0
index 583955f..9e715e5 100644
@@ -11,4 +11,10 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
 L2 table size too big
 qemu-io: can't open device TEST_DIR/t.vmdk
 no file open, try 'help open'
+=== Testing too big L1 table size ===
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
+L1 size too big
+qemu-io: can't open device TEST_DIR/t.vmdk
+no file open, try 'help open'
 *** done
Signature · 1 line
1.8.3.4