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