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