Merge pull request #32210 from YHNdnzj/execcommand-done

core: modernize ExecCommand parsing and fix one memory leak
This commit is contained in:
Daan De Meyer 2024-04-11 09:26:37 +02:00 committed by GitHub
commit 8e7c157387
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 90 additions and 98 deletions

View file

@ -1306,18 +1306,24 @@ int bus_set_transient_exec_command(
sd_bus_message *message,
UnitWriteFlags flags,
sd_bus_error *error) {
bool is_ex_prop = endswith(name, "Ex");
unsigned n = 0;
const char *ex_prop = endswith(ASSERT_PTR(name), "Ex");
size_t n = 0;
int r;
/* Drop Ex from the written setting. E.g. ExecStart=, not ExecStartEx=. */
const char *written_name = is_ex_prop ? strndupa_safe(name, strlen(name) - 2) : name;
assert(u);
assert(exec_command);
assert(message);
assert(error);
r = sd_bus_message_enter_container(message, 'a', is_ex_prop ? "(sasas)" : "(sasb)");
/* Drop Ex from the written setting. E.g. ExecStart=, not ExecStartEx=. */
const char *written_name = ex_prop ? strndupa_safe(name, ex_prop - name) : name;
r = sd_bus_message_enter_container(message, 'a', ex_prop ? "(sasas)" : "(sasb)");
if (r < 0)
return r;
while ((r = sd_bus_message_enter_container(message, 'r', is_ex_prop ? "sasas" : "sasb")) > 0) {
while ((r = sd_bus_message_enter_container(message, 'r', ex_prop ? "sasas" : "sasb")) > 0) {
_cleanup_strv_free_ char **argv = NULL, **ex_opts = NULL;
const char *path;
int b;
@ -1339,7 +1345,7 @@ int bus_set_transient_exec_command(
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"\"%s\" argv cannot be empty", name);
r = is_ex_prop ? sd_bus_message_read_strv(message, &ex_opts) : sd_bus_message_read(message, "b", &b);
r = ex_prop ? sd_bus_message_read_strv(message, &ex_opts) : sd_bus_message_read(message, "b", &b);
if (r < 0)
return r;
@ -1348,29 +1354,28 @@ int bus_set_transient_exec_command(
return r;
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
ExecCommand *c;
_cleanup_(exec_command_freep) ExecCommand *c = NULL;
c = new0(ExecCommand, 1);
c = new(ExecCommand, 1);
if (!c)
return -ENOMEM;
c->path = strdup(path);
if (!c->path) {
free(c);
return -ENOMEM;
}
*c = (ExecCommand) {
.argv = TAKE_PTR(argv),
};
c->argv = TAKE_PTR(argv);
r = path_simplify_alloc(path, &c->path);
if (r < 0)
return r;
if (is_ex_prop) {
if (ex_prop) {
r = exec_command_flags_from_strv(ex_opts, &c->flags);
if (r < 0)
return r;
} else
c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0;
} else if (b)
c->flags |= EXEC_COMMAND_IGNORE_FAILURE;
path_simplify(c->path);
exec_command_append_list(exec_command, c);
exec_command_append_list(exec_command, TAKE_PTR(c));
}
n++;

View file

@ -672,13 +672,19 @@ void exec_command_done_array(ExecCommand *c, size_t n) {
exec_command_done(i);
}
ExecCommand* exec_command_free(ExecCommand *c) {
if (!c)
return NULL;
exec_command_done(c);
return mfree(c);
}
ExecCommand* exec_command_free_list(ExecCommand *c) {
ExecCommand *i;
while ((i = LIST_POP(command, c))) {
exec_command_done(i);
free(i);
}
while ((i = LIST_POP(command, c)))
exec_command_free(i);
return NULL;
}

View file

@ -487,10 +487,13 @@ int exec_spawn(Unit *unit,
void exec_command_done(ExecCommand *c);
void exec_command_done_array(ExecCommand *c, size_t n);
ExecCommand* exec_command_free(ExecCommand *c);
DEFINE_TRIVIAL_CLEANUP_FUNC(ExecCommand*, exec_command_free);
ExecCommand* exec_command_free_list(ExecCommand *c);
void exec_command_free_array(ExecCommand **c, size_t n);
void exec_command_reset_status_array(ExecCommand *c, size_t n);
void exec_command_reset_status_list_array(ExecCommand **c, size_t n);
void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
void exec_command_append_list(ExecCommand **l, ExecCommand *e);
int exec_command_set(ExecCommand *c, const char *path, ...) _sentinel_;

View file

@ -856,9 +856,7 @@ int config_parse_exec(
void *userdata) {
ExecCommand **e = ASSERT_PTR(data);
const Unit *u = userdata;
const char *p;
bool semicolon;
const Unit *u = ASSERT_PTR(userdata);
int r;
assert(filename);
@ -873,15 +871,11 @@ int config_parse_exec(
return 0;
}
p = rvalue;
const char *p = rvalue;
bool semicolon;
do {
_cleanup_free_ char *path = NULL, *firstword = NULL;
ExecCommandFlags flags = 0;
bool ignore = false, separate_argv0 = false;
_cleanup_free_ ExecCommand *nce = NULL;
_cleanup_strv_free_ char **n = NULL;
size_t nlen = 0;
const char *f;
semicolon = false;
@ -895,25 +889,30 @@ int config_parse_exec(
continue;
}
f = firstword;
for (;;) {
/* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't
* exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of
* argv[0]; if it's prefixed with :, we will not do environment variable substitution;
* if it's prefixed with +, it will be run with full privileges and no sandboxing; if
* it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if
* it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient
* capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most
* other sandboxing, with some special exceptions for changing UID.
*
* The idea is that '!!' may be used to write services that can take benefit of systemd's
* UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to
* privilege dropping within the daemon if the kernel does not offer that. */
const char *f = firstword;
bool ignore, separate_argv0 = false;
ExecCommandFlags flags = 0;
if (*f == '-' && !(flags & EXEC_COMMAND_IGNORE_FAILURE)) {
for (;; f++) {
/* We accept an absolute path as first argument. Valid prefixes and their effect:
*
* "-": Ignore if the path doesn't exist
* "@": Allow overridding argv[0] (supplied as a separate argument)
* ":": Disable environment variable substitution
* "+": Run with full privileges and no sandboxing
* "!": Apply sandboxing except for user/group credentials
* "!!": Apply user/group credentials if the kernel supports ambient capabilities -
* if it doesn't we don't apply the credentials themselves, but do apply
* most other sandboxing, with some special exceptions for changing UID.
*
* The idea is that '!!' may be used to write services that can take benefit of
* systemd's UID/GID dropping if the kernel supports ambient creds, but provide
* an automatic fallback to privilege dropping within the daemon if the kernel
* does not offer that. */
if (*f == '-' && !(flags & EXEC_COMMAND_IGNORE_FAILURE))
flags |= EXEC_COMMAND_IGNORE_FAILURE;
ignore = true;
} else if (*f == '@' && !separate_argv0)
else if (*f == '@' && !separate_argv0)
separate_argv0 = true;
else if (*f == ':' && !(flags & EXEC_COMMAND_NO_ENV_EXPAND))
flags |= EXEC_COMMAND_NO_ENV_EXPAND;
@ -926,9 +925,10 @@ int config_parse_exec(
flags |= EXEC_COMMAND_AMBIENT_MAGIC;
} else
break;
f++;
}
ignore = FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE);
r = unit_path_printf(u, f, &path);
if (r < 0) {
log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
@ -938,19 +938,18 @@ int config_parse_exec(
}
if (isempty(path)) {
/* First word is either "-" or "@" with no command. */
log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
"Empty path in command line%s: '%s'",
"Empty path in command line%s: %s",
ignore ? ", ignoring" : "", rvalue);
return ignore ? 0 : -ENOEXEC;
}
if (!string_is_safe(path)) {
log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
"Executable name contains special characters%s: %s",
"Executable path contains special characters%s: %s",
ignore ? ", ignoring" : "", path);
return ignore ? 0 : -ENOEXEC;
}
if (endswith(path, "/")) {
if (path_implies_directory(path)) {
log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
"Executable path specifies a directory%s: %s",
ignore ? ", ignoring" : "", path);
@ -964,92 +963,71 @@ int config_parse_exec(
return ignore ? 0 : -ENOEXEC;
}
if (!separate_argv0) {
char *w = NULL;
_cleanup_strv_free_ char **args = NULL;
if (!GREEDY_REALLOC0(n, nlen + 2))
if (!separate_argv0)
if (strv_extend(&args, path) < 0)
return log_oom();
w = strdup(path);
if (!w)
return log_oom();
n[nlen++] = w;
n[nlen] = NULL;
}
path_simplify(path);
while (!isempty(p)) {
_cleanup_free_ char *word = NULL, *resolved = NULL;
/* Check explicitly for an unquoted semicolon as
* command separator token. */
/* Check explicitly for an unquoted semicolon as command separator token. */
if (p[0] == ';' && (!p[1] || strchr(WHITESPACE, p[1]))) {
p++;
p += strspn(p, WHITESPACE);
p = skip_leading_chars(p, /* bad = */ NULL);
semicolon = true;
break;
}
/* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc.
* extract_first_word() would return the same for all of those. */
* extract_first_word() would return the same for all of those. */
if (p[0] == '\\' && p[1] == ';' && (!p[2] || strchr(WHITESPACE, p[2]))) {
char *w;
p += 2;
p += strspn(p, WHITESPACE);
p = skip_leading_chars(p, /* bad = */ NULL);
if (!GREEDY_REALLOC0(n, nlen + 2))
if (strv_extend(&args, ";") < 0)
return log_oom();
w = strdup(";");
if (!w)
return log_oom();
n[nlen++] = w;
n[nlen] = NULL;
continue;
}
r = extract_first_word_and_warn(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
if (r == 0)
break;
if (r < 0)
return ignore ? 0 : -ENOEXEC;
if (r == 0)
break;
r = unit_full_printf(u, word, &resolved);
if (r < 0) {
log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
"Failed to resolve unit specifiers in %s%s: %m",
"Failed to resolve unit specifiers in '%s'%s: %m",
word, ignore ? ", ignoring" : "");
return ignore ? 0 : -ENOEXEC;
}
if (!GREEDY_REALLOC(n, nlen + 2))
if (strv_consume(&args, TAKE_PTR(resolved)) < 0)
return log_oom();
n[nlen++] = TAKE_PTR(resolved);
n[nlen] = NULL;
}
if (!n || !n[0]) {
if (strv_isempty(args)) {
log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
"Empty executable name or zeroeth argument%s: %s",
ignore ? ", ignoring" : "", rvalue);
return ignore ? 0 : -ENOEXEC;
}
nce = new0(ExecCommand, 1);
if (!nce)
ExecCommand *nec = new(ExecCommand, 1);
if (!nec)
return log_oom();
nce->argv = TAKE_PTR(n);
nce->path = TAKE_PTR(path);
nce->flags = flags;
*nec = (ExecCommand) {
.path = path_simplify(TAKE_PTR(path)),
.argv = TAKE_PTR(args),
.flags = flags,
};
exec_command_append_list(e, nce);
/* Do not _cleanup_free_ these. */
nce = NULL;
exec_command_append_list(e, nec);
rvalue = p;
} while (semicolon);