[PATCH] qemu: Fix escape_monitor(escape_shell(command))

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Suspending a VM which contains shell meta characters doesn't work with
libvirt-0.8.7:
/var/log/libvirt/qemu/andreas_231-ne\ doch\ nicht.log:
  sh: -c: line 0: syntax error near unexpected token `doch'
  sh: -c: line 0: `cat | { dd bs=4096 seek=1 if=/dev/null && dd bs=1048576; }

Although target="andreas_231-ne doch nicht" contains shell meta
characters (here: blanks), they are not properly escaped by
src/qemu/qemu_monitor_{json,text}.c#qemuMonitor{JSON,Text}MigrateToFile()

First, the filename needs to be properly escaped for the shell, than
this command line has to be properly escaped for qemu again.

For this to work, remove the old qemuMonitorEscapeArg() wrapper, rename
qemuMonitorEscape() to it removing the handling for shell=TRUE, and
implement a new qemuMonitorEscapeShell() returning strings using single
quotes.

Using double quotes or escaping special shell characters with backslashes
would also be possible, but the set of special characters heavily
depends on the concrete shell (dsh, bash, zsh) and its setting (history
expansion, interactive use, ...)

Signed-off-by: Philipp Hahn <hahn@xxxxxxxxxxxxx>
---
 src/qemu/qemu_monitor.c |   65 +++++++++++++++++++++++++++-------------------
 1 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index fdb6b79..e3d0a0f 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -79,7 +79,7 @@ VIR_ENUM_IMPL(qemuMonitorMigrationStatus,
               QEMU_MONITOR_MIGRATION_STATUS_LAST,
               "inactive", "active", "completed", "failed", "cancelled")
 
-static char *qemuMonitorEscape(const char *in, int shell)
+char *qemuMonitorEscapeArg(const char *in)
 {
     int len = 0;
     int i, j;
@@ -87,9 +87,6 @@ static char *qemuMonitorEscape(const char *in, int shell)
 
     /* To pass through the QEMU monitor, we need to use escape
        sequences: \r, \n, \", \\
-
-       To pass through both QEMU + the shell, we need to escape
-       the single character ' as the five characters '\\''
     */
 
     for (i = 0; in[i] != '\0'; i++) {
@@ -100,12 +97,6 @@ static char *qemuMonitorEscape(const char *in, int shell)
         case '\\':
             len += 2;
             break;
-        case '\'':
-            if (shell)
-                len += 5;
-            else
-                len += 1;
-            break;
         default:
             len += 1;
             break;
@@ -130,17 +121,6 @@ static char *qemuMonitorEscape(const char *in, int shell)
             out[j++] = '\\';
             out[j++] = in[i];
             break;
-        case '\'':
-            if (shell) {
-                out[j++] = '\'';
-                out[j++] = '\\';
-                out[j++] = '\\';
-                out[j++] = '\'';
-                out[j++] = '\'';
-            } else {
-                out[j++] = in[i];
-            }
-            break;
         default:
             out[j++] = in[i];
             break;
@@ -151,14 +131,45 @@ static char *qemuMonitorEscape(const char *in, int shell)
     return out;
 }
 
-char *qemuMonitorEscapeArg(const char *in)
-{
-    return qemuMonitorEscape(in, 0);
-}
-
 char *qemuMonitorEscapeShell(const char *in)
 {
-    return qemuMonitorEscape(in, 1);
+    int len = 2; /* leading and trailing single quote */
+    int i, j;
+    char *out;
+
+    for (i = 0; in[i] != '\0'; i++) {
+        switch(in[i]) {
+        case '\'':
+            len += 4; /* '\'' */
+            break;
+        default:
+            len += 1;
+            break;
+        }
+    }
+
+    if (VIR_ALLOC_N(out, len + 1) < 0)
+        return NULL;
+
+    j = 0;
+    out[j++] = '\'';
+    for (i = 0; in[i] != '\0'; i++) {
+        switch(in[i]) {
+        case '\'':
+            out[j++] = '\'';
+            out[j++] = '\\';
+            out[j++] = '\'';
+            out[j++] = '\'';
+            break;
+        default:
+            out[j++] = in[i];
+            break;
+        }
+    }
+    out[j++] = '\'';
+    out[j] = '\0';
+
+    return out;
 }
 
 
--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]