Blame SOURCES/kvm-dmg-sanitize-chunk-length-and-sectorcount-CVE-2014-0.patch

0a122b
From ec4d254830e3da2ebb1cd9fec16c407e5b644ca6 Mon Sep 17 00:00:00 2001
0a122b
From: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
Date: Tue, 25 Mar 2014 14:23:44 +0100
0a122b
Subject: [PATCH 37/49] dmg: sanitize chunk length and sectorcount (CVE-2014-0145)
0a122b
0a122b
RH-Author: Kevin Wolf <kwolf@redhat.com>
0a122b
Message-id: <1395753835-7591-38-git-send-email-kwolf@redhat.com>
0a122b
Patchwork-id: n/a
0a122b
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 37/48] dmg: sanitize chunk length and sectorcount (CVE-2014-0145)
0a122b
Bugzilla: 1079325
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
From: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
0a122b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1079325
0a122b
Upstream status: Embargoed
0a122b
0a122b
Chunk length and sectorcount are used for decompression buffers as well
0a122b
as the bdrv_pread() count argument. Ensure that they have reasonable
0a122b
values so neither memory allocation nor conversion from uint64_t to int
0a122b
will cause problems.
0a122b
0a122b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
---
0a122b
 block/dmg.c |   24 ++++++++++++++++++++++++
0a122b
 1 files changed, 24 insertions(+), 0 deletions(-)
0a122b
0a122b
diff --git a/block/dmg.c b/block/dmg.c
0a122b
index f98c94d..ad253fe 100644
0a122b
--- a/block/dmg.c
0a122b
+++ b/block/dmg.c
0a122b
@@ -27,6 +27,14 @@
0a122b
 #include "qemu/module.h"
0a122b
 #include <zlib.h>
0a122b
 
0a122b
+enum {
0a122b
+    /* Limit chunk sizes to prevent unreasonable amounts of memory being used
0a122b
+     * or truncating when converting to 32-bit types
0a122b
+     */
0a122b
+    DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
0a122b
+    DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
0a122b
+};
0a122b
+
0a122b
 typedef struct BDRVDMGState {
0a122b
     CoMutex lock;
0a122b
     /* each chunk contains a certain number of sectors,
0a122b
@@ -208,6 +216,14 @@ static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
                 }
0a122b
                 offset += 8;
0a122b
 
0a122b
+                if (s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
0a122b
+                    error_report("sector count %" PRIu64 " for chunk %u is "
0a122b
+                                 "larger than max (%u)",
0a122b
+                                 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
0a122b
+                    ret = -EINVAL;
0a122b
+                    goto fail;
0a122b
+                }
0a122b
+
0a122b
                 ret = read_uint64(bs, offset, &s->offsets[i]);
0a122b
                 if (ret < 0) {
0a122b
                     goto fail;
0a122b
@@ -221,6 +237,14 @@ static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
                 }
0a122b
                 offset += 8;
0a122b
 
0a122b
+                if (s->lengths[i] > DMG_LENGTHS_MAX) {
0a122b
+                    error_report("length %" PRIu64 " for chunk %u is larger "
0a122b
+                                 "than max (%u)",
0a122b
+                                 s->lengths[i], i, DMG_LENGTHS_MAX);
0a122b
+                    ret = -EINVAL;
0a122b
+                    goto fail;
0a122b
+                }
0a122b
+
0a122b
                 if (s->lengths[i] > max_compressed_size) {
0a122b
                     max_compressed_size = s->lengths[i];
0a122b
                 }
0a122b
-- 
0a122b
1.7.1
0a122b