jail: correctly check for null pointer
[project/procd.git] / jail / jail.c
index ede09442487f25df33345b91de5a9357c941cde6..31b64e5ddb565e83378dd01cc7f46e8e899505bd 100644 (file)
@@ -31,6 +31,7 @@
 #define RLIM_NLIMITS 16
 #endif
 
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
@@ -42,6 +43,7 @@
 #include <linux/filter.h>
 #include <linux/limits.h>
 #include <linux/nsfs.h>
+#include <linux/securebits.h>
 #include <signal.h>
 #include <inttypes.h>
 
 #include "log.h"
 #include "seccomp-oci.h"
 #include "cgroups.h"
+#include "netifd.h"
 
-#include <libubox/utils.h>
 #include <libubox/blobmsg.h>
 #include <libubox/blobmsg_json.h>
 #include <libubox/list.h>
 #include <libubox/vlist.h>
 #include <libubox/uloop.h>
+#include <libubox/utils.h>
 #include <libubus.h>
 
 #ifndef CLONE_NEWCGROUP
@@ -66,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:i"
+#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"
 
@@ -107,6 +110,7 @@ static struct {
        char **envp;
        char *uidmap;
        char *gidmap;
+       char *pidfile;
        struct sysctl_val **sysctl;
        int no_new_privs;
        int namespace;
@@ -129,6 +133,7 @@ static struct {
        int pw_uid;
        int pw_gid;
        int gr_gid;
+       int root_map_uid;
        gid_t *additional_gids;
        size_t num_additional_gids;
        mode_t umask;
@@ -148,6 +153,7 @@ static struct {
        char *ocibundle;
        bool immediately;
        struct blob_attr *annotations;
+       int term_timeout;
 } opts;
 
 static struct blob_buf ocibuf;
@@ -178,28 +184,30 @@ return ((opts.setns.pid != -1) ||
        opts.namespace);
 }
 
+static void free_oci_envp(char **p) {
+       char **tmp;
+
+       if (p) {
+               tmp = p;
+               while (*tmp)
+                       free(*(tmp++));
+
+               free(p);
+       }
+}
+
 static void free_hooklist(struct hook_execvpe **hooklist)
 {
        struct hook_execvpe *cur;
-       char **tmp;
 
        if (!hooklist)
                return;
 
        cur = *hooklist;
        while (cur) {
+               free_oci_envp(cur->argv);
+               free_oci_envp(cur->envp);
                free(cur->file);
-               tmp = cur->argv;
-               while (tmp)
-                       free(*(tmp++));
-
-               free(cur->argv);
-
-               tmp = cur->envp;
-               while (tmp)
-                       free(*(tmp++));
-
-               free(cur->envp);
                free(cur++);
        }
        free(hooklist);
@@ -207,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) {
@@ -240,7 +252,10 @@ static void free_rlimits(void) {
 }
 
 static void free_opts(bool parent) {
-       char **tmp;
+
+       free_library_search();
+       mount_free();
+       cgroups_free();
 
        /* we need to keep argv, envp and seccomp filter in child */
        if (parent) { /* parent-only */
@@ -249,20 +264,8 @@ static void free_opts(bool parent) {
                        free(opts.ociseccomp);
                }
 
-               tmp = opts.jail_argv;
-               while(tmp)
-                       free(*(tmp++));
-
-               free(opts.jail_argv);
-
-               tmp = opts.envp;
-               while (tmp)
-                       free(*(tmp++));
-
-               free(opts.envp);
-       } else { /* child-only */
-               if (opts.ocibundle)
-                       cgroups_free();
+               free_oci_envp(opts.jail_argv);
+               free_oci_envp(opts.envp);
        }
 
        free_rlimits();
@@ -270,11 +273,11 @@ static void free_opts(bool parent) {
        free_devices();
        free(opts.hostname);
        free(opts.cwd);
-       free(opts.extroot);
        free(opts.uidmap);
        free(opts.gidmap);
        free(opts.annotations);
-       free(opts.ocibundle);
+       free(opts.extroot);
+       free(opts.overlaydir);
        free_hooklist(opts.hooks.createRuntime);
        free_hooklist(opts.hooks.createContainer);
        free_hooklist(opts.hooks.startContainer);
@@ -318,16 +321,16 @@ static int mount_overlay(char *jail_root, char *overlaydir) {
                goto upper_etc_printf;
 
        fd = creat(upperresolvconf, 0644);
-       if (fd == -1) {
-               ERROR("creat(%s) failed: %m\n", upperresolvconf);
-               goto upper_resolvconf_printf;
+       if (fd < 0) {
+               if (errno != EEXIST)
+                       ERROR("creat(%s) failed: %m\n", upperresolvconf);
+       } else {
+               close(fd);
        }
-       close(fd);
-
        DEBUG("mount -t overlay %s %s (%s)\n", jail_root, jail_root, optsstr);
 
        if (mount(jail_root, jail_root, "overlay", MS_NOATIME, optsstr))
-               goto opts_printf;
+               goto upper_resolvconf_printf;
 
        ret = 0;
 
@@ -375,7 +378,7 @@ static int create_dev_console(const char *jail_root)
 
        /* Open UNIX/98 virtual console */
        console_fd = posix_openpt(O_RDWR | O_NOCTTY);
-       if (console_fd == -1)
+       if (console_fd < 0)
                return -1;
 
        console_fname = ptsname(console_fd);
@@ -393,11 +396,14 @@ static int create_dev_console(const char *jail_root)
        snprintf(dev_console_path, sizeof(dev_console_path), "%s/dev/console", jail_root);
        close(creat(dev_console_path, 0620));
 
-       if (mount(console_fname, dev_console_path, NULL, MS_BIND, NULL))
+       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);
@@ -460,7 +466,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);
 
@@ -470,9 +476,6 @@ static void run_hooklist(void)
        if (!((unsigned long)s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
                hook_process_handler(&hook_process, EPERM);
 
-       if (!((unsigned long)s.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)))
-               hook_process_handler(&hook_process, EPERM);
-
        hook_running = 1;
        hook_process.pid = fork();
        if (hook_process.pid == 0) {
@@ -522,8 +525,7 @@ static int apply_sysctl(const char *jail_root)
        if (!opts.sysctl)
                return 0;
 
-       asprintf(&procdir, "%s/proc", jail_root);
-       if (!procdir)
+       if (asprintf(&procdir, "%s/proc", jail_root) < 0)
                return ENOMEM;
 
        mkdir(procdir, 0700);
@@ -533,18 +535,23 @@ static int apply_sysctl(const char *jail_root)
        cur = opts.sysctl;
 
        while (*cur) {
-               asprintf(&fname, "%s/sys/%s", procdir, (*cur)->entry);
-               if (!fname)
+               if (asprintf(&fname, "%s/sys/%s", procdir, (*cur)->entry) < 0)
                        return ENOMEM;
 
                DEBUG("sysctl: writing '%s' to %s\n", (*cur)->value, fname);
 
                f = open(fname, O_WRONLY);
-               if (f == -1) {
+               if (f < 0) {
                        ERROR("sysctl: can't open %s\n", fname);
+                       free(fname);
+                       return errno;
+               }
+               if (write(f, (*cur)->value, strlen((*cur)->value)) < 0) {
+                       ERROR("sysctl: write to %s\n", fname);
+                       free(fname);
+                       close(f);
                        return errno;
                }
-               write(f, (*cur)->value, strlen((*cur)->value));
 
                free(fname);
                close(f);
@@ -581,6 +588,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;
@@ -588,12 +597,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;
@@ -615,11 +645,25 @@ only_default_devices:
        }
 
        /* Dev symbolic links as defined in OCI spec */
-       symlink("/dev/pts/ptmx", "/dev/ptmx");
-       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");
+       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;
 }
@@ -631,6 +675,7 @@ static void enter_jail_fs(void);
 static int build_jail_fs(void)
 {
        char *overlaydir = NULL;
+       int ret;
 
        old_umask = umask(0);
 
@@ -645,13 +690,13 @@ static int build_jail_fs(void)
        }
 
        /* oldroot can't be MS_SHARED else pivot_root() fails */
-       if (mount("none", "/", NULL, MS_REC|MS_PRIVATE, NULL)) {
+       if (mount("none", "/", "none", MS_REC|MS_PRIVATE, NULL)) {
                ERROR("private mount failed %m\n");
                return -1;
        }
 
        if (opts.extroot) {
-               if (mount(opts.extroot, jail_root, NULL, MS_BIND, NULL)) {
+               if (mount(opts.extroot, jail_root, "bind", MS_BIND, NULL)) {
                        ERROR("extroot mount failed %m\n");
                        return -1;
                }
@@ -682,8 +727,11 @@ static int build_jail_fs(void)
        if (opts.overlaydir)
                overlaydir = opts.overlaydir;
 
-       if (overlaydir)
-               mount_overlay(jail_root, overlaydir);
+       if (overlaydir) {
+               ret = mount_overlay(jail_root, overlaydir);
+               if (ret)
+                       return ret;
+       }
 
        if (chdir(jail_root)) {
                ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root);
@@ -708,7 +756,9 @@ static int build_jail_fs(void)
                if (overlaydir)
                        unlink(jaillink);
 
-               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);
@@ -716,6 +766,20 @@ static int build_jail_fs(void)
        return 0;
 }
 
+static bool exit_from_child;
+static void free_and_exit(int ret)
+{
+       if (!exit_from_child && opts.ocibundle)
+               cgroups_free();
+
+       if (!exit_from_child && parent_ctx)
+               ubus_free(parent_ctx);
+
+       free_opts(!exit_from_child);
+
+       exit(ret);
+}
+
 static void post_jail_fs(void);
 static void enter_jail_fs(void)
 {
@@ -726,11 +790,11 @@ static void enter_jail_fs(void)
 
        if (pivot_root(jail_root, dirbuf) == -1) {
                ERROR("pivot_root(%s, %s) failed: %m\n", jail_root, dirbuf);
-               exit(-1);
+               free_and_exit(-1);
        }
        if (chdir("/")) {
                ERROR("chdir(/) (after pivot_root) failed: %m\n");
-               exit(-1);
+               free_and_exit(-1);
        }
 
        snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
@@ -748,10 +812,10 @@ static void enter_jail_fs(void)
 
        if (create_devices()) {
                ERROR("create_devices() failed\n");
-               exit(-1);
+               free_and_exit(-1);
        }
        if (opts.ronly)
-               mount(NULL, "/", NULL, MS_REMOUNT | MS_BIND | MS_RDONLY, 0);
+               mount(NULL, "/", "bind", MS_REMOUNT | MS_BIND | MS_RDONLY, 0);
 
        umask(old_umask);
        post_jail_fs();
@@ -766,7 +830,7 @@ static int write_uid_gid_map(pid_t child_pid, bool gidmap, char *mapstr)
                child_pid, gidmap?"gid_map":"uid_map") < 0)
                return -1;
 
-       if ((map_file = open(map_path, O_WRONLY)) == -1)
+       if ((map_file = open(map_path, O_WRONLY)) < 0)
                return -1;
 
        if (dprintf(map_file, "%s", mapstr)) {
@@ -775,7 +839,6 @@ static int write_uid_gid_map(pid_t child_pid, bool gidmap, char *mapstr)
        }
 
        close(map_file);
-       free(mapstr);
        return 0;
 }
 
@@ -788,10 +851,10 @@ static int write_single_uid_gid_map(pid_t child_pid, bool gidmap, int id)
                child_pid, gidmap?"gid_map":"uid_map") < 0)
                return -1;
 
-       if ((map_file = open(map_path, O_WRONLY)) == -1)
+       if ((map_file = open(map_path, O_WRONLY)) < 0)
                return -1;
 
-       if (dprintf(map_file, map_format, 0, id, 1) == -1) {
+       if (dprintf(map_file, map_format, 0, id, 1) < 0) {
                close(map_file);
                return -1;
        }
@@ -810,7 +873,7 @@ static int write_setgroups(pid_t child_pid, bool allow)
                return -1;
        }
 
-       if ((setgroups_file = open(setgroups_path, O_WRONLY)) == -1) {
+       if ((setgroups_file = open(setgroups_path, O_WRONLY)) < 0) {
                return -1;
        }
 
@@ -833,7 +896,7 @@ static void get_jail_user(int *user, int *user_gid, int *gr_gid)
                if (!p) {
                        ERROR("failed to get uid/gid for user %s: %d (%s)\n",
                              opts.user, errno, strerror(errno));
-                       exit(EXIT_FAILURE);
+                       free_and_exit(EXIT_FAILURE);
                }
                *user = p->pw_uid;
                *user_gid = p->pw_gid;
@@ -846,7 +909,7 @@ static void get_jail_user(int *user, int *user_gid, int *gr_gid)
                g = getgrnam(opts.group);
                if (!g) {
                        ERROR("failed to get gid for group %s: %m\n", opts.group);
-                       exit(EXIT_FAILURE);
+                       free_and_exit(EXIT_FAILURE);
                }
                *gr_gid = g->gr_gid;
        } else {
@@ -858,17 +921,17 @@ static void set_jail_user(int pw_uid, int user_gid, int gr_gid)
 {
        if (opts.user && (user_gid != -1) && initgroups(opts.user, user_gid)) {
                ERROR("failed to initgroups() for user %s: %m\n", opts.user);
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        }
 
        if ((gr_gid != -1) && setregid(gr_gid, gr_gid)) {
                ERROR("failed to set group id %d: %m\n", gr_gid);
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        }
 
        if ((pw_uid != -1) && setreuid(pw_uid, pw_uid)) {
                ERROR("failed to set user id %d: %m\n", pw_uid);
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        }
 }
 
@@ -888,12 +951,13 @@ static int apply_rlimits(void)
        return 0;
 }
 
-#define MAX_ENVP       8
+#define MAX_ENVP       64
 static char** build_envp(const char *seccomp, char **ocienvp)
 {
        static char *envp[MAX_ENVP];
        static char preload_var[PATH_MAX];
        static char seccomp_var[PATH_MAX];
+       static char seccomp_debug_var[20];
        static char debug_var[] = "LD_DEBUG=all";
        static char container_var[] = "container=ujail";
        const char *preload_lib = find_lib("libpreload-seccomp.so");
@@ -908,6 +972,8 @@ static char** build_envp(const char *seccomp, char **ocienvp)
        if (seccomp) {
                snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
                envp[count++] = seccomp_var;
+               snprintf(seccomp_debug_var, sizeof(seccomp_debug_var), "SECCOMP_DEBUG=%2d", debug);
+               envp[count++] = seccomp_debug_var;
                snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
                envp[count++] = preload_var;
        }
@@ -936,6 +1002,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");
@@ -956,7 +1023,8 @@ static void usage(void)
        fprintf(stderr, "  -E\t\tfail if jail cannot be setup\n");
        fprintf(stderr, "  -y\t\tprovide jail console\n");
        fprintf(stderr, "  -J <dir>\tcreate container from OCI bundle\n");
-       fprintf(stderr, "  -j\t\tstart container immediately\n");
+       fprintf(stderr, "  -i\t\tstart container immediately\n");
+       fprintf(stderr, "  -P <pidfile>\tcreate <pidfile>\n");
        fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
 and he has the same powers as root outside the jail,\n\
 thus he can escape the jail and/or break stuff.\n\
@@ -996,10 +1064,9 @@ static int setns_open(unsigned long nstype)
 {
        int *fd = get_namespace_fd(nstype);
 
-       if (!*fd)
-               return EFAULT;
+       assert(fd != NULL);
 
-       if (*fd == -1)
+       if (*fd < 0)
                return 0;
 
        if (setns(*fd, nstype) == -1) {
@@ -1048,11 +1115,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);
        }
 }
 
@@ -1067,7 +1140,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;
@@ -1085,6 +1158,9 @@ static int exec_jail(void *arg)
 {
        char buf[1];
 
+       exit_from_child = true;
+       prctl(PR_SET_SECUREBITS, 0);
+
        uloop_init();
        signals_init();
 
@@ -1115,38 +1191,41 @@ static int exec_jail(void *arg)
        if (opts.namespace & CLONE_NEWCGROUP)
                unshare(CLONE_NEWCGROUP);
 
+       setns_open(CLONE_NEWCGROUP);
+
        if ((opts.namespace & CLONE_NEWUSER) || (opts.setns.user != -1)) {
                if (setregid(0, 0) < 0) {
                        ERROR("setgid\n");
-                       exit(EXIT_FAILURE);
+                       free_and_exit(EXIT_FAILURE);
                }
                if (setreuid(0, 0) < 0) {
                        ERROR("setuid\n");
-                       exit(EXIT_FAILURE);
+                       free_and_exit(EXIT_FAILURE);
                }
                if (setgroups(0, NULL) < 0) {
                        ERROR("setgroups\n");
-                       exit(EXIT_FAILURE);
+                       free_and_exit(EXIT_FAILURE);
                }
        }
 
        if (opts.namespace && opts.hostname && strlen(opts.hostname) > 0
                        && sethostname(opts.hostname, strlen(opts.hostname))) {
                ERROR("sethostname(%s) failed: %m\n", opts.hostname);
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        }
 
        uloop_timeout_add(&pre_exec_timeout);
        uloop_run();
 
-       exit(-1);
+       free_and_exit(-1);
+       return -1;
 }
 
 static void pre_exec_jail(struct uloop_timeout *t)
 {
        if ((opts.namespace & CLONE_NEWNS) && build_jail_fs()) {
                ERROR("failed to build jail fs\n");
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        } else {
                run_hooks(opts.hooks.createContainer, post_jail_fs);
        }
@@ -1159,11 +1238,11 @@ static void post_jail_fs(void)
 
        if (read(pipes[2], buf, 1) < 1) {
                ERROR("can't read from parent\n");
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        }
        if (buf[0] != '!') {
                ERROR("parent had an error, child exiting\n");
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        }
        close(pipes[2]);
 
@@ -1172,39 +1251,63 @@ static void post_jail_fs(void)
 
 static void post_start_hook(void)
 {
-       if (applyOCIcapabilities(opts.capset))
-               exit(EXIT_FAILURE);
+       int pw_uid, pw_gid, gr_gid;
+
+       /*
+        * make sure setuid/setgid won't drop capabilities in case capabilities
+        * have been specified explicitely.
+        */
+       if (opts.capset.apply) {
+               if (prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP)) {
+                       ERROR("prctl(PR_SET_SECUREBITS) failed: %m\n");
+                       free_and_exit(EXIT_FAILURE);
+               }
+       }
 
-       if (!(opts.namespace & CLONE_NEWUSER) && (opts.setns.user == -1)) {
-               int pw_uid, pw_gid, gr_gid;
-               get_jail_user(&pw_uid, &pw_gid, &gr_gid);
+       /* drop capabilities, retain those still needed to further setup jail */
+       if (applyOCIcapabilities(opts.capset, (1LLU << CAP_SETGID) | (1LLU << CAP_SETUID) | (1LLU << CAP_SETPCAP)))
+               free_and_exit(EXIT_FAILURE);
 
-               set_jail_user(opts.pw_uid?:pw_uid, opts.pw_gid?:pw_gid, opts.gr_gid?:gr_gid);
-       }
+       /* use either cmdline-supplied user/group or uid/gid from OCI spec */
+       get_jail_user(&pw_uid, &pw_gid, &gr_gid);
+       set_jail_user(opts.pw_uid?:pw_uid, opts.pw_gid?:pw_gid, opts.gr_gid?:gr_gid);
 
        if (opts.additional_gids &&
            (setgroups(opts.num_additional_gids, opts.additional_gids) < 0)) {
                ERROR("setgroups failed: %m\n");
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        }
 
        if (opts.set_umask)
                umask(opts.umask);
 
+       /* restore securebits back to normal (and lock them if not in userns) */
+       if (opts.capset.apply) {
+               if (prctl(PR_SET_SECUREBITS, (opts.namespace & CLONE_NEWUSER)?0:
+                   SECBIT_KEEP_CAPS_LOCKED|SECBIT_NO_SETUID_FIXUP_LOCKED|SECBIT_NOROOT_LOCKED)) {
+                       ERROR("prctl(PR_SET_SECUREBITS) failed: %m\n");
+                       free_and_exit(EXIT_FAILURE);
+               }
+       }
+
+       /* drop remaining capabilities to end up with specified sets */
+       if (applyOCIcapabilities(opts.capset, 0))
+               free_and_exit(EXIT_FAILURE);
+
        if (opts.no_new_privs && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
-                ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
-               exit(EXIT_FAILURE);
+               ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
+               free_and_exit(EXIT_FAILURE);
        }
 
        char **envp = build_envp(opts.seccomp, opts.envp);
        if (!envp)
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
 
        if (opts.cwd && chdir(opts.cwd))
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
 
        if (opts.ociseccomp && applyOCIlinuxseccomp(opts.ociseccomp))
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
 
        uloop_end();
        free_opts(false);
@@ -1219,7 +1322,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];
 
@@ -1228,26 +1331,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;
@@ -1288,27 +1371,39 @@ static const struct blobmsg_policy oci_root_policy[] = {
 
 static int parseOCIroot(const char *jsonfile, struct blob_attr *msg)
 {
-       static char rootpath[PATH_MAX] = { 0 };
+       char extroot[PATH_MAX] = { 0 };
        struct blob_attr *tb[__OCI_ROOT_MAX];
        char *cur;
+       char *root_path;
 
        blobmsg_parse(oci_root_policy, __OCI_ROOT_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
 
        if (!tb[OCI_ROOT_PATH])
                return ENODATA;
 
-       strncpy(rootpath, jsonfile, PATH_MAX);
-       cur = strrchr(rootpath, '/');
+       root_path = blobmsg_get_string(tb[OCI_ROOT_PATH]);
+
+       /* prepend bundle directory in case of relative paths */
+       if (root_path[0] != '/') {
+               strncpy(extroot, jsonfile, PATH_MAX - 1);
 
-       if (!cur)
-               return ENOTDIR;
+               cur = strrchr(extroot, '/');
 
-       *(++cur) = '\0';
-       strncat(rootpath, blobmsg_get_string(tb[OCI_ROOT_PATH]), PATH_MAX - (strlen(rootpath) + 1));
+               if (!cur)
+                       return ENOTDIR;
 
-       opts.extroot = rootpath;
+               *(++cur) = '\0';
+       }
+
+       strncat(extroot, root_path, PATH_MAX - (strlen(extroot) + 1));
+
+       /* follow symbolic link(s) */
+       opts.extroot = realpath(extroot, NULL);
+       if (!opts.extroot)
+               return errno;
 
-       opts.ronly = blobmsg_get_bool(tb[OCI_ROOT_READONLY]);
+       if (tb[OCI_ROOT_READONLY])
+               opts.ronly = blobmsg_get_bool(tb[OCI_ROOT_READONLY]);
 
        return 0;
 }
@@ -1654,8 +1749,11 @@ static int parseOCIprocess(struct blob_attr *msg)
        if (res)
                return res;
 
-       opts.console = blobmsg_get_bool(tb[OCI_PROCESS_TERMINAL]);
-       opts.no_new_privs = blobmsg_get_bool(tb[OCI_PROCESS_NONEWPRIVILEGES]);
+       if (tb[OCI_PROCESS_TERMINAL])
+               opts.console = blobmsg_get_bool(tb[OCI_PROCESS_TERMINAL]);
+
+       if (tb[OCI_PROCESS_NONEWPRIVILEGES])
+               opts.no_new_privs = blobmsg_get_bool(tb[OCI_PROCESS_NONEWPRIVILEGES]);
 
        if (tb[OCI_PROCESS_CWD])
                opts.cwd = strdup(blobmsg_get_string(tb[OCI_PROCESS_CWD]));
@@ -1705,6 +1803,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))
@@ -1756,11 +1856,13 @@ static int parseOCIlinuxns(struct blob_attr *msg)
                        blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_PATH]));
 
                fd = open(blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_PATH]), O_RDONLY);
-               if (fd == -1)
+               if (fd < 0)
                        return errno?:ESTALE;
 
-               if (ioctl(fd, NS_GET_NSTYPE) != nstype)
+               if (ioctl(fd, NS_GET_NSTYPE) != nstype) {
+                       close(fd);
                        return EINVAL;
+               }
 
                DEBUG("opened existing %s namespace got filehandler %u\n",
                        blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]),
@@ -1772,8 +1874,75 @@ 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)
+               if (!is_gidmap)
+                       opts.root_map_uid = host_id;
+}
 
 enum {
        OCI_LINUX_UIDGIDMAP_CONTAINERID,
@@ -1790,30 +1959,12 @@ static const struct blobmsg_policy oci_linux_uidgidmap_policy[] = {
 
 static int parseOCIuidgidmappings(struct blob_attr *msg, bool is_gidmap)
 {
-       const char *map_format = "%d %d %d\n";
        struct blob_attr *tb[__OCI_LINUX_UIDGIDMAP_MAX];
        struct blob_attr *cur;
-       int rem, len;
-       char **mappings;
-       char *map, *curstr;
-       unsigned int cnt = 0;
-       size_t totallen = 0;
-
-       /* count number of mappings */
-       blobmsg_for_each_attr(cur, msg, rem)
-               cnt++;
-
-       if (!cnt)
-               return 0;
-
-       /* allocate array for mappings */
-       mappings = calloc(1 + cnt, sizeof(char*));
-       if (!mappings)
-               return ENOMEM;
-
-       mappings[cnt] = NULL;
+       int rem;
+       char *map;
+       size_t len, pos, totallen = 0;
 
-       cnt = 0;
        blobmsg_for_each_attr(cur, msg, rem) {
                blobmsg_parse(oci_linux_uidgidmap_policy, __OCI_LINUX_UIDGIDMAP_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
 
@@ -1822,32 +1973,36 @@ static int parseOCIuidgidmappings(struct blob_attr *msg, bool is_gidmap)
                    !tb[OCI_LINUX_UIDGIDMAP_SIZE])
                        return EINVAL;
 
-               /* write mapping line into allocated string */
-               len = asprintf(&mappings[cnt++], map_format,
+               /* count length */
+               totallen += snprintf(NULL, 0, "%d %d %d\n",
                         blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_CONTAINERID]),
                         blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_HOSTID]),
                         blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_SIZE]));
