[CRIU] [PATCH 7/9] Use strlcpy

Kir Kolyshkin kir at openvz.org
Wed Oct 7 02:44:22 PDT 2015


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 at openvz.org>
---
 cr-dump.c    | 8 +++++---
 lib/criu.c   | 3 ++-
 net.c        | 5 +++--
 proc_parse.c | 3 ++-
 4 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/cr-dump.c b/cr-dump.c
index 3af077b..a67a587 100644
--- a/cr-dump.c
+++ b/cr-dump.c
@@ -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;
 
diff --git a/lib/criu.c b/lib/criu.c
index b048f05..b53ea38 100644
--- a/lib/criu.c
+++ b/lib/criu.c
@@ -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);
 
diff --git a/net.c b/net.c
index ef73d0b..e9b5870 100644
--- a/net.c
+++ b/net.c
@@ -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);
diff --git a/proc_parse.c b/proc_parse.c
index 6fdd840..3910590 100644
--- a/proc_parse.c
+++ b/proc_parse.c
@@ -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 "
-- 
2.4.3



More information about the CRIU mailing list