From patchwork Tue Apr 1 17:19:09 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 336072 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4100D1400BD for ; Wed, 2 Apr 2014 04:29:50 +1100 (EST) Received: from localhost ([::1]:33498 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WV2VP-0006Ri-VC for incoming@patchwork.ozlabs.org; Tue, 01 Apr 2014 13:29:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42222) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WV2NA-0001xH-RJ for qemu-devel@nongnu.org; Tue, 01 Apr 2014 13:21:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WV2N4-0003K2-Lx for qemu-devel@nongnu.org; Tue, 01 Apr 2014 13:21:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58107) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WV2N4-0003Jq-Cy for qemu-devel@nongnu.org; Tue, 01 Apr 2014 13:21:10 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s31HL8Gi009352 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 1 Apr 2014 13:21:08 -0400 Received: from localhost (ovpn-112-69.ams2.redhat.com [10.36.112.69]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s31HL7qk031287; Tue, 1 Apr 2014 13:21:07 -0400 From: Stefan Hajnoczi To: Date: Tue, 1 Apr 2014 19:19:09 +0200 Message-Id: <1396372769-11688-32-git-send-email-stefanha@redhat.com> In-Reply-To: <1396372769-11688-1-git-send-email-stefanha@redhat.com> References: <1396372769-11688-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Peter Maydell , Stefan Hajnoczi Subject: [Qemu-devel] [PULL for-2.0 31/51] qcow2: Check new refcount table size on growth 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 If the size becomes larger than what qcow2_open() would accept, fail the growing operation. Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz Signed-off-by: Stefan Hajnoczi --- block/qcow2-refcount.c | 4 ++++ block/qcow2.c | 4 +--- block/qcow2.h | 9 +++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 561d659..1c78ff8 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -311,6 +311,10 @@ static int alloc_refcount_block(BlockDriverState *bs, uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT); uint64_t blocks_used = DIV_ROUND_UP(cluster_index, refcount_block_clusters); + if (blocks_used > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) { + return -EFBIG; + } + /* And now we need at least one block more for the new metadata */ uint64_t table_size = next_refcount_table_size(s, blocks_used + 1); uint64_t last_table_size; diff --git a/block/qcow2.c b/block/qcow2.c index d4d991c..b64564d 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -609,9 +609,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, 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) */ + if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) { error_setg(errp, "Reference count table too large"); ret = -EINVAL; goto fail; diff --git a/block/qcow2.h b/block/qcow2.h index b153505..4015373 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -40,6 +40,10 @@ #define QCOW_MAX_CRYPT_CLUSTERS 32 #define QCOW_MAX_SNAPSHOTS 65536 +/* 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) */ +#define QCOW_MAX_REFTABLE_SIZE 0x800000 + /* indicate that the refcount of the referenced cluster is exactly one. */ #define QCOW_OFLAG_COPIED (1ULL << 63) /* indicate that the cluster is compressed (they never have the copied flag) */ @@ -410,6 +414,11 @@ static inline int64_t qcow2_vm_state_offset(BDRVQcowState *s) return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); } +static inline uint64_t qcow2_max_refcount_clusters(BDRVQcowState *s) +{ + return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits; +} + static inline int qcow2_get_cluster_type(uint64_t l2_entry) { if (l2_entry & QCOW_OFLAG_COMPRESSED) {