-
-               if (len < 0)
-                       return ENOMEM;
-
-               totallen += len;
        }
 
        /* allocate combined mapping string */
-       map = calloc(1 + totallen, sizeof(char));
+       map = malloc(totallen + 1);
        if (!map)
                return ENOMEM;
 
-       map[0] = '\0';
+       pos = 0;
+       blobmsg_for_each_attr(cur, msg, rem) {
+               blobmsg_parse(oci_linux_uidgidmap_policy, __OCI_LINUX_UIDGIDMAP_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
+
+               get_jail_root_user(is_gidmap, blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_CONTAINERID]),
+                        blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_HOSTID]),
+                        blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_SIZE]));
 
-       /* concatenate mapping strings into combined string */
-       curstr = mappings[0];
-       while (curstr) {
-               strcat(map, curstr);
-               free(curstr++);
+               /* write mapping line into pre-allocated string */
+               len = snprintf(&map[pos], totallen + 1, "%d %d %d\n",
+                        blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_CONTAINERID]),
+                        blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_HOSTID]),
+                        blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_SIZE]));
+               pos += len;
+               totallen -= len;
        }
-       free(mappings);
+
+       assert(totallen == 0);
 
        if (is_gidmap)
                opts.gidmap = map;
@@ -1916,20 +2071,26 @@ static int parseOCIdevices(struct blob_attr *msg)
                        return ENOMEM;
 
                tmp->mode = resolve_devtype(blobmsg_get_string(tb[OCI_DEVICES_TYPE]));
