chiark / gitweb /
systemd-run: properly escape arguments
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 31 Aug 2013 18:28:09 +0000 (20:28 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 9 Sep 2013 17:49:29 +0000 (13:49 -0400)
Spaces, quotes, and such, were not properly escaped. We should
write them like we read them.

https://bugs.freedesktop.org/show_bug.cgi?id=67971

src/core/dbus-service.c
src/shared/strv.c
src/shared/strv.h
src/test/test-strv.c

index 85b13f01ef924b93c8673d14c702b8f39e236d8c..696c4462fe257010dcd394afd4bea39e7060bdb6 100644 (file)
@@ -273,21 +273,16 @@ static int bus_service_set_transient_property(
                         fputs("ExecStart=\n", f);
 
                         LIST_FOREACH(command, c, s->exec_command[SERVICE_EXEC_START]) {
-                                char **a;
-                                fputs("ExecStart=", f);
+                                _cleanup_free_ char *a;
 
-                                if (c->ignore)
-                                        fputc('-', f);
-
-                                fputc('@', f);
-                                fputs(c->path, f);
-
-                                STRV_FOREACH(a, c->argv) {
-                                        fputc(' ', f);
-                                        fputs(*a, f);
-                                }
+                                a = strv_join_quoted(c->argv);
+                                if (!a)
+                                        return -ENOMEM;
 
-                                fputc('\n', f);
+                                fprintf(f, "ExecStart=%s@%s %s\n",
+                                        c->ignore ? "-" : "",
+                                        c->path,
+                                        a);
                         }
 
                         fflush(f);
index 3e7778d61ca663eab8c2f23f9d024ef14ffa842c..2df478f30bbce900cb3e2b0673b61a1e4eada0c2 100644 (file)
@@ -356,6 +356,43 @@ char *strv_join(char **l, const char *separator) {
         return r;
 }
 
+char *strv_join_quoted(char **l) {
+        char *buf = NULL;
+        char **s;
+        size_t allocated = 0, len = 0;
+
+        STRV_FOREACH(s, l) {
+                /* assuming here that escaped string cannot be more
+                 * than twice as long, and reserving space for the
+                 * separator and quotes.
+                 */
+                _cleanup_free_ char *esc = NULL;
+                size_t needed;
+
+                if (!GREEDY_REALLOC(buf, allocated,
+                                    len + strlen(*s) * 2 + 3))
+                        goto oom;
+
+                esc = cescape(*s);
+                if (!esc)
+                        goto oom;
+
+                needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
+                                  len > 0 ? " " : "", esc);
+                assert(needed < allocated - len);
+                len += needed;
+        }
+
+        if (!buf)
+                buf = malloc0(1);
+
+        return buf;
+
+ oom:
+        free(buf);
+        return NULL;
+}
+
 char **strv_append(char **l, const char *s) {
         char **r, **k;
 
index 4ade827a4231a56dcdb032c998132355c904e475..4e80ea6d89ba373f43f3e30f2836f0de33b08edd 100644 (file)
@@ -68,6 +68,7 @@ char **strv_split_quoted(const char *s);
 char **strv_split_newlines(const char *s);
 
 char *strv_join(char **l, const char *separator);
+char *strv_join_quoted(char **l);
 
 char **strv_parse_nulstr(const char *s, size_t l);
 char **strv_split_nulstr(const char *s);
index 074e1bb3d411c399a5401b2dd7d260ab9f2891fe..25bee22dfe9c4f91e0d0c92a766f585f9c84fc0e 100644 (file)
@@ -42,50 +42,68 @@ static void test_specifier_printf(void) {
         assert_se(streq(w, "xxx a=AAAA b=BBBB yyy"));
 }
 
-static void test_strv_find(void) {
-        const char * const input_table[] = {
-                "one",
-                "two",
-                "three",
-                NULL
-        };
+static const char* const input_table_multiple[] = {
+        "one",
+        "two",
+        "three",
+        NULL,
+};
+
+static const char* const input_table_one[] = {
+        "one",
+        NULL,
+};
+
+static const char* const input_table_none[] = {
+        NULL,
+};
+
+static const char* const input_table_quotes[] = {
+        "\"",
+        "'",
+        "\"\"",
+        "\\",
+        "\\\\",
+        NULL,
+};
+#define QUOTES_STRING                            \
+        "\"\\\"\" "                              \
+        "\"\\\'\" "                              \
+        "\"\\\"\\\"\" "                          \
+        "\"\\\\\" "                              \
+        "\"\\\\\\\\\""
+
+static const char * const input_table_spaces[] = {
+        " ",
+        "' '",
+        "\" ",
+        " \"",
+        " \\\\ ",
+        NULL,
+};
+#define SPACES_STRING                           \
+        "\" \" "                                \
+        "\"\\' \\'\" "                          \
+        "\"\\\" \" "                            \
+        "\" \\\"\" "                            \
+        "\" \\\\\\\\ \""
 
-        assert_se(strv_find((char **)input_table, "three"));
-        assert_se(!strv_find((char **)input_table, "four"));
+static void test_strv_find(void) {
+        assert_se(strv_find((char **)input_table_multiple, "three"));
+        assert_se(!strv_find((char **)input_table_multiple, "four"));
 }
 
 static void test_strv_find_prefix(void) {
-        const char * const input_table[] = {
-                "one",
-                "two",
-                "three",
-                NULL
-        };
-
-        assert_se(strv_find_prefix((char **)input_table, "o"));
-        assert_se(strv_find_prefix((char **)input_table, "one"));
-        assert_se(strv_find_prefix((char **)input_table, ""));
-        assert_se(!strv_find_prefix((char **)input_table, "xxx"));
-        assert_se(!strv_find_prefix((char **)input_table, "onee"));
+        assert_se(strv_find_prefix((char **)input_table_multiple, "o"));
+        assert_se(strv_find_prefix((char **)input_table_multiple, "one"));
+        assert_se(strv_find_prefix((char **)input_table_multiple, ""));
+        assert_se(!strv_find_prefix((char **)input_table_multiple, "xxx"));
+        assert_se(!strv_find_prefix((char **)input_table_multiple, "onee"));
 }
 
 static void test_strv_join(void) {
         _cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL;
 
-        const char * const input_table_multiple[] = {
-                "one",
-                "two",
-                "three",
-                NULL
-        };
-        const char * const input_table_one[] = {
-                "one",
-                NULL
-        };
-        const char * const input_table_none[] = {
-                NULL
-        };
-
         p = strv_join((char **)input_table_multiple, ", ");
         assert_se(p);
         assert_se(streq(p, "one, two, three"));
@@ -107,6 +125,25 @@ static void test_strv_join(void) {
         assert_se(streq(t, ""));
 }
 
+static void test_strv_quote_unquote(const char* const *split, const char *quoted) {
+        _cleanup_free_ char *p;
+        _cleanup_strv_free_ char **s;
+        char **t;
+
+        p = strv_join_quoted((char **)split);
+        printf("-%s- --- -%s-\n", p, quoted); /* fprintf deals with NULL, puts does not */
+        assert_se(p);
+        assert_se(streq(p, quoted));
+
+        s = strv_split_quoted(quoted);
+        assert_se(s);
+        STRV_FOREACH(t, s) {
+                assert_se(*t);
+                assert_se(streq(*t, *split));
+                split++;
+        }
+}
+
 static void test_strv_split_nulstr(void) {
         _cleanup_strv_free_ char **l = NULL;
         const char nulstr[] = "str0\0str1\0str2\0str3\0";
@@ -253,6 +290,13 @@ int main(int argc, char *argv[]) {
         test_strv_find();
         test_strv_find_prefix();
         test_strv_join();
+
+        test_strv_quote_unquote(input_table_multiple, "\"one\" \"two\" \"three\"");
+        test_strv_quote_unquote(input_table_one, "\"one\"");
+        test_strv_quote_unquote(input_table_none, "");
+        test_strv_quote_unquote(input_table_quotes, QUOTES_STRING);
+        test_strv_quote_unquote(input_table_spaces, SPACES_STRING);
+
         test_strv_split_nulstr();
         test_strv_parse_nulstr();
         test_strv_overlap();