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
15USE_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))
16USE_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
18config 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
40config NSENTER
41  bool "nsenter"
42  default y
43  help
44    usage: nsenter [-t pid] [-F] [-i] [-m] [-n] [-p] [-u] [-U] COMMAND...
45
46    Run COMMAND in an existing (set of) namespace(s).
47
48    -t  PID to take namespaces from    (--target)
49    -F  don't fork, even if -p is used (--no-fork)
50
51    The namespaces to switch are:
52
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)
59
60    If -t isn't specified, each namespace argument must provide a path
61    to a namespace file, ala "-i=/proc/$PID/ns/ipc"
62*/
63
64#define FOR_nsenter
65#include "toys.h"
66#include <linux/sched.h>
67int unshare(int flags);
68int setns(int fd, int nstype);
69
70GLOBALS(
71  char *nsnames[6];
72  long targetpid;
73)
74
75// Code that must run in unshare's flag context
76#define CLEANUP_nsenter
77#define FOR_unshare
78#include <generated/flags.h>
79
80static void write_ugid_map(char *map, unsigned eugid)
81{
82  int bytes = sprintf(toybuf, "0 %u 1", eugid), fd = xopen(map, O_WRONLY);
83
84  xwrite(fd, toybuf, bytes);
85  xclose(fd);
86}
87
88static void handle_r(int euid, int egid)
89{
90  int fd;
91
92  if ((fd = open("/proc/self/setgroups", O_WRONLY)) >= 0) {
93    xwrite(fd, "deny", 4);
94    close(fd);
95  }
96
97  write_ugid_map("/proc/self/uid_map", euid);
98  write_ugid_map("/proc/self/gid_map", egid);
99}
100
101static int test_r()
102{
103  return toys.optflags & FLAG_r;
104}
105
106static int test_f()
107{
108  return toys.optflags & FLAG_f;
109}
110
111// Shift back to the context GLOBALS lives in (I.E. matching the filename).
112#define CLEANUP_unshare
113#define FOR_nsenter
114#include <generated/flags.h>
115
116void 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.nsnames[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.targetpid, nsnames);
152          filename = toybuf;
153        }
154
155        if (setns(fd = xopen(filename, O_RDONLY), flags[i]))
156          perror_exit("setns");
157        close(fd);
158      }
159      nsnames += strlen(nsnames)+1;
160    }
161
162    if ((toys.optflags & FLAG_p) && !(toys.optflags & FLAG_F)) {
163      toys.exitval = xrun(toys.optargs);
164
165      return;
166    }
167  }
168
169  xexec(toys.optargs);
170}
171
172void nsenter_main(void)
173{
174  unshare_main();
175}
176