-               if (!tmp->mode)
+               if (!tmp->mode) {
+                       free(tmp);
                        return EINVAL;
+               }
 
                if (tmp->mode != S_IFIFO) {
-                       if (!tb[OCI_DEVICES_MAJOR] || !tb[OCI_DEVICES_MINOR])
+                       if (!tb[OCI_DEVICES_MAJOR] || !tb[OCI_DEVICES_MINOR]) {
+                               free(tmp);
                                return ENODATA;
+                       }
 
                        tmp->dev = makedev(blobmsg_get_u32(tb[OCI_DEVICES_MAJOR]),
                                           blobmsg_get_u32(tb[OCI_DEVICES_MINOR]));
                }
 
                if (tb[OCI_DEVICES_FILEMODE]) {
-                       if (~(S_IRWXU|S_IRWXG|S_IRWXO) & blobmsg_get_u32(tb[OCI_DEVICES_FILEMODE]))
+                       if (~(S_IRWXU|S_IRWXG|S_IRWXO) & blobmsg_get_u32(tb[OCI_DEVICES_FILEMODE])) {
+                               free(tmp);
                                return EINVAL;
+                       }
 
                        tmp->mode |= blobmsg_get_u32(tb[OCI_DEVICES_FILEMODE]);
                } else {
@@ -2063,7 +2224,7 @@ static int parseOCIlinux(struct blob_attr *msg)
 
        if (tb[OCI_LINUX_READONLYPATHS]) {
                blobmsg_for_each_attr(cur, tb[OCI_LINUX_READONLYPATHS], rem) {
-                       res = add_mount(NULL, blobmsg_get_string(cur), NULL, MS_BIND | MS_REC | MS_RDONLY, NULL, 0);
+                       res = add_mount(NULL, blobmsg_get_string(cur), NULL, MS_BIND | MS_REC | MS_RDONLY, 0, NULL, 0);
                        if (res)
                                return res;
                }
@@ -2071,7 +2232,7 @@ static int parseOCIlinux(struct blob_attr *msg)
 
        if (tb[OCI_LINUX_MASKEDPATHS]) {
                blobmsg_for_each_attr(cur, tb[OCI_LINUX_MASKEDPATHS], rem) {
-                       res = add_mount((void *)(-1), blobmsg_get_string(cur), NULL, 0, NULL, 1);
+                       res = add_mount((void *)(-1), blobmsg_get_string(cur), NULL, 0, 0, NULL, 0);
                        if (res)
                                return res;
                }
@@ -2098,21 +2259,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 */
@@ -2160,53 +2324,65 @@ static int parseOCI(const char *jsonfile)
        int res;
 
        blob_buf_init(&ocibuf, 0);
-       if (!blobmsg_add_json_from_file(&ocibuf, jsonfile))
-               return ENOENT;
+
+       if (!blobmsg_add_json_from_file(&ocibuf, jsonfile)) {
+               res=ENOENT;
+               goto errout;
+       }
 
        blobmsg_parse(oci_policy, __OCI_MAX, tb, blob_data(ocibuf.head), blob_len(ocibuf.head));
 
-       if (!tb[OCI_VERSION])
-               return ENOMSG;
+       if (!tb[OCI_VERSION]) {
+               res=ENOMSG;
+               goto errout;
+       }
 
        if (strncmp("1.0", blobmsg_get_string(tb[OCI_VERSION]), 3)) {
                ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb[OCI_VERSION]));
-               return ENOTSUP;
+               res=ENOTSUP;
+               goto errout;
        }
 
        if (tb[OCI_HOSTNAME])
                opts.hostname = strdup(blobmsg_get_string(tb[OCI_HOSTNAME]));
 
-       if (!tb[OCI_PROCESS])
-               return ENODATA;
+       if (!tb[OCI_PROCESS]) {
+               res=ENODATA;
+               goto errout;
+       }
 
        if ((res = parseOCIprocess(tb[OCI_PROCESS])))
-               return res;
-
-       if (!tb[OCI_ROOT])
-               return ENODATA;
+               goto errout;
 
+       if (!tb[OCI_ROOT]) {
+               res=ENODATA;
+               goto errout;
+       }
        if ((res = parseOCIroot(jsonfile, tb[OCI_ROOT])))
-               return res;
+               goto errout;
 
-       if (!tb[OCI_MOUNTS])
-               return ENODATA;
+       if (!tb[OCI_MOUNTS]) {
+               res=ENODATA;
+               goto errout;
+       }
 
        blobmsg_for_each_attr(cur, tb[OCI_MOUNTS], rem)
                if ((res = parseOCImount(cur)))
-                       return res;
+                       goto errout;
 
        if (tb[OCI_LINUX] && (res = parseOCIlinux(tb[OCI_LINUX])))
-               return res;
+               goto errout;
 
        if (tb[OCI_HOOKS] && (res = parseOCIhooks(tb[OCI_HOOKS])))
-               return res;
+               goto errout;
 
        if (tb[OCI_ANNOTATIONS])
                opts.annotations = blob_memdup(tb[OCI_ANNOTATIONS]);
 
+errout:
        blob_buf_free(&ocibuf);
 
-       return 0;
+       return res;
 }
 
 static int set_oom_score_adj(void)
