• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* nsenter.c - Enter existing namespaces
2  *
3  * Copyright 2014 Andy Lutomirski <luto@amacapital.net>
4  *
5  * See http://man7.org/linux/man-pages/man1/nsenter.1.html
6  *
7  * unshare.c - run command in new context
8  *
9  * Copyright 2011 Rob Landley <rob@landley.net>
10  *
11  * See http://man7.org/linux/man-pages/man1/unshare.1.html
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^f(fork);r(map-root-user);i:(ipc);m:(mount);n:(net);p:(pid);u:(uts);U:(user);", 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     -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)
36 
37     A namespace allows a set of processes to have a different view of the
38     system than other sets of processes.
39 
40 config NSENTER
41   bool "nsenter"
42   depends on TOYBOX_CONTAINER
43   default y
44   help
45     usage: nsenter [-t pid] [-F] [-i] [-m] [-n] [-p] [-u] [-U] COMMAND...
46 
47     Run COMMAND in an existing (set of) namespace(s).
48 
49     -t	PID to take namespaces from    (--target)
50     -F	don't fork, even if -p is used (--no-fork)
51 
52     The namespaces to switch are:
53 
54     -i	SysV IPC: message queues, semaphores, shared memory (--ipc)
55     -m	Mount/unmount tree (--mount)
56     -n	Network address, sockets, routing, iptables (--net)
57     -p	Process IDs and init, will fork unless -F is used (--pid)
58     -u	Host and domain names (--uts)
59     -U	UIDs, GIDs, capabilities (--user)
60 
61     If -t isn't specified, each namespace argument must provide a path
62     to a namespace file, ala "-i=/proc/$PID/ns/ipc"
63 */
64 
65 #define FOR_nsenter
66 #include "toys.h"
67 #include <linux/sched.h>
68 
69 #define unshare(flags) syscall(SYS_unshare, flags)
70 #define setns(fd, nstype) syscall(SYS_setns, fd, nstype)
71 
GLOBALS(char * Uupnmi[6];long t;)72 GLOBALS(
73   char *Uupnmi[6];
74   long t;
75 )
76 
77 // Code that must run in unshare's flag context
78 #define FOR_unshare
79 #include <generated/flags.h>
80 
81 static void write_ugid_map(char *map, unsigned eugid)
82 {
83   int fd = xopen(map, O_WRONLY);
84 
85   dprintf(fd, "0 %u 1", eugid);
86   xclose(fd);
87 }
88 
handle_r(int euid,int egid)89 static void handle_r(int euid, int egid)
90 {
91   int fd;
92 
93   if ((fd = open("/proc/self/setgroups", O_WRONLY)) >= 0) {
94     xwrite(fd, "deny", 4);
95     close(fd);
96   }
97 
98   write_ugid_map("/proc/self/uid_map", euid);
99   write_ugid_map("/proc/self/gid_map", egid);
100 }
101 
test_r()102 static int test_r()
103 {
104   return toys.optflags & FLAG_r;
105 }
106 
test_f()107 static int test_f()
108 {
109   return toys.optflags & FLAG_f;
110 }
111 
112 // Shift back to the context GLOBALS lives in (I.E. matching the filename).
113 #define FOR_nsenter
114 #include <generated/flags.h>
115 
unshare_main(void)116 void unshare_main(void)
117 {
118   unsigned flags[]={CLONE_NEWUSER, CLONE_NEWUTS, CLONE_NEWPID, CLONE_NEWNET,
119                     CLONE_NEWNS, CLONE_NEWIPC}, f = 0;
120   int i, fd;
121 
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();
126 
127     // unshare -U does not imply -r, so we cannot use [+rU]
128     if (test_r()) toys.optflags |= FLAG_U;
129 
130     for (i = 0; i<ARRAY_LEN(flags); i++)
131       if (toys.optflags & (1<<i)) f |= flags[i];
132 
133     if (unshare(f)) perror_exit(0);
134     if (test_r()) handle_r(euid, egid);
135 
136     if (test_f()) {
137       toys.exitval = xrun(toys.optargs);
138 
139       return;
140     }
141   // Bind to existing namespace(s)?
142   } else if (CFG_NSENTER) {
143     char *nsnames = "user\0uts\0pid\0net\0mnt\0ipc";
144 
145     for (i = 0; i<ARRAY_LEN(flags); i++) {
146       char *filename = TT.Uupnmi[i];
147 
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.t, nsnames);
152           filename = toybuf;
153         }
154 
155         if (setns(fd = xopenro(filename), flags[i])) perror_exit("setns");
156         close(fd);
157       }
158       nsnames += strlen(nsnames)+1;
159     }
160 
161     if ((toys.optflags & FLAG_p) && !(toys.optflags & FLAG_F)) {
162       toys.exitval = xrun(toys.optargs);
163 
164       return;
165     }
166   }
167 
168   xexec(toys.optargs);
169 }
170 
nsenter_main(void)171 void nsenter_main(void)
172 {
173   unshare_main();
174 }
175