From nobody Mon Nov 23 04:28:07 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1795097100; cv=none; d=zohomail.com; s=zohoarc; b=G+ZxD3hFHXkiIZzxu12kqMvADoP8C3YHIQFX//z++Styct2SR0eZSpoArVSacj1tnYZnWVk/GJdXFwOMTEQmm4Ssayt4NsTf2u8Ga+6DQNSuHDMCza9nhT/vXmaY67J8orxNO85IXixpG6c9FLYHDgQJZuflv+zkXestpB+obmw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795097100; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=CFjR7KoIzZi0APpVGZa/YSYzouuZgQbuPxDzBcXTztw=; b=bP/flaMyywPcfsemPm5DLohd7bp/Q3vPO81hOLgyn8X5jHI1jPHtCuTTBb2pb2L6nvcoCVyDojAsPtknm2fIeToHWrISqu2LWEnw9iA3ls44HIj7HdvQtwFmktwdI4YYPG5wU0uQwU4Ha0tOJLkC8JYsnSnZlFMf/lTMHdSh6G4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1795097100915245.0031435889597; Thu, 19 Nov 2026 06:05:00 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU2jy-0003ze-Py; Thu, 19 Nov 2026 09:03:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xU2jq-0003x1-7Z for qemu-devel@nongnu.org; Thu, 19 Nov 2026 09:03:46 -0500 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xU2jn-0000S3-6i for qemu-devel@nongnu.org; Thu, 19 Nov 2026 09:03:45 -0500 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-490c3ae81d3so1520203f8f.2 for ; Thu, 19 Nov 2026 06:03:42 -0800 (PST) Received: from lanath.. (wildly.archaic.org.uk. [81.2.115.145]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab53e891d8sm19118f8f.10.2026.11.19.06.03.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2026 06:03:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1795097021; x=1795701821; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CFjR7KoIzZi0APpVGZa/YSYzouuZgQbuPxDzBcXTztw=; b=y2ApfxcGT+Bl68XaQQeDZRboeWtSiH4l34v4rwEj2+vyCxTnKnhDfJWBQtXbXmnVMt 3Ve6/AB8edYBoV/mcz73zy9ACCGWLZlgM3C1epb9meK/Ay+fR2RB1MfbIi0hrwWkfgiU iCPGfN/Ycqwe/jUTBetNWcFR8Ux1i7VBZcVNsmw8uh/uBuon/5bXUCbG5oIFylCrcccN 07zqdnLuU++rZY2ekO2DkhIW8Q0bnrR8oYyzAE+OneUmQQ46gUX0/hJd01GuZGde+0Mj hCe8wdESYlVii2Z34JaMvhx3xpmFRFN53XZOWH/X+C4MOwy0QLE91pj0P/DsGV0RvqIO /PGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795097021; x=1795701821; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=CFjR7KoIzZi0APpVGZa/YSYzouuZgQbuPxDzBcXTztw=; b=HdhzQZ4+s1X0T8cqgHPmLplVHibvVZ9c/RRSFilNFN2Jji8z90hv8e1LurjyrsfZlk xN/I09cil07xWVRQyrnGmmQjx/MexACmaLW137LXjh/PAg9hbckpM5RftbXTTFk0F7tN 64sj2PsXc/XrthGHlehX//epGTzBiXQPxu6dMqr311mqtkypwxXk/Cvk4R6RpghWRIpx EGPBqMk+Gd55PiYBlVYZM3kXuXG0/4WS1kax1s0n+A4rh/jwkcsBsx8Rwyvv8nA8Pc7m 4Lzks9qlLA9rtR6cvuF2R5fBGIpGOjJaC+UDvBpp3DnrrW8/iA84+7Y/ax5nPXuViKiI NWZw== X-Gm-Message-State: APuU4Z+9l/HfVpSb7C7V3FGuqxTerTXIuqFe1h0s3O1rxgJpyY8xROQK jITVdrEdoR4XKe8NvU9/hpZfgXA5HS7X4mJlu6glMOhdmH7BUblmQV5PLalbqnRCzY9xqoJrQco +gP2FW0M= X-Gm-Gg: AZFdTZep3fycSerqXqSnYUtaPBnRxk4GKJ2VctDpEIhDT+q1gC0iBteii5Bjg26mFjD mBhAJePnFcbzquwCR7LiUK4yYPyFmk1Lkb6owhQnkdoHsSUDiJrCiY6iVX8T2RqpVCk3PQlE6bJ xQh3SPSlgHjPFwk7a9FN5+1THc4KS1oLGvgGpMh453rI4xlHaO7RSYJKMXwVlfKN1p1y+I7LMW3 oERDYm/alCHSi9IAJ25TSZgUBAS3PNo3QJDiz7WRfAX+8TfBGja9kc1V0cXRWUC3sWXIOjQDmKw 6FaZwhCKMfzd3fI3+Q1EwE33cQdAxbYlQEqlwpvLOsTTWmoC7LS/R1YzK/9jv9KbmwufdsLGZ8E MK3PpgieTQl+1hc8ZoSZPU4dOqlFYuM2qXHtZTFYpTCyublTBg9lzLTv5k8unmO1xql3IjJ6s3j S2+p9FyJr/kEHmQo7z6E3NSVj+woDixjPw2M9OzsL9o3BEKyl1l+Npwq1arCEGX/3FhiBPQVqOy lhu8eDJE2BruXIf5O6ovjZzWPVjprqZcdDeuEoTpYTWIWXkDqbdWmJ8Ek6ChaOF0JweO6Kfzfoy PiDi7CypIetOGmli6idtjM8GduA/dGB78J3OgGfC+9GFwAxR9WW3hNVk6rMRNB/llbasozvhl1/ pbpDHzABcahLriCyZet5tZsKpQRfxaNBbKw== X-Received: by 2002:a05:6000:4027:b0:48e:932:aefa with SMTP id ffacd0b85a97d-4a63d2d0341mr14147413f8f.26.1795097021431; Thu, 19 Nov 2026 06:03:41 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Kevin Wolf , Hanna Reitz Subject: [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block is empty Date: Thu, 19 Nov 2026 14:03:37 +0000 Message-ID: <20261119140338.2918386-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261119140338.2918386-1-peter.maydell@linaro.org> References: <20261119140338.2918386-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x429.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1795097102135158500 Content-Type: text/plain; charset="utf-8" In a QCOW2 image, the header is always cluster zero, and so that cluster must have a non-zero refcount. This means that an image where the first entry in the refcount table is zero, or where it points to a refblock which is empty, must be corrupt. Detect this in alloc_refblock(). This fixes a bug where 'qemu-img amend -o refcount_bits=3D...' would crash on a corrupted image where the entire refcount table was empty. The crash happened because walk_over_reftable() would return with new_reftable_size =3D=3D 0 and new_reftable =3D=3D NULL. qcow2_change_refcount_table() updated s->refcount_table and s->refcount_table_size with these bogus values, and update_max_refcount_table_index() would crash trying to dereference NULL[UINT_MAX]. We add a defensive assertion here that the new refcount table isn't zero-sized; this can't be reached because it only happens in the corruption case that we now detect. Cc: qemu-stable@nongnu.org Fixes: 791c9a004efbcff ("qcow2: Add function for refcount order amendment") Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3690 Suggested-by: Kevin Wolf Signed-off-by: Peter Maydell Reported-by: a1gxer Reviewed-by: Eric Blake --- block/qcow2-refcount.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index df7f6d2f234..2102386147a 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -3139,6 +3139,14 @@ alloc_refblock(BlockDriverState *bs, uint64_t **reft= able, BDRVQcow2State *s =3D bs->opaque; int64_t offset; =20 + if (reftable_index =3D=3D 0 && refblock_empty) { + /* The header is in cluster 0 so it must have a non-zero refcount = */ + qcow2_signal_corruption(bs, true, -1, -1, + "first refblock is empty"); + error_setg(errp, "Image is corrupt (first refblock is empty)"); + return -EIO; + } + if (!refblock_empty && reftable_index >=3D *reftable_size) { uint64_t *new_reftable; uint64_t new_reftable_size; @@ -3451,6 +3459,13 @@ int qcow2_change_refcount_order(BlockDriverState *bs= , int refcount_order, } } while (new_allocation); =20 + /* + * This can only happen if the reftable is all zeroes, but the header = is + * in cluster 0 and must have a non-zero refcount, and we check that in + * alloc_refblock(). + */ + assert(new_reftable_size !=3D 0); + /* Second, write the new refblocks */ ret =3D walk_over_reftable(bs, &new_reftable, &new_reftable_index, &new_reftable_size, new_refblock, --=20 2.43.0 From nobody Mon Nov 23 04:28:07 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1795097050; cv=none; d=zohomail.com; s=zohoarc; b=mdb2xK8ISg3EcdYsxXDEClnb0clqkei0CvMbHt7q3b4eWkx/kSVuhtE+aeb/7mV+WeA92I11+HY5QlBJV+tjlEsmRW/XlcNxjBFyhpcCi/slULIgD2V6iqh5CoLez8WhhoierndMIONV3VcfCX7z0Md8r5vb1sih47SUEjZEyPg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795097050; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=20ZZPMEs1Mta7IBgztmsk/aVB/t8IiEIowQV5vkdB+w=; b=M/lMjXR/Ad9NG7O3eBlwE5FVkNsiCeaZust8esCDQWLf7VK5w2z9ybsk7Kjd3EuFYm/ZRqiPgSJ8Dqbc4K1xWc8M1ucC7kLEJL8Z1e4NxFdxWYlzMJdhSIKms+H5bjCopJf9zzolMksDPMNqTKbdQmQ//ClQ4CzrUTpQ3ko87uU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1795097050688144.77419151486765; Thu, 19 Nov 2026 06:04:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU2jw-0003zK-Se; Thu, 19 Nov 2026 09:03:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xU2jt-0003yW-ED for qemu-devel@nongnu.org; Thu, 19 Nov 2026 09:03:50 -0500 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xU2jp-0000SO-Fp for qemu-devel@nongnu.org; Thu, 19 Nov 2026 09:03:47 -0500 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-4a358881793so1211504f8f.0 for ; Thu, 19 Nov 2026 06:03:44 -0800 (PST) Received: from lanath.. (wildly.archaic.org.uk. [81.2.115.145]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab53e891d8sm19118f8f.10.2026.11.19.06.03.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2026 06:03:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1795097023; x=1795701823; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=20ZZPMEs1Mta7IBgztmsk/aVB/t8IiEIowQV5vkdB+w=; b=q3pI1FwodsA+XBZ6BaCw1tLbFXNUDgGUij4nO9UfTWfbqtM0qSlMNKWI8SQNEEKIf8 rbp+t+SH2w1KEWoMV8nViSvqJqJm/8iR1RJ6+ZzvH3EIqMYvlOeDcCXDS4PSvrfjLwzD mYP5olSwXv6CjrbdBF7rDjnfbYRAnadqOPVEuQM5tu4zHpwTfxIjNTHB6UQbdRAfYxl1 GltxMaJug/XTjRQULq05xb6KpP0kJp3norAcCS24dJhMnHLs3m+dnEhYOiut5q4X6r2r DieWFqIMn5k4UHn8VObxWcE6AKsq+0wfbROdkkxYboiGpPSn/zI2MQTMuVavb5dvvIwf eABA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795097023; x=1795701823; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=20ZZPMEs1Mta7IBgztmsk/aVB/t8IiEIowQV5vkdB+w=; b=E9lcxzPxliBPF2vKDpEVOi2oTP6d7vcgbk1VmfOuZujv+wCCcaUZIFh3As+6Y2kPQl YiRVD2u9RsK7twF2jDvMHHTtQUAl9LFZjPCVLZOUISFSgnEol9hlUiltooF0d23opKVe BcxcO70g95NZHiRGzgf/V1EvuJU5LprxT9wujFPcvDXvseTE3DIWJHNlv3d/IK92pIB6 QRCA6z+18PKc/fdGzmNXuZW93gTGiwLfHm3KGYCuH/6ZoUkt4khV8U8gHmX46HfnGRCT VSJSqUh0LaFEn9pYFs+InTBymjaqpebgwfXqTDYwwcvqHuvQQsvIISJMqub5al/NE58K qsPQ== X-Gm-Message-State: APuU4Z+VP6cPCarwpePHbxZKycLGWvZk44TGjctCy0YWg1Tvl+k0znPK thR1h7dgaXChkuTMlhwGgQpQZOpWZXO26j0tvYthuDr2KH5F4fxOq75X8jFXvY+rBkMw+yI9cli 8S0tlQJ8= X-Gm-Gg: AZFdTZcleD5pkV2F2QvtsGlmZygXHWSHRkXqapHsM9remhA6xbQM2vl1EJ2GDZatGWu 404xFVOGKmanaqxhW0P2fjqmKeE5fn/CR74WpyyOtd+Cni1KwKKZwbwLc6H/jUAU45E56n50opv MxK7M5KazWNl3iaPS4XykZcPqQVUCT/602B14c0USv4L0zGlYwebi4yGFbFBJwOlp22LFrOnPVj umm6vjW0xpn641TryNpDT0MIIFFQm1GtNiAAK2Dtj2nTwFjJwlG5eaSc6wB2zAnBJQsxEnczZ2H J/mdf6HjL6q91gu99bU/zzX9E7i7GabZRGP/jFkrgkuqJr7vkp+PyDxdXURDNmA/Yyv+MXInFDi oRqJCc1YVFa6ovVrkDNteFxjZuVFAnKWVOosZnEyvAQjT4poI54sCIXftLL8Q/k7utZus4ikKaF 52AghjP834RHUizAqOqE1VTFknVySZPIodT6ZcKInCiC28A04XmpxzQEzH9N6+ZRP61A59vF8QZ O8Dt6l2aCBtCZ8ht/B2Eb+vth7qwtqLY3fa3x1ZP8XSeq38T169x/oa5AF71juSOd92Rm35XKV/ dABZ/R72hA7/bdgOVcAmj/d5dOUy09KhGp+RiuFMIDR+ONVnGmKafPO2zLAmV1FC8b/7wne/5Q6 a1sSd7m8s1cFaom9EhEqMs5tyW7reP01NjzqWo05jNtVDDJzGQFZuaQv+ X-Received: by 2002:a05:6000:1a8d:b0:4a2:ee0f:4fa3 with SMTP id ffacd0b85a97d-4a6346da8e3mr13286867f8f.8.1795097022911; Thu, 19 Nov 2026 06:03:42 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Kevin Wolf , Hanna Reitz Subject: [PATCH for-11.2 2/2] tests/qemu-iotest: Add test for qcow2 image with empty reftable Date: Thu, 19 Nov 2026 14:03:38 +0000 Message-ID: <20261119140338.2918386-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261119140338.2918386-1-peter.maydell@linaro.org> References: <20261119140338.2918386-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x429.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1795097053597158500 Content-Type: text/plain; charset="utf-8" This test creates a qcow2 image, manually zeroes out its entire reftable, and then runs 'qemu-img amend -o refcount_bits=3D64" on it. This used to cause qemu-img to crash; now we will report that the image can't be opened because it's corrupted. Signed-off-by: Peter Maydell Reviewed-by: Eric Blake --- tests/qemu-iotests/315 | 59 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/315.out | 9 ++++++ 2 files changed, 68 insertions(+) create mode 100755 tests/qemu-iotests/315 create mode 100644 tests/qemu-iotests/315.out diff --git a/tests/qemu-iotests/315 b/tests/qemu-iotests/315 new file mode 100755 index 00000000000..ff1a22bddd5 --- /dev/null +++ b/tests/qemu-iotests/315 @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# group: rw auto quick +# +# Test handling of corrupt qcow2 file with all-zeroes reftable +# +# Copyright (c) 2026 Linaro Ltd +# +# SPDX-License-Identifier: GPL-2.0-or-later + +seq=3D`basename $0` +echo "QA output created by $seq" + +status=3D1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +# This tests qcow2-specific low-level functionality +_supported_fmt qcow2 +_supported_proto file +_supported_os Linux +# We are going to check changing refcount_bits from 16 to 64, +# so anything else is not supported +_unsupported_imgopts 'refcount_bits=3D\([^1]\|.\([^6]\|$\)\)' + +echo +echo "=3D=3D=3D Testing empty refcount table =3D=3D=3D" +echo + +# Create an image, then modify it to zero out the entire reftable. +# Check that this corrupted image doesn't cause qemu-img amend to crash. + +_make_test_img -o 'refcount_bits=3D16' 64K + +reftable_ofs=3D$(peek_file_be "$TEST_IMG" 48 8) +reftable_clusters=3D$(peek_file_be "$TEST_IMG" 56 4) +cluster_bits=3D$(peek_file_be "$TEST_IMG" 20 4) +# Cluster size is (1 << cluster_bits). Zero out the data starting +# at reftable_ofs for (cluster_size * reftable_clusters) bytes. +reftable_size=3D$(((1 << cluster_bits) * reftable_clusters)) + +dd "of=3D$TEST_IMG" "bs=3D1" "seek=3D$reftable_ofs" "count=3D$reftable_siz= e" if=3D/dev/zero conv=3Dnotrunc >&/dev/null + +# This should complain that the image is corrupt +$QEMU_IMG amend -f qcow2 -o refcount_bits=3D64 "$TEST_IMG" 2>&1 + +# success, all done +echo +echo '*** done' +rm -f $seq.full +status=3D0 + diff --git a/tests/qemu-iotests/315.out b/tests/qemu-iotests/315.out new file mode 100644 index 00000000000..91b718df5ca --- /dev/null +++ b/tests/qemu-iotests/315.out @@ -0,0 +1,9 @@ +QA output created by 315 + +=3D=3D=3D Testing empty refcount table =3D=3D=3D + +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D65536 +qcow2: Marking image as corrupt: first refblock is empty; further corrupti= on events will be suppressed +qemu-img: Image is corrupt (first refblock is empty) + +*** done --=20 2.43.0