9ae3a8
From f0b6df5c7146425c80ab72928aa8848c8f1a93a9 Mon Sep 17 00:00:00 2001
9ae3a8
From: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
Date: Tue, 25 Mar 2014 14:23:12 +0100
9ae3a8
Subject: [PATCH 05/49] block/cloop: prevent offsets_size integer overflow (CVE-2014-0143)
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1395753835-7591-6-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: n/a
9ae3a8
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 05/48] block/cloop: prevent offsets_size integer overflow (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
From: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1079320
9ae3a8
Upstream status: Embargoed
9ae3a8
9ae3a8
The following integer overflow in offsets_size can lead to out-of-bounds
9ae3a8
memory stores when n_blocks has a huge value:
9ae3a8
9ae3a8
uint32_t n_blocks, offsets_size;
9ae3a8
[...]
9ae3a8
ret = bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4);
9ae3a8
[...]
9ae3a8
s->n_blocks = be32_to_cpu(s->n_blocks);
9ae3a8
9ae3a8
/bin /boot /cgroup /dev /etc /home /lib /lib64 /lost+found /media /misc /mnt /net /opt /proc /root /sbin /selinux /srv /sys /tmp /usr /var read offsets audio/ backends/ block/ bsd-user/ default-configs/ disas/ docs/ dtc/ fpu/ fsdev/ gdb-xml/ hw/ include/ ldscripts/ libcacard/ linux-headers/ linux-user/ net/ pc-bios/ pixman/ po/ qapi/ qga/ QMP/ qobject/ qom/ redhat/ roms/ scripts/ slirp/ stubs/ sysconfigs/ target-alpha/ target-arm/ target-cris/ target-i386/ target-lm32/ target-m68k/ target-microblaze/ target-mips/ target-moxie/ target-openrisc/ target-ppc/ target-s390x/ target-sh4/ target-sparc/ target-unicore32/ target-xtensa/ tcg/ tests/ trace/ ui/ util/
9ae3a8
offsets_size = s->n_blocks 0001-usb-host-move-legacy-cmd-line-bits.patch 0002-usb-host-remove-usb_host_device_close.patch 0003-use-libusb-for-usb-host.patch 0004-libusb-spec-file-windup.patch aio-posix.c aio-win32.c arch_init.c async.c audio backends balloon.c block block.c blockdev.c blockdev-nbd.c blockjob.c block-migration.c bsd-user bt-host.c bt-vhci.c Changelog cmd.c cmd.h CODING_STYLE configure COPYING COPYING.LIB coroutine-gthread.c coroutine-sigaltstack.c coroutine-ucontext.c coroutine-win32.c cpu-exec.c cpus.c cputlb.c default-configs device-hotplug.c device_tree.c disas disas.c dma-helpers.c docs dtc dump.c exec.c fpu fsdev gdbstub.c gdb-xml HACKING hmp.c hmp-commands.hx hmp.h hw include iohandler.c ioport.c kvm-all.c kvm-stub.c kvmvapic-add-ioport-read-accessor.patch ldscripts libcacard LICENSE linux-headers linux-user main-loop.c MAINTAINERS Makefile Makefile.objs Makefile.target memory.c memory_mapping.c memory_mapping-stub.c migration.c migration-exec.c migration-fd.c migration-rdma.c migration-tcp.c migration-unix.c monitor.c nbd.c net os-posix.c os-win32.c page_cache.c pc-bios pixman po qapi qapi-schema.json qdev-monitor.c qdict-test-data.txt qemu-bridge-helper.c qemu-char.c qemu-coroutine.c qemu-coroutine-io.c qemu-coroutine-lock.c qemu-coroutine-sleep.c qemu-doc.texi qemu-img.c qemu-img-cmds.hx qemu-img.texi qemu-io.c qemu-log.c qemu-nbd.c qemu-nbd.texi qemu-options.h qemu-options.hx qemu-options-wrapper.h qemu.sasl qemu-seccomp.c qemu-tech.texi qemu-timer.c qga QMP qmp.c qmp-commands.hx qobject qom qtest.c readline.c README redhat roms rules.mak savevm.c scripts slirp spice-qemu-char.c stubs sysconfigs target-alpha target-arm target-cris target-i386 target-lm32 target-m68k target-microblaze target-mips target-moxie target-openrisc target-ppc target-s390x target-sh4 target-sparc target-unicore32 target-xtensa tcg tcg-runtime.c tci.c tests thread-pool.c thunk.c tpm.c trace trace-events translate-all.c translate-all.h ui user-exec.c util VERSION version.rc vl.c vl.c.orig xbzrle.c xen-all.c xen-mapcache.c xen-stub.c sizeof(uint64_t);
9ae3a8
s->offsets = g_malloc(offsets_size);
9ae3a8
9ae3a8
[...]
9ae3a8
9ae3a8
for(i=0;i<s->n_blocks;i++) {
9ae3a8
s->offsets[i] = be64_to_cpu(s->offsets[i]);
9ae3a8
9ae3a8
offsets_size can be smaller than n_blocks due to integer overflow.
9ae3a8
Therefore s->offsets[] is too small when the for loop byteswaps offsets.
9ae3a8
9ae3a8
This patch refuses to open files if offsets_size would overflow.
9ae3a8
9ae3a8
Note that changing the type of offsets_size is not a fix since 32-bit
9ae3a8
hosts still only have 32-bit size_t.
9ae3a8
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block/cloop.c              |    7 +++++++
9ae3a8
 tests/qemu-iotests/075     |    7 +++++++
9ae3a8
 tests/qemu-iotests/075.out |    4 ++++
9ae3a8
 3 files changed, 18 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/cloop.c b/block/cloop.c
9ae3a8
index f021663..563e916 100644
9ae3a8
--- a/block/cloop.c
9ae3a8
+++ b/block/cloop.c
9ae3a8
@@ -99,6 +99,13 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
     s->n_blocks = be32_to_cpu(s->n_blocks);
9ae3a8
 
9ae3a8
     /* read offsets */
9ae3a8
+    if (s->n_blocks > UINT32_MAX / sizeof(uint64_t)) {
9ae3a8
+        /* Prevent integer overflow */
9ae3a8
+        error_setg(errp, "n_blocks %u must be %zu or less",
9ae3a8
+                   s->n_blocks,
9ae3a8
+                   UINT32_MAX / sizeof(uint64_t));
9ae3a8
+        return -EINVAL;
9ae3a8
+    }
9ae3a8
     offsets_size = s->n_blocks * sizeof(uint64_t);
9ae3a8
     s->offsets = g_malloc(offsets_size);
9ae3a8
 
9ae3a8
diff --git a/tests/qemu-iotests/075 b/tests/qemu-iotests/075
9ae3a8
index 8f54a99..9ce6b1f 100755
9ae3a8
--- a/tests/qemu-iotests/075
9ae3a8
+++ b/tests/qemu-iotests/075
9ae3a8
@@ -43,6 +43,7 @@ _supported_proto generic
9ae3a8
 _supported_os Linux
9ae3a8
 
9ae3a8
 block_size_offset=128
9ae3a8
+n_blocks_offset=132
9ae3a8
 
9ae3a8
 echo
9ae3a8
 echo "== check that the first sector can be read =="
9ae3a8
@@ -67,6 +68,12 @@ _use_sample_img simple-pattern.cloop.bz2
9ae3a8
 poke_file "$TEST_IMG" "$block_size_offset" "\xff\xff\xfe\x00"
9ae3a8
 $QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
 
9ae3a8
+echo
9ae3a8
+echo "== offsets_size overflow ==="
9ae3a8
+_use_sample_img simple-pattern.cloop.bz2
9ae3a8
+poke_file "$TEST_IMG" "$n_blocks_offset" "\xff\xff\xff\xff"
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/075.out b/tests/qemu-iotests/075.out
9ae3a8
index d362c95..a771789 100644
9ae3a8
--- a/tests/qemu-iotests/075.out
9ae3a8
+++ b/tests/qemu-iotests/075.out
9ae3a8
@@ -15,4 +15,8 @@ no file open, try 'help open'
9ae3a8
 == huge block_size ===
9ae3a8
 qemu-io: can't open device TEST_DIR/simple-pattern.cloop: block_size 4294966784 must be 64 MB or less
9ae3a8
 no file open, try 'help open'
9ae3a8
+
9ae3a8
+== offsets_size overflow ===
9ae3a8
+qemu-io: can't open device TEST_DIR/simple-pattern.cloop: n_blocks 4294967295 must be 536870911 or less
9ae3a8
+no file open, try 'help open'
9ae3a8
 *** done
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8