From 11f5089300c1c368d896c95890827dc85a67f132 Mon Sep 17 00:00:00 2001 From: Jason Wessel Date: Fri, 15 Jun 2018 08:04:35 -0700 Subject: [PATCH] container.c: Fix compiler errors that gcc 8.1.0 reports gcc 8.1.0 reports the following compiler errors/warnings. They can be fixed by using snprintf and checking the result for truncation. This patch also uses a named constant instead of inserting the value 512 in many locations. container.c: In function 'hyper_setup_container_rootfs': container.c:630:24: error: '/' directive writing 1 byte into a region of size between 0 and 511 [-Werror=format-overflow=] sprintf(rootfs, "%s/%s/", root, container->rootfs); ^ container.c:630:2: note: 'sprintf' output 3 or more bytes (assuming 514) into a destination of size 512 sprintf(rootfs, "%s/%s/", root, container->rootfs); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ container.c:262:18: error: '%s' directive writing up to 511 bytes into a region of size 510 [-Werror=format-overflow=] sprintf(dst, "./%s", src); ^~ ~~~ container.c:262:2: note: 'sprintf' output between 3 and 514 bytes into a destination of size 512 sprintf(dst, "./%s", src); ^~~~~~~~~~~~~~~~~~~~~~~~~ container.c:218:24: error: '/_data' directive writing 6 bytes into a region of size between 1 and 512 [-Werror=format-overflow=] sprintf(volume, "%s/_data", path); ^~~~~~ container.c:218:5: note: 'sprintf' output between 7 and 518 bytes into a destination of size 512 sprintf(volume, "%s/_data", path); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ container.c:149:24: error: '/_data' directive writing 6 bytes into a region of size between 0 and 511 [-Werror=format-overflow=] sprintf(volume, "/%s/_data", path); ^~~~~~ container.c:149:4: note: 'sprintf' output between 8 and 519 bytes into a destination of size 512 sprintf(volume, "/%s/_data", path); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ container.c:131:24: error: '/' directive writing 1 byte into a region of size between 0 and 511 [-Werror=format-overflow=] sprintf(volume, "/%s/", path); ^ container.c:131:4: note: 'sprintf' output between 3 and 514 bytes into a destination of size 512 sprintf(volume, "/%s/", path); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ container.c:176:24: error: '/_data/' directive writing 7 bytes into a region of size between 0 and 511 [-Werror=format-overflow=] sprintf(volume, "/%s/_data/%s", path, filevolume); ^~~~~~~ container.c:176:4: note: 'sprintf' output 9 or more bytes (assuming 520) into a destination of size 512 sprintf(volume, "/%s/_data/%s", path, filevolume); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Jason Wessel --- src/container.c | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/container.c b/src/container.c index 0938d82..b1c52d4 100644 --- a/src/container.c +++ b/src/container.c @@ -22,6 +22,8 @@ #include "syscall.h" #include "netlink.h" +#define MAX_PBUF 512 + static int container_populate_volume(char *src, char *dest) { struct stat st; @@ -99,12 +101,12 @@ static int container_setup_volume(struct hyper_pod *pod, struct hyper_container *container) { int i; - char dev[512], path[512]; + char dev[MAX_PBUF], path[MAX_PBUF]; struct volume *vol; for (i = 0; i < container->vols_num; i++) { - char volume[512]; - char mountpoint[512]; + char volume[MAX_PBUF]; + char mountpoint[MAX_PBUF]; char *options = NULL; const char *filevolume = NULL; vol = &container->vols[i]; @@ -128,7 +130,8 @@ static int container_setup_volume(struct hyper_pod *pod, if (hyper_mount_nfs(vol->device, path) < 0) return -1; /* nfs export has implicitly included _data part of the volume */ - sprintf(volume, "/%s/", path); + if (snprintf(volume, MAX_PBUF, "/%s/", path) >= MAX_PBUF) + return -1; } else { fprintf(stdout, "mount %s to %s, tmp path %s\n", dev, vol->mountpoint, path); @@ -137,7 +140,7 @@ static int container_setup_volume(struct hyper_pod *pod, options = "nouuid"; if (access(dev, R_OK) < 0) { - char device[512]; + char device[MAX_PBUF]; sprintf(device, "/block/%s", vol->device); hyper_netlink_wait_dev(pod->ueventfd, device); } @@ -146,7 +149,8 @@ static int container_setup_volume(struct hyper_pod *pod, perror("mount volume device failed"); return -1; } - sprintf(volume, "/%s/_data", path); + if (snprintf(volume, MAX_PBUF, "/%s/_data", path) >= MAX_PBUF) + return -1; } if (container_check_file_volume(volume, &filevolume) < 0) @@ -173,7 +177,8 @@ static int container_setup_volume(struct hyper_pod *pod, perror("create volume file failed"); return -1; } - sprintf(volume, "/%s/_data/%s", path, filevolume); + if (snprintf(volume, MAX_PBUF, "/%s/_data/%s", path, filevolume) >= MAX_PBUF) + return -1; /* 0777 so that any user can read/write the new file volume */ if (chmod(volume, 0777) < 0) { fprintf(stderr, "fail to chmod directory %s\n", volume); @@ -197,9 +202,9 @@ static int container_setup_volume(struct hyper_pod *pod, for (i = 0; i < container->maps_num; i++) { struct stat st; - char *src, path[512], volume[512]; + char *src, path[MAX_PBUF], volume[MAX_PBUF]; struct fsmap *map = &container->maps[i]; - char mountpoint[512]; + char mountpoint[MAX_PBUF]; sprintf(path, "%s/%s", SHARED_DIR, map->source); sprintf(mountpoint, "./%s", map->path); @@ -215,7 +220,8 @@ static int container_setup_volume(struct hyper_pod *pod, } if (map->docker) { /* converted from volume */ - sprintf(volume, "%s/_data", path); + if (snprintf(volume, MAX_PBUF, "%s/_data", path) >= MAX_PBUF) + return -1; src = volume; if (container->initialize && (container_populate_volume(mountpoint, volume) < 0)) { @@ -251,7 +257,7 @@ static int container_setup_modules(struct hyper_container *container) { struct stat st; struct utsname uts; - char src[512], dst[512]; + char src[MAX_PBUF], dst[MAX_PBUF]; if (uname(&uts) < 0) { perror("fail to call uname"); @@ -259,7 +265,8 @@ static int container_setup_modules(struct hyper_container *container) } sprintf(src, "/lib/modules/%s", uts.release); - sprintf(dst, "./%s", src); + if (snprintf(dst, MAX_PBUF, "./%s", src) >= MAX_PBUF) + return -1; if (stat(dst, &st) == 0) { struct dirent **list; @@ -291,7 +298,7 @@ static int container_setup_modules(struct hyper_container *container) static int container_setup_mount(struct hyper_container *container) { - char src[512]; + char src[MAX_PBUF]; // current dir is container rootfs, the operations on "./PATH" are the operations on container's "/PATH" if (!container->readonly) { @@ -546,7 +553,7 @@ static int hyper_setup_container_rootfs(void *data) { struct hyper_container_arg *arg = data; struct hyper_container *container = arg->c; - char root[512], rootfs[512]; + char root[MAX_PBUF], rootfs[MAX_PBUF]; int setup_dns; /* wait for ns-opened ready message */ @@ -609,7 +616,7 @@ static int hyper_setup_container_rootfs(void *data) goto fail; } } else { - char path[512]; + char path[MAX_PBUF]; sprintf(path, "%s/%s/", SHARED_DIR, container->image); fprintf(stdout, "src directory %s\n", path); @@ -627,7 +634,9 @@ static int hyper_setup_container_rootfs(void *data) fprintf(stdout, "root directory for container is %s/%s, init task %s\n", root, container->rootfs, container->exec.argv[0]); - sprintf(rootfs, "%s/%s/", root, container->rootfs); + if (snprintf(rootfs, MAX_PBUF, "%s/%s/", root, container->rootfs) >= MAX_PBUF) + goto fail; + if (mount(rootfs, rootfs, NULL, MS_BIND|MS_REC, NULL) < 0) { perror("failed to bind rootfs"); goto fail; @@ -710,7 +719,7 @@ fail: static int hyper_setup_pty(struct hyper_container *c) { - char root[512]; + char root[MAX_PBUF]; sprintf(root, "/tmp/hyper/%s/devpts/", c->id); @@ -730,7 +739,7 @@ static int hyper_setup_pty(struct hyper_container *c) static void hyper_cleanup_pty(struct hyper_container *c) { - char path[512]; + char path[MAX_PBUF]; sprintf(path, "/tmp/hyper/%s/devpts/", c->id); if (umount(path) < 0) @@ -739,7 +748,7 @@ static void hyper_cleanup_pty(struct hyper_container *c) int container_prepare_rootfs_dev(struct hyper_container *container, struct hyper_pod *pod) { - char dev[512]; + char dev[MAX_PBUF]; if (container->fstype == NULL) return 0; -- 2.17.1