Skip to content

Commit 7ead946

Browse files
committed
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. Cc: [email protected] Signed-off-by: Kevin Wolf <[email protected]> Reviewed-by: Eric Blake <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Reviewed-by: Hanna Czenczek <[email protected]>
1 parent 7e11106 commit 7ead946

File tree

1 file changed

+57
-33
lines changed

1 file changed

+57
-33
lines changed

block.c

+57-33
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,
8686
BlockDriverState *parent,
8787
const BdrvChildClass *child_class,
8888
BdrvChildRole child_role,
89+
bool parse_filename,
8990
Error **errp);
9091

9192
static bool bdrv_recurse_has_child(BlockDriverState *bs,
@@ -2055,7 +2056,8 @@ static void parse_json_protocol(QDict *options, const char **pfilename,
20552056
* block driver has been specified explicitly.
20562057
*/
20572058
static int bdrv_fill_options(QDict **options, const char *filename,
2058-
int *flags, Error **errp)
2059+
int *flags, bool allow_parse_filename,
2060+
Error **errp)
20592061
{
20602062
const char *drvname;
20612063
bool protocol = *flags & BDRV_O_PROTOCOL;
@@ -2097,7 +2099,7 @@ static int bdrv_fill_options(QDict **options, const char *filename,
20972099
if (protocol && filename) {
20982100
if (!qdict_haskey(*options, "filename")) {
20992101
qdict_put_str(*options, "filename", filename);
2100-
parse_filename = true;
2102+
parse_filename = allow_parse_filename;
21012103
} else {
21022104
error_setg(errp, "Can't specify 'file' and 'filename' options at "
21032105
"the same time");
@@ -3660,7 +3662,8 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
36603662
}
36613663

36623664
backing_hd = bdrv_open_inherit(backing_filename, reference, options, 0, bs,
3663-
&child_of_bds, bdrv_backing_role(bs), errp);
3665+
&child_of_bds, bdrv_backing_role(bs), true,
3666+
errp);
36643667
if (!backing_hd) {
36653668
bs->open_flags |= BDRV_O_NO_BACKING;
36663669
error_prepend(errp, "Could not open backing file: ");
@@ -3694,7 +3697,8 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
36943697
static BlockDriverState *
36953698
bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key,
36963699
BlockDriverState *parent, const BdrvChildClass *child_class,
3697-
BdrvChildRole child_role, bool allow_none, Error **errp)
3700+
BdrvChildRole child_role, bool allow_none,
3701+
bool parse_filename, Error **errp)
36983702
{
36993703
BlockDriverState *bs = NULL;
37003704
QDict *image_options;
@@ -3725,7 +3729,8 @@ bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key,
37253729
}
37263730

37273731
bs = bdrv_open_inherit(filename, reference, image_options, 0,
3728-
parent, child_class, child_role, errp);
3732+
parent, child_class, child_role, parse_filename,
3733+
errp);
37293734
if (!bs) {
37303735
goto done;
37313736
}
@@ -3735,6 +3740,33 @@ bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key,
37353740
return bs;
37363741
}
37373742

