Unverified Commit eaebae7d by Christian Brauner Committed by GitHub

Merge pull request #2414 from 2xsec/bugfix

secure coding: strcpy => strlcpy
parents bbb97736 94b1cade
...@@ -923,7 +923,7 @@ static bool restore_net_info(struct lxc_container *c) ...@@ -923,7 +923,7 @@ static bool restore_net_info(struct lxc_container *c)
if (!lxc_mkifname(template)) if (!lxc_mkifname(template))
goto out_unlock; goto out_unlock;
strcpy(netdev->priv.veth_attr.veth1, template); (void)strlcpy(netdev->priv.veth_attr.veth1, template, IFNAMSIZ);
} }
} }
......
...@@ -1192,7 +1192,8 @@ static int do_create_container_dir(const char *path, struct lxc_conf *conf) ...@@ -1192,7 +1192,8 @@ static int do_create_container_dir(const char *path, struct lxc_conf *conf)
len = strlen(path); len = strlen(path);
p = alloca(len + 1); p = alloca(len + 1);
strcpy(p, path); (void)strlcpy(p, path, len + 1);
if (!lxc_list_empty(&conf->id_map)) { if (!lxc_list_empty(&conf->id_map)) {
ret = chown_mapped_root(p, conf); ret = chown_mapped_root(p, conf);
if (ret < 0) if (ret < 0)
...@@ -4777,6 +4778,7 @@ out: ...@@ -4777,6 +4778,7 @@ out:
struct lxc_container *lxc_container_new(const char *name, const char *configpath) struct lxc_container *lxc_container_new(const char *name, const char *configpath)
{ {
struct lxc_container *c; struct lxc_container *c;
size_t len;
if (!name) if (!name)
return NULL; return NULL;
...@@ -4799,12 +4801,14 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath ...@@ -4799,12 +4801,14 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath
} }
remove_trailing_slashes(c->config_path); remove_trailing_slashes(c->config_path);
c->name = malloc(strlen(name)+1);
len = strlen(name);
c->name = malloc(len + 1);
if (!c->name) { if (!c->name) {
fprintf(stderr, "Failed to allocate memory for %s\n", name); fprintf(stderr, "Failed to allocate memory for %s\n", name);
goto err; goto err;
} }
strcpy(c->name, name); (void)strlcpy(c->name, name, len + 1);
c->numthreads = 1; c->numthreads = 1;
c->slock = lxc_newlock(c->config_path, name); c->slock = lxc_newlock(c->config_path, name);
......
...@@ -1992,7 +1992,7 @@ char *lxc_mkifname(char *template) ...@@ -1992,7 +1992,7 @@ char *lxc_mkifname(char *template)
/* Generate random names until we find one that doesn't exist. */ /* Generate random names until we find one that doesn't exist. */
while (true) { while (true) {
name[0] = '\0'; name[0] = '\0';
strcpy(name, template); (void)strlcpy(name, template, IFNAMSIZ);
exists = false; exists = false;
for (i = 0; i < strlen(name); i++) { for (i = 0; i < strlen(name); i++) {
...@@ -2017,7 +2017,9 @@ char *lxc_mkifname(char *template) ...@@ -2017,7 +2017,9 @@ char *lxc_mkifname(char *template)
} }
freeifaddrs(ifaddr); freeifaddrs(ifaddr);
return strcpy(template, name); (void)strlcpy(template, name, strlen(template) + 1);
return template;
} }
int setup_private_host_hw_addr(char *veth1) int setup_private_host_hw_addr(char *veth1)
...@@ -2108,6 +2110,7 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna ...@@ -2108,6 +2110,7 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna
char *token, *saveptr = NULL; char *token, *saveptr = NULL;
char netdev_link[IFNAMSIZ]; char netdev_link[IFNAMSIZ];
char buffer[MAXPATHLEN] = {0}; char buffer[MAXPATHLEN] = {0};
size_t retlen;
if (netdev->type != LXC_NET_VETH) { if (netdev->type != LXC_NET_VETH) {
ERROR("Network type %d not support for unprivileged use", netdev->type); ERROR("Network type %d not support for unprivileged use", netdev->type);
...@@ -2224,12 +2227,12 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna ...@@ -2224,12 +2227,12 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna
return -1; return -1;
} }
if (strlen(token) >= IFNAMSIZ) { retlen = strlcpy(netdev->priv.veth_attr.veth1, token, IFNAMSIZ);
if (retlen >= IFNAMSIZ) {
ERROR("Host side veth device name returned by lxc-user-nic is " ERROR("Host side veth device name returned by lxc-user-nic is "
"too long"); "too long");
return -E2BIG; return -E2BIG;
} }
strcpy(netdev->priv.veth_attr.veth1, token);
/* netdev->priv.veth_attr.ifindex */ /* netdev->priv.veth_attr.ifindex */
token = strtok_r(NULL, ":", &saveptr); token = strtok_r(NULL, ":", &saveptr);
...@@ -2880,9 +2883,9 @@ static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev *netdev) ...@@ -2880,9 +2883,9 @@ static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev *netdev)
*/ */
if (netdev->name[0] == '\0') { if (netdev->name[0] == '\0') {
if (netdev->type == LXC_NET_PHYS) if (netdev->type == LXC_NET_PHYS)
strcpy(netdev->name, netdev->link); (void)strlcpy(netdev->name, netdev->link, IFNAMSIZ);
else else
strcpy(netdev->name, "eth%d"); (void)strlcpy(netdev->name, "eth%d", IFNAMSIZ);
} }
/* rename the interface name */ /* rename the interface name */
...@@ -2908,7 +2911,7 @@ static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev *netdev) ...@@ -2908,7 +2911,7 @@ static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev *netdev)
* name of the network device in the child's network namespace. We will * name of the network device in the child's network namespace. We will
* later on send this information back to the parent. * later on send this information back to the parent.
*/ */
strcpy(netdev->name, current_ifname); (void)strlcpy(netdev->name, current_ifname, IFNAMSIZ);
/* set a mac address */ /* set a mac address */
if (netdev->hwaddr) { if (netdev->hwaddr) {
......
...@@ -110,9 +110,11 @@ static void print_top_failing_dir(const char *path) ...@@ -110,9 +110,11 @@ static void print_top_failing_dir(const char *path)
len = strlen(path); len = strlen(path);
copy = alloca(len + 1); copy = alloca(len + 1);
strcpy(copy, path); (void)strlcpy(copy, path, len + 1);
p = copy; p = copy;
e = copy + len; e = copy + len;
while (p < e) { while (p < e) {
while (p < e && *p == '/') while (p < e && *p == '/')
p++; p++;
......
...@@ -88,8 +88,8 @@ char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name, ...@@ -88,8 +88,8 @@ char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name,
retpath = malloc(len); retpath = malloc(len);
if (!retpath) if (!retpath)
return NULL; return NULL;
strcpy(retpath, args.name); (void)strlcpy(retpath, args.name, len);
strcat(retpath, "/"); strncat(retpath, "/", 1);
strncat(retpath, name, name_len); strncat(retpath, name, name_len);
} else { } else {
/* we're at the root of ref_tree */ /* we're at the root of ref_tree */
...@@ -602,17 +602,20 @@ static bool update_tree_node(struct mytree_node *n, u64 id, u64 parent, ...@@ -602,17 +602,20 @@ static bool update_tree_node(struct mytree_node *n, u64 id, u64 parent,
if (!n->name) if (!n->name)
return false; return false;
strcpy(n->name, name); (void)strlcpy(n->name, name, name_len + 1);
} }
if (dirname) { if (dirname) {
n->dirname = malloc(strlen(dirname) + 1); size_t len;
len = strlen(dirname);
n->dirname = malloc(len + 1);
if (!n->dirname) { if (!n->dirname) {
free(n->name); free(n->name);
return false; return false;
} }
strcpy(n->dirname, dirname); (void)strlcpy(n->dirname, dirname, len + 1);
} }
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment