From patchwork Tue May 14 14:54:28 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 243735 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id BCD032C00C4 for ; Wed, 15 May 2013 00:55:20 +1000 (EST) Received: from localhost ([::1]:56624 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UcGdL-0005CZ-0s for incoming@patchwork.ozlabs.org; Tue, 14 May 2013 10:55:19 -0400 Received: from eggs.gnu.org ([208.118.235.92]:38056) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UcGcm-00054Y-IV for qemu-devel@nongnu.org; Tue, 14 May 2013 10:54:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UcGch-0008O7-IA for qemu-devel@nongnu.org; Tue, 14 May 2013 10:54:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34182) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UcGch-0008Nk-7y for qemu-devel@nongnu.org; Tue, 14 May 2013 10:54:39 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r4EEscMi013492 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 14 May 2013 10:54:38 -0400 Received: from localhost (ovpn-112-36.ams2.redhat.com [10.36.112.36]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r4EEsaeQ001825; Tue, 14 May 2013 10:54:37 -0400 From: Stefan Hajnoczi To: Date: Tue, 14 May 2013 16:54:28 +0200 Message-Id: <1368543269-29784-2-git-send-email-stefanha@redhat.com> In-Reply-To: <1368543269-29784-1-git-send-email-stefanha@redhat.com> References: <1368543269-29784-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 1/2] qcow2: Catch some L1 table index overflows X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Kevin Wolf This catches the situation that is described in the bug report at https://bugs.launchpad.net/qemu/+bug/865518 and goes like this: $ qemu-img create -f qcow2 huge.qcow2 $((1024*1024))T Formatting 'huge.qcow2', fmt=qcow2 size=1152921504606846976 encryption=off cluster_size=65536 lazy_refcounts=off $ qemu-io /tmp/huge.qcow2 -c "write $((1024*1024*1024*1024*1024*1024 - 1024)) 512" Segmentation fault With this patch applied the segfault will be avoided, however the case will still fail, though gracefully: $ qemu-img create -f qcow2 /tmp/huge.qcow2 $((1024*1024))T Formatting 'huge.qcow2', fmt=qcow2 size=1152921504606846976 encryption=off cluster_size=65536 lazy_refcounts=off qemu-img: The image size is too large for file format 'qcow2' Note that even long before these overflow checks kick in, you get insanely high memory usage (up to INT_MAX * sizeof(uint64_t) = 16 GB for the L1 table), so with somewhat smaller image sizes you'll probably see qemu aborting for a failed g_malloc(). If you need huge image sizes, you should increase the cluster size to the maximum of 2 MB in order to get higher limits. Signed-off-by: Kevin Wolf Signed-off-by: Stefan Hajnoczi --- block/qcow2-cluster.c | 23 +++++++++++++++-------- block/qcow2.c | 13 +++++++++++-- block/qcow2.h | 5 +++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index c71470a..76f30e5 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -29,12 +29,13 @@ #include "block/qcow2.h" #include "trace.h" -int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size) +int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, + bool exact_size) { BDRVQcowState *s = bs->opaque; - int new_l1_size, new_l1_size2, ret, i; + int new_l1_size2, ret, i; uint64_t *new_l1_table; - int64_t new_l1_table_offset; + int64_t new_l1_table_offset, new_l1_size; uint8_t data[12]; if (min_size <= s->l1_size) @@ -53,8 +54,13 @@ int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size) } } + if (new_l1_size > INT_MAX) { + return -EFBIG; + } + #ifdef DEBUG_ALLOC2 - fprintf(stderr, "grow l1_table from %d to %d\n", s->l1_size, new_l1_size); + fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n", + s->l1_size, new_l1_size); #endif new_l1_size2 = sizeof(uint64_t) * new_l1_size; @@ -391,8 +397,8 @@ int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, int *num, uint64_t *cluster_offset) { BDRVQcowState *s = bs->opaque; - unsigned int l1_index, l2_index; - uint64_t l2_offset, *l2_table; + unsigned int l2_index; + uint64_t l1_index, l2_offset, *l2_table; int l1_bits, c; unsigned int index_in_cluster, nb_clusters; uint64_t nb_available, nb_needed; @@ -507,8 +513,8 @@ static int get_cluster_table(BlockDriverState *bs, uint64_t offset, int *new_l2_index) { BDRVQcowState *s = bs->opaque; - unsigned int l1_index, l2_index; - uint64_t l2_offset; + unsigned int l2_index; + uint64_t l1_index, l2_offset; uint64_t *l2_table = NULL; int ret; @@ -522,6 +528,7 @@ static int get_cluster_table(BlockDriverState *bs, uint64_t offset, } } + assert(l1_index < s->l1_size); l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK; /* seek the l2 table of the given l2 offset */ diff --git a/block/qcow2.c b/block/qcow2.c index 2e346d8..0fa5cb2 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -307,6 +307,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags) QemuOpts *opts; Error *local_err = NULL; uint64_t ext_end; + uint64_t l1_vm_state_index; ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); if (ret < 0) { @@ -424,7 +425,14 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags) /* read the level 1 table */ s->l1_size = header.l1_size; - s->l1_vm_state_index = size_to_l1(s, header.size); + + l1_vm_state_index = size_to_l1(s, header.size); + if (l1_vm_state_index > INT_MAX) { + ret = -EFBIG; + goto fail; + } + s->l1_vm_state_index = l1_vm_state_index; + /* the L1 table must contain at least enough entries to put header.size bytes */ if (s->l1_size < s->l1_vm_state_index) { @@ -1480,7 +1488,8 @@ static coroutine_fn int qcow2_co_discard(BlockDriverState *bs, static int qcow2_truncate(BlockDriverState *bs, int64_t offset) { BDRVQcowState *s = bs->opaque; - int ret, new_l1_size; + int64_t new_l1_size; + int ret; if (offset & 511) { error_report("The new size must be a multiple of 512"); diff --git a/block/qcow2.h b/block/qcow2.h index 9421843..6959c6a 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -284,7 +284,7 @@ static inline int size_to_clusters(BDRVQcowState *s, int64_t size) return (size + (s->cluster_size - 1)) >> s->cluster_bits; } -static inline int size_to_l1(BDRVQcowState *s, int64_t size) +static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size) { int shift = s->cluster_bits + s->l2_bits; return (size + (1ULL << shift) - 1) >> shift; @@ -360,7 +360,8 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix); /* qcow2-cluster.c functions */ -int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size); +int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, + bool exact_size); void qcow2_l2_cache_reset(BlockDriverState *bs); int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,