Skip to contentSkip to conversation

monitor usb: Fixes for encrypted images

PATCH In review Markus Armbruster 3 participants 8 messages Mar 12, 2015 – Mar 17, 2015

lore
Share & tools
lore
#2 lore raw

PATCH 2/5 monitor: Plug memory leak in monitor_read_bdrv_key_start()

In reply to Markus Armbruster
---
 monitor.c | 2 ++
 1 file changed, 2 insertions(+)
monitor.c +2 -0
index 29c930a..12d80a1 100644
@@ -5390,9 +5390,11 @@ int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
 
     if (monitor_ctrl_mode(mon)) {
         qerror_report_err(local_err);
+        error_free(local_err);
         return -1;
     }
 
+    error_free(local_err);
     monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
                    bdrv_get_encrypted_filename(bs));
 
Signature · 1 line
1.9.3
#5 lore raw

PATCH 5/5 usb/dev-storage: Avoid qerror_report_err() outside QMP handlers

In reply to Markus Armbruster

qerror_report_err() is a transitional interface to help with converting existing monitor commands to QMP. It should not be used elsewhere.

usb_msd_password_cb() is only called from within an HMP command handler. Replace by error_report_err().

---
 hw/usb/dev-storage.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/usb/dev-storage.c +1 -2
index f50bcb8..ae8d40d 100644
@@ -559,8 +559,7 @@ static void usb_msd_password_cb(void *opaque, int err)
     }
 
     if (local_err) {
-        qerror_report_err(local_err);
-        error_free(local_err);
+        error_report_err(local_err);
         qdev_unplug(&s->dev.qdev, NULL);
     }
 }
Signature · 1 line
1.9.3
#4 lore raw

PATCH 4/5 usb/dev-storage: Fix QMP device_add missing encryption key failure

In reply to Markus Armbruster

When the image is encrypted, QMP device_add creates the device, defers actually attaching it to when the key becomes available, then returns an error. This is wrong. device_add must either create the device and succeed, or do nothing and fail.

The bug is in usb_msd_realize_storage(). It posts an error with qerror_report_err(), and returns success. Device realization relies on the return value, and completes. The QMP monitor, however, relies on the posted error, and sends it in an error reply.

Reproducer:

    $ qemu-system-x86_64 -nodefaults -display none -usb -qmp stdio -drive if=none,id=foo,file=geheim.qcow2
    {"QMP": {"version": {"qemu": {"micro": 50, "minor": 2, "major": 2}, "package": ""}, "capabilities": []}}
    { "execute": "qmp_capabilities" }
    {"return": {}}
    { "execute": "device_add", "arguments": { "driver": "usb-storage", "id": "bar", "drive": "foo" } }
    {"error": {"class": "DeviceEncrypted", "desc": "'foo' (geheim.qcow2) is encrypted"}}

Even though we got an error back, the device got created just fine. To demonstrate, let's unplug it again:

    {"execute":"device_del","arguments": { "id": "bar" } }
    {"timestamp": {"seconds": 1426003440, "microseconds": 237181}, "event": "DEVICE_DELETED", "data": {"path": "/machine/peripheral/bar/bar.0/legacy[0]"}}
    {"timestamp": {"seconds": 1426003440, "microseconds": 238231}, "event": "DEVICE_DELETED", "data": {"device": "bar", "path": "/machine/peripheral/bar"}}
    {"return": {}}

Fix by making usb_msd_realize_storage() fail properly.

---
 hw/usb/dev-storage.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)
hw/usb/dev-storage.c +17 -19
index f47c856..f50bcb8 100644
@@ -610,6 +610,23 @@ static void usb_msd_realize_storage(USBDevice *dev, Error **errp)
         return;
     }
 
+    bdrv_add_key(blk_bs(blk), NULL, &err);
+    if (err) {
+        if (monitor_cur_is_qmp()) {
+            error_propagate(errp, err);
+            return;
+        }
+        error_free(err);
+        err = NULL;
+        if (cur_mon) {
+            monitor_read_bdrv_key_start(cur_mon, blk_bs(blk),
+                                        usb_msd_password_cb, s);
+            s->dev.auto_attach = 0;
+        } else {
+            autostart = 0;
+        }
+    }
+
     blkconf_serial(&s->conf, &dev->serial);
     blkconf_blocksizes(&s->conf);
 
@@ -638,25 +655,6 @@ static void usb_msd_realize_storage(USBDevice *dev, Error **errp)
     }
     usb_msd_handle_reset(dev);
     s->scsi_dev = scsi_dev;
-
-    if (bdrv_key_required(blk_bs(blk))) {
-        if (cur_mon) {
-            bdrv_add_key(blk_bs(blk), NULL, &err);
-            if (!err) {
-                usb_msd_password_cb(s, 0);
-            } else if (monitor_cur_is_qmp()) {
-                qerror_report_err(err);
-                error_free(err);
-            } else {
-                error_free(err);
-                monitor_read_bdrv_key_start(cur_mon, blk_bs(blk),
-                                            usb_msd_password_cb, s);
-            }
-            s->dev.auto_attach = 0;
-        } else {
-            autostart = 0;
-        }
-    }
 }
 
 static void usb_msd_realize_bot(USBDevice *dev, Error **errp)
Signature · 1 line
1.9.3
#0 lore raw

cover monitor usb: Fixes for encrypted images

Fixes for encrypted images is of course a fool's errand. My actual motivation is killing a few more qerror_report() calls.

