jail: mount /sys read-only
[oweals/procd.git] / jail / jail.c
1 /*
2  * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #define _GNU_SOURCE
15 #include <sys/mount.h>
16 #include <sys/prctl.h>
17 #include <sys/wait.h>
18 #include <sys/types.h>
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <libgen.h>
29 #include <sched.h>
30 #include <linux/limits.h>
31 #include <signal.h>
32
33 #include "capabilities.h"
34 #include "elf.h"
35 #include "fs.h"
36 #include "jail.h"
37 #include "log.h"
38
39 #include <libubox/uloop.h>
40 #include <libubus.h>
41
42 #define STACK_SIZE      (1024 * 1024)
43 #define OPT_ARGS        "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:"
44
45 static struct {
46         char *name;
47         char *hostname;
48         char **jail_argv;
49         char *seccomp;
50         char *capabilities;
51         char *user;
52         char *group;
53         char *extroot;
54         char *overlaydir;
55         char *tmpoverlaysize;
56         int no_new_privs;
57         int namespace;
58         int procfs;
59         int ronly;
60         int sysfs;
61         int pw_uid;
62         int pw_gid;
63         int gr_gid;
64 } opts;
65
66
67 extern int pivot_root(const char *new_root, const char *put_old);
68
69 int debug = 0;
70
71 static char child_stack[STACK_SIZE];
72
73 static int mkdir_p(char *dir, mode_t mask)
74 {
75         char *l = strrchr(dir, '/');
76         int ret;
77
78         if (!l)
79                 return 0;
80
81         *l = '\0';
82
83         if (mkdir_p(dir, mask))
84                 return -1;
85
86         *l = '/';
87
88         ret = mkdir(dir, mask);
89         if (ret && errno == EEXIST)
90                 return 0;
91
92         if (ret)
93                 ERROR("mkdir(%s, %d) failed: %m\n", dir, mask);
94
95         return ret;
96 }
97
98 static int _mount_bind(const char *root, const char *path, const char *target, int readonly, int strict, int error)
99 {
100         struct stat s;
101         char new[PATH_MAX];
102         int fd;
103         int remount_flags = MS_BIND | MS_REMOUNT;
104
105         if (stat(path, &s)) {
106                 ERROR("stat(%s) failed: %m\n", path);
107                 return error;
108         }
109
110         snprintf(new, sizeof(new), "%s%s", root, target?target:path);
111
112         if (S_ISDIR(s.st_mode)) {
113                 mkdir_p(new, 0755);
114         } else {
115                 mkdir_p(dirname(new), 0755);
116                 snprintf(new, sizeof(new), "%s%s", root, target?target:path);
117                 fd = creat(new, 0644);
118                 if (fd == -1) {
119                         ERROR("creat(%s) failed: %m\n", new);
120                         return -1;
121                 }
122                 close(fd);
123         }
124
125         if (mount(path, new, NULL, MS_BIND, NULL)) {
126                 ERROR("failed to mount -B %s %s: %m\n", path, new);
127                 return -1;
128         }
129
130         if (readonly)
131                 remount_flags |= MS_RDONLY;
132
133         if (strict)
134                 remount_flags |= MS_NOEXEC | MS_NOSUID | MS_NODEV;
135
136         if ((strict || readonly) && mount(NULL, new, NULL, remount_flags, NULL)) {
137                 ERROR("failed to remount (%s%s%s) %s: %m\n", readonly?"ro":"rw",
138                       (readonly && strict)?", ":"", strict?"strict":"", new);
139                 return -1;
140         }
141
142         DEBUG("mount -B %s %s (%s%s%s)\n", path, new,
143               readonly?"ro":"rw", (readonly && strict)?", ":"", strict?"strict":"");
144
145         return 0;
146 }
147
148 int mount_bind(const char *root, const char *path, int readonly, int error) {
149         return _mount_bind(root, path, NULL, readonly, 0, error);
150 }
151
152 static int mount_overlay(char *jail_root, char *overlaydir) {
153         char *upperdir, *workdir, *optsstr;
154         const char mountoptsformat[] = "lowerdir=%s,upperdir=%s,workdir=%s";
155         int ret = -1;
156
157         if (asprintf(&upperdir, "%s%s", overlaydir, "/upper") < 0)
158                 goto out;
159
160         if (asprintf(&workdir, "%s%s", overlaydir, "/work") < 0)
161                 goto upper_printf;
162
163         if (asprintf(&optsstr, mountoptsformat, jail_root, upperdir, workdir) < 0)
164                 goto work_printf;
165
166         if (mkdir_p(upperdir, 0755) || mkdir_p(workdir, 0755))
167                 goto opts_printf;
168
169         DEBUG("mount -t overlay %s %s (%s)\n", jail_root, jail_root, optsstr);
170
171         if (mount(jail_root, jail_root, "overlay", MS_NOATIME, optsstr))
172                 goto opts_printf;
173
174         ret = 0;
175
176 opts_printf:
177         free(optsstr);
178 work_printf:
179         free(workdir);
180 upper_printf:
181         free(upperdir);
182 out:
183         return ret;
184 }
185
186 static int build_jail_fs(void)
187 {
188         char jail_root[] = "/tmp/ujail-XXXXXX";
189         char tmpovdir[] = "/tmp/ujail-overlay-XXXXXX";
190         char *overlaydir = NULL;
191
192         if (mkdtemp(jail_root) == NULL) {
193                 ERROR("mkdtemp(%s) failed: %m\n", jail_root);
194                 return -1;
195         }
196
197         /* oldroot can't be MS_SHARED else pivot_root() fails */
198         if (mount("none", "/", NULL, MS_REC|MS_PRIVATE, NULL)) {
199                 ERROR("private mount failed %m\n");
200                 return -1;
201         }
202
203         if (opts.extroot) {
204                 if (mount(opts.extroot, jail_root, NULL, MS_BIND, NULL)) {
205                         ERROR("extroot mount failed %m\n");
206                         return -1;
207                 }
208         } else {
209                 if (mount("tmpfs", jail_root, "tmpfs", MS_NOATIME, "mode=0755")) {
210                         ERROR("tmpfs mount failed %m\n");
211                         return -1;
212                 }
213         }
214
215         if (opts.tmpoverlaysize) {
216                 char mountoptsstr[] = "mode=0755,size=XXXXXXXX";
217
218                 snprintf(mountoptsstr, sizeof(mountoptsstr),
219                          "mode=0755,size=%s", opts.tmpoverlaysize);
220                 if (mkdtemp(tmpovdir) == NULL) {
221                         ERROR("mkdtemp(%s) failed: %m\n", jail_root);
222                         return -1;
223                 }
224                 if (mount("tmpfs", tmpovdir, "tmpfs", MS_NOATIME,
225                           mountoptsstr)) {
226                         ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts.tmpoverlaysize);
227                         return -1;
228                 }
229                 overlaydir = tmpovdir;
230         }
231
232         if (opts.overlaydir)
233                 overlaydir = opts.overlaydir;
234
235         if (overlaydir)
236                 mount_overlay(jail_root, overlaydir);
237
238         if (chdir(jail_root)) {
239                 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root);
240                 return -1;
241         }
242
243         if (mount_all(jail_root)) {
244                 ERROR("mount_all() failed\n");
245                 return -1;
246         }
247
248         if (opts.namespace & CLONE_NEWNET) {
249                 char hostdir[PATH_MAX], jailetc[PATH_MAX], jaillink[PATH_MAX];
250
251                 snprintf(hostdir, PATH_MAX, "/tmp/resolv.conf-%s.d", opts.name);
252                 mkdir_p(hostdir, 0755);
253                 _mount_bind(jail_root, hostdir, "/tmp/resolv.conf.d", 1, 1, -1);
254                 snprintf(jailetc, PATH_MAX, "%s/etc", jail_root);
255                 mkdir_p(jailetc, 0755);
256                 snprintf(jaillink, PATH_MAX, "%s/etc/resolv.conf", jail_root);
257                 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink);
258         }
259
260         char dirbuf[sizeof(jail_root) + 4];
261         snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
262         mkdir(dirbuf, 0755);
263
264         if (pivot_root(jail_root, dirbuf) == -1) {
265                 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root, dirbuf);
266                 return -1;
267         }
268         if (chdir("/")) {
269                 ERROR("chdir(/) (after pivot_root) failed: %m\n");
270                 return -1;
271         }
272
273         snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
274         umount2(dirbuf, MNT_DETACH);
275         rmdir(dirbuf);
276         if (opts.tmpoverlaysize) {
277                 char tmpdirbuf[sizeof(tmpovdir) + 4];
278                 snprintf(tmpdirbuf, sizeof(tmpdirbuf), "/old%s", tmpovdir);
279                 umount2(tmpdirbuf, MNT_DETACH);
280                 rmdir(tmpdirbuf);
281         }
282
283         umount2("/old", MNT_DETACH);
284         rmdir("/old");
285
286         if (opts.procfs) {
287                 mkdir("/proc", 0755);
288                 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
289                 /*
290                  * make /proc/sys read-only while keeping read-write to
291                  * /proc/sys/net if CLONE_NEWNET is set.
292                  */
293                 if (opts.namespace & CLONE_NEWNET)
294                         mount("/proc/sys/net", "/proc/self/net", NULL, MS_BIND, 0);
295
296                 mount("/proc/sys", "/proc/sys", NULL, MS_BIND, 0);
297                 mount(NULL, "/proc/sys", NULL, MS_REMOUNT | MS_RDONLY, 0);
298                 mount(NULL, "/proc", NULL, MS_REMOUNT, 0);
299
300                 if (opts.namespace & CLONE_NEWNET)
301                         mount("/proc/self/net", "/proc/sys/net", NULL, MS_MOVE, 0);
302         }
303         if (opts.sysfs) {
304                 mkdir("/sys", 0755);
305                 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, 0);
306         }
307         if (opts.ronly)
308                 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
309
310         return 0;
311 }
312
313 static int write_uid_gid_map(pid_t child_pid, bool gidmap, int id)
314 {
315         int map_file;
316         char map_path[64];
317         const char *map_format = "%d %d %d\n";
318         if (snprintf(map_path, sizeof(map_path), "/proc/%d/%s",
319                 child_pid, gidmap?"gid_map":"uid_map") < 0)
320                 return -1;
321
322         if ((map_file = open(map_path, O_WRONLY)) == -1)
323                 return -1;
324
325         if (dprintf(map_file, map_format, 0, id, 1) == -1) {
326                 close(map_file);
327                 return -1;
328         }
329
330         close(map_file);
331         return 0;
332 }
333
334 static int write_setgroups(pid_t child_pid, bool allow)
335 {
336         int setgroups_file;
337         char setgroups_path[64];
338
339         if (snprintf(setgroups_path, sizeof(setgroups_path), "/proc/%d/setgroups",
340                 child_pid) < 0) {
341                 return -1;
342         }
343
344         if ((setgroups_file = open(setgroups_path, O_WRONLY)) == -1) {
345                 return -1;
346         }
347
348         if (dprintf(setgroups_file, allow?"allow":"deny") == -1) {
349                 close(setgroups_file);
350                 return -1;
351         }
352
353         close(setgroups_file);
354         return 0;
355 }
356
357 static void get_jail_user(int *user, int *user_gid, int *gr_gid)
358 {
359         struct passwd *p = NULL;
360         struct group *g = NULL;
361
362         if (opts.user) {
363                 p = getpwnam(opts.user);
364                 if (!p) {
365                         ERROR("failed to get uid/gid for user %s: %d (%s)\n",
366                               opts.user, errno, strerror(errno));
367                         exit(EXIT_FAILURE);
368                 }
369                 *user = p->pw_uid;
370                 *user_gid = p->pw_gid;
371         } else {
372                 *user = -1;
373                 *user_gid = -1;
374         }
375
376         if (opts.group) {
377                 g = getgrnam(opts.group);
378                 if (!g) {
379                         ERROR("failed to get gid for group %s: %m\n", opts.group);
380                         exit(EXIT_FAILURE);
381                 }
382                 *gr_gid = g->gr_gid;
383         } else {
384                 *gr_gid = -1;
385         }
386 };
387
388 static void set_jail_user(int pw_uid, int user_gid, int gr_gid)
389 {
390         if ((user_gid != -1) && initgroups(opts.user, user_gid)) {
391                 ERROR("failed to initgroups() for user %s: %m\n", opts.user);
392                 exit(EXIT_FAILURE);
393         }
394
395         if ((gr_gid != -1) && setregid(gr_gid, gr_gid)) {
396                 ERROR("failed to set group id %d: %m\n", gr_gid);
397                 exit(EXIT_FAILURE);
398         }
399
400         if ((pw_uid != -1) && setreuid(pw_uid, pw_uid)) {
401                 ERROR("failed to set user id %d: %m\n", pw_uid);
402                 exit(EXIT_FAILURE);
403         }
404 }
405
406 #define MAX_ENVP        8
407 static char** build_envp(const char *seccomp)
408 {
409         static char *envp[MAX_ENVP];
410         static char preload_var[PATH_MAX];
411         static char seccomp_var[PATH_MAX];
412         static char debug_var[] = "LD_DEBUG=all";
413         static char container_var[] = "container=ujail";
414         const char *preload_lib = find_lib("libpreload-seccomp.so");
415         int count = 0;
416
417         if (seccomp && !preload_lib) {
418                 ERROR("failed to add preload-lib to env\n");
419                 return NULL;
420         }
421         if (seccomp) {
422                 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
423                 envp[count++] = seccomp_var;
424                 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
425                 envp[count++] = preload_var;
426         }
427
428         envp[count++] = container_var;
429
430         if (debug > 1)
431                 envp[count++] = debug_var;
432
433         return envp;
434 }
435
436 static void usage(void)
437 {
438         fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
439         fprintf(stderr, "  -d <num>\tshow debug log (increase num to increase verbosity)\n");
440         fprintf(stderr, "  -S <file>\tseccomp filter config\n");
441         fprintf(stderr, "  -C <file>\tcapabilities drop config\n");
442         fprintf(stderr, "  -c\t\tset PR_SET_NO_NEW_PRIVS\n");
443         fprintf(stderr, "  -n <name>\tthe name of the jail\n");
444         fprintf(stderr, "namespace jail options:\n");
445         fprintf(stderr, "  -h <hostname>\tchange the hostname of the jail\n");
446         fprintf(stderr, "  -N\t\tjail has network namespace\n");
447         fprintf(stderr, "  -f\t\tjail has user namespace\n");
448         fprintf(stderr, "  -F\t\tjail has cgroups namespace\n");
449         fprintf(stderr, "  -r <file>\treadonly files that should be staged\n");
450         fprintf(stderr, "  -w <file>\twriteable files that should be staged\n");
451         fprintf(stderr, "  -p\t\tjail has /proc\n");
452         fprintf(stderr, "  -s\t\tjail has /sys\n");
453         fprintf(stderr, "  -l\t\tjail has /dev/log\n");
454         fprintf(stderr, "  -u\t\tjail has a ubus socket\n");
455         fprintf(stderr, "  -U <name>\tuser to run jailed process\n");
456         fprintf(stderr, "  -G <name>\tgroup to run jailed process\n");
457         fprintf(stderr, "  -o\t\tremont jail root (/) read only\n");
458         fprintf(stderr, "  -R <dir>\texternal jail rootfs (system container)\n");
459         fprintf(stderr, "  -O <dir>\tdirectory for r/w overlayfs\n");
460         fprintf(stderr, "  -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
461         fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
462 and he has the same powers as root outside the jail,\n\
463 thus he can escape the jail and/or break stuff.\n\
464 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
465 If you use none of the namespace jail options,\n\
466 ujail will not use namespace/build a jail,\n\
467 and will only drop capabilities/apply seccomp filter.\n\n");
468 }
469
470 static int exec_jail(void *pipes_ptr)
471 {
472         int *pipes = (int*)pipes_ptr;
473         char buf[1];
474         int pw_uid, pw_gid, gr_gid;
475
476         close(pipes[0]);
477         close(pipes[3]);
478
479
480         buf[0] = 'i';
481         if (write(pipes[1], buf, 1) < 1) {
482                 ERROR("can't write to parent\n");
483                 exit(EXIT_FAILURE);
484         }
485         if (read(pipes[2], buf, 1) < 1) {
486                 ERROR("can't read from parent\n");
487                 exit(EXIT_FAILURE);
488         }
489         if (buf[0] != 'O') {
490                 ERROR("parent had an error, child exiting\n");
491                 exit(EXIT_FAILURE);
492         }
493
494         close(pipes[1]);
495         close(pipes[2]);
496
497         if (opts.namespace & CLONE_NEWUSER) {
498                 if (setgid(0) < 0) {
499                         ERROR("setgid\n");
500                         exit(EXIT_FAILURE);
501                 }
502                 if (setuid(0) < 0) {
503                         ERROR("setuid\n");
504                         exit(EXIT_FAILURE);
505                 }
506 //              if (setgroups(0, NULL) < 0) {
507 //                      ERROR("setgroups\n");
508 //                      exit(EXIT_FAILURE);
509 //              }
510         }
511
512         if (opts.namespace && opts.hostname && strlen(opts.hostname) > 0
513                         && sethostname(opts.hostname, strlen(opts.hostname))) {
514                 ERROR("sethostname(%s) failed: %m\n", opts.hostname);
515                 exit(EXIT_FAILURE);
516         }
517
518         if ((opts.namespace & CLONE_NEWNS) && build_jail_fs()) {
519                 ERROR("failed to build jail fs\n");
520                 exit(EXIT_FAILURE);
521         }
522
523         if (opts.capabilities && drop_capabilities(opts.capabilities))
524                 exit(EXIT_FAILURE);
525
526         if (opts.no_new_privs && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
527                 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
528                 exit(EXIT_FAILURE);
529         }
530
531         if (!(opts.namespace & CLONE_NEWUSER)) {
532                 get_jail_user(&pw_uid, &pw_gid, &gr_gid);
533                 set_jail_user(pw_uid, pw_gid, gr_gid);
534         }
535
536         char **envp = build_envp(opts.seccomp);
537         if (!envp)
538                 exit(EXIT_FAILURE);
539
540         INFO("exec-ing %s\n", *opts.jail_argv);
541         execve(*opts.jail_argv, opts.jail_argv, envp);
542         /* we get there only if execve fails */
543         ERROR("failed to execve %s: %m\n", *opts.jail_argv);
544         exit(EXIT_FAILURE);
545 }
546
547 static int jail_running = 1;
548 static int jail_return_code = 0;
549
550 static void jail_process_timeout_cb(struct uloop_timeout *t);
551 static struct uloop_timeout jail_process_timeout = {
552         .cb = jail_process_timeout_cb,
553 };
554
555 static void jail_process_handler(struct uloop_process *c, int ret)
556 {
557         uloop_timeout_cancel(&jail_process_timeout);
558         if (WIFEXITED(ret)) {
559                 jail_return_code = WEXITSTATUS(ret);
560                 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
561         } else {
562                 jail_return_code = WTERMSIG(ret);
563                 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
564         }
565         jail_running = 0;
566         uloop_end();
567 }
568
569 static struct uloop_process jail_process = {
570         .cb = jail_process_handler,
571 };
572
573 static void jail_process_timeout_cb(struct uloop_timeout *t)
574 {
575         DEBUG("jail process failed to stop, sending SIGKILL\n");
576         kill(jail_process.pid, SIGKILL);
577 }
578
579 static void jail_handle_signal(int signo)
580 {
581         DEBUG("forwarding signal %d to the jailed process\n", signo);
582         kill(jail_process.pid, signo);
583 }
584
585 static int netns_open_pid(const pid_t target_ns)
586 {
587         char pid_net_path[PATH_MAX];
588
589         snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
590
591         return open(pid_net_path, O_RDONLY);
592 }
593
594 static void netns_updown(pid_t pid, bool start)
595 {
596         struct ubus_context *ctx = ubus_connect(NULL);
597         static struct blob_buf req;
598         uint32_t id;
599
600         if (!ctx)
601                 return;
602
603         blob_buf_init(&req, 0);
604         blobmsg_add_string(&req, "jail", opts.name);
605         blobmsg_add_u32(&req, "pid", pid);
606         blobmsg_add_u8(&req, "start", start);
607
608         if (ubus_lookup_id(ctx, "network", &id) ||
609             ubus_invoke(ctx, id, "netns_updown", req.head, NULL, NULL, 3000))
610                 INFO("ubus request failed\n");
611
612         blob_buf_free(&req);
613         ubus_free(ctx);
614 }
615
616 int main(int argc, char **argv)
617 {
618         sigset_t sigmask;
619         uid_t uid = getuid();
620         char log[] = "/dev/log";
621         char ubus[] = "/var/run/ubus.sock";
622         int ch, i;
623         int pipes[4];
624         char sig_buf[1];
625         int netns_fd;
626
627         if (uid) {
628                 ERROR("not root, aborting: %m\n");
629                 return EXIT_FAILURE;
630         }
631
632         umask(022);
633         mount_list_init();
634         init_library_search();
635
636         while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
637                 switch (ch) {
638                 case 'd':
639                         debug = atoi(optarg);
640                         break;
641                 case 'p':
642                         opts.namespace |= CLONE_NEWNS;
643                         opts.procfs = 1;
644                         break;
645                 case 'o':
646                         opts.namespace |= CLONE_NEWNS;
647                         opts.ronly = 1;
648                         break;
649                 case 'f':
650                         opts.namespace |= CLONE_NEWUSER;
651                         break;
652                 case 'F':
653                         opts.namespace |= CLONE_NEWCGROUP;
654                         break;
655                 case 'R':
656                         opts.extroot = optarg;
657                         break;
658                 case 's':
659                         opts.namespace |= CLONE_NEWNS;
660                         opts.sysfs = 1;
661                         break;
662                 case 'S':
663                         opts.seccomp = optarg;
664                         add_mount(optarg, 1, -1);
665                         break;
666                 case 'C':
667                         opts.capabilities = optarg;
668                         break;
669                 case 'c':
670                         opts.no_new_privs = 1;
671                         break;
672                 case 'n':
673                         opts.name = optarg;
674                         break;
675                 case 'N':
676                         opts.namespace |= CLONE_NEWNET;
677                         break;
678                 case 'h':
679                         opts.namespace |= CLONE_NEWUTS;
680                         opts.hostname = optarg;
681                         break;
682                 case 'r':
683                         opts.namespace |= CLONE_NEWNS;
684                         add_path_and_deps(optarg, 1, 0, 0);
685                         break;
686                 case 'w':
687                         opts.namespace |= CLONE_NEWNS;
688                         add_path_and_deps(optarg, 0, 0, 0);
689                         break;
690                 case 'u':
691                         opts.namespace |= CLONE_NEWNS;
692                         add_mount(ubus, 0, -1);
693                         break;
694                 case 'l':
695                         opts.namespace |= CLONE_NEWNS;
696                         add_mount(log, 0, -1);
697                         break;
698                 case 'U':
699                         opts.user = optarg;
700                         break;
701                 case 'G':
702                         opts.group = optarg;
703                         break;
704                 case 'O':
705                         opts.overlaydir = optarg;
706                         break;
707                 case 'T':
708                         opts.tmpoverlaysize = optarg;
709                         break;
710                 }
711         }
712
713         if (opts.namespace)
714                 opts.namespace |= CLONE_NEWIPC | CLONE_NEWPID;
715
716         if (opts.tmpoverlaysize && strlen(opts.tmpoverlaysize) > 8) {
717                 ERROR("size parameter too long: \"%s\"\n", opts.tmpoverlaysize);
718                 return -1;
719         }
720
721         /* no <binary> param found */
722         if (argc - optind < 1) {
723                 usage();
724                 return EXIT_FAILURE;
725         }
726         if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
727                 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
728                 usage();
729                 return EXIT_FAILURE;
730         }
731         DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
732                 opts.namespace,
733                 opts.capabilities != 0,
734                 opts.seccomp != 0);
735
736         opts.jail_argv = &argv[optind];
737
738         get_jail_user(&opts.pw_uid, &opts.pw_gid, &opts.gr_gid);
739
740         if (!opts.extroot) {
741                 if (opts.namespace && add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
742                         ERROR("failed to load dependencies\n");
743                         return -1;
744                 }
745         }
746
747         if (opts.namespace && opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
748                 ERROR("failed to load libpreload-seccomp.so\n");
749                 return -1;
750         }
751
752         if (opts.name)
753                 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
754
755         uloop_init();
756
757         sigfillset(&sigmask);
758         for (i = 0; i < _NSIG; i++) {
759                 struct sigaction s = { 0 };
760
761                 if (!sigismember(&sigmask, i))
762                         continue;
763                 if ((i == SIGCHLD) || (i == SIGPIPE))
764                         continue;
765
766                 s.sa_handler = jail_handle_signal;
767                 sigaction(i, &s, NULL);
768         }
769
770         if (opts.namespace) {
771                 if (opts.namespace & CLONE_NEWNS) {
772                         add_mount("/dev/full", 0, -1);
773                         add_mount("/dev/null", 0, -1);
774                         add_mount("/dev/random", 0, -1);
775                         add_mount("/dev/urandom", 0, -1);
776                         add_mount("/dev/tty", 0, -1);
777                         add_mount("/dev/zero", 0, -1);
778                         add_mount("/dev/console", 0, -1);
779
780                         if (!opts.extroot && (opts.user || opts.group)) {
781                                 add_mount("/etc/passwd", 0, -1);
782                                 add_mount("/etc/group", 0, -1);
783                         }
784
785                         if (!(opts.namespace & CLONE_NEWNET)) {
786                                 add_mount("/etc/resolv.conf", 0, -1);
787                         }
788                 }
789
790                 if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0)
791                         return -1;
792
793                 jail_process.pid = clone(exec_jail, child_stack + STACK_SIZE, SIGCHLD | opts.namespace, &pipes);
794         } else {
795                 jail_process.pid = fork();
796         }
797
798         if (jail_process.pid > 0) {
799                 seteuid(0);
800                 /* parent process */
801                 close(pipes[1]);
802                 close(pipes[2]);
803                 if (read(pipes[0], sig_buf, 1) < 1) {
804                         ERROR("can't read from child\n");
805                         return -1;
806                 }
807                 close(pipes[0]);
808                 if (opts.namespace & CLONE_NEWUSER) {
809                         bool has_gr = (opts.gr_gid != -1);
810                         if (write_setgroups(jail_process.pid, false)) {
811                                 ERROR("can't write setgroups\n");
812                                 return -1;
813                         }
814                         if (opts.pw_uid != -1) {
815                                 write_uid_gid_map(jail_process.pid, 0, opts.pw_uid);
816                                 write_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:opts.pw_gid);
817                         } else {
818                                 write_uid_gid_map(jail_process.pid, 0, 65534);
819                                 write_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:65534);
820                         }
821                 }
822
823                 if (opts.namespace & CLONE_NEWNET) {
824                         netns_fd = netns_open_pid(jail_process.pid);
825                         netns_updown(jail_process.pid, true);
826                 }
827
828                 sig_buf[0] = 'O';
829                 if (write(pipes[3], sig_buf, 1) < 0) {
830                         ERROR("can't write to child\n");
831                         return -1;
832                 }
833                 close(pipes[3]);
834                 uloop_process_add(&jail_process);
835                 uloop_run();
836                 if (jail_running) {
837                         DEBUG("uloop interrupted, killing jail process\n");
838                         kill(jail_process.pid, SIGTERM);
839                         uloop_timeout_set(&jail_process_timeout, 1000);
840                         uloop_run();
841                 }
842                 uloop_done();
843                 if (opts.namespace & CLONE_NEWNET) {
844                         setns(netns_fd, CLONE_NEWNET);
845                         netns_updown(getpid(), false);
846                         close(netns_fd);
847                 }
848                 return jail_return_code;
849         } else if (jail_process.pid == 0) {
850                 /* fork child process */
851                 return exec_jail(NULL);
852         } else {
853                 ERROR("failed to clone/fork: %m\n");
854                 return EXIT_FAILURE;
855         }
856 }