9ae3a8
From fdbe21cc29a983e05b6df725a75b1a1b15d7795e Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Tue, 25 Mar 2014 14:23:32 +0100
9ae3a8
Subject: [PATCH 25/49] qcow2: Validate active L1 table offset and size (CVE-2014-0144)
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1395753835-7591-26-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: n/a
9ae3a8
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 25/48] qcow2: Validate active L1 table offset and 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 avoids an unbounded allocation.
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2.c              |   16 ++++++++++++++++
9ae3a8
 tests/qemu-iotests/080     |   18 ++++++++++++++++++
9ae3a8
 tests/qemu-iotests/080.out |   11 +++++++++++
9ae3a8
 3 files changed, 45 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index 8c74dea..8be82f0 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -641,6 +641,13 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
     s->nb_snapshots = header.nb_snapshots;
9ae3a8
 
9ae3a8
     /* read the level 1 table */
9ae3a8
+    if (header.l1_size > 0x2000000) {
9ae3a8
+        /* 32 MB L1 table is enough for 2 PB images at 64k cluster size
9ae3a8
+         * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
9ae3a8
+        error_setg(errp, "Active L1 table too large");
9ae3a8
+        ret = -EFBIG;
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
     s->l1_size = header.l1_size;
9ae3a8
 
9ae3a8
     l1_vm_state_index = size_to_l1(s, header.size);
9ae3a8
@@ -658,7 +665,16 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
         ret = -EINVAL;
9ae3a8
         goto fail;
9ae3a8
     }
9ae3a8
+
9ae3a8
+    ret = validate_table_offset(bs, header.l1_table_offset,
9ae3a8
+                                header.l1_size, sizeof(uint64_t));
9ae3a8
+    if (ret < 0) {
9ae3a8
+        error_setg(errp, "Invalid L1 table offset");
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
     s->l1_table_offset = header.l1_table_offset;
9ae3a8
+
9ae3a8
+
9ae3a8
     if (s->l1_size > 0) {
9ae3a8
         s->l1_table = g_malloc0(
9ae3a8
             align_offset(s->l1_size * sizeof(uint64_t), 512));
9ae3a8
diff --git a/tests/qemu-iotests/080 b/tests/qemu-iotests/080
9ae3a8
index 8a8b460..7255b6c 100755
9ae3a8
--- a/tests/qemu-iotests/080
9ae3a8
+++ b/tests/qemu-iotests/080
9ae3a8
@@ -45,6 +45,8 @@ _supported_os Linux
9ae3a8
 header_size=104
9ae3a8
 
9ae3a8
 offset_backing_file_offset=8
9ae3a8
+offset_l1_size=36
9ae3a8
+offset_l1_table_offset=40
9ae3a8
 offset_refcount_table_offset=48
9ae3a8
 offset_refcount_table_clusters=56
9ae3a8
 offset_nb_snapshots=60
9ae3a8
@@ -117,6 +119,22 @@ 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
+echo
9ae3a8
+echo "== Invalid L1 table =="
9ae3a8
+_make_test_img 64M
9ae3a8
+poke_file "$TEST_IMG" "$offset_l1_size" "\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_l1_size" "\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_l1_table_offset" "\x7f\xff\xff\xff\xff\xff\x00\x00"
9ae3a8
+poke_file "$TEST_IMG" "$offset_l1_size" "\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_l1_table_offset" "\x12\x34\x56\x78\x90\xab\xcd\xef"
9ae3a8
+poke_file "$TEST_IMG" "$offset_l1_size" "\x00\x00\x00\x01"
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 b06f47f..4ec2545 100644
9ae3a8
--- a/tests/qemu-iotests/080.out
9ae3a8
+++ b/tests/qemu-iotests/080.out
9ae3a8
@@ -47,4 +47,15 @@ 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
+
9ae3a8
+== Invalid L1 table ==
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Active L1 table too large
9ae3a8
+no file open, try 'help open'
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Active L1 table too large
9ae3a8
+no file open, try 'help open'
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Invalid L1 table offset
9ae3a8
+no file open, try 'help open'
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Invalid L1 table offset
9ae3a8
+no file open, try 'help open'
9ae3a8
 *** done
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8