9ae3a8
From eabeace95bfc1dd3c2280105c4fa83195c7bd4bd Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Tue, 25 Mar 2014 14:23:54 +0100
9ae3a8
Subject: [PATCH 47/49] parallels: Fix catalog size integer overflow (CVE-2014-0143)
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1395753835-7591-48-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: n/a
9ae3a8
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 47/48] parallels: Fix catalog 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
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1079320
9ae3a8
Upstream status: Embargoed
9ae3a8
9ae3a8
The first test case would cause a huge memory allocation, leading to a
9ae3a8
qemu abort; the second one to a too small malloc() for the catalog
9ae3a8
(smaller than s->catalog_size), which causes a read-only out-of-bounds
9ae3a8
array access and on big endian hosts an endianess conversion for an
9ae3a8
undefined memory area.
9ae3a8
9ae3a8
The sample image used here is not an original Parallels image. It was
9ae3a8
created using an hexeditor on the basis of the struct that qemu uses.
9ae3a8
Good enough for trying to crash the driver, but not for ensuring
9ae3a8
compatibility.
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
tests/qemu-iotests/group
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block/parallels.c                                  |    7 ++-
9ae3a8
 tests/qemu-iotests/076                             |   69 ++++++++++++++++++++
9ae3a8
 tests/qemu-iotests/076.out                         |   14 ++++
9ae3a8
 tests/qemu-iotests/common                          |    7 ++
9ae3a8
 tests/qemu-iotests/group                           |    1 +
9ae3a8
 .../qemu-iotests/sample_images/fake.parallels.bz2  |  Bin 0 -> 141 bytes
9ae3a8
 6 files changed, 97 insertions(+), 1 deletions(-)
9ae3a8
 create mode 100755 tests/qemu-iotests/076
9ae3a8
 create mode 100644 tests/qemu-iotests/076.out
9ae3a8
 create mode 100644 tests/qemu-iotests/sample_images/fake.parallels.bz2
9ae3a8
9ae3a8
diff --git a/block/parallels.c b/block/parallels.c
9ae3a8
index 2121e43..5d1c0af 100644
9ae3a8
--- a/block/parallels.c
9ae3a8
+++ b/block/parallels.c
9ae3a8
@@ -49,7 +49,7 @@ typedef struct BDRVParallelsState {
9ae3a8
     CoMutex lock;
9ae3a8
 
9ae3a8
     uint32_t *catalog_bitmap;
9ae3a8
-    int catalog_size;
9ae3a8
+    unsigned int catalog_size;
9ae3a8
 
9ae3a8
     int tracks;
9ae3a8
 } BDRVParallelsState;
9ae3a8
@@ -94,6 +94,11 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
     s->tracks = le32_to_cpu(ph.tracks);
9ae3a8
 
9ae3a8
     s->catalog_size = le32_to_cpu(ph.catalog_entries);
