qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	qemu-block@nongnu.org, Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH 1/2] block: Add "json-file:" pseudo protocol
Date: Tue,  3 Nov 2015 15:01:17 +0800	[thread overview]
Message-ID: <1446534078-11172-2-git-send-email-famz@redhat.com> (raw)
In-Reply-To: <1446534078-11172-1-git-send-email-famz@redhat.com>

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 <famz@redhat.com>
---
 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

  reply	other threads:[~2015-11-03  7:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-03  7:01 [Qemu-devel] [PATCH 0/2] block: Introduce "json-file:" protocol Fam Zheng
2015-11-03  7:01 ` Fam Zheng [this message]
2015-11-03  7:01 ` [Qemu-devel] [PATCH 2/2] iotests: Add tests for "json-file:" pseudo protocol Fam Zheng
2015-11-03  7:35 ` [Qemu-devel] [PATCH 0/2] block: Introduce "json-file:" protocol Daniel P. Berrange
2015-11-03  7:56   ` Fam Zheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1446534078-11172-2-git-send-email-famz@redhat.com \
    --to=famz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).