Blame SOURCES/kvm-bochs-Check-catalog_size-header-field-CVE-2014-0143.patch

0a122b
From 920f41e5268801ff36a4f76ccaf2ac2584ac009e Mon Sep 17 00:00:00 2001
0a122b
From: Kevin Wolf <kwolf@redhat.com>
0a122b
Date: Tue, 25 Mar 2014 14:23:19 +0100
0a122b
Subject: [PATCH 12/49] bochs: Check catalog_size header field (CVE-2014-0143)
0a122b
0a122b
RH-Author: Kevin Wolf <kwolf@redhat.com>
0a122b
Message-id: <1395753835-7591-13-git-send-email-kwolf@redhat.com>
0a122b
Patchwork-id: n/a
0a122b
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 12/48] bochs: Check catalog_size header field (CVE-2014-0143)
0a122b
Bugzilla: 1079320
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=1079320
0a122b
Upstream status: Embargoed
0a122b
0a122b
It should neither become negative nor allow unbounded memory
0a122b
allocations. This fixes aborts in g_malloc() and an s->catalog_bitmap
0a122b
buffer overflow on big endian hosts.
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
---
0a122b
 block/bochs.c              |   13 +++++++++++++
0a122b
 tests/qemu-iotests/078     |   13 +++++++++++++
0a122b
 tests/qemu-iotests/078.out |   10 +++++++++-
0a122b
 3 files changed, 35 insertions(+), 1 deletions(-)
0a122b
0a122b
diff --git a/block/bochs.c b/block/bochs.c
0a122b
index 04cca71..d1b1a2c 100644
0a122b
--- a/block/bochs.c
0a122b
+++ b/block/bochs.c
0a122b
@@ -122,7 +122,14 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
         bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
0a122b
     }
0a122b
 
0a122b
+    /* Limit to 1M entries to avoid unbounded allocation. This is what is
0a122b
+     * needed for the largest image that bximage can create (~8 TB). */
0a122b
     s->catalog_size = le32_to_cpu(bochs.catalog);
0a122b
+    if (s->catalog_size > 0x100000) {
0a122b
+        error_setg(errp, "Catalog size is too large");
0a122b
+        return -EFBIG;
0a122b
+    }
0a122b
+
0a122b
     s->catalog_bitmap = g_malloc(s->catalog_size * 4);
0a122b
 
0a122b
     ret = bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_bitmap,
0a122b
@@ -141,6 +148,12 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
 
0a122b
     s->extent_size = le32_to_cpu(bochs.extent);
0a122b
 
0a122b
+    if (s->catalog_size < bs->total_sectors / s->extent_size) {
0a122b
+        error_setg(errp, "Catalog size is too small for this disk size");
0a122b
+        ret = -EINVAL;
0a122b
+        goto fail;
0a122b
+    }
0a122b
+
0a122b
     qemu_co_mutex_init(&s->lock);
0a122b
     return 0;
0a122b
 
0a122b
diff --git a/tests/qemu-iotests/078 b/tests/qemu-iotests/078
0a122b
index 73b573a..902ef0f 100755
0a122b
--- a/tests/qemu-iotests/078
0a122b
+++ b/tests/qemu-iotests/078
0a122b
@@ -43,6 +43,7 @@ _supported_proto generic
0a122b
 _supported_os Linux
0a122b
 
0a122b
 catalog_size_offset=$((0x48))
0a122b
+disk_size_offset=$((0x58))
0a122b
 
0a122b
 echo
0a122b
 echo "== Read from a valid image =="
0a122b
@@ -55,6 +56,18 @@ _use_sample_img empty.bochs.bz2
0a122b
 poke_file "$TEST_IMG" "$catalog_size_offset" "\xff\xff\xff\xff"
0a122b
 { $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
0a122b
 
0a122b
+echo
0a122b
+echo "== Overflow for catalog size * sizeof(uint32_t) =="
0a122b
+_use_sample_img empty.bochs.bz2
0a122b
+poke_file "$TEST_IMG" "$catalog_size_offset" "\x00\x00\x00\x40"
0a122b
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
0a122b
+
0a122b
+echo
0a122b
+echo "== Too small catalog bitmap for image size =="
0a122b
+_use_sample_img empty.bochs.bz2
0a122b
+poke_file "$TEST_IMG" "$disk_size_offset" "\x00\xc0\x0f\x00\x00\x00\x00\x7f"
0a122b
+{ $QEMU_IO -c "read 2T 4k" $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/078.out b/tests/qemu-iotests/078.out
0a122b
index ef8c42d..7254693 100644
0a122b
--- a/tests/qemu-iotests/078.out
0a122b
+++ b/tests/qemu-iotests/078.out
0a122b
@@ -5,6 +5,14 @@ read 512/512 bytes at offset 0
0a122b
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
0a122b
 
0a122b
 == Negative catalog size ==
0a122b
-qemu-io: can't open device TEST_DIR/empty.bochs: Could not open 'TEST_DIR/empty.bochs': Interrupted system call
0a122b
+qemu-io: can't open device TEST_DIR/empty.bochs: Catalog size is too large
0a122b
+no file open, try 'help open'
0a122b
+
0a122b
+== Overflow for catalog size * sizeof(uint32_t) ==
0a122b
+qemu-io: can't open device TEST_DIR/empty.bochs: Catalog size is too large
0a122b
+no file open, try 'help open'
0a122b
+
0a122b
+== Too small catalog bitmap for image size ==
0a122b
+qemu-io: can't open device TEST_DIR/empty.bochs: Catalog size is too small for this disk size
0a122b
 no file open, try 'help open'
0a122b
 *** done
0a122b
-- 
0a122b
1.7.1
0a122b