@@ -2219,7 +2395,7 @@ static int set_oom_score_adj(void)
 
        snprintf(fname, sizeof(fname), "/proc/%u/oom_score_adj", jail_process.pid);
        f = open(fname, O_WRONLY | O_TRUNC);
-       if (f == -1)
+       if (f < 0)
                return errno;
 
        dprintf(f, "%d", opts.oom_score_adj);
@@ -2334,6 +2510,41 @@ container_handle_kill(struct ubus_context *ctx, struct ubus_object *obj,
        return UBUS_STATUS_UNKNOWN_ERROR;
 }
 
+static int
+jail_writepid(pid_t pid)
+{
+       FILE *_pidfile;
+
+       if (!opts.pidfile)
+               return 0;
+
+       _pidfile = fopen(opts.pidfile, "w");
+       if (_pidfile == NULL)
+               return errno;
+
+       if (fprintf(_pidfile, "%d\n", pid) < 0) {
+               fclose(_pidfile);
+               return errno;
+       }
+
+       if (fclose(_pidfile))
+               return errno;
+
+       return 0;
+}
+
+static int checkpath(const char *path)
+{
+       int dirfd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+       if (dirfd < 0) {
+               ERROR("path %s open failed %m\n", path);
+               return -1;
+       }
+       close(dirfd);
+
+       return 0;
+}
+
 static struct ubus_method container_methods[] = {
        UBUS_METHOD_NOARG("start", handle_start),
        UBUS_METHOD_NOARG("state", handle_state),
@@ -2359,27 +2570,60 @@ 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();
        const char log[] = "/dev/log";
        const char ubus[] = "/var/run/ubus/ubus.sock";
-       int ch, ret;
+       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();
+       cgroups_prepare();
+       exit_from_child = false;
 
        while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
                switch (ch) {
                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;
@@ -2395,7 +2639,7 @@ int main(int argc, char **argv)
                        opts.namespace |= CLONE_NEWCGROUP;
                        break;
                case 'R':
-                       opts.extroot = strdup(optarg);
+                       opts.extroot = realpath(optarg, NULL);
                        break;
                case 's':
                        opts.namespace |= CLONE_NEWNS;
@@ -2421,13 +2665,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;
@@ -2444,7 +2703,10 @@ int main(int argc, char **argv)
                        opts.group = optarg;
                        break;
                case 'O':
-                       opts.overlaydir = optarg;
+                       opts.overlaydir = realpath(optarg, NULL);
+                       break;
+               case 't':
+                       opts.term_timeout = atoi(optarg);
                        break;
                case 'T':
                        opts.tmpoverlaysize = optarg;
@@ -2456,61 +2718,132 @@ int main(int argc, char **argv)
                        opts.console = 1;
                        break;
                case 'J':
-                       opts.ocibundle = strdup(optarg);
+                       opts.ocibundle = optarg;
                        break;
                case 'i':
                        opts.immediately = true;
                        break;
+               case 'P':
+                       opts.pidfile = optarg;
+                       break;
                }
        }
 
        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
