1 /* nsenter.c - Enter existing namespaces
2  *
3  * Copyright 2014 andy Lutomirski <luto@amacapital.net>
4  *
5  * No standard
6  *
7  * unshare.c - run command in new context
8  *
9  * Copyright 2011 Rob Landley <rob@landley.net>
10  *
11  * No Standard
12  *
13 
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^rimnpuU", TOYFLAG_USR|TOYFLAG_BIN))
17 
18 config UNSHARE
19   bool "unshare"
20   default y
21   depends on TOYBOX_CONTAINER
22   help
23     usage: unshare [-imnpuUr] COMMAND...
24 
25     Create new container namespace(s) for this process and its children, so
26     some attribute is not shared with the parent process.
27 
28     -i	SysV IPC (message queues, semaphores, shared memory)
29     -m	Mount/unmount tree
30     -n	Network address, sockets, routing, iptables
31     -p	Process IDs and init
32     -r	Become root (map current euid/egid to 0/0, implies -U)
33     -u	Host and domain names
34     -U	UIDs, GIDs, capabilities
35 
36     A namespace allows a set of processes to have a different view of the
37     system than other sets of processes.
38 
39 config NSENTER
40   bool "nsenter"
41   default y
42   help
43     usage: nsenter [-t pid] [-F] [-i] [-m] [-n] [-p] [-u] [-U] COMMAND...
44 
45     Run COMMAND in an existing (set of) namespace(s).
46 
47     -t  PID to take namespaces from    (--target)
48     -F  don't fork, even if -p is used (--no-fork)
49 
50     The namespaces to switch are:
51 
52     -i	SysV IPC: message queues, semaphores, shared memory (--ipc)
53     -m	Mount/unmount tree (--mnt)
54     -n	Network address, sockets, routing, iptables (--net)
55     -p	Process IDs and init, will fork unless -F is used (--pid)
56     -u	Host and domain names (--uts)
57     -U	UIDs, GIDs, capabilities (--user)
58 
59     If -t isn't specified, each namespace argument must provide a path
60     to a namespace file, ala "-i=/proc/$PID/ns/ipc"
61 */
62 
63 #define FOR_nsenter
64 #include "toys.h"
65 #include <linux/sched.h>
66 int unshare(int flags);
67 int setns(int fd, int nstype);
68 
GLOBALS(char * nsnames[6];long targetpid;)69 GLOBALS(
70   char *nsnames[6];
71   long targetpid;
72 )
73 
74 // Code that must run in unshare's flag context
75 #define CLEANUP_nsenter
76 #define FOR_unshare
77 #include <generated/flags.h>
78 
79 static void write_ugid_map(char *map, unsigned eugid)
80 {
81   int bytes = sprintf(toybuf, "0 %u 1", eugid), fd = xopen(map, O_WRONLY);
82 
83   xwrite(fd, toybuf, bytes);
84   xclose(fd);
85 }
86 
handle_r(int euid,int egid)87 static void handle_r(int euid, int egid)
88 {
89   int fd;
90 
91   if ((fd = open("/proc/self/setgroups", O_WRONLY)) >= 0) {
92     xwrite(fd, "deny", 4);
93     close(fd);
94   }
95 
96   write_ugid_map("/proc/self/uid_map", euid);
97   write_ugid_map("/proc/self/gid_map", egid);
98 }
99 
test_r()100 static int test_r()
101 {
102   return toys.optflags & FLAG_r;
103 }
104 
105 // Shift back to the context GLOBALS lives in (I.E. matching the filename).
106 #define CLEANUP_unshare
107 #define FOR_nsenter
108 #include <generated/flags.h>
109 
unshare_main(void)110 void unshare_main(void)
111 {
112   unsigned flags[]={CLONE_NEWUSER, CLONE_NEWUTS, CLONE_NEWPID, CLONE_NEWNET,
113                     CLONE_NEWNS, CLONE_NEWIPC}, f = 0;
114   int i, fd;
115 
116   // Create new namespace(s)?
117   if (CFG_UNSHARE && *toys.which->name=='u') {
118     // For -r, we have to save our original [ug]id before calling unshare()
119     int euid = geteuid(), egid = getegid();
120 
121     // unshare -U does not imply -r, so we cannot use [+rU]
122     if (test_r()) toys.optflags |= FLAG_U;
123 
124     for (i = 0; i<ARRAY_LEN(flags); i++)
125       if (toys.optflags & (1<<i)) f |= flags[i];
126 
127     if (unshare(f)) perror_exit(0);
128     if (test_r()) handle_r(euid, egid);
129 
130   // Bind to existing namespace(s)?
131   } else if (CFG_NSENTER) {
132     char *nsnames = "user\0uts\0pid\0net\0mnt\0ipc";
133 
134     for (i = 0; i<ARRAY_LEN(flags); i++) {
135       char *filename = TT.nsnames[i];
136 
137       if (toys.optflags & (1<<i)) {
138         if (!filename || !*filename) {
139           if (!(toys.optflags & FLAG_t)) error_exit("need -t or =filename");
140           sprintf(toybuf, "/proc/%ld/ns/%s", TT.targetpid, nsnames);
141           filename = toybuf;
142         }
143 
144         if (setns(fd = xopen(filename, O_RDONLY), flags[i]))
145           perror_exit("setns");
146         close(fd);
147       }
148       nsnames += strlen(nsnames)+1;
149     }
150 
151     if ((toys.optflags & FLAG_p) && !(toys.optflags & FLAG_F)) {
152       pid_t pid = xfork();
153 
154       if (pid) {
155         while (waitpid(pid, 0, 0) == -1 && errno == EINTR);
156         return;
157       }
158     }
159   }
160 
161   xexec(toys.optargs);
162 }
163 
nsenter_main(void)164 void nsenter_main(void)
165 {
166   unshare_main();
167 }
168