9ae3a8
+    if (s->catalog_size > INT_MAX / 4) {
9ae3a8
+        error_setg(errp, "Catalog too large");
9ae3a8
+        ret = -EFBIG;
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
     s->catalog_bitmap = g_malloc(s->catalog_size * 4);
9ae3a8
 
9ae3a8
     ret = bdrv_pread(bs->file, 64, s->catalog_bitmap, s->catalog_size * 4);
9ae3a8
diff --git a/tests/qemu-iotests/076 b/tests/qemu-iotests/076
9ae3a8
new file mode 100755
9ae3a8
index 0000000..6028ac5
9ae3a8
--- /dev/null
9ae3a8
+++ b/tests/qemu-iotests/076
9ae3a8
@@ -0,0 +1,69 @@
9ae3a8
+#!/bin/bash
9ae3a8
+#
9ae3a8
+# parallels format input validation tests
9ae3a8
+#
9ae3a8
+# Copyright (C) 2013 Red Hat, Inc.
9ae3a8
+#
9ae3a8
+# This program is free software; you can redistribute it and/or modify
9ae3a8
+# it under the terms of the GNU General Public License as published by
9ae3a8
+# the Free Software Foundation; either version 2 of the License, or
9ae3a8
+# (at your option) any later version.
9ae3a8
+#
9ae3a8
+# This program is distributed in the hope that it will be useful,
9ae3a8
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
9ae3a8
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9ae3a8
+# GNU General Public License for more details.
9ae3a8
+#
9ae3a8
+# You should have received a copy of the GNU General Public License
9ae3a8
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
9ae3a8
+#
9ae3a8
+
9ae3a8
+# creator
9ae3a8
+owner=kwolf@redhat.com
9ae3a8
+
9ae3a8
+seq=`basename $0`
9ae3a8
+echo "QA output created by $seq"
9ae3a8
+
9ae3a8
+here=`pwd`
9ae3a8
+tmp=/tmp/$$
9ae3a8
+status=1	# failure is the default!
9ae3a8
+
9ae3a8
+_cleanup()
9ae3a8
+{
9ae3a8
+	_cleanup_test_img
9ae3a8
+}
9ae3a8
+trap "_cleanup; exit \$status" 0 1 2 3 15
9ae3a8
+
9ae3a8
+# get standard environment, filters and checks
9ae3a8
+. ./common.rc
9ae3a8
+. ./common.filter
9ae3a8
+
9ae3a8
+_supported_fmt parallels
9ae3a8
+_supported_proto generic
9ae3a8
+_supported_os Linux
9ae3a8
+
9ae3a8
+catalog_entries_offset=$((0x20))
9ae3a8
+nb_sectors_offset=$((0x24))
9ae3a8
+
9ae3a8
+echo
9ae3a8
+echo "== Read from a valid (enough) image =="
9ae3a8
+_use_sample_img fake.parallels.bz2
9ae3a8
+{ $QEMU_IO -c "read -P 0x11 0 64k" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+
9ae3a8
+echo
9ae3a8
+echo "== Negative catalog size =="
9ae3a8
+_use_sample_img fake.parallels.bz2
9ae3a8
+poke_file "$TEST_IMG" "$catalog_entries_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 in catalog allocation =="
9ae3a8
+_use_sample_img fake.parallels.bz2
9ae3a8
+poke_file "$TEST_IMG" "$nb_sectors_offset" "\xff\xff\xff\xff"
9ae3a8
+poke_file "$TEST_IMG" "$catalog_entries_offset" "\x01\x00\x00\x40"
9ae3a8
+{ $QEMU_IO -c "read 64M 64M" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+
9ae3a8
+# success, all done
9ae3a8
+echo "*** done"
9ae3a8
+rm -f $seq.full
9ae3a8
+status=0
9ae3a8
diff --git a/tests/qemu-iotests/076.out b/tests/qemu-iotests/076.out
9ae3a8
new file mode 100644
9ae3a8
index 0000000..12af42a
9ae3a8
--- /dev/null
9ae3a8
+++ b/tests/qemu-iotests/076.out
9ae3a8
@@ -0,0 +1,14 @@
9ae3a8
+QA output created by 076
9ae3a8
+
9ae3a8
+== Read from a valid (enough) image ==
9ae3a8
+read 65536/65536 bytes at offset 0
9ae3a8
+64 KiB, 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/fake.parallels: Catalog too large
9ae3a8
+no file open, try 'help open'
9ae3a8
+
9ae3a8
+== Overflow in catalog allocation ==
9ae3a8
+qemu-io: can't open device TEST_DIR/fake.parallels: Catalog too large
9ae3a8
+no file open, try 'help open'
9ae3a8
+*** done
9ae3a8
diff --git a/tests/qemu-iotests/common b/tests/qemu-iotests/common
9ae3a8
index 35abbfc..f8c1b56 100644
9ae3a8
--- a/tests/qemu-iotests/common
9ae3a8
+++ b/tests/qemu-iotests/common
9ae3a8
@@ -131,6 +131,7 @@ check options
9ae3a8
     -bochs              test bochs
9ae3a8
     -cow                test cow
9ae3a8
     -cloop              test cloop
9ae3a8
+    -parallels          test parallels
9ae3a8
     -qcow               test qcow
9ae3a8
     -qcow2              test qcow2
9ae3a8
     -qed                test qed
9ae3a8
@@ -181,6 +182,12 @@ testlist options
9ae3a8
             xpand=false
9ae3a8
             ;;
9ae3a8
 
9ae3a8
+        -parallels)
9ae3a8
+            IMGFMT=parallels
9ae3a8
+            IMGFMT_GENERIC=false
9ae3a8
+            xpand=false
9ae3a8
+            ;;
9ae3a8
+
9ae3a8
         -qcow)
9ae3a8
             IMGFMT=qcow
9ae3a8
             xpand=false
9ae3a8
diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
9ae3a8
index e8ad780..ad96fcf 100644
9ae3a8
--- a/tests/qemu-iotests/group
9ae3a8
+++ b/tests/qemu-iotests/group
9ae3a8
@@ -72,6 +72,7 @@
9ae3a8
 068 rw auto
9ae3a8
 070 rw auto
9ae3a8
 075 rw auto
9ae3a8
+076 auto
9ae3a8
 077 rw auto
9ae3a8
 078 rw auto
9ae3a8
 079 rw auto
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8