When opening the image, "json-file:/path/to/json/file" has the same as "json-file:$(cat /path/to/json/file)". The advantage is that sensitive information that would be exposed in /proc/$PID/cmdline or log files, is no longer visible this way.
Signed-off-by: Fam Zheng <[email protected]> --- block.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/block.c b/block.c index e9f40dc..785f317 100644 --- a/block.c +++ b/block.c @@ -962,10 +962,6 @@ static QDict *parse_json_filename(const char *filename, Error **errp) { QObject *options_obj; QDict *options; - int ret; - - ret = strstart(filename, "json:", &filename); - assert(ret); options_obj = qobject_from_json(filename); if (!options_obj) { @@ -985,6 +981,36 @@ static QDict *parse_json_filename(const char *filename, Error **errp) return options; } +static char *read_json_file(const char *filename, Error **errp) +{ + BlockDriverState *file = NULL; + QDict *options; + int r; + int64_t sectors; + char *buf; + + options = qdict_new(); + qdict_put(options, "driver", + qstring_from_str("raw")); + r = bdrv_open(&file, filename, NULL, options, 0, errp); + if (r) { + return NULL; + } + sectors = DIV_ROUND_UP(bdrv_getlength(file), BDRV_SECTOR_SIZE); + if (sectors > (1024 * 1024 / BDRV_SECTOR_SIZE)) { + error_setg(errp, "JSON file is too large"); + return NULL; + } + buf = g_malloc0(sectors << BDRV_SECTOR_BITS); + r = bdrv_read(file, 0, (uint8_t *)buf, sectors); + if (r) { + error_setg(errp, "Failed to read JSON file"); + g_free(buf); + return NULL; + } + return buf; +} + /* * Fills in default options for opening images and converts the legacy * filename/flags pair to option QDict entries. @@ -1002,8 +1028,28 @@ static int bdrv_fill_options(QDict **options, const char **pfilename, Error *local_err = NULL; /* Parse json: pseudo-protocol */ - if (filename && g_str_has_prefix(filename, "json:")) { - QDict *json_options = parse_json_filename(filename, &local_err); + if (filename && + (g_str_has_prefix(filename, "json:") || + g_str_has_prefix(filename, "json-file:"))) { + char *json_str; + QDict *json_options; + int ret; + + if (g_str_has_prefix(filename, "json-file:")) { + strstart(filename, "json-file:", &filename); + json_str = read_json_file(filename, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return -EIO; + } + } else { + ret = strstart(filename, "json:", &filename); + assert(ret); + + json_str = g_strdup(filename); + } + json_options = parse_json_filename(json_str, &local_err); + g_free(json_str); if (local_err) { error_propagate(errp, local_err); return -EINVAL; -- 2.4.3