Markus Armbruster (5):
monitor: Drop dead QMP check from monitor_read_password()
monitor: Plug memory leak in monitor_read_bdrv_key_start() monitor usb: Inline monitor_read_bdrv_key_start()'s first part usb/dev-storage: Fix QMP device_add missing encryption key failure usb/dev-storage: Avoid qerror_report_err() outside QMP handlers

 hw/usb/dev-storage.c | 30 ++++++++++++++++++------------
 monitor.c            | 32 ++++++++++++--------------------
 2 files changed, 30 insertions(+), 32 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/5 monitor: Drop dead QMP check from monitor_read_password()

Function is only called in HMP context since commit 333a96e "qapi:
Convert change".

---
 monitor.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
monitor.c +1 -4
index c86a89e..29c930a 100644
@@ -265,10 +265,7 @@ void monitor_read_command(Monitor *mon, int show_prompt)
 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
                           void *opaque)
 {
-    if (monitor_ctrl_mode(mon)) {
-        qerror_report(QERR_MISSING_PARAMETER, "password");
-        return -EINVAL;
-    } else if (mon->rs) {
+    if (mon->rs) {
         readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
         /* prompt is printed on return from the command handler */
         return 0;
Signature · 1 line
1.9.3
#3 lore raw

PATCH 3/5 monitor usb: Inline monitor_read_bdrv_key_start()'s first part

In reply to Markus Armbruster

monitor_read_bdrv_key_start() does several things:

1. If no key is needed, call completion_cb() and succeed

2. If we're in QMP context, call qerror_report_err() and fail

3. Start reading the key in the monitor.

This is two things too many. Inline 1. and 2. into its callers monitor_read_block_device_key() and usb_msd_realize_storage().

Since monitor_read_block_device_key() only ever runs in HMP context, drop 2. there.

The next commit will clean up the result in usb_msd_realize_storage().

---
 hw/usb/dev-storage.c | 13 +++++++++++--
 monitor.c            | 29 +++++++++++------------------
 2 files changed, 22 insertions(+), 20 deletions(-)
hw/usb/dev-storage.c +11 -2
index dacefd7..f47c856 100644
@@ -641,8 +641,17 @@ static void usb_msd_realize_storage(USBDevice *dev, Error **errp)
 
     if (bdrv_key_required(blk_bs(blk))) {
         if (cur_mon) {
-            monitor_read_bdrv_key_start(cur_mon, blk_bs(blk),
-                                        usb_msd_password_cb, s);
+            bdrv_add_key(blk_bs(blk), NULL, &err);
+            if (!err) {
+                usb_msd_password_cb(s, 0);
+            } else if (monitor_cur_is_qmp()) {
+                qerror_report_err(err);
+                error_free(err);
+            } else {
+                error_free(err);
+                monitor_read_bdrv_key_start(cur_mon, blk_bs(blk),
+                                            usb_msd_password_cb, s);
+            }
             s->dev.auto_attach = 0;
         } else {
             autostart = 0;
monitor.c +11 -18
index 12d80a1..85223cf 100644
@@ -5376,25 +5376,8 @@ int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
                                 BlockCompletionFunc *completion_cb,
                                 void *opaque)
 {
-    Error *local_err = NULL;
     int err;
 
-    bdrv_add_key(bs, NULL, &local_err);
-    if (!local_err) {
-        if (completion_cb)
-            completion_cb(opaque, 0);
-        return 0;
-    }
-
-    /* Need a key for @bs */
-
-    if (monitor_ctrl_mode(mon)) {
-        qerror_report_err(local_err);
-        error_free(local_err);
-        return -1;
-    }
-
-    error_free(local_err);
     monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
                    bdrv_get_encrypted_filename(bs));
 
@@ -5413,6 +5396,7 @@ int monitor_read_block_device_key(Monitor *mon, const char *device,
                                   BlockCompletionFunc *completion_cb,
                                   void *opaque)
 {
+    Error *err = NULL;
     BlockDriverState *bs;
 
     bs = bdrv_find(device);
@@ -5421,7 +5405,16 @@ int monitor_read_block_device_key(Monitor *mon, const char *device,
         return -1;
     }
 
-    return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
+    bdrv_add_key(bs, NULL, &err);
+    if (err) {
+        error_free(err);
+        return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
+    }
+
+    if (completion_cb) {
+        completion_cb(opaque, 0);
+    }
+    return 0;
 }
 
 QemuOptsList qemu_mon_opts = {
Signature · 1 line
1.9.3
Reviewed-by on the v1 series
#6 lore raw
In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

Fixes for encrypted images is of course a fool's errand. My actual motivation is killing a few more qerror_report() calls.

Markus Armbruster (5):
monitor: Drop dead QMP check from monitor_read_password()
monitor: Plug memory leak in monitor_read_bdrv_key_start() monitor usb: Inline monitor_read_bdrv_key_start()'s first part usb/dev-storage: Fix QMP device_add missing encryption key failure usb/dev-storage: Avoid qerror_report_err() outside QMP handlers

Quoted 5 lines
 hw/usb/dev-storage.c | 30 ++++++++++++++++++------------
 monitor.c            | 32 ++++++++++++--------------------
 2 files changed, 30 insertions(+), 32 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#7 lore raw
In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

Fixes for encrypted images is of course a fool's errand. My actual motivation is killing a few more qerror_report() calls.

Markus Armbruster (5):
monitor: Drop dead QMP check from monitor_read_password()
monitor: Plug memory leak in monitor_read_bdrv_key_start() monitor usb: Inline monitor_read_bdrv_key_start()'s first part usb/dev-storage: Fix QMP device_add missing encryption key failure usb/dev-storage: Avoid qerror_report_err() outside QMP handlers

Added to usb patch queue.

thanks,
Gerd