+        * user namespace, defaults to nobody unless specified in uidMappings
+        */
+       opts.root_map_uid = 65534;
 
        if (opts.capabilities && parseOCIcapabilities_from_file(&opts.capset, opts.capabilities)) {
                ERROR("failed to read capabilities from file %s\n", opts.capabilities);
-               return -1;
+               ret=-1;
+               goto errout;
        }
 
        if (opts.ocibundle) {
                char *jsonfile;
                int ocires;
 
-               asprintf(&jsonfile, "%s/config.json", opts.ocibundle);
+               if (!opts.name) {
+                       ERROR("OCI bundle needs a named jail\n");
+                       ret=-1;
+                       goto errout;
+               }
+               if (asprintf(&jsonfile, "%s/config.json", opts.ocibundle) < 0) {
+                       ret=-ENOMEM;
+                       goto errout;
+               }
                ocires = parseOCI(jsonfile);
                free(jsonfile);
                if (ocires) {
                        ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires), ocires);
-                       return ocires;
+                       ret=ocires;
+                       goto errout;
+               }
+       }
+
+       if (opts.namespace & CLONE_NEWNET) {
+               if (!opts.name) {
+                       ERROR("netns needs a named jail\n");
+                       ret=-1;
+                       goto errout;
                }
        }
 
