From nobody Fri May 3 01:20:18 2024 Delivered-To: importer@patchew.org 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=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; 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 ARC-Seal: i=1; a=rsa-sha256; t=1578597425; cv=none; d=zohomail.com; s=zohoarc; b=PBYfaC3jgoiMVf7XHpUcxzLpyvNX9gyVrI+cIxOZGGRGHXqBkeNihoNkd5VrNcY0Dbnok14uVBKUuAfw2x6BGdx74D5DYwopuYL2BMeOFfNtGUNSt8avMYcU180hoY6VJsM3W4xJAai4DmWo8kkYV/9DY0V3P5G6Hnt9huBW9IU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1578597425; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=DhiaKh5w5AEQ3dxJRd+rhkGo4UZ0bhFyz01VRRPy1lk=; b=Z7S0XpdzA5Pj19m0R2iRaQvZ/u0nFbblXzOG7CK3xAJa5e5pRaDwfT6tTQK7u2FZTXWsLFouzg77uRcRAssPCAu1M3pJOgfQxOIwEUD2AOgM+sxvuftvSLdZe6Hdqurj/n6EjSmXhA/6BpAFAyY51npoUhwMb4vDWKgx8qwfWac= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1578597424972418.6477200053673; Thu, 9 Jan 2020 11:17:04 -0800 (PST) Received: from localhost ([::1]:36524 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipdIl-00054z-2D for importer@patchew.org; Thu, 09 Jan 2020 14:17:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39312) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipdGC-0003WT-SH for qemu-devel@nongnu.org; Thu, 09 Jan 2020 14:14:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ipdGB-0007Jj-GO for qemu-devel@nongnu.org; Thu, 09 Jan 2020 14:14:24 -0500 Received: from fanzine.igalia.com ([178.60.130.6]:58343) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ipdG9-0006BB-G7; Thu, 09 Jan 2020 14:14:22 -0500 Received: from [213.99.255.143] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1ipdFp-0006Mi-Q1; Thu, 09 Jan 2020 20:14:02 +0100 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1ipdF0-0005R0-L2; Thu, 09 Jan 2020 20:13:10 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=DhiaKh5w5AEQ3dxJRd+rhkGo4UZ0bhFyz01VRRPy1lk=; b=Zdt/HR4WjCUD9VjfGQ7GcEirsC/4nwoejewd5T5Hb9T45YfHviBfkwmakAl99FhSOJ6MQ1Yqr6Xu+iMSaTX8j+yM7N629Iu7BadBVdv6PDRiwmd8Rs4+u3EIvteCHwEiLSECyYLNOYmztWk4y3ZZZ2OMU4tyaeYZxtSGPRuTiGChdjt3UU8ZNkyeHXXx4rXWjZpaA8wnLHpIZmGv47Yh7j/r7mfNHmwaSSAV7T1VbTnAIdjk/sS4F+g5XeolDTOopVxNgoMaKxgaHQv21ukfe7eIRc89bmA53CHI8+vptbhUCxKPD3IIET00VSto4uJ069C9oS7cFJ+DbJkqd3Ee9A==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH v2 1/4] qcow2: Require that the virtual size is a multiple of the sector size Date: Thu, 9 Jan 2020 20:12:59 +0100 Message-Id: <6a1cfcbb533b487bac96e1d2282ebf210954d27a.1578596897.git.berto@igalia.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 178.60.130.6 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Nir Soffer Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The qcow2 header specifies the virtual size of the image in bytes, but BlockDriverState stores it as a number of 512-byte sectors. If the user tries to create an image with a size that is not a multiple of the sector size then this is fixed on creation by silently rounding the image size up (see commit c2eb918e32). qcow2_co_truncate() is more strict and returns an error instead. However when an image is opened the virtual size is rounded down, which means that trying to access the last few advertised bytes will result in an error. As seen above QEMU cannot create such images and there's no good use case that would require us to try to handle them so let's just treat them as unsupported. Signed-off-by: Alberto Garcia --- block/qcow2.c | 7 +++++++ docs/interop/qcow2.txt | 3 ++- tests/qemu-iotests/080 | 8 ++++++++ tests/qemu-iotests/080.out | 5 +++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/block/qcow2.c b/block/qcow2.c index 7fbaac8457..87ca2832f0 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1326,6 +1326,13 @@ static int coroutine_fn qcow2_do_open(BlockDriverSta= te *bs, QDict *options, goto fail; } =20 + if (!QEMU_IS_ALIGNED(header.size, BDRV_SECTOR_SIZE)) { + error_setg(errp, "Virtual size is not a multiple of %u", + (unsigned) BDRV_SECTOR_SIZE); + ret =3D -EINVAL; + goto fail; + } + if (header.header_length > sizeof(header)) { s->unknown_header_fields_size =3D header.header_length - sizeof(he= ader); s->unknown_header_fields =3D g_malloc(s->unknown_header_fields_siz= e); diff --git a/docs/interop/qcow2.txt b/docs/interop/qcow2.txt index af5711e533..891f5662d8 100644 --- a/docs/interop/qcow2.txt +++ b/docs/interop/qcow2.txt @@ -40,7 +40,8 @@ The first cluster of a qcow2 image contains the file head= er: with larger cluster sizes. =20 24 - 31: size - Virtual disk size in bytes. + Virtual disk size in bytes. qemu can only handle + sizes that are a multiple of 512 bytes. =20 Note: qemu has an implementation limit of 32 MB as the maximum L1 table size. With a 2 MB cluster diff --git a/tests/qemu-iotests/080 b/tests/qemu-iotests/080 index 4bcb5021e8..6f136d616f 100755 --- a/tests/qemu-iotests/080 +++ b/tests/qemu-iotests/080 @@ -48,6 +48,7 @@ header_size=3D104 =20 offset_backing_file_offset=3D8 offset_backing_file_size=3D16 +offset_virtual_size=3D24 offset_l1_size=3D36 offset_l1_table_offset=3D40 offset_refcount_table_offset=3D48 @@ -197,6 +198,13 @@ poke_file "$TEST_IMG" "$offset_snap1_l1_size" "\x10\x0= 0\x00\x00" { $QEMU_IMG snapshot -d test $TEST_IMG; } 2>&1 | _filter_testdir _check_test_img =20 +echo +echo "=3D=3D Image size not a multiple of the sector size =3D=3D" +_make_test_img 64k +echo "Modifying virtual size to 65535 bytes" +poke_file "$TEST_IMG" "$offset_virtual_size" "\x00\x00\x00\x00\x00\x00\xff= \xff" +{ $QEMU_IMG info $TEST_IMG; } 2>&1 | _filter_testdir | _filter_imgfmt + # success, all done echo "*** done" rm -f $seq.full diff --git a/tests/qemu-iotests/080.out b/tests/qemu-iotests/080.out index 45ab01db8e..aadc817339 100644 --- a/tests/qemu-iotests/080.out +++ b/tests/qemu-iotests/080.out @@ -104,4 +104,9 @@ Data may be corrupted, or further writes to the image m= ay corrupt it. =20 3 leaked clusters were found on the image. This means waste of disk space, but no harm to data. + +=3D=3D Image size not a multiple of the sector size =3D=3D +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D65536 +Modifying virtual size to 65535 bytes +qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Virtual size is not a multip= le of 512 *** done --=20 2.20.1 From nobody Fri May 3 01:20:18 2024 Delivered-To: importer@patchew.org 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=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; 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 ARC-Seal: i=1; a=rsa-sha256; t=1578597616; cv=none; d=zohomail.com; s=zohoarc; b=gH4R0lOYN6ouzzVXdU23LukzmN7emIHy+cSvVgTUxXMlkR0yUW8ZNqamHLMH38dStoNnPF7s7pHPOt9gM8vSz9RdPJJayDgrjgEf0UjrcsuFQLpkjtD762mG49bSBPlSWLJ6gG6n+XQrhDRaOskFxLSuPAXtXx+M0CblA0dRTk8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1578597616; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=6r67467UjiAqot9W1R09uwC8mdguy5+C3Pq/7SaJD5A=; b=Yei0or2QJm3RKkGtnfnE9i9meRBbag+j/oBDNaq/7tc6bZTkrBV280iLe7So0r1dZV+x1zf8tLHlhXtJCTqR7Z+MCv43gzmwNWKSPJTFAYAaQEBcIjvwiuOpycljimCyLbhNHWn4yJ5O9QXkRyaH37IsYosq7ULyezMTxKynH3E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1578597616696575.0985733091346; Thu, 9 Jan 2020 11:20:16 -0800 (PST) Received: from localhost ([::1]:36578 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipdLr-0001AV-DQ for importer@patchew.org; Thu, 09 Jan 2020 14:20:15 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39313) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipdGC-0003WU-Sz for qemu-devel@nongnu.org; Thu, 09 Jan 2020 14:14:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ipdGB-0007J7-ER for qemu-devel@nongnu.org; Thu, 09 Jan 2020 14:14:24 -0500 Received: from fanzine.igalia.com ([178.60.130.6]:58341) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ipdG9-0006BD-G9; Thu, 09 Jan 2020 14:14:22 -0500 Received: from [213.99.255.143] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1ipdFp-0006Mj-Pd; Thu, 09 Jan 2020 20:14:02 +0100 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1ipdF0-0005R2-ME; Thu, 09 Jan 2020 20:13:10 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=6r67467UjiAqot9W1R09uwC8mdguy5+C3Pq/7SaJD5A=; b=nGk0aEq+VtVTAwrWgDvpNupvGIQdop027e0P6jHZ7gAxxaWVagjKttPu44swwgqBgpIhcyGxkSvpC8fQxTtsjUdekl+tZuL7KKFwL16Dq+n8rzxyrSuFPvjcwsMRYTgUwKOOJJVWImHkKy/2w8r4O1EHpQ/9D2WQJjl03nRRmmpI9fojwtdu0pdf1CTCjviJb3A5unoQ8f4GSTipy2+LrxEEIn7rdLIsNCoFjdFSF8Pc1hliyALr3x6lM0LAuJQxbkskH8dKBYe9V6bXAmGV8qz1wVyVt/0czLR9GDWeGxsIywC6NHLsxapfNjHMo7VXb8ZzKvyBTobTTvbDwJDRmA==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH v2 2/4] qcow2: Don't round the L1 table allocation up to the sector size Date: Thu, 9 Jan 2020 20:13:00 +0100 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 178.60.130.6 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Nir Soffer Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The L1 table is read from disk using the byte-based bdrv_pread() and is never accessed beyond its last element, so there's no need to allocate more memory than that. Signed-off-by: Alberto Garcia Reviewed-by: Max Reitz --- block/qcow2-cluster.c | 5 ++--- block/qcow2-refcount.c | 2 +- block/qcow2-snapshot.c | 3 +-- block/qcow2.c | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 8982b7b762..932fc48919 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -124,12 +124,11 @@ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_= t min_size, #endif =20 new_l1_size2 =3D sizeof(uint64_t) * new_l1_size; - new_l1_table =3D qemu_try_blockalign(bs->file->bs, - ROUND_UP(new_l1_size2, 512)); + new_l1_table =3D qemu_try_blockalign(bs->file->bs, new_l1_size2); if (new_l1_table =3D=3D NULL) { return -ENOMEM; } - memset(new_l1_table, 0, ROUND_UP(new_l1_size2, 512)); + memset(new_l1_table, 0, new_l1_size2); =20 if (s->l1_size) { memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t)); diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index f67ac6b2d8..c963bc8de1 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1262,7 +1262,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *= bs, * l1_table_offset when it is the current s->l1_table_offset! Be caref= ul * when changing this! */ if (l1_table_offset !=3D s->l1_table_offset) { - l1_table =3D g_try_malloc0(ROUND_UP(l1_size2, 512)); + l1_table =3D g_try_malloc0(l1_size2); if (l1_size2 && l1_table =3D=3D NULL) { ret =3D -ENOMEM; goto fail; diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 5ab64da1ec..82c32d4c9b 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -1024,8 +1024,7 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, return ret; } new_l1_bytes =3D sn->l1_size * sizeof(uint64_t); - new_l1_table =3D qemu_try_blockalign(bs->file->bs, - ROUND_UP(new_l1_bytes, 512)); + new_l1_table =3D qemu_try_blockalign(bs->file->bs, new_l1_bytes); if (new_l1_table =3D=3D NULL) { return -ENOMEM; } diff --git a/block/qcow2.c b/block/qcow2.c index 87ca2832f0..848a6c5182 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1499,7 +1499,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverStat= e *bs, QDict *options, =20 if (s->l1_size > 0) { s->l1_table =3D qemu_try_blockalign(bs->file->bs, - ROUND_UP(s->l1_size * sizeof(uint64_t), 512)); + s->l1_size * sizeof(uint64_t)); if (s->l1_table =3D=3D NULL) { error_setg(errp, "Could not allocate L1 table"); ret =3D -ENOMEM; --=20 2.20.1 From nobody Fri May 3 01:20:18 2024 Delivered-To: importer@patchew.org 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=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; 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 ARC-Seal: i=1; a=rsa-sha256; t=1578597438; cv=none; d=zohomail.com; s=zohoarc; b=ZVdkge0OVVmnCZ3ijecLyVwjn2lLSmMxayi+BDKe8jq8GC7KPfzjgMmIDalQq4FvLbCU3Y7V2bgk67LXYWbsfOVUWnMnbIMnEQkc6icqp2ZTKCxz+MkwMUEcQdRuDrcUKsbCfvYv3vHF1Su1IKGyQXPY2HZfsFnW9tb9aDgL+Zg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1578597438; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=rYrNjyo1odRCvrRwXrISC+TPRRtheFI6QJmzzrZY1Qw=; b=jU+pmFY6mA/MuM0oC4dTaT1onLg6h/+qL23X9VX04BybG74+3HFTqxUTachDUxdjLYwGcYRS73vST7vgZ7MGGhpCB1KrfBbTMsOwnwrL8aeI8/tAP6nLPLj0SHud8vwxxSo2TRdcs8hvzAgXwOWJAJP5smZvOUKHFVyBeQClOSE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1578597438737559.778294854377; Thu, 9 Jan 2020 11:17:18 -0800 (PST) Received: from localhost ([::1]:36528 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipdIz-000555-2C for importer@patchew.org; Thu, 09 Jan 2020 14:17:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39315) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipdGC-0003WV-T3 for qemu-devel@nongnu.org; Thu, 09 Jan 2020 14:14:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ipdGB-0007J6-EI for qemu-devel@nongnu.org; Thu, 09 Jan 2020 14:14:24 -0500 Received: from fanzine.igalia.com ([178.60.130.6]:58342) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ipdG9-0006BE-Fv; Thu, 09 Jan 2020 14:14:22 -0500 Received: from [213.99.255.143] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1ipdFp-0006Ml-Nu; Thu, 09 Jan 2020 20:14:01 +0100 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1ipdF0-0005R5-Nf; Thu, 09 Jan 2020 20:13:10 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=rYrNjyo1odRCvrRwXrISC+TPRRtheFI6QJmzzrZY1Qw=; b=WYIM4E/o1s/61ArO3ZGiKM2aUsZZdkh1ptkOJe+Aq06Oy664J9fVmRhdrhXBAfcdNmIkafeLWlUouiDQQEIcpX4AdK1pnHcfaLoNnBftwepjRTnYB8KpDxmtzjTc8anRswXWxTIdDBZqeGdZmxnP4Byjizq5mrcZwb6fCMFABz59UylZVURzeH/W2D0FIng4H6pe52Z6MeqwWNqnWgSPoikyTJKinvUn9qNGJJVEtaVEHiQHk6Jx0i5bFSzDaP25k59yvDW3HPbFs5oRSkP+JczgUGeSa6yphGnc91AaVIzAasbjVd6d6/ll1wUeEFUB0u/glojuO3NToLONTbE5DQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH v2 3/4] qcow2: Tighten cluster_offset alignment assertions Date: Thu, 9 Jan 2020 20:13:01 +0100 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 178.60.130.6 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Nir Soffer Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" qcow2_alloc_cluster_offset() and qcow2_get_cluster_offset() always return offsets that are cluster-aligned so don't just check that they are sector-aligned. The check in qcow2_co_preadv_task() is also replaced by an assertion for the same reason. Signed-off-by: Alberto Garcia Reviewed-by: Max Reitz --- block/qcow2.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 848a6c5182..783d2b9578 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2175,10 +2175,7 @@ static coroutine_fn int qcow2_co_preadv_task(BlockDr= iverState *bs, offset, bytes, qiov, qiov_offset= ); =20 case QCOW2_CLUSTER_NORMAL: - if ((file_cluster_offset & 511) !=3D 0) { - return -EIO; - } - + assert(QEMU_IS_ALIGNED(file_cluster_offset, s->cluster_size)); if (bs->encrypted) { return qcow2_co_preadv_encrypted(bs, file_cluster_offset, offset, bytes, qiov, qiov_off= set); @@ -2514,7 +2511,7 @@ static coroutine_fn int qcow2_co_pwritev_part( goto out_locked; } =20 - assert((cluster_offset & 511) =3D=3D 0); + assert(QEMU_IS_ALIGNED(cluster_offset, s->cluster_size)); =20 ret =3D qcow2_pre_write_overlap_check(bs, 0, cluster_offset + offset_in_clu= ster, @@ -3904,7 +3901,7 @@ qcow2_co_copy_range_to(BlockDriverState *bs, goto fail; } =20 - assert((cluster_offset & 511) =3D=3D 0); + assert(QEMU_IS_ALIGNED(cluster_offset, s->cluster_size)); =20 ret =3D qcow2_pre_write_overlap_check(bs, 0, cluster_offset + offset_in_cluster, cur_bytes, true); --=20 2.20.1 From nobody Fri May 3 01:20:18 2024 Delivered-To: importer@patchew.org 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=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; 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 ARC-Seal: i=1; a=rsa-sha256; t=1578597614; cv=none; d=zohomail.com; s=zohoarc; b=UaBGAAmLlbmV9hY3htAnsqshuVgK6rGc0it8lI6HPU88jxlknleMMlLYUXmk+N3kUVxnAeWzERKII0N9XgrTd+ucCg7qp/Z/IFsC+pbkjNEKxbFBZ0CaGTvlhR0f+Ox4NFpsp71+PLb4VJW1xFzq1kXtfoGxiwK/gzxAValxaVA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1578597614; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Clwr8OmkgLClIPLb5A4r0y5WGdUX1DP4zBqunbRF28Y=; b=GCepDouwSl1Crmlr9on/6kv1ndZF1ZptG4HcfDYNivtR7xdkwXZdVZJKB0LXikxASDGNDThX5OQlVBYx+Iz0j49SKCLLRGT3/kUYj9BVLw2XjbpdpwUEMEYSkZoEilrK5hYakV6guP4g8ucFMK0Sgd1Vnc2EanPLgACZaebTFMs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15785976143984.12339241892596; Thu, 9 Jan 2020 11:20:14 -0800 (PST) Received: from localhost ([::1]:36574 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipdLo-00016w-Vd for importer@patchew.org; Thu, 09 Jan 2020 14:20:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39309) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipdGC-0003WS-Rt for qemu-devel@nongnu.org; Thu, 09 Jan 2020 14:14:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ipdGB-0007JK-EN for qemu-devel@nongnu.org; Thu, 09 Jan 2020 14:14:24 -0500 Received: from fanzine.igalia.com ([178.60.130.6]:58346) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ipdG9-0006BC-FX; Thu, 09 Jan 2020 14:14:22 -0500 Received: from [213.99.255.143] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1ipdFp-0006Mm-P5; Thu, 09 Jan 2020 20:14:01 +0100 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1ipdF0-0005R7-Om; Thu, 09 Jan 2020 20:13:10 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=Clwr8OmkgLClIPLb5A4r0y5WGdUX1DP4zBqunbRF28Y=; b=msGUMMPheY916npCnP2Zv70bYMzo4GHL/NYPrwCZ9/Ho0jpO8QpN6xqTmwJJO0ArsgrgKiCnhQSNUNuGOVhcmb/AqiVGaZTy5GXBOITckdONUt8bDuZRCZ0a5ust0jjBmNXLkZi1godGF3u25ZM1sGY5KglbLnXn8kL55ltUHQPLD2RaZET5Yk4ie1ov+O9fuc+vB9JDj94mKwqka7XeN6gbmJtsCPNZ5I4TiuZjeiiWO4wThuv8cVJQMIVIVxFKKqy41CrSgPW/lRiqLn9BjJVA6rVbngqhPHwciXOwGgUCFhh66QCPnPWd2KuiGg1LyE9tJxyksk8hvmCGxMRrYw==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH v2 4/4] qcow2: Use BDRV_SECTOR_SIZE instead of the hardcoded value Date: Thu, 9 Jan 2020 20:13:02 +0100 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 178.60.130.6 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Nir Soffer Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This replaces all remaining instances in the qcow2 code. Signed-off-by: Alberto Garcia --- block/qcow2-cluster.c | 2 +- block/qcow2.c | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 932fc48919..777ca2d409 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -219,7 +219,7 @@ static int l2_load(BlockDriverState *bs, uint64_t offse= t, * Writes one sector of the L1 table to the disk (can't update single entr= ies * and we really don't want bdrv_pread to perform a read-modify-write) */ -#define L1_ENTRIES_PER_SECTOR (512 / 8) +#define L1_ENTRIES_PER_SECTOR (BDRV_SECTOR_SIZE / 8) int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index) { BDRVQcow2State *s =3D bs->opaque; diff --git a/block/qcow2.c b/block/qcow2.c index 783d2b9578..c0f3e715ef 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3280,7 +3280,8 @@ qcow2_co_create(BlockdevCreateOptions *create_options= , Error **errp) =20 /* Validate options and set default values */ if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) { - error_setg(errp, "Image size must be a multiple of 512 bytes"); + error_setg(errp, "Image size must be a multiple of %u bytes", + (unsigned) BDRV_SECTOR_SIZE); ret =3D -EINVAL; goto out; } @@ -3836,7 +3837,7 @@ qcow2_co_copy_range_from(BlockDriverState *bs, case QCOW2_CLUSTER_NORMAL: child =3D s->data_file; copy_offset +=3D offset_into_cluster(s, src_offset); - if ((copy_offset & 511) !=3D 0) { + if (!QEMU_IS_ALIGNED(copy_offset, BDRV_SECTOR_SIZE)) { ret =3D -EIO; goto out; } @@ -3958,8 +3959,9 @@ static int coroutine_fn qcow2_co_truncate(BlockDriver= State *bs, int64_t offset, return -ENOTSUP; } =20 - if (offset & 511) { - error_setg(errp, "The new size must be a multiple of 512"); + if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) { + error_setg(errp, "The new size must be a multiple of %u", + (unsigned) BDRV_SECTOR_SIZE); return -EINVAL; } =20 --=20 2.20.1