jail: fs: add support for asymmetric mount bind
[project/procd.git] / jail / jail.c
index 24a3df3a38643b6e90735a3c39ca32820f9d791d..ff54d970b6953a1539910bda1327007c43641f04 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:EfFG:h:ij:J:ln:NoO:pP:r:R:sS:uU:w:T:y"
 
 #define OCI_VERSION_STRING "1.0.2"
 
@@ -395,6 +396,9 @@ static int create_dev_console(const char *jail_root)
 
        /* 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);
@@ -723,7 +727,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);
@@ -1790,6 +1794,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))
@@ -1861,6 +1867,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)
@@ -2183,21 +2250,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 */
@@ -2498,12 +2568,25 @@ int main(int argc, char **argv)
        const char ubus[] = "/var/run/ubus/ubus.sock";
        int ret = EXIT_FAILURE;
        int ch;
+       char *tmp;
 
        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
+
        umask(022);
        mount_list_init();
        init_library_search();
@@ -2556,13 +2639,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;
@@ -2605,18 +2703,6 @@ 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
-
        /*
         * uid in parent user namespace representing root user in new
         * user namespace, defaults to nobody unless specified in uidMappings
@@ -2684,7 +2770,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;
@@ -2720,7 +2812,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;
@@ -2785,18 +2877,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);
@@ -2935,6 +3028,7 @@ static void post_main(struct uloop_timeout *t)
                }
 
                if (opts.namespace & CLONE_NEWNET) {
+                       jail_network_start(parent_ctx, opts.name, jail_process.pid);
                        netns_fd = ns_open_pid("net", jail_process.pid);
                        netns_updown(jail_process.pid, true);
                }
@@ -3004,6 +3098,7 @@ 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);