From 68402488dbb9652f3bd7df6602dd484b182cf01f Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Tue, 25 Mar 2014 11:45:23 +0100 Subject: [PATCH 05/48] qcow2: Validate snapshot table offset/size (CVE-2014-0144) RH-Author: Kevin Wolf Message-id: <1395744364-16049-5-git-send-email-kwolf@redhat.com> Patchwork-id: n/a O-Subject: [EMBARGOED RHEL-6.6/6.5.z qemu-kvm PATCH v2 04/45] qcow2: Validate snapshot table offset/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 This avoid unbounded memory allocation and fixes a potential buffer overflow on 32 bit hosts. Signed-off-by: Kevin Wolf Conflicts: block/qcow2-snapshot.c tests/qemu-iotests/080 tests/qemu-iotests/080.out Signed-off-by: Kevin Wolf --- block/qcow2-snapshot.c | 24 ++++-------------------- block/qcow2.c | 15 +++++++++++++++ block/qcow2.h | 24 +++++++++++++++++++++++- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index bf0f20d..5d74175 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -26,26 +26,6 @@ #include "block_int.h" #include "block/qcow2.h" -typedef struct __attribute__((packed)) QCowSnapshotHeader { - /* header is 8 byte aligned */ - uint64_t l1_table_offset; - - uint32_t l1_size; - uint16_t id_str_size; - uint16_t name_size; - - uint32_t date_sec; - uint32_t date_nsec; - - uint64_t vm_clock_nsec; - - uint32_t vm_state_size; - uint32_t extra_data_size; /* for extension */ - /* extra data follows */ - /* id_str follows */ - /* name follows */ -} QCowSnapshotHeader; - void qcow2_free_snapshots(BlockDriverState *bs) { BDRVQcowState *s = bs->opaque; @@ -284,6 +264,10 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) uint64_t *l1_table = NULL; int64_t l1_table_offset; + if (s->nb_snapshots >= QCOW_MAX_SNAPSHOTS) { + return -EFBIG; + } + memset(sn, 0, sizeof(*sn)); /* Generate an ID if it wasn't passed */ diff --git a/block/qcow2.c b/block/qcow2.c index 9ff85a2..faf0e22 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -274,6 +274,21 @@ static int qcow2_open(BlockDriverState *bs, int flags) goto fail; } + /* Snapshot table offset/length */ + if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) { + qerror_report(QERR_GENERIC_ERROR, "Too many snapshots"); + ret = -EINVAL; + goto fail; + } + + ret = validate_table_offset(bs, header.snapshots_offset, + header.nb_snapshots, + sizeof(QCowSnapshotHeader)); + if (ret < 0) { + qerror_report(QERR_GENERIC_ERROR, "Invalid snapshot table offset"); + goto fail; + } + s->snapshots_offset = header.snapshots_offset; s->nb_snapshots = header.nb_snapshots; diff --git a/block/qcow2.h b/block/qcow2.h index 2ea2aa3..5625cf8 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -40,6 +40,7 @@ #define QCOW_CRYPT_AES 1 #define QCOW_MAX_CRYPT_CLUSTERS 32 +#define QCOW_MAX_SNAPSHOTS 65536 /* indicate that the refcount of the referenced cluster is exactly one. */ #define QCOW_OFLAG_COPIED (1LL << 63) @@ -74,6 +75,27 @@ typedef struct QCowHeader { uint64_t snapshots_offset; } QCowHeader; +typedef struct __attribute__((packed)) QCowSnapshotHeader { + /* header is 8 byte aligned */ + uint64_t l1_table_offset; + + uint32_t l1_size; + uint16_t id_str_size; + uint16_t name_size; + + uint32_t date_sec; + uint32_t date_nsec; + + uint64_t vm_clock_nsec; + + uint32_t vm_state_size; + uint32_t extra_data_size; /* for extension */ + /* extra data follows */ + /* id_str follows */ + /* name follows */ +} QCowSnapshotHeader; + + typedef struct QCowSnapshot { uint64_t l1_table_offset; uint32_t l1_size; @@ -131,7 +153,7 @@ typedef struct BDRVQcowState { AES_KEY aes_decrypt_key; uint64_t snapshots_offset; int snapshots_size; - int nb_snapshots; + unsigned int nb_snapshots; QCowSnapshot *snapshots; QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext; -- 1.7.1