Block layer patches (CVE-2024-4467)
The following changes since commit c80a339587fe4148292c260716482dd2f86d4476:
Merge tag 'pull-target-arm-20240701' of https://git.linaro.org/people/pmaydell/qemu-arm into staging (2024-07-01 10:41:45 -0700)
are available in the Git repository at:
https://repo.or.cz/qemu/kevin.git tags/for-upstream
for you to fetch changes up to 7ead946998610657d38d1a505d5f25300d4ca613:
block: Parse filenames only when explicitly requested (2024-07-02 18:12:30 +0200)
----------------------------------------------------------------
Block layer patches (CVE-2024-4467)
- Don't open qcow2 data files in 'qemu-img info'
- Disallow protocol prefixes for qcow2 data files, VMDK extent files and other child nodes that are neither 'file' nor 'backing'
----------------------------------------------------------------
Kevin Wolf (4):
qcow2: Don't open data_file with BDRV_O_NO_IO iotests/244: Don't store data-file with protocol in image iotests/270: Don't store data-file with json: prefix in image
block: Parse filenames only when explicitly requested
block.c | 90 +++++++++++++++++++++++++++++----------------- block/qcow2.c | 17 ++++++++- tests/qemu-iotests/061 | 6 ++-- tests/qemu-iotests/061.out | 8 +++-- tests/qemu-iotests/244 | 19 ++++++++-- tests/qemu-iotests/270 | 14 ++++++-- 6 files changed, 110 insertions(+), 44 deletions(-)
[PULL 1/4] qcow2: Don't open data_file with BDRV_O_NO_IO
One use case for 'qemu-img info' is verifying that untrusted images don't reference an unwanted external file, be it as a backing file or an external data file. To make sure that calling 'qemu-img info' can't already have undesired side effects with a malicious image, just don't open the data file at all with BDRV_O_NO_IO. If nothing ever tries to do I/O, we don't need to have it open.
This changes the output of iotests case 061, which used 'qemu-img info' to show that opening an image with an invalid data file fails. After this patch, it succeeds. Replace this part of the test with a qemu-io call, but keep the final 'qemu-img info' to show that the invalid data file is correctly displayed in the output.
--- block/qcow2.c | 17 ++++++++++++++++- tests/qemu-iotests/061 | 6 ++++-- tests/qemu-iotests/061.out | 8 ++++++-- 3 files changed, 26 insertions(+), 5 deletions(-)
block/qcow2.c +16 -1
@@ -1636,7 +1636,22 @@ qcow2_do_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } -if (open_data_file) { +if (open_data_file && (flags &BDRV_O_NO_IO )) { +/* +* Don't open the data file for 'qemu-img info' so that it can be used +* to verify that an untrusted qcow2 image doesn't refer to external +* files. +* +* Note: This still makes has_data_file() return true. +*/ +if (s->incompatible_features &QCOW2_INCOMPAT_DATA_FILE ) { + s->data_file =NULL ; + }else { + s->data_file = bs->file; + } +qdict_extract_subqdict (options,NULL ,"data-file." ); +qdict_del (options,"data-file" ); + }else if (open_data_file) {/* Open external data file */ bdrv_graph_co_rdunlock (); s->data_file = bdrv_co_open_child(NULL, options, "data-file", bs,
tests/qemu-iotests/061 +4 -2
@@ -326,12 +326,14 @@ $QEMU_IMG amend -o "data_file=foo" "$TEST_IMG" echo _make_test_img -o "compat=1.1,data_file=$TEST_IMG.data" 64M $QEMU_IMG amend -o "data_file=foo" "$TEST_IMG" -_img_info --format-specific +$QEMU_IO -c "read 0 4k" "$TEST_IMG" 2>&1 | _filter_testdir | _filter_imgfmt +$QEMU_IO -c "open -o data-file.filename=$TEST_IMG.data,file.filename=$TEST_IMG" -c "read 0 4k" | _filter_qemu_io TEST_IMG="data-file.filename=$TEST_IMG.data,file.filename=$TEST_IMG" _img_info --format-specific --image-opts echo $QEMU_IMG amend -o "data_file=" --image-opts "data-file.filename=$TEST_IMG.data,file.filename=$TEST_IMG" -_img_info --format-specific +$QEMU_IO -c "read 0 4k" "$TEST_IMG" 2>&1 | _filter_testdir | _filter_imgfmt +$QEMU_IO -c "open -o data-file.filename=$TEST_IMG.data,file.filename=$TEST_IMG" -c "read 0 4k" | _filter_qemu_io TEST_IMG="data-file.filename=$TEST_IMG.data,file.filename=$TEST_IMG" _img_info --format-specific --image-opts echo
tests/qemu-iotests/061.out +6 -2
@@ -545,7 +545,9 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 qemu-img: data-file can only be set for images that use an external data file Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 data_file=TEST_DIR/t.IMGFMT.data -qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Could not open 'foo': No such file or directory +qemu-io: can't open device TEST_DIR/t.IMGFMT: Could not open 'foo': No such file or directory +read 4096/4096 bytes at offset 0 +4 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) image: TEST_DIR/t.IMGFMT file format: IMGFMT virtual size: 64 MiB (67108864 bytes)
@@ -560,7 +562,9 @@ Format specific information: corrupt: false extended l2: false -qemu-img: Could not open 'TEST_DIR/t.IMGFMT': 'data-file' is required for this image +qemu-io: can't open device TEST_DIR/t.IMGFMT: 'data-file' is required for this image +read 4096/4096 bytes at offset 0 +4 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) image: TEST_DIR/t.IMGFMT file format: IMGFMT virtual size: 64 MiB (67108864 bytes)
Signature · 1 line
2.45.2
[PULL 2/4] iotests/244: Don't store data-file with protocol in image
In reply to Kevin WolfWe want to disable filename parsing for data files because it's too easy to abuse in malicious image files. Make the test ready for the change by passing the data file explicitly in command line options.
--- tests/qemu-iotests/244 | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
tests/qemu-iotests/244 +16 -3
@@ -215,9 +215,22 @@ $QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG" $QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG" # blkdebug doesn't support copy offloading, so this tests the error path -$QEMU_IMG amend -f $IMGFMT -o "data_file=blkdebug::$TEST_IMG.data" "$TEST_IMG" -$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG" -$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG" +test_img_with_blkdebug="json:{ + 'driver': 'qcow2', + 'file': { + 'driver': 'file', + 'filename': '$TEST_IMG' + }, + 'data-file': { + 'driver': 'blkdebug', + 'image': { + 'driver': 'file', + 'filename': '$TEST_IMG.data' + } + } +}" +$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$test_img_with_blkdebug" +$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$test_img_with_blkdebug" echo echo "=== Flushing should flush the data file ==="
Signature · 1 line
2.45.2
[PULL 3/4] iotests/270: Don't store data-file with json: prefix in image
In reply to Kevin WolfWe want to disable filename parsing for data files because it's too easy to abuse in malicious image files. Make the test ready for the change by passing the data file explicitly in command line options.
--- tests/qemu-iotests/270 | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
tests/qemu-iotests/270 +11 -3
@@ -60,8 +60,16 @@ _make_test_img -o cluster_size=2M,data_file="$TEST_IMG.orig" \ # "write" 2G of data without using any space. # (qemu-img create does not like it, though, because null-co does not # support image creation.) -$QEMU_IMG amend -o data_file="json:{'driver':'null-co',,'size':'4294967296'}" \ - "$TEST_IMG" +test_img_with_null_data="json:{ + 'driver': '$IMGFMT', + 'file': { + 'filename': '$TEST_IMG' + }, + 'data-file': { + 'driver': 'null-co', + 'size':'4294967296' + } +}" # This gives us a range of: # 2^31 - 512 + 768 - 1 = 2^31 + 255 > 2^31
@@ -74,7 +82,7 @@ $QEMU_IMG amend -o data_file="json:{'driver':'null-co',,'size':'4294967296'}" \ # on L2 boundaries, we need large L2 tables; hence the cluster size of # 2 MB. (Anything from 256 kB should work, though, because then one L2 # table covers 8 GB.) -$QEMU_IO -c "write 768 $((2 ** 31 - 512))" "$TEST_IMG" | _filter_qemu_io +$QEMU_IO -c "write 768 $((2 ** 31 - 512))" "$test_img_with_null_data" | _filter_qemu_io _check_test_img
Signature · 1 line
2.45.2
[PULL 4/4] block: Parse filenames only when explicitly requested
In reply to Kevin WolfWhen handling image filenames from legacy options such as -drive or from tools, these filenames are parsed for protocol prefixes, including for the json:{} pseudo-protocol.
This behaviour is intended for filenames that come directly from the command line and for backing files, which may come from the image file itself. Higher level management tools generally take care to verify that untrusted images don't contain a bad (or any) backing file reference; 'qemu-img info' is a suitable tool for this.
However, for other files that can be referenced in images, such as qcow2 data files or VMDK extents, the string from the image file is usually not verified by management tools - and 'qemu-img info' wouldn't be suitable because in contrast to backing files, it already opens these other referenced files. So here the string should be interpreted as a literal local filename. More complex configurations need to be specified explicitly on the command line or in QMP.
This patch changes bdrv_open_inherit() so that it only parses filenames if a new parameter parse_filename is true. It is set for the top level in bdrv_open(), for the file child and for the backing file child. All other callers pass false and disable filename parsing this way.
--- block.c | 90 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 33 deletions(-)
block.c +57 -33
@@ -86,6 +86,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,BlockDriverState *parent, const BdrvChildClass *child_class, BdrvChildRole child_role, + bool parse_filename, Error **errp); static bool bdrv_recurse_has_child(BlockDriverState *bs,
@@ -2055,7 +2056,8 @@ static void parse_json_protocol(QDict *options, const char **pfilename,* block driver has been specified explicitly. */ static int bdrv_fill_options (QDict **options,const char *filename, -int *flags,Error **errp) +int *flags,bool allow_parse_filename, +Error **errp) {const char *drvname;bool protocol = *flags &BDRV_O_PROTOCOL ;
@@ -2097,7 +2099,7 @@ static int bdrv_fill_options(QDict **options, const char *filename,if (protocol && filename) {if (!qdict_haskey (*options,"filename" )) {qdict_put_str (*options,"filename" , filename); - parse_filename = true; + parse_filename = allow_parse_filename; }else {error_setg (errp,"Can't specify 'file' and 'filename' options at " "the same time" );
@@ -3660,7 +3662,8 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options, } backing_hd =bdrv_open_inherit (backing_filename, reference, options,0 , bs, - &child_of_bds,bdrv_backing_role (bs), errp); + &child_of_bds,bdrv_backing_role (bs), true, + errp);if (!backing_hd) { bs->open_flags |=BDRV_O_NO_BACKING ;error_prepend (errp,"Could not open backing file: " );
@@ -3694,7 +3697,8 @@ free_exit:static BlockDriverState *bdrv_open_child_bs (const char *filename,QDict *options,const char *bdref_key,BlockDriverState *parent,const BdrvChildClass *child_class, -BdrvChildRole child_role,bool allow_none,Error **errp) +BdrvChildRole child_role,bool allow_none, +bool parse_filename,Error **errp) {BlockDriverState *bs =NULL ;QDict *image_options;
@@ -3725,7 +3729,8 @@ bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key, } bs =bdrv_open_inherit (filename, reference, image_options,0 , - parent, child_class, child_role, errp); + parent, child_class, child_role, parse_filename, + errp);if (!bs) { goto done; }
@@ -3735,6 +3740,33 @@ done:return bs; } +static BdrvChild *bdrv_open_child_common (const char *filename, +QDict *options,const char *bdref_key, +BlockDriverState *parent, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +bool allow_none,bool parse_filename, +Error **errp) +{ +BlockDriverState *bs; +BdrvChild *child; + +GLOBAL_STATE_CODE (); + + bs =bdrv_open_child_bs (filename, options, bdref_key, parent, child_class, + child_role, allow_none, parse_filename, errp); +if (bs ==NULL ) { +return NULL ; + } + +bdrv_graph_wrlock (); + child =bdrv_attach_child (parent, bs, bdref_key, child_class, child_role, + errp); +bdrv_graph_wrunlock (); + +return child; +} +/* * Opens a disk image whose options are given as BlockdevRef in another block * device's options.
@@ -3758,27 +3790,15 @@ BdrvChild *bdrv_open_child(const char *filename,BdrvChildRole child_role, bool allow_none, Error **errp) { - BlockDriverState *bs; -BdrvChild *child; - -GLOBAL_STATE_CODE (); - - bs =bdrv_open_child_bs (filename, options, bdref_key, parent, child_class, - child_role, allow_none, errp); -if (bs ==NULL ) { -return NULL ; - } - -bdrv_graph_wrlock (); - child =bdrv_attach_child (parent, bs, bdref_key, child_class, child_role, - errp); -bdrv_graph_wrunlock (); - -return child; + return bdrv_open_child_common(filename, options, bdref_key, parent, +child_class ,child_role ,allow_none ,false , +errp ); }/* -* Wrapper on bdrv_open_child() for most popular case: open primary child of bs. +* This does mostly the same as bdrv_open_child(), but for opening the primary +* child of a node. A notable difference from bdrv_open_child() is that it +* enables filename parsing for protocol names (including json:). * * @parent can move to a different AioContext in this function. */
@@ -3793,8 +3813,8 @@ int bdrv_open_file_child(const char *filename, role = parent->drv->is_filter ? (BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY ) :BDRV_CHILD_IMAGE ; -if (!bdrv_open_child (filename, options, bdref_key, parent, - &child_of_bds, role, false, errp)) +if (!bdrv_open_child_common (filename, options, bdref_key, parent, + &child_of_bds, role, false, true, errp)) {return -EINVAL ; }
@@ -3839,7 +3859,8 @@ BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp) } - bs =bdrv_open_inherit (NULL , reference, qdict,0 ,NULL ,NULL ,0 , errp); + bs =bdrv_open_inherit (NULL , reference, qdict,0 ,NULL ,NULL ,0 , false, + errp); obj =NULL ;qobject_unref (obj);visit_free (v);
@@ -3929,7 +3950,7 @@ static BlockDriverState * no_coroutine_fn bdrv_open_inherit(const char *filename, const char *reference, QDict *options, int flags, BlockDriverState *parent, const BdrvChildClass *child_class, BdrvChildRole child_role, - Error **errp) + bool parse_filename, Error **errp) { int ret;BlockBackend *file =NULL ;
@@ -3977,9 +3998,11 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options, }/* json: syntax counts as explicit options, as if in the QDict */ -parse_json_protocol (options, &filename, &local_err); -if (local_err) { - goto fail; +if (parse_filename) { +parse_json_protocol (options, &filename, &local_err); +if (local_err) { + goto fail; + } } bs->explicit_options =qdict_clone_shallow (options);
@@ -4004,7 +4027,8 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options, parent->open_flags, parent->options); } - ret =bdrv_fill_options (&options, filename, &flags, &local_err); + ret =bdrv_fill_options (&options, filename, &flags, parse_filename, + &local_err);if (ret <0 ) { goto fail; }
@@ -4073,7 +4097,7 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options, file_bs =bdrv_open_child_bs (filename, options,"file" , bs, &child_of_bds,BDRV_CHILD_IMAGE , - true, &local_err); + true, true, &local_err);if (local_err) { goto fail; }
@@ -4222,7 +4246,7 @@ BlockDriverState *bdrv_open(const char *filename, const char *reference,GLOBAL_STATE_CODE ();return bdrv_open_inherit (filename, reference, options, flags,NULL , -NULL ,0 , errp); +NULL ,0 , true, errp); }/* Return true if the NULL-terminated @list contains @str */
Signature · 1 line
2.45.2
Michael Tokarev
Reviewed-by Eric Blake Reviewed-by Stefan Hajnoczi Reviewed-by Hanna Czenczek Reviewed-by Eric Blake Reviewed-by Stefan Hajnoczi Reviewed-by Hanna CzenczekRe: [PULL 4/4] block: Parse filenames only when explicitly requested
Quoted 18 lines · Kevin Wolf
When handling image filenames from legacy options such as -drive or from tools, these filenames are parsed for protocol prefixes, including for the json:{} pseudo-protocol.
This behaviour is intended for filenames that come directly from the command line and for backing files, which may come from the image file itself. Higher level management tools generally take care to verify that untrusted images don't contain a bad (or any) backing file reference; 'qemu-img info' is a suitable tool for this.
However, for other files that can be referenced in images, such as qcow2 data files or VMDK extents, the string from the image file is usually not verified by management tools - and 'qemu-img info' wouldn't be suitable because in contrast to backing files, it already opens these other referenced files. So here the string should be interpreted as a literal local filename. More complex configurations need to be specified explicitly on the command line or in QMP.
Quoted 4 lines
This patch changes
bdrv_open_inherit()so that it only parses filenames if a new parameterparse_filenameis true. It is set for the top level inbdrv_open(), for the file child and for the backing file child. All other callers pass false and disable filename parsing this way.
I'm attaching backports of this change to 8.2 and 7.2 series.
Please check if the resulting backports are correct, or if they're needed in the first place. Note: 7.2 lacks quite some locking in this area, eg v8.0.0-2069-g8394c35ee148 "block: Fix AioContext locking in bdrv_open_child()" v8.2.0-rc0-59-g6bc0bcc89f84 "block: Fix deadlocks in bdrv_graph_wrunlock()".
Thanks,
/mjt
Signature · 4 lines
GPG Key transition (from rsa2048 to rsa4096) since 2024-04-24. New key: rsa4096/61AD3D98ECDF2C8E 9D8B E14E 3F2A 9DD7 9199 28F1 61AD 3D98 ECDF 2C8E Old key: rsa2048/457CE0A0804465C5 6EE1 95D1 886E 8FFB 810D 4324 457C E0A0 8044 65C5 Transition statement: http://www.corpit.ru/mjt/gpg-transition-2024.txt
From 0408443ecb8785a20652b529aa61f020e304112c Mon Sep 17 00:00:00 2001
From: Kevin Wolf <[email protected]>
Date: Thu, 25 Apr 2024 14:56:02 +0200
Subject: [PATCH] block: Parse filenames only when explicitly requested
When handling image filenames from legacy options such as -drive or from tools, these filenames are parsed for protocol prefixes, including for the json:{} pseudo-protocol.
This behaviour is intended for filenames that come directly from the command line and for backing files, which may come from the image file itself. Higher level management tools generally take care to verify that untrusted images don't contain a bad (or any) backing file reference; 'qemu-img info' is a suitable tool for this.
However, for other files that can be referenced in images, such as qcow2 data files or VMDK extents, the string from the image file is usually not verified by management tools - and 'qemu-img info' wouldn't be suitable because in contrast to backing files, it already opens these other referenced files. So here the string should be interpreted as a literal local filename. More complex configurations need to be specified explicitly on the command line or in QMP.
This patch changes bdrv_open_inherit() so that it only parses filenames if a new parameter parse_filename is true. It is set for the top level in bdrv_open(), for the file child and for the backing file child. All other callers pass false and disable filename parsing this way.
(Mjt: backport patch to 7.2, without:
v8.0.0-2069-g8394c35ee148 "block: Fix AioContext locking in bdrv_open_child()" v8.1.0-801-gafdaeb9ea06e "block: Mark bdrv_attach_child() GRAPH_WRLOCK" v8.2.0-rc0-59-g6bc0bcc89f84 "block: Fix deadlocks in bdrv_graph_wrunlock()" v8.2.0-132-g6bc30f194985 "graph-lock: remove AioContext locking" v8.2.0-133-gb49f4755c7fa "block: remove AioContext locking")
--- block.c | 76 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 27 deletions(-)
block.c +49 -27
@@ -85,6 +85,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,BlockDriverState *parent, const BdrvChildClass *child_class, BdrvChildRole child_role, + bool parse_filename, Error **errp); static bool bdrv_recurse_has_child(BlockDriverState *bs,
@@ -2051,7 +2052,8 @@ static void parse_json_protocol(QDict *options, const char **pfilename,* block driver has been specified explicitly. */ static int bdrv_fill_options (QDict **options,const char *filename, -int *flags,Error **errp) +int *flags,bool allow_parse_filename, +Error **errp) {const char *drvname;bool protocol = *flags &BDRV_O_PROTOCOL ;
@@ -2093,7 +2095,7 @@ static int bdrv_fill_options(QDict **options, const char *filename,if (protocol && filename) {if (!qdict_haskey (*options,"filename" )) {qdict_put_str (*options,"filename" , filename); - parse_filename = true; + parse_filename = allow_parse_filename; }else {error_setg (errp,"Can't specify 'file' and 'filename' options at " "the same time" );
@@ -3516,7 +3518,8 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options, } backing_hd =bdrv_open_inherit (backing_filename, reference, options,0 , bs, - &child_of_bds,bdrv_backing_role (bs), errp); + &child_of_bds,bdrv_backing_role (bs), true, + errp);if (!backing_hd) { bs->open_flags |=BDRV_O_NO_BACKING ;error_prepend (errp,"Could not open backing file: " );
@@ -3549,7 +3552,8 @@ free_exit:static BlockDriverState *bdrv_open_child_bs (const char *filename,QDict *options,const char *bdref_key,BlockDriverState *parent,const BdrvChildClass *child_class, -BdrvChildRole child_role,bool allow_none,Error **errp) +BdrvChildRole child_role,bool allow_none, +bool parse_filename,Error **errp) {BlockDriverState *bs =NULL ;QDict *image_options;
@@ -3580,7 +3584,8 @@ bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key, } bs =bdrv_open_inherit (filename, reference, image_options,0 , - parent, child_class, child_role, errp); + parent, child_class, child_role, parse_filename, + errp);if (!bs) { goto done; }
@@ -3590,6 +3595,28 @@ done:return bs; } +static BdrvChild *bdrv_open_child_common (const char *filename, +QDict *options,const char *bdref_key, +BlockDriverState *parent, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +bool allow_none,bool parse_filename, +Error **errp) +{ +BlockDriverState *bs; + +GLOBAL_STATE_CODE (); + + bs =bdrv_open_child_bs (filename, options, bdref_key, parent, child_class, + child_role, allow_none, parse_filename, errp); +if (bs ==NULL ) { +return NULL ; + } + +return bdrv_attach_child (parent, bs, bdref_key, child_class, child_role, + errp); +} +/* * Opens a disk image whose options are given as BlockdevRef in another block * device's options.
@@ -3611,18 +3638,9 @@ BdrvChild *bdrv_open_child(const char *filename,BdrvChildRole child_role, bool allow_none, Error **errp) { - BlockDriverState *bs; - -GLOBAL_STATE_CODE (); - - bs =bdrv_open_child_bs (filename, options, bdref_key, parent, child_class, - child_role, allow_none, errp); -if (bs ==NULL ) { -return NULL ; - } - -return bdrv_attach_child (parent, bs, bdref_key, child_class, child_role, - errp); + return bdrv_open_child_common(filename, options, bdref_key, parent, +child_class ,child_role ,allow_none ,false , +errp ); }/*
@@ -3639,8 +3657,8 @@ int bdrv_open_file_child(const char *filename, role = parent->drv->is_filter ? (BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY ) :BDRV_CHILD_IMAGE ; -if (!bdrv_open_child (filename, options, bdref_key, parent, - &child_of_bds, role, false, errp)) +if (!bdrv_open_child_common (filename, options, bdref_key, parent, + &child_of_bds, role, false, true, errp)) {return -EINVAL ; }
@@ -3685,7 +3703,8 @@ BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp) } - bs =bdrv_open_inherit (NULL , reference, qdict,0 ,NULL ,NULL ,0 , errp); + bs =bdrv_open_inherit (NULL , reference, qdict,0 ,NULL ,NULL ,0 , false, + errp); obj =NULL ;qobject_unref (obj);visit_free (v);
@@ -3775,7 +3794,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,BlockDriverState *parent, const BdrvChildClass *child_class, BdrvChildRole child_role, - Error **errp) + bool parse_filename, Error **errp) { int ret;BlockBackend *file =NULL ;
@@ -3819,9 +3838,11 @@ static BlockDriverState *bdrv_open_inherit(const char *filename, }/* json: syntax counts as explicit options, as if in the QDict */ -parse_json_protocol (options, &filename, &local_err); -if (local_err) { - goto fail; +if (parse_filename) { +parse_json_protocol (options, &filename, &local_err); +if (local_err) { + goto fail; + } } bs->explicit_options =qdict_clone_shallow (options);
@@ -3846,7 +3867,8 @@ static BlockDriverState *bdrv_open_inherit(const char *filename, parent->open_flags, parent->options); } - ret =bdrv_fill_options (&options, filename, &flags, &local_err); + ret =bdrv_fill_options (&options, filename, &flags, parse_filename, + &local_err);if (ret <0 ) { goto fail; }
@@ -3915,7 +3937,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename, file_bs =bdrv_open_child_bs (filename, options,"file" , bs, &child_of_bds,BDRV_CHILD_IMAGE , - true, &local_err); + true, true, &local_err);if (local_err) { goto fail; }
@@ -4062,7 +4084,7 @@ BlockDriverState *bdrv_open(const char *filename, const char *reference,GLOBAL_STATE_CODE ();return bdrv_open_inherit (filename, reference, options, flags,NULL , -NULL ,0 , errp); +NULL ,0 , true, errp); }/* Return true if the NULL-terminated @list contains @str */
Signature · 1 line
2.39.2
From aea89f41799f85ca1ed724d9e5fc2fc6cce25078 Mon Sep 17 00:00:00 2001
From: Kevin Wolf <[email protected]>
Date: Thu, 25 Apr 2024 14:56:02 +0200
Subject: [PATCH] block: Parse filenames only when explicitly requested
When handling image filenames from legacy options such as -drive or from tools, these filenames are parsed for protocol prefixes, including for the json:{} pseudo-protocol.
This behaviour is intended for filenames that come directly from the command line and for backing files, which may come from the image file itself. Higher level management tools generally take care to verify that untrusted images don't contain a bad (or any) backing file reference; 'qemu-img info' is a suitable tool for this.
However, for other files that can be referenced in images, such as qcow2 data files or VMDK extents, the string from the image file is usually not verified by management tools - and 'qemu-img info' wouldn't be suitable because in contrast to backing files, it already opens these other referenced files. So here the string should be interpreted as a literal local filename. More complex configurations need to be specified explicitly on the command line or in QMP.
This patch changes bdrv_open_inherit() so that it only parses filenames if a new parameter parse_filename is true. It is set for the top level in bdrv_open(), for the file child and for the backing file child. All other callers pass false and disable filename parsing this way.
(Mjt: backport patch to 8.2, without:
v8.2.0-132-g6bc30f194985 "graph-lock: remove AioContext locking" v8.2.0-133-gb49f4755c7fa "block: remove AioContext locking")
--- block.c | 98 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 37 deletions(-)
block.c +61 -37
191 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PULL 4/4] block: Parse filenames only when explicitly requested
Am 03.07.2024 um 23:16 hat Michael Tokarev geschrieben:
Quoted 24 lines
Quoted 18 lines · Kevin Wolf
When handling image filenames from legacy options such as -drive or from tools, these filenames are parsed for protocol prefixes, including for the json:{} pseudo-protocol.
This behaviour is intended for filenames that come directly from the command line and for backing files, which may come from the image file itself. Higher level management tools generally take care to verify that untrusted images don't contain a bad (or any) backing file reference; 'qemu-img info' is a suitable tool for this.
However, for other files that can be referenced in images, such as qcow2 data files or VMDK extents, the string from the image file is usually not verified by management tools - and 'qemu-img info' wouldn't be suitable because in contrast to backing files, it already opens these other referenced files. So here the string should be interpreted as a literal local filename. More complex configurations need to be specified explicitly on the command line or in QMP.
Quoted 4 lines
This patch changes
bdrv_open_inherit()so that it only parses filenames if a new parameterparse_filenameis true. It is set for the top level inbdrv_open(), for the file child and for the backing file child. All other callers pass false and disable filename parsing this way.
Quoted 6 lines
I'm attaching backports of this change to 8.2 and 7.2 series.
Please check if the resulting backports are correct, or if they're needed in the first place. Note: 7.2 lacks quite some locking in this area, eg v8.0.0-2069-g8394c35ee148 "block: Fix AioContext locking in
bdrv_open_child()" v8.2.0-rc0-59-g6bc0bcc89f84 "block: Fix deadlocks inbdrv_graph_wrunlock()".
Apart from minor style differences, your 7.2 backport is a perfect match of the RHEL 9.2 backport which I already reviewed downstream. The 8.2 patch is a bit different from the RHEL 9.4 one because we backported the final bits of the multiqueue work, but the differences make sense for upstream QEMU 8.2.
So both patches look good to me.
Kevin
Re: [PULL 4/4] block: Parse filenames only when explicitly requested
Quoted 9 lines · Kevin Wolf
Am 03.07.2024 um 23:16 hat Michael Tokarev geschrieben:
Quoted 6 lines
I'm attaching backports of this change to 8.2 and 7.2 series.
Please check if the resulting backports are correct, or if they're needed in the first place. Note: 7.2 lacks quite some locking in this area, eg v8.0.0-2069-g8394c35ee148 "block: Fix AioContext locking in
bdrv_open_child()" v8.2.0-rc0-59-g6bc0bcc89f84 "block: Fix deadlocks inbdrv_graph_wrunlock()".
Quoted 7 lines
Apart from minor style differences, your 7.2 backport is a perfect match of the RHEL 9.2 backport which I already reviewed downstream. The 8.2 patch is a bit different from the RHEL 9.4 one because we backported the final bits of the multiqueue work, but the differences make sense for upstream QEMU 8.2.
So both patches look good to me.
Ok, this makes sense. Thank you for the review, Kevin!
/mjt
Signature · 4 lines
GPG Key transition (from rsa2048 to rsa4096) since 2024-04-24. New key: rsa4096/61AD3D98ECDF2C8E 9D8B E14E 3F2A 9DD7 9199 28F1 61AD 3D98 ECDF 2C8E Old key: rsa2048/457CE0A0804465C5 6EE1 95D1 886E 8FFB 810D 4324 457C E0A0 8044 65C5 Transition statement: http://www.corpit.ru/mjt/gpg-transition-2024.txt
Richard Henderson
AppliedQuoted 10 lines · Kevin Wolf
The following changes since commit
c80a339587fe4148292c260716482dd2f86d4476:Merge tag 'pull-target-arm-20240701' ofhttps://git.linaro.org/people/pmaydell/qemu-arm into staging (2024-07-01 10:41:45 -0700)are available in the Git repository at:
https://repo.or.cz/qemu/kevin.git tags/for-upstream
for you to fetch changes up to
7ead946998610657d38d1a505d5f25300d4ca613:
Quoted 8 lines
block: Parse filenames only when explicitly requested (2024-07-02 18:12:30 +0200)
----------------------------------------------------------------
Block layer patches (CVE-2024-4467)- Don't open qcow2 data files in 'qemu-img info'
- Disallow protocol prefixes for qcow2 data files, VMDK extent files and other child nodes that are neither 'file' nor 'backing'
Applied, thanks. Please update https://wiki.qemu.org/ChangeLog/9.1 as appropriate.
r~