From nobody Wed May 15 09:53:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1556876420; cv=none; d=zoho.com; s=zohoarc; b=bG4h4fghX6pygzS+nWsXPO9jrdLEM1lhnXTmGjwChw0uDYU3DT9I8NcVhbQqonylqjxCwlnnA3TsncHwhQgLzVgZ6vdpGrSO+KjRi/zNWrjusAnPK3k0XM+jSNgk7rZnQfzoq+nBhkr4u6F/n88pENIdTLBah/GXFSYKad/sPTE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556876420; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=jzIvCeYjHa1BNoF9zJsEAuP4EQ3Rz6mekQkzZKCVRGA=; b=NjIoyiwe7099fSuq/0435GywovhA0nRbP854lPK1Y36uGAhBSYrDelrZ8XH3T4jOG+slVyIZ/7TvOfWRzJuCGkUzdLveLwALWMEk9QfTxz6aXOU0Ft/d+N0yTdZi8b1CAK73+9fyIDIGtXdocmB2uS7gzhiLwQ6YdooK0ADGO+M= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.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 1556876418968751.3330779200417; Fri, 3 May 2019 02:40:18 -0700 (PDT) Received: from localhost ([127.0.0.1]:37273 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMUfq-00031D-No for importer@patchew.org; Fri, 03 May 2019 05:40:10 -0400 Received: from eggs.gnu.org ([209.51.188.92]:40698) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMUex-0002bZ-Hg for qemu-devel@nongnu.org; Fri, 03 May 2019 05:39:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMUeu-0005MH-N5 for qemu-devel@nongnu.org; Fri, 03 May 2019 05:39:15 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:36030) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMUeu-0005J5-4l; Fri, 03 May 2019 05:39:12 -0400 Received: from 87-92-6-174.bb.dnainternet.fi ([87.92.6.174] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1hMUec-00061g-DH; Fri, 03 May 2019 11:38:54 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1hMUeN-0002MC-Pq; Fri, 03 May 2019 12:38:39 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Message-Id:Date:Subject:Cc:To:From; bh=jzIvCeYjHa1BNoF9zJsEAuP4EQ3Rz6mekQkzZKCVRGA=; b=OQrjdLTndLRcQVnyvUjHv4SJKuzanthl2vBnQynvVe4UVwLe7khCV3/9iebiBqlh9/JxeaNG9GzvqqAmyBUyTFwPGkmFUfyBXqlYaC4Kgroid9JqoRRxTDXcyQfqOpg09ZCVTcXccUn4duTqfr6oWIV1YBhi9/JinCHIVKLYjIA3FKz4pe4+JZ5t8clNq+I0nvNnN1p/cTi987h1Y8P4rAiH16As+tTSdRX04uZNhZAWpvoELy65eHzzjbZpGTtQ7BS0mqM5VIP0X9oLDFEgdKNOvuqHLF7ga4YECt5vNFDG1hPXXzmWg4WLApS+24Jnv1HhMf5jVB2D9HcV08E8mQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Fri, 3 May 2019 12:38:32 +0300 Message-Id: <20190503093832.9015-1-berto@igalia.com> X-Mailer: git-send-email 2.11.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH] qcow2: Assert that host cluster offsets fit in L2 table entries X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The standard cluster descriptor in L2 table entries has a field to store the host cluster offset. When we need to get that offset from an entry we use L2E_OFFSET_MASK to ensure that we only use the bits that belong to that field. But while that mask is used every time we read from an L2 entry, it is never used when we write to it. Due to the QCOW_MAX_CLUSTER_OFFSET limit set in the cluster allocation code QEMU can never produce offsets that don't fit in that field so any such offset would indicate a bug in QEMU. Compressed cluster descriptors contain two fields (host cluster offset and size of the compressed data) and the situation with them is similar. In this case the masks are not constant but are stored in the csize_mask and cluster_offset_mask fields of BDRVQcow2State. Signed-off-by: Alberto Garcia Reviewed-by: Eric Blake --- block/qcow2-cluster.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 974a4e8656..4a96966ab9 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -799,6 +799,10 @@ int qcow2_alloc_compressed_cluster_offset(BlockDriverS= tate *bs, nb_csectors =3D ((cluster_offset + compressed_size - 1) >> 9) - (cluster_offset >> 9); =20 + /* The offset and size must fit in their fields of the L2 table entry = */ + assert((cluster_offset & s->cluster_offset_mask) =3D=3D cluster_offset= ); + assert((nb_csectors & s->csize_mask) =3D=3D nb_csectors); + cluster_offset |=3D QCOW_OFLAG_COMPRESSED | ((uint64_t)nb_csectors << s->csize_shift); =20 @@ -984,6 +988,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, Q= CowL2Meta *m) =20 assert(l2_index + m->nb_clusters <=3D s->l2_slice_size); for (i =3D 0; i < m->nb_clusters; i++) { + uint64_t offset =3D cluster_offset + (i << s->cluster_bits); /* if two concurrent writes happen to the same unallocated cluster * each write allocates separate cluster and writes data concurren= tly. * The first one to complete updates l2 table with pointer to its @@ -994,8 +999,10 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, = QCowL2Meta *m) old_cluster[j++] =3D l2_slice[l2_index + i]; } =20 - l2_slice[l2_index + i] =3D cpu_to_be64((cluster_offset + - (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); + /* The offset must fit in the offset field of the L2 table entry */ + assert((offset & L2E_OFFSET_MASK) =3D=3D offset); + + l2_slice[l2_index + i] =3D cpu_to_be64(offset | QCOW_OFLAG_COPIED); } =20 =20 @@ -1928,6 +1935,9 @@ static int expand_zero_clusters_in_l1(BlockDriverStat= e *bs, uint64_t *l1_table, goto fail; } =20 + /* The offset must fit in the offset field */ + assert((offset & L2E_OFFSET_MASK) =3D=3D offset); + if (l2_refcount > 1) { /* For shared L2 tables, set the refcount accordin= gly * (it is already 1 and needs to be l2_refcount) */ --=20 2.11.0