[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 01/17] qcow2: Assert that host cluster offsets fit in L2 table ent
From: |
Max Reitz |
Subject: |
[PULL 01/17] qcow2: Assert that host cluster offsets fit in L2 table entries |
Date: |
Thu, 6 Feb 2020 13:51:16 +0100 |
From: Alberto Garcia <address@hidden>
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 <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
Message-id: address@hidden
Signed-off-by: Max Reitz <address@hidden>
---
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 8982b7b762..e9431f6785 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -777,6 +777,10 @@ int qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs,
(cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
(cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
+ /* The offset and size must fit in their fields of the L2 table entry */
+ assert((cluster_offset & s->cluster_offset_mask) == cluster_offset);
+ assert((nb_csectors & s->csize_mask) == nb_csectors);
+
cluster_offset |= QCOW_OFLAG_COMPRESSED |
((uint64_t)nb_csectors << s->csize_shift);
@@ -972,6 +976,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs,
QCowL2Meta *m)
assert(l2_index + m->nb_clusters <= s->l2_slice_size);
for (i = 0; i < m->nb_clusters; i++) {
+ uint64_t offset = cluster_offset + (i << s->cluster_bits);
/* if two concurrent writes happen to the same unallocated cluster
* each write allocates separate cluster and writes data concurrently.
* The first one to complete updates l2 table with pointer to its
@@ -982,8 +987,10 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs,
QCowL2Meta *m)
old_cluster[j++] = l2_slice[l2_index + i];
}
- l2_slice[l2_index + i] = 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) == offset);
+
+ l2_slice[l2_index + i] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
}
@@ -1913,6 +1920,9 @@ static int expand_zero_clusters_in_l1(BlockDriverState
*bs, uint64_t *l1_table,
goto fail;
}
+ /* The offset must fit in the offset field */
+ assert((offset & L2E_OFFSET_MASK) == offset);
+
if (l2_refcount > 1) {
/* For shared L2 tables, set the refcount accordingly
* (it is already 1 and needs to be l2_refcount) */
--
2.24.1
- [PULL 00/17] Block patches, Max Reitz, 2020/02/06
- [PULL 01/17] qcow2: Assert that host cluster offsets fit in L2 table entries,
Max Reitz <=
- [PULL 02/17] block: Use a GString in bdrv_perm_names(), Max Reitz, 2020/02/06
- [PULL 03/17] block: fix memleaks in bdrv_refresh_filename, Max Reitz, 2020/02/06
- [PULL 04/17] qcow2: Use a GString in report_unsupported_feature(), Max Reitz, 2020/02/06
- [PULL 05/17] iotests: remove 'linux' from default supported platforms, Max Reitz, 2020/02/06
- [PULL 06/17] iotests: Test 041 only works on certain systems, Max Reitz, 2020/02/06
- [PULL 07/17] iotests: Test 183 does not work on macOS and OpenBSD, Max Reitz, 2020/02/06
- [PULL 08/17] iotests: Check for the availability of the required devices in 267 and 127, Max Reitz, 2020/02/06
- [PULL 09/17] iotests: Skip Python-based tests if QEMU does not support virtio-blk, Max Reitz, 2020/02/06
- [PULL 10/17] iotests: Enable more tests in the 'auto' group to improve test coverage, Max Reitz, 2020/02/06
- [PULL 12/17] qcow2: Tighten cluster_offset alignment assertions, Max Reitz, 2020/02/06