From 3f45443fd03e7ea82a98691888802cdf1d011bd1 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 22 Aug 2023 14:07:36 -0400 Subject: [PATCH] Fix start api call to split quoted strings in execute or init command. If a user of the container.start api call provided NULL for the argv argument, then lxc would load either 'lxc.execute.cmd' or 'lxc.init.cmd' configuration items as the command. Given a config like: lxc.execute.cmd = /usr/bin/touch "file one" "file 2" lxc would just split the string on spaces and end up executing array: ['touch', 'file"', 'one"', '"file', '2"'] This differs from the experience with the `lxc-start` command which would use lxc_string_split_quoted and execute: ['touch', 'file one', 'file 2'] Note that as described in lxc_string_split_quoted, commands that include nested quotes and possibly other characters are still a problem. In those cases, the caller of 'start' can provide an argv array. Signed-off-by: Scott Moser --- src/lxc/lxccontainer.c | 53 ++++-------------------------------------- 1 file changed, 5 insertions(+), 48 deletions(-) diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 44be044ee..9805b7a3f 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -768,51 +768,6 @@ static bool am_single_threaded(void) return count == 1; } -static void push_arg(char ***argp, char *arg, int *nargs) -{ - char *copy; - char **argv; - - copy = must_copy_string(arg); - - do { - argv = realloc(*argp, (*nargs + 2) * sizeof(char *)); - } while (!argv); - - *argp = argv; - argv[*nargs] = copy; - (*nargs)++; - argv[*nargs] = NULL; -} - -static char **split_init_cmd(const char *incmd) -{ - __do_free char *copy = NULL; - char *p; - char **argv; - int nargs = 0; - - if (!incmd) - return NULL; - - copy = must_copy_string(incmd); - - do { - argv = malloc(sizeof(char *)); - } while (!argv); - - argv[0] = NULL; - lxc_iterate_parts (p, copy, " ") - push_arg(&argv, p, &nargs); - - if (nargs == 0) { - free(argv); - return NULL; - } - - return argv; -} - static void free_init_cmd(char **argv) { int i = 0; @@ -934,10 +889,12 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a return false; if (!argv) { + char *cfgcmd = conf->init_cmd; if (useinit && conf->execute_cmd) - argv = init_cmd = split_init_cmd(conf->execute_cmd); - else - argv = init_cmd = split_init_cmd(conf->init_cmd); + cfgcmd = conf->execute_cmd; + + if (cfgcmd != NULL) + argv = init_cmd = lxc_string_split_quoted(cfgcmd); } /* ... otherwise use default_args. */ -- 2.47.2