+
        if (opts.tmpoverlaysize && strlen(opts.tmpoverlaysize) > 8) {
                ERROR("size parameter too long: \"%s\"\n", opts.tmpoverlaysize);
-               return -1;
+               ret=-1;
+               goto errout;
+       }
+
+       if (opts.extroot && checkpath(opts.extroot)) {
+               ERROR("invalid rootfs path '%s'", opts.extroot);
+               ret=-1;
+               goto errout;
+       }
+
+       if (opts.overlaydir && checkpath(opts.overlaydir)) {
+               ERROR("invalid rootfs overlay path '%s'", opts.overlaydir);
+               ret=-1;
+               goto errout;
        }
 
        /* no <binary> param found */
        if (!opts.ocibundle && (argc - optind < 1)) {
                usage();
-               return EXIT_FAILURE;
-       }
-       if (!(opts.ocibundle||opts.namespace||opts.capabilities||opts.seccomp)) {
+               ret=EXIT_FAILURE;
+               goto errout;
+       }
+       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();
-               return EXIT_FAILURE;
+               ret=EXIT_FAILURE;
+               goto errout;
        }
        DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
                opts.namespace,
@@ -2525,24 +2858,28 @@ int main(int argc, char **argv)
 
        if (opts.ocibundle) {
                char *objname;
-               if (asprintf(&objname, "container.%s", opts.name) < 0)
-                       exit(-ENOMEM);
+               if (asprintf(&objname, "container.%s", opts.name) < 0) {
+                       ret=-ENOMEM;
+                       goto errout;
+               }
 
                container_object.name = objname;
                ret = ubus_add_object(parent_ctx, &container_object);
                if (ret) {
                        ERROR("Failed to add object: %s\n", ubus_strerror(ret));
-                       exit(-1);
+                       ret=-1;
+                       goto errout;
                }
        }
 
        /* 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**));
-               if (!opts.jail_argv)
-                       return EXIT_FAILURE;
-
+               opts.jail_argv = calloc(1 + argc - optind, sizeof(void *));
+               if (!opts.jail_argv) {
+                       ret=EXIT_FAILURE;
+                       goto errout;
+               }
                for (size_t s = optind; s < argc; s++)
                        opts.jail_argv[s - optind] = strdup(argv[s]);
 
@@ -2553,65 +2890,75 @@ int main(int argc, char **argv)
        if (!opts.extroot) {
                if (opts.namespace && add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
                        ERROR("failed to load dependencies\n");
-                       return -1;
+                       ret=-1;
+                       goto errout;
                }
        }
 
        if (opts.namespace && opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
                ERROR("failed to load libpreload-seccomp.so\n");
                opts.seccomp = 0;
-               if (opts.require_jail)
-                       return -1;
+               if (opts.require_jail) {
+                       ret=-1;
+                       goto errout;
+               }
        }
 
        uloop_timeout_add(&post_main_timeout);
        uloop_run();
 
-       /* unreachable */
-       return 0;
+errout:
+       if (opts.ocibundle)
+               cgroups_free();
+
+       free_opts(true);
+
+       return ret;
 }
 
 static void post_main(struct uloop_timeout *t)
 {
        if (apply_rlimits()) {
                ERROR("error applying resource limits\n");
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        }
 
        if (opts.name)
                prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
 
        if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0)
-               exit(-1);
+               free_and_exit(-1);
 
        if (has_namespaces()) {
                if (opts.namespace & CLONE_NEWNS) {
                        if (!opts.extroot && (opts.user || opts.group)) {
-                               add_mount_bind("/etc/passwd", 0, -1);
-                               add_mount_bind("/etc/group", 0, -1);
+                               add_mount_bind("/etc/passwd", 1, -1);
+                               add_mount_bind("/etc/group", 1, -1);
                        }
 
 #if defined(__GLIBC__)
                        if (!opts.extroot)
-                               add_mount_bind("/etc/nsswitch.conf", 0, -1);
+                               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", 0, -1);
-                       } else if (opts.setns.net == -1) {
-                               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, NULL, -1);
+                                       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, "size=1M", -1);
-                       add_mount(NULL, "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "newinstance,ptmxmode=0666,mode=0620,gid=5", 0);
+                       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);
 
                        if (opts.procfs || opts.ocibundle) {
-                               add_mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL, -1);
+                               add_mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0, NULL, -1);
 
                                /*
                                 * hack to make /proc/sys/net read-write while the rest of /proc/sys is read-only
@@ -2629,17 +2976,17 @@ static void post_main(struct uloop_timeout *t)
                                 * move-mount of /proc/sys/net follows because 'e' preceeds 'y' in the ASCII
                                 * table (and in the alphabet).
                                 */
