diff --git a/src/container.c b/src/container.c index ff94ef7..046d0ac 100644 --- a/src/container.c +++ b/src/container.c @@ -313,15 +313,22 @@ static int container_setup_mount(struct hyper_container *container) return -1; } - if (unlink("./dev/ptmx") < 0) + if (unlink("./dev/ptmx") < 0) { perror("remove /dev/ptmx failed"); - if (symlink("/dev/pts/ptmx", "./dev/ptmx") < 0) + return -1; + } + if (symlink("/dev/pts/ptmx", "./dev/ptmx") < 0) { perror("link /dev/pts/ptmx to /dev/ptmx failed"); + return -1; + } - symlink("/proc/self/fd", "./dev/fd"); - symlink("/proc/self/fd/0", "./dev/stdin"); - symlink("/proc/self/fd/1", "./dev/stdout"); - symlink("/proc/self/fd/2", "./dev/stderr"); + if (symlink("/proc/self/fd", "./dev/fd") < 0 || + symlink("/proc/self/fd/0", "./dev/stdin") < 0 || + symlink("/proc/self/fd/1", "./dev/stdout") < 0 || + symlink("/proc/self/fd/2", "./dev/stderr") < 0) { + perror("failed to symlink for /dev/fd, /dev/stdin, /dev/stdout or /dev/stderr"); + return -1; + } return 0; } @@ -584,7 +591,10 @@ static int hyper_setup_container_rootfs(void *data) perror("failed to bind rootfs"); goto fail; } - chdir(rootfs); + if (chdir(rootfs) < 0) { + perror("failed to change the root to path of the container root(before manipulating)"); + goto fail; + } /* * Recreate dns resolver iif configured by pod spec. Other cases @@ -621,9 +631,15 @@ static int hyper_setup_container_rootfs(void *data) } /* pivot_root won't work, see * Documention/filesystem/ramfs-rootfs-initramfs.txt */ - chroot("."); + if (chroot(".") < 0) { + perror("failed to setup the root for the mount namepsace"); + goto fail; + } - chdir("/"); + if (chdir("/") < 0) { + perror("failed chdir to the new root"); + goto fail; + } if (container_setup_sysctl(container) < 0) { fprintf(stderr, "container sets up sysctl failed\n"); diff --git a/src/exec.c b/src/exec.c index ef57889..780e676 100644 --- a/src/exec.c +++ b/src/exec.c @@ -271,7 +271,10 @@ static int hyper_setup_exec_user(struct hyper_exec *exec) if (exec->tty) { char ptmx[512]; sprintf(ptmx, "/dev/pts/%d", exec->ptyno); - chown(ptmx, uid, gid); + if (chown(ptmx, uid, gid) < 0) { + perror("failed to change the owner for the slave pty file"); + goto fail; + } } // apply @@ -490,7 +493,10 @@ static int hyper_do_exec_cmd(struct hyper_exec *exec, struct hyper_pod *pod, int perror("fail to enter container ns"); goto out; } - chdir("/"); + if (chdir("/") < 0) { + perror("fail to change to the root of the rootfs"); + goto out; + } /* TODO: merge container env to exec env in hyperd */ if (hyper_setup_env(c->exec.envs, c->exec.envs_num) < 0) { diff --git a/src/init.c b/src/init.c index bae9036..67fe147 100644 --- a/src/init.c +++ b/src/init.c @@ -1370,13 +1370,20 @@ int main(int argc, char *argv[]) return -1; } - symlink("/busybox", "/sh"); - symlink("/busybox", "/tar"); - symlink("/busybox", "/sbin/modprobe"); - symlink("/busybox", "/sbin/depmod"); - symlink("/iptables", "/sbin/iptables"); - symlink("/iptables", "/sbin/iptables-restore"); - symlink("/iptables", "/sbin/iptables-save"); + if (symlink("/busybox", "/sh") < 0 || + symlink("/busybox", "/tar") < 0 || + symlink("/busybox", "/sbin/modprobe") < 0 || + symlink("/busybox", "/sbin/depmod") < 0) { + perror("failed to symlink tools to /busybox"); + return -1; + } + + if (symlink("/iptables", "/sbin/iptables") < 0 || + symlink("/iptables", "/sbin/iptables-restore") < 0 || + symlink("/iptables", "/sbin/iptables-save") < 0) { + perror("failed to symlink tools to /iptables"); + /* TODO disable portmapping */ + } cmdline = read_cmdline();