From patchwork Tue Nov 3 07:01:17 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 539251 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id F3AB8140311 for ; Tue, 3 Nov 2015 18:02:01 +1100 (AEDT) Received: from localhost ([::1]:46071 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZtVbT-0003MU-Vq for incoming@patchwork.ozlabs.org; Tue, 03 Nov 2015 02:02:00 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZtVb2-0002a6-Ub for qemu-devel@nongnu.org; Tue, 03 Nov 2015 02:01:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZtVb1-00063X-VN for qemu-devel@nongnu.org; Tue, 03 Nov 2015 02:01:32 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47283) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZtVay-000624-EP; Tue, 03 Nov 2015 02:01:28 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 1342236B6F0; Tue, 3 Nov 2015 07:01:28 +0000 (UTC) Received: from fam-t430.redhat.com (vpn1-5-146.pek2.redhat.com [10.72.5.146]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tA371I5g017509; Tue, 3 Nov 2015 02:01:24 -0500 From: Fam Zheng To: qemu-devel@nongnu.org Date: Tue, 3 Nov 2015 15:01:17 +0800 Message-Id: <1446534078-11172-2-git-send-email-famz@redhat.com> In-Reply-To: <1446534078-11172-1-git-send-email-famz@redhat.com> References: <1446534078-11172-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , qemu-block@nongnu.org, Max Reitz Subject: [Qemu-devel] [PATCH 1/2] block: Add "json-file:" pseudo protocol X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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;