jail: leak less memory
[project/procd.git] / jail / jail.c
1 /*
2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 * Copyright (C) 2020 Daniel Golle <daniel@makrotopia.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #define _GNU_SOURCE
16 #include <sys/mount.h>
17 #include <sys/prctl.h>
18 #include <sys/wait.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/stat.h>
23 #include <sys/sysmacros.h>
24
25 /* musl only defined 15 limit types, make sure all 16 are supported */
26 #ifndef RLIMIT_RTTIME
27 #define RLIMIT_RTTIME 15
28 #undef RLIMIT_NLIMITS
29 #define RLIMIT_NLIMITS 16
30 #undef RLIM_NLIMITS
31 #define RLIM_NLIMITS 16
32 #endif
33
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <pwd.h>
38 #include <grp.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <sched.h>
42 #include <linux/filter.h>
43 #include <linux/limits.h>
44 #include <linux/nsfs.h>
45 #include <linux/securebits.h>
46 #include <signal.h>
47 #include <inttypes.h>
48
49 #include "capabilities.h"
50 #include "elf.h"
51 #include "fs.h"
52 #include "jail.h"
53 #include "log.h"
54 #include "seccomp-oci.h"
55 #include "cgroups.h"
56
57 #include <libubox/utils.h>
58 #include <libubox/blobmsg.h>
59 #include <libubox/blobmsg_json.h>
60 #include <libubox/list.h>
61 #include <libubox/vlist.h>
62 #include <libubox/uloop.h>
63 #include <libubus.h>
64
65 #ifndef CLONE_NEWCGROUP
66 #define CLONE_NEWCGROUP 0x02000000
67 #endif
68
69 #define STACK_SIZE (1024 * 1024)
70 #define OPT_ARGS "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:EyJ:iP:"
71
72 #define OCI_VERSION_STRING "1.0.2"
73
74 struct hook_execvpe {
75 char *file;
76 char **argv;
77 char **envp;
78 int timeout;
79 };
80
81 struct sysctl_val {
82 char *entry;
83 char *value;
84 };
85
86 struct mknod_args {
87 char *path;
88 mode_t mode;
89 dev_t dev;
90 uid_t uid;
91 gid_t gid;
92 };
93
94 static struct {
95 char *name;
96 char *hostname;
97 char **jail_argv;
98 char *cwd;
99 char *seccomp;
100 struct sock_fprog *ociseccomp;
101 char *capabilities;
102 struct jail_capset capset;
103 char *user;
104 char *group;
105 char *extroot;
106 char *overlaydir;
107 char *tmpoverlaysize;
108 char **envp;
109 char *uidmap;
110 char *gidmap;
111 char *pidfile;
112 struct sysctl_val **sysctl;
113 int no_new_privs;
114 int namespace;
115 struct {
116 int pid;
117 int net;
118 int ns;
119 int ipc;
120 int uts;
121 int user;
122 int cgroup;
123 #ifdef CLONE_NEWTIME
124 int time;
125 #endif
126 } setns;
127 int procfs;
128 int ronly;
129 int sysfs;
130 int console;
131 int pw_uid;
132 int pw_gid;
133 int gr_gid;
134 gid_t *additional_gids;
135 size_t num_additional_gids;
136 mode_t umask;
137 bool set_umask;
138 int require_jail;
139 struct {
140 struct hook_execvpe **createRuntime;
141 struct hook_execvpe **createContainer;
142 struct hook_execvpe **startContainer;
143 struct hook_execvpe **poststart;
144 struct hook_execvpe **poststop;
145 } hooks;
146 struct rlimit *rlimits[RLIM_NLIMITS];
147 int oom_score_adj;
148 bool set_oom_score_adj;
149 struct mknod_args **devices;
150 char *ocibundle;
151 bool immediately;
152 struct blob_attr *annotations;
153 } opts;
154
155 static struct blob_buf ocibuf;
156
157 extern int pivot_root(const char *new_root, const char *put_old);
158
159 int debug = 0;
160
161 static char child_stack[STACK_SIZE];
162
163 static struct ubus_context *parent_ctx;
164
165 int console_fd;
166
167
168 static inline bool has_namespaces(void)
169 {
170 return ((opts.setns.pid != -1) ||
171 (opts.setns.net != -1) ||
172 (opts.setns.ns != -1) ||
173 (opts.setns.ipc != -1) ||
174 (opts.setns.uts != -1) ||
175 (opts.setns.user != -1) ||
176 (opts.setns.cgroup != -1) ||
177 #ifdef CLONE_NEWTIME
178 (opts.setns.time != -1) ||
179 #endif
180 opts.namespace);
181 }
182
183 static void free_oci_envp(char **p) {
184 char **tmp;
185
186 if (p) {
187 tmp = p;
188 while (*tmp)
189 free(*(tmp++));
190
191 free(p);
192 }
193 }
194
195 static void free_hooklist(struct hook_execvpe **hooklist)
196 {
197 struct hook_execvpe *cur;
198
199 if (!hooklist)
200 return;
201
202 cur = *hooklist;
203 while (cur) {
204 free_oci_envp(cur->argv);
205 free_oci_envp(cur->envp);
206 free(cur->file);
207 free(cur++);
208 }
209 free(hooklist);
210 }
211
212 static void free_sysctl(void) {
213 struct sysctl_val *cur;
214 cur = *opts.sysctl;
215
216 while (cur) {
217 free(cur->entry);
218 free(cur->value);
219 free(cur++);
220 }
221 free(opts.sysctl);
222 }
223
224 static void free_devices(void) {
225 struct mknod_args **cur;
226
227 if (!opts.devices)
228 return;
229
230 cur = opts.devices;
231
232 while (*cur) {
233 free((*cur)->path);
234 free(*(cur++));
235 }
236 free(opts.devices);
237 }
238
239 static void free_rlimits(void) {
240 int type;
241
242 for (type = 0; type < RLIM_NLIMITS; ++type)
243 free(opts.rlimits[type]);
244 }
245
246 static void free_opts(bool parent) {
247
248 free_library_search();
249 mount_free();
250
251 /* we need to keep argv, envp and seccomp filter in child */
252 if (parent) { /* parent-only */
253 if (opts.ociseccomp) {
254 free(opts.ociseccomp->filter);
255 free(opts.ociseccomp);
256 }
257
258 free_oci_envp(opts.jail_argv);
259 free_oci_envp(opts.envp);
260 } else { /* child-only */
261 if (opts.ocibundle)
262 cgroups_free();
263 }
264
265 free_rlimits();
266 free_sysctl();
267 free_devices();
268 free(opts.hostname);
269 free(opts.cwd);
270 free(opts.uidmap);
271 free(opts.gidmap);
272 free(opts.annotations);
273 free_hooklist(opts.hooks.createRuntime);
274 free_hooklist(opts.hooks.createContainer);
275 free_hooklist(opts.hooks.startContainer);
276 free_hooklist(opts.hooks.poststart);
277 free_hooklist(opts.hooks.poststop);
278 }
279
280 static int mount_overlay(char *jail_root, char *overlaydir) {
281 char *upperdir, *workdir, *optsstr, *upperetc, *upperresolvconf;
282 const char mountoptsformat[] = "lowerdir=%s,upperdir=%s,workdir=%s";
283 int ret = -1, fd;
284
285 if (asprintf(&upperdir, "%s%s", overlaydir, "/upper") < 0)
286 goto out;
287
288 if (asprintf(&workdir, "%s%s", overlaydir, "/work") < 0)
289 goto upper_printf;
290
291 if (asprintf(&optsstr, mountoptsformat, jail_root, upperdir, workdir) < 0)
292 goto work_printf;
293
294 if (mkdir_p(upperdir, 0755) || mkdir_p(workdir, 0755))
295 goto opts_printf;
296
297 /*
298 * make sure /etc/resolv.conf exists in overlay and is owned by jail userns root
299 * this is to work-around a bug in overlayfs described in the overlayfs-userns
300 * patch:
301 * 3. modification of a file 'hithere' which is in l but not yet
302 * in u, and which is not owned by T, is not allowed, even if
303 * writes to u are allowed. This may be a bug in overlayfs,
304 * but it is safe behavior.
305 */
306 if (asprintf(&upperetc, "%s/etc", upperdir) < 0)
307 goto opts_printf;
308
309 if (mkdir_p(upperetc, 0755))
310 goto upper_etc_printf;
311
312 if (asprintf(&upperresolvconf, "%s/resolv.conf", upperetc) < 0)
313 goto upper_etc_printf;
314
315 fd = creat(upperresolvconf, 0644);
316 if (fd == -1) {
317 ERROR("creat(%s) failed: %m\n", upperresolvconf);
318 goto upper_resolvconf_printf;
319 }
320 close(fd);
321
322 DEBUG("mount -t overlay %s %s (%s)\n", jail_root, jail_root, optsstr);
323
324 if (mount(jail_root, jail_root, "overlay", MS_NOATIME, optsstr))
325 goto opts_printf;
326
327 ret = 0;
328
329 upper_resolvconf_printf:
330 free(upperresolvconf);
331 upper_etc_printf:
332 free(upperetc);
333 opts_printf:
334 free(optsstr);
335 work_printf:
336 free(workdir);
337 upper_printf:
338 free(upperdir);
339 out:
340 return ret;
341 }
342
343 static void pass_console(int console_fd)
344 {
345 struct ubus_context *child_ctx = ubus_connect(NULL);
346 static struct blob_buf req;
347 uint32_t id;
348
349 if (!child_ctx)
350 return;
351
352 blob_buf_init(&req, 0);
353 blobmsg_add_string(&req, "name", opts.name);
354
355 if (ubus_lookup_id(child_ctx, "container", &id) ||
356 ubus_invoke_fd(child_ctx, id, "console_set", req.head, NULL, NULL, 3000, console_fd))
357 INFO("ubus request failed\n");
358 else
359 close(console_fd);
360
361 blob_buf_free(&req);
362 ubus_free(child_ctx);
363 }
364
365 static int create_dev_console(const char *jail_root)
366 {
367 char *console_fname;
368 char dev_console_path[PATH_MAX];
369 int slave_console_fd;
370
371 /* Open UNIX/98 virtual console */
372 console_fd = posix_openpt(O_RDWR | O_NOCTTY);
373 if (console_fd == -1)
374 return -1;
375
376 console_fname = ptsname(console_fd);
377 DEBUG("got console fd %d and PTS client name %s\n", console_fd, console_fname);
378 if (!console_fname)
379 goto no_console;
380
381 grantpt(console_fd);
382 unlockpt(console_fd);
383
384 /* pass PTY master to procd */
385 pass_console(console_fd);
386
387 /* mount-bind PTY slave to /dev/console in jail */
388 snprintf(dev_console_path, sizeof(dev_console_path), "%s/dev/console", jail_root);
389 close(creat(dev_console_path, 0620));
390
391 if (mount(console_fname, dev_console_path, NULL, MS_BIND, NULL))
392 goto no_console;
393
394 /* use PTY slave for stdio */
395 slave_console_fd = open(console_fname, O_RDWR); /* | O_NOCTTY */
396 dup2(slave_console_fd, 0);
397 dup2(slave_console_fd, 1);
398 dup2(slave_console_fd, 2);
399 close(slave_console_fd);
400
401 INFO("using guest console %s\n", console_fname);
402
403 return 0;
404
405 no_console:
406 close(console_fd);
407 return 1;
408 }
409
410 static int hook_running = 0;
411 static int hook_return_code = 0;
412 static struct hook_execvpe **current_hook = NULL;
413 typedef void (*hook_return_handler)(void);
414 static hook_return_handler hook_return_cb = NULL;
415
416 static void hook_process_timeout_cb(struct uloop_timeout *t);
417 static struct uloop_timeout hook_process_timeout = {
418 .cb = hook_process_timeout_cb,
419 };
420
421 static void run_hooklist(void);
422 static void hook_process_handler(struct uloop_process *c, int ret)
423 {
424 uloop_timeout_cancel(&hook_process_timeout);
425
426 if (WIFEXITED(ret)) {
427 hook_return_code = WEXITSTATUS(ret);
428 if (hook_return_code)
429 ERROR("hook (%d) exited with exit: %d\n", c->pid, hook_return_code);
430 else
431 DEBUG("hook (%d) exited with exit: %d\n", c->pid, hook_return_code);
432
433 } else {
434 hook_return_code = WTERMSIG(ret);
435 ERROR("hook (%d) exited with signal: %d\n", c->pid, hook_return_code);
436 }
437 hook_running = 0;
438 ++current_hook;
439 run_hooklist();
440 }
441
442 static struct uloop_process hook_process = {
443 .cb = hook_process_handler,
444 };
445
446 static void hook_process_timeout_cb(struct uloop_timeout *t)
447 {
448 DEBUG("hook process failed to stop, sending SIGKILL\n");
449 kill(hook_process.pid, SIGKILL);
450 }
451
452 static void run_hooklist(void)
453 {
454 struct hook_execvpe *hook = *current_hook;
455 struct stat s;
456
457 if (!hook)
458 hook_return_cb();
459
460 DEBUG("executing hook %s\n", hook->file);
461
462 if (stat(hook->file, &s))
463 hook_process_handler(&hook_process, ENOENT);
464
465 if (!((unsigned long)s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
466 hook_process_handler(&hook_process, EPERM);
467
468 if (!((unsigned long)s.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)))
469 hook_process_handler(&hook_process, EPERM);
470
471 hook_running = 1;
472 hook_process.pid = fork();
473 if (hook_process.pid == 0) {
474 /* child */
475 execve(hook->file, hook->argv, hook->envp);
476 ERROR("execve error %m\n");
477 _exit(errno);
478 } else if (hook_process.pid < 0) {
479 /* fork error */
480 ERROR("hook fork error\n");
481 hook_running = 0;
482 hook_process_handler(&hook_process, errno);
483 }
484
485 /* parent */
486 uloop_process_add(&hook_process);
487
488 if (hook->timeout > 0)
489 uloop_timeout_set(&hook_process_timeout, 1000 * hook->timeout);
490
491 uloop_run();
492 if (hook_running) {
493 DEBUG("uloop interrupted, killing jail process\n");
494 kill(hook_process.pid, SIGTERM);
495 uloop_timeout_set(&hook_process_timeout, 1000);
496 uloop_run();
497 }
498 }
499
500 static void run_hooks(struct hook_execvpe **hooklist, hook_return_handler return_cb)
501 {
502 if (!hooklist)
503 return_cb();
504
505 current_hook = hooklist;
506 hook_return_cb = return_cb;
507
508 run_hooklist();
509 }
510
511 static int apply_sysctl(const char *jail_root)
512 {
513 struct sysctl_val **cur;
514 char *procdir, *fname;
515 int f;
516
517 if (!opts.sysctl)
518 return 0;
519
520 asprintf(&procdir, "%s/proc", jail_root);
521 if (!procdir)
522 return ENOMEM;
523
524 mkdir(procdir, 0700);
525 if (mount("proc", procdir, "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0))
526 return EPERM;
527
528 cur = opts.sysctl;
529
530 while (*cur) {
531 asprintf(&fname, "%s/sys/%s", procdir, (*cur)->entry);
532 if (!fname)
533 return ENOMEM;
534
535 DEBUG("sysctl: writing '%s' to %s\n", (*cur)->value, fname);
536
537 f = open(fname, O_WRONLY);
538 if (f == -1) {
539 ERROR("sysctl: can't open %s\n", fname);
540 return errno;
541 }
542 write(f, (*cur)->value, strlen((*cur)->value));
543
544 free(fname);
545 close(f);
546 ++cur;
547 }
548 umount(procdir);
549 rmdir(procdir);
550 free(procdir);
551
552 return 0;
553 }
554
555 /* glibc defines makedev calling a function. make sure it's a pure macro */
556 #if defined(__GLIBC__)
557 #undef makedev
558 /* from musl's sys/sysmacros.h */
559 #define makedev(x,y) ( \
560 (((x)&0xfffff000ULL) << 32) | \
561 (((x)&0x00000fffULL) << 8) | \
562 (((y)&0xffffff00ULL) << 12) | \
563 (((y)&0x000000ffULL)) )
564 #endif
565
566 static struct mknod_args default_devices[] = {
567 { .path = "/dev/null", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 3) },
568 { .path = "/dev/zero", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 5) },
569 { .path = "/dev/full", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 7) },
570 { .path = "/dev/random", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 8) },
571 { .path = "/dev/urandom", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 9) },
572 { .path = "/dev/tty", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP), .dev = makedev(5, 0), .gid = 5 },
573 { 0 },
574 };
575
576 static int create_devices(void)
577 {
578 struct mknod_args **cur, *curdef;
579
580 if (!opts.devices)
581 goto only_default_devices;
582
583 cur = opts.devices;
584
585 while (*cur) {
586 DEBUG("creating %s (mode=%08o)\n", (*cur)->path, (*cur)->mode);
587 if (mknod((*cur)->path, (*cur)->mode, (*cur)->dev))
588 return errno;
589
590 if (((*cur)->uid || (*cur)->gid) &&
591 chown((*cur)->path, (*cur)->uid, (*cur)->gid))
592 return errno;
593
594 ++cur;
595 }
596
597 only_default_devices:
598 curdef = default_devices;
599 while(curdef->path) {
600 DEBUG("creating %s (mode=%08o)\n", curdef->path, curdef->mode);
601 if (mknod(curdef->path, curdef->mode, curdef->dev)) {
602 ++curdef;
603 continue; /* may already exist, eg. due to a bind-mount */
604 }
605 if ((curdef->uid || curdef->gid) &&
606 chown(curdef->path, curdef->uid, curdef->gid))
607 return errno;
608
609 ++curdef;
610 }
611
612 /* Dev symbolic links as defined in OCI spec */
613 symlink("/dev/pts/ptmx", "/dev/ptmx");
614 symlink("/proc/self/fd", "/dev/fd");
615 symlink("/proc/self/fd/0", "/dev/stdin");
616 symlink("/proc/self/fd/1", "/dev/stdout");
617 symlink("/proc/self/fd/2", "/dev/stderr");
618
619 return 0;
620 }
621
622 static char jail_root[] = "/tmp/ujail-XXXXXX";
623 static char tmpovdir[] = "/tmp/ujail-overlay-XXXXXX";
624 static mode_t old_umask;
625 static void enter_jail_fs(void);
626 static int build_jail_fs(void)
627 {
628 char *overlaydir = NULL;
629
630 old_umask = umask(0);
631
632 if (mkdtemp(jail_root) == NULL) {
633 ERROR("mkdtemp(%s) failed: %m\n", jail_root);
634 return -1;
635 }
636
637 if (apply_sysctl(jail_root)) {
638 ERROR("failed to apply sysctl values\n");
639 return -1;
640 }
641
642 /* oldroot can't be MS_SHARED else pivot_root() fails */
643 if (mount("none", "/", NULL, MS_REC|MS_PRIVATE, NULL)) {
644 ERROR("private mount failed %m\n");
645 return -1;
646 }
647
648 if (opts.extroot) {
649 if (mount(opts.extroot, jail_root, NULL, MS_BIND, NULL)) {
650 ERROR("extroot mount failed %m\n");
651 return -1;
652 }
653 } else {
654 if (mount("tmpfs", jail_root, "tmpfs", MS_NOATIME, "mode=0755")) {
655 ERROR("tmpfs mount failed %m\n");
656 return -1;
657 }
658 }
659
660 if (opts.tmpoverlaysize) {
661 char mountoptsstr[] = "mode=0755,size=XXXXXXXX";
662
663 snprintf(mountoptsstr, sizeof(mountoptsstr),
664 "mode=0755,size=%s", opts.tmpoverlaysize);
665 if (mkdtemp(tmpovdir) == NULL) {
666 ERROR("mkdtemp(%s) failed: %m\n", jail_root);
667 return -1;
668 }
669 if (mount("tmpfs", tmpovdir, "tmpfs", MS_NOATIME,
670 mountoptsstr)) {
671 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts.tmpoverlaysize);
672 return -1;
673 }
674 overlaydir = tmpovdir;
675 }
676
677 if (opts.overlaydir)
678 overlaydir = opts.overlaydir;
679
680 if (overlaydir)
681 mount_overlay(jail_root, overlaydir);
682
683 if (chdir(jail_root)) {
684 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root);
685 return -1;
686 }
687
688 if (mount_all(jail_root)) {
689 ERROR("mount_all() failed\n");
690 return -1;
691 }
692
693 if (opts.console)
694 create_dev_console(jail_root);
695
696 /* make sure /etc/resolv.conf exists if in new network namespace */
697 if (opts.namespace & CLONE_NEWNET) {
698 char jailetc[PATH_MAX], jaillink[PATH_MAX];
699
700 snprintf(jailetc, PATH_MAX, "%s/etc", jail_root);
701 mkdir_p(jailetc, 0755);
702 snprintf(jaillink, PATH_MAX, "%s/etc/resolv.conf", jail_root);
703 if (overlaydir)
704 unlink(jaillink);
705
706 symlink("../dev/resolv.conf.d/resolv.conf.auto", jaillink);
707 }
708
709 run_hooks(opts.hooks.createContainer, enter_jail_fs);
710
711 return 0;
712 }
713
714 static bool exit_from_child;
715 static void free_and_exit(int ret)
716 {
717 if (!exit_from_child && opts.ocibundle)
718 cgroups_free();
719
720 if (!exit_from_child && parent_ctx)
721 ubus_free(parent_ctx);
722
723 free_opts(!exit_from_child);
724
725 exit(ret);
726 }
727
728 static void post_jail_fs(void);
729 static void enter_jail_fs(void)
730 {
731 char dirbuf[sizeof(jail_root) + 4];
732
733 snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
734 mkdir(dirbuf, 0755);
735
736 if (pivot_root(jail_root, dirbuf) == -1) {
737 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root, dirbuf);
738 free_and_exit(-1);
739 }
740 if (chdir("/")) {
741 ERROR("chdir(/) (after pivot_root) failed: %m\n");
742 free_and_exit(-1);
743 }
744
745 snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
746 umount2(dirbuf, MNT_DETACH);
747 rmdir(dirbuf);
748 if (opts.tmpoverlaysize) {
749 char tmpdirbuf[sizeof(tmpovdir) + 4];
750 snprintf(tmpdirbuf, sizeof(tmpdirbuf), "/old%s", tmpovdir);
751 umount2(tmpdirbuf, MNT_DETACH);
752 rmdir(tmpdirbuf);
753 }
754
755 umount2("/old", MNT_DETACH);
756 rmdir("/old");
757
758 if (create_devices()) {
759 ERROR("create_devices() failed\n");
760 free_and_exit(-1);
761 }
762 if (opts.ronly)
763 mount(NULL, "/", NULL, MS_REMOUNT | MS_BIND | MS_RDONLY, 0);
764
765 umask(old_umask);
766 post_jail_fs();
767 }
768
769 static int write_uid_gid_map(pid_t child_pid, bool gidmap, char *mapstr)
770 {
771 int map_file;
772 char map_path[64];
773
774 if (snprintf(map_path, sizeof(map_path), "/proc/%d/%s",
775 child_pid, gidmap?"gid_map":"uid_map") < 0)
776 return -1;
777
778 if ((map_file = open(map_path, O_WRONLY)) == -1)
779 return -1;
780
781 if (dprintf(map_file, "%s", mapstr)) {
782 close(map_file);
783 return -1;
784 }
785
786 close(map_file);
787 return 0;
788 }
789
790 static int write_single_uid_gid_map(pid_t child_pid, bool gidmap, int id)
791 {
792 int map_file;
793 char map_path[64];
794 const char *map_format = "%d %d %d\n";
795 if (snprintf(map_path, sizeof(map_path), "/proc/%d/%s",
796 child_pid, gidmap?"gid_map":"uid_map") < 0)
797 return -1;
798
799 if ((map_file = open(map_path, O_WRONLY)) == -1)
800 return -1;
801
802 if (dprintf(map_file, map_format, 0, id, 1) == -1) {
803 close(map_file);
804 return -1;
805 }
806
807 close(map_file);
808 return 0;
809 }
810
811 static int write_setgroups(pid_t child_pid, bool allow)
812 {
813 int setgroups_file;
814 char setgroups_path[64];
815
816 if (snprintf(setgroups_path, sizeof(setgroups_path), "/proc/%d/setgroups",
817 child_pid) < 0) {
818 return -1;
819 }
820
821 if ((setgroups_file = open(setgroups_path, O_WRONLY)) == -1) {
822 return -1;
823 }
824
825 if (dprintf(setgroups_file, "%s", allow?"allow":"deny") == -1) {
826 close(setgroups_file);
827 return -1;
828 }
829
830 close(setgroups_file);
831 return 0;
832 }
833
834 static void get_jail_user(int *user, int *user_gid, int *gr_gid)
835 {
836 struct passwd *p = NULL;
837 struct group *g = NULL;
838
839 if (opts.user) {
840 p = getpwnam(opts.user);
841 if (!p) {
842 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
843 opts.user, errno, strerror(errno));
844 free_and_exit(EXIT_FAILURE);
845 }
846 *user = p->pw_uid;
847 *user_gid = p->pw_gid;
848 } else {
849 *user = -1;
850 *user_gid = -1;
851 }
852
853 if (opts.group) {
854 g = getgrnam(opts.group);
855 if (!g) {
856 ERROR("failed to get gid for group %s: %m\n", opts.group);
857 free_and_exit(EXIT_FAILURE);
858 }
859 *gr_gid = g->gr_gid;
860 } else {
861 *gr_gid = -1;
862 }
863 };
864
865 static void set_jail_user(int pw_uid, int user_gid, int gr_gid)
866 {
867 if (opts.user && (user_gid != -1) && initgroups(opts.user, user_gid)) {
868 ERROR("failed to initgroups() for user %s: %m\n", opts.user);
869 free_and_exit(EXIT_FAILURE);
870 }
871
872 if ((gr_gid != -1) && setregid(gr_gid, gr_gid)) {
873 ERROR("failed to set group id %d: %m\n", gr_gid);
874 free_and_exit(EXIT_FAILURE);
875 }
876
877 if ((pw_uid != -1) && setreuid(pw_uid, pw_uid)) {
878 ERROR("failed to set user id %d: %m\n", pw_uid);
879 free_and_exit(EXIT_FAILURE);
880 }
881 }
882
883 static int apply_rlimits(void)
884 {
885 int resource;
886
887 for (resource = 0; resource < RLIM_NLIMITS; ++resource) {
888 if (opts.rlimits[resource])
889 DEBUG("applying limits to resource %u\n", resource);
890
891 if (opts.rlimits[resource] &&
892 setrlimit(resource, opts.rlimits[resource]))
893 return errno;
894 }
895
896 return 0;
897 }
898
899 #define MAX_ENVP 8
900 static char** build_envp(const char *seccomp, char **ocienvp)
901 {
902 static char *envp[MAX_ENVP];
903 static char preload_var[PATH_MAX];
904 static char seccomp_var[PATH_MAX];
905 static char debug_var[] = "LD_DEBUG=all";
906 static char container_var[] = "container=ujail";
907 const char *preload_lib = find_lib("libpreload-seccomp.so");
908 char **addenv;
909
910 int count = 0;
911
912 if (seccomp && !preload_lib) {
913 ERROR("failed to add preload-lib to env\n");
914 return NULL;
915 }
916 if (seccomp) {
917 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
918 envp[count++] = seccomp_var;
919 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
920 envp[count++] = preload_var;
921 }
922
923 envp[count++] = container_var;
924
925 if (debug > 1)
926 envp[count++] = debug_var;
927
928 addenv = ocienvp;
929 while (addenv && *addenv) {
930 envp[count++] = *(addenv++);
931 if (count >= MAX_ENVP) {
932 ERROR("environment limited to %d extra records, truncating\n", MAX_ENVP);
933 break;
934 }
935 }
936 return envp;
937 }
938
939 static void usage(void)
940 {
941 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
942 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
943 fprintf(stderr, " -S <file>\tseccomp filter config\n");
944 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
945 fprintf(stderr, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
946 fprintf(stderr, " -n <name>\tthe name of the jail\n");
947 fprintf(stderr, "namespace jail options:\n");
948 fprintf(stderr, " -h <hostname>\tchange the hostname of the jail\n");
949 fprintf(stderr, " -N\t\tjail has network namespace\n");
950 fprintf(stderr, " -f\t\tjail has user namespace\n");
951 fprintf(stderr, " -F\t\tjail has cgroups namespace\n");
952 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
953 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
954 fprintf(stderr, " -p\t\tjail has /proc\n");
955 fprintf(stderr, " -s\t\tjail has /sys\n");
956 fprintf(stderr, " -l\t\tjail has /dev/log\n");
957 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
958 fprintf(stderr, " -U <name>\tuser to run jailed process\n");
959 fprintf(stderr, " -G <name>\tgroup to run jailed process\n");
960 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
961 fprintf(stderr, " -R <dir>\texternal jail rootfs (system container)\n");
962 fprintf(stderr, " -O <dir>\tdirectory for r/w overlayfs\n");
963 fprintf(stderr, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
964 fprintf(stderr, " -E\t\tfail if jail cannot be setup\n");
965 fprintf(stderr, " -y\t\tprovide jail console\n");
966 fprintf(stderr, " -J <dir>\tcreate container from OCI bundle\n");
967 fprintf(stderr, " -j\t\tstart container immediately\n");
968 fprintf(stderr, " -P <pidfile>\tcreate <pidfile>\n");
969 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
970 and he has the same powers as root outside the jail,\n\
971 thus he can escape the jail and/or break stuff.\n\
972 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
973 If you use none of the namespace jail options,\n\
974 ujail will not use namespace/build a jail,\n\
975 and will only drop capabilities/apply seccomp filter.\n\n");
976 }
977
978 static int* get_namespace_fd(const unsigned int nstype)
979 {
980 switch (nstype) {
981 case CLONE_NEWPID:
982 return &opts.setns.pid;
983 case CLONE_NEWNET:
984 return &opts.setns.net;
985 case CLONE_NEWNS:
986 return &opts.setns.ns;
987 case CLONE_NEWIPC:
988 return &opts.setns.ipc;
989 case CLONE_NEWUTS:
990 return &opts.setns.uts;
991 case CLONE_NEWUSER:
992 return &opts.setns.user;
993 case CLONE_NEWCGROUP:
994 return &opts.setns.cgroup;
995 #ifdef CLONE_NEWTIME
996 case CLONE_NEWTIME:
997 return &opts.setns.time;
998 #endif
999 default:
1000 return NULL;
1001 }
1002 }
1003
1004 static int setns_open(unsigned long nstype)
1005 {
1006 int *fd = get_namespace_fd(nstype);
1007
1008 if (!*fd)
1009 return EFAULT;
1010
1011 if (*fd == -1)
1012 return 0;
1013
1014 if (setns(*fd, nstype) == -1) {
1015 close(*fd);
1016 return errno;
1017 }
1018
1019 close(*fd);
1020 return 0;
1021 }
1022
1023 static int jail_running = 0;
1024 static int jail_return_code = 0;
1025
1026 static void jail_process_timeout_cb(struct uloop_timeout *t);
1027 static struct uloop_timeout jail_process_timeout = {
1028 .cb = jail_process_timeout_cb,
1029 };
1030 static void poststop(void);
1031 static void jail_process_handler(struct uloop_process *c, int ret)
1032 {
1033 uloop_timeout_cancel(&jail_process_timeout);
1034 if (WIFEXITED(ret)) {
1035 jail_return_code = WEXITSTATUS(ret);
1036 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
1037 } else {
1038 jail_return_code = WTERMSIG(ret);
1039 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
1040 }
1041 jail_running = 0;
1042 poststop();
1043 }
1044
1045 static struct uloop_process jail_process = {
1046 .cb = jail_process_handler,
1047 };
1048
1049 static void jail_process_timeout_cb(struct uloop_timeout *t)
1050 {
1051 DEBUG("jail process failed to stop, sending SIGKILL\n");
1052 kill(jail_process.pid, SIGKILL);
1053 }
1054
1055 static void jail_handle_signal(int signo)
1056 {
1057 if (hook_running) {
1058 DEBUG("forwarding signal %d to the hook process\n", signo);
1059 kill(hook_process.pid, signo);
1060 }
1061
1062 if (jail_running) {
1063 DEBUG("forwarding signal %d to the jailed process\n", signo);
1064 kill(jail_process.pid, signo);
1065 }
1066 }
1067
1068 static void signals_init(void)
1069 {
1070 int i;
1071 sigset_t sigmask;
1072
1073 sigfillset(&sigmask);
1074 for (i = 0; i < _NSIG; i++) {
1075 struct sigaction s = { 0 };
1076
1077 if (!sigismember(&sigmask, i))
1078 continue;
1079 if ((i == SIGCHLD) || (i == SIGPIPE) || (i == SIGSEGV))
1080 continue;
1081
1082 s.sa_handler = jail_handle_signal;
1083 sigaction(i, &s, NULL);
1084 }
1085 }
1086
1087 static void pre_exec_jail(struct uloop_timeout *t);
1088 static struct uloop_timeout pre_exec_timeout = {
1089 .cb = pre_exec_jail,
1090 };
1091
1092 int pipes[4];
1093 static int exec_jail(void *arg)
1094 {
1095 char buf[1];
1096
1097 exit_from_child = true;
1098 uloop_init();
1099 signals_init();
1100
1101 close(pipes[0]);
1102 close(pipes[3]);
1103
1104 setns_open(CLONE_NEWUSER);
1105 setns_open(CLONE_NEWNET);
1106 setns_open(CLONE_NEWNS);
1107 setns_open(CLONE_NEWIPC);
1108 setns_open(CLONE_NEWUTS);
1109
1110 buf[0] = 'i';
1111 if (write(pipes[1], buf, 1) < 1) {
1112 ERROR("can't write to parent\n");
1113 return EXIT_FAILURE;
1114 }
1115 close(pipes[1]);
1116 if (read(pipes[2], buf, 1) < 1) {
1117 ERROR("can't read from parent\n");
1118 return EXIT_FAILURE;
1119 }
1120 if (buf[0] != 'O') {
1121 ERROR("parent had an error, child exiting\n");
1122 return EXIT_FAILURE;
1123 }
1124
1125 if (opts.namespace & CLONE_NEWCGROUP)
1126 unshare(CLONE_NEWCGROUP);
1127
1128 if ((opts.namespace & CLONE_NEWUSER) || (opts.setns.user != -1)) {
1129 if (setregid(0, 0) < 0) {
1130 ERROR("setgid\n");
1131 free_and_exit(EXIT_FAILURE);
1132 }
1133 if (setreuid(0, 0) < 0) {
1134 ERROR("setuid\n");
1135 free_and_exit(EXIT_FAILURE);
1136 }
1137 if (setgroups(0, NULL) < 0) {
1138 ERROR("setgroups\n");
1139 free_and_exit(EXIT_FAILURE);
1140 }
1141 }
1142
1143 if (opts.namespace && opts.hostname && strlen(opts.hostname) > 0
1144 && sethostname(opts.hostname, strlen(opts.hostname))) {
1145 ERROR("sethostname(%s) failed: %m\n", opts.hostname);
1146 free_and_exit(EXIT_FAILURE);
1147 }
1148
1149 uloop_timeout_add(&pre_exec_timeout);
1150 uloop_run();
1151
1152 free_and_exit(-1);
1153 return -1;
1154 }
1155
1156 static void pre_exec_jail(struct uloop_timeout *t)
1157 {
1158 if ((opts.namespace & CLONE_NEWNS) && build_jail_fs()) {
1159 ERROR("failed to build jail fs\n");
1160 free_and_exit(EXIT_FAILURE);
1161 } else {
1162 run_hooks(opts.hooks.createContainer, post_jail_fs);
1163 }
1164 }
1165
1166 static void post_start_hook(void);
1167 static void post_jail_fs(void)
1168 {
1169 char buf[1];
1170
1171 if (read(pipes[2], buf, 1) < 1) {
1172 ERROR("can't read from parent\n");
1173 free_and_exit(EXIT_FAILURE);
1174 }
1175 if (buf[0] != '!') {
1176 ERROR("parent had an error, child exiting\n");
1177 free_and_exit(EXIT_FAILURE);
1178 }
1179 close(pipes[2]);
1180
1181 run_hooks(opts.hooks.startContainer, post_start_hook);
1182 }
1183
1184 static void post_start_hook(void)
1185 {
1186 int pw_uid, pw_gid, gr_gid;
1187
1188 /*
1189 * make sure setuid/setgid won't drop capabilities in case capabilities
1190 * have been specified explicitely.
1191 */
1192 if (opts.capset.apply) {
1193 if (prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP)) {
1194 ERROR("prctl(PR_SET_SECUREBITS) failed: %m\n");
1195 free_and_exit(EXIT_FAILURE);
1196 }
1197 }
1198
1199 /* drop capabilities, retain those still needed to further setup jail */
1200 if (applyOCIcapabilities(opts.capset, (1LLU << CAP_SETGID) | (1LLU << CAP_SETUID) | (1LLU << CAP_SETPCAP)))
1201 free_and_exit(EXIT_FAILURE);
1202
1203 /* use either cmdline-supplied user/group or uid/gid from OCI spec */
1204 get_jail_user(&pw_uid, &pw_gid, &gr_gid);
1205 set_jail_user(opts.pw_uid?:pw_uid, opts.pw_gid?:pw_gid, opts.gr_gid?:gr_gid);
1206
1207 if (opts.additional_gids &&
1208 (setgroups(opts.num_additional_gids, opts.additional_gids) < 0)) {
1209 ERROR("setgroups failed: %m\n");
1210 free_and_exit(EXIT_FAILURE);
1211 }
1212
1213 if (opts.set_umask)
1214 umask(opts.umask);
1215
1216 /* restore securebits back to normal */
1217 if (opts.capset.apply) {
1218 if (prctl(PR_SET_SECUREBITS, 0)) {
1219 ERROR("prctl(PR_SET_SECUREBITS) failed: %m\n");
1220 free_and_exit(EXIT_FAILURE);
1221 }
1222 }
1223
1224 /* drop remaining capabilities to end up with specified sets */
1225 if (applyOCIcapabilities(opts.capset, 0))
1226 free_and_exit(EXIT_FAILURE);
1227
1228 if (opts.no_new_privs && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
1229 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
1230 free_and_exit(EXIT_FAILURE);
1231 }
1232
1233 char **envp = build_envp(opts.seccomp, opts.envp);
1234 if (!envp)
1235 free_and_exit(EXIT_FAILURE);
1236
1237 if (opts.cwd && chdir(opts.cwd))
1238 free_and_exit(EXIT_FAILURE);
1239
1240 if (opts.ociseccomp && applyOCIlinuxseccomp(opts.ociseccomp))
1241 free_and_exit(EXIT_FAILURE);
1242
1243 uloop_end();
1244 free_opts(false);
1245 INFO("exec-ing %s\n", *opts.jail_argv);
1246 if (opts.envp) /* respect PATH if potentially set in ENV */
1247 execvpe(*opts.jail_argv, opts.jail_argv, envp);
1248 else
1249 execve(*opts.jail_argv, opts.jail_argv, envp);
1250
1251 /* we get there only if execve fails */
1252 ERROR("failed to execve %s: %m\n", *opts.jail_argv);
1253 exit(EXIT_FAILURE);
1254 }
1255
1256 static int ns_open_pid(const char *nstype, const pid_t target_ns)
1257 {
1258 char pid_pid_path[PATH_MAX];
1259
1260 snprintf(pid_pid_path, sizeof(pid_pid_path), "/proc/%u/ns/%s", target_ns, nstype);
1261
1262 return open(pid_pid_path, O_RDONLY);
1263 }
1264
1265 static void netns_updown(pid_t pid, bool start)
1266 {
1267 static struct blob_buf req;
1268 uint32_t id;
1269
1270 if (!parent_ctx)
1271 return;
1272
1273 blob_buf_init(&req, 0);
1274 blobmsg_add_string(&req, "jail", opts.name);
1275 blobmsg_add_u32(&req, "pid", pid);
1276 blobmsg_add_u8(&req, "start", start);
1277
1278 if (ubus_lookup_id(parent_ctx, "network", &id) ||
1279 ubus_invoke(parent_ctx, id, "netns_updown", req.head, NULL, NULL, 3000))
1280 INFO("ubus request failed\n");
1281
1282 blob_buf_free(&req);
1283 }
1284
1285 static int parseOCIenvarray(struct blob_attr *msg, char ***envp)
1286 {
1287 struct blob_attr *cur;
1288 int sz = 0, rem;
1289
1290 blobmsg_for_each_attr(cur, msg, rem)
1291 ++sz;
1292
1293 if (sz > 0) {
1294 *envp = calloc(1 + sz, sizeof(char*));
1295 if (!(*envp))
1296 return ENOMEM;
1297 } else {
1298 *envp = NULL;
1299 return 0;
1300 }
1301
1302 sz = 0;
1303 blobmsg_for_each_attr(cur, msg, rem)
1304 (*envp)[sz++] = strdup(blobmsg_get_string(cur));
1305
1306 if (sz)
1307 (*envp)[sz] = NULL;
1308
1309 return 0;
1310 }
1311
1312 enum {
1313 OCI_ROOT_PATH,
1314 OCI_ROOT_READONLY,
1315 __OCI_ROOT_MAX,
1316 };
1317
1318 static const struct blobmsg_policy oci_root_policy[] = {
1319 [OCI_ROOT_PATH] = { "path", BLOBMSG_TYPE_STRING },
1320 [OCI_ROOT_READONLY] = { "readonly", BLOBMSG_TYPE_BOOL },
1321 };
1322
1323 static int parseOCIroot(const char *jsonfile, struct blob_attr *msg)
1324 {
1325 static char extroot[PATH_MAX] = { 0 };
1326 struct blob_attr *tb[__OCI_ROOT_MAX];
1327 char *cur;
1328 char *root_path;
1329
1330 blobmsg_parse(oci_root_policy, __OCI_ROOT_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1331
1332 if (!tb[OCI_ROOT_PATH])
1333 return ENODATA;
1334
1335 root_path = blobmsg_get_string(tb[OCI_ROOT_PATH]);
1336
1337 /* prepend bundle directory in case of relative paths */
1338 if (root_path[0] != '/') {
1339 strncpy(extroot, jsonfile, PATH_MAX);
1340 cur = strrchr(extroot, '/');
1341
1342 if (!cur)
1343 return ENOTDIR;
1344
1345 *(++cur) = '\0';
1346 }
1347
1348 strncat(extroot, root_path, PATH_MAX - (strlen(extroot) + 1));
1349
1350 opts.extroot = extroot;
1351
1352 if (tb[OCI_ROOT_READONLY])
1353 opts.ronly = blobmsg_get_bool(tb[OCI_ROOT_READONLY]);
1354
1355 return 0;
1356 }
1357
1358
1359 enum {
1360 OCI_HOOK_PATH,
1361 OCI_HOOK_ARGS,
1362 OCI_HOOK_ENV,
1363 OCI_HOOK_TIMEOUT,
1364 __OCI_HOOK_MAX,
1365 };
1366
1367 static const struct blobmsg_policy oci_hook_policy[] = {
1368 [OCI_HOOK_PATH] = { "path", BLOBMSG_TYPE_STRING },
1369 [OCI_HOOK_ARGS] = { "args", BLOBMSG_TYPE_ARRAY },
1370 [OCI_HOOK_ENV] = { "env", BLOBMSG_TYPE_ARRAY },
1371 [OCI_HOOK_TIMEOUT] = { "timeout", BLOBMSG_TYPE_INT32 },
1372 };
1373
1374
1375 static int parseOCIhook(struct hook_execvpe ***hooklist, struct blob_attr *msg)
1376 {
1377 struct blob_attr *tb[__OCI_HOOK_MAX];
1378 struct blob_attr *cur;
1379 int rem, ret = 0;
1380 int idx = 0;
1381
1382 blobmsg_for_each_attr(cur, msg, rem)
1383 ++idx;
1384
1385 if (!idx)
1386 return 0;
1387
1388 *hooklist = calloc(idx + 1, sizeof(struct hook_execvpe *));
1389 idx = 0;
1390
1391 if (!(*hooklist))
1392 return ENOMEM;
1393
1394 blobmsg_for_each_attr(cur, msg, rem) {
1395 blobmsg_parse(oci_hook_policy, __OCI_HOOK_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
1396
1397 if (!tb[OCI_HOOK_PATH]) {
1398 ret = EINVAL;
1399 goto errout;
1400 }
1401
1402 (*hooklist)[idx] = calloc(1, sizeof(struct hook_execvpe));
1403 if (tb[OCI_HOOK_ARGS]) {
1404 ret = parseOCIenvarray(tb[OCI_HOOK_ARGS], &((*hooklist)[idx]->argv));
1405 if (ret)
1406 goto errout;
1407 } else {
1408 (*hooklist)[idx]->argv = calloc(2, sizeof(char *));
1409 ((*hooklist)[idx]->argv)[0] = strdup(blobmsg_get_string(tb[OCI_HOOK_PATH]));
1410 ((*hooklist)[idx]->argv)[1] = NULL;
1411 };
1412
1413
1414 if (tb[OCI_HOOK_ENV]) {
1415 ret = parseOCIenvarray(tb[OCI_HOOK_ENV], &((*hooklist)[idx]->envp));
1416 if (ret)
1417 goto errout;
1418 }
1419
1420 if (tb[OCI_HOOK_TIMEOUT])
1421 (*hooklist)[idx]->timeout = blobmsg_get_u32(tb[OCI_HOOK_TIMEOUT]);
1422
1423 (*hooklist)[idx]->file = strdup(blobmsg_get_string(tb[OCI_HOOK_PATH]));
1424
1425 ++idx;
1426 }
1427
1428 (*hooklist)[idx] = NULL;
1429
1430 DEBUG("added %d hooks\n", idx);
1431
1432 return 0;
1433
1434 errout:
1435 free_hooklist(*hooklist);
1436 *hooklist = NULL;
1437
1438 return ret;
1439 };
1440
1441
1442 enum {
1443 OCI_HOOKS_PRESTART,
1444 OCI_HOOKS_CREATERUNTIME,
1445 OCI_HOOKS_CREATECONTAINER,
1446 OCI_HOOKS_STARTCONTAINER,
1447 OCI_HOOKS_POSTSTART,
1448 OCI_HOOKS_POSTSTOP,
1449 __OCI_HOOKS_MAX,
1450 };
1451
1452 static const struct blobmsg_policy oci_hooks_policy[] = {
1453 [OCI_HOOKS_PRESTART] = { "prestart", BLOBMSG_TYPE_ARRAY },
1454 [OCI_HOOKS_CREATERUNTIME] = { "createRuntime", BLOBMSG_TYPE_ARRAY },
1455 [OCI_HOOKS_CREATECONTAINER] = { "createContainer", BLOBMSG_TYPE_ARRAY },
1456 [OCI_HOOKS_STARTCONTAINER] = { "startContainer", BLOBMSG_TYPE_ARRAY },
1457 [OCI_HOOKS_POSTSTART] = { "poststart", BLOBMSG_TYPE_ARRAY },
1458 [OCI_HOOKS_POSTSTOP] = { "poststop", BLOBMSG_TYPE_ARRAY },
1459 };
1460
1461 static int parseOCIhooks(struct blob_attr *msg)
1462 {
1463 struct blob_attr *tb[__OCI_HOOKS_MAX];
1464 int ret;
1465
1466 blobmsg_parse(oci_hooks_policy, __OCI_HOOKS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1467
1468 if (tb[OCI_HOOKS_PRESTART])
1469 INFO("warning: ignoring deprecated prestart hook\n");
1470
1471 if (tb[OCI_HOOKS_CREATERUNTIME]) {
1472 ret = parseOCIhook(&opts.hooks.createRuntime, tb[OCI_HOOKS_CREATERUNTIME]);
1473 if (ret)
1474 return ret;
1475 }
1476
1477 if (tb[OCI_HOOKS_CREATECONTAINER]) {
1478 ret = parseOCIhook(&opts.hooks.createContainer, tb[OCI_HOOKS_CREATECONTAINER]);
1479 if (ret)
1480 goto out_createruntime;
1481 }
1482
1483 if (tb[OCI_HOOKS_STARTCONTAINER]) {
1484 ret = parseOCIhook(&opts.hooks.startContainer, tb[OCI_HOOKS_STARTCONTAINER]);
1485 if (ret)
1486 goto out_createcontainer;
1487 }
1488
1489 if (tb[OCI_HOOKS_POSTSTART]) {
1490 ret = parseOCIhook(&opts.hooks.poststart, tb[OCI_HOOKS_POSTSTART]);
1491 if (ret)
1492 goto out_startcontainer;
1493 }
1494
1495 if (tb[OCI_HOOKS_POSTSTOP]) {
1496 ret = parseOCIhook(&opts.hooks.poststop, tb[OCI_HOOKS_POSTSTOP]);
1497 if (ret)
1498 goto out_poststart;
1499 }
1500
1501 return 0;
1502
1503 out_poststart:
1504 free_hooklist(opts.hooks.poststart);
1505 out_startcontainer:
1506 free_hooklist(opts.hooks.startContainer);
1507 out_createcontainer:
1508 free_hooklist(opts.hooks.createContainer);
1509 out_createruntime:
1510 free_hooklist(opts.hooks.createRuntime);
1511
1512 return ret;
1513 };
1514
1515
1516 enum {
1517 OCI_PROCESS_USER_UID,
1518 OCI_PROCESS_USER_GID,
1519 OCI_PROCESS_USER_UMASK,
1520 OCI_PROCESS_USER_ADDITIONALGIDS,
1521 __OCI_PROCESS_USER_MAX,
1522 };
1523
1524 static const struct blobmsg_policy oci_process_user_policy[] = {
1525 [OCI_PROCESS_USER_UID] = { "uid", BLOBMSG_TYPE_INT32 },
1526 [OCI_PROCESS_USER_GID] = { "gid", BLOBMSG_TYPE_INT32 },
1527 [OCI_PROCESS_USER_UMASK] = { "umask", BLOBMSG_TYPE_INT32 },
1528 [OCI_PROCESS_USER_ADDITIONALGIDS] = { "additionalGids", BLOBMSG_TYPE_ARRAY },
1529 };
1530
1531 static int parseOCIprocessuser(struct blob_attr *msg) {
1532 struct blob_attr *tb[__OCI_PROCESS_USER_MAX];
1533 struct blob_attr *cur;
1534 int rem;
1535 int has_gid = 0;
1536
1537 blobmsg_parse(oci_process_user_policy, __OCI_PROCESS_USER_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1538
1539 if (tb[OCI_PROCESS_USER_UID])
1540 opts.pw_uid = blobmsg_get_u32(tb[OCI_PROCESS_USER_UID]);
1541
1542 if (tb[OCI_PROCESS_USER_GID]) {
1543 opts.pw_gid = blobmsg_get_u32(tb[OCI_PROCESS_USER_GID]);
1544 opts.gr_gid = blobmsg_get_u32(tb[OCI_PROCESS_USER_GID]);
1545 has_gid = 1;
1546 }
1547
1548 if (tb[OCI_PROCESS_USER_ADDITIONALGIDS]) {
1549 size_t gidcnt = 0;
1550
1551 blobmsg_for_each_attr(cur, tb[OCI_PROCESS_USER_ADDITIONALGIDS], rem) {
1552 ++gidcnt;
1553 if (has_gid && (blobmsg_get_u32(cur) == opts.gr_gid))
1554 continue;
1555 }
1556
1557 if (gidcnt) {
1558 opts.additional_gids = calloc(gidcnt + has_gid, sizeof(gid_t));
1559 gidcnt = 0;
1560
1561 /* always add primary GID to set of GIDs if set */
1562 if (has_gid)
1563 opts.additional_gids[gidcnt++] = opts.gr_gid;
1564
1565 blobmsg_for_each_attr(cur, tb[OCI_PROCESS_USER_ADDITIONALGIDS], rem) {
1566 if (has_gid && (blobmsg_get_u32(cur) == opts.gr_gid))
1567 continue;
1568 opts.additional_gids[gidcnt++] = blobmsg_get_u32(cur);
1569 }
1570 opts.num_additional_gids = gidcnt;
1571 }
1572 DEBUG("read %zu additional groups\n", gidcnt);
1573 }
1574
1575 if (tb[OCI_PROCESS_USER_UMASK]) {
1576 opts.umask = blobmsg_get_u32(tb[OCI_PROCESS_USER_UMASK]);
1577 opts.set_umask = true;
1578 }
1579
1580 return 0;
1581 }
1582
1583 enum {
1584 OCI_PROCESS_RLIMIT_TYPE,
1585 OCI_PROCESS_RLIMIT_SOFT,
1586 OCI_PROCESS_RLIMIT_HARD,
1587 __OCI_PROCESS_RLIMIT_MAX,
1588 };
1589
1590 static const struct blobmsg_policy oci_process_rlimit_policy[] = {
1591 [OCI_PROCESS_RLIMIT_TYPE] = { "type", BLOBMSG_TYPE_STRING },
1592 [OCI_PROCESS_RLIMIT_SOFT] = { "soft", BLOBMSG_CAST_INT64 },
1593 [OCI_PROCESS_RLIMIT_HARD] = { "hard", BLOBMSG_CAST_INT64 },
1594 };
1595
1596 /* from manpage GETRLIMIT(2) */
1597 static const char* const rlimit_names[RLIM_NLIMITS] = {
1598 [RLIMIT_AS] = "AS",
1599 [RLIMIT_CORE] = "CORE",
1600 [RLIMIT_CPU] = "CPU",
1601 [RLIMIT_DATA] = "DATA",
1602 [RLIMIT_FSIZE] = "FSIZE",
1603 [RLIMIT_LOCKS] = "LOCKS",
1604 [RLIMIT_MEMLOCK] = "MEMLOCK",
1605 [RLIMIT_MSGQUEUE] = "MSGQUEUE",
1606 [RLIMIT_NICE] = "NICE",
1607 [RLIMIT_NOFILE] = "NOFILE",
1608 [RLIMIT_NPROC] = "NPROC",
1609 [RLIMIT_RSS] = "RSS",
1610 [RLIMIT_RTPRIO] = "RTPRIO",
1611 [RLIMIT_RTTIME] = "RTTIME",
1612 [RLIMIT_SIGPENDING] = "SIGPENDING",
1613 [RLIMIT_STACK] = "STACK",
1614 };
1615
1616 static int resolve_rlimit(char *type) {
1617 unsigned int rltype;
1618
1619 for (rltype = 0; rltype < RLIM_NLIMITS; ++rltype)
1620 if (rlimit_names[rltype] &&
1621 !strncmp("RLIMIT_", type, 7) &&
1622 !strcmp(rlimit_names[rltype], type + 7))
1623 return rltype;
1624
1625 return -1;
1626 }
1627
1628
1629 static int parseOCIrlimit(struct blob_attr *msg)
1630 {
1631 struct blob_attr *tb[__OCI_PROCESS_RLIMIT_MAX];
1632 int limtype = -1;
1633 struct rlimit *curlim;
1634
1635 blobmsg_parse(oci_process_rlimit_policy, __OCI_PROCESS_RLIMIT_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1636
1637 if (!tb[OCI_PROCESS_RLIMIT_TYPE] ||
1638 !tb[OCI_PROCESS_RLIMIT_SOFT] ||
1639 !tb[OCI_PROCESS_RLIMIT_HARD])
1640 return ENODATA;
1641
1642 limtype = resolve_rlimit(blobmsg_get_string(tb[OCI_PROCESS_RLIMIT_TYPE]));
1643
1644 if (limtype < 0)
1645 return EINVAL;
1646
1647 if (opts.rlimits[limtype])
1648 return ENOTUNIQ;
1649
1650 curlim = malloc(sizeof(struct rlimit));
1651 curlim->rlim_cur = blobmsg_cast_u64(tb[OCI_PROCESS_RLIMIT_SOFT]);
1652 curlim->rlim_max = blobmsg_cast_u64(tb[OCI_PROCESS_RLIMIT_HARD]);
1653
1654 opts.rlimits[limtype] = curlim;
1655
1656 return 0;
1657 };
1658
1659 enum {
1660 OCI_PROCESS_ARGS,
1661 OCI_PROCESS_CAPABILITIES,
1662 OCI_PROCESS_CWD,
1663 OCI_PROCESS_ENV,
1664 OCI_PROCESS_OOMSCOREADJ,
1665 OCI_PROCESS_NONEWPRIVILEGES,
1666 OCI_PROCESS_RLIMITS,
1667 OCI_PROCESS_TERMINAL,
1668 OCI_PROCESS_USER,
1669 __OCI_PROCESS_MAX,
1670 };
1671
1672 static const struct blobmsg_policy oci_process_policy[] = {
1673 [OCI_PROCESS_ARGS] = { "args", BLOBMSG_TYPE_ARRAY },
1674 [OCI_PROCESS_CAPABILITIES] = { "capabilities", BLOBMSG_TYPE_TABLE },
1675 [OCI_PROCESS_CWD] = { "cwd", BLOBMSG_TYPE_STRING },
1676 [OCI_PROCESS_ENV] = { "env", BLOBMSG_TYPE_ARRAY },
1677 [OCI_PROCESS_OOMSCOREADJ] = { "oomScoreAdj", BLOBMSG_TYPE_INT32 },
1678 [OCI_PROCESS_NONEWPRIVILEGES] = { "noNewPrivileges", BLOBMSG_TYPE_BOOL },
1679 [OCI_PROCESS_RLIMITS] = { "rlimits", BLOBMSG_TYPE_ARRAY },
1680 [OCI_PROCESS_TERMINAL] = { "terminal", BLOBMSG_TYPE_BOOL },
1681 [OCI_PROCESS_USER] = { "user", BLOBMSG_TYPE_TABLE },
1682 };
1683
1684
1685 static int parseOCIprocess(struct blob_attr *msg)
1686 {
1687 struct blob_attr *tb[__OCI_PROCESS_MAX], *cur;
1688 int rem, res;
1689
1690 blobmsg_parse(oci_process_policy, __OCI_PROCESS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1691
1692 if (!tb[OCI_PROCESS_ARGS])
1693 return ENOENT;
1694
1695 res = parseOCIenvarray(tb[OCI_PROCESS_ARGS], &opts.jail_argv);
1696 if (res)
1697 return res;
1698
1699 if (tb[OCI_PROCESS_TERMINAL])
1700 opts.console = blobmsg_get_bool(tb[OCI_PROCESS_TERMINAL]);
1701
1702 if (tb[OCI_PROCESS_NONEWPRIVILEGES])
1703 opts.no_new_privs = blobmsg_get_bool(tb[OCI_PROCESS_NONEWPRIVILEGES]);
1704
1705 if (tb[OCI_PROCESS_CWD])
1706 opts.cwd = strdup(blobmsg_get_string(tb[OCI_PROCESS_CWD]));
1707
1708 if (tb[OCI_PROCESS_ENV]) {
1709 res = parseOCIenvarray(tb[OCI_PROCESS_ENV], &opts.envp);
1710 if (res)
1711 return res;
1712 }
1713
1714 if (tb[OCI_PROCESS_USER] && (res = parseOCIprocessuser(tb[OCI_PROCESS_USER])))
1715 return res;
1716
1717 if (tb[OCI_PROCESS_CAPABILITIES] &&
1718 (res = parseOCIcapabilities(&opts.capset, tb[OCI_PROCESS_CAPABILITIES])))
1719 return res;
1720
1721 if (tb[OCI_PROCESS_RLIMITS]) {
1722 blobmsg_for_each_attr(cur, tb[OCI_PROCESS_RLIMITS], rem) {
1723 res = parseOCIrlimit(cur);
1724 if (res)
1725 return res;
1726 }
1727 }
1728
1729 if (tb[OCI_PROCESS_OOMSCOREADJ]) {
1730 opts.oom_score_adj = blobmsg_get_u32(tb[OCI_PROCESS_OOMSCOREADJ]);
1731 opts.set_oom_score_adj = true;
1732 }
1733
1734 return 0;
1735 }
1736
1737 enum {
1738 OCI_LINUX_NAMESPACE_TYPE,
1739 OCI_LINUX_NAMESPACE_PATH,
1740 __OCI_LINUX_NAMESPACE_MAX,
1741 };
1742
1743 static const struct blobmsg_policy oci_linux_namespace_policy[] = {
1744 [OCI_LINUX_NAMESPACE_TYPE] = { "type", BLOBMSG_TYPE_STRING },
1745 [OCI_LINUX_NAMESPACE_PATH] = { "path", BLOBMSG_TYPE_STRING },
1746 };
1747
1748 static int resolve_nstype(char *type) {
1749 if (!strcmp("pid", type))
1750 return CLONE_NEWPID;
1751 else if (!strcmp("network", type))
1752 return CLONE_NEWNET;
1753 else if (!strcmp("mount", type))
1754 return CLONE_NEWNS;
1755 else if (!strcmp("ipc", type))
1756 return CLONE_NEWIPC;
1757 else if (!strcmp("uts", type))
1758 return CLONE_NEWUTS;
1759 else if (!strcmp("user", type))
1760 return CLONE_NEWUSER;
1761 else if (!strcmp("cgroup", type))
1762 return CLONE_NEWCGROUP;
1763 #ifdef CLONE_NEWTIME
1764 else if (!strcmp("time", type))
1765 return CLONE_NEWTIME;
1766 #endif
1767 else
1768 return 0;
1769 }
1770
1771 static int parseOCIlinuxns(struct blob_attr *msg)
1772 {
1773 struct blob_attr *tb[__OCI_LINUX_NAMESPACE_MAX];
1774 int nstype;
1775 int *setns;
1776 int fd;
1777
1778 blobmsg_parse(oci_linux_namespace_policy, __OCI_LINUX_NAMESPACE_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1779
1780 if (!tb[OCI_LINUX_NAMESPACE_TYPE])
1781 return EINVAL;
1782
1783 nstype = resolve_nstype(blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]));
1784 if (!nstype)
1785 return EINVAL;
1786
1787 if (opts.namespace & nstype)
1788 return ENOTUNIQ;
1789
1790 setns = get_namespace_fd(nstype);
1791
1792 if (!setns)
1793 return EFAULT;
1794
1795 if (*setns != -1)
1796 return ENOTUNIQ;
1797
1798 if (tb[OCI_LINUX_NAMESPACE_PATH]) {
1799 DEBUG("opening existing %s namespace from path %s\n",
1800 blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]),
1801 blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_PATH]));
1802
1803 fd = open(blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_PATH]), O_RDONLY);
1804 if (fd == -1)
1805 return errno?:ESTALE;
1806
1807 if (ioctl(fd, NS_GET_NSTYPE) != nstype)
1808 return EINVAL;
1809
1810 DEBUG("opened existing %s namespace got filehandler %u\n",
1811 blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]),
1812 fd);
1813
1814 *setns = fd;
1815 } else {
1816 opts.namespace |= nstype;
1817 }
1818
1819 return 0;
1820 };
1821
1822
1823 enum {
1824 OCI_LINUX_UIDGIDMAP_CONTAINERID,
1825 OCI_LINUX_UIDGIDMAP_HOSTID,
1826 OCI_LINUX_UIDGIDMAP_SIZE,
1827 __OCI_LINUX_UIDGIDMAP_MAX,
1828 };
1829
1830 static const struct blobmsg_policy oci_linux_uidgidmap_policy[] = {
1831 [OCI_LINUX_UIDGIDMAP_CONTAINERID] = { "containerID", BLOBMSG_TYPE_INT32 },
1832 [OCI_LINUX_UIDGIDMAP_HOSTID] = { "hostID", BLOBMSG_TYPE_INT32 },
1833 [OCI_LINUX_UIDGIDMAP_SIZE] = { "size", BLOBMSG_TYPE_INT32 },
1834 };
1835
1836 static int parseOCIuidgidmappings(struct blob_attr *msg, bool is_gidmap)
1837 {
1838 const char *map_format = "%d %d %d\n";
1839 struct blob_attr *tb[__OCI_LINUX_UIDGIDMAP_MAX];
1840 struct blob_attr *cur;
1841 int rem, len;
1842 char **mappings;
1843 char *map, *curstr;
1844 unsigned int cnt = 0;
1845 size_t totallen = 0;
1846
1847 /* count number of mappings */
1848 blobmsg_for_each_attr(cur, msg, rem)
1849 cnt++;
1850
1851 if (!cnt)
1852 return 0;
1853
1854 /* allocate array for mappings */
1855 mappings = calloc(1 + cnt, sizeof(char*));
1856 if (!mappings)
1857 return ENOMEM;
1858
1859 mappings[cnt] = NULL;
1860
1861 cnt = 0;
1862 blobmsg_for_each_attr(cur, msg, rem) {
1863 blobmsg_parse(oci_linux_uidgidmap_policy, __OCI_LINUX_UIDGIDMAP_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
1864
1865 if (!tb[OCI_LINUX_UIDGIDMAP_CONTAINERID] ||
1866 !tb[OCI_LINUX_UIDGIDMAP_HOSTID] ||
1867 !tb[OCI_LINUX_UIDGIDMAP_SIZE])
1868 return EINVAL;
1869
1870 /* write mapping line into allocated string */
1871 len = asprintf(&mappings[cnt++], map_format,
1872 blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_CONTAINERID]),
1873 blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_HOSTID]),
1874 blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_SIZE]));
1875
1876 if (len < 0)
1877 return ENOMEM;
1878
1879 totallen += len;
1880 }
1881
1882 /* allocate combined mapping string */
1883 map = calloc(1 + totallen, sizeof(char));
1884 if (!map)
1885 return ENOMEM;
1886
1887 map[0] = '\0';
1888
1889 /* concatenate mapping strings into combined string */
1890 curstr = mappings[0];
1891 while (curstr) {
1892 strcat(map, curstr);
1893 free(curstr++);
1894 }
1895 free(mappings);
1896
1897 if (is_gidmap)
1898 opts.gidmap = map;
1899 else
1900 opts.uidmap = map;
1901
1902 return 0;
1903 }
1904
1905 enum {
1906 OCI_DEVICES_TYPE,
1907 OCI_DEVICES_PATH,
1908 OCI_DEVICES_MAJOR,
1909 OCI_DEVICES_MINOR,
1910 OCI_DEVICES_FILEMODE,
1911 OCI_DEVICES_UID,
1912 OCI_DEVICES_GID,
1913 __OCI_DEVICES_MAX,
1914 };
1915
1916 static const struct blobmsg_policy oci_devices_policy[] = {
1917 [OCI_DEVICES_TYPE] = { "type", BLOBMSG_TYPE_STRING },
1918 [OCI_DEVICES_PATH] = { "path", BLOBMSG_TYPE_STRING },
1919 [OCI_DEVICES_MAJOR] = { "major", BLOBMSG_TYPE_INT32 },
1920 [OCI_DEVICES_MINOR] = { "minor", BLOBMSG_TYPE_INT32 },
1921 [OCI_DEVICES_FILEMODE] = { "fileMode", BLOBMSG_TYPE_INT32 },
1922 [OCI_DEVICES_UID] = { "uid", BLOBMSG_TYPE_INT32 },
1923 [OCI_DEVICES_GID] = { "uid", BLOBMSG_TYPE_INT32 },
1924 };
1925
1926 static mode_t resolve_devtype(char *tstr)
1927 {
1928 if (!strcmp("c", tstr) ||
1929 !strcmp("u", tstr))
1930 return S_IFCHR;
1931 else if (!strcmp("b", tstr))
1932 return S_IFBLK;
1933 else if (!strcmp("p", tstr))
1934 return S_IFIFO;
1935 else
1936 return 0;
1937 }
1938
1939 static int parseOCIdevices(struct blob_attr *msg)
1940 {
1941 struct blob_attr *tb[__OCI_DEVICES_MAX];
1942 struct blob_attr *cur;
1943 int rem;
1944 size_t cnt = 0;
1945 struct mknod_args *tmp;
1946
1947 blobmsg_for_each_attr(cur, msg, rem)
1948 ++cnt;
1949
1950 opts.devices = calloc(cnt + 1, sizeof(struct mknod_args *));
1951
1952 cnt = 0;
1953 blobmsg_for_each_attr(cur, msg, rem) {
1954 blobmsg_parse(oci_devices_policy, __OCI_DEVICES_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
1955 if (!tb[OCI_DEVICES_TYPE] ||
1956 !tb[OCI_DEVICES_PATH])
1957 return ENODATA;
1958
1959 tmp = calloc(1, sizeof(struct mknod_args));
1960 if (!tmp)
1961 return ENOMEM;
1962
1963 tmp->mode = resolve_devtype(blobmsg_get_string(tb[OCI_DEVICES_TYPE]));
1964 if (!tmp->mode)
1965 return EINVAL;
1966
1967 if (tmp->mode != S_IFIFO) {
1968 if (!tb[OCI_DEVICES_MAJOR] || !tb[OCI_DEVICES_MINOR])
1969 return ENODATA;
1970
1971 tmp->dev = makedev(blobmsg_get_u32(tb[OCI_DEVICES_MAJOR]),
1972 blobmsg_get_u32(tb[OCI_DEVICES_MINOR]));
1973 }
1974
1975 if (tb[OCI_DEVICES_FILEMODE]) {
1976 if (~(S_IRWXU|S_IRWXG|S_IRWXO) & blobmsg_get_u32(tb[OCI_DEVICES_FILEMODE]))
1977 return EINVAL;
1978
1979 tmp->mode |= blobmsg_get_u32(tb[OCI_DEVICES_FILEMODE]);
1980 } else {
1981 tmp->mode |= (S_IRUSR|S_IWUSR); /* 0600 */
1982 }
1983
1984 tmp->path = strdup(blobmsg_get_string(tb[OCI_DEVICES_PATH]));
1985
1986 if (tb[OCI_DEVICES_UID])
1987 tmp->uid = blobmsg_get_u32(tb[OCI_DEVICES_UID]);
1988 else
1989 tmp->uid = -1;
1990
1991 if (tb[OCI_DEVICES_GID])
1992 tmp->gid = blobmsg_get_u32(tb[OCI_DEVICES_GID]);
1993 else
1994 tmp->gid = -1;
1995
1996 DEBUG("read device %s (%s)\n", blobmsg_get_string(tb[OCI_DEVICES_PATH]), blobmsg_get_string(tb[OCI_DEVICES_TYPE]));
1997 opts.devices[cnt++] = tmp;
1998 }
1999
2000 opts.devices[cnt] = NULL;
2001
2002 return 0;
2003 }
2004
2005 static int parseOCIsysctl(struct blob_attr *msg)
2006 {
2007 struct blob_attr *cur;
2008 int rem;
2009 char *tmp, *tc;
2010 size_t cnt = 0;
2011
2012 blobmsg_for_each_attr(cur, msg, rem) {
2013 if (!blobmsg_name(cur) || !blobmsg_get_string(cur))
2014 return EINVAL;
2015
2016 ++cnt;
2017 }
2018
2019 if (!cnt)
2020 return 0;
2021
2022 opts.sysctl = calloc(cnt + 1, sizeof(struct sysctl_val *));
2023 if (!opts.sysctl)
2024 return ENOMEM;
2025
2026 cnt = 0;
2027 blobmsg_for_each_attr(cur, msg, rem) {
2028 opts.sysctl[cnt] = malloc(sizeof(struct sysctl_val));
2029 if (!opts.sysctl[cnt])
2030 return ENOMEM;
2031
2032 /* replace '.' with '/' in entry name */
2033 tc = tmp = strdup(blobmsg_name(cur));
2034 while ((tc = strchr(tc, '.')))
2035 *tc = '/';
2036
2037 opts.sysctl[cnt]->value = strdup(blobmsg_get_string(cur));
2038 opts.sysctl[cnt]->entry = tmp;
2039
2040 ++cnt;
2041 }
2042
2043 opts.sysctl[cnt] = NULL;
2044
2045 return 0;
2046 }
2047
2048
2049 enum {
2050 OCI_LINUX_CGROUPSPATH,
2051 OCI_LINUX_RESOURCES,
2052 OCI_LINUX_SECCOMP,
2053 OCI_LINUX_SYSCTL,
2054 OCI_LINUX_NAMESPACES,
2055 OCI_LINUX_DEVICES,
2056 OCI_LINUX_UIDMAPPINGS,
2057 OCI_LINUX_GIDMAPPINGS,
2058 OCI_LINUX_MASKEDPATHS,
2059 OCI_LINUX_READONLYPATHS,
2060 OCI_LINUX_ROOTFSPROPAGATION,
2061 __OCI_LINUX_MAX,
2062 };
2063
2064 static const struct blobmsg_policy oci_linux_policy[] = {
2065 [OCI_LINUX_CGROUPSPATH] = { "cgroupsPath", BLOBMSG_TYPE_STRING },
2066 [OCI_LINUX_RESOURCES] = { "resources", BLOBMSG_TYPE_TABLE },
2067 [OCI_LINUX_SECCOMP] = { "seccomp", BLOBMSG_TYPE_TABLE },
2068 [OCI_LINUX_SYSCTL] = { "sysctl", BLOBMSG_TYPE_TABLE },
2069 [OCI_LINUX_NAMESPACES] = { "namespaces", BLOBMSG_TYPE_ARRAY },
2070 [OCI_LINUX_DEVICES] = { "devices", BLOBMSG_TYPE_ARRAY },
2071 [OCI_LINUX_UIDMAPPINGS] = { "uidMappings", BLOBMSG_TYPE_ARRAY },
2072 [OCI_LINUX_GIDMAPPINGS] = { "gidMappings", BLOBMSG_TYPE_ARRAY },
2073 [OCI_LINUX_MASKEDPATHS] = { "maskedPaths", BLOBMSG_TYPE_ARRAY },
2074 [OCI_LINUX_READONLYPATHS] = { "readonlyPaths", BLOBMSG_TYPE_ARRAY },
2075 [OCI_LINUX_ROOTFSPROPAGATION] = { "rootfsPropagation", BLOBMSG_TYPE_STRING },
2076 };
2077
2078 static int parseOCIlinux(struct blob_attr *msg)
2079 {
2080 struct blob_attr *tb[__OCI_LINUX_MAX];
2081 struct blob_attr *cur;
2082 int rem;
2083 int res = 0;
2084 char *cgpath;
2085 char cgfullpath[256] = "/sys/fs/cgroup";
2086
2087 blobmsg_parse(oci_linux_policy, __OCI_LINUX_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
2088
2089 if (tb[OCI_LINUX_NAMESPACES]) {
2090 blobmsg_for_each_attr(cur, tb[OCI_LINUX_NAMESPACES], rem) {
2091 res = parseOCIlinuxns(cur);
2092 if (res)
2093 return res;
2094 }
2095 }
2096
2097 if (tb[OCI_LINUX_UIDMAPPINGS]) {
2098 res = parseOCIuidgidmappings(tb[OCI_LINUX_GIDMAPPINGS], 0);
2099 if (res)
2100 return res;
2101 }
2102
2103 if (tb[OCI_LINUX_GIDMAPPINGS]) {
2104 res = parseOCIuidgidmappings(tb[OCI_LINUX_GIDMAPPINGS], 1);
2105 if (res)
2106 return res;
2107 }
2108
2109 if (tb[OCI_LINUX_READONLYPATHS]) {
2110 blobmsg_for_each_attr(cur, tb[OCI_LINUX_READONLYPATHS], rem) {
2111 res = add_mount(NULL, blobmsg_get_string(cur), NULL, MS_BIND | MS_REC | MS_RDONLY, 0, NULL, 0);
2112 if (res)
2113 return res;
2114 }
2115 }
2116
2117 if (tb[OCI_LINUX_MASKEDPATHS]) {
2118 blobmsg_for_each_attr(cur, tb[OCI_LINUX_MASKEDPATHS], rem) {
2119 res = add_mount((void *)(-1), blobmsg_get_string(cur), NULL, 0, 0, NULL, 0);
2120 if (res)
2121 return res;
2122 }
2123 }
2124
2125 if (tb[OCI_LINUX_SYSCTL]) {
2126 res = parseOCIsysctl(tb[OCI_LINUX_SYSCTL]);
2127 if (res)
2128 return res;
2129 }
2130
2131 if (tb[OCI_LINUX_SECCOMP]) {
2132 opts.ociseccomp = parseOCIlinuxseccomp(tb[OCI_LINUX_SECCOMP]);
2133 if (!opts.ociseccomp)
2134 return EINVAL;
2135 }
2136
2137 if (tb[OCI_LINUX_DEVICES]) {
2138 res = parseOCIdevices(tb[OCI_LINUX_DEVICES]);
2139 if (res)
2140 return res;
2141 }
2142
2143 if (tb[OCI_LINUX_CGROUPSPATH]) {
2144 cgpath = blobmsg_get_string(tb[OCI_LINUX_CGROUPSPATH]);
2145 if (cgpath[0] == '/') {
2146 if (strlen(cgpath) >= (sizeof(cgfullpath) - strlen(cgfullpath)))
2147 return E2BIG;
2148
2149 strcat(cgfullpath, cgpath);
2150 } else {
2151 strcat(cgfullpath, "/containers/");
2152 strcat(cgfullpath, opts.name); /* should be container name rather than jail name */
2153 strcat(cgfullpath, "/");
2154 if (strlen(cgpath) >= (sizeof(cgfullpath) - strlen(cgfullpath)))
2155 return E2BIG;
2156
2157 strcat(cgfullpath, cgpath);
2158 }
2159 } else {
2160 strcat(cgfullpath, "/containers/");
2161 strcat(cgfullpath, opts.name); /* should be container name rather than jail name */
2162 strcat(cgfullpath, "/");
2163 strcat(cgfullpath, opts.name); /* should be container instance name rather than jail name */
2164 }
2165
2166 cgroups_init(cgfullpath);
2167
2168 if (tb[OCI_LINUX_RESOURCES]) {
2169 res = parseOCIlinuxcgroups(tb[OCI_LINUX_RESOURCES]);
2170 if (res)
2171 return res;
2172 }
2173
2174 return 0;
2175 }
2176
2177 enum {
2178 OCI_VERSION,
2179 OCI_HOSTNAME,
2180 OCI_PROCESS,
2181 OCI_ROOT,
2182 OCI_MOUNTS,
2183 OCI_HOOKS,
2184 OCI_LINUX,
2185 OCI_ANNOTATIONS,
2186 __OCI_MAX,
2187 };
2188
2189 static const struct blobmsg_policy oci_policy[] = {
2190 [OCI_VERSION] = { "ociVersion", BLOBMSG_TYPE_STRING },
2191 [OCI_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
2192 [OCI_PROCESS] = { "process", BLOBMSG_TYPE_TABLE },
2193 [OCI_ROOT] = { "root", BLOBMSG_TYPE_TABLE },
2194 [OCI_MOUNTS] = { "mounts", BLOBMSG_TYPE_ARRAY },
2195 [OCI_HOOKS] = { "hooks", BLOBMSG_TYPE_TABLE },
2196 [OCI_LINUX] = { "linux", BLOBMSG_TYPE_TABLE },
2197 [OCI_ANNOTATIONS] = { "annotations", BLOBMSG_TYPE_TABLE },
2198 };
2199
2200 static int parseOCI(const char *jsonfile)
2201 {
2202 struct blob_attr *tb[__OCI_MAX];
2203 struct blob_attr *cur;
2204 int rem;
2205 int res;
2206
2207 blob_buf_init(&ocibuf, 0);
2208
2209 if (!blobmsg_add_json_from_file(&ocibuf, jsonfile)) {
2210 res=ENOENT;
2211 goto errout;
2212 }
2213
2214 blobmsg_parse(oci_policy, __OCI_MAX, tb, blob_data(ocibuf.head), blob_len(ocibuf.head));
2215
2216 if (!tb[OCI_VERSION]) {
2217 res=ENOMSG;
2218 goto errout;
2219 }
2220
2221 if (strncmp("1.0", blobmsg_get_string(tb[OCI_VERSION]), 3)) {
2222 ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb[OCI_VERSION]));
2223 res=ENOTSUP;
2224 goto errout;
2225 }
2226
2227 if (tb[OCI_HOSTNAME])
2228 opts.hostname = strdup(blobmsg_get_string(tb[OCI_HOSTNAME]));
2229
2230 if (!tb[OCI_PROCESS]) {
2231 res=ENODATA;
2232 goto errout;
2233 }
2234
2235 if ((res = parseOCIprocess(tb[OCI_PROCESS])))
2236 goto errout;
2237
2238 if (!tb[OCI_ROOT]) {
2239 res=ENODATA;
2240 goto errout;
2241 }
2242 if ((res = parseOCIroot(jsonfile, tb[OCI_ROOT])))
2243 goto errout;
2244
2245 if (!tb[OCI_MOUNTS]) {
2246 res=ENODATA;
2247 goto errout;
2248 }
2249
2250 blobmsg_for_each_attr(cur, tb[OCI_MOUNTS], rem)
2251 if ((res = parseOCImount(cur)))
2252 goto errout;
2253
2254 if (tb[OCI_LINUX] && (res = parseOCIlinux(tb[OCI_LINUX])))
2255 goto errout;
2256
2257 if (tb[OCI_HOOKS] && (res = parseOCIhooks(tb[OCI_HOOKS])))
2258 goto errout;
2259
2260 if (tb[OCI_ANNOTATIONS])
2261 opts.annotations = blob_memdup(tb[OCI_ANNOTATIONS]);
2262
2263 errout:
2264 blob_buf_free(&ocibuf);
2265
2266 return res;
2267 }
2268
2269 static int set_oom_score_adj(void)
2270 {
2271 int f;
2272 char fname[32];
2273
2274 if (!opts.set_oom_score_adj)
2275 return 0;
2276
2277 snprintf(fname, sizeof(fname), "/proc/%u/oom_score_adj", jail_process.pid);
2278 f = open(fname, O_WRONLY | O_TRUNC);
2279 if (f == -1)
2280 return errno;
2281
2282 dprintf(f, "%d", opts.oom_score_adj);
2283 close(f);
2284
2285 return 0;
2286 }
2287
2288
2289 enum {
2290 OCI_STATE_CREATING,
2291 OCI_STATE_CREATED,
2292 OCI_STATE_RUNNING,
2293 OCI_STATE_STOPPED,
2294 };
2295
2296 static int jail_oci_state = OCI_STATE_CREATED;
2297 static void pipe_send_start_container(struct uloop_timeout *t);
2298 static struct uloop_timeout start_container_timeout = {
2299 .cb = pipe_send_start_container,
2300 };
2301
2302 static int handle_start(struct ubus_context *ctx, struct ubus_object *obj,
2303 struct ubus_request_data *req, const char *method,
2304 struct blob_attr *msg)
2305 {
2306 if (jail_oci_state != OCI_STATE_CREATED)
2307 return UBUS_STATUS_INVALID_ARGUMENT;
2308
2309 uloop_timeout_add(&start_container_timeout);
2310
2311 return UBUS_STATUS_OK;
2312 }
2313
2314 static struct blob_buf bb;
2315 static int handle_state(struct ubus_context *ctx, struct ubus_object *obj,
2316 struct ubus_request_data *req, const char *method,
2317 struct blob_attr *msg)
2318 {
2319 char *statusstr;
2320
2321 switch (jail_oci_state) {
2322 case OCI_STATE_CREATING:
2323 statusstr = "creating";
2324 break;
2325 case OCI_STATE_CREATED:
2326 statusstr = "created";
2327 break;
2328 case OCI_STATE_RUNNING:
2329 statusstr = "running";
2330 break;
2331 case OCI_STATE_STOPPED:
2332 statusstr = "stopped";
2333 break;
2334 default:
2335 statusstr = "unknown";
2336 }
2337
2338 blob_buf_init(&bb, 0);
2339 blobmsg_add_string(&bb, "ociVersion", OCI_VERSION_STRING);
2340 blobmsg_add_string(&bb, "id", opts.name);
2341 blobmsg_add_string(&bb, "status", statusstr);
2342 if (jail_oci_state == OCI_STATE_CREATED ||
2343 jail_oci_state == OCI_STATE_RUNNING)
2344 blobmsg_add_u32(&bb, "pid", jail_process.pid);
2345
2346 blobmsg_add_string(&bb, "bundle", opts.ocibundle);
2347
2348 if (opts.annotations)
2349 blobmsg_add_blob(&bb, opts.annotations);
2350
2351 ubus_send_reply(ctx, req, bb.head);
2352
2353 return UBUS_STATUS_OK;
2354 }
2355
2356 enum {
2357 CONTAINER_KILL_ATTR_SIGNAL,
2358 __CONTAINER_KILL_ATTR_MAX,
2359 };
2360
2361 static const struct blobmsg_policy container_kill_attrs[__CONTAINER_KILL_ATTR_MAX] = {
2362 [CONTAINER_KILL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
2363 };
2364
2365 static int
2366 container_handle_kill(struct ubus_context *ctx, struct ubus_object *obj,
2367 struct ubus_request_data *req, const char *method,
2368 struct blob_attr *msg)
2369 {
2370 struct blob_attr *tb[__CONTAINER_KILL_ATTR_MAX], *cur;
2371 int sig = SIGTERM;
2372
2373 blobmsg_parse(container_kill_attrs, __CONTAINER_KILL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
2374
2375 cur = tb[CONTAINER_KILL_ATTR_SIGNAL];
2376 if (cur)
2377 sig = blobmsg_get_u32(cur);
2378
2379 if (jail_oci_state == OCI_STATE_CREATING)
2380 return UBUS_STATUS_NOT_FOUND;
2381
2382 if (kill(jail_process.pid, sig) == 0)
2383 return 0;
2384
2385 switch (errno) {
2386 case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
2387 case EPERM: return UBUS_STATUS_PERMISSION_DENIED;
2388 case ESRCH: return UBUS_STATUS_NOT_FOUND;
2389 }
2390
2391 return UBUS_STATUS_UNKNOWN_ERROR;
2392 }
2393
2394 static int
2395 jail_writepid(pid_t pid)
2396 {
2397 FILE *_pidfile;
2398
2399 if (!opts.pidfile)
2400 return 0;
2401
2402 _pidfile = fopen(opts.pidfile, "w");
2403 if (_pidfile == NULL)
2404 return errno;
2405
2406 if (fprintf(_pidfile, "%d\n", pid) < 0) {
2407 fclose(_pidfile);
2408 return errno;
2409 }
2410
2411 if (fclose(_pidfile))
2412 return errno;
2413
2414 return 0;
2415 }
2416
2417 static struct ubus_method container_methods[] = {
2418 UBUS_METHOD_NOARG("start", handle_start),
2419 UBUS_METHOD_NOARG("state", handle_state),
2420 UBUS_METHOD("kill", container_handle_kill, container_kill_attrs),
2421 };
2422
2423 static struct ubus_object_type container_object_type =
2424 UBUS_OBJECT_TYPE("container", container_methods);
2425
2426 static struct ubus_object container_object = {
2427 .type = &container_object_type,
2428 .methods = container_methods,
2429 .n_methods = ARRAY_SIZE(container_methods),
2430 };
2431
2432 static void post_main(struct uloop_timeout *t);
2433 static struct uloop_timeout post_main_timeout = {
2434 .cb = post_main,
2435 };
2436 static int netns_fd;
2437 static int pidns_fd;
2438 #ifdef CLONE_NEWTIME
2439 static int timens_fd;
2440 #endif
2441 static void post_create_runtime(void);
2442 int main(int argc, char **argv)
2443 {
2444 uid_t uid = getuid();
2445 const char log[] = "/dev/log";
2446 const char ubus[] = "/var/run/ubus/ubus.sock";
2447 int ch, ret;
2448
2449 if (uid) {
2450 ERROR("not root, aborting: %m\n");
2451 return EXIT_FAILURE;
2452 }
2453
2454 umask(022);
2455 mount_list_init();
2456 init_library_search();
2457 cgroups_prepare();
2458 exit_from_child = false;
2459
2460 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
2461 switch (ch) {
2462 case 'd':
2463 debug = atoi(optarg);
2464 break;
2465 case 'p':
2466 opts.namespace |= CLONE_NEWNS;
2467 opts.procfs = 1;
2468 break;
2469 case 'o':
2470 opts.namespace |= CLONE_NEWNS;
2471 opts.ronly = 1;
2472 break;
2473 case 'f':
2474 opts.namespace |= CLONE_NEWUSER;
2475 break;
2476 case 'F':
2477 opts.namespace |= CLONE_NEWCGROUP;
2478 break;
2479 case 'R':
2480 opts.extroot = optarg;
2481 break;
2482 case 's':
2483 opts.namespace |= CLONE_NEWNS;
2484 opts.sysfs = 1;
2485 break;
2486 case 'S':
2487 opts.seccomp = optarg;
2488 add_mount_bind(optarg, 1, -1);
2489 break;
2490 case 'C':
2491 opts.capabilities = optarg;
2492 break;
2493 case 'c':
2494 opts.no_new_privs = 1;
2495 break;
2496 case 'n':
2497 opts.name = optarg;
2498 break;
2499 case 'N':
2500 opts.namespace |= CLONE_NEWNET;
2501 break;
2502 case 'h':
2503 opts.namespace |= CLONE_NEWUTS;
2504 opts.hostname = strdup(optarg);
2505 break;
2506 case 'r':
2507 opts.namespace |= CLONE_NEWNS;
2508 add_path_and_deps(optarg, 1, 0, 0);
2509 break;
2510 case 'w':
2511 opts.namespace |= CLONE_NEWNS;
2512 add_path_and_deps(optarg, 0, 0, 0);
2513 break;
2514 case 'u':
2515 opts.namespace |= CLONE_NEWNS;
2516 add_mount_bind(ubus, 0, -1);
2517 break;
2518 case 'l':
2519 opts.namespace |= CLONE_NEWNS;
2520 add_mount_bind(log, 0, -1);
2521 break;
2522 case 'U':
2523 opts.user = optarg;
2524 break;
2525 case 'G':
2526 opts.group = optarg;
2527 break;
2528 case 'O':
2529 opts.overlaydir = optarg;
2530 break;
2531 case 'T':
2532 opts.tmpoverlaysize = optarg;
2533 break;
2534 case 'E':
2535 opts.require_jail = 1;
2536 break;
2537 case 'y':
2538 opts.console = 1;
2539 break;
2540 case 'J':
2541 opts.ocibundle = optarg;
2542 break;
2543 case 'i':
2544 opts.immediately = true;
2545 break;
2546 case 'P':
2547 opts.pidfile = optarg;
2548 break;
2549 }
2550 }
2551
2552 if (opts.namespace && !opts.ocibundle)
2553 opts.namespace |= CLONE_NEWIPC | CLONE_NEWPID;
2554
2555 /* those are filehandlers, so -1 indicates unused */
2556 opts.setns.pid = -1;
2557 opts.setns.net = -1;
2558 opts.setns.ns = -1;
2559 opts.setns.ipc = -1;
2560 opts.setns.uts = -1;
2561 opts.setns.user = -1;
2562 opts.setns.cgroup = -1;
2563 #ifdef CLONE_NEWTIME
2564 opts.setns.time = -1;
2565 #endif
2566
2567 if (opts.capabilities && parseOCIcapabilities_from_file(&opts.capset, opts.capabilities)) {
2568 ERROR("failed to read capabilities from file %s\n", opts.capabilities);
2569 ret=-1;
2570 goto errout;
2571 }
2572
2573 if (opts.ocibundle) {
2574 char *jsonfile;
2575 int ocires;
2576
2577 asprintf(&jsonfile, "%s/config.json", opts.ocibundle);
2578 ocires = parseOCI(jsonfile);
2579 free(jsonfile);
2580 if (ocires) {
2581 ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires), ocires);
2582 ret=ocires;
2583 goto errout;
2584 }
2585 }
2586
2587 if (opts.tmpoverlaysize && strlen(opts.tmpoverlaysize) > 8) {
2588 ERROR("size parameter too long: \"%s\"\n", opts.tmpoverlaysize);
2589 ret=-1;
2590 goto errout;
2591 }
2592
2593 /* no <binary> param found */
2594 if (!opts.ocibundle && (argc - optind < 1)) {
2595 usage();
2596 ret=EXIT_FAILURE;
2597 goto errout;
2598 }
2599 if (!(opts.ocibundle||opts.namespace||opts.capabilities||opts.seccomp)) {
2600 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
2601 usage();
2602 ret=EXIT_FAILURE;
2603 goto errout;
2604 }
2605 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
2606 opts.namespace,
2607 opts.capset.apply,
2608 opts.seccomp != 0 || opts.ociseccomp != 0);
2609
2610 uloop_init();
2611 signals_init();
2612
2613 parent_ctx = ubus_connect(NULL);
2614 ubus_add_uloop(parent_ctx);
2615
2616 if (opts.ocibundle) {
2617 char *objname;
2618 if (asprintf(&objname, "container.%s", opts.name) < 0) {
2619 ret=-ENOMEM;
2620 goto errout;
2621 }
2622
2623 container_object.name = objname;
2624 ret = ubus_add_object(parent_ctx, &container_object);
2625 if (ret) {
2626 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
2627 ret=-1;
2628 goto errout;
2629 }
2630 }
2631
2632 /* deliberately not using 'else' on unrelated conditional branches */
2633 if (!opts.ocibundle) {
2634 /* allocate NULL-terminated array for argv */
2635 opts.jail_argv = calloc(1 + argc - optind, sizeof(char**));
2636 if (!opts.jail_argv) {
2637 ret=EXIT_FAILURE;
2638 goto errout;
2639 }
2640 for (size_t s = optind; s < argc; s++)
2641 opts.jail_argv[s - optind] = strdup(argv[s]);
2642
2643 if (opts.namespace & CLONE_NEWUSER)
2644 get_jail_user(&opts.pw_uid, &opts.pw_gid, &opts.gr_gid);
2645 }
2646
2647 if (!opts.extroot) {
2648 if (opts.namespace && add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
2649 ERROR("failed to load dependencies\n");
2650 ret=-1;
2651 goto errout;
2652 }
2653 }
2654
2655 if (opts.namespace && opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
2656 ERROR("failed to load libpreload-seccomp.so\n");
2657 opts.seccomp = 0;
2658 if (opts.require_jail) {
2659 ret=-1;
2660 goto errout;
2661 }
2662 }
2663
2664 uloop_timeout_add(&post_main_timeout);
2665 uloop_run();
2666
2667 errout:
2668 if (opts.ocibundle)
2669 cgroups_free();
2670
2671 free_opts(true);
2672
2673 return ret;
2674 }
2675
2676 static void post_main(struct uloop_timeout *t)
2677 {
2678 if (apply_rlimits()) {
2679 ERROR("error applying resource limits\n");
2680 free_and_exit(EXIT_FAILURE);
2681 }
2682
2683 if (opts.name)
2684 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
2685
2686 if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0)
2687 free_and_exit(-1);
2688
2689 if (has_namespaces()) {
2690 if (opts.namespace & CLONE_NEWNS) {
2691 if (!opts.extroot && (opts.user || opts.group)) {
2692 add_mount_bind("/etc/passwd", 1, -1);
2693 add_mount_bind("/etc/group", 1, -1);
2694 }
2695
2696 #if defined(__GLIBC__)
2697 if (!opts.extroot)
2698 add_mount_bind("/etc/nsswitch.conf", 1, -1);
2699 #endif
2700
2701 if (!(opts.namespace & CLONE_NEWNET)) {
2702 add_mount_bind("/etc/resolv.conf", 1, -1);
2703 } else if (opts.setns.net == -1) {
2704 char hostdir[PATH_MAX];
2705
2706 snprintf(hostdir, PATH_MAX, "/tmp/resolv.conf-%s.d", opts.name);
2707 mkdir_p(hostdir, 0755);
2708 add_mount(hostdir, "/dev/resolv.conf.d", NULL, MS_BIND | MS_NOEXEC | MS_NOATIME | MS_NOSUID | MS_NODEV | MS_RDONLY, 0, NULL, -1);
2709 }
2710
2711 /* default mounts */
2712 add_mount(NULL, "/dev", "tmpfs", MS_NOATIME | MS_NOEXEC | MS_NOSUID, 0, "size=1M", -1);
2713 add_mount(NULL, "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, 0, "newinstance,ptmxmode=0666,mode=0620,gid=5", 0);
2714
2715 if (opts.procfs || opts.ocibundle) {
2716 add_mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0, NULL, -1);
2717
2718 /*
2719 * hack to make /proc/sys/net read-write while the rest of /proc/sys is read-only
2720 * which cannot be expressed with OCI spec, but happends to be very useful.
2721 * Only apply it if '/proc/sys' is not already listed as mount, maskedPath or
2722 * readonlyPath.
2723 * If not running in a new network namespace, only make /proc/sys read-only.
2724 * If running in a new network namespace, temporarily stash (ie. mount-bind)
2725 * /proc/sys/net into (totally unrelated, but surely existing) /proc/self/net.
2726 * Then we mount-bind /proc/sys read-only and then mount-move /proc/self/net into
2727 * /proc/sys/net.
2728 * This works because mounts are executed in incrementing strcmp() order and
2729 * /proc/self/net appears there before /proc/sys/net and hence the operation
2730 * succeeds as the bind-mount of /proc/self/net is performed first and then
2731 * move-mount of /proc/sys/net follows because 'e' preceeds 'y' in the ASCII
2732 * table (and in the alphabet).
2733 */
2734 if (!add_mount(NULL, "/proc/sys", NULL, MS_BIND | MS_RDONLY, 0, NULL, -1))
2735 if (opts.namespace & CLONE_NEWNET)
2736 if (!add_mount_inner("/proc/self/net", "/proc/sys/net", NULL, MS_MOVE, 0, NULL, -1))
2737 add_mount_inner("/proc/sys/net", "/proc/self/net", NULL, MS_BIND, 0, NULL, -1);
2738
2739 }
2740 if (opts.sysfs || opts.ocibundle)
2741 add_mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, 0, NULL, -1);
2742
2743 if (opts.ocibundle)
2744 add_mount("shm", "/dev/shm", "tmpfs", MS_NOSUID | MS_NOEXEC | MS_NODEV, 0, "mode=1777", -1);
2745
2746 }
2747
2748 if (opts.setns.pid != -1) {
2749 pidns_fd = ns_open_pid("pid", getpid());
2750 setns_open(CLONE_NEWPID);
2751 } else {
2752 pidns_fd = -1;
2753 }
2754
2755 #ifdef CLONE_NEWTIME
2756 if (opts.setns.time != -1) {
2757 timens_fd = ns_open_pid("time", getpid());
2758 setns_open(CLONE_NEWTIME);
2759 }
2760 #endif
2761
2762 jail_process.pid = clone(exec_jail, child_stack + STACK_SIZE, SIGCHLD | (opts.namespace & (~CLONE_NEWCGROUP)), NULL);
2763 } else {
2764 jail_process.pid = fork();
2765 }
2766
2767 if (jail_process.pid > 0) {
2768 /* parent process */
2769 char sig_buf[1];
2770
2771 uloop_process_add(&jail_process);
2772 jail_running = 1;
2773 seteuid(0);
2774 if (pidns_fd != -1) {
2775 setns(pidns_fd, CLONE_NEWPID);
2776 close(pidns_fd);
2777 }
2778 #ifdef CLONE_NEWTIME
2779 if (timens_fd != -1)
2780 setns(timens_fd, CLONE_NEWTIME);
2781 close(timens_fd);
2782 }
2783 #endif
2784 if (opts.setns.net != -1)
2785 close(opts.setns.net);
2786 if (opts.setns.ns != -1)
2787 close(opts.setns.ns);
2788 if (opts.setns.ipc != -1)
2789 close(opts.setns.ipc);
2790 if (opts.setns.uts != -1)
2791 close(opts.setns.uts);
2792 if (opts.setns.user != -1)
2793 close(opts.setns.user);
2794 if (opts.setns.cgroup != -1)
2795 close(opts.setns.cgroup);
2796 close(pipes[1]);
2797 close(pipes[2]);
2798 if (read(pipes[0], sig_buf, 1) < 1) {
2799 ERROR("can't read from child\n");
2800 free_and_exit(-1);
2801 }
2802 close(pipes[0]);
2803 set_oom_score_adj();
2804
2805 if (opts.ocibundle)
2806 cgroups_apply(jail_process.pid);
2807
2808 if (opts.namespace & CLONE_NEWUSER) {
2809 if (write_setgroups(jail_process.pid, true)) {
2810 ERROR("can't write setgroups\n");
2811 free_and_exit(-1);
2812 }
2813 if (!opts.uidmap) {
2814 bool has_gr = (opts.gr_gid != -1);
2815 if (opts.pw_uid != -1) {
2816 write_single_uid_gid_map(jail_process.pid, 0, opts.pw_uid);
2817 write_single_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:opts.pw_gid);
2818 } else {
2819 write_single_uid_gid_map(jail_process.pid, 0, 65534);
2820 write_single_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:65534);
2821 }
2822 } else {
2823 write_uid_gid_map(jail_process.pid, 0, opts.uidmap);
2824 if (opts.gidmap)
2825 write_uid_gid_map(jail_process.pid, 1, opts.gidmap);
2826 }
2827 }
2828
2829 if (opts.namespace & CLONE_NEWNET) {
2830 if (!opts.name) {
2831 ERROR("netns needs a named jail\n");
2832 free_and_exit(-1);
2833 }
2834 netns_fd = ns_open_pid("net", jail_process.pid);
2835 netns_updown(jail_process.pid, true);
2836 }
2837 if (jail_writepid(jail_process.pid)) {
2838 ERROR("failed to write pidfile: %m\n");
2839 free_and_exit(-1);
2840 }
2841 } else if (jail_process.pid == 0) {
2842 /* fork child process */
2843 free_and_exit(exec_jail(NULL));
2844 } else {
2845 ERROR("failed to clone/fork: %m\n");
2846 free_and_exit(EXIT_FAILURE);
2847 }
2848 run_hooks(opts.hooks.createRuntime, post_create_runtime);
2849 }
2850
2851 static void post_poststart(void);
2852 static void post_create_runtime(void)
2853 {
2854 char sig_buf[1];
2855
2856 sig_buf[0] = 'O';
2857 if (write(pipes[3], sig_buf, 1) < 0) {
2858 ERROR("can't write to child\n");
2859 free_and_exit(-1);
2860 }
2861
2862 jail_oci_state = OCI_STATE_CREATED;
2863 if (opts.ocibundle && !opts.immediately)
2864 uloop_run(); /* wait for 'start' command via ubus */
2865 else
2866 pipe_send_start_container(NULL);
2867 }
2868
2869 static void pipe_send_start_container(struct uloop_timeout *t)
2870 {
2871 char sig_buf[1];
2872
2873 jail_oci_state = OCI_STATE_RUNNING;
2874 sig_buf[0] = '!';
2875 if (write(pipes[3], sig_buf, 1) < 0) {
2876 ERROR("can't write to child\n");
2877 free_and_exit(-1);
2878 }
2879 close(pipes[3]);
2880
2881 run_hooks(opts.hooks.poststart, post_poststart);
2882 }
2883
2884 static void post_poststart(void)
2885 {
2886 uloop_run(); /* idle here while jail is running */
2887 if (jail_running) {
2888 DEBUG("uloop interrupted, killing jail process\n");
2889 kill(jail_process.pid, SIGTERM);
2890 uloop_timeout_set(&jail_process_timeout, 1000);
2891 uloop_run();
2892 }
2893 uloop_done();
2894 poststop();
2895 }
2896
2897 static void post_poststop(void);
2898 static void poststop(void) {
2899 if (opts.namespace & CLONE_NEWNET) {
2900 setns(netns_fd, CLONE_NEWNET);
2901 netns_updown(getpid(), false);
2902 close(netns_fd);
2903 }
2904 run_hooks(opts.hooks.poststop, post_poststop);
2905 }
2906
2907 static void post_poststop(void)
2908 {
2909 free_opts(true);
2910 if (parent_ctx)
2911 ubus_free(parent_ctx);
2912
2913 exit(jail_return_code);
2914 }