]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Fix start api call to split quoted strings in execute or init command. 4336/head
authorScott Moser <smoser@brickies.net>
Tue, 22 Aug 2023 18:07:36 +0000 (14:07 -0400)
committerScott Moser <smoser@brickies.net>
Tue, 22 Aug 2023 20:50:45 +0000 (16:50 -0400)
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 <smoser@brickies.net>
src/lxc/lxccontainer.c

index 44be044ee659bb3b66aeb03a0e91d7ae39d2cb6b..9805b7a3f368c7d87f5bf8e77fa3c14db6c40837 100644 (file)
@@ -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. */