lxc/0111-lxc-use-safe_strdup-instead-of-strdup.patch

753 lines
25 KiB
Diff
Raw Normal View History

From b1ff42de6d5a507d66e9fe091a18d003f3022fbb Mon Sep 17 00:00:00 2001
2019-09-30 11:03:07 -04:00
From: LiFeng <lifeng68@huawei.com>
Date: Wed, 3 Jul 2019 23:41:15 -0400
Subject: [PATCH 111/140] lxc: use safe_strdup instead of strdup
2019-09-30 11:03:07 -04:00
Signed-off-by: LiFeng <lifeng68@huawei.com>
---
src/lxc/attach.c | 14 +++++---------
src/lxc/conf.c | 32 +++++++++----------------------
src/lxc/confile.c | 32 +++++++++----------------------
2019-09-30 11:03:07 -04:00
src/lxc/json/defs.c | 1 -
src/lxc/json/json_common.c | 41 ++++++++++++----------------------------
2019-09-30 11:03:07 -04:00
src/lxc/json/json_common.h | 4 +---
src/lxc/json/logger_json_file.c | 1 -
src/lxc/json/oci_runtime_hooks.c | 4 ++--
src/lxc/json/oci_runtime_spec.c | 1 -
src/lxc/lxccontainer.c | 16 +++++++---------
src/lxc/path.c | 20 +++++---------------
src/lxc/start.c | 18 +++++++++---------
src/lxc/terminal.c | 18 +++++++-----------
2019-09-30 11:03:07 -04:00
src/lxc/tools/lxc_attach.c | 4 ++--
src/lxc/tools/lxc_start.c | 2 +-
src/lxc/utils.c | 16 ++++++++++++++++
2019-09-30 11:03:07 -04:00
src/lxc/utils.h | 2 ++
17 files changed, 87 insertions(+), 139 deletions(-)
diff --git a/src/lxc/attach.c b/src/lxc/attach.c
index 6480eb9..d7b16e3 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/attach.c
+++ b/src/lxc/attach.c
@@ -1070,15 +1070,15 @@ static int lxc_attach_terminal(struct lxc_conf *conf,
/* isulad: if we pass fifo in option, use them as init fifos */
if (options->init_fifo[0]) {
free(terminal->init_fifo[0]);
- terminal->init_fifo[0] = strdup(options->init_fifo[0]);
+ terminal->init_fifo[0] = safe_strdup(options->init_fifo[0]);
}
if (options->init_fifo[1]) {
free(terminal->init_fifo[1]);
- terminal->init_fifo[1] = strdup(options->init_fifo[1]);
+ terminal->init_fifo[1] = safe_strdup(options->init_fifo[1]);
}
if (options->init_fifo[2]) {
free(terminal->init_fifo[2]);
- terminal->init_fifo[2] = strdup(options->init_fifo[2]);
+ terminal->init_fifo[2] = safe_strdup(options->init_fifo[2]);
}
ret = lxc_terminal_create(terminal);
@@ -1562,9 +1562,7 @@ int lxc_attach(const char *name, const char *lxcpath,
size_read = read(conf->errpipe[0], errbuf, BUFSIZ);
if (size_read > 0) {
if (err_msg)
- *err_msg = strdup(errbuf);
- if (!(*err_msg))
- ERROR("Out of memory");
+ *err_msg = safe_strdup(errbuf);
goto close_mainloop;
}
@@ -1585,9 +1583,7 @@ int lxc_attach(const char *name, const char *lxcpath,
}
if (g_attach_timeout_state == ATTACH_TIMEOUT && err_msg != NULL && *err_msg == NULL) {
- *err_msg = strdup("Attach exceeded timeout");
- if (!(*err_msg))
- ERROR("Out of memory");
+ *err_msg = safe_strdup("Attach exceeded timeout");
}
close_mainloop:
if (options->attach_flags & LXC_ATTACH_TERMINAL)
diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 8311723..1dfdaf3 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -1356,9 +1356,7 @@ static int rootfs_parent_mount_private(char *rootfs)
target = get_field(line, 4);
if (!target)
continue;
- tmptarget = strdup(target);
- if (!tmptarget)
- continue;
+ tmptarget = safe_strdup(target);
null_endofword(tmptarget);
if (!strstr(rootfs, tmptarget)) {
free(tmptarget);
@@ -1376,9 +1374,7 @@ static int rootfs_parent_mount_private(char *rootfs)
continue;
null_endofword(opts);
free(options);
- options = strdup(opts);
- if (!options)
- continue;
+ options = safe_strdup(opts);
}
if (!parent || !options) {
@@ -1417,7 +1413,7 @@ static int lxc_mount_rootfs(struct lxc_conf *conf)
// isulad: bind mount / to rootfs.mount. then we can do pivot root even if we use / as root.
if (!access(rootfs->mount, F_OK)) {
- rootfs->path = strdup("/");
+ rootfs->path = safe_strdup("/");
if (mount("/", rootfs->mount, NULL, MS_BIND, 0)) {
SYSERROR("Failed to mount / to %s.", rootfs->mount);
return -1;
@@ -2225,9 +2221,7 @@ int parse_mntopts(const char *mntopts, unsigned long *mntflags, unsigned long *p
if (!mntopts)
return 0;
- s = strdup(mntopts);
- if (!s)
- return -1;
+ s = safe_strdup(mntopts);
size = strlen(s) + 1;
data = malloc(size);
@@ -4026,7 +4020,7 @@ static int setup_populate_devs(const struct lxc_rootfs *rootfs, struct lxc_list
return -1;
/* create any missing directories */
- pathdirname = strdup(path);
+ pathdirname = safe_strdup(path);
pathdirname = dirname(pathdirname);
ret = mkdir_p(pathdirname, 0750);
free(pathdirname);
@@ -4545,7 +4539,7 @@ static char **merge_ocihook_env(char **oldenvs, size_t env_len, size_t *merge_en
for(i = 0; i < env_len; i++) {
if (oldenvs[i])
- result[i] = strdup(oldenvs[i]);
+ result[i] = safe_strdup(oldenvs[i]);
}
for(j = 0; j < (sizeof(lxc_envs) / sizeof(char *)); j++) {
@@ -4869,7 +4863,7 @@ static char *get_root_path(const char *path, const char *backend)
char *tmp = NULL;
if (!path) {
- ret = strdup("/");
+ ret = safe_strdup("/");
return ret;
}
if (!backend) {
@@ -4885,20 +4879,12 @@ static char *get_root_path(const char *path, const char *backend)
return NULL;
}
tmp++;
- ret = strdup(tmp);
- if (!ret) {
- ERROR("Out of memory");
- return NULL;
- }
+ ret = safe_strdup(tmp);
return ret;
}
default_out:
- ret = strdup(path);
- if (!ret) {
- ERROR("Out of memory");
- return NULL;
- }
+ ret = safe_strdup(path);
return ret;
}
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 216a688..8262d1e 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -1011,9 +1011,7 @@ static int set_config_group(const char *key, const char *value,
if (lxc_config_value_empty(value))
return lxc_clear_groups(lxc_conf);
- groups = strdup(value);
- if (!groups)
- return -1;
+ groups = safe_strdup(value);
/* In case several groups are specified in a single line split these
* groups in a single element for the list.
@@ -2281,7 +2279,7 @@ static int set_config_populate_device(const char *key, const char *value,
int ret = 0, major = 0, minor = 0;
uid_t uid = (uid_t)-1;
gid_t gid = (gid_t)-1;
- char name[PATH_MAX] = {0};
+ char name[4096] = {0}; /* MAX dev path name */
char type[3] = {0};
char *replace_value = NULL;
mode_t filemode = 0;
@@ -2295,7 +2293,7 @@ static int set_config_populate_device(const char *key, const char *value,
/* lxc.populate.device = PATH_IN_CONTAINER:DEVICETYPE:MAJOR:MINOR:MODE:UID:GID
* For e.g. lxc.populate.device = /dev/sda:b:8:0:0666:0:0
*/
- ret = sscanf(value, "%[^:]:%2[^:]:%i:%i:%i:%u:%u", name, type, &major, &minor, &filemode, &uid, &gid);
+ ret = sscanf(value, "%4095[^:]:%2[^:]:%i:%i:%i:%u:%u", name, type, &major, &minor, &filemode, &uid, &gid);
if (ret != 7)
return -1;
@@ -2306,9 +2304,7 @@ static int set_config_populate_device(const char *key, const char *value,
if (strcmp(name, dev_elem->name) != 0)
continue;
- replace_value = strdup(type);
- if (!replace_value)
- return -1;
+ replace_value = safe_strdup(type);
free(dev_elem->type);
dev_elem->type = replace_value;
@@ -2332,13 +2328,9 @@ static int set_config_populate_device(const char *key, const char *value,
goto on_error;
memset(dev_elem, 0, sizeof(*dev_elem));
- dev_elem->name = strdup(name);
- if (!dev_elem->name)
- goto on_error;
+ dev_elem->name = safe_strdup(name);
- dev_elem->type = strdup(type);
- if (!dev_elem->type)
- goto on_error;
+ dev_elem->type = safe_strdup(type);
dev_elem->file_mode = filemode;
dev_elem->maj = major;
@@ -2373,10 +2365,7 @@ static int set_config_rootfs_masked_paths(const char *key, const char *value,
if (!list_item)
goto on_error;
- list_item->elem = strdup(value);
-
- if (!list_item->elem)
- goto on_error;
+ list_item->elem = safe_strdup(value);
lxc_list_add_tail(&lxc_conf->rootfs.maskedpaths, list_item);
@@ -2401,10 +2390,7 @@ static int set_config_rootfs_ro_paths(const char *key, const char *value,
if (!list_item)
goto on_error;
- list_item->elem = strdup(value);
-
- if (!list_item->elem)
- goto on_error;
+ list_item->elem = safe_strdup(value);
lxc_list_add_tail(&lxc_conf->rootfs.ropaths, list_item);
@@ -2446,7 +2432,7 @@ static int set_config_systemd(const char *key, const char *value,
ERROR("Empty umask");
return -1;
}
- lxc_conf->systemd = strdup(value);
+ lxc_conf->systemd = safe_strdup(value);
return 0;
}
diff --git a/src/lxc/json/defs.c b/src/lxc/json/defs.c
index e7d9a09..8a052a8 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/json/defs.c
+++ b/src/lxc/json/defs.c
@@ -4,7 +4,6 @@
#endif
#include <string.h>
#include <read-file.h>
-#include "securec.h"
#include "defs.h"
defs_hook *make_defs_hook(yajl_val tree, struct parser_context *ctx, parser_error *err) {
diff --git a/src/lxc/json/json_common.c b/src/lxc/json/json_common.c
index 54b7b61..bea9b14 100755
2019-09-30 11:03:07 -04:00
--- a/src/lxc/json/json_common.c
+++ b/src/lxc/json/json_common.c
@@ -381,23 +381,6 @@ int common_safe_int(const char *numstr, int *converted) {
return 0;
}
-char *safe_strdup(const char *src)
-{
- char *dst = NULL;
-
- if (src == NULL) {
- return NULL;
- }
-
- dst = strdup(src);
- if (dst == NULL) {
- abort();
- }
-
- return dst;
-}
-
-
yajl_gen_status gen_json_map_int_int(void *ctx, json_map_int_int *map, struct parser_context *ptx, parser_error *err) {
yajl_gen_status stat = yajl_gen_status_ok;
yajl_gen g = (yajl_gen) ctx;
@@ -522,12 +505,12 @@ int append_json_map_int_int(json_map_int_int *map, int key, int val) {
vals = safe_malloc(len * sizeof(int));
if (map->len) {
- if (memcpy(keys, map->keys, map->len * sizeof(int)) != EOK) {
+ if (memcpy(keys, map->keys, map->len * sizeof(int)) != NULL) {
free(keys);
free(vals);
return -1;
}
- if (memcpy(vals, map->values, map->len * sizeof(int)) != EOK) {
+ if (memcpy(vals, map->values, map->len * sizeof(int)) != NULL) {
free(keys);
free(vals);
return -1;
@@ -663,12 +646,12 @@ int append_json_map_int_bool(json_map_int_bool *map, int key, bool val) {
vals = safe_malloc(len * sizeof(bool));
if (map->len) {
- if (memcpy(keys, map->keys, map->len * sizeof(int)) != EOK) {
+ if (memcpy(keys, map->keys, map->len * sizeof(int)) != NULL) {
free(keys);
free(vals);
return -1;
}
- if (memcpy(vals, map->values, map->len * sizeof(bool)) != EOK) {
+ if (memcpy(vals, map->values, map->len * sizeof(bool)) != NULL) {
free(keys);
free(vals);
return -1;
@@ -803,12 +786,12 @@ int append_json_map_int_string(json_map_int_string *map, int key, const char *va
vals = safe_malloc(len * sizeof(char *));
if (map->len) {
- if (memcpy(keys, map->keys, map->len * sizeof(int)) != EOK) {
+ if (memcpy(keys, map->keys, map->len * sizeof(int)) != NULL) {
free(keys);
free(vals);
return -1;
}
- if (memcpy(vals, map->values, map->len * sizeof(char *)) != EOK) {
+ if (memcpy(vals, map->values, map->len * sizeof(char *)) != NULL) {
free(keys);
free(vals);
return -1;
@@ -930,12 +913,12 @@ int append_json_map_string_int(json_map_string_int *map, const char *key, int va
vals = safe_malloc(len * sizeof(int));
if (map->len) {
- if (memcpy(keys, map->keys, map->len * sizeof(char *)) != EOK) {
+ if (memcpy(keys, map->keys, map->len * sizeof(char *)) != NULL) {
free(keys);
free(vals);
return -1;
}
- if (memcpy(vals, map->values, map->len * sizeof(int)) != EOK) {
+ if (memcpy(vals, map->values, map->len * sizeof(int)) != NULL) {
free(keys);
free(vals);
return -1;
@@ -1052,12 +1035,12 @@ int append_json_map_string_bool(json_map_string_bool *map, const char *key, bool
vals = safe_malloc(len * sizeof(bool));
if (map->len) {
- if (memcpy(keys, map->keys, map->len * sizeof(char *)) != EOK) {
+ if (memcpy(keys, map->keys, map->len * sizeof(char *)) != NULL) {
free(keys);
free(vals);
return -1;
}
- if (memcpy(vals, map->values, map->len * sizeof(bool)) != EOK) {
+ if (memcpy(vals, map->values, map->len * sizeof(bool)) != NULL) {
free(keys);
free(vals);
return -1;
@@ -1181,12 +1164,12 @@ int append_json_map_string_string(json_map_string_string *map, const char *key,
vals = safe_malloc(len * sizeof(char *));
if (map->len) {
- if (memcpy(keys, map->keys, map->len * sizeof(char *)) != EOK) {
+ if (memcpy(keys, map->keys, map->len * sizeof(char *)) != NULL) {
free(keys);
free(vals);
return -1;
}
- if (memcpy(vals, map->values, map->len * sizeof(char *)) != EOK) {
+ if (memcpy(vals, map->values, map->len * sizeof(char *)) != NULL) {
free(keys);
free(vals);
return -1;
diff --git a/src/lxc/json/json_common.h b/src/lxc/json/json_common.h
index 218a837..60aa5fd 100755
2019-09-30 11:03:07 -04:00
--- a/src/lxc/json/json_common.h
+++ b/src/lxc/json/json_common.h
@@ -9,7 +9,7 @@
#include <stdint.h>
#include <yajl/yajl_tree.h>
#include <yajl/yajl_gen.h>
-#include "securec.h"
+#include "utils.h"
#ifdef __cplusplus
extern "C" {
@@ -94,8 +94,6 @@ int common_safe_int64(const char *numstr, int64_t *converted);
int common_safe_int(const char *numstr, int *converted);
-char *safe_strdup(const char *src);
-
typedef struct {
int *keys;
int *values;
diff --git a/src/lxc/json/logger_json_file.c b/src/lxc/json/logger_json_file.c
index 409ea11..842d35b 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/json/logger_json_file.c
+++ b/src/lxc/json/logger_json_file.c
@@ -4,7 +4,6 @@
#endif
#include <string.h>
#include <read-file.h>
-#include "securec.h"
#include "logger_json_file.h"
logger_json_file *make_logger_json_file(yajl_val tree, struct parser_context *ctx, parser_error *err) {
diff --git a/src/lxc/json/oci_runtime_hooks.c b/src/lxc/json/oci_runtime_hooks.c
index 43ff8d7..41ddb67 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/json/oci_runtime_hooks.c
+++ b/src/lxc/json/oci_runtime_hooks.c
@@ -34,7 +34,7 @@ oci_runtime_spec_hooks *oci_runtime_spec_hooks_parse_file(const char *filename,
char errbuf[PARSE_ERR_BUFFER_SIZE];
if (content == NULL) {
if (asprintf(err, "cannot read the file: %s", filename) < 0) {
- *err = strdup("error allocating memory");
+ *err = safe_strdup("error allocating memory");
}
return NULL;
}
@@ -42,7 +42,7 @@ oci_runtime_spec_hooks *oci_runtime_spec_hooks_parse_file(const char *filename,
free(content);
if (tree == NULL) {
if (asprintf(err, "cannot parse the file: %s", errbuf) < 0) {
- *err = strdup("error allocating memory");
+ *err = safe_strdup("error allocating memory");
}
return NULL;
}
diff --git a/src/lxc/json/oci_runtime_spec.c b/src/lxc/json/oci_runtime_spec.c
index 4ccb635..fd342de 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/json/oci_runtime_spec.c
+++ b/src/lxc/json/oci_runtime_spec.c
@@ -4,7 +4,6 @@
#endif
#include <string.h>
#include <read-file.h>
-#include "securec.h"
#include "oci_runtime_spec.h"
oci_runtime_spec_hooks *make_oci_runtime_spec_hooks(yajl_val tree, struct parser_context *ctx, parser_error *err) {
diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index a09e066..ede4c88 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -1055,9 +1055,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
if (!started) {
size_read = read(conf->errpipe[0], errbuf, BUFSIZ);
if (size_read > 0) {
- conf->errmsg = strdup(errbuf);
- if (!conf->errmsg)
- ERROR("Out of memory");
+ conf->errmsg = safe_strdup(errbuf);
}
}
close(conf->errpipe[0]);
@@ -3199,7 +3197,7 @@ static bool container_destroy(struct lxc_container *c,
ERROR("Failed to destroy directory \"%s\" for \"%s\"", path,
c->name);
sprintf(msg, "Failed to destroy directory \"%s\": %s", path, errno ? strerror(errno) : "error");
- c->error_string = strdup(msg);
+ c->error_string = safe_strdup(msg);
goto out;
}
INFO("Destroyed directory \"%s\" for \"%s\"", path, c->name);
@@ -5147,17 +5145,17 @@ static bool do_lxcapi_set_terminal_default_fifos(struct lxc_container *c, const
if (in) {
if (conf->console.init_fifo[0])
free(conf->console.init_fifo[0]);
- conf->console.init_fifo[0] = strdup(in);
+ conf->console.init_fifo[0] = safe_strdup(in);
}
if (out) {
if (conf->console.init_fifo[1])
free(conf->console.init_fifo[1]);
- conf->console.init_fifo[1] = strdup(out);
+ conf->console.init_fifo[1] = safe_strdup(out);
}
if (err) {
if (conf->console.init_fifo[2])
free(conf->console.init_fifo[2]);
- conf->console.init_fifo[2] = strdup(err);
+ conf->console.init_fifo[2] = safe_strdup(err);
}
container_mem_unlock(c);
@@ -5181,7 +5179,7 @@ static bool do_lxcapi_set_container_info_file(struct lxc_container *c, const cha
conf = c->lxc_conf;
if (conf->container_info_file)
free(conf->container_info_file);
- conf->container_info_file = strdup(info_file);
+ conf->container_info_file = safe_strdup(info_file);
container_mem_unlock(c);
return true;
@@ -5347,7 +5345,7 @@ static struct lxc_container *do_lxc_container_new(const char *name, const char *
fprintf(stderr, "Failed to get lxc path for %s\n", name);
goto err;
}
- c->config_path = strdup(tmp);
+ c->config_path = safe_strdup(tmp);
}
if (!c->config_path) {
fprintf(stderr, "Failed to allocate memory for %s\n", name);
diff --git a/src/lxc/path.c b/src/lxc/path.c
index 36d5e0b..c545887 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/path.c
+++ b/src/lxc/path.c
@@ -23,11 +23,7 @@ bool specify_current_dir(const char *path)
char *basec = NULL, *bname = NULL;
bool res = false;
- basec = strdup(path);
- if (!basec) {
- ERROR("Out of memory");
- return false;
- }
+ basec = safe_strdup(path);
bname = basename(basec);
if (bname == NULL) {
@@ -106,13 +102,7 @@ bool filepath_split(const char *path, char **dir, char **base)
memcpy(*dir, path, i + 1);
*(*dir + i + 1) = '\0';
- *base = strdup(path + i + 1);
- if (!*base) {
- ERROR("Out of memory");
- free(*dir);
- *dir = NULL;
- return false;
- }
+ *base = safe_strdup(path + i + 1);
return true;
}
@@ -459,7 +449,7 @@ static char *eval_symlinks_in_scope(const char *fullpath, const char *rootpath)
}
if (!strcmp(fullpath, root)) {
- return strdup(fullpath);
+ return safe_strdup(fullpath);
}
if (strstr(fullpath, root) == NULL) {
@@ -592,7 +582,7 @@ char *path_relative(const char *basepath, const char *targpath)
}
if (strcmp(base, targ) == 0)
- return strdup(".");
+ return safe_strdup(".");
bl = strlen(base);
tl = strlen(targ);
@@ -646,5 +636,5 @@ char *path_relative(const char *basepath, const char *targpath)
return buf;
}
- return strdup(targ + t0);
+ return safe_strdup(targ + t0);
}
diff --git a/src/lxc/start.c b/src/lxc/start.c
index 2380581..d6c706e 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -2795,35 +2795,35 @@ static int clean_resource_set_env(struct lxc_handler *handler)
/* Start of environment variable setup for hooks. */
if (name) {
snprintf(bufstr, PATH_MAX + 1, "LXC_NAME=%s", name);
- conf->ocihooks->poststop[i]->env[j++] = strdup(bufstr);
+ conf->ocihooks->poststop[i]->env[j++] = safe_strdup(bufstr);
}
if (conf->rcfile) {
snprintf(bufstr, PATH_MAX + 1, "LXC_CONFIG_FILE=%s", conf->rcfile);
- conf->ocihooks->poststop[i]->env[j++] = strdup(bufstr);
+ conf->ocihooks->poststop[i]->env[j++] = safe_strdup(bufstr);
}
if (conf->rootfs.mount) {
snprintf(bufstr, PATH_MAX + 1, "LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount);
- conf->ocihooks->poststop[i]->env[j++] = strdup(bufstr);
+ conf->ocihooks->poststop[i]->env[j++] = safe_strdup(bufstr);
}
if (conf->rootfs.path) {
snprintf(bufstr, PATH_MAX + 1, "LXC_ROOTFS_PATH=%s", conf->rootfs.path);
- conf->ocihooks->poststop[i]->env[j++] = strdup(bufstr);
+ conf->ocihooks->poststop[i]->env[j++] = safe_strdup(bufstr);
}
if (conf->console.path) {
snprintf(bufstr, PATH_MAX + 1, "LXC_CONSOLE=%s", conf->console.path);
- conf->ocihooks->poststop[i]->env[j++] = strdup(bufstr);
+ conf->ocihooks->poststop[i]->env[j++] = safe_strdup(bufstr);
}
if (conf->console.log_path) {
snprintf(bufstr, PATH_MAX + 1, "LXC_CONSOLE_LOGPATH=%s", conf->console.log_path);
- conf->ocihooks->poststop[i]->env[j++] = strdup(bufstr);
+ conf->ocihooks->poststop[i]->env[j++] = safe_strdup(bufstr);
}
- conf->ocihooks->poststop[i]->env[j++] = strdup("LXC_CGNS_AWARE=1");
+ conf->ocihooks->poststop[i]->env[j++] = safe_strdup("LXC_CGNS_AWARE=1");
snprintf(bufstr, PATH_MAX + 1, "LXC_PID=%d", handler->pid);
- conf->ocihooks->poststop[i]->env[j++] = strdup(bufstr);
+ conf->ocihooks->poststop[i]->env[j++] = safe_strdup(bufstr);
if (handler->cgroup_ops->container_cgroup) {
snprintf(bufstr, PATH_MAX + 1, "LXC_CGROUP_PATH=%s", handler->cgroup_ops->container_cgroup);
- conf->ocihooks->poststop[i]->env[j++] = strdup(bufstr);
+ conf->ocihooks->poststop[i]->env[j++] = safe_strdup(bufstr);
}
conf->ocihooks->poststop[i]->env_len = j;
/* End of environment variable setup for hooks. */
diff --git a/src/lxc/terminal.c b/src/lxc/terminal.c
index 6b117de..970db69 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/terminal.c
+++ b/src/lxc/terminal.c
@@ -246,7 +246,7 @@ static int lxc_terminal_rename_old_log_file(struct lxc_terminal *terminal)
return -EFBIG;
}
free(rename_fname);
- rename_fname = strdup(tmp);
+ rename_fname = safe_strdup(tmp);
ret = sprintf(tmp, "%s.%u", terminal->log_path, (i - 1));
if (ret < 0) {
free(rename_fname);
@@ -457,10 +457,10 @@ static ssize_t lxc_logger_write(struct lxc_terminal *terminal, const char *type,
}
memcpy(msg->log, buf, bytes_read);
msg->log_len = bytes_read;
- msg->stream = type ? strdup(type) : strdup("stdout");
+ msg->stream = type ? safe_strdup(type) : safe_strdup("stdout");
get_now_time_buffer(timebuffer, sizeof(timebuffer));
- msg->time = strdup(timebuffer);
+ msg->time = safe_strdup(timebuffer);
json = logger_json_file_generate_json(msg, &ctx, &err);
if (!json) {
@@ -1324,9 +1324,9 @@ static int lxc_terminal_set_fifo(struct lxc_terminal *console, const char *in, c
}
memset(fifo_elem, 0, sizeof(*fifo_elem));
- fifo_elem->in_fifo = strdup(in ? in : "");
- fifo_elem->out_fifo = strdup(out ? out : "");
- fifo_elem->err_fifo = strdup(err ? err : "");
+ fifo_elem->in_fifo = safe_strdup(in ? in : "");
+ fifo_elem->out_fifo = safe_strdup(out ? out : "");
+ fifo_elem->err_fifo = safe_strdup(err ? err : "");
fifo_elem->in_fd = fifofd_in;
fifo_elem->out_fd = fifofd_out;
fifo_elem->err_fd = fifofd_err;
@@ -1810,11 +1810,7 @@ int lxc_terminal_add_fifos(struct lxc_conf *conf, const char *fifonames)
char *tmp = NULL, *saveptr = NULL, *in = NULL, *out = NULL, *err = NULL;
const char *none_fifo_name = "none";
- tmp = strdup(fifonames);
- if (!tmp) {
- ret = -1;
- goto free_out;
- }
+ tmp = safe_strdup(fifonames);
in = strtok_r(tmp, "&&&&", &saveptr);
if (!in) {
diff --git a/src/lxc/tools/lxc_attach.c b/src/lxc/tools/lxc_attach.c
index 854b3a2..7d51ad7 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/tools/lxc_attach.c
+++ b/src/lxc/tools/lxc_attach.c
@@ -309,7 +309,7 @@ static int do_attach_foreground(struct lxc_container *c, lxc_attach_command_t *c
}
out:
if (c->lxc_conf->errmsg)
- *errmsg = strdup(c->lxc_conf->errmsg);
+ *errmsg = safe_strdup(c->lxc_conf->errmsg);
return wexit;
}
@@ -353,7 +353,7 @@ static int do_attach_background(struct lxc_container *c, lxc_attach_command_t *c
msgpipe[1] = -1;
size_read = read(msgpipe[0], msgbuf, BUFSIZ);
if (size_read > 0) {
- *errmsg = strdup(msgbuf);
+ *errmsg = safe_strdup(msgbuf);
ret = -1;
}
diff --git a/src/lxc/tools/lxc_start.c b/src/lxc/tools/lxc_start.c
index af63f58..e48e5b3 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/tools/lxc_start.c
+++ b/src/lxc/tools/lxc_start.c
@@ -360,7 +360,7 @@ int main(int argc, char *argv[])
/* isulad: fifo used to monitor state of monitor process */
if (my_args.exit_monitor_fifo != NULL) {
- c->exit_fifo = strdup(my_args.exit_monitor_fifo);
+ c->exit_fifo = safe_strdup(my_args.exit_monitor_fifo);
}
/* isulad: add start timeout */
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 9db762f..e6e8905 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -2157,3 +2157,19 @@ FILE *lxc_fopen(const char *filename, const char *mode)
return fopen_cloexec(rpath, mode);
}
+char *safe_strdup(const char *src)
+{
+ char *dst = NULL;
+
+ if (src == NULL) {
+ return NULL;
+ }
+
+ dst = strdup(src);
+ if (dst == NULL) {
+ abort();
+ }
+
+ return dst;
+}
+
diff --git a/src/lxc/utils.h b/src/lxc/utils.h
index 2406ee1..0b33f69 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/utils.h
+++ b/src/lxc/utils.h
@@ -328,4 +328,6 @@ extern int lxc_mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t o
extern void *lxc_common_calloc_s(size_t size);
extern int lxc_open(const char *filename, int flags, mode_t mode);
extern FILE *lxc_fopen(const char *filename, const char *mode);
+extern char *safe_strdup(const char *src);
+
#endif /* __LXC_UTILS_H */
--
1.8.3.1
2019-09-30 11:03:07 -04:00