From patchwork Thu Jan 23 03:04:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 313443 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 EDA222C0099 for ; Thu, 23 Jan 2014 14:07:47 +1100 (EST) Received: from localhost ([::1]:38621 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W6Adt-0007Iy-Dx for incoming@patchwork.ozlabs.org; Wed, 22 Jan 2014 22:07:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53933) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W6AdE-00076t-Ac for qemu-devel@nongnu.org; Wed, 22 Jan 2014 22:07:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W6Ad9-0002BP-Ig for qemu-devel@nongnu.org; Wed, 22 Jan 2014 22:07:04 -0500 Received: from [222.73.24.84] (port=30874 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W6Ad9-00029M-5U for qemu-devel@nongnu.org; Wed, 22 Jan 2014 22:06:59 -0500 X-IronPort-AV: E=Sophos;i="4.95,704,1384272000"; d="scan'208";a="9445978" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 23 Jan 2014 11:03:15 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id s0N36t11025667; Thu, 23 Jan 2014 11:06:55 +0800 Received: from G08FNSTD100614.fnst.cn.fujitsu.com ([10.167.226.102]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2014012311044912-1301300 ; Thu, 23 Jan 2014 11:04:49 +0800 From: Hu Tao To: qemu-devel@nongnu.org Date: Thu, 23 Jan 2014 11:04:06 +0800 Message-Id: X-Mailer: git-send-email 1.8.5.2.229.g4448466 In-Reply-To: References: X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2014/01/23 11:04:49, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2014/01/23 11:05:30, Serialize complete at 2014/01/23 11:05:30 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 222.73.24.84 Cc: Kevin Wolf Subject: [Qemu-devel] [PATCH v4 2/4] qcow2: fix offset overflow in qcow2_alloc_clusters_at() 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 When cluster size is big enough it can lead offset overflow in qcow2_alloc_clusters_at(). This patch fixes it. The allocation each time is stopped at L2 table boundary (see handle_alloc()), so the possible maximum bytes could be 2^(cluster_bits - 3 + cluster_bits) so int is safe for cluster_bits<=17, unsafe otherwise. Reviewed-by: Max Reitz Signed-off-by: Hu Tao --- block/qcow2-refcount.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index c974abe..8712d8b 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -676,7 +676,13 @@ int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, BDRVQcowState *s = bs->opaque; uint64_t cluster_index; uint64_t old_free_cluster_index; - int i, refcount, ret; + uint64_t i; + int refcount, ret; + + assert(nb_clusters >= 0); + if (nb_clusters == 0) { + return 0; + } /* Check how many clusters there are free */ cluster_index = offset >> s->cluster_bits;