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

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