From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58373) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wgn9b-0006Oi-5j for qemu-devel@nongnu.org; Sat, 03 May 2014 23:31:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wgn9V-0005nt-0u for qemu-devel@nongnu.org; Sat, 03 May 2014 23:31:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45800) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wgn9U-0005nj-PI for qemu-devel@nongnu.org; Sat, 03 May 2014 23:31:44 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s443VhTJ025104 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sat, 3 May 2014 23:31:43 -0400 From: Max Reitz Date: Sun, 4 May 2014 05:31:40 +0200 Message-Id: <1399174300-27583-1-git-send-email-mreitz@redhat.com> Subject: [Qemu-devel] [PATCH] qcow2: Fix alloc_clusters_noref() overflow detection List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Stefan Hajnoczi , Max Reitz If the very first allocation has a length of 0, the free_cluster_index is still 0 after the for loop, which means that subtracting one from it will underflow and signal an invalid range of clusters by returning -EFBIG. However, there is no such range, as its length is 0. Fix this by preventing underflows on free_cluster_index during the check. Signed-off-by: Max Reitz --- block/qcow2-refcount.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index e79895d..9507aef 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -656,7 +656,9 @@ retry: /* Make sure that all offsets in the "allocated" range are representable * in an int64_t */ - if (s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits)) { + if (s->free_cluster_index > 0 && + s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits)) + { return -EFBIG; } -- 1.9.2