3743+
static BdrvChild *bdrv_open_child_common(const char *filename,
3744+
QDict *options, const char *bdref_key,
3745+
BlockDriverState *parent,
3746+
const BdrvChildClass *child_class,
3747+
BdrvChildRole child_role,
3748+
bool allow_none, bool parse_filename,
3749+
Error **errp)
3750+
{
3751+
BlockDriverState *bs;
3752+
BdrvChild *child;
3753+
3754+
GLOBAL_STATE_CODE();
3755+
3756+
bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_class,
3757+
child_role, allow_none, parse_filename, errp);
3758+
if (bs == NULL) {
3759+
return NULL;
3760+
}
3761+
3762+
bdrv_graph_wrlock();
3763+
child = bdrv_attach_child(parent, bs, bdref_key, child_class, child_role,
3764+
errp);
3765+
bdrv_graph_wrunlock();
3766+
3767+
return child;
3768+
}
3769+
37383770
/*
37393771
* Opens a disk image whose options are given as BlockdevRef in another block
37403772
* device's options.
@@ -3758,27 +3790,15 @@ BdrvChild *bdrv_open_child(const char *filename,
37583790
BdrvChildRole child_role,
37593791
bool allow_none, Error **errp)
37603792
{
3761-
BlockDriverState *bs;
3762-
BdrvChild *child;
3763-
3764-
GLOBAL_STATE_CODE();
3765-
3766-
bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_class,
3767-
child_role, allow_none, errp);
3768-
if (bs == NULL) {
3769-
return NULL;
3770-
}
3771-
3772-
bdrv_graph_wrlock();
3773-
child = bdrv_attach_child(parent, bs, bdref_key, child_class, child_role,
3774-
errp);
3775-
bdrv_graph_wrunlock();
3776-
3777-
return child;
3793+
return bdrv_open_child_common(filename, options, bdref_key, parent,
3794+
child_class, child_role, allow_none, false,
3795+
errp);
37783796
}
37793797

37803798
/*
3781-
* Wrapper on bdrv_open_child() for most popular case: open primary child of bs.
3799+
* This does mostly the same as bdrv_open_child(), but for opening the primary
3800+
* child of a node. A notable difference from bdrv_open_child() is that it
3801+
* enables filename parsing for protocol names (including json:).
37823802
*
37833803
* @parent can move to a different AioContext in this function.
37843804
*/
@@ -3793,8 +3813,8 @@ int bdrv_open_file_child(const char *filename,
37933813
role = parent->drv->is_filter ?
37943814
(BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY) : BDRV_CHILD_IMAGE;
37953815

3796-
if (!bdrv_open_child(filename, options, bdref_key, parent,
3797-
&child_of_bds, role, false, errp))
3816+
if (!bdrv_open_child_common(filename, options, bdref_key, parent,
3817+
&child_of_bds, role, false, true, errp))
37983818
{
37993819
return -EINVAL;
38003820
}
@@ -3839,7 +3859,8 @@ BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp)
38393859

38403860
}
38413861

3842-
bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, 0, errp);
3862+
bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, 0, false,
3863+
errp);
38433864
obj = NULL;
38443865
qobject_unref(obj);
38453866
visit_free(v);
@@ -3929,7 +3950,7 @@ static BlockDriverState * no_coroutine_fn
39293950
bdrv_open_inherit(const char *filename, const char *reference, QDict *options,
39303951
int flags, BlockDriverState *parent,
39313952
const BdrvChildClass *child_class, BdrvChildRole child_role,
3932-
Error **errp)
3953+
bool parse_filename, Error **errp)
39333954
{
39343955
int ret;
39353956
BlockBackend *file = NULL;
@@ -3977,9 +3998,11 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options,
39773998
}
39783999

39794000
/* json: syntax counts as explicit options, as if in the QDict */
3980-
parse_json_protocol(options, &filename, &local_err);
3981-
if (local_err) {
3982-
goto fail;
4001+
if (parse_filename) {
4002+
parse_json_protocol(options, &filename, &local_err);
4003+
if (local_err) {
4004+
goto fail;
4005+
}
39834006
}
39844007

39854008
bs->explicit_options = qdict_clone_shallow(options);
@@ -4004,7 +4027,8 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options,
40044027
parent->open_flags, parent->options);
40054028
}
40064029

4007-
ret = bdrv_fill_options(&options, filename, &flags, &local_err);
4030+
ret = bdrv_fill_options(&options, filename, &flags, parse_filename,
4031+
&local_err);
40084032
if (ret < 0) {
40094033
goto fail;
40104034
}
@@ -4073,7 +4097,7 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options,
40734097

40744098
file_bs = bdrv_open_child_bs(filename, options, "file", bs,
40754099
&child_of_bds, BDRV_CHILD_IMAGE,
4076-
true, &local_err);
4100+
true, true, &local_err);
40774101
if (local_err) {
40784102
goto fail;
40794103
}
@@ -4222,7 +4246,7 @@ BlockDriverState *bdrv_open(const char *filename, const char *reference,
42224246
GLOBAL_STATE_CODE();
42234247

42244248
return bdrv_open_inherit(filename, reference, options, flags, NULL,
4225-
NULL, 0, errp);
4249+
NULL, 0, true, errp);
42264250
}
42274251

42284252
/* Return true if the NULL-terminated @list contains @str */

0 commit comments

Comments
 (0)