9ae3a8
From 2318681bf850996a3e9c682ca3290aa9f05fd15c Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Tue, 25 Mar 2014 14:23:31 +0100
9ae3a8
Subject: [PATCH 24/49] qcow2: Validate snapshot table offset/size (CVE-2014-0144)
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1395753835-7591-25-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: n/a
9ae3a8
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 24/48] qcow2: Validate snapshot table offset/size (CVE-2014-0144)
9ae3a8
Bugzilla: 1079455
9ae3a8
RH-Acked-by: Jeff Cody <jcody@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
9ae3a8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1079455
9ae3a8
Upstream status: Embargoed
9ae3a8
9ae3a8
This avoid unbounded memory allocation and fixes a potential buffer
9ae3a8
overflow on 32 bit hosts.
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-snapshot.c     |   29 ++++-------------------------
9ae3a8
 block/qcow2.c              |   15 +++++++++++++++
9ae3a8
 block/qcow2.h              |   29 ++++++++++++++++++++++++++++-
9ae3a8
 tests/qemu-iotests/080     |   27 +++++++++++++++++++++++++++
9ae3a8
 tests/qemu-iotests/080.out |   17 +++++++++++++++++
9ae3a8
 5 files changed, 91 insertions(+), 26 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c
9ae3a8
index aa88c51..5d73506 100644
9ae3a8
--- a/block/qcow2-snapshot.c
9ae3a8
+++ b/block/qcow2-snapshot.c
9ae3a8
@@ -26,31 +26,6 @@
9ae3a8
 #include "block/block_int.h"
9ae3a8
 #include "block/qcow2.h"
9ae3a8
 
9ae3a8
-typedef struct QEMU_PACKED QCowSnapshotHeader {
9ae3a8
-    /* header is 8 byte aligned */
9ae3a8
-    uint64_t l1_table_offset;
9ae3a8
-
9ae3a8
-    uint32_t l1_size;
9ae3a8
-    uint16_t id_str_size;
9ae3a8
-    uint16_t name_size;
9ae3a8
-
9ae3a8
-    uint32_t date_sec;
9ae3a8
-    uint32_t date_nsec;
9ae3a8
-
9ae3a8
-    uint64_t vm_clock_nsec;
9ae3a8
-
9ae3a8
-    uint32_t vm_state_size;
9ae3a8
-    uint32_t extra_data_size; /* for extension */
9ae3a8
-    /* extra data follows */
9ae3a8
-    /* id_str follows */
9ae3a8
-    /* name follows  */
9ae3a8
-} QCowSnapshotHeader;
9ae3a8
-
9ae3a8
-typedef struct QEMU_PACKED QCowSnapshotExtraData {
9ae3a8
-    uint64_t vm_state_size_large;
9ae3a8
-    uint64_t disk_size;
9ae3a8
-} QCowSnapshotExtraData;
9ae3a8
-
9ae3a8
 void qcow2_free_snapshots(BlockDriverState *bs)
9ae3a8
 {
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
@@ -334,6 +309,10 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
9ae3a8
     uint64_t *l1_table = NULL;
9ae3a8
     int64_t l1_table_offset;
9ae3a8
 
9ae3a8
+    if (s->nb_snapshots >= QCOW_MAX_SNAPSHOTS) {
9ae3a8
+        return -EFBIG;
9ae3a8
+    }
9ae3a8
+
9ae3a8
     memset(sn, 0, sizeof(*sn));
9ae3a8
 
9ae3a8
     /* Generate an ID if it wasn't passed */
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index 5513fac..8c74dea 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -622,6 +622,21 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
         goto fail;
9ae3a8
     }
9ae3a8
 
