systemd-run: properly escape arguments

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
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2013-08-31 20:28:09 +02:00
parent a012ab5293
commit a6fde35332
Notes: Lennart Poettering 2013-10-04 17:11:46 +02:00
Backport: bugfix
4 changed files with 125 additions and 48 deletions

View 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);
a = strv_join_quoted(c->argv);
if (!a)
return -ENOMEM;
fputc('@', f);
fputs(c->path, f);
STRV_FOREACH(a, c->argv) {
fputc(' ', f);
fputs(*a, f);
}
fputc('\n', f);
fprintf(f, "ExecStart=%s@%s %s\n",
c->ignore ? "-" : "",
c->path,
a);
}
fflush(f);

View 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;

View 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);

View 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,
};
assert_se(strv_find((char **)input_table, "three"));
assert_se(!strv_find((char **)input_table, "four"));
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 \
"\" \" " \
"\"\\' \\'\" " \
"\"\\\" \" " \
"\" \\\"\" " \
"\" \\\\\\\\ \""
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();