From 327cce76ad2f435e84b7d53e64a1e0a5edbd568b Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Fri, 16 Feb 2018 22:04:35 +0100 Subject: [PATCH] conf: fix run_script_argv() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Make sure that we allocate the buffer **after** we determined how much space we need in total. This fixes a SIGBUS/SIGSEGV Stéphane reported on aarch64 and armf. Reported-by: Stéphane Graber Signed-off-by: Christian Brauner --- src/lxc/conf.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/lxc/conf.c b/src/lxc/conf.c index f75d69656..c208d5680 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -376,8 +376,6 @@ int run_script_argv(const char *name, unsigned int hook_version, if (size > INT_MAX) return -EFBIG; - buffer = alloca(size); - if (hook_version == 0) { size += strlen(hookname); size++; @@ -390,19 +388,19 @@ int run_script_argv(const char *name, unsigned int hook_version, if (size > INT_MAX) return -EFBIG; + } + buffer = alloca(size); + if (hook_version == 0) buf_pos = snprintf(buffer, size, "exec %s %s %s %s", script, name, section, hookname); - if (buf_pos < 0 || (size_t)buf_pos >= size) { - ERROR("Failed to create command line for script \"%s\"", script); - return -1; - } - } else { + else buf_pos = snprintf(buffer, size, "exec %s", script); - if (buf_pos < 0 || (size_t)buf_pos >= size) { - ERROR("Failed to create command line for script \"%s\"", script); - return -1; - } + if (buf_pos < 0 || (size_t)buf_pos >= size) { + ERROR("Failed to create command line for script \"%s\"", script); + return -1; + } + if (hook_version == 1) { ret = setenv("LXC_HOOK_TYPE", hookname, 1); if (ret < 0) { SYSERROR("Failed to set environment variable: " -- 2.47.2