• 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 <sys/syscall.h>
68 #include <linux/sched.h>
69 
70 #define unshare(flags) syscall(SYS_unshare, flags)
71 #define setns(fd, nstype) syscall(SYS_setns, fd, nstype)
72 
GLOBALS(char * Uupnmi[6];long t;)73 GLOBALS(
74   char *Uupnmi[6];
75   long t;
76 )
77 
78 // Code that must run in unshare's flag context
79 #define CLEANUP_nsenter
80 #define FOR_unshare
81 #include <generated/flags.h>
82 
83 static void write_ugid_map(char *map, unsigned eugid)
84 {
85   int fd = xopen(map, O_WRONLY);
86 
87   dprintf(fd, "0 %u 1", eugid);
88   xclose(fd);
89 }
90 
handle_r(int euid,int egid)91 static void handle_r(int euid, int egid)
92 {
93   int fd;
94 
95   if ((fd = open("/proc/self/setgroups", O_WRONLY)) >= 0) {
96     xwrite(fd, "deny", 4);
97     close(fd);
98   }
99 
100   write_ugid_map("/proc/self/uid_map", euid);
101   write_ugid_map("/proc/self/gid_map", egid);
102 }
103 
test_r()104 static int test_r()
105 {
106   return toys.optflags & FLAG_r;
107 }
108 
test_f()109 static int test_f()
110 {
111   return toys.optflags & FLAG_f;
112 }
113 
114 // Shift back to the context GLOBALS lives in (I.E. matching the filename).
115 #define CLEANUP_unshare
116 #define FOR_nsenter
117 #include <generated/flags.h>
118 
unshare_main(void)119 void unshare_main(void)
120 {
121   unsigned flags[]={CLONE_NEWUSER, CLONE_NEWUTS, CLONE_NEWPID, CLONE_NEWNET,
122                     CLONE_NEWNS, CLONE_NEWIPC}, f = 0;
123   int i, fd;
124 
125   // Create new namespace(s)?
126   if (CFG_UNSHARE && *toys.which->name=='u') {
127     // For -r, we have to save our original [ug]id before calling unshare()
128     int euid = geteuid(), egid = getegid();
129 
130     // unshare -U does not imply -r, so we cannot use [+rU]
131     if (test_r()) toys.optflags |= FLAG_U;
132 
133     for (i = 0; i<ARRAY_LEN(flags); i++)
134       if (toys.optflags & (1<<i)) f |= flags[i];
135 
136     if (unshare(f)) perror_exit(0);
137     if (test_r()) handle_r(euid, egid);
138 
139     if (test_f()) {
140       toys.exitval = xrun(toys.optargs);
141 
142       return;
143     }
144   // Bind to existing namespace(s)?
145   } else if (CFG_NSENTER) {
146     char *nsnames = "user\0uts\0pid\0net\0mnt\0ipc";
147 
148     for (i = 0; i<ARRAY_LEN(flags); i++) {
149       char *filename = TT.Uupnmi[i];
150 
151       if (toys.optflags & (1<<i)) {
152         if (!filename || !*filename) {
153           if (!(toys.optflags & FLAG_t)) error_exit("need -t or =filename");
154           sprintf(toybuf, "/proc/%ld/ns/%s", TT.t, nsnames);
155           filename = toybuf;
156         }
157 
158         if (setns(fd = xopenro(filename), flags[i])) perror_exit("setns");
159         close(fd);
160       }
161       nsnames += strlen(nsnames)+1;
162     }
163 
164     if ((toys.optflags & FLAG_p) && !(toys.optflags & FLAG_F)) {
165       toys.exitval = xrun(toys.optargs);
166 
167       return;
168     }
169   }
170 
171   xexec(toys.optargs);
172 }
173 
nsenter_main(void)174 void nsenter_main(void)
175 {
176   unshare_main();
177 }
178