From 3c33195d5213f9e760765fb4ba93a1aa95875e9f Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Tue, 25 Mar 2014 11:45:21 +0100 Subject: [PATCH 03/48] qcow2: Check refcount table size (CVE-2014-0144) RH-Author: Kevin Wolf Message-id: <1395744364-16049-3-git-send-email-kwolf@redhat.com> Patchwork-id: n/a O-Subject: [EMBARGOED RHEL-6.6/6.5.z qemu-kvm PATCH v2 02/45] qcow2: Check refcount table size (CVE-2014-0144) Bugzilla: 1079453 RH-Acked-by: Max Reitz RH-Acked-by: Stefan Hajnoczi RH-Acked-by: Jeff Cody Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1079453 Upstream status: Embargoed Limit the in-memory reference count table size to 8 MB, it's enough in practice. This fixes an unbounded allocation as well as a buffer overflow in qcow2_refcount_init(). Signed-off-by: Kevin Wolf Conflicts: tests/qemu-iotests/080 tests/qemu-iotests/080.out Signed-off-by: Kevin Wolf --- block/qcow2-refcount.c | 4 +++- block/qcow2.c | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 30570c9..9f79654 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -38,8 +38,10 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, int qcow2_refcount_init(BlockDriverState *bs) { BDRVQcowState *s = bs->opaque; - int ret, refcount_table_size2, i; + unsigned int refcount_table_size2, i; + int ret; + assert(s->refcount_table_size <= INT_MAX / sizeof(uint64_t)); refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t); s->refcount_table = g_malloc(refcount_table_size2); if (s->refcount_table_size > 0) { diff --git a/block/qcow2.c b/block/qcow2.c index f402cd7..0cd126e 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -227,10 +227,19 @@ static int qcow2_open(BlockDriverState *bs, int flags) s->csize_shift = (62 - (s->cluster_bits - 8)); s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; s->cluster_offset_mask = (1LL << s->csize_shift) - 1; + s->refcount_table_offset = header.refcount_table_offset; s->refcount_table_size = header.refcount_table_clusters << (s->cluster_bits - 3); + if (header.refcount_table_clusters > (0x800000 >> s->cluster_bits)) { + /* 8 MB refcount table is enough for 2 PB images at 64k cluster size + * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */ + qerror_report(QERR_GENERIC_ERROR, "Reference count table too large"); + ret = -EINVAL; + goto fail; + } + s->snapshots_offset = header.snapshots_offset; s->nb_snapshots = header.nb_snapshots; -- 1.7.1