2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-31 06:15:24 +00:00

Use strlcpy

It's better to

1. Use strlcpy() instead of strncpy() as otherwise we might end up
   with a not NULL-terminated string, which opens a portal to hell.
   There are a few places reported by Coverity for this, such as:
    - in criu_connect(), Coverity CID 51591;
    - in proc_pid_parse(), Coverity CID 51590;
    - in move_veth_to_bridge(), Coverity CID 51593;
    - etc.

2. Use strlcpy() instead of strcpy() to avoid buffer overruns.
   Some of these are also reported by Coverity, for example
   the one in dump_filemap(), Coverity CID 51630.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
This commit is contained in:
Kir Kolyshkin
2015-10-07 02:44:22 -07:00
committed by Pavel Emelyanov
parent bf607a6ea8
commit e5654e586c
4 changed files with 12 additions and 7 deletions

View File

@@ -41,6 +41,7 @@
#include "crtools.h"
#include "cr_options.h"
#include "servicefd.h"
#include "string.h"
#include "syscall.h"
#include "ptrace.h"
#include "util.h"
@@ -344,7 +345,8 @@ static int dump_filemap(pid_t pid, struct vma_area *vma_area,
if (vma_area->aufs_rpath) {
struct fd_link aufs_link;
strcpy(aufs_link.name, vma_area->aufs_rpath);
strlcpy(aufs_link.name, vma_area->aufs_rpath,
sizeof(aufs_link.name));
aufs_link.len = strlen(aufs_link.name);
p.link = &aufs_link;
}
@@ -683,7 +685,7 @@ static int dump_task_core_all(struct pstree_item *item,
core->tc->seccomp_mode = dmpi(item)->pi_creds->seccomp_mode;
}
strncpy((char *)core->tc->comm, stat->comm, TASK_COMM_LEN);
strlcpy((char *)core->tc->comm, stat->comm, TASK_COMM_LEN);
core->tc->flags = stat->flags;
core->tc->task_state = item->state;
core->tc->exit_code = 0;
@@ -800,7 +802,7 @@ static int dump_one_zombie(const struct pstree_item *item,
if (!core)
return -1;
strncpy((char *)core->tc->comm, pps->comm, TASK_COMM_LEN);
strlcpy((char *)core->tc->comm, pps->comm, TASK_COMM_LEN);
core->tc->task_state = TASK_DEAD;
core->tc->exit_code = pps->exit_code;

View File

@@ -13,6 +13,7 @@
#include <alloca.h>
#include "criu.h"
#include "string.h"
#include "rpc.pb-c.h"
#include "cr-service-const.h"
@@ -881,7 +882,7 @@ static int criu_connect(criu_opts *opts)
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
strncpy(addr.sun_path, opts->service_address, sizeof(addr.sun_path));
strlcpy(addr.sun_path, opts->service_address, sizeof(addr.sun_path));
addr_len = strlen(addr.sun_path) + sizeof(addr.sun_family);

5
net.c
View File

@@ -23,6 +23,7 @@
#include "action-scripts.h"
#include "sockets.h"
#include "pstree.h"
#include "string.h"
#include "sysctl.h"
#include "protobuf.h"
#include "protobuf/netdev.pb-c.h"
@@ -975,7 +976,7 @@ int move_veth_to_bridge(void)
ret = -1;
break;
}
strncpy(ifr.ifr_name, n->bridge, IFNAMSIZ);
strlcpy(ifr.ifr_name, n->bridge, IFNAMSIZ);
ret = ioctl(s, SIOCBRADDIF, &ifr);
if (ret < 0) {
pr_perror("Can't add interface %s to bridge %s",
@@ -988,7 +989,7 @@ int move_veth_to_bridge(void)
* $ ip link set dev <device> up
*/
ifr.ifr_ifindex = 0;
strncpy(ifr.ifr_name, n->outside, IFNAMSIZ);
strlcpy(ifr.ifr_name, n->outside, IFNAMSIZ);
ret = ioctl(s, SIOCGIFFLAGS, &ifr);
if (ret < 0) {
pr_perror("Can't get flags of interface %s", n->outside);

View File

@@ -28,6 +28,7 @@
#include "cr_options.h"
#include "sysfs_parse.h"
#include "seccomp.h"
#include "string.h"
#include "namespaces.h"
#include "files-reg.h"
@@ -645,7 +646,7 @@ int parse_pid_stat(pid_t pid, struct proc_pid_stat *s)
*tok = '\0';
*p = '\0';
strncpy(s->comm, tok + 1, sizeof(s->comm));
strlcpy(s->comm, tok + 1, sizeof(s->comm));
n = sscanf(p + 1,
" %c %d %d %d %d %d %u %lu %lu %lu %lu "