-                               if (!add_mount(NULL, "/proc/sys", NULL, MS_BIND | MS_RDONLY, NULL, -1))
+                               if (!add_mount(NULL, "/proc/sys", NULL, MS_BIND | MS_RDONLY, 0, NULL, -1))
                                        if (opts.namespace & CLONE_NEWNET)
-                                               if (!add_mount_inner("/proc/self/net", "/proc/sys/net", NULL, MS_MOVE, NULL, -1))
-                                                       add_mount_inner("/proc/sys/net", "/proc/self/net", NULL, MS_BIND, NULL, -1);
+                                               if (!add_mount_inner("/proc/self/net", "/proc/sys/net", NULL, MS_MOVE, 0, NULL, -1))
+                                                       add_mount_inner("/proc/sys/net", "/proc/self/net", NULL, MS_BIND, 0, NULL, -1);
 
                        }
                        if (opts.sysfs || opts.ocibundle)
-                               add_mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, NULL, -1);
+                               add_mount("sysfs", "/sys", "sysfs", MS_RELATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, 0, NULL, -1);
 
                        if (opts.ocibundle)
-                               add_mount("shm", "/dev/shm", "tmpfs", MS_NOSUID | MS_NOEXEC | MS_NODEV, "mode=1777", -1);
+                               add_mount("shm", "/dev/shm", "tmpfs", MS_NOSUID | MS_NOEXEC | MS_NODEV, 0, "mode=1777", -1);
 
                }
 
@@ -2654,9 +3001,22 @@ static void post_main(struct uloop_timeout *t)
                if (opts.setns.time != -1) {
                        timens_fd = ns_open_pid("time", getpid());
                        setns_open(CLONE_NEWTIME);
+               } else {
+                       timens_fd = -1;
                }
 #endif
 
+               if (opts.namespace & CLONE_NEWUSER) {
+                       if (prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP)) {
+                               ERROR("prctl(PR_SET_SECUREBITS) failed: %m\n");
+                               free_and_exit(EXIT_FAILURE);
+                       }
+                       if (seteuid(opts.root_map_uid)) {
+                               ERROR("seteuid(%d) failed: %m\n", opts.root_map_uid);
+                               free_and_exit(EXIT_FAILURE);
+                       }
+               }
+
                jail_process.pid = clone(exec_jail, child_stack + STACK_SIZE, SIGCHLD | (opts.namespace & (~CLONE_NEWCGROUP)), NULL);
        } else {
                jail_process.pid = fork();
@@ -2668,13 +3028,19 @@ static void post_main(struct uloop_timeout *t)
 
                uloop_process_add(&jail_process);
                jail_running = 1;
-               seteuid(0);
+               if (seteuid(0)) {
+                       ERROR("seteuid(%d) failed: %m\n", opts.root_map_uid);
+                       free_and_exit(EXIT_FAILURE);
+               }
+
+               prctl(PR_SET_SECUREBITS, 0);
+
                if (pidns_fd != -1) {
                        setns(pidns_fd, CLONE_NEWPID);
                        close(pidns_fd);
                }
 #ifdef CLONE_NEWTIME
-               if (timens_fd != -1)
+               if (timens_fd != -1) {
                        setns(timens_fd, CLONE_NEWTIME);
                        close(timens_fd);
                }
@@ -2695,7 +3061,7 @@ static void post_main(struct uloop_timeout *t)
                close(pipes[2]);
                if (read(pipes[0], sig_buf, 1) < 1) {
                        ERROR("can't read from child\n");
-                       exit(-1);
+                       free_and_exit(-1);
                }
                close(pipes[0]);
                set_oom_score_adj();
@@ -2706,7 +3072,7 @@ static void post_main(struct uloop_timeout *t)
                if (opts.namespace & CLONE_NEWUSER) {
                        if (write_setgroups(jail_process.pid, true)) {
                                ERROR("can't write setgroups\n");
-                               exit(-1);
+                               free_and_exit(-1);
                        }
                        if (!opts.uidmap) {
                                bool has_gr = (opts.gr_gid != -1);
@@ -2724,20 +3090,19 @@ static void post_main(struct uloop_timeout *t)
                        }
                }
 
-               if (opts.namespace & CLONE_NEWNET) {
-                       if (!opts.name) {
-                               ERROR("netns needs a named jail\n");
-                               exit(-1);
-                       }
-                       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");
+                       free_and_exit(-1);
                }
        } else if (jail_process.pid == 0) {
                /* fork child process */
-               exit(exec_jail(NULL));
+               free_and_exit(exec_jail(NULL));
        } else {
                ERROR("failed to clone/fork: %m\n");
-               exit(EXIT_FAILURE);
+               free_and_exit(EXIT_FAILURE);
        }
        run_hooks(opts.hooks.createRuntime, post_create_runtime);
 }
@@ -2750,7 +3115,7 @@ static void post_create_runtime(void)
        sig_buf[0] = 'O';
        if (write(pipes[3], sig_buf, 1) < 0) {
                ERROR("can't write to child\n");
-               exit(-1);
+               free_and_exit(-1);
        }
 
        jail_oci_state = OCI_STATE_CREATED;
@@ -2768,7 +3133,7 @@ static void pipe_send_start_container(struct uloop_timeout *t)
        sig_buf[0] = '!';
        if (write(pipes[3], sig_buf, 1) < 0) {
                ERROR("can't write to child\n");
-               exit(-1);
+               free_and_exit(-1);
        }
        close(pipes[3]);
 
@@ -2792,7 +3157,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);