9ae3a8
+    /* Snapshot table offset/length */
9ae3a8
+    if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) {
9ae3a8
+        error_setg(errp, "Too many snapshots");
9ae3a8
+        ret = -EINVAL;
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    ret = validate_table_offset(bs, header.snapshots_offset,
9ae3a8
+                                header.nb_snapshots,
9ae3a8
+                                sizeof(QCowSnapshotHeader));
9ae3a8
+    if (ret < 0) {
9ae3a8
+        error_setg(errp, "Invalid snapshot table offset");
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
     s->snapshots_offset = header.snapshots_offset;
9ae3a8
     s->nb_snapshots = header.nb_snapshots;
9ae3a8
 
9ae3a8
diff --git a/block/qcow2.h b/block/qcow2.h
9ae3a8
index 4a653c5..5efc96e 100644
9ae3a8
--- a/block/qcow2.h
9ae3a8
+++ b/block/qcow2.h
9ae3a8
@@ -38,6 +38,7 @@
9ae3a8
 #define QCOW_CRYPT_AES  1
9ae3a8
 
9ae3a8
 #define QCOW_MAX_CRYPT_CLUSTERS 32
9ae3a8
+#define QCOW_MAX_SNAPSHOTS 65536
9ae3a8
 
9ae3a8
 /* indicate that the refcount of the referenced cluster is exactly one. */
9ae3a8
 #define QCOW_OFLAG_COPIED     (1LL << 63)
9ae3a8
@@ -97,6 +98,32 @@ typedef struct QCowHeader {
9ae3a8
     uint32_t header_length;
9ae3a8
 } QCowHeader;
9ae3a8
 
9ae3a8
+typedef struct QEMU_PACKED QCowSnapshotHeader {
9ae3a8
+    /* header is 8 byte aligned */
9ae3a8
+    uint64_t l1_table_offset;
9ae3a8
+
9ae3a8
+    uint32_t l1_size;
9ae3a8
+    uint16_t id_str_size;
9ae3a8
+    uint16_t name_size;
9ae3a8
+
9ae3a8
+    uint32_t date_sec;
9ae3a8
+    uint32_t date_nsec;
9ae3a8
+
9ae3a8
+    uint64_t vm_clock_nsec;
9ae3a8
+
9ae3a8
+    uint32_t vm_state_size;
9ae3a8
+    uint32_t extra_data_size; /* for extension */
9ae3a8
+    /* extra data follows */
9ae3a8
+    /* id_str follows */
9ae3a8
+    /* name follows  */
9ae3a8
+} QCowSnapshotHeader;
9ae3a8
+
9ae3a8
+typedef struct QEMU_PACKED QCowSnapshotExtraData {
9ae3a8
+    uint64_t vm_state_size_large;
9ae3a8
+    uint64_t disk_size;
9ae3a8
+} QCowSnapshotExtraData;
9ae3a8
+
9ae3a8
+
9ae3a8
 typedef struct QCowSnapshot {
9ae3a8
     uint64_t l1_table_offset;
9ae3a8
     uint32_t l1_size;
9ae3a8
@@ -202,7 +229,7 @@ typedef struct BDRVQcowState {
9ae3a8
     AES_KEY aes_decrypt_key;
9ae3a8
     uint64_t snapshots_offset;
9ae3a8
     int snapshots_size;
9ae3a8
-    int nb_snapshots;
9ae3a8
+    unsigned int nb_snapshots;
9ae3a8
     QCowSnapshot *snapshots;
9ae3a8
 
9ae3a8
     int flags;
9ae3a8
diff --git a/tests/qemu-iotests/080 b/tests/qemu-iotests/080
9ae3a8
index f58ac73..8a8b460 100755
9ae3a8
--- a/tests/qemu-iotests/080
9ae3a8
+++ b/tests/qemu-iotests/080
9ae3a8
@@ -47,6 +47,8 @@ header_size=104
9ae3a8
 offset_backing_file_offset=8
9ae3a8
 offset_refcount_table_offset=48
9ae3a8
 offset_refcount_table_clusters=56
9ae3a8
+offset_nb_snapshots=60
9ae3a8
+offset_snapshots_offset=64
9ae3a8
 offset_header_size=100
9ae3a8
 offset_ext_magic=$header_size
9ae3a8
 offset_ext_size=$((header_size + 4))
9ae3a8
@@ -90,6 +92,31 @@ poke_file "$TEST_IMG" "$offset_refcount_table_offset" "\xff\xff\xff\xff\xff\xff\
9ae3a8
 poke_file "$TEST_IMG" "$offset_refcount_table_clusters" "\x00\x00\x00\x7f"
9ae3a8
 { $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
 
9ae3a8
+echo
9ae3a8
+echo "== Invalid snapshot table =="
9ae3a8
+_make_test_img 64M
9ae3a8
+poke_file "$TEST_IMG" "$offset_nb_snapshots" "\xff\xff\xff\xff"
9ae3a8
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+poke_file "$TEST_IMG" "$offset_nb_snapshots" "\x7f\xff\xff\xff"
9ae3a8
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+
9ae3a8
+poke_file "$TEST_IMG" "$offset_snapshots_offset" "\xff\xff\xff\xff\xff\xff\x00\x00"
9ae3a8
+poke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x00\xff\xff"
9ae3a8
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+
9ae3a8
+poke_file "$TEST_IMG" "$offset_snapshots_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef"
9ae3a8
+poke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x00\x00\x00"
9ae3a8
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+
9ae3a8
+echo
9ae3a8
+echo "== Hitting snapshot table size limit =="
9ae3a8
+_make_test_img 64M
9ae3a8
+# Put the refcount table in a more or less safe place (16 MB)
9ae3a8
+poke_file "$TEST_IMG" "$offset_snapshots_offset" "\x00\x00\x00\x00\x01\x00\x00\x00"
9ae3a8
+poke_file "$TEST_IMG" "$offset_nb_snapshots" "\x00\x01\x00\x00"
9ae3a8
+{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir
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
 rm -f $seq.full
9ae3a8
diff --git a/tests/qemu-iotests/080.out b/tests/qemu-iotests/080.out
9ae3a8
index f919b58..b06f47f 100644
9ae3a8
--- a/tests/qemu-iotests/080.out
9ae3a8
+++ b/tests/qemu-iotests/080.out
9ae3a8
@@ -30,4 +30,21 @@ no file open, try 'help open'
9ae3a8
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
9ae3a8
 qemu-io: can't open device TEST_DIR/t.qcow2: Invalid reference count table offset
9ae3a8
 no file open, try 'help open'
9ae3a8
+
9ae3a8
+== Invalid snapshot table ==
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Too many snapshots
9ae3a8
+no file open, try 'help open'
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Too many snapshots
9ae3a8
+no file open, try 'help open'
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Invalid snapshot table offset
9ae3a8
+no file open, try 'help open'
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Invalid snapshot table offset
9ae3a8
+no file open, try 'help open'
9ae3a8
+
9ae3a8
+== Hitting snapshot table size limit ==
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
9ae3a8
+qemu-img: Could not create snapshot 'test': -27 (File too large)
9ae3a8
+read 512/512 bytes at offset 0
9ae3a8
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
9ae3a8
 *** done
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8