1 /* nsenter.c - Enter existing namespaces
3 * Copyright 2014 Andy Lutomirski <luto@amacapital.net>
5 * See http://man7.org/linux/man-pages/man1/nsenter.1.html
7 * unshare.c - run command in new context
9 * Copyright 2011 Rob Landley <rob@landley.net>
11 * See http://man7.org/linux/man-pages/man1/unshare.1.html
14 // Note: flags go in same order (right to left) for shared subset
15 USE_NSENTER(NEWTOY(nsenter, "<1F(no-fork)t#<1(target)i:(ipc);m:(mount);n:(net);p:(pid);u:(uts);U:(user);", TOYFLAG_USR|TOYFLAG_BIN))
16 USE_UNSHARE(NEWTOY(unshare, "<1^f(fork);r(map-root-user);i:(ipc);m:(mount);n:(net);p:(pid);u:(uts);U:(user);", TOYFLAG_USR|TOYFLAG_BIN))
21 depends on TOYBOX_CONTAINER
23 usage: unshare [-imnpuUr] COMMAND...
25 Create new container namespace(s) for this process and its children, so
26 some attribute is not shared with the parent process.
28 -f Fork command in the background (--fork)
29 -i SysV IPC (message queues, semaphores, shared memory) (--ipc)
30 -m Mount/unmount tree (--mount)
31 -n Network address, sockets, routing, iptables (--net)
32 -p Process IDs and init (--pid)
33 -r Become root (map current euid/egid to 0/0, implies -U) (--map-root-user)
34 -u Host and domain names (--uts)
35 -U UIDs, GIDs, capabilities (--user)
37 A namespace allows a set of processes to have a different view of the
38 system than other sets of processes.
44 usage: nsenter [-t pid] [-F] [-i] [-m] [-n] [-p] [-u] [-U] COMMAND...
46 Run COMMAND in an existing (set of) namespace(s).
48 -t PID to take namespaces from (--target)
49 -F don't fork, even if -p is used (--no-fork)
51 The namespaces to switch are:
53 -i SysV IPC: message queues, semaphores, shared memory (--ipc)
54 -m Mount/unmount tree (--mount)
55 -n Network address, sockets, routing, iptables (--net)
56 -p Process IDs and init, will fork unless -F is used (--pid)
57 -u Host and domain names (--uts)
58 -U UIDs, GIDs, capabilities (--user)
60 If -t isn't specified, each namespace argument must provide a path
61 to a namespace file, ala "-i=/proc/$PID/ns/ipc"
66 #include <linux/sched.h>
67 int unshare(int flags);
68 int setns(int fd, int nstype);
75 // Code that must run in unshare's flag context
76 #define CLEANUP_nsenter
78 #include <generated/flags.h>
80 static void write_ugid_map(char *map, unsigned eugid)
82 int bytes = sprintf(toybuf, "0 %u 1", eugid), fd = xopen(map, O_WRONLY);
84 xwrite(fd, toybuf, bytes);
88 static void handle_r(int euid, int egid)
92 if ((fd = open("/proc/self/setgroups", O_WRONLY)) >= 0) {
93 xwrite(fd, "deny", 4);
97 write_ugid_map("/proc/self/uid_map", euid);
98 write_ugid_map("/proc/self/gid_map", egid);
103 return toys.optflags & FLAG_r;
108 return toys.optflags & FLAG_f;
111 // Shift back to the context GLOBALS lives in (I.E. matching the filename).
112 #define CLEANUP_unshare
114 #include <generated/flags.h>
116 void unshare_main(void)
118 unsigned flags[]={CLONE_NEWUSER, CLONE_NEWUTS, CLONE_NEWPID, CLONE_NEWNET,
119 CLONE_NEWNS, CLONE_NEWIPC}, f = 0;
122 // Create new namespace(s)?
123 if (CFG_UNSHARE && *toys.which->name=='u') {
124 // For -r, we have to save our original [ug]id before calling unshare()
125 int euid = geteuid(), egid = getegid();
127 // unshare -U does not imply -r, so we cannot use [+rU]
128 if (test_r()) toys.optflags |= FLAG_U;
130 for (i = 0; i<ARRAY_LEN(flags); i++)
131 if (toys.optflags & (1<<i)) f |= flags[i];
133 if (unshare(f)) perror_exit(0);
134 if (test_r()) handle_r(euid, egid);
137 toys.exitval = xrun(toys.optargs);
141 // Bind to existing namespace(s)?
142 } else if (CFG_NSENTER) {
143 char *nsnames = "user\0uts\0pid\0net\0mnt\0ipc";
145 for (i = 0; i<ARRAY_LEN(flags); i++) {
146 char *filename = TT.nsnames[i];
148 if (toys.optflags & (1<<i)) {
149 if (!filename || !*filename) {
150 if (!(toys.optflags & FLAG_t)) error_exit("need -t or =filename");
151 sprintf(toybuf, "/proc/%ld/ns/%s", TT.targetpid, nsnames);
155 if (setns(fd = xopenro(filename), flags[i])) perror_exit("setns");
158 nsnames += strlen(nsnames)+1;
161 if ((toys.optflags & FLAG_p) && !(toys.optflags & FLAG_F)) {
162 toys.exitval = xrun(toys.optargs);
171 void nsenter_main(void)