From 580fdae537e1a0296c02c2700b3bd22d5057a4cf Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Tue, 25 Mar 2014 11:46:01 +0100 Subject: [PATCH 43/48] dmg: prevent chunk buffer overflow (CVE-2014-0145) RH-Author: Kevin Wolf Message-id: <1395744364-16049-43-git-send-email-kwolf@redhat.com> Patchwork-id: n/a O-Subject: [EMBARGOED RHEL-6.6/6.5.z qemu-kvm PATCH v2 42/45] dmg: prevent chunk buffer overflow (CVE-2014-0145) Bugzilla: 1079324 RH-Acked-by: Max Reitz RH-Acked-by: Stefan Hajnoczi RH-Acked-by: Laszlo Ersek Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1079324 Upstream status: Embargoed Both compressed and uncompressed I/O is buffered. dmg_open() calculates the maximum buffer size needed from the metadata in the image file. There is currently a buffer overflow since ->lengths[] is accounted against the maximum compressed buffer size but actually uses the uncompressed buffer: switch (s->types[chunk]) { case 1: /* copy */ ret = bdrv_pread(bs->file, s->offsets[chunk], s->uncompressed_chunk, s->lengths[chunk]); We must account against the maximum uncompressed buffer size for type=1 chunks. This patch fixes the maximum buffer size calculation to take into account the chunk type. It is critical that we update the correct maximum since there are two buffers ->compressed_chunk and ->uncompressed_chunk. Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf Conflicts: block/dmg.c Signed-off-by: Kevin Wolf --- block/dmg.c | 39 +++++++++++++++++++++++++++++++++------ 1 files changed, 33 insertions(+), 6 deletions(-) diff --git a/block/dmg.c b/block/dmg.c index 93786fe..dd53188 100644 --- a/block/dmg.c +++ b/block/dmg.c @@ -93,6 +93,37 @@ static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result) return 0; } +/* Increase max chunk sizes, if necessary. This function is used to calculate + * the buffer sizes needed for compressed/uncompressed chunk I/O. + */ +static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk, + uint32_t *max_compressed_size, + uint32_t *max_sectors_per_chunk) +{ + uint32_t compressed_size = 0; + uint32_t uncompressed_sectors = 0; + + switch (s->types[chunk]) { + case 0x80000005: /* zlib compressed */ + compressed_size = s->lengths[chunk]; + uncompressed_sectors = s->sectorcounts[chunk]; + break; + case 1: /* copy */ + uncompressed_sectors = (s->lengths[chunk] + 511) / 512; + break; + case 2: /* zero */ + uncompressed_sectors = s->sectorcounts[chunk]; + break; + } + + if (compressed_size > *max_compressed_size) { + *max_compressed_size = compressed_size; + } + if (uncompressed_sectors > *max_sectors_per_chunk) { + *max_sectors_per_chunk = uncompressed_sectors; + } +} + static int dmg_open(BlockDriverState *bs, int flags) { BDRVDMGState *s = bs->opaque; @@ -237,12 +268,8 @@ static int dmg_open(BlockDriverState *bs, int flags) goto fail; } - if (s->lengths[i] > max_compressed_size) { - max_compressed_size = s->lengths[i]; - } - if (s->sectorcounts[i] > max_sectors_per_chunk) { - max_sectors_per_chunk = s->sectorcounts[i]; - } + update_max_chunk_size(s, i, &max_compressed_size, + &max_sectors_per_chunk); } s->n_chunks += chunk_count; } -- 1.7.1