cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-block-cloop-validate-block_size-header-field-CVE-201.patch

9ae3a8
From ba8c0cc73f3a13b5621c8fe44eb8af20ddb9c5d0 Mon Sep 17 00:00:00 2001
9ae3a8
From: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
Date: Tue, 25 Mar 2014 14:23:11 +0100
9ae3a8
Subject: [PATCH 04/49] block/cloop: validate block_size header field (CVE-2014-0144)
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1395753835-7591-5-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: n/a
9ae3a8
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 04/48] block/cloop: validate block_size header field (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
From: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1079455
9ae3a8
Upstream status: Embargoed
9ae3a8
9ae3a8
Avoid unbounded s->uncompressed_block memory allocation by checking that
9ae3a8
the block_size header field has a reasonable value. Also enforce the
9ae3a8
assumption that the value is a non-zero multiple of 512.
9ae3a8
9ae3a8
These constraints conform to cloop 2.639's code so we accept existing
9ae3a8
image files.
9ae3a8
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block/cloop.c              |   23 +++++++++++++++++++++++
9ae3a8
 tests/qemu-iotests/075     |   20 ++++++++++++++++++++
9ae3a8
 tests/qemu-iotests/075.out |   12 ++++++++++++
9ae3a8
 3 files changed, 55 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/cloop.c b/block/cloop.c
9ae3a8
index b907023..f021663 100644
9ae3a8
--- a/block/cloop.c
9ae3a8
+++ b/block/cloop.c
9ae3a8
@@ -26,6 +26,9 @@
9ae3a8
 #include "qemu/module.h"
9ae3a8
 #include <zlib.h>
9ae3a8
 
9ae3a8
+/* Maximum compressed block size */
9ae3a8
+#define MAX_BLOCK_SIZE (64 * 1024 * 1024)
9ae3a8
+
9ae3a8
 typedef struct BDRVCloopState {
9ae3a8
     CoMutex lock;
9ae3a8
     uint32_t block_size;
9ae3a8
@@ -68,6 +71,26 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
     s->block_size = be32_to_cpu(s->block_size);
9ae3a8
+    if (s->block_size % 512) {
9ae3a8
+        error_setg(errp, "block_size %u must be a multiple of 512",
9ae3a8
+                   s->block_size);
9ae3a8
+        return -EINVAL;
9ae3a8
+    }
9ae3a8
+    if (s->block_size == 0) {
9ae3a8
+        error_setg(errp, "block_size cannot be zero");
9ae3a8
+        return -EINVAL;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* cloop's create_compressed_fs.c warns about block sizes beyond 256 KB but
9ae3a8
+     * we can accept more.  Prevent ridiculous values like 4 GB - 1 since we
9ae3a8
+     * need a buffer this big.
9ae3a8
+     */
9ae3a8
+    if (s->block_size > MAX_BLOCK_SIZE) {
9ae3a8
+        error_setg(errp, "block_size %u must be %u MB or less",
9ae3a8
+                   s->block_size,
9ae3a8
+                   MAX_BLOCK_SIZE / (1024 * 1024));
9ae3a8
+        return -EINVAL;
9ae3a8
+    }
9ae3a8
 
9ae3a8
     ret = bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4);
9ae3a8
     if (ret < 0) {
9ae3a8
diff --git a/tests/qemu-iotests/075 b/tests/qemu-iotests/075
9ae3a8
index 88ae8bb..8f54a99 100755
9ae3a8
--- a/tests/qemu-iotests/075
9ae3a8
+++ b/tests/qemu-iotests/075
9ae3a8
@@ -42,11 +42,31 @@ _supported_fmt cloop
9ae3a8
 _supported_proto generic
9ae3a8
 _supported_os Linux
9ae3a8
 
9ae3a8
+block_size_offset=128
9ae3a8
+
9ae3a8
 echo
9ae3a8
 echo "== check that the first sector can be read =="
9ae3a8
 _use_sample_img simple-pattern.cloop.bz2
9ae3a8
 $QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
 
9ae3a8
+echo
9ae3a8
+echo "== block_size must be a multiple of 512 =="
9ae3a8
+_use_sample_img simple-pattern.cloop.bz2
9ae3a8
+poke_file "$TEST_IMG" "$block_size_offset" "\x00\x00\x02\x01"
9ae3a8
+$QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+
9ae3a8
+echo
9ae3a8
+echo "== block_size cannot be zero =="
9ae3a8
+_use_sample_img simple-pattern.cloop.bz2
9ae3a8
+poke_file "$TEST_IMG" "$block_size_offset" "\x00\x00\x00\x00"
9ae3a8
+$QEMU_IO -c "read 0 512" $TEST_IMG 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+
9ae3a8
+echo
9ae3a8
+echo "== huge block_size ==="
9ae3a8
+_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
 # 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 26661fa..d362c95 100644
9ae3a8
--- a/tests/qemu-iotests/075.out
9ae3a8
+++ b/tests/qemu-iotests/075.out
9ae3a8
@@ -3,4 +3,16 @@ QA output created by 075
9ae3a8
 == check that the first sector can be read ==
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
+== block_size must be a multiple of 512 ==
9ae3a8
+qemu-io: can't open device TEST_DIR/simple-pattern.cloop: block_size 513 must be a multiple of 512
9ae3a8
+no file open, try 'help open'
9ae3a8
+
9ae3a8
+== block_size cannot be zero ==
9ae3a8
+qemu-io: can't open device TEST_DIR/simple-pattern.cloop: block_size cannot be zero
9ae3a8
+no file open, try 'help open'
9ae3a8
+
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
 *** done
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8