jail: replace /etc/resolv.conf with symlink in extroot+overlay
[project/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 if (overlaydir)
258 unlink(jaillink);
259 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink);
260 }
261
262 char dirbuf[sizeof(jail_root) + 4];
263 snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
264 mkdir(dirbuf, 0755);
265
266 if (pivot_root(jail_root, dirbuf) == -1) {
267 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root, dirbuf);
268 return -1;
269 }
270 if (chdir("/")) {
271 ERROR("chdir(/) (after pivot_root) failed: %m\n");
272 return -1;
273 }
274
275 snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
276 umount2(dirbuf, MNT_DETACH);
277 rmdir(dirbuf);
278 if (opts.tmpoverlaysize) {
279 char tmpdirbuf[sizeof(tmpovdir) + 4];
280 snprintf(tmpdirbuf, sizeof(tmpdirbuf), "/old%s", tmpovdir);
281 umount2(tmpdirbuf, MNT_DETACH);
282 rmdir(tmpdirbuf);
283 }
284
285 umount2("/old", MNT_DETACH);
286 rmdir("/old");
287
288 if (opts.procfs) {
289 mkdir("/proc", 0755);
290 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
291 /*
292 * make /proc/sys read-only while keeping read-write to
293 * /proc/sys/net if CLONE_NEWNET is set.
294 */
295 if (opts.namespace & CLONE_NEWNET)
296 mount("/proc/sys/net", "/proc/self/net", NULL, MS_BIND, 0);
297
298 mount("/proc/sys", "/proc/sys", NULL, MS_BIND, 0);
299 mount(NULL, "/proc/sys", NULL, MS_REMOUNT | MS_RDONLY, 0);
300 mount(NULL, "/proc", NULL, MS_REMOUNT, 0);
301
302 if (opts.namespace & CLONE_NEWNET)
303 mount("/proc/self/net", "/proc/sys/net", NULL, MS_MOVE, 0);
304 }
305 if (opts.sysfs) {
306 mkdir("/sys", 0755);
307 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, 0);
308 }
309 if (opts.ronly)
310 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
311
312 return 0;
313 }
314
315 static int write_uid_gid_map(pid_t child_pid, bool gidmap, int id)
316 {
317 int map_file;
318 char map_path[64];
319 const char *map_format = "%d %d %d\n";
320 if (snprintf(map_path, sizeof(map_path), "/proc/%d/%s",
321 child_pid, gidmap?"gid_map":"uid_map") < 0)
322 return -1;
323
324 if ((map_file = open(map_path, O_WRONLY)) == -1)
325 return -1;
326
327 if (dprintf(map_file, map_format, 0, id, 1) == -1) {
328 close(map_file);
329 return -1;
330 }
331
332 close(map_file);
333 return 0;
334 }
335
336 static int write_setgroups(pid_t child_pid, bool allow)
337 {
338 int setgroups_file;
339 char setgroups_path[64];
340
341 if (snprintf(setgroups_path, sizeof(setgroups_path), "/proc/%d/setgroups",
342 child_pid) < 0) {
343 return -1;
344 }
345
346 if ((setgroups_file = open(setgroups_path, O_WRONLY)) == -1) {
347 return -1;
348 }
349
350 if (dprintf(setgroups_file, allow?"allow":"deny") == -1) {
351 close(setgroups_file);
352 return -1;
353 }
354
355 close(setgroups_file);
356 return 0;
357 }
358
359 static void get_jail_user(int *user, int *user_gid, int *gr_gid)
360 {
361 struct passwd *p = NULL;
362 struct group *g = NULL;
363
364 if (opts.user) {
365 p = getpwnam(opts.user);
366 if (!p) {
367 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
368 opts.user, errno, strerror(errno));
369 exit(EXIT_FAILURE);
370 }
371 *user = p->pw_uid;
372 *user_gid = p->pw_gid;
373 } else {
374 *user = -1;
375 *user_gid = -1;
376 }
377
378 if (opts.group) {
379 g = getgrnam(opts.group);
380 if (!g) {
381 ERROR("failed to get gid for group %s: %m\n", opts.group);
382 exit(EXIT_FAILURE);
383 }
384 *gr_gid = g->gr_gid;
385 } else {
386 *gr_gid = -1;
387 }
388 };
389
390 static void set_jail_user(int pw_uid, int user_gid, int gr_gid)
391 {
392 if ((user_gid != -1) && initgroups(opts.user, user_gid)) {
393 ERROR("failed to initgroups() for user %s: %m\n", opts.user);
394 exit(EXIT_FAILURE);
395 }
396
397 if ((gr_gid != -1) && setregid(gr_gid, gr_gid)) {
398 ERROR("failed to set group id %d: %m\n", gr_gid);
399 exit(EXIT_FAILURE);
400 }
401
402 if ((pw_uid != -1) && setreuid(pw_uid, pw_uid)) {
403 ERROR("failed to set user id %d: %m\n", pw_uid);
404 exit(EXIT_FAILURE);
405 }
406 }
407
408 #define MAX_ENVP 8
409 static char** build_envp(const char *seccomp)
410 {
411 static char *envp[MAX_ENVP];
412 static char preload_var[PATH_MAX];
413 static char seccomp_var[PATH_MAX];
414 static char debug_var[] = "LD_DEBUG=all";
415 static char container_var[] = "container=ujail";
416 const char *preload_lib = find_lib("libpreload-seccomp.so");
417 int count = 0;
418
419 if (seccomp && !preload_lib) {
420 ERROR("failed to add preload-lib to env\n");
421 return NULL;
422 }
423 if (seccomp) {
424 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
425 envp[count++] = seccomp_var;
426 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
427 envp[count++] = preload_var;
428 }
429
430 envp[count++] = container_var;
431
432 if (debug > 1)
433 envp[count++] = debug_var;
434
435 return envp;
436 }
437
438 static void usage(void)
439 {
440 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
441 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
442 fprintf(stderr, " -S <file>\tseccomp filter config\n");
443 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
444 fprintf(stderr, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
445 fprintf(stderr, " -n <name>\tthe name of the jail\n");
446 fprintf(stderr, "namespace jail options:\n");
447 fprintf(stderr, " -h <hostname>\tchange the hostname of the jail\n");
448 fprintf(stderr, " -N\t\tjail has network namespace\n");
449 fprintf(stderr, " -f\t\tjail has user namespace\n");
450 fprintf(stderr, " -F\t\tjail has cgroups namespace\n");
451 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
452 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
453 fprintf(stderr, " -p\t\tjail has /proc\n");
454 fprintf(stderr, " -s\t\tjail has /sys\n");
455 fprintf(stderr, " -l\t\tjail has /dev/log\n");
456 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
457 fprintf(stderr, " -U <name>\tuser to run jailed process\n");
458 fprintf(stderr, " -G <name>\tgroup to run jailed process\n");
459 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
460 fprintf(stderr, " -R <dir>\texternal jail rootfs (system container)\n");
461 fprintf(stderr, " -O <dir>\tdirectory for r/w overlayfs\n");
462 fprintf(stderr, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
463 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
464 and he has the same powers as root outside the jail,\n\
465 thus he can escape the jail and/or break stuff.\n\
466 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
467 If you use none of the namespace jail options,\n\
468 ujail will not use namespace/build a jail,\n\
469 and will only drop capabilities/apply seccomp filter.\n\n");
470 }
471
472 static int exec_jail(void *pipes_ptr)
473 {
474 int *pipes = (int*)pipes_ptr;
475 char buf[1];
476 int pw_uid, pw_gid, gr_gid;
477
478 close(pipes[0]);
479 close(pipes[3]);
480
481
482 buf[0] = 'i';
483 if (write(pipes[1], buf, 1) < 1) {
484 ERROR("can't write to parent\n");
485 exit(EXIT_FAILURE);
486 }
487 if (read(pipes[2], buf, 1) < 1) {
488 ERROR("can't read from parent\n");
489 exit(EXIT_FAILURE);
490 }
491 if (buf[0] != 'O') {
492 ERROR("parent had an error, child exiting\n");
493 exit(EXIT_FAILURE);
494 }
495
496 close(pipes[1]);
497 close(pipes[2]);
498
499 if (opts.namespace & CLONE_NEWUSER) {
500 if (setgid(0) < 0) {
501 ERROR("setgid\n");
502 exit(EXIT_FAILURE);
503 }
504 if (setuid(0) < 0) {
505 ERROR("setuid\n");
506 exit(EXIT_FAILURE);
507 }
508 // if (setgroups(0, NULL) < 0) {
509 // ERROR("setgroups\n");
510 // exit(EXIT_FAILURE);
511 // }
512 }
513
514 if (opts.namespace && opts.hostname && strlen(opts.hostname) > 0
515 && sethostname(opts.hostname, strlen(opts.hostname))) {
516 ERROR("sethostname(%s) failed: %m\n", opts.hostname);
517 exit(EXIT_FAILURE);
518 }
519
520 if ((opts.namespace & CLONE_NEWNS) && build_jail_fs()) {
521 ERROR("failed to build jail fs\n");
522 exit(EXIT_FAILURE);
523 }
524
525 if (opts.capabilities && drop_capabilities(opts.capabilities))
526 exit(EXIT_FAILURE);
527
528 if (opts.no_new_privs && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
529 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
530 exit(EXIT_FAILURE);
531 }
532
533 if (!(opts.namespace & CLONE_NEWUSER)) {
534 get_jail_user(&pw_uid, &pw_gid, &gr_gid);
535 set_jail_user(pw_uid, pw_gid, gr_gid);
536 }
537
538 char **envp = build_envp(opts.seccomp);
539 if (!envp)
540 exit(EXIT_FAILURE);
541
542 INFO("exec-ing %s\n", *opts.jail_argv);
543 execve(*opts.jail_argv, opts.jail_argv, envp);
544 /* we get there only if execve fails */
545 ERROR("failed to execve %s: %m\n", *opts.jail_argv);
546 exit(EXIT_FAILURE);
547 }
548
549 static int jail_running = 1;
550 static int jail_return_code = 0;
551
552 static void jail_process_timeout_cb(struct uloop_timeout *t);
553 static struct uloop_timeout jail_process_timeout = {
554 .cb = jail_process_timeout_cb,
555 };
556
557 static void jail_process_handler(struct uloop_process *c, int ret)
558 {
559 uloop_timeout_cancel(&jail_process_timeout);
560 if (WIFEXITED(ret)) {
561 jail_return_code = WEXITSTATUS(ret);
562 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
563 } else {
564 jail_return_code = WTERMSIG(ret);
565 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
566 }
567 jail_running = 0;
568 uloop_end();
569 }
570
571 static struct uloop_process jail_process = {
572 .cb = jail_process_handler,
573 };
574
575 static void jail_process_timeout_cb(struct uloop_timeout *t)
576 {
577 DEBUG("jail process failed to stop, sending SIGKILL\n");
578 kill(jail_process.pid, SIGKILL);
579 }
580
581 static void jail_handle_signal(int signo)
582 {
583 DEBUG("forwarding signal %d to the jailed process\n", signo);
584 kill(jail_process.pid, signo);
585 }
586
587 static int netns_open_pid(const pid_t target_ns)
588 {
589 char pid_net_path[PATH_MAX];
590
591 snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
592
593 return open(pid_net_path, O_RDONLY);
594 }
595
596 static void netns_updown(pid_t pid, bool start)
597 {
598 struct ubus_context *ctx = ubus_connect(NULL);
599 static struct blob_buf req;
600 uint32_t id;
601
602 if (!ctx)
603 return;
604
605 blob_buf_init(&req, 0);
606 blobmsg_add_string(&req, "jail", opts.name);
607 blobmsg_add_u32(&req, "pid", pid);
608 blobmsg_add_u8(&req, "start", start);
609
610 if (ubus_lookup_id(ctx, "network", &id) ||
611 ubus_invoke(ctx, id, "netns_updown", req.head, NULL, NULL, 3000))
612 INFO("ubus request failed\n");
613
614 blob_buf_free(&req);
615 ubus_free(ctx);
616 }
617
618 int main(int argc, char **argv)
619 {
620 sigset_t sigmask;
621 uid_t uid = getuid();
622 char log[] = "/dev/log";
623 char ubus[] = "/var/run/ubus.sock";
624 int ch, i;
625 int pipes[4];
626 char sig_buf[1];
627 int netns_fd;
628
629 if (uid) {
630 ERROR("not root, aborting: %m\n");
631 return EXIT_FAILURE;
632 }
633
634 umask(022);
635 mount_list_init();
636 init_library_search();
637
638 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
639 switch (ch) {
640 case 'd':
641 debug = atoi(optarg);
642 break;
643 case 'p':
644 opts.namespace |= CLONE_NEWNS;
645 opts.procfs = 1;
646 break;
647 case 'o':
648 opts.namespace |= CLONE_NEWNS;
649 opts.ronly = 1;
650 break;
651 case 'f':
652 opts.namespace |= CLONE_NEWUSER;
653 break;
654 case 'F':
655 opts.namespace |= CLONE_NEWCGROUP;
656 break;
657 case 'R':
658 opts.extroot = optarg;
659 break;
660 case 's':
661 opts.namespace |= CLONE_NEWNS;
662 opts.sysfs = 1;
663 break;
664 case 'S':
665 opts.seccomp = optarg;
666 add_mount(optarg, 1, -1);
667 break;
668 case 'C':
669 opts.capabilities = optarg;
670 break;
671 case 'c':
672 opts.no_new_privs = 1;
673 break;
674 case 'n':
675 opts.name = optarg;
676 break;
677 case 'N':
678 opts.namespace |= CLONE_NEWNET;
679 break;
680 case 'h':
681 opts.namespace |= CLONE_NEWUTS;
682 opts.hostname = optarg;
683 break;
684 case 'r':
685 opts.namespace |= CLONE_NEWNS;
686 add_path_and_deps(optarg, 1, 0, 0);
687 break;
688 case 'w':
689 opts.namespace |= CLONE_NEWNS;
690 add_path_and_deps(optarg, 0, 0, 0);
691 break;
692 case 'u':
693 opts.namespace |= CLONE_NEWNS;
694 add_mount(ubus, 0, -1);
695 break;
696 case 'l':
697 opts.namespace |= CLONE_NEWNS;
698 add_mount(log, 0, -1);
699 break;
700 case 'U':
701 opts.user = optarg;
702 break;
703 case 'G':
704 opts.group = optarg;
705 break;
706 case 'O':
707 opts.overlaydir = optarg;
708 break;
709 case 'T':
710 opts.tmpoverlaysize = optarg;
711 break;
712 }
713 }
714
715 if (opts.namespace)
716 opts.namespace |= CLONE_NEWIPC | CLONE_NEWPID;
717
718 if (opts.tmpoverlaysize && strlen(opts.tmpoverlaysize) > 8) {
719 ERROR("size parameter too long: \"%s\"\n", opts.tmpoverlaysize);
720 return -1;
721 }
722
723 /* no <binary> param found */
724 if (argc - optind < 1) {
725 usage();
726 return EXIT_FAILURE;
727 }
728 if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
729 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
730 usage();
731 return EXIT_FAILURE;
732 }
733 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
734 opts.namespace,
735 opts.capabilities != 0,
736 opts.seccomp != 0);
737
738 opts.jail_argv = &argv[optind];
739
740 get_jail_user(&opts.pw_uid, &opts.pw_gid, &opts.gr_gid);
741
742 if (!opts.extroot) {
743 if (opts.namespace && add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
744 ERROR("failed to load dependencies\n");
745 return -1;
746 }
747 }
748
749 if (opts.namespace && opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
750 ERROR("failed to load libpreload-seccomp.so\n");
751 return -1;
752 }
753
754 if (opts.name)
755 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
756
757 uloop_init();
758
759 sigfillset(&sigmask);
760 for (i = 0; i < _NSIG; i++) {
761 struct sigaction s = { 0 };
762
763 if (!sigismember(&sigmask, i))
764 continue;
765 if ((i == SIGCHLD) || (i == SIGPIPE))
766 continue;
767
768 s.sa_handler = jail_handle_signal;
769 sigaction(i, &s, NULL);
770 }
771
772 if (opts.namespace) {
773 if (opts.namespace & CLONE_NEWNS) {
774 add_mount("/dev/full", 0, -1);
775 add_mount("/dev/null", 0, -1);
776 add_mount("/dev/random", 0, -1);
777 add_mount("/dev/urandom", 0, -1);
778 add_mount("/dev/tty", 0, -1);
779 add_mount("/dev/zero", 0, -1);
780 add_mount("/dev/console", 0, -1);
781
782 if (!opts.extroot && (opts.user || opts.group)) {
783 add_mount("/etc/passwd", 0, -1);
784 add_mount("/etc/group", 0, -1);
785 }
786
787 if (!(opts.namespace & CLONE_NEWNET)) {
788 add_mount("/etc/resolv.conf", 0, -1);
789 }
790 }
791
792 if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0)
793 return -1;
794
795 jail_process.pid = clone(exec_jail, child_stack + STACK_SIZE, SIGCHLD | opts.namespace, &pipes);
796 } else {
797 jail_process.pid = fork();
798 }
799
800 if (jail_process.pid > 0) {
801 seteuid(0);
802 /* parent process */
803 close(pipes[1]);
804 close(pipes[2]);
805 if (read(pipes[0], sig_buf, 1) < 1) {
806 ERROR("can't read from child\n");
807 return -1;
808 }
809 close(pipes[0]);
810 if (opts.namespace & CLONE_NEWUSER) {
811 bool has_gr = (opts.gr_gid != -1);
812 if (write_setgroups(jail_process.pid, false)) {
813 ERROR("can't write setgroups\n");
814 return -1;
815 }
816 if (opts.pw_uid != -1) {
817 write_uid_gid_map(jail_process.pid, 0, opts.pw_uid);
818 write_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:opts.pw_gid);
819 } else {
820 write_uid_gid_map(jail_process.pid, 0, 65534);
821 write_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:65534);
822 }
823 }
824
825 if (opts.namespace & CLONE_NEWNET) {
826 netns_fd = netns_open_pid(jail_process.pid);
827 netns_updown(jail_process.pid, true);
828 }
829
830 sig_buf[0] = 'O';
831 if (write(pipes[3], sig_buf, 1) < 0) {
832 ERROR("can't write to child\n");
833 return -1;
834 }
835 close(pipes[3]);
836 uloop_process_add(&jail_process);
837 uloop_run();
838 if (jail_running) {
839 DEBUG("uloop interrupted, killing jail process\n");
840 kill(jail_process.pid, SIGTERM);
841 uloop_timeout_set(&jail_process_timeout, 1000);
842 uloop_run();
843 }
844 uloop_done();
845 if (opts.namespace & CLONE_NEWNET) {
846 setns(netns_fd, CLONE_NEWNET);
847 netns_updown(getpid(), false);
848 close(netns_fd);
849 }
850 return jail_return_code;
851 } else if (jail_process.pid == 0) {
852 /* fork child process */
853 return exec_jail(NULL);
854 } else {
855 ERROR("failed to clone/fork: %m\n");
856 return EXIT_FAILURE;
857 }
858 }