jail: don't assume positive return value of creat
[project/procd.git] / jail / jail.c
index fc8d8247c5d521e255368e00768255f0b55fb3b5..09780ac7e2b732cd6456c835f93c29816ad3dc89 100644 (file)
@@ -54,6 +54,7 @@
 #include "log.h"
 #include "seccomp-oci.h"
 #include "cgroups.h"
+#include "netifd.h"
 
 #include <libubox/blobmsg.h>
 #include <libubox/blobmsg_json.h>
@@ -68,7 +69,7 @@
 #endif
 
 #define STACK_SIZE     (1024 * 1024)
-#define OPT_ARGS       "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:EyJ:iP:"
+#define OPT_ARGS       "cC:d:e:EfFG:h:ij:J:ln:NoO:pP:r:R:sS:uU:w:t:T:y"
 
 #define OCI_VERSION_STRING "1.0.2"
 
@@ -152,6 +153,7 @@ static struct {
        char *ocibundle;
        bool immediately;
        struct blob_attr *annotations;
+       int term_timeout;
 } opts;
 
 static struct blob_buf ocibuf;
@@ -213,6 +215,10 @@ static void free_hooklist(struct hook_execvpe **hooklist)
 
 static void free_sysctl(void) {
        struct sysctl_val *cur;
+
+       if (!opts.sysctl)
+               return;
+
        cur = *opts.sysctl;
 
        while (cur) {
@@ -368,7 +374,7 @@ static int create_dev_console(const char *jail_root)
 {
        char *console_fname;
        char dev_console_path[PATH_MAX];
-       int slave_console_fd;
+       int slave_console_fd, dev_console_dummy;
 
        /* Open UNIX/98 virtual console */
        console_fd = posix_openpt(O_RDWR | O_NOCTTY);
@@ -388,13 +394,20 @@ static int create_dev_console(const char *jail_root)
 
        /* mount-bind PTY slave to /dev/console in jail */
        snprintf(dev_console_path, sizeof(dev_console_path), "%s/dev/console", jail_root);
-       close(creat(dev_console_path, 0620));
+       dev_console_dummy = creat(dev_console_path, 0620);
+       if (dev_console_dummy < 0)
+               goto no_console;
+
+       close(dev_console_dummy);
 
        if (mount(console_fname, dev_console_path, "bind", MS_BIND, NULL))
                goto no_console;
 
        /* use PTY slave for stdio */
        slave_console_fd = open(console_fname, O_RDWR); /* | O_NOCTTY */
+       if (slave_console_fd < 0)
+               goto no_console;
+
        dup2(slave_console_fd, 0);
        dup2(slave_console_fd, 1);
        dup2(slave_console_fd, 2);
@@ -457,7 +470,7 @@ static void run_hooklist(void)
        struct stat s;
 
        if (!hook)
-               hook_return_cb();
+               return hook_return_cb();
 
        DEBUG("executing hook %s\n", hook->file);
 
@@ -579,6 +592,8 @@ static struct mknod_args default_devices[] = {
 static int create_devices(void)
 {
        struct mknod_args **cur, *curdef;
+       char *path, *tmp;
+       int ret;
 
        if (!opts.devices)
                goto only_default_devices;
@@ -586,12 +601,33 @@ static int create_devices(void)
        cur = opts.devices;
 
        while (*cur) {
-               DEBUG("creating %s (mode=%08o)\n", (*cur)->path, (*cur)->mode);
-               if (mknod((*cur)->path, (*cur)->mode, (*cur)->dev))
+               path = (*cur)->path;
+               /* don't allow devices outside of /dev */
+               if (strncmp(path, "/dev", 4))
+                       return EPERM;
+
+               /* make sure parent folder exists */
+               tmp = strrchr(path, '/');
+               if (!tmp)
+                       return EINVAL;
+
+               *tmp = '\0';
+               if (strcmp(path, "/dev")) {
+                       DEBUG("creating directory %s\n", path);
+
+                       mkdir_p(path, 0755);
+               }
+               *tmp = '/';
+
+               DEBUG("creating %s (mode=%08o)\n", path, (*cur)->mode);
+
+               /* create device */
+               if (mknod(path, (*cur)->mode, (*cur)->dev))
                        return errno;
 
+               /* change owner, if needed */
                if (((*cur)->uid || (*cur)->gid) &&
-                   chown((*cur)->path, (*cur)->uid, (*cur)->gid))
+                   chown(path, (*cur)->uid, (*cur)->gid))
                        return errno;
 
                ++cur;
@@ -613,11 +649,25 @@ only_default_devices:
        }
 
        /* Dev symbolic links as defined in OCI spec */
-       (void) symlink("/dev/pts/ptmx", "/dev/ptmx");
-       (void) symlink("/proc/self/fd", "/dev/fd");
-       (void) symlink("/proc/self/fd/0", "/dev/stdin");
-       (void) symlink("/proc/self/fd/1", "/dev/stdout");
-       (void) symlink("/proc/self/fd/2", "/dev/stderr");
+       ret = symlink("/dev/pts/ptmx", "/dev/ptmx");
+       if (ret < 0)
+               WARNING("symlink() failed to create link to /dev/pts/ptmx");
+
+       ret = symlink("/proc/self/fd", "/dev/fd");
+       if (ret < 0)
+               WARNING("symlink() failed to create link to /proc/self/fd");
+
+       ret = symlink("/proc/self/fd/0", "/dev/stdin");
+       if (ret < 0)
+               WARNING("symlink() failed to create link to /proc/self/fd/0");
+
+       ret = symlink("/proc/self/fd/1", "/dev/stdout");
+       if (ret < 0)
+               WARNING("symlink() failed to create link to /proc/self/fd/1");
+
+       ret = symlink("/proc/self/fd/2", "/dev/stderr");
+       if (ret < 0)
+               WARNING("symlink() failed to create link to /proc/self/fd/2");
 
        return 0;
 }
@@ -701,7 +751,7 @@ static int build_jail_fs(void)
                create_dev_console(jail_root);
 
        /* make sure /etc/resolv.conf exists if in new network namespace */
-       if (!opts.extroot && opts.namespace & CLONE_NEWNET) {
+       if (opts.namespace & CLONE_NEWNET) {
                char jailetc[PATH_MAX], jaillink[PATH_MAX];
 
                snprintf(jailetc, PATH_MAX, "%s/etc", jail_root);
@@ -710,7 +760,9 @@ static int build_jail_fs(void)
                if (overlaydir)
                        unlink(jaillink);
 
-               (void) symlink("../dev/resolv.conf.d/resolv.conf.auto", jaillink);
+               ret = symlink("../dev/resolv.conf.d/resolv.conf.auto", jaillink);
+               if (ret < 0)
+                       WARNING("symlink() failed to create link to ../dev/resolv.conf.d/resolv.conf.auto");
        }
 
        run_hooks(opts.hooks.createContainer, enter_jail_fs);
@@ -954,6 +1006,7 @@ static void usage(void)
        fprintf(stderr, "  -C <file>\tcapabilities drop config\n");
        fprintf(stderr, "  -c\t\tset PR_SET_NO_NEW_PRIVS\n");
        fprintf(stderr, "  -n <name>\tthe name of the jail\n");
+       fprintf(stderr, "  -e <var>\timport environment variable\n");
        fprintf(stderr, "namespace jail options:\n");
        fprintf(stderr, "  -h <hostname>\tchange the hostname of the jail\n");
        fprintf(stderr, "  -N\t\tjail has network namespace\n");
@@ -1066,11 +1119,17 @@ static void jail_handle_signal(int signo)
        if (hook_running) {
                DEBUG("forwarding signal %d to the hook process\n", signo);
                kill(hook_process.pid, signo);
+               /* set timeout to send SIGKILL hook process in case SIGTERM doesn't succeed */
+               if (signo == SIGTERM)
+                       uloop_timeout_set(&hook_process_timeout, opts.term_timeout * 1000);
        }
 
        if (jail_running) {
                DEBUG("forwarding signal %d to the jailed process\n", signo);
                kill(jail_process.pid, signo);
+               /* set timeout to send SIGKILL jail process in case SIGTERM doesn't succeed */
+               if (signo == SIGTERM)
+                       uloop_timeout_set(&jail_process_timeout, opts.term_timeout * 1000);
        }
 }
 
@@ -1085,7 +1144,7 @@ static void signals_init(void)
 
                if (!sigismember(&sigmask, i))
                        continue;
-               if ((i == SIGCHLD) || (i == SIGPIPE) || (i == SIGSEGV))
+               if ((i == SIGCHLD) || (i == SIGPIPE) || (i == SIGSEGV) || (i == SIGSTOP) || (i == SIGKILL))
                        continue;
 
                s.sa_handler = jail_handle_signal;
@@ -1267,7 +1326,7 @@ static void post_start_hook(void)
        exit(EXIT_FAILURE);
 }
 
-static int ns_open_pid(const char *nstype, const pid_t target_ns)
+int ns_open_pid(const char *nstype, const pid_t target_ns)
 {
        char pid_pid_path[PATH_MAX];
 
@@ -1276,26 +1335,6 @@ static int ns_open_pid(const char *nstype, const pid_t target_ns)
        return open(pid_pid_path, O_RDONLY);
 }
 
-static void netns_updown(pid_t pid, bool start)
-{
-       static struct blob_buf req;
-       uint32_t id;
-
-       if (!parent_ctx)
-               return;
-
-       blob_buf_init(&req, 0);
-       blobmsg_add_string(&req, "jail", opts.name);
-       blobmsg_add_u32(&req, "pid", pid);
-       blobmsg_add_u8(&req, "start", start);
-
-       if (ubus_lookup_id(parent_ctx, "network", &id) ||
-           ubus_invoke(parent_ctx, id, "netns_updown", req.head, NULL, NULL, 3000))
-               INFO("ubus request failed\n");
-
-       blob_buf_free(&req);
-}
-
 static int parseOCIenvarray(struct blob_attr *msg, char ***envp)
 {
        struct blob_attr *cur;
@@ -1768,6 +1807,8 @@ static int resolve_nstype(char *type) {
                return CLONE_NEWPID;
        else if (!strcmp("network", type))
                return CLONE_NEWNET;
+       else if (!strcmp("net", type))
+               return CLONE_NEWNET;
        else if (!strcmp("mount", type))
                return CLONE_NEWNS;
        else if (!strcmp("ipc", type))
@@ -1839,6 +1880,67 @@ static int parseOCIlinuxns(struct blob_attr *msg)
        return 0;
 }
 
+/*
+ * join namespace of existing PID
+ * The string argument is the reference PID followed by ':' and a
+ * ',' separated list of namespaces to to join.
+ */
+static int jail_join_ns(char *arg)
+{
+       pid_t pid;
+       int fd;
+       int nstype;
+       char *tmp, *etmp, *nspath;
+       int *setns;
+
+       tmp = strchr(arg, ':');
+       if (!tmp)
+               return EINVAL;
+
+       *tmp = '\0';
+       pid = atoi(arg);
+
+       do {
+               ++tmp;
+               etmp = strchr(tmp, ',');
+               if (etmp)
+                       *etmp = '\0';
+
+               nstype = resolve_nstype(tmp);
+               if (!nstype)
+                       return EINVAL;
+
+               if (opts.namespace & nstype)
+                       return ENOTUNIQ;
+
+               setns = get_namespace_fd(nstype);
+
+               if (!setns)
+                       return EFAULT;
+
+               if (*setns != -1)
+                       return ENOTUNIQ;
+
+               if (asprintf(&nspath, "/proc/%d/ns/%s", pid, tmp) < 0)
+                       return ENOMEM;
+
+               fd = open(nspath, O_RDONLY);
+               free(nspath);
+
+               if (fd < 0)
+                       return errno?:ESTALE;
+
+               *setns = fd;
+
+               if (etmp)
+                       tmp = etmp;
+               else
+                       tmp = NULL;
+       } while (tmp);
+
+       return 0;
+}
+
 static void get_jail_root_user(bool is_gidmap, uint32_t container_id, uint32_t host_id, uint32_t size)
 {
        if (container_id == 0 && size >= 1)
@@ -2161,21 +2263,24 @@ static int parseOCIlinux(struct blob_attr *msg)
        if (tb[OCI_LINUX_CGROUPSPATH]) {
                cgpath = blobmsg_get_string(tb[OCI_LINUX_CGROUPSPATH]);
                if (cgpath[0] == '/') {
-                       if (strlen(cgpath) >= (sizeof(cgfullpath) - strlen(cgfullpath)))
+                       if (strlen(cgpath) + 1 >= (sizeof(cgfullpath) - strlen(cgfullpath)))
                                return E2BIG;
 
                        strcat(cgfullpath, cgpath);
                } else {
                        strcat(cgfullpath, "/containers/");
-                       strcat(cgfullpath, opts.name); /* should be container name rather than jail name */
-                       strcat(cgfullpath, "/");
-                       if (strlen(cgpath) >= (sizeof(cgfullpath) - strlen(cgfullpath)))
+                       if (strlen(opts.name) + strlen(cgpath) + 2 >= (sizeof(cgfullpath) - strlen(cgfullpath)))
                                return E2BIG;
 
+                       strcat(cgfullpath, opts.name); /* should be container name rather than jail name */
+                       strcat(cgfullpath, "/");
                        strcat(cgfullpath, cgpath);
                }
        } else {
                strcat(cgfullpath, "/containers/");
+               if (2 * strlen(opts.name) + 2 >= (sizeof(cgfullpath) - strlen(cgfullpath)))
+                       return E2BIG;
+
                strcat(cgfullpath, opts.name); /* should be container name rather than jail name */
                strcat(cgfullpath, "/");
                strcat(cgfullpath, opts.name); /* should be container instance name rather than jail name */
@@ -2469,6 +2574,12 @@ static int pidns_fd;
 static int timens_fd;
 #endif
 static void post_create_runtime(void);
+
+struct env_e {
+       struct list_head list;
+       char *envarg;
+};
+
 int main(int argc, char **argv)
 {
        uid_t uid = getuid();
@@ -2476,12 +2587,31 @@ int main(int argc, char **argv)
        const char ubus[] = "/var/run/ubus/ubus.sock";
        int ret = EXIT_FAILURE;
        int ch;
+       char *tmp;
+       struct list_head envl = LIST_HEAD_INIT(envl);
+       struct env_e *enve, *tmpenve;
+       unsigned short int envn = 0, envc = 0;
 
        if (uid) {
                ERROR("not root, aborting: %m\n");
                return EXIT_FAILURE;
        }
 
+       /* those are filehandlers, so -1 indicates unused */
+       opts.setns.pid = -1;
+       opts.setns.net = -1;
+       opts.setns.ns = -1;
+       opts.setns.ipc = -1;
+       opts.setns.uts = -1;
+       opts.setns.user = -1;
+       opts.setns.cgroup = -1;
+#ifdef CLONE_NEWTIME
+       opts.setns.time = -1;
+#endif
+
+       /* default 5 seconds timeout after SIGTERM before SIGKILL is sent */
+       opts.term_timeout = 5;
+
        umask(022);
        mount_list_init();
        init_library_search();
@@ -2493,6 +2623,11 @@ int main(int argc, char **argv)
                case 'd':
                        debug = atoi(optarg);
                        break;
+               case 'e':
+                       enve = calloc(1, sizeof(*enve));
+                       enve->envarg = optarg;
+                       list_add_tail(&enve->list, &envl);
+                       break;
                case 'p':
                        opts.namespace |= CLONE_NEWNS;
                        opts.procfs = 1;
@@ -2534,13 +2669,28 @@ int main(int argc, char **argv)
                        opts.namespace |= CLONE_NEWUTS;
                        opts.hostname = strdup(optarg);
                        break;
+               case 'j':
+                       jail_join_ns(optarg);
+                       break;
                case 'r':
                        opts.namespace |= CLONE_NEWNS;
-                       add_path_and_deps(optarg, 1, 0, 0);
+                       tmp = strchr(optarg, ':');
+                       if (tmp) {
+                               *(tmp++) = '\0';
+                               add_2paths_and_deps(optarg, tmp, 1, 0, 0);
+                       } else {
+                               add_path_and_deps(optarg, 1, 0, 0);
+                       }
                        break;
                case 'w':
                        opts.namespace |= CLONE_NEWNS;
-                       add_path_and_deps(optarg, 0, 0, 0);
+                       tmp = strchr(optarg, ':');
+                       if (tmp) {
+                               *(tmp++) = '\0';
+                               add_2paths_and_deps(optarg, tmp, 0, 0, 0);
+                       } else {
+                               add_path_and_deps(optarg, 0, 0, 0);
+                       }
                        break;
                case 'u':
                        opts.namespace |= CLONE_NEWNS;
@@ -2559,6 +2709,9 @@ int main(int argc, char **argv)
                case 'O':
                        opts.overlaydir = realpath(optarg, NULL);
                        break;
+               case 't':
+                       opts.term_timeout = atoi(optarg);
+                       break;
                case 'T':
                        opts.tmpoverlaysize = optarg;
                        break;
@@ -2583,17 +2736,39 @@ int main(int argc, char **argv)
        if (opts.namespace && !opts.ocibundle)
                opts.namespace |= CLONE_NEWIPC | CLONE_NEWPID;
 
-       /* those are filehandlers, so -1 indicates unused */
-       opts.setns.pid = -1;
-       opts.setns.net = -1;
-       opts.setns.ns = -1;
-       opts.setns.ipc = -1;
-       opts.setns.uts = -1;
-       opts.setns.user = -1;
-       opts.setns.cgroup = -1;
-#ifdef CLONE_NEWTIME
-       opts.setns.time = -1;
-#endif
+       /*
+        * env import from cmdline is not available for OCI containers
+        */
+       if (opts.ocibundle && !list_empty(&envl)) {
+               ret=-ENOTSUP;
+               goto errout;
+       }
+
+       /*
+        * prepare list of env variables to import for slim containers
+        */
+       if (!list_empty(&envl)) {
+               list_for_each_entry(enve, &envl, list)
+                       ++envn;
+
+               opts.envp = calloc(1 + envn, sizeof(char*));
+               list_for_each_entry_safe(enve, tmpenve, &envl, list) {
+                       tmp = getenv(enve->envarg);
+                       if (tmp) {
+                               ret = asprintf(&opts.envp[envc++], "%s=%s", enve->envarg, tmp);
+                               if (ret < 0) {
+                                       ERROR("filed to handle envargs %s\n", tmp);
+                                       free(enve);
+                                       goto errout;
+                               }
+                       }
+
+                       list_del(&enve->list);
+                       free(enve);
+               }
+
+               opts.envp[envc] = NULL;
+       }
 
        /*
         * uid in parent user namespace representing root user in new
@@ -2662,7 +2837,13 @@ int main(int argc, char **argv)
                ret=EXIT_FAILURE;
                goto errout;
        }
-       if (!(opts.ocibundle||opts.namespace||opts.capabilities||opts.seccomp)) {
+       if (!(opts.ocibundle||opts.namespace||opts.capabilities||opts.seccomp||
+               (opts.setns.net != -1) ||
+               (opts.setns.ns != -1) ||
+               (opts.setns.ipc != -1) ||
+               (opts.setns.uts != -1) ||
+               (opts.setns.user != -1) ||
+               (opts.setns.cgroup != -1))) {
                ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
                usage();
                ret=EXIT_FAILURE;
@@ -2698,7 +2879,7 @@ int main(int argc, char **argv)
        /* deliberately not using 'else' on unrelated conditional branches */
        if (!opts.ocibundle) {
                /* allocate NULL-terminated array for argv */
-               opts.jail_argv = calloc(1 + argc - optind, sizeof(char**));
+               opts.jail_argv = calloc(1 + argc - optind, sizeof(void *));
                if (!opts.jail_argv) {
                        ret=EXIT_FAILURE;
                        goto errout;
@@ -2763,18 +2944,19 @@ static void post_main(struct uloop_timeout *t)
                        if (!opts.extroot)
                                add_mount_bind("/etc/nsswitch.conf", 1, -1);
 #endif
+                       if (opts.setns.ns == -1) {
+                               if (!(opts.namespace & CLONE_NEWNET)) {
+                                       add_mount_bind("/etc/resolv.conf", 1, 0);
+                               } else {
+                                       /* new mount namespace to provide /dev/resolv.conf.d */
+                                       char hostdir[PATH_MAX];
 
-                       if (!(opts.namespace & CLONE_NEWNET)) {
-                               add_mount_bind("/etc/resolv.conf", 1, 0);
-                       } else if (opts.setns.ns == -1) {
-                               /* new mount namespace to provide /dev/resolv.conf.d */
-                               char hostdir[PATH_MAX];
-
-                               snprintf(hostdir, PATH_MAX, "/tmp/resolv.conf-%s.d", opts.name);
-                               mkdir_p(hostdir, 0755);
-                               add_mount(hostdir, "/dev/resolv.conf.d", NULL, MS_BIND | MS_NOEXEC | MS_NOATIME | MS_NOSUID | MS_NODEV | MS_RDONLY, 0, NULL, 0);
+                                       snprintf(hostdir, PATH_MAX, "/tmp/resolv.conf-%s.d", opts.name);
+                                       mkdir_p(hostdir, 0755);
+                                       add_mount(hostdir, "/dev/resolv.conf.d", NULL,
+                                               MS_BIND | MS_NOEXEC | MS_NOATIME | MS_NOSUID | MS_NODEV | MS_RDONLY, 0, NULL, 0);
+                               }
                        }
-
                        /* default mounts */
                        add_mount(NULL, "/dev", "tmpfs", MS_NOATIME | MS_NOEXEC | MS_NOSUID, 0, "size=1M", -1);
                        add_mount(NULL, "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, 0, "newinstance,ptmxmode=0666,mode=0620,gid=5", 0);
@@ -2912,10 +3094,8 @@ static void post_main(struct uloop_timeout *t)
                        }
                }
 
-               if (opts.namespace & CLONE_NEWNET) {
-                       netns_fd = ns_open_pid("net", jail_process.pid);
-                       netns_updown(jail_process.pid, true);
-               }
+               if (opts.namespace & CLONE_NEWNET)
+                       jail_network_start(parent_ctx, opts.name, jail_process.pid);
 
                if (jail_writepid(jail_process.pid)) {
                        ERROR("failed to write pidfile: %m\n");
@@ -2981,7 +3161,7 @@ static void post_poststop(void);
 static void poststop(void) {
        if (opts.namespace & CLONE_NEWNET) {
                setns(netns_fd, CLONE_NEWNET);
-               netns_updown(getpid(), false);
+               jail_network_stop();
                close(netns_fd);
        }
        run_hooks(opts.hooks.poststop, post_poststop);