parallels: fix Format Extension parsing
cover parallels: fix Format Extension parsing
The Parallels Format Extension parser trusts the cluster it has just read. A crafted image makes it read past the end of that cluster, and with a large enough cluster size it makes the dirty bitmap code memset memory which does not belong to it. All of it is reachable from
qemu-img info -f parallels image
on an untrusted image, with no guest and no write involved.
The reported problem
--------------------
parallels_parse_format_extension() advances its cursor over a feature payload but subtracts only the feature header size from the number of bytes it believes are left. A feature whose payload ends exactly at the end of the cluster leaves the cursor at the end of the allocation while the count still permits one more header, and the next iteration reads it:
Invalid read of size 8
at memcpy (string_fortified.h:29)
by parallels_parse_format_extension (parallels-ext.c:250) by parallels_read_format_extension (parallels-ext.c:313) by parallels_open (parallels.c:1386)
Address 0x87831e0 is 0 bytes after a block of size 512 alloc'd
That is patch 1, reported privately on
https://gitlab.com/qemu-project/qemu/-/issues/4156.
The worse one
-------------
bdrv_dirty_bitmap_serialization_coverage() takes the chunk size as an int and shifts it left by 3 before the result is widened, so a cluster of 1 << 28 or above overflows. parallels_open() allows clusters up to about 2 GiB, and with a bitmap needing two L1 entries the bogus limit makes "bm_size - offset" underflow in parallels_load_bitmap_data(). Both wrong values pass the assertions in serialization_chunk() and the resulting index lands outside the hbitmap:
#1 memset (__ch=255) at string_fortified.h:59 #2 hbitmap_deserialize_ones (start=18446742974197923840,
count=3298534883328) at hbitmap.c:753
#3 bdrv_dirty_bitmap_deserialize_ones (...) at dirty-bitmap.c:648 #4 parallels_load_bitmap_data (...) at parallels-ext.c:88
A 128 KiB image file is enough to get there. The target lands far outside any mapping in the case I built, so what I can demonstrate is a wild write and a crash rather than a controlled one, but it is a write and not a read. Patch 3 fixes the overflow. Patch 4 refuses such an offset in the parallels loop as well, since both the cluster size and the L1 contents come from the image.
The rest
--------
Patch 2 validates the bitmap granularity, which reaches bdrv_create_dirty_bitmap() unchecked and trips its assertion for a granularity of zero, one which is not a power of two, or one which wraps when shifted by BDRV_SECTOR_BITS. Patch 5 refuses an extension cluster which does not lie inside the image file, patch 6 stops aborting when that cluster cannot be allocated, and patch 7 is a leak.
Patch 3 is the only one outside the parallels driver and can be taken on its own. The parallels patches do not depend on it, although without it patch 4 is what keeps the memset from happening.
Testing
-------
Patch 8 extends tests/qemu-iotests/tests/parallels-read-bitmap with hand crafted extension clusters. With this series reverted, four of the cases abort in bdrv_create_dirty_bitmap(), one segfaults in hbitmap_deserialize_ones() and one reports a feature header made of heap bytes read past the cluster. parallels-read-bitmap, parallels-checks, 076 and 118 pass, and every patch builds on its own.
No CVE has been requested.
Denis V. Lunev (8):
parallels: fix out-of-bounds read in format extension parsing
parallels: validate dirty bitmap granularity
dirty-bitmap: fix integer overflow in serialization coverage
parallels: bound the bitmap L1 table against the bitmap size
parallels: reject a Format Extension outside the image file
parallels: allocate the Format Extension cluster gracefully
parallels: fix GSList leak on the format extension success path
iotests: cover the Parallels format extension parser
block/dirty-bitmap.c | 2 +- block/parallels-ext.c | 46 ++++- block/parallels.c | 9 +- .../qemu-iotests/tests/parallels-read-bitmap | 180 +++++++++++++++++- .../tests/parallels-read-bitmap.out | 33 ++++ 5 files changed, 259 insertions(+), 11 deletions(-)
base-commit: 3e3ccab106f879b1512f8e0d51a827dd4de30e22
Signature · 1 line
2.53.0
PATCH 1/8 parallels: fix out-of-bounds read in format extension parsing
parallels_parse_format_extension() advances the cursor over the feature payload but subtracts only the feature header size from the remaining byte count. A feature whose payload reaches the end of the extension cluster leaves the cursor at the end of the allocation while the count still allows one more header, which the next iteration then reads out of bounds, 0 bytes after the 512 byte cluster. The stale count also breaks the data_size bound of any further feature, so parallels_load_bitmap() can read past the cluster as well.
Account the aligned payload in both the cursor and the count. Aligning data_size before the bound check changes nothing, as the count is always a multiple of 8, but it has to be computed in 64 bits: on a uint32_t a data_size of 0xfffffff9 or above wraps to zero.
Reported-by: Martin Holeček
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4156
--- block/parallels-ext.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
block/parallels-ext.c +5 -2
@@ -239,6 +239,7 @@ parallels_parse_format_extension(BlockDriverState *bs, uint8_t *ext_cluster,while (true) {ParallelsFeatureHeader fh;BdrvDirtyBitmap *bitmap; +uint64_t data_size;if (remaining <sizeof (fh)) { error_setg(errp, "Can not read feature header, as remaining bytes "
@@ -260,7 +261,8 @@ parallels_parse_format_extension(BlockDriverState *bs, uint8_t *ext_cluster, goto fail; } -if (fh.data_size > remaining) { + data_size =QEMU_ALIGN_UP ((uint64_t )fh.data_size,8 ); +if (data_size > remaining) {error_setg (errp,"Feature data_size exceedes Format Extension " "cluster" ); goto fail;
@@ -283,7 +285,8 @@ parallels_parse_format_extension(BlockDriverState *bs, uint8_t *ext_cluster, goto fail; } - pos = ext_cluster +QEMU_ALIGN_UP (pos + fh.data_size - ext_cluster,8 ); + pos += data_size; + remaining -= data_size; } fail:
Signature · 1 line
2.53.0
PATCH 2/8 parallels: validate dirty bitmap granularity
In reply to Denis V. Lunevbf.granularity comes from the image and is passed to bdrv_create_dirty_bitmap(), which asserts on it. An otherwise valid image thus aborts qemu-img for a granularity of zero or one which is not a power of two. The shift by BDRV_SECTOR_BITS is done on a uint32_t as well, so 1 << 23 sectors and above wrap to zero and hit the same assertion.
Compute the granularity in 64 bits and reject what bdrv_create_dirty_bitmap() cannot accept.
--- block/parallels-ext.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
block/parallels-ext.c +14 -3
@@ -31,6 +31,7 @@#include "parallels.h" #include "crypto/hash.h" #include "qemu/bswap.h" +#include "qemu/host-utils.h" #include "qemu/uuid.h" #include "qemu/memalign.h"
@@ -122,7 +123,7 @@ parallels_load_bitmap(BlockDriverState *bs, uint8_t *data, size_t data_size,BdrvDirtyBitmap *bitmap;QemuUUID uuid;char uuidstr[UUID_STR_LEN ]; -uint64_t bm_size, tab_size; +uint64_t bm_size, tab_size, granularity;int i;if (data_size <sizeof (bf)) {
@@ -133,7 +134,7 @@ parallels_load_bitmap(BlockDriverState *bs, uint8_t *data, size_t data_size, }memcpy (&bf, data,sizeof (bf)); bf.size =le64_to_cpu (bf.size); - bf.granularity =le32_to_cpu (bf.granularity) <<BDRV_SECTOR_BITS ; + bf.granularity =le32_to_cpu (bf.granularity); bf.l1_size =le32_to_cpu (bf.l1_size); data +=sizeof (bf); data_size -=sizeof (bf);
@@ -144,6 +145,16 @@ parallels_load_bitmap(BlockDriverState *bs, uint8_t *data, size_t data_size,return NULL ; } +/* bdrv_create_dirty_bitmap() asserts on an unusable granularity */ + granularity = (uint64_t )bf.granularity <<BDRV_SECTOR_BITS ; +if (granularity <BDRV_SECTOR_SIZE || granularity >UINT32_MAX || + !is_power_of_2 (granularity)) { +error_setg (errp,"Invalid bitmap granularity %" PRIu64", expected a " +"power of two of at least %" PRIu64" bytes" , granularity, + (uint64_t )BDRV_SECTOR_SIZE ); +return NULL ; + } +if (bf.l1_size *sizeof (uint64_t ) > data_size) {error_setg (errp,"Bitmaps feature corrupted: l1 table exceeds " "extension data_size" );
@@ -152,7 +163,7 @@ parallels_load_bitmap(BlockDriverState *bs, uint8_t *data, size_t data_size,memcpy (&uuid, bf.id,sizeof (uuid));qemu_uuid_unparse (&uuid, uuidstr); - bitmap =bdrv_create_dirty_bitmap (bs, bf.granularity, uuidstr, errp); + bitmap =bdrv_create_dirty_bitmap (bs, granularity, uuidstr, errp);if (!bitmap) {return NULL ; }
Signature · 1 line
2.53.0
PATCH 3/8 dirty-bitmap: fix integer overflow in serialization coverage
In reply to Denis V. LunevThe chunk size is an int and is shifted left by 3 before the result is widened, so a chunk size of 1 << 28 or above overflows.
parallels passes s->cluster_size, which parallels_open() lets reach 2 GiB. With a bitmap needing two L1 entries the bogus limit makes the "bm_size - offset" in parallels_load_bitmap_data() underflow; both wrong values slip past the assertions in serialization_chunk() and the resulting index lands outside the hbitmap, so a 128 KiB image memsets unrelated memory through hbitmap_deserialize_ones().
Widen the shift. qcow2, the only other caller, never exceeds a 2 MiB cluster.
--- block/dirty-bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/dirty-bitmap.c +1 -1
@@ -612,7 +612,7 @@ uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size, const BdrvDirtyBitmap *bitmap) {uint64_t granularity =bdrv_dirty_bitmap_granularity (bitmap); -uint64_t limit = granularity * (serialized_chunk_size <<3 ); +uint64_t limit = granularity * ((uint64_t )serialized_chunk_size <<3 );assert (QEMU_IS_ALIGNED (limit,bdrv_dirty_bitmap_serialization_align (bitmap)));
Signature · 1 line
2.53.0
Re: [PATCH 3/8] dirty-bitmap: fix integer overflow in serialization coverage
Quoted 28 lines · Denis V. Lunev
The chunk size is an int and is shifted left by 3 before the result is widened, so a chunk size of 1 << 28 or above overflows.
parallels passes
s->cluster_size, whichparallels_open()lets reach 2 GiB. With a bitmap needing two L1 entries the bogus limit makes the "bm_size- offset" inparallels_load_bitmap_data()underflow; both wrong values slip past the assertions inserialization_chunk()and the resulting index lands outside the hbitmap, so a 128 KiB image memsets unrelated memory throughhbitmap_deserialize_ones().Widen the shift. qcow2, the only other caller, never exceeds a 2 MiB cluster.
Fixes 35f428ba3971 ("qcow2-bitmap: make bytes_covered_by_bitmap_cluster() public")Cc Eric Blake <[email protected]>Cc Vladimir Sementsov-Ogievskiy <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Thomas Huth <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>--- block/dirty-bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)block/dirty-bitmap.c +0
-0
block/dirty-bitmap.c uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size, · line 618
Quoted 8 lines
block/dirty-bitmap.c +1
-1@@ -612,7 +612,7 @@ uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size, const BdrvDirtyBitmap *bitmap) {uint64_t granularity =bdrv_dirty_bitmap_granularity (bitmap); -uint64_t limit = granularity * (serialized_chunk_size <<3 ); +uint64_t limit = granularity * ((uint64_t )serialized_chunk_size <<3 );assert (QEMU_IS_ALIGNED (limit,bdrv_dirty_bitmap_serialization_align (bitmap)));
I think, it's good to pick this one for my next pull request with another bitmap-related fixes.
Thanks, applied to my block branch (this one 3/8 patch).
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 3/8] dirty-bitmap: fix integer overflow in serialization coverage
Quoted 40 lines · Vladimir Sementsov-Ogievskiy
Quoted 28 lines · Denis V. Lunev
The chunk size is an int and is shifted left by 3 before the result is widened, so a chunk size of 1 << 28 or above overflows.
parallels passes
s->cluster_size, whichparallels_open()lets reach 2 GiB. With a bitmap needing two L1 entries the bogus limit makes the "bm_size- offset" inparallels_load_bitmap_data()underflow; both wrong values slip past the assertions inserialization_chunk()and the resulting index lands outside the hbitmap, so a 128 KiB image memsets unrelated memory throughhbitmap_deserialize_ones().Widen the shift. qcow2, the only other caller, never exceeds a 2 MiB cluster.
Fixes 35f428ba3971 ("qcow2-bitmap: make bytes_covered_by_bitmap_cluster() public")Cc Eric Blake <[email protected]>Cc Vladimir Sementsov-Ogievskiy <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Thomas Huth <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>--- block/dirty-bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)block/dirty-bitmap.c +0
-0block/dirty-bitmap.c uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size, · line 618
Quoted 8 lines
block/dirty-bitmap.c +1
-1@@ -612,7 +612,7 @@ uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size, const BdrvDirtyBitmap *bitmap) {uint64_t granularity =bdrv_dirty_bitmap_granularity (bitmap); -uint64_t limit = granularity * (serialized_chunk_size <<3 ); +uint64_t limit = granularity * ((uint64_t )serialized_chunk_size <<3 );assert (QEMU_IS_ALIGNED (limit,bdrv_dirty_bitmap_serialization_align (bitmap)));Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>
Quoted 6 lines
I think, it's good to pick this one for my next pull request with another bitmap-related fixes.
Thanks, applied to my block branch (this one 3/8 patch).
Great. Will remove it from pull request.
Thanks!
PATCH 4/8 parallels: bound the bitmap L1 table against the bitmap size
In reply to Denis V. Lunevparallels_load_bitmap_data() derives the number of bytes to deserialize from "bm_size - offset" without checking that the offset is still inside the bitmap, and an offset past the end makes that subtraction underflow.
The overflow which used to produce such an offset is fixed by "dirty-bitmap: fix integer overflow in serialization coverage", but both the cluster size and the L1 contents come from the image, so refuse the table explicitly. The check cannot reject a valid table:l1_size is DIV_ROUND_UP(bm_size, limit), so the largest offset the loop reaches is (l1_size - 1) * limit, always below bm_size.
--- block/parallels-ext.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
block/parallels-ext.c +11 -2
@@ -76,8 +76,17 @@ parallels_load_bitmap_data(BlockDriverState *bs, const uint64_t *l1_table, buf =qemu_blockalign (bs, s->cluster_size); limit =bdrv_dirty_bitmap_serialization_coverage (s->cluster_size, bitmap);for (i =0 , offset =0 ; i < l1_size; ++i, offset += limit) { -uint64_t count =MIN (bm_size - offset, limit); -uint64_t entry = l1_table[i]; +uint64_t count, entry; + +if (offset >= bm_size) { +error_setg (errp,"Bitmap L1 table covers more than the bitmap " +"size %" PRIu64, bm_size); + ret = -EINVAL ; + goto finish; + } + + count =MIN (bm_size - offset, limit); + entry = l1_table[i];if (entry ==0 ) {/* No need to deserialize zeros because @bitmap is cleared. */
Signature · 1 line
2.53.0
PATCH 5/8 parallels: reject a Format Extension outside the image file
In reply to Denis V. LunevThe Format Extension offset and the cluster size both come from the image header and neither is checked against the image file. Reject the image rather than reading a cluster which is not there.
--- block/parallels.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
block/parallels.c +8 -1
@@ -1375,6 +1375,8 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, }if (ph.ext_off) { +int64_t ext_off =le64_to_cpu (ph.ext_off); +if (flags &BDRV_O_RDWR ) {/* * It's unsafe to open image RW if there is an extension (as we
@@ -1382,9 +1384,14 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,* ignores the extension, so print warning and don't care. */ warn_report ("Format Extension ignored in RW mode" ); + }else if (ext_off + s->tracks > file_nb_sectors) { +error_setg (errp,"Invalid image: Format Extension is outside the " +"image file" ); + ret = -EINVAL ; + goto fail; }else { ret =parallels_read_format_extension ( - bs,le64_to_cpu (ph.ext_off) <<BDRV_SECTOR_BITS , errp); + bs, ext_off <<BDRV_SECTOR_BITS , errp);if (ret <0 ) { goto fail; }
Signature · 1 line
2.53.0
PATCH 6/8 parallels: allocate the Format Extension cluster gracefully
In reply to Denis V. Lunevs->cluster_size comes from the image and qemu_blockalign() aborts when the allocation cannot be satisfied, so use the try variant and report the failure instead.
The two are not interchangeable: qemu_try_blockalign() asserts that the alignment is non zero, while qemu_blockalign() reaches qemu_try_memalign(), which quietly raises a zero alignment to sizeof(void *). bs->bl is only filled in once .bdrv_open() returns, so the alignment has to come from bs->file, which is the node the cluster is read through anyway and what parallels_open() uses for the header.
--- block/parallels-ext.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
block/parallels-ext.c +7 -1
@@ -323,10 +323,16 @@ int parallels_read_format_extension(BlockDriverState *bs, {BDRVParallelsState *s = bs->opaque;int ret; -uint8_t *ext_cluster =qemu_blockalign (bs, s->cluster_size); +uint8_t *ext_cluster;assert (ext_off >0 ); + ext_cluster =qemu_try_blockalign (bs->file->bs, s->cluster_size); +if (!ext_cluster) { +error_setg (errp,"Failed to allocate the Format Extension cluster" ); +return -ENOMEM ; + } + ret =bdrv_pread (bs->file, ext_off, s->cluster_size, ext_cluster,0 );if (ret <0 ) {error_setg_errno (errp, -ret,"Failed to read Format Extension cluster" );
Signature · 1 line
2.53.0
PATCH 7/8 parallels: fix GSList leak on the format extension success path
In reply to Denis V. Lunevparallels_parse_format_extension() returns directly on the end of features marker, leaking the list which tracks the loaded bitmaps. The bitmaps themselves belong to the block driver state, only the list nodes are lost.
--- block/parallels-ext.c | 1 + 1 file changed, 1 insertion(+)
block/parallels-ext.c +1 -0
@@ -290,6 +290,7 @@ parallels_parse_format_extension(BlockDriverState *bs, uint8_t *ext_cluster,switch (fh.magic) {case PARALLELS_END_OF_FEATURES_MAGIC : +g_slist_free (bitmaps);return 0 ;case PARALLELS_DIRTY_BITMAP_FEATURE_MAGIC :
Signature · 1 line
2.53.0
PATCH 8/8 iotests: cover the Parallels format extension parser
In reply to Denis V. LunevExtend the existing test with hand crafted extension clusters, built in the test rather than shipped as samples. Each corruption case is one qemu-img info run which either opens the image or reports why it was rejected: extension magic and checksum, feature magic and flags, payloads running past the cluster, bitmap granularity, an L1 entry which overflows when converted to an offset, and a cluster_size larger than the image file. Two more cases load a bitmap spanning two L1 entries.
Termination by a signal is not covered by check=False, so it is caught and logged to keep the remaining cases running.
--- .../qemu-iotests/tests/parallels-read-bitmap | 180 +++++++++++++++++- .../tests/parallels-read-bitmap.out | 33 ++++ 2 files changed, 212 insertions(+), 1 deletion(-)
tests/qemu-iotests/tests/parallels-read-bitmap +179 -1
@@ -18,14 +18,22 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +import hashlib +import struct +import uuid + +from qemu.utils import VerboseProcessError + import iotests -from iotests import qemu_nbd_popen, qemu_img_map, log, file_path +from iotests import qemu_nbd_popen, qemu_img, qemu_img_map, log, file_path iotests.script_initialize(supported_fmts=['parallels']) nbd_sock = file_path('nbd-sock', base_dir=iotests.sock_dir) +nbd_sock2 = file_path('nbd-sock2', base_dir=iotests.sock_dir) disk = iotests.file_path('disk') bitmap = 'e4f2eed0-37fe-4539-b50b-85d2e7fd235f' +MULTI_L1_UUID = '5f2e1c00-0000-4000-8000-0123456789ab' nbd_opts = f'driver=nbd,server.type=unix,server.path={nbd_sock}' \ f',x-dirty-bitmap=qemu:dirty-bitmap:{bitmap}'
@@ -51,3 +59,173 @@ with qemu_nbd_popen('--read-only', f'--socket={nbd_sock}', log(f'{a}-{b-1}') else: log(a) + + +# Hand crafted Format Extension clusters. + +EXT_MAGIC = 0xAB234CEF23DCEA87 +BITMAP_MAGIC = 0x20385FAE252CB34A + +CLUSTER = 512 # one sector per track +SECTORS = 8 # 4 KiB disk +EXT_SECTOR = 1 # the extension cluster follows the header +EH_SIZE = 24 # ParallelsFormatExtensionHeader +FH_SIZE = 24 # ParallelsFeatureHeader +BF_SIZE = 32 # ParallelsDirtyBitmapFeature + +crafted = file_path('crafted') + + +def feature(magic, data_size, flags=0): + return struct.pack('<QQII', magic, flags, data_size, 0) + + +def bitmap_feature(l1, granularity=1, nb_sectors=SECTORS, bid=None): + """ParallelsDirtyBitmapFeature followed by its L1 table""" + bid = uuid.UUID(bid).bytes if bid else uuid.uuid4().bytes + return (struct.pack('<Q16sII', nb_sectors, bid, granularity, len(l1)) + + b''.join(struct.pack('<Q', entry) for entry in l1)) + + +def extension(body, magic=EXT_MAGIC, checksum=True): + body = body.ljust(CLUSTER - EH_SIZE, b'\0') + csum = hashlib.md5(body).digest() if checksum else bytes(16) + return struct.pack('<Q16s', magic, csum) + body + + +def parallels_header(tracks, bat_entries, nb_sectors, ext_sector): + return struct.pack('<16sIIIIIQIIIQ', b'WithouFreSpacExt', 2, 16, 0, + tracks, bat_entries, nb_sectors, 0, 1, 0, ext_sector) + + +def write_sparse_image(body, tracks, nb_sectors, bat_entries, ext_sector): + """Image whose extension cluster is the given body, zero padded.""" + cluster_size = tracks * 512 + md5 = hashlib.md5() + md5.update(body) + md5.update(bytes(cluster_size - EH_SIZE - len(body))) + + with open(crafted, 'wb') as f: + f.write(parallels_header(tracks, bat_entries, nb_sectors, ext_sector)) + f.write(bytes(ext_sector * 512 - 64)) # BAT, unallocated + f.write(struct.pack('<Q16s', EXT_MAGIC, md5.digest()) + body) + f.truncate(ext_sector * 512 + cluster_size) # sparse zero tail + + +def report(name): + log(f'--- {name}') + try: + res = qemu_img('info', '-f', iotests.imgfmt, crafted, check=False) + except VerboseProcessError as exc: + # check=False does not cover termination by a signal + log(f'qemu-img died with signal {-exc.returncode}') + return + + log('image opened' if res.returncode == 0 + else iotests.filter_testfiles(res.stdout).strip()) + + +def check(name, ext, tracks=1): + header = parallels_header(tracks, SECTORS, SECTORS, EXT_SECTOR) + with open(crafted, 'wb') as f: + f.write(header) + f.write(bytes(EXT_SECTOR * 512 - len(header))) # BAT, unallocated + f.write(ext) + + report(name) + + +log('') + +# Control case, must keep working. +good = bitmap_feature([0]) +check('well-formed extension', + extension(feature(BITMAP_MAGIC, len(good)) + good + feature(0, 0))) + +check('wrong extension magic', + extension(feature(0, 0), magic=EXT_MAGIC ^ 1)) + +check('wrong extension checksum', + extension(feature(0, 0), checksum=False)) + +check('unknown feature', extension(feature(BITMAP_MAGIC ^ 1, 0))) + +check('feature flags set', extension(feature(0, 0, flags=1))) + +check('feature data_size beyond the cluster', + extension(feature(BITMAP_MAGIC, CLUSTER))) + +payload = CLUSTER - EH_SIZE - FH_SIZE +check('feature payload consumes the cluster', + extension(feature(BITMAP_MAGIC, payload) + + good.ljust(payload, b'\0'))) + +# bf.size must match the disk size or the compiler elides the OOB loads. +data1 = CLUSTER - 40 - EH_SIZE - FH_SIZE +check('second feature payload beyond the cluster', + extension(feature(BITMAP_MAGIC, data1) + good.ljust(data1, b'\0') + + feature(BITMAP_MAGIC, BF_SIZE + 8) + + struct.pack('<Q', SECTORS))) + +# Zero, not a power of two, and wrapping when shifted: all three assert. +for gran in (0, 3, 1 << 23): + bf = bitmap_feature([0], granularity=gran) + check(f'bitmap granularity {gran}', + extension(feature(BITMAP_MAGIC, len(bf)) + bf + feature(0, 0))) + +# entry << BDRV_SECTOR_BITS must not wrap into a negative offset. +bf = bitmap_feature([0xffffffffffffffff]) +check('bitmap L1 entry overflows', + extension(feature(BITMAP_MAGIC, len(bf)) + bf + feature(0, 0))) + +# Largest cluster_size parallels_open() accepts, about 2 GiB. +check('cluster_size beyond the file size', + extension(feature(0, 0)), tracks=0x7fffffff // 513) + + +def check_big_cluster(): + """Reaches the serialization coverage overflow, costs about 550 MiB.""" + tracks = 1 << 19 # cluster_size = 2^28 + nb_sectors = 1 << 32 # 2 TiB, needs two L1 entries + ext_sector = 256 # past the BAT + l1 = [0, 1] # entry 1 deserializes as all ones + + bmap = bitmap_feature(l1, nb_sectors=nb_sectors) + body = feature(BITMAP_MAGIC, len(bmap)) + bmap + feature(0, 0) + + write_sparse_image(body, tracks, nb_sectors, nb_sectors // tracks, + ext_sector) + report('bitmap serialization coverage overflow') + + +check_big_cluster() + + +def check_multi_l1(): + """Bitmap spanning two L1 entries with an ordinary 64 KiB cluster.""" + tracks = 128 # cluster_size = 64 KiB + nb_sectors = 1 << 27 # 64 GiB, needs two L1 entries + gran_sectors = 128 # 64 KiB, as in the sample image + bat_entries = nb_sectors // tracks + ext_sector = (64 + 4 * bat_entries) // 512 + 8 # past the BAT + l1 = [0, 1] # first half clean, second all ones + + bmap = bitmap_feature(l1, gran_sectors, nb_sectors, MULTI_L1_UUID) + body = feature(BITMAP_MAGIC, len(bmap)) + bmap + feature(0, 0) + + write_sparse_image(body, tracks, nb_sectors, bat_entries, ext_sector) + + log('--- bitmap spanning two L1 entries') + opts = f'driver=nbd,server.type=unix,server.path={nbd_sock2}' \ + f',x-dirty-bitmap=qemu:dirty-bitmap:{MULTI_L1_UUID}' + with qemu_nbd_popen('--read-only', f'--socket={nbd_sock2}', + f'--bitmap={MULTI_L1_UUID}', '-f', iotests.imgfmt, + crafted): + for chunk in qemu_img_map('--image-opts', opts): + if not chunk['data']: + first = chunk['start'] >> 30 + last = (chunk['start'] + chunk['length']) >> 30 + log(f'dirty {first}-{last} GiB') + + +check_multi_l1()
tests/qemu-iotests/tests/parallels-read-bitmap.out +33 -0
@@ -4,3 +4,36 @@ dirty clusters (cluster size is 64K): 10-12 30 Kill NBD server + +--- well-formed extension +image opened +--- wrong extension magic +qemu-img: Could not open 'TEST_DIR/PID-crafted': Wrong parallels Format Extension magic: 0xab234cef23dcea86, expected: 0xab234cef23dcea87 +--- wrong extension checksum +qemu-img: Could not open 'TEST_DIR/PID-crafted': Wrong checksum in Format Extension header. Format extension is corrupted. +--- unknown feature +qemu-img: Could not open 'TEST_DIR/PID-crafted': Unknown feature: 0x20385fae252cb34b +--- feature flags set +qemu-img: Could not open 'TEST_DIR/PID-crafted': Flags for extension feature are unsupported +--- feature data_size beyond the cluster +qemu-img: Could not open 'TEST_DIR/PID-crafted': Feature data_size exceedes Format Extension cluster +--- feature payload consumes the cluster +qemu-img: Could not open 'TEST_DIR/PID-crafted': Can not read feature header, as remaining bytes (0) in Format Extension is less than Feature header size (24) +--- second feature payload beyond the cluster +qemu-img: Could not open 'TEST_DIR/PID-crafted': Feature data_size exceedes Format Extension cluster +--- bitmap granularity 0 +qemu-img: Could not open 'TEST_DIR/PID-crafted': Invalid bitmap granularity 0, expected a power of two of at least 512 bytes +--- bitmap granularity 3 +qemu-img: Could not open 'TEST_DIR/PID-crafted': Invalid bitmap granularity 1536, expected a power of two of at least 512 bytes +--- bitmap granularity 8388608 +qemu-img: Could not open 'TEST_DIR/PID-crafted': Invalid bitmap granularity 4294967296, expected a power of two of at least 512 bytes +--- bitmap L1 entry overflows +qemu-img: Could not open 'TEST_DIR/PID-crafted': Failed to read bitmap data cluster: Input/output error +--- cluster_size beyond the file size +qemu-img: Could not open 'TEST_DIR/PID-crafted': Invalid image: Format Extension is outside the image file +--- bitmap serialization coverage overflow +image opened +--- bitmap spanning two L1 entries +Start NBD server +dirty 32-64 GiB +Kill NBD server
Signature · 1 line
2.53.0