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