9ae3a8
From a38284209acf74b1e44b32deca45da2cf451ba68 Mon Sep 17 00:00:00 2001
9ae3a8
Message-Id: <a38284209acf74b1e44b32deca45da2cf451ba68.1418766606.git.jen@redhat.com>
9ae3a8
In-Reply-To: <6f81b4847eb68ebdf54a8f1a771e19d112d74152.1418766606.git.jen@redhat.com>
9ae3a8
References: <6f81b4847eb68ebdf54a8f1a771e19d112d74152.1418766606.git.jen@redhat.com>
9ae3a8
From: Fam Zheng <famz@redhat.com>
9ae3a8
Date: Thu, 4 Dec 2014 00:05:01 -0600
9ae3a8
Subject: [CHANGE 07/31] vmdk: Check for overhead when opening
9ae3a8
To: rhvirt-patches@redhat.com,
9ae3a8
    jen@redhat.com
9ae3a8
9ae3a8
RH-Author: Fam Zheng <famz@redhat.com>
9ae3a8
Message-id: <1417651524-18041-8-git-send-email-famz@redhat.com>
9ae3a8
Patchwork-id: 62680
9ae3a8
O-Subject: [RHEL-7.1 qemu-kvm PATCH v5 07/30] vmdk: Check for overhead when opening
9ae3a8
Bugzilla: 1134251
9ae3a8
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
9ae3a8
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
Report an error if file size is even smaller than metadata.
9ae3a8
9ae3a8
Signed-off-by: Fam Zheng <famz@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 34ceed81f9ca31829448276dafe3d9151d66962c)
9ae3a8
Signed-off-by: Fam Zheng <famz@redhat.com>
9ae3a8
Signed-off-by: Jeff E. Nelson <jen@redhat.com>
9ae3a8
---
9ae3a8
 block/vmdk.c               | 7 +++++++
9ae3a8
 tests/qemu-iotests/059     | 6 ++++++
9ae3a8
 tests/qemu-iotests/059.out | 5 +++++
9ae3a8
 3 files changed, 18 insertions(+)
9ae3a8
9ae3a8
diff --git a/block/vmdk.c b/block/vmdk.c
9ae3a8
index 4226baa..7ea3aef 100644
9ae3a8
--- a/block/vmdk.c
9ae3a8
+++ b/block/vmdk.c
9ae3a8
@@ -636,6 +636,13 @@ 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
+    if (bdrv_getlength(file) <
9ae3a8
+            le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE) {
9ae3a8
+        error_report("File truncated, expecting at least %lld bytes",
9ae3a8
+                le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
9ae3a8
+        return -EINVAL;
9ae3a8
+    }
9ae3a8
+
9ae3a8
     ret = vmdk_add_extent(bs, file, false,
9ae3a8
                           le64_to_cpu(header.capacity),
9ae3a8
                           le64_to_cpu(header.gd_offset) << 9,
9ae3a8
diff --git a/tests/qemu-iotests/059 b/tests/qemu-iotests/059
9ae3a8
index dde4a59..71b7b49 100755
9ae3a8
--- a/tests/qemu-iotests/059
9ae3a8
+++ b/tests/qemu-iotests/059
9ae3a8
@@ -98,6 +98,12 @@ EOF
9ae3a8
 _img_info
9ae3a8
 
9ae3a8
 echo
9ae3a8
+echo "=== Testing truncated sparse ==="
9ae3a8
+IMGOPTS="subformat=monolithicSparse" _make_test_img 100G
9ae3a8
+truncate -s 10M $TEST_IMG
9ae3a8
+_img_info
9ae3a8
+
9ae3a8
+echo
9ae3a8
 echo "=== Testing version 3 ==="
9ae3a8
 _use_sample_img iotest-version3.vmdk.bz2
9ae3a8
 _img_info
9ae3a8
diff --git a/tests/qemu-iotests/059.out b/tests/qemu-iotests/059.out
9ae3a8
index 75d9858..f4ca52d 100644
9ae3a8
--- a/tests/qemu-iotests/059.out
9ae3a8
+++ b/tests/qemu-iotests/059.out
9ae3a8
@@ -2043,6 +2043,11 @@ qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Invalid extent lines:
9ae3a8
 RW 12582912 VMFS "dummy.IMGFMT" 1
9ae3a8
 
9ae3a8
 
9ae3a8
+=== Testing truncated sparse ===
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=107374182400
9ae3a8
+qemu-img: File truncated, expecting at least 13172736 bytes
9ae3a8
+qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Could not open 'TEST_DIR/t.IMGFMT': Wrong medium type
9ae3a8
+
9ae3a8
 === Testing version 3 ===
9ae3a8
 image: TEST_DIR/iotest-version3.IMGFMT
9ae3a8
 file format: IMGFMT
9ae3a8
-- 
9ae3a8
2.1.0
9ae3a8