The following pull request was submitted through Github. It can be accessed and reviewed at: https://github.com/lxc/lxc/pull/2821
This e-mail was sent by the LXC bot, direct replies will not reach the author unless they happen to be subscribed to this list. === Description (from pull-request) === Signed-off-by: Christian Brauner <[email protected]>
From 9254aa43388062b252ebcc2f212f5936c36d3b6f Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 06:51:55 +0100 Subject: [PATCH 01/23] memory_utils: add memory_utils.h The header defines a simple wrapper for free() that can be used with gcc's and clang's __attribute__((__cleanup__(<cleanup-fun>))) macro. Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/Makefile.am | 2 ++ src/lxc/memory_utils.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/lxc/memory_utils.h diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index 95b0a2f72..aa6368840 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -21,6 +21,7 @@ noinst_HEADERS = api_extensions.h \ lxc.h \ lxclock.h \ macro.h \ + memory_utils.h \ monitor.h \ namespace.h \ raw_syscalls.h \ @@ -112,6 +113,7 @@ liblxc_la_SOURCES = af_unix.c af_unix.h \ lxclock.c lxclock.h \ lxcseccomp.h \ macro.h \ + memory_utils.h \ mainloop.c mainloop.h \ namespace.c namespace.h \ nl.c nl.h \ diff --git a/src/lxc/memory_utils.h b/src/lxc/memory_utils.h new file mode 100644 index 000000000..8669af5bf --- /dev/null +++ b/src/lxc/memory_utils.h @@ -0,0 +1,31 @@ +/* liblxcapi + * + * Copyright © 2018 Christian Brauner <[email protected]>. + * Copyright © 2018 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __LXC_MEMORY_UTILS_H +#define __LXC_MEMORY_UTILS_H + +#include <stdlib.h> + +static inline void __auto_free__(void *p) +{ + if (p) + free(p); +} + +#endif /* __LXC_MEMORY_UTILS_H */ From 1a69ff5a956732c36eafcb75bb6fc5991b5bdb60 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:00:58 +0100 Subject: [PATCH 02/23] lxcmntent: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/include/lxcmntent.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/include/lxcmntent.c b/src/include/lxcmntent.c index 10c10c549..04eda0c83 100644 --- a/src/include/lxcmntent.c +++ b/src/include/lxcmntent.c @@ -21,7 +21,7 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE 1 #endif -#include <alloca.h> +#include <errno.h> #include <mntent.h> #include <stdio.h> #include <stdlib.h> @@ -154,20 +154,24 @@ struct mntent *getmntent(FILE *stream) /* Prepare to begin reading and/or writing mount table entries from the * beginning of FILE. MODE is as for `fopen'. */ +#define __SETMNTENT_MODE_MAX 256 FILE *setmntent(const char *file, const char *mode) { /* Extend the mode parameter with "c" to disable cancellation in the * I/O functions and "e" to set FD_CLOEXEC. */ size_t modelen = strlen(mode); - char *newmode; + char newmode[__SETMNTENT_MODE_MAX]; - newmode = alloca(modelen + 3); + if (modelen >= (__SETMNTENT_MODE_MAX - 2)) { + errno = -EFBIG; + return NULL; + } memcpy(newmode, mode, modelen); memcpy(newmode + modelen, "ce", 3); - return fopen (file, newmode); + return fopen(file, newmode); } /* Close a stream opened with `setmntent'. */ From b606bcdc750a8cf0d53a3a8fd3d59570feae5ed6 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:01:33 +0100 Subject: [PATCH 03/23] cgroups: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/cgroups/cgfsng.c | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index d359b67fc..4b5fd6892 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -58,6 +58,7 @@ #include "config.h" #include "log.h" #include "macro.h" +#include "memory_utils.h" #include "storage/storage.h" #include "utils.h" @@ -888,15 +889,18 @@ static bool controller_in_clist(char *cgline, char *c) return false; len = eol - cgline; - tmp = alloca(len + 1); + tmp = must_realloc(NULL, len + 1); memcpy(tmp, cgline, len); tmp[len] = '\0'; lxc_iterate_parts(tok, tmp, ",") { - if (strcmp(tok, c) == 0) + if (strcmp(tok, c) == 0) { + free(tmp); return true; + } } + free(tmp); return false; } @@ -2209,15 +2213,12 @@ __cgfsng_ops static int cgfsng_get(struct cgroup_ops *ops, const char *filename, char *value, size_t len, const char *name, const char *lxcpath) { - int ret = -1; - size_t controller_len; - char *controller, *p, *path; + __attribute__((__cleanup__(__auto_free__))) char *controller; + char *p, *path; struct hierarchy *h; + int ret = -1; - controller_len = strlen(filename); - controller = alloca(controller_len + 1); - (void)strlcpy(controller, filename, controller_len + 1); - + controller = must_copy_string(filename); p = strchr(controller, '.'); if (p) *p = '\0'; @@ -2248,15 +2249,12 @@ __cgfsng_ops static int cgfsng_set(struct cgroup_ops *ops, const char *filename, const char *value, const char *name, const char *lxcpath) { - int ret = -1; - size_t controller_len; - char *controller, *p, *path; + __attribute__((__cleanup__(__auto_free__))) char *controller; + char *p, *path; struct hierarchy *h; + int ret = -1; - controller_len = strlen(filename); - controller = alloca(controller_len + 1); - (void)strlcpy(controller, filename, controller_len + 1); - + controller = must_copy_string(filename); p = strchr(controller, '.'); if (p) *p = '\0'; @@ -2363,18 +2361,14 @@ static int convert_devpath(const char *invalue, char *dest) static int cg_legacy_set_data(struct cgroup_ops *ops, const char *filename, const char *value) { - size_t len; + __attribute__((__cleanup__(__auto_free__))) char *controller; char *fullpath, *p; /* "b|c <2^64-1>:<2^64-1> r|w|m" = 47 chars max */ char converted_value[50]; struct hierarchy *h; int ret = 0; - char *controller = NULL; - - len = strlen(filename); - controller = alloca(len + 1); - (void)strlcpy(controller, filename, len + 1); + controller = must_copy_string(filename); p = strchr(controller, '.'); if (p) *p = '\0'; From 3246aecd95c571b41fa78462348b5cf5d818fa4f Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:01:50 +0100 Subject: [PATCH 04/23] lxc_user_nic: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/Makefile.am | 2 ++ src/lxc/cmd/lxc_user_nic.c | 9 ++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index aa6368840..7d1522bf2 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -368,9 +368,11 @@ lxc_monitord_SOURCES = cmd/lxc_monitord.c \ lxc_user_nic_SOURCES = cmd/lxc_user_nic.c \ ../include/netns_ifaddrs.c ../include/netns_ifaddrs.h \ log.c log.h \ + memory_utils.h \ network.c network.h \ parse.c parse.h \ raw_syscalls.c raw_syscalls.h \ + string_utils.c string_utils.h \ syscall_wrappers.h lxc_usernsexec_SOURCES = cmd/lxc_usernsexec.c \ conf.c conf.h \ diff --git a/src/lxc/cmd/lxc_user_nic.c b/src/lxc/cmd/lxc_user_nic.c index 12c3d83c7..d56e33f9b 100644 --- a/src/lxc/cmd/lxc_user_nic.c +++ b/src/lxc/cmd/lxc_user_nic.c @@ -49,9 +49,11 @@ #include "config.h" #include "log.h" +#include "memory_utils.h" #include "network.h" #include "parse.h" #include "raw_syscalls.h" +#include "string_utils.h" #include "syscall_wrappers.h" #include "utils.h" @@ -838,13 +840,10 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, static bool create_db_dir(char *fnam) { + __attribute__((__cleanup__(__auto_free__))) char *p; int ret; - char *p; - size_t len; - len = strlen(fnam); - p = alloca(len + 1); - (void)strlcpy(p, fnam, len + 1); + must_copy_string(fnam); fnam = p; p = p + 1; From 3c838071cb9c6a775ac7926c527744b996399f6f Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:02:07 +0100 Subject: [PATCH 05/23] commands: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/commands.c | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/src/lxc/commands.c b/src/lxc/commands.c index 5f2e25b6d..d539eabe2 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -48,6 +48,7 @@ #include "lxc.h" #include "lxclock.h" #include "mainloop.h" +#include "memory_utils.h" #include "monitor.h" #include "start.h" #include "terminal.h" @@ -524,8 +525,8 @@ char *lxc_cmd_get_config_item(const char *name, const char *item, static int lxc_cmd_get_config_item_callback(int fd, struct lxc_cmd_req *req, struct lxc_handler *handler) { + __attribute__((__cleanup__(__auto_free__))) char *cidata = NULL; int cilen; - char *cidata; struct lxc_config_t *item; struct lxc_cmd_rsp rsp; @@ -538,7 +539,7 @@ static int lxc_cmd_get_config_item_callback(int fd, struct lxc_cmd_req *req, if (cilen <= 0) goto err1; - cidata = alloca(cilen + 1); + cidata = must_realloc(NULL, cilen + 1); if (item->get(req->data, cidata, cilen + 1, handler->conf, NULL) != cilen) goto err1; @@ -1103,9 +1104,9 @@ static void lxc_cmd_fd_cleanup(int fd, struct lxc_handler *handler, static int lxc_cmd_handler(int fd, uint32_t events, void *data, struct lxc_epoll_descr *descr) { + __attribute__((__cleanup__(__auto_free__))) void *reqdata = NULL; int ret; struct lxc_cmd_req req; - void *reqdata = NULL; struct lxc_handler *handler = data; ret = lxc_abstract_unix_rcv_credential(fd, &req, sizeof(req)); @@ -1143,21 +1144,7 @@ static int lxc_cmd_handler(int fd, uint32_t events, void *data, } if (req.datalen > 0) { - /* LXC_CMD_CONSOLE_LOG needs to be able to allocate data - * that exceeds LXC_CMD_DATA_MAX: use malloc() for that. - */ - if (req.cmd == LXC_CMD_CONSOLE_LOG) - reqdata = malloc(req.datalen); - else - reqdata = alloca(req.datalen); - if (!reqdata) { - ERROR("Failed to allocate memory for \"%s\" command", - lxc_cmd_str(req.cmd)); - errno = ENOMEM; - ret = -ENOMEM; - goto out_close; - } - + reqdata = must_realloc(NULL, req.datalen); ret = lxc_recv_nointr(fd, reqdata, req.datalen, 0); if (ret != req.datalen) { WARN("Failed to receive full command request. Ignoring " @@ -1177,9 +1164,6 @@ static int lxc_cmd_handler(int fd, uint32_t events, void *data, } out: - if (req.cmd == LXC_CMD_CONSOLE_LOG && reqdata) - free(reqdata); - return ret; out_close: From 4c141eaf81164baf42043c4a38d58a23d7bca7bd Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:06:02 +0100 Subject: [PATCH 06/23] commands_utils: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/commands_utils.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lxc/commands_utils.c b/src/lxc/commands_utils.c index f48f1188c..61c911568 100644 --- a/src/lxc/commands_utils.c +++ b/src/lxc/commands_utils.c @@ -38,6 +38,7 @@ #include "initutils.h" #include "log.h" #include "lxclock.h" +#include "memory_utils.h" #include "monitor.h" #include "state.h" #include "utils.h" @@ -102,9 +103,9 @@ int lxc_make_abstract_socket_name(char *path, size_t pathlen, const char *hashed_sock_name, const char *suffix) { + __attribute__((__cleanup__(__auto_free__))) char *tmppath = NULL; const char *name; char *offset; - char *tmppath; size_t len; size_t tmplen; uint64_t hash; @@ -153,7 +154,7 @@ int lxc_make_abstract_socket_name(char *path, size_t pathlen, /* ret >= len; lxcpath or name is too long. hash both */ tmplen = strlen(name) + strlen(lxcpath) + 2; - tmppath = alloca(tmplen); + tmppath = must_realloc(NULL, tmplen); ret = snprintf(tmppath, tmplen, "%s/%s", lxcpath, name); if (ret < 0 || (size_t)ret >= tmplen) { ERROR("Failed to create abstract socket name"); From ccabd0a6148b0c84c21692acf531a1d5eb8921fb Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:08:44 +0100 Subject: [PATCH 07/23] conf: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/conf.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/lxc/conf.c b/src/lxc/conf.c index be2852f27..b86cd6b9f 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -67,6 +67,7 @@ #include "lxclock.h" #include "lxcseccomp.h" #include "macro.h" +#include "memory_utils.h" #include "namespace.h" #include "network.h" #include "parse.h" @@ -486,8 +487,9 @@ int run_script_argv(const char *name, unsigned int hook_version, int run_script(const char *name, const char *section, const char *script, ...) { + __attribute__((__cleanup__(__auto_free__))) char *buffer = NULL; int ret; - char *buffer, *p; + char *p; va_list ap; size_t size = 0; @@ -508,7 +510,7 @@ int run_script(const char *name, const char *section, const char *script, ...) if (size > INT_MAX) return -1; - buffer = alloca(size); + buffer = must_realloc(NULL, size); ret = snprintf(buffer, size, "exec %s %s %s", script, name, section); if (ret < 0 || ret >= size) return -1; @@ -1136,16 +1138,16 @@ static int lxc_create_ttys(struct lxc_handler *handler) static int mount_autodev(const char *name, const struct lxc_rootfs *rootfs, const char *lxcpath) { + __attribute__((__cleanup__(__auto_free__))) char *path = NULL; int ret; size_t clen; - char *path; mode_t cur_mask; INFO("Preparing \"/dev\""); /* $(rootfs->mount) + "/dev/pts" + '\0' */ clen = (rootfs->path ? strlen(rootfs->mount) : 0) + 9; - path = alloca(clen); + path = must_realloc(NULL, clen); ret = snprintf(path, clen, "%s/dev", rootfs->path ? rootfs->mount : ""); if (ret < 0 || (size_t)ret >= clen) @@ -2560,6 +2562,7 @@ static int setup_caps(struct lxc_list *caps) static int dropcaps_except(struct lxc_list *caps) { + __attribute__((__cleanup__(__auto_free__))) int *caplist = NULL; int i, capid, numcaps; char *keep_entry; struct lxc_list *iterator; @@ -2570,7 +2573,7 @@ static int dropcaps_except(struct lxc_list *caps) TRACE("Found %d capabilities", numcaps); /* caplist[i] is 1 if we keep capability i */ - int *caplist = alloca(numcaps * sizeof(int)); + caplist = must_realloc(NULL, numcaps * sizeof(int)); memset(caplist, 0, numcaps * sizeof(int)); lxc_list_for_each (iterator, caps) { From 45769223c43cc1a2da803b77c6e087d06ffc2764 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:12:17 +0100 Subject: [PATCH 08/23] confile: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/confile.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 564cbe38a..782f2ca15 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -53,6 +53,7 @@ #include "../include/netns_ifaddrs.h" #include "log.h" #include "lxcseccomp.h" +#include "memory_utils.h" #include "network.h" #include "parse.h" #include "storage.h" @@ -2710,12 +2711,12 @@ int write_config(int fd, const struct lxc_conf *conf) bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key, const char *v) { + __attribute__((__cleanup__(__auto_free__))) char *tmp; int ret; size_t len; - char *tmp; len = strlen(key) + strlen(v) + 4; - tmp = alloca(len); + tmp = must_realloc(NULL, len); if (lxc_config_value_empty(v)) ret = snprintf(tmp, len, "%s =", key); @@ -2777,21 +2778,23 @@ bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath, const char *newpath, const char *oldname, const char *newname, const char *ovldir) { + __attribute__((__cleanup__(__auto_free__))) char *newdir = NULL, + *olddir = NULL; int ret; - char *lend, *newdir, *olddir, *p, *q; + char *lend, *p, *q; size_t newdirlen, olddirlen; char *lstart = conf->unexpanded_config; const char *key = "lxc.mount.entry"; olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2; - olddir = alloca(olddirlen + 1); + olddir = must_realloc(NULL, olddirlen + 1); ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath, oldname); if (ret < 0 || ret >= olddirlen + 1) return false; newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2; - newdir = alloca(newdirlen + 1); + newdir = must_realloc(NULL, newdirlen + 1); ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath, newname); if (ret < 0 || ret >= newdirlen + 1) @@ -2885,20 +2888,22 @@ bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath, const char *newpath, const char *oldname, const char *newname) { + __attribute__((__cleanup__(__auto_free__))) char *newdir = NULL, + *olddir = NULL; int ret; - char *lend, *newdir, *olddir, *p; + char *lend, *p; char *lstart = conf->unexpanded_config; size_t newdirlen, olddirlen; const char *key = "lxc.hook"; olddirlen = strlen(oldpath) + strlen(oldname) + 1; - olddir = alloca(olddirlen + 1); + olddir = must_realloc(NULL, olddirlen + 1); ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname); if (ret < 0 || ret >= olddirlen + 1) return false; newdirlen = strlen(newpath) + strlen(newname) + 1; - newdir = alloca(newdirlen + 1); + newdir = must_realloc(NULL, newdirlen + 1); ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname); if (ret < 0 || ret >= newdirlen + 1) return false; From 0536b074acfad58fe7d879b6c142e7aa8b0ec20a Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:19:56 +0100 Subject: [PATCH 09/23] lxccontainer: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/lxccontainer.c | 60 ++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 7c826a9fd..6d2ac7d8f 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -61,6 +61,7 @@ #include "lxc.h" #include "lxccontainer.h" #include "lxclock.h" +#include "memory_utils.h" #include "monitor.h" #include "namespace.h" #include "network.h" @@ -120,13 +121,13 @@ static bool do_lxcapi_save_config(struct lxc_container *c, const char *alt_file) static bool config_file_exists(const char *lxcpath, const char *cname) { + __attribute__((__cleanup__(__auto_free__))) char *fname; int ret; size_t len; - char *fname; /* $lxcpath + '/' + $cname + '/config' + \0 */ len = strlen(lxcpath) + strlen(cname) + 9; - fname = alloca(len); + fname = must_realloc(NULL, len); ret = snprintf(fname, len, "%s/%s/config", lxcpath, cname); if (ret < 0 || (size_t)ret >= len) return false; @@ -144,13 +145,13 @@ static bool config_file_exists(const char *lxcpath, const char *cname) */ static int ongoing_create(struct lxc_container *c) { + __attribute__((__cleanup__(__auto_free__))) char *path; int fd, ret; size_t len; - char *path; struct flock lk = {0}; len = strlen(c->config_path) + strlen(c->name) + 10; - path = alloca(len); + path = must_realloc(NULL, len); ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); if (ret < 0 || (size_t)ret >= len) return -1; @@ -190,14 +191,14 @@ static int ongoing_create(struct lxc_container *c) static int create_partial(struct lxc_container *c) { + __attribute__((__cleanup__(__auto_free__))) char *path; int fd, ret; size_t len; - char *path; struct flock lk = {0}; /* $lxcpath + '/' + $name + '/partial' + \0 */ len = strlen(c->config_path) + strlen(c->name) + 10; - path = alloca(len); + path = must_realloc(NULL, len); ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); if (ret < 0 || (size_t)ret >= len) return -1; @@ -227,15 +228,15 @@ static int create_partial(struct lxc_container *c) static void remove_partial(struct lxc_container *c, int fd) { + __attribute__((__cleanup__(__auto_free__))) char *path; int ret; size_t len; - char *path; close(fd); /* $lxcpath + '/' + $name + '/partial' + \0 */ len = strlen(c->config_path) + strlen(c->name) + 10; - path = alloca(len); + path = must_realloc(NULL, len); ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); if (ret < 0 || (size_t)ret >= len) return; @@ -768,26 +769,22 @@ static void push_arg(char ***argp, char *arg, int *nargs) static char **split_init_cmd(const char *incmd) { - size_t len, retlen; - char *copy, *p; + __attribute__((__cleanup__(__auto_free__))) char *copy = NULL; + char *p; char **argv; int nargs = 0; if (!incmd) return NULL; - len = strlen(incmd) + 1; - copy = alloca(len); - retlen = strlcpy(copy, incmd, len); - if (retlen >= len) - return NULL; + copy = must_copy_string(incmd); do { argv = malloc(sizeof(char *)); } while (!argv); argv[0] = NULL; - lxc_iterate_parts(p, copy, " ") + lxc_iterate_parts (p, copy, " ") push_arg(&argv, p, &nargs); if (nargs == 0) { @@ -1209,9 +1206,9 @@ WRAP_API(bool, lxcapi_stop) static int do_create_container_dir(const char *path, struct lxc_conf *conf) { + __attribute__((__cleanup__(__auto_free__))) char *p = NULL; int lasterr; size_t len; - char *p; int ret = -1; mode_t mask = umask(0002); @@ -1226,9 +1223,7 @@ static int do_create_container_dir(const char *path, struct lxc_conf *conf) ret = 0; } - len = strlen(path); - p = alloca(len + 1); - (void)strlcpy(p, path, len + 1); + p = must_copy_string(path); if (!lxc_list_empty(&conf->id_map)) { ret = chown_mapped_root(p, conf); @@ -1270,9 +1265,9 @@ static struct lxc_storage *do_storage_create(struct lxc_container *c, const char *type, struct bdev_specs *specs) { + __attribute__((__cleanup__(__auto_free__))) char *dest; int ret; size_t len; - char *dest; struct lxc_storage *bdev; /* rootfs.path or lxcpath/lxcname/rootfs */ @@ -1280,12 +1275,12 @@ static struct lxc_storage *do_storage_create(struct lxc_container *c, (access(c->lxc_conf->rootfs.path, F_OK) == 0)) { const char *rpath = c->lxc_conf->rootfs.path; len = strlen(rpath) + 1; - dest = alloca(len); + dest = must_realloc(NULL, len); ret = snprintf(dest, len, "%s", rpath); } else { const char *lxcpath = do_lxcapi_get_config_path(c); len = strlen(c->name) + strlen(lxcpath) + 9; - dest = alloca(len); + dest = must_realloc(NULL, len); ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name); } if (ret < 0 || (size_t)ret >= len) @@ -3408,12 +3403,12 @@ static int copy_file(const char *old, const char *new) static int copyhooks(struct lxc_container *oldc, struct lxc_container *c) { + __attribute__((__cleanup__(__auto_free__))) char *cpath; int i, len, ret; struct lxc_list *it; - char *cpath; len = strlen(oldc->config_path) + strlen(oldc->name) + 3; - cpath = alloca(len); + cpath = must_realloc(NULL, len); ret = snprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name); if (ret < 0 || ret >= len) return -1; @@ -3571,13 +3566,14 @@ static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0) bool should_default_to_snapshot(struct lxc_container *c0, struct lxc_container *c1) { + __attribute__((__cleanup__(__auto_free__))) char *p0, *p1; int ret; size_t l0 = strlen(c0->config_path) + strlen(c0->name) + 2; size_t l1 = strlen(c1->config_path) + strlen(c1->name) + 2; - char *p0 = alloca(l0 + 1); - char *p1 = alloca(l1 + 1); char *rootfs = c0->lxc_conf->rootfs.path; + p0 = must_realloc(NULL, l0 + 1); + p1 = must_realloc(NULL, l1 + 1); ret = snprintf(p0, l0, "%s/%s", c0->config_path, c0->name); if (ret < 0 || ret >= l0) return false; @@ -4099,11 +4095,11 @@ static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t static int get_next_index(const char *lxcpath, char *cname) { - char *fname; + __attribute__((__cleanup__(__auto_free__))) char *fname; struct stat sb; int i = 0, ret; - fname = alloca(strlen(lxcpath) + 20); + fname = must_realloc(NULL, strlen(lxcpath) + 20); while (1) { sprintf(fname, "%s/snap%d", lxcpath, i); @@ -4149,6 +4145,7 @@ static bool get_snappath_dir(struct lxc_container *c, char *snappath) static int do_lxcapi_snapshot(struct lxc_container *c, const char *commentfile) { + __attribute__((__cleanup__(__auto_free__))) char *dfnam = NULL; int i, flags, ret; time_t timer; struct tm tm_info; @@ -4212,7 +4209,7 @@ static int do_lxcapi_snapshot(struct lxc_container *c, const char *commentfile) strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", &tm_info); - char *dfnam = alloca(strlen(snappath) + strlen(newname) + 5); + dfnam = must_realloc(NULL, strlen(snappath) + strlen(newname) + 5); sprintf(dfnam, "%s/%s/ts", snappath, newname); f = fopen(dfnam, "w"); if (!f) { @@ -4233,10 +4230,11 @@ static int do_lxcapi_snapshot(struct lxc_container *c, const char *commentfile) } if (commentfile) { + __attribute__((__cleanup__(__auto_free__))) char *path; /* $p / $name / comment \0 */ int len = strlen(snappath) + strlen(newname) + 10; - char *path = alloca(len); + path = must_realloc(NULL, len); sprintf(path, "%s/%s/comment", snappath, newname); return copy_file(commentfile, path) < 0 ? -1 : i; } From bfed36b6fce0de95f85688fba492b1a37bcefa5a Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:23:19 +0100 Subject: [PATCH 10/23] namespace: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/namespace.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lxc/namespace.c b/src/lxc/namespace.c index b6eab04e6..5fbf02b0c 100644 --- a/src/lxc/namespace.c +++ b/src/lxc/namespace.c @@ -24,7 +24,6 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE 1 #endif -#include <alloca.h> #include <errno.h> #include <fcntl.h> #include <sched.h> @@ -37,6 +36,7 @@ #include "config.h" #include "log.h" +#include "memory_utils.h" #include "namespace.h" #include "utils.h" @@ -55,13 +55,14 @@ static int do_clone(void *arg) pid_t lxc_clone(int (*fn)(void *), void *arg, int flags) { + __attribute__((__cleanup__(__auto_free__))) char *stack; struct clone_arg clone_arg = { - .fn = fn, - .arg = arg, + .fn = fn, + .arg = arg, }; size_t stack_size = lxc_getpagesize(); - void *stack = alloca(stack_size); + stack = must_realloc(NULL, stack_size); pid_t ret; #ifdef __ia64__ From f13caa983478c3c03541a64b50519e268802a781 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:25:12 +0100 Subject: [PATCH 11/23] start: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/start.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lxc/start.c b/src/lxc/start.c index 91f1e51b7..6f39e95c9 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -26,7 +26,6 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE 1 #endif -#include <alloca.h> #include <dirent.h> #include <errno.h> #include <fcntl.h> @@ -67,6 +66,7 @@ #include "lxcseccomp.h" #include "macro.h" #include "mainloop.h" +#include "memory_utils.h" #include "monitor.h" #include "namespace.h" #include "network.h" @@ -97,14 +97,16 @@ static void lxc_destroy_container_on_signal(struct lxc_handler *handler, static void print_top_failing_dir(const char *path) { + __attribute__((__cleanup__(__auto_free__))) char *copy; int ret; size_t len; - char *copy, *e, *p, saved; + char *e, *p, saved; len = strlen(path); - copy = alloca(len + 1); + copy = must_realloc(NULL, len + 1); (void)strlcpy(copy, path, len + 1); + copy = must_copy_string(path); p = copy; e = copy + len; From b57bbeff7b09ca44ffc839c0afb5e7d6a32bcace Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:26:19 +0100 Subject: [PATCH 12/23] terminal: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/terminal.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lxc/terminal.c b/src/lxc/terminal.c index de0089101..1f5eeba48 100644 --- a/src/lxc/terminal.c +++ b/src/lxc/terminal.c @@ -44,6 +44,7 @@ #include "log.h" #include "lxclock.h" #include "mainloop.h" +#include "memory_utils.h" #include "start.h" #include "syscall_wrappers.h" #include "terminal.h" @@ -199,9 +200,9 @@ static int lxc_terminal_truncate_log_file(struct lxc_terminal *terminal) static int lxc_terminal_rotate_log_file(struct lxc_terminal *terminal) { + __attribute__((__cleanup__(__auto_free__))) char *tmp = NULL; int ret; size_t len; - char *tmp; if (!terminal->log_path || terminal->log_rotate == 0) return -EOPNOTSUPP; @@ -211,7 +212,7 @@ static int lxc_terminal_rotate_log_file(struct lxc_terminal *terminal) return -EBADF; len = strlen(terminal->log_path) + sizeof(".1"); - tmp = alloca(len); + tmp = must_realloc(NULL, len); ret = snprintf(tmp, len, "%s.1", terminal->log_path); if (ret < 0 || (size_t)ret >= len) From 624784e41854ec109b9120623ab6ef3dc764ba54 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:27:29 +0100 Subject: [PATCH 13/23] network: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/network.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lxc/network.c b/src/lxc/network.c index 499ddff6e..b5bfbe004 100644 --- a/src/lxc/network.c +++ b/src/lxc/network.c @@ -54,6 +54,7 @@ #include "file_utils.h" #include "log.h" #include "macro.h" +#include "memory_utils.h" #include "network.h" #include "nl.h" #include "raw_syscalls.h" @@ -549,15 +550,15 @@ int lxc_netdev_move_by_index(int ifindex, pid_t pid, const char *ifname) #define PHYSNAME "/sys/class/net/%s/phy80211/name" static char *is_wlan(const char *ifname) { + __attribute__((__cleanup__(__auto_free__))) char *path; int i, ret; long physlen; size_t len; - char *path; FILE *f; char *physname = NULL; len = strlen(ifname) + strlen(PHYSNAME) - 1; - path = alloca(len + 1); + path = must_realloc(NULL, len + 1); ret = snprintf(path, len, PHYSNAME, ifname); if (ret < 0 || (size_t)ret >= len) goto bad; From 196ee599f4a2d50dc7be55eaf8044171bc950821 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:30:00 +0100 Subject: [PATCH 14/23] string_utils: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/string_utils.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/lxc/string_utils.c b/src/lxc/string_utils.c index 0d7538c1f..9a92fd790 100644 --- a/src/lxc/string_utils.c +++ b/src/lxc/string_utils.c @@ -46,6 +46,7 @@ #include "config.h" #include "lxclock.h" #include "macro.h" +#include "memory_utils.h" #include "namespace.h" #include "parse.h" #include "string_utils.h" @@ -318,17 +319,14 @@ char *lxc_append_paths(const char *first, const char *second) bool lxc_string_in_list(const char *needle, const char *haystack, char _sep) { - char *token, *str; + __attribute__((__cleanup__(__auto_free__))) char *str = NULL; + char *token; char sep[2] = { _sep, '\0' }; - size_t len; if (!haystack || !needle) return 0; - len = strlen(haystack); - str = alloca(len + 1); - (void)strlcpy(str, haystack, len + 1); - + str = must_copy_string(haystack); lxc_iterate_parts(token, str, sep) if (strcmp(needle, token) == 0) return 1; @@ -338,21 +336,18 @@ bool lxc_string_in_list(const char *needle, const char *haystack, char _sep) char **lxc_string_split(const char *string, char _sep) { - char *token, *str; + __attribute__((__cleanup__(__auto_free__))) char *str = NULL; + char *token; char sep[2] = {_sep, '\0'}; char **tmp = NULL, **result = NULL; size_t result_capacity = 0; size_t result_count = 0; int r, saved_errno; - size_t len; if (!string) return calloc(1, sizeof(char *)); - len = strlen(string); - str = alloca(len + 1); - (void)strlcpy(str, string, len + 1); - + str = must_copy_string(string); lxc_iterate_parts(token, str, sep) { r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16); if (r < 0) @@ -458,22 +453,19 @@ char **lxc_string_split_quoted(char *string) char **lxc_string_split_and_trim(const char *string, char _sep) { - char *token, *str; + __attribute__((__cleanup__(__auto_free__))) char *str = NULL; + char *token; char sep[2] = { _sep, '\0' }; char **result = NULL; size_t result_capacity = 0; size_t result_count = 0; int r, saved_errno; size_t i = 0; - size_t len; if (!string) return calloc(1, sizeof(char *)); - len = strlen(string); - str = alloca(len + 1); - (void)strlcpy(str, string, len + 1); - + str = must_copy_string(string); lxc_iterate_parts(token, str, sep) { while (token[0] == ' ' || token[0] == '\t') token++; From 1ec622860e7599a8015196a7f47867af41178478 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:31:16 +0100 Subject: [PATCH 15/23] monitor: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/monitor.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lxc/monitor.c b/src/lxc/monitor.c index 4f8c285a0..03584e531 100644 --- a/src/lxc/monitor.c +++ b/src/lxc/monitor.c @@ -49,6 +49,7 @@ #include "log.h" #include "lxclock.h" #include "macro.h" +#include "memory_utils.h" #include "monitor.h" #include "state.h" #include "utils.h" @@ -170,9 +171,9 @@ int lxc_monitor_close(int fd) */ int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) { + __attribute__((__cleanup__(__auto_free__))) char *path; size_t len; int ret; - char *path; uint64_t hash; /* addr.sun_path is only 108 bytes, so we hash the full name and @@ -183,7 +184,7 @@ int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) /* strlen("lxc/") + strlen("/monitor-sock") + 1 = 18 */ len = strlen(lxcpath) + 18; - path = alloca(len); + path = must_realloc(NULL, len); ret = snprintf(path, len, "lxc/%s/monitor-sock", lxcpath); if (ret < 0 || (size_t)ret >= len) { ERROR("Failed to create name for monitor socket"); From b4055bbed9bf647755d38d175b621d8086e3192c Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:32:21 +0100 Subject: [PATCH 16/23] storage: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/storage/storage.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lxc/storage/storage.c b/src/lxc/storage/storage.c index c4f4c2ea3..95cf9c6ae 100644 --- a/src/lxc/storage/storage.c +++ b/src/lxc/storage/storage.c @@ -51,6 +51,7 @@ #include "lvm.h" #include "lxc.h" #include "lxclock.h" +#include "memory_utils.h" #include "namespace.h" #include "nbd.h" #include "overlay.h" @@ -568,13 +569,11 @@ struct lxc_storage *storage_create(const char *dest, const char *type, /* -B lvm,dir */ if (strchr(type, ',')) { - char *dup, *token; + __attribute__((__cleanup__(__auto_free__))) char *dup; + char *token; size_t len; - len = strlen(type); - dup = alloca(len + 1); - (void)strlcpy(dup, type, len + 1); - + dup = must_copy_string(type); lxc_iterate_parts(token, dup, ",") { bdev = do_storage_create(dest, token, cname, specs); if (bdev) From 1e06ae28b8d3118556b7ab1fbc358e32258f670f Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:33:48 +0100 Subject: [PATCH 17/23] pam_cgfs: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/Makefile.am | 1 + src/lxc/pam/pam_cgfs.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index 7d1522bf2..6ba9ecad2 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -433,6 +433,7 @@ pam_LTLIBRARIES = pam_cgfs.la pam_cgfs_la_SOURCES = pam/pam_cgfs.c \ file_utils.c file_utils.h \ macro.h \ + memory_utils.h \ string_utils.c string_utils.h if !HAVE_STRLCAT diff --git a/src/lxc/pam/pam_cgfs.c b/src/lxc/pam/pam_cgfs.c index 4a45600ea..955227cce 100644 --- a/src/lxc/pam/pam_cgfs.c +++ b/src/lxc/pam/pam_cgfs.c @@ -59,6 +59,7 @@ #include "config.h" #include "file_utils.h" #include "macro.h" +#include "memory_utils.h" #include "string_utils.h" #define PAM_SM_SESSION @@ -842,8 +843,9 @@ static char **cgv1_get_proc_mountinfo_controllers(char **klist, char **nlist, ch /* Check if a cgroupfs v2 controller is present in the string @cgline. */ static bool cgv1_controller_in_clist(char *cgline, char *c) { + __attribute__((__cleanup__(__auto_free__))) char *tmp = NULL; size_t len; - char *tok, *eol, *tmp; + char *tok, *eol; char *saveptr = NULL; eol = strchr(cgline, ':'); @@ -851,7 +853,7 @@ static bool cgv1_controller_in_clist(char *cgline, char *c) return false; len = eol - cgline; - tmp = alloca(len + 1); + tmp = must_realloc(NULL, len + 1); memcpy(tmp, cgline, len); tmp[len] = '\0'; From 7d03f71e470612a2d963f75efb86bf1e1c63330e Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:38:02 +0100 Subject: [PATCH 18/23] loop: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/storage/loop.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lxc/storage/loop.c b/src/lxc/storage/loop.c index 35cb13e06..776fe86de 100644 --- a/src/lxc/storage/loop.c +++ b/src/lxc/storage/loop.c @@ -39,6 +39,7 @@ #include "config.h" #include "log.h" #include "loop.h" +#include "memory_utils.h" #include "storage.h" #include "storage_utils.h" #include "utils.h" @@ -56,9 +57,9 @@ int loop_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, const char *lxcpath, int snap, uint64_t newsize, struct lxc_conf *conf) { + __attribute__((__cleanup__(__auto_free__))) char *srcdev = NULL; uint64_t size = newsize; int len, ret; - char *srcdev; char fstype[100] = "ext4"; if (snap) { @@ -70,7 +71,7 @@ int loop_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, return -1; len = strlen(lxcpath) + strlen(cname) + strlen("rootdev") + 3; - srcdev = alloca(len); + srcdev = must_realloc(NULL, len); ret = snprintf(srcdev, len, "%s/%s/rootdev", lxcpath, cname); if (ret < 0 || ret >= len) { ERROR("Failed to create string"); @@ -136,10 +137,10 @@ int loop_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, int loop_create(struct lxc_storage *bdev, const char *dest, const char *n, struct bdev_specs *specs) { + __attribute__((__cleanup__(__auto_free__))) char *srcdev; const char *fstype; uint64_t sz; int ret, len; - char *srcdev; if (!specs) return -1; @@ -148,7 +149,7 @@ int loop_create(struct lxc_storage *bdev, const char *dest, const char *n, * be <lxcpath>/<lxcname>/rootdev, and <src> will be "loop:<srcdev>". */ len = strlen(dest) + 2; - srcdev = alloca(len); + srcdev = must_realloc(NULL, len); ret = snprintf(srcdev, len, "%s", dest); if (ret < 0 || ret >= len) { From d79360c21e7d20c9549df2506e2ed5d066022f25 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:39:51 +0100 Subject: [PATCH 19/23] lvm: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/storage/lvm.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lxc/storage/lvm.c b/src/lxc/storage/lvm.c index c06e1a325..151f33ca8 100644 --- a/src/lxc/storage/lvm.c +++ b/src/lxc/storage/lvm.c @@ -37,6 +37,7 @@ #include "config.h" #include "log.h" #include "lvm.h" +#include "memory_utils.h" #include "rsync.h" #include "storage.h" #include "storage_utils.h" @@ -113,7 +114,7 @@ static int do_lvm_create(const char *path, uint64_t size, const char *thinpool) char *pathdup, *vg, *lv; char cmd_output[PATH_MAX]; char sz[24]; - char *tp = NULL; + __attribute__((__cleanup__(__auto_free__))) char *tp; struct lvcreate_args cmd_args = {0}; ret = snprintf(sz, 24, "%" PRIu64 "b", size); @@ -149,7 +150,7 @@ static int do_lvm_create(const char *path, uint64_t size, const char *thinpool) if (thinpool) { len = strlen(pathdup) + strlen(thinpool) + 2; - tp = alloca(len); + tp = must_realloc(NULL, len); ret = snprintf(tp, len, "%s/%s", pathdup, thinpool); if (ret < 0 || ret >= len) { @@ -266,16 +267,16 @@ int lvm_umount(struct lxc_storage *bdev) int lvm_compare_lv_attr(const char *path, int pos, const char expected) { + __attribute__((__cleanup__(__auto_free__))) char *cmd; struct lxc_popen_FILE *f; int ret, status; size_t len; - char *cmd; char output[12]; int start = 0; const char *lvscmd = "lvs --unbuffered --noheadings -o lv_attr %s 2>/dev/null"; len = strlen(lvscmd) + strlen(path) + 1; - cmd = alloca(len); + cmd = must_realloc(NULL, len); ret = snprintf(cmd, len, lvscmd, path); if (ret < 0 || (size_t)ret >= len) From c585aa2678e08a69b3688d76f60895c977662859 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:42:50 +0100 Subject: [PATCH 20/23] nbd: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/storage/nbd.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/lxc/storage/nbd.c b/src/lxc/storage/nbd.c index 2fae1fdb2..410b3b057 100644 --- a/src/lxc/storage/nbd.c +++ b/src/lxc/storage/nbd.c @@ -35,6 +35,7 @@ #include "config.h" #include "log.h" +#include "memory_utils.h" #include "nbd.h" #include "storage.h" #include "storage_utils.h" @@ -61,14 +62,11 @@ static bool wait_for_partition(const char *path); bool attach_nbd(char *src, struct lxc_conf *conf) { - char *orig, *p, path[50]; + __attribute__((__cleanup__(__auto_free__))) char *orig; + char *p, path[50]; int i = 0; - size_t len; - - len = strlen(src); - orig = alloca(len + 1); - (void)strlcpy(orig, src, len + 1); + orig = must_copy_string(src); /* if path is followed by a partition, drop that for now */ p = strchr(orig, ':'); if (p) From 86198961259a4c52867d1d524e23c9bcf140e089 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:44:13 +0100 Subject: [PATCH 21/23] rbd: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/storage/rbd.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lxc/storage/rbd.c b/src/lxc/storage/rbd.c index e02a629b9..8cc8605ae 100644 --- a/src/lxc/storage/rbd.c +++ b/src/lxc/storage/rbd.c @@ -33,6 +33,7 @@ #include "config.h" #include "log.h" +#include "memory_utils.h" #include "storage.h" #include "storage_utils.h" #include "utils.h" @@ -195,9 +196,9 @@ int rbd_create(struct lxc_storage *bdev, const char *dest, const char *n, int rbd_destroy(struct lxc_storage *orig) { + __attribute__((__cleanup__(__auto_free__))) char *rbdfullname = NULL; int ret; const char *src; - char *rbdfullname; char cmd_output[PATH_MAX]; struct rbd_args args = {0}; size_t len; @@ -215,7 +216,7 @@ int rbd_destroy(struct lxc_storage *orig) } len = strlen(src); - rbdfullname = alloca(len - 8); + rbdfullname = must_realloc(NULL, len - 8); (void)strlcpy(rbdfullname, &src[9], len - 8); args.rbd_name = rbdfullname; From 37f909346fdef4b34d517854cb8e686cbfe389b1 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:46:13 +0100 Subject: [PATCH 22/23] overlay: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/storage/overlay.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lxc/storage/overlay.c b/src/lxc/storage/overlay.c index 01546b1bf..36a545126 100644 --- a/src/lxc/storage/overlay.c +++ b/src/lxc/storage/overlay.c @@ -35,6 +35,7 @@ #include "log.h" #include "lxccontainer.h" #include "macro.h" +#include "memory_utils.h" #include "overlay.h" #include "rsync.h" #include "storage.h" @@ -491,8 +492,10 @@ bool ovl_detect(const char *path) int ovl_mount(struct lxc_storage *bdev) { - char *tmp, *options, *dup, *lower, *upper; - char *options_work, *work, *lastslash; + __attribute__((__cleanup__(__auto_free__))) char *options = NULL, + *options_work = NULL; + char *tmp, *dup, *lower, *upper; + char *work, *lastslash; int lastslashidx; size_t len, len2; unsigned long mntflags; @@ -602,27 +605,27 @@ int ovl_mount(struct lxc_storage *bdev) if (mntdata) { len = strlen(lower) + strlen(upper) + strlen("upperdir=,lowerdir=,") + strlen(mntdata) + 1; - options = alloca(len); + options = must_realloc(NULL, len); ret = snprintf(options, len, "upperdir=%s,lowerdir=%s,%s", upper, lower, mntdata); len2 = strlen(lower) + strlen(upper) + strlen(work) + strlen("upperdir=,lowerdir=,workdir=") + strlen(mntdata) + 1; - options_work = alloca(len2); + options_work = must_realloc(NULL, len2); ret2 = snprintf(options, len2, "upperdir=%s,lowerdir=%s,workdir=%s,%s", upper, lower, work, mntdata); } else { len = strlen(lower) + strlen(upper) + strlen("upperdir=,lowerdir=") + 1; - options = alloca(len); + options = must_realloc(NULL, len); ret = snprintf(options, len, "upperdir=%s,lowerdir=%s", upper, lower); len2 = strlen(lower) + strlen(upper) + strlen(work) + strlen("upperdir=,lowerdir=,workdir=") + 1; - options_work = alloca(len2); + options_work = must_realloc(NULL, len2); ret2 = snprintf(options_work, len2, "upperdir=%s,lowerdir=%s,workdir=%s", upper, lower, work); From 3fbf0a23aa52a795d39135f20f78a2e7264553ce Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Tue, 5 Feb 2019 07:47:18 +0100 Subject: [PATCH 23/23] lxc-unshare: remove stack allocations Signed-off-by: Christian Brauner <[email protected]> --- src/lxc/tools/lxc_unshare.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lxc/tools/lxc_unshare.c b/src/lxc/tools/lxc_unshare.c index a86d12b3c..ed9350906 100644 --- a/src/lxc/tools/lxc_unshare.c +++ b/src/lxc/tools/lxc_unshare.c @@ -398,7 +398,7 @@ int main(int argc, char *argv[]) if (my_args.setuid) { uint64_t wait_val = 1; /* enough space to accommodate uids */ - char *umap = (char *)alloca(100); + char umap[100]; /* create new uid mapping using current UID and the one * specified as parameter
_______________________________________________ lxc-devel mailing list [email protected] http://lists.linuxcontainers.org/listinfo/lxc-devel
