123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- #include <sched.h>
- #ifndef CLONE_NEWUTS
- # define CLONE_NEWUTS 0x04000000
- #endif
- #ifndef CLONE_NEWIPC
- # define CLONE_NEWIPC 0x08000000
- #endif
- #ifndef CLONE_NEWUSER
- # define CLONE_NEWUSER 0x10000000
- #endif
- #ifndef CLONE_NEWPID
- # define CLONE_NEWPID 0x20000000
- #endif
- #ifndef CLONE_NEWNET
- # define CLONE_NEWNET 0x40000000
- #endif
- #include <sys/mount.h>
- #ifndef MS_REC
- # define MS_REC (1 << 14)
- #endif
- #ifndef MS_PRIVATE
- # define MS_PRIVATE (1 << 18)
- #endif
- #ifndef MS_SLAVE
- # define MS_SLAVE (1 << 19)
- #endif
- #ifndef MS_SHARED
- # define MS_SHARED (1 << 20)
- #endif
- #include "libbb.h"
- static void mount_or_die(const char *source, const char *target,
- const char *fstype, unsigned long mountflags)
- {
- if (mount(source, target, fstype, mountflags, NULL)) {
- bb_perror_msg_and_die("can't mount %s on %s (flags:0x%lx)",
- source, target, mountflags);
-
- }
- }
- #define PATH_PROC_SETGROUPS "/proc/self/setgroups"
- #define PATH_PROC_UIDMAP "/proc/self/uid_map"
- #define PATH_PROC_GIDMAP "/proc/self/gid_map"
- struct namespace_descr {
- int flag;
- const char nsfile4[4];
- };
- struct namespace_ctx {
- char *path;
- };
- enum {
- OPT_mount = 1 << 0,
- OPT_uts = 1 << 1,
- OPT_ipc = 1 << 2,
- OPT_net = 1 << 3,
- OPT_pid = 1 << 4,
- OPT_user = 1 << 5,
- OPT_fork = 1 << 6,
- OPT_map_root = 1 << 7,
- OPT_mount_proc = 1 << 8,
- OPT_propagation = 1 << 9,
- OPT_setgroups = 1 << 10,
- };
- enum {
- NS_MNT_POS = 0,
- NS_UTS_POS,
- NS_IPC_POS,
- NS_NET_POS,
- NS_PID_POS,
- NS_USR_POS,
- NS_COUNT,
- };
- static const struct namespace_descr ns_list[] = {
- { CLONE_NEWNS, "mnt" },
- { CLONE_NEWUTS, "uts" },
- { CLONE_NEWIPC, "ipc" },
- { CLONE_NEWNET, "net" },
- { CLONE_NEWPID, "pid" },
- { CLONE_NEWUSER, "user" },
- };
- #define OPT_STR "+muinpU""fr""\xfd::""\xfe:""\xff:"
- static const char unshare_longopts[] ALIGN1 =
- "mount\0" Optional_argument "\xf0"
- "uts\0" Optional_argument "\xf1"
- "ipc\0" Optional_argument "\xf2"
- "net\0" Optional_argument "\xf3"
- "pid\0" Optional_argument "\xf4"
- "user\0" Optional_argument "\xf5"
- "fork\0" No_argument "f"
- "map-root-user\0" No_argument "r"
- "mount-proc\0" Optional_argument "\xfd"
- "propagation\0" Required_argument "\xfe"
- "setgroups\0" Required_argument "\xff"
- ;
- #define PRIVATE_STR "private\0""unchanged\0""shared\0""slave\0"
- #define PRIVATE_UNCHANGED_SHARED_SLAVE PRIVATE_STR
- static unsigned long parse_propagation(const char *prop_str)
- {
- int i = index_in_strings(PRIVATE_UNCHANGED_SHARED_SLAVE, prop_str);
- if (i < 0)
- bb_error_msg_and_die("unrecognized: --%s=%s", "propagation", prop_str);
- if (i == 0)
- return MS_REC | MS_PRIVATE;
- if (i == 1)
- return 0;
- if (i == 2)
- return MS_REC | MS_SHARED;
- return MS_REC | MS_SLAVE;
- }
- static void mount_namespaces(pid_t pid, struct namespace_ctx *ns_ctx_list)
- {
- const struct namespace_descr *ns;
- struct namespace_ctx *ns_ctx;
- int i;
- for (i = 0; i < NS_COUNT; i++) {
- char nsf[sizeof("/proc/%u/ns/AAAA") + sizeof(int)*3];
- ns = &ns_list[i];
- ns_ctx = &ns_ctx_list[i];
- if (!ns_ctx->path)
- continue;
- sprintf(nsf, "/proc/%u/ns/%.4s", (unsigned)pid, ns->nsfile4);
- mount_or_die(nsf, ns_ctx->path, NULL, MS_BIND);
- }
- }
- int unshare_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int unshare_main(int argc UNUSED_PARAM, char **argv)
- {
- int i;
- unsigned int opts;
- int unsflags;
- uintptr_t need_mount;
- const char *proc_mnt_target;
- const char *prop_str;
- const char *setgrp_str;
- unsigned long prop_flags;
- uid_t reuid = geteuid();
- gid_t regid = getegid();
- struct fd_pair fdp;
- pid_t child = child;
- struct namespace_ctx ns_ctx_list[NS_COUNT];
- memset(ns_ctx_list, 0, sizeof(ns_ctx_list));
- proc_mnt_target = "/proc";
- prop_str = PRIVATE_STR;
- setgrp_str = NULL;
- opts = getopt32long(argv, "^" OPT_STR "\0"
- "\xf0""m"
- ":\xf1""u"
- ":\xf2""i"
- ":\xf3""n"
- ":\xf4""p"
- ":\xf5""U"
- ":rU"
- ":\xfd""m"
- , unshare_longopts,
- &proc_mnt_target, &prop_str, &setgrp_str,
- &ns_ctx_list[NS_MNT_POS].path,
- &ns_ctx_list[NS_UTS_POS].path,
- &ns_ctx_list[NS_IPC_POS].path,
- &ns_ctx_list[NS_NET_POS].path,
- &ns_ctx_list[NS_PID_POS].path,
- &ns_ctx_list[NS_USR_POS].path
- );
- argv += optind;
-
-
-
-
-
-
- if (setgrp_str) {
- if (strcmp(setgrp_str, "allow") == 0) {
- if (opts & OPT_map_root) {
- bb_error_msg_and_die(
- "--setgroups=allow and --map-root-user "
- "are mutually exclusive"
- );
- }
- } else {
-
- if (strcmp(setgrp_str, "deny") != 0)
- bb_error_msg_and_die("unrecognized: --%s=%s",
- "setgroups", setgrp_str);
- }
- }
- unsflags = 0;
- need_mount = 0;
- for (i = 0; i < NS_COUNT; i++) {
- const struct namespace_descr *ns = &ns_list[i];
- struct namespace_ctx *ns_ctx = &ns_ctx_list[i];
- if (opts & (1 << i))
- unsflags |= ns->flag;
- need_mount |= (uintptr_t)(ns_ctx->path);
- }
-
- prop_flags = MS_REC | MS_PRIVATE;
-
- if (opts & OPT_mount)
- prop_flags = parse_propagation(prop_str);
-
- fdp.wr = -1;
- if (need_mount && (opts & OPT_mount)) {
-
- pid_t ppid = getpid();
- xpiped_pair(fdp);
- child = xfork();
- if (child == 0) {
-
- close(fdp.wr);
-
- read(fdp.rd, ns_ctx_list, 1);
-
-
- mount_namespaces(ppid, ns_ctx_list);
- return EXIT_SUCCESS;
- }
-
- }
- if (unshare(unsflags) != 0)
- bb_perror_msg_and_die("unshare(0x%x)", unsflags);
- if (fdp.wr >= 0) {
- close(fdp.wr);
- close(fdp.rd);
- }
- if (need_mount) {
-
- if (opts & OPT_mount) {
- int exit_status = wait_for_exitstatus(child);
- if (WIFEXITED(exit_status) &&
- WEXITSTATUS(exit_status) != EXIT_SUCCESS)
- return WEXITSTATUS(exit_status);
- } else {
-
- mount_namespaces(getpid(), ns_ctx_list);
- }
- }
-
- if (opts & OPT_fork) {
- xvfork_parent_waits_and_exits();
-
- }
- if (opts & OPT_map_root) {
- char uidmap_buf[sizeof("0 %u 1") + sizeof(int)*3];
-
- xopen_xwrite_close(PATH_PROC_SETGROUPS, "deny");
- sprintf(uidmap_buf, "0 %u 1", (unsigned)reuid);
- xopen_xwrite_close(PATH_PROC_UIDMAP, uidmap_buf);
- sprintf(uidmap_buf, "0 %u 1", (unsigned)regid);
- xopen_xwrite_close(PATH_PROC_GIDMAP, uidmap_buf);
- } else
- if (setgrp_str) {
-
- xopen_xwrite_close(PATH_PROC_SETGROUPS, setgrp_str);
- }
- if (opts & OPT_mount) {
- mount_or_die("none", "/", NULL, prop_flags);
- }
- if (opts & OPT_mount_proc) {
-
- mount_or_die("none", proc_mnt_target, NULL, MS_PRIVATE | MS_REC);
- mount_or_die("proc", proc_mnt_target, "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV);
- }
- exec_prog_or_SHELL(argv);
- }
|