2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <sys/mount.h>
16 #include <sys/prctl.h>
18 #include <sys/types.h>
30 #include <linux/limits.h>
31 #include <linux/filter.h>
34 #include "capabilities.h"
39 #include "seccomp-oci.h"
41 #include <libubox/utils.h>
42 #include <libubox/blobmsg.h>
43 #include <libubox/blobmsg_json.h>
44 #include <libubox/list.h>
45 #include <libubox/vlist.h>
46 #include <libubox/uloop.h>
49 #ifndef CLONE_NEWCGROUP
50 #define CLONE_NEWCGROUP 0x02000000
53 #define STACK_SIZE (1024 * 1024)
54 #define OPT_ARGS "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:EyJ:"
62 struct sock_fprog
*ociseccomp
;
64 struct jail_capset capset
;
85 static struct blob_buf ocibuf
;
87 extern int pivot_root(const char *new_root
, const char *put_old
);
91 static char child_stack
[STACK_SIZE
];
95 static int mkdir_p(char *dir
, mode_t mask
)
97 char *l
= strrchr(dir
, '/');
105 if (mkdir_p(dir
, mask
))
110 ret
= mkdir(dir
, mask
);
111 if (ret
&& errno
== EEXIST
)
115 ERROR("mkdir(%s, %d) failed: %m\n", dir
, mask
);
120 static int _mount_bind(const char *root
, const char *path
, const char *target
, int readonly
, int strict
, int error
)
125 int remount_flags
= MS_BIND
| MS_REMOUNT
;
127 if (stat(path
, &s
)) {
128 ERROR("stat(%s) failed: %m\n", path
);
132 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
134 if (S_ISDIR(s
.st_mode
)) {
137 mkdir_p(dirname(new), 0755);
138 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
139 fd
= creat(new, 0644);
141 ERROR("creat(%s) failed: %m\n", new);
147 if (mount(path
, new, NULL
, MS_BIND
, NULL
)) {
148 ERROR("failed to mount -B %s %s: %m\n", path
, new);
153 remount_flags
|= MS_RDONLY
;
156 remount_flags
|= MS_NOEXEC
| MS_NOSUID
| MS_NODEV
;
158 if ((strict
|| readonly
) && mount(NULL
, new, NULL
, remount_flags
, NULL
)) {
159 ERROR("failed to remount (%s%s%s) %s: %m\n", readonly
?"ro":"rw",
160 (readonly
&& strict
)?", ":"", strict
?"strict":"", new);
164 DEBUG("mount -B %s %s (%s%s%s)\n", path
, new,
165 readonly
?"ro":"rw", (readonly
&& strict
)?", ":"", strict
?"strict":"");
170 int mount_bind(const char *root
, const char *path
, int readonly
, int error
) {
171 return _mount_bind(root
, path
, NULL
, readonly
, 0, error
);
174 static int mount_overlay(char *jail_root
, char *overlaydir
) {
175 char *upperdir
, *workdir
, *optsstr
, *upperetc
, *upperresolvconf
;
176 const char mountoptsformat
[] = "lowerdir=%s,upperdir=%s,workdir=%s";
179 if (asprintf(&upperdir
, "%s%s", overlaydir
, "/upper") < 0)
182 if (asprintf(&workdir
, "%s%s", overlaydir
, "/work") < 0)
185 if (asprintf(&optsstr
, mountoptsformat
, jail_root
, upperdir
, workdir
) < 0)
188 if (mkdir_p(upperdir
, 0755) || mkdir_p(workdir
, 0755))
192 * make sure /etc/resolv.conf exists in overlay and is owned by jail userns root
193 * this is to work-around a bug in overlayfs described in the overlayfs-userns
195 * 3. modification of a file 'hithere' which is in l but not yet
196 * in u, and which is not owned by T, is not allowed, even if
197 * writes to u are allowed. This may be a bug in overlayfs,
198 * but it is safe behavior.
200 if (asprintf(&upperetc
, "%s/etc", upperdir
) < 0)
203 if (mkdir_p(upperetc
, 0755))
204 goto upper_etc_printf
;
206 if (asprintf(&upperresolvconf
, "%s/resolv.conf", upperetc
) < 0)
207 goto upper_etc_printf
;
209 fd
= creat(upperresolvconf
, 0644);
211 ERROR("creat(%s) failed: %m\n", upperresolvconf
);
212 goto upper_resolvconf_printf
;
216 DEBUG("mount -t overlay %s %s (%s)\n", jail_root
, jail_root
, optsstr
);
218 if (mount(jail_root
, jail_root
, "overlay", MS_NOATIME
, optsstr
))
223 upper_resolvconf_printf
:
224 free(upperresolvconf
);
237 static void pass_console(int console_fd
)
239 struct ubus_context
*ctx
= ubus_connect(NULL
);
240 static struct blob_buf req
;
246 blob_buf_init(&req
, 0);
247 blobmsg_add_string(&req
, "name", opts
.name
);
249 if (ubus_lookup_id(ctx
, "container", &id
) ||
250 ubus_invoke_fd(ctx
, id
, "console_set", req
.head
, NULL
, NULL
, 3000, console_fd
))
251 INFO("ubus request failed\n");
259 static int create_dev_console(const char *jail_root
)
262 char dev_console_path
[PATH_MAX
];
263 int slave_console_fd
;
265 /* Open UNIX/98 virtual console */
266 console_fd
= posix_openpt(O_RDWR
| O_NOCTTY
);
267 if (console_fd
== -1)
270 console_fname
= ptsname(console_fd
);
271 DEBUG("got console fd %d and PTS client name %s\n", console_fd
, console_fname
);
276 unlockpt(console_fd
);
278 /* pass PTY master to procd */
279 pass_console(console_fd
);
281 /* mount-bind PTY slave to /dev/console in jail */
282 snprintf(dev_console_path
, sizeof(dev_console_path
), "%s/dev/console", jail_root
);
283 close(creat(dev_console_path
, 0620));
285 if (mount(console_fname
, dev_console_path
, NULL
, MS_BIND
, NULL
))
288 /* use PTY slave for stdio */
289 slave_console_fd
= open(console_fname
, O_RDWR
); /* | O_NOCTTY */
290 dup2(slave_console_fd
, 0);
291 dup2(slave_console_fd
, 1);
292 dup2(slave_console_fd
, 2);
293 close(slave_console_fd
);
295 INFO("using guest console %s\n", console_fname
);
304 static int build_jail_fs(void)
306 char jail_root
[] = "/tmp/ujail-XXXXXX";
307 char tmpovdir
[] = "/tmp/ujail-overlay-XXXXXX";
308 char tmpdevdir
[] = "/tmp/ujail-XXXXXX/dev";
309 char tmpdevptsdir
[] = "/tmp/ujail-XXXXXX/dev/pts";
310 char *overlaydir
= NULL
;
312 if (mkdtemp(jail_root
) == NULL
) {
313 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
317 /* oldroot can't be MS_SHARED else pivot_root() fails */
318 if (mount("none", "/", NULL
, MS_REC
|MS_PRIVATE
, NULL
)) {
319 ERROR("private mount failed %m\n");
324 if (mount(opts
.extroot
, jail_root
, NULL
, MS_BIND
, NULL
)) {
325 ERROR("extroot mount failed %m\n");
329 if (mount("tmpfs", jail_root
, "tmpfs", MS_NOATIME
, "mode=0755")) {
330 ERROR("tmpfs mount failed %m\n");
335 if (opts
.tmpoverlaysize
) {
336 char mountoptsstr
[] = "mode=0755,size=XXXXXXXX";
338 snprintf(mountoptsstr
, sizeof(mountoptsstr
),
339 "mode=0755,size=%s", opts
.tmpoverlaysize
);
340 if (mkdtemp(tmpovdir
) == NULL
) {
341 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
344 if (mount("tmpfs", tmpovdir
, "tmpfs", MS_NOATIME
,
346 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts
.tmpoverlaysize
);
349 overlaydir
= tmpovdir
;
353 overlaydir
= opts
.overlaydir
;
356 mount_overlay(jail_root
, overlaydir
);
358 if (chdir(jail_root
)) {
359 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root
);
363 snprintf(tmpdevdir
, sizeof(tmpdevdir
), "%s/dev", jail_root
);
364 mkdir_p(tmpdevdir
, 0755);
365 if (mount(NULL
, tmpdevdir
, "tmpfs", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, "size=1M"))
368 snprintf(tmpdevptsdir
, sizeof(tmpdevptsdir
), "%s/dev/pts", jail_root
);
369 mkdir_p(tmpdevptsdir
, 0755);
370 if (mount(NULL
, tmpdevptsdir
, "devpts", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, NULL
))
374 create_dev_console(jail_root
);
376 if (mount_all(jail_root
)) {
377 ERROR("mount_all() failed\n");
381 if (opts
.namespace & CLONE_NEWNET
) {
382 char hostdir
[PATH_MAX
], jailetc
[PATH_MAX
], jaillink
[PATH_MAX
];
384 snprintf(hostdir
, PATH_MAX
, "/tmp/resolv.conf-%s.d", opts
.name
);
385 mkdir_p(hostdir
, 0755);
386 _mount_bind(jail_root
, hostdir
, "/tmp/resolv.conf.d", 1, 1, -1);
387 snprintf(jailetc
, PATH_MAX
, "%s/etc", jail_root
);
388 mkdir_p(jailetc
, 0755);
389 snprintf(jaillink
, PATH_MAX
, "%s/etc/resolv.conf", jail_root
);
392 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink
);
395 char dirbuf
[sizeof(jail_root
) + 4];
396 snprintf(dirbuf
, sizeof(dirbuf
), "%s/old", jail_root
);
399 if (pivot_root(jail_root
, dirbuf
) == -1) {
400 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root
, dirbuf
);
404 ERROR("chdir(/) (after pivot_root) failed: %m\n");
408 snprintf(dirbuf
, sizeof(dirbuf
), "/old%s", jail_root
);
409 umount2(dirbuf
, MNT_DETACH
);
411 if (opts
.tmpoverlaysize
) {
412 char tmpdirbuf
[sizeof(tmpovdir
) + 4];
413 snprintf(tmpdirbuf
, sizeof(tmpdirbuf
), "/old%s", tmpovdir
);
414 umount2(tmpdirbuf
, MNT_DETACH
);
418 umount2("/old", MNT_DETACH
);
422 mkdir("/proc", 0755);
423 mount("proc", "/proc", "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
425 * make /proc/sys read-only while keeping read-write to
426 * /proc/sys/net if CLONE_NEWNET is set.
428 if (opts
.namespace & CLONE_NEWNET
)
429 mount("/proc/sys/net", "/proc/self/net", NULL
, MS_BIND
, 0);
431 mount("/proc/sys", "/proc/sys", NULL
, MS_BIND
, 0);
432 mount(NULL
, "/proc/sys", NULL
, MS_REMOUNT
| MS_RDONLY
, 0);
433 mount(NULL
, "/proc", NULL
, MS_REMOUNT
, 0);
435 if (opts
.namespace & CLONE_NEWNET
)
436 mount("/proc/self/net", "/proc/sys/net", NULL
, MS_MOVE
, 0);
440 mount("sysfs", "/sys", "sysfs", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RDONLY
, 0);
443 mount(NULL
, "/", NULL
, MS_RDONLY
| MS_REMOUNT
, 0);
448 static int write_uid_gid_map(pid_t child_pid
, bool gidmap
, char *mapstr
)
453 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
454 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
457 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
460 if (dprintf(map_file
, "%s", mapstr
)) {
470 static int write_single_uid_gid_map(pid_t child_pid
, bool gidmap
, int id
)
474 const char *map_format
= "%d %d %d\n";
475 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
476 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
479 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
482 if (dprintf(map_file
, map_format
, 0, id
, 1) == -1) {
491 static int write_setgroups(pid_t child_pid
, bool allow
)
494 char setgroups_path
[64];
496 if (snprintf(setgroups_path
, sizeof(setgroups_path
), "/proc/%d/setgroups",
501 if ((setgroups_file
= open(setgroups_path
, O_WRONLY
)) == -1) {
505 if (dprintf(setgroups_file
, "%s", allow
?"allow":"deny") == -1) {
506 close(setgroups_file
);
510 close(setgroups_file
);
514 static void get_jail_user(int *user
, int *user_gid
, int *gr_gid
)
516 struct passwd
*p
= NULL
;
517 struct group
*g
= NULL
;
520 p
= getpwnam(opts
.user
);
522 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
523 opts
.user
, errno
, strerror(errno
));
527 *user_gid
= p
->pw_gid
;
534 g
= getgrnam(opts
.group
);
536 ERROR("failed to get gid for group %s: %m\n", opts
.group
);
545 static void set_jail_user(int pw_uid
, int user_gid
, int gr_gid
)
547 if (opts
.user
&& (user_gid
!= -1) && initgroups(opts
.user
, user_gid
)) {
548 ERROR("failed to initgroups() for user %s: %m\n", opts
.user
);
552 if ((gr_gid
!= -1) && setregid(gr_gid
, gr_gid
)) {
553 ERROR("failed to set group id %d: %m\n", gr_gid
);
557 if ((pw_uid
!= -1) && setreuid(pw_uid
, pw_uid
)) {
558 ERROR("failed to set user id %d: %m\n", pw_uid
);
564 static char** build_envp(const char *seccomp
, char **ocienvp
)
566 static char *envp
[MAX_ENVP
];
567 static char preload_var
[PATH_MAX
];
568 static char seccomp_var
[PATH_MAX
];
569 static char debug_var
[] = "LD_DEBUG=all";
570 static char container_var
[] = "container=ujail";
571 const char *preload_lib
= find_lib("libpreload-seccomp.so");
576 if (seccomp
&& !preload_lib
) {
577 ERROR("failed to add preload-lib to env\n");
581 snprintf(seccomp_var
, sizeof(seccomp_var
), "SECCOMP_FILE=%s", seccomp
);
582 envp
[count
++] = seccomp_var
;
583 snprintf(preload_var
, sizeof(preload_var
), "LD_PRELOAD=%s", preload_lib
);
584 envp
[count
++] = preload_var
;
587 envp
[count
++] = container_var
;
590 envp
[count
++] = debug_var
;
593 while (addenv
&& *addenv
) {
594 envp
[count
++] = *(addenv
++);
595 if (count
>= MAX_ENVP
) {
596 ERROR("environment limited to %d extra records, truncating\n", MAX_ENVP
);
603 static void usage(void)
605 fprintf(stderr
, "ujail <options> -- <binary> <params ...>\n");
606 fprintf(stderr
, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
607 fprintf(stderr
, " -S <file>\tseccomp filter config\n");
608 fprintf(stderr
, " -C <file>\tcapabilities drop config\n");
609 fprintf(stderr
, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
610 fprintf(stderr
, " -n <name>\tthe name of the jail\n");
611 fprintf(stderr
, "namespace jail options:\n");
612 fprintf(stderr
, " -h <hostname>\tchange the hostname of the jail\n");
613 fprintf(stderr
, " -N\t\tjail has network namespace\n");
614 fprintf(stderr
, " -f\t\tjail has user namespace\n");
615 fprintf(stderr
, " -F\t\tjail has cgroups namespace\n");
616 fprintf(stderr
, " -r <file>\treadonly files that should be staged\n");
617 fprintf(stderr
, " -w <file>\twriteable files that should be staged\n");
618 fprintf(stderr
, " -p\t\tjail has /proc\n");
619 fprintf(stderr
, " -s\t\tjail has /sys\n");
620 fprintf(stderr
, " -l\t\tjail has /dev/log\n");
621 fprintf(stderr
, " -u\t\tjail has a ubus socket\n");
622 fprintf(stderr
, " -U <name>\tuser to run jailed process\n");
623 fprintf(stderr
, " -G <name>\tgroup to run jailed process\n");
624 fprintf(stderr
, " -o\t\tremont jail root (/) read only\n");
625 fprintf(stderr
, " -R <dir>\texternal jail rootfs (system container)\n");
626 fprintf(stderr
, " -O <dir>\tdirectory for r/w overlayfs\n");
627 fprintf(stderr
, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
628 fprintf(stderr
, " -E\t\tfail if jail cannot be setup\n");
629 fprintf(stderr
, " -y\t\tprovide jail console\n");
630 fprintf(stderr
, " -J <dir>\tstart OCI bundle\n");
631 fprintf(stderr
, "\nWarning: by default root inside the jail is the same\n\
632 and he has the same powers as root outside the jail,\n\
633 thus he can escape the jail and/or break stuff.\n\
634 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
635 If you use none of the namespace jail options,\n\
636 ujail will not use namespace/build a jail,\n\
637 and will only drop capabilities/apply seccomp filter.\n\n");
640 static int exec_jail(void *pipes_ptr
)
642 int *pipes
= (int*)pipes_ptr
;
644 int pw_uid
, pw_gid
, gr_gid
;
650 if (write(pipes
[1], buf
, 1) < 1) {
651 ERROR("can't write to parent\n");
654 if (read(pipes
[2], buf
, 1) < 1) {
655 ERROR("can't read from parent\n");
659 ERROR("parent had an error, child exiting\n");
666 if (opts
.namespace & CLONE_NEWUSER
) {
667 if (setregid(0, 0) < 0) {
671 if (setreuid(0, 0) < 0) {
675 if (setgroups(0, NULL
) < 0) {
676 ERROR("setgroups\n");
681 if (opts
.namespace && opts
.hostname
&& strlen(opts
.hostname
) > 0
682 && sethostname(opts
.hostname
, strlen(opts
.hostname
))) {
683 ERROR("sethostname(%s) failed: %m\n", opts
.hostname
);
687 if ((opts
.namespace & CLONE_NEWNS
) && build_jail_fs()) {
688 ERROR("failed to build jail fs\n");
692 if (applyOCIcapabilities(opts
.capset
))
695 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
698 if (opts
.no_new_privs
&& prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
699 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
703 if (!(opts
.namespace & CLONE_NEWUSER
)) {
704 get_jail_user(&pw_uid
, &pw_gid
, &gr_gid
);
706 set_jail_user(opts
.pw_uid
?:pw_uid
, opts
.pw_gid
?:pw_gid
, opts
.gr_gid
?:gr_gid
);
709 char **envp
= build_envp(opts
.seccomp
, opts
.envp
);
713 if (opts
.ociseccomp
&& applyOCIlinuxseccomp(opts
.ociseccomp
))
716 INFO("exec-ing %s\n", *opts
.jail_argv
);
717 if (opts
.envp
) /* respect PATH if potentially set in ENV */
718 execvpe(*opts
.jail_argv
, opts
.jail_argv
, envp
);
720 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
722 /* we get there only if execve fails */
723 ERROR("failed to execve %s: %m\n", *opts
.jail_argv
);
727 static int jail_running
= 1;
728 static int jail_return_code
= 0;
730 static void jail_process_timeout_cb(struct uloop_timeout
*t
);
731 static struct uloop_timeout jail_process_timeout
= {
732 .cb
= jail_process_timeout_cb
,
735 static void jail_process_handler(struct uloop_process
*c
, int ret
)
737 uloop_timeout_cancel(&jail_process_timeout
);
738 if (WIFEXITED(ret
)) {
739 jail_return_code
= WEXITSTATUS(ret
);
740 INFO("jail (%d) exited with exit: %d\n", c
->pid
, jail_return_code
);
742 jail_return_code
= WTERMSIG(ret
);
743 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
749 static struct uloop_process jail_process
= {
750 .cb
= jail_process_handler
,
753 static void jail_process_timeout_cb(struct uloop_timeout
*t
)
755 DEBUG("jail process failed to stop, sending SIGKILL\n");
756 kill(jail_process
.pid
, SIGKILL
);
759 static void jail_handle_signal(int signo
)
761 DEBUG("forwarding signal %d to the jailed process\n", signo
);
762 kill(jail_process
.pid
, signo
);
765 static int netns_open_pid(const pid_t target_ns
)
767 char pid_net_path
[PATH_MAX
];
769 snprintf(pid_net_path
, sizeof(pid_net_path
), "/proc/%u/ns/net", target_ns
);
771 return open(pid_net_path
, O_RDONLY
);
774 static void netns_updown(pid_t pid
, bool start
)
776 struct ubus_context
*ctx
= ubus_connect(NULL
);
777 static struct blob_buf req
;
783 blob_buf_init(&req
, 0);
784 blobmsg_add_string(&req
, "jail", opts
.name
);
785 blobmsg_add_u32(&req
, "pid", pid
);
786 blobmsg_add_u8(&req
, "start", start
);
788 if (ubus_lookup_id(ctx
, "network", &id
) ||
789 ubus_invoke(ctx
, id
, "netns_updown", req
.head
, NULL
, NULL
, 3000))
790 INFO("ubus request failed\n");
803 static const struct blobmsg_policy oci_root_policy
[] = {
804 [OCI_ROOT_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
805 [OCI_ROOT_READONLY
] = { "readonly", BLOBMSG_TYPE_BOOL
},
808 static int parseOCIroot(const char *jsonfile
, struct blob_attr
*msg
)
810 static char rootpath
[PATH_MAX
] = { 0 };
811 struct blob_attr
*tb
[__OCI_ROOT_MAX
];
814 blobmsg_parse(oci_root_policy
, __OCI_ROOT_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
816 if (!tb
[OCI_ROOT_PATH
])
819 strncpy(rootpath
, jsonfile
, PATH_MAX
);
820 cur
= strrchr(rootpath
, '/');
826 strncat(rootpath
, blobmsg_get_string(tb
[OCI_ROOT_PATH
]), PATH_MAX
- (strlen(rootpath
) + 1));
828 opts
.extroot
= rootpath
;
830 opts
.ronly
= blobmsg_get_bool(tb
[OCI_ROOT_READONLY
]);
838 OCI_MOUNT_DESTINATION
,
844 static const struct blobmsg_policy oci_mount_policy
[] = {
845 [OCI_MOUNT_SOURCE
] = { "source", BLOBMSG_TYPE_STRING
},
846 [OCI_MOUNT_DESTINATION
] = { "destination", BLOBMSG_TYPE_STRING
},
847 [OCI_MOUNT_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
848 [OCI_MOUNT_OPTIONS
] = { "options", BLOBMSG_TYPE_ARRAY
},
851 static int parseOCImount(struct blob_attr
*msg
)
853 struct blob_attr
*tb
[__OCI_MOUNT_MAX
];
855 blobmsg_parse(oci_mount_policy
, __OCI_MOUNT_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
857 if (!tb
[OCI_MOUNT_DESTINATION
])
860 if (!strcmp("proc", blobmsg_get_string(tb
[OCI_MOUNT_TYPE
])) &&
861 !strcmp("/proc", blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]))) {
866 if (!strcmp("sysfs", blobmsg_get_string(tb
[OCI_MOUNT_TYPE
])) &&
867 !strcmp("/sys", blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]))) {
872 if (!strcmp("tmpfs", blobmsg_get_string(tb
[OCI_MOUNT_TYPE
])) &&
873 !strcmp("/dev", blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]))) {
874 /* we always mount a small tmpfs on /dev */
878 INFO("ignoring unsupported mount %s %s -t %s -o %s\n",
879 blobmsg_get_string(tb
[OCI_MOUNT_SOURCE
]),
880 blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]),
881 blobmsg_get_string(tb
[OCI_MOUNT_TYPE
]),
882 blobmsg_format_json(tb
[OCI_MOUNT_OPTIONS
], true));
889 OCI_PROCESS_USER_UID
,
890 OCI_PROCESS_USER_GID
,
891 OCI_PROCESS_USER_UMASK
,
892 OCI_PROCESS_USER_ADDITIONALGIDS
,
893 __OCI_PROCESS_USER_MAX
,
896 static const struct blobmsg_policy oci_process_user_policy
[] = {
897 [OCI_PROCESS_USER_UID
] = { "uid", BLOBMSG_TYPE_INT32
},
898 [OCI_PROCESS_USER_GID
] = { "gid", BLOBMSG_TYPE_INT32
},
899 [OCI_PROCESS_USER_UMASK
] = { "umask", BLOBMSG_TYPE_INT32
},
900 [OCI_PROCESS_USER_ADDITIONALGIDS
] = { "additionalGids", BLOBMSG_TYPE_ARRAY
},
903 static int parseOCIprocessuser(struct blob_attr
*msg
) {
904 struct blob_attr
*tb
[__OCI_PROCESS_USER_MAX
];
906 blobmsg_parse(oci_process_user_policy
, __OCI_PROCESS_USER_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
908 if (tb
[OCI_PROCESS_USER_UID
])
909 opts
.pw_uid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_UID
]);
911 if (tb
[OCI_PROCESS_USER_GID
]) {
912 opts
.pw_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
913 opts
.gr_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
916 /* ToDo: umask, additional GIDs */
923 OCI_PROCESS_CAPABILITIES
,
926 OCI_PROCESS_NONEWPRIVILEGES
,
928 OCI_PROCESS_TERMINAL
,
933 static const struct blobmsg_policy oci_process_policy
[] = {
934 [OCI_PROCESS_ARGS
] = { "args", BLOBMSG_TYPE_ARRAY
},
935 [OCI_PROCESS_CAPABILITIES
] = { "capabilities", BLOBMSG_TYPE_TABLE
},
936 [OCI_PROCESS_CWD
] = { "cwd", BLOBMSG_TYPE_STRING
},
937 [OCI_PROCESS_ENV
] = { "env", BLOBMSG_TYPE_ARRAY
},
938 [OCI_PROCESS_NONEWPRIVILEGES
] = { "noNewPrivileges", BLOBMSG_TYPE_BOOL
},
939 [OCI_PROCESS_RLIMITS
] = { "rlimits", BLOBMSG_TYPE_ARRAY
},
940 [OCI_PROCESS_TERMINAL
] = { "terminal", BLOBMSG_TYPE_BOOL
},
941 [OCI_PROCESS_USER
] = { "user", BLOBMSG_TYPE_TABLE
},
944 static int parseOCIprocess(struct blob_attr
*msg
)
946 struct blob_attr
*tb
[__OCI_PROCESS_MAX
];
947 struct blob_attr
*cur
;
952 blobmsg_parse(oci_process_policy
, __OCI_PROCESS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
954 if (!tb
[OCI_PROCESS_ARGS
])
957 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_ARGS
], rem
)
963 opts
.jail_argv
= calloc(1 + sz
, sizeof(char*));
968 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_ARGS
], rem
)
969 opts
.jail_argv
[sz
++] = blobmsg_get_string(cur
);
971 opts
.console
= blobmsg_get_bool(tb
[OCI_PROCESS_TERMINAL
]);
972 opts
.no_new_privs
= blobmsg_get_bool(tb
[OCI_PROCESS_NONEWPRIVILEGES
]);
974 if (tb
[OCI_PROCESS_CWD
])
975 opts
.cwd
= blobmsg_get_string(tb
[OCI_PROCESS_CWD
]);
978 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_ENV
], rem
)
982 opts
.envp
= calloc(1 + sz
, sizeof(char*));
988 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_ENV
], rem
)
989 opts
.envp
[sz
++] = strdup(blobmsg_get_string(cur
));
991 if (tb
[OCI_PROCESS_USER
] && (res
= parseOCIprocessuser(tb
[OCI_PROCESS_USER
])))
994 if (tb
[OCI_PROCESS_CAPABILITIES
] &&
995 (res
= parseOCIcapabilities(&opts
.capset
, tb
[OCI_PROCESS_CAPABILITIES
])))
998 /* ToDo: rlimits, capabilities */
1004 OCI_LINUX_NAMESPACE_TYPE
,
1005 OCI_LINUX_NAMESPACE_PATH
,
1006 __OCI_LINUX_NAMESPACE_MAX
,
1009 static const struct blobmsg_policy oci_linux_namespace_policy
[] = {
1010 [OCI_LINUX_NAMESPACE_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
1011 [OCI_LINUX_NAMESPACE_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
1014 static unsigned int resolve_nstype(char *type
) {
1015 if (!strcmp("pid", type
))
1016 return CLONE_NEWPID
;
1017 else if (!strcmp("network", type
))
1018 return CLONE_NEWNET
;
1019 else if (!strcmp("mount", type
))
1021 else if (!strcmp("ipc", type
))
1022 return CLONE_NEWIPC
;
1023 else if (!strcmp("uts", type
))
1024 return CLONE_NEWUTS
;
1025 else if (!strcmp("user", type
))
1026 return CLONE_NEWUSER
;
1027 else if (!strcmp("cgroup", type
))
1028 return CLONE_NEWCGROUP
;
1033 static int parseOCIlinuxns(struct blob_attr
*msg
)
1035 struct blob_attr
*tb
[__OCI_LINUX_NAMESPACE_MAX
];
1038 blobmsg_parse(oci_linux_namespace_policy
, __OCI_LINUX_NAMESPACE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1040 if (!tb
[OCI_LINUX_NAMESPACE_TYPE
])
1043 if (tb
[OCI_LINUX_NAMESPACE_PATH
])
1044 return ENOTSUP
; /* ToDo */
1046 opts
.namespace |= resolve_nstype(blobmsg_get_string(tb
[OCI_LINUX_NAMESPACE_TYPE
]));
1053 OCI_LINUX_UIDGIDMAP_CONTAINERID
,
1054 OCI_LINUX_UIDGIDMAP_HOSTID
,
1055 OCI_LINUX_UIDGIDMAP_SIZE
,
1056 __OCI_LINUX_UIDGIDMAP_MAX
,
1059 static const struct blobmsg_policy oci_linux_uidgidmap_policy
[] = {
1060 [OCI_LINUX_UIDGIDMAP_CONTAINERID
] = { "containerID", BLOBMSG_TYPE_INT32
},
1061 [OCI_LINUX_UIDGIDMAP_HOSTID
] = { "hostID", BLOBMSG_TYPE_INT32
},
1062 [OCI_LINUX_UIDGIDMAP_SIZE
] = { "size", BLOBMSG_TYPE_INT32
},
1065 static int parseOCIuidgidmappings(struct blob_attr
*msg
, bool is_gidmap
)
1067 const char *map_format
= "%d %d %d\n";
1068 struct blob_attr
*tb
[__OCI_LINUX_UIDGIDMAP_MAX
];
1069 struct blob_attr
*cur
;
1073 unsigned int cnt
= 0;
1074 size_t totallen
= 0;
1076 /* count number of mappings */
1077 blobmsg_for_each_attr(cur
, msg
, rem
)
1083 /* allocate array for mappings */
1084 mappings
= calloc(1 + cnt
, sizeof(char*));
1088 mappings
[cnt
] = NULL
;
1091 blobmsg_for_each_attr(cur
, msg
, rem
) {
1092 blobmsg_parse(oci_linux_uidgidmap_policy
, __OCI_LINUX_UIDGIDMAP_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1094 if (!tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
] ||
1095 !tb
[OCI_LINUX_UIDGIDMAP_HOSTID
] ||
1096 !tb
[OCI_LINUX_UIDGIDMAP_SIZE
])
1099 /* write mapping line into allocated string */
1100 len
= asprintf(&mappings
[cnt
++], map_format
,
1101 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
]),
1102 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_HOSTID
]),
1103 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_SIZE
]));
1111 /* allocate combined mapping string */
1112 map
= calloc(1 + totallen
, sizeof(char));
1118 /* concatenate mapping strings into combined string */
1119 curstr
= mappings
[0];
1121 strcat(map
, curstr
);
1135 OCI_LINUX_RESOURCES
,
1138 OCI_LINUX_NAMESPACES
,
1139 OCI_LINUX_UIDMAPPINGS
,
1140 OCI_LINUX_GIDMAPPINGS
,
1141 OCI_LINUX_MASKEDPATHS
,
1142 OCI_LINUX_READONLYPATHS
,
1143 OCI_LINUX_ROOTFSPROPAGATION
,
1147 static const struct blobmsg_policy oci_linux_policy
[] = {
1148 [OCI_LINUX_RESOURCES
] = { "resources", BLOBMSG_TYPE_TABLE
},
1149 [OCI_LINUX_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_TABLE
},
1150 [OCI_LINUX_SYSCTL
] = { "sysctl", BLOBMSG_TYPE_TABLE
},
1151 [OCI_LINUX_NAMESPACES
] = { "namespaces", BLOBMSG_TYPE_ARRAY
},
1152 [OCI_LINUX_UIDMAPPINGS
] = { "uidMappings", BLOBMSG_TYPE_ARRAY
},
1153 [OCI_LINUX_GIDMAPPINGS
] = { "gidMappings", BLOBMSG_TYPE_ARRAY
},
1154 [OCI_LINUX_MASKEDPATHS
] = { "maskedPaths", BLOBMSG_TYPE_ARRAY
},
1155 [OCI_LINUX_READONLYPATHS
] = { "readonlyPaths", BLOBMSG_TYPE_ARRAY
},
1156 [OCI_LINUX_ROOTFSPROPAGATION
] = { "rootfsPropagation", BLOBMSG_TYPE_STRING
},
1159 static int parseOCIlinux(struct blob_attr
*msg
)
1161 struct blob_attr
*tb
[__OCI_LINUX_MAX
];
1162 struct blob_attr
*cur
;
1166 blobmsg_parse(oci_linux_policy
, __OCI_LINUX_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1168 if (tb
[OCI_LINUX_NAMESPACES
]) {
1169 blobmsg_for_each_attr(cur
, tb
[OCI_LINUX_NAMESPACES
], rem
) {
1170 res
= parseOCIlinuxns(cur
);
1176 if (tb
[OCI_LINUX_UIDMAPPINGS
]) {
1177 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 0);
1182 if (tb
[OCI_LINUX_GIDMAPPINGS
]) {
1183 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 1);
1188 if (tb
[OCI_LINUX_SECCOMP
]) {
1189 opts
.ociseccomp
= parseOCIlinuxseccomp(tb
[OCI_LINUX_SECCOMP
]);
1190 if (!opts
.ociseccomp
)
1207 static const struct blobmsg_policy oci_policy
[] = {
1208 [OCI_VERSION
] = { "ociVersion", BLOBMSG_TYPE_STRING
},
1209 [OCI_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
1210 [OCI_PROCESS
] = { "process", BLOBMSG_TYPE_TABLE
},
1211 [OCI_ROOT
] = { "root", BLOBMSG_TYPE_TABLE
},
1212 [OCI_MOUNTS
] = { "mounts", BLOBMSG_TYPE_ARRAY
},
1213 [OCI_LINUX
] = { "linux", BLOBMSG_TYPE_TABLE
},
1216 static int parseOCI(const char *jsonfile
)
1218 struct blob_attr
*tb
[__OCI_MAX
];
1219 struct blob_attr
*cur
;
1223 blob_buf_init(&ocibuf
, 0);
1224 if (!blobmsg_add_json_from_file(&ocibuf
, jsonfile
))
1227 blobmsg_parse(oci_policy
, __OCI_MAX
, tb
, blob_data(ocibuf
.head
), blob_len(ocibuf
.head
));
1229 if (!tb
[OCI_VERSION
])
1232 if (strncmp("1.0", blobmsg_get_string(tb
[OCI_VERSION
]), 3)) {
1233 ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb
[OCI_VERSION
]));
1237 if (tb
[OCI_HOSTNAME
])
1238 opts
.hostname
= blobmsg_get_string(tb
[OCI_HOSTNAME
]);
1240 if (!tb
[OCI_PROCESS
])
1243 if ((res
= parseOCIprocess(tb
[OCI_PROCESS
])))
1249 if ((res
= parseOCIroot(jsonfile
, tb
[OCI_ROOT
])))
1252 if (!tb
[OCI_MOUNTS
])
1255 blobmsg_for_each_attr(cur
, tb
[OCI_MOUNTS
], rem
)
1256 if ((res
= parseOCImount(cur
)))
1259 if (tb
[OCI_LINUX
] && (res
= parseOCIlinux(tb
[OCI_LINUX
])))
1265 int main(int argc
, char **argv
)
1268 uid_t uid
= getuid();
1269 const char log
[] = "/dev/log";
1270 const char ubus
[] = "/var/run/ubus.sock";
1271 char *jsonfile
= NULL
;
1278 ERROR("not root, aborting: %m\n");
1279 return EXIT_FAILURE
;
1284 init_library_search();
1286 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
1289 debug
= atoi(optarg
);
1292 opts
.namespace |= CLONE_NEWNS
;
1296 opts
.namespace |= CLONE_NEWNS
;
1300 opts
.namespace |= CLONE_NEWUSER
;
1303 opts
.namespace |= CLONE_NEWCGROUP
;
1306 opts
.extroot
= optarg
;
1309 opts
.namespace |= CLONE_NEWNS
;
1313 opts
.seccomp
= optarg
;
1314 add_mount(optarg
, 1, -1);
1317 opts
.capabilities
= optarg
;
1320 opts
.no_new_privs
= 1;
1326 opts
.namespace |= CLONE_NEWNET
;
1329 opts
.namespace |= CLONE_NEWUTS
;
1330 opts
.hostname
= optarg
;
1333 opts
.namespace |= CLONE_NEWNS
;
1334 add_path_and_deps(optarg
, 1, 0, 0);
1337 opts
.namespace |= CLONE_NEWNS
;
1338 add_path_and_deps(optarg
, 0, 0, 0);
1341 opts
.namespace |= CLONE_NEWNS
;
1342 add_mount(ubus
, 0, -1);
1345 opts
.namespace |= CLONE_NEWNS
;
1346 add_mount(log
, 0, -1);
1352 opts
.group
= optarg
;
1355 opts
.overlaydir
= optarg
;
1358 opts
.tmpoverlaysize
= optarg
;
1361 opts
.require_jail
= 1;
1367 asprintf(&jsonfile
, "%s/config.json", optarg
);
1373 opts
.namespace |= CLONE_NEWIPC
| CLONE_NEWPID
;
1377 ocires
= parseOCI(jsonfile
);
1380 ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires
), ocires
);
1385 if (opts
.tmpoverlaysize
&& strlen(opts
.tmpoverlaysize
) > 8) {
1386 ERROR("size parameter too long: \"%s\"\n", opts
.tmpoverlaysize
);
1390 /* no <binary> param found */
1391 if (!jsonfile
&& (argc
- optind
< 1)) {
1393 return EXIT_FAILURE
;
1395 if (!(opts
.namespace||opts
.capabilities
||opts
.seccomp
)) {
1396 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
1398 return EXIT_FAILURE
;
1400 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
1402 opts
.capabilities
!= 0 || opts
.capset
.apply
,
1403 opts
.seccomp
!= 0 || opts
.ociseccomp
!= 0);
1406 opts
.jail_argv
= &argv
[optind
];
1407 if (opts
.namespace & CLONE_NEWUSER
)
1408 get_jail_user(&opts
.pw_uid
, &opts
.pw_gid
, &opts
.gr_gid
);
1411 if (!opts
.extroot
) {
1412 if (opts
.namespace && add_path_and_deps(*opts
.jail_argv
, 1, -1, 0)) {
1413 ERROR("failed to load dependencies\n");
1418 if (opts
.namespace && opts
.seccomp
&& add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
1419 ERROR("failed to load libpreload-seccomp.so\n");
1421 if (opts
.require_jail
)
1426 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
1430 sigfillset(&sigmask
);
1431 for (i
= 0; i
< _NSIG
; i
++) {
1432 struct sigaction s
= { 0 };
1434 if (!sigismember(&sigmask
, i
))
1436 if ((i
== SIGCHLD
) || (i
== SIGPIPE
) || (i
== SIGSEGV
))
1439 s
.sa_handler
= jail_handle_signal
;
1440 sigaction(i
, &s
, NULL
);
1443 if (opts
.namespace) {
1444 if (opts
.namespace & CLONE_NEWNS
) {
1445 add_mount("/dev/full", 0, -1);
1446 add_mount("/dev/null", 0, -1);
1447 add_mount("/dev/random", 0, -1);
1448 add_mount("/dev/urandom", 0, -1);
1449 add_mount("/dev/zero", 0, -1);
1450 add_mount("/dev/ptmx", 0, -1);
1451 add_mount("/dev/tty", 0, -1);
1453 if (!opts
.extroot
&& (opts
.user
|| opts
.group
)) {
1454 add_mount("/etc/passwd", 0, -1);
1455 add_mount("/etc/group", 0, -1);
1458 #if defined(__GLIBC__)
1460 add_mount("/etc/nsswitch.conf", 0, -1);
1463 if (!(opts
.namespace & CLONE_NEWNET
)) {
1464 add_mount("/etc/resolv.conf", 0, -1);
1468 if (pipe(&pipes
[0]) < 0 || pipe(&pipes
[2]) < 0)
1471 jail_process
.pid
= clone(exec_jail
, child_stack
+ STACK_SIZE
, SIGCHLD
| opts
.namespace, &pipes
);
1473 jail_process
.pid
= fork();
1476 if (jail_process
.pid
> 0) {
1478 /* parent process */
1481 if (read(pipes
[0], sig_buf
, 1) < 1) {
1482 ERROR("can't read from child\n");
1486 if (opts
.namespace & CLONE_NEWUSER
) {
1487 if (write_setgroups(jail_process
.pid
, true)) {
1488 ERROR("can't write setgroups\n");
1492 bool has_gr
= (opts
.gr_gid
!= -1);
1493 if (opts
.pw_uid
!= -1) {
1494 write_single_uid_gid_map(jail_process
.pid
, 0, opts
.pw_uid
);
1495 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:opts
.pw_gid
);
1497 write_single_uid_gid_map(jail_process
.pid
, 0, 65534);
1498 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:65534);
1501 write_uid_gid_map(jail_process
.pid
, 0, opts
.uidmap
);
1503 write_uid_gid_map(jail_process
.pid
, 1, opts
.gidmap
);
1507 if (opts
.namespace & CLONE_NEWNET
) {
1509 ERROR("netns needs a named jail\n");
1512 netns_fd
= netns_open_pid(jail_process
.pid
);
1513 netns_updown(jail_process
.pid
, true);
1517 if (write(pipes
[3], sig_buf
, 1) < 0) {
1518 ERROR("can't write to child\n");
1522 uloop_process_add(&jail_process
);
1525 DEBUG("uloop interrupted, killing jail process\n");
1526 kill(jail_process
.pid
, SIGTERM
);
1527 uloop_timeout_set(&jail_process_timeout
, 1000);
1531 if (opts
.namespace & CLONE_NEWNET
) {
1532 setns(netns_fd
, CLONE_NEWNET
);
1533 netns_updown(getpid(), false);
1536 return jail_return_code
;
1537 } else if (jail_process
.pid
== 0) {
1538 /* fork child process */
1539 return exec_jail(NULL
);
1541 ERROR("failed to clone/fork: %m\n");
1542 return EXIT_FAILURE
;