thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-qcow2-Validate-snapshot-table-offset-size-CVE-2014-0.patch

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