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  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>
68int unshare(int flags);
69int setns(int fd, int nstype);
70
71GLOBALS(
72  char *nsnames[6];
73  long targetpid;
74)
75
76// Code that must run in unshare's flag context
77#define CLEANUP_nsenter
78#define FOR_unshare
79#include <generated/flags.h>
80
81static void write_ugid_map(char *map, unsigned eugid)
82{
83  int bytes = sprintf(toybuf, "0 %u 1", eugid), fd = xopen(map, O_WRONLY);
84
85  xwrite(fd, toybuf, bytes);
86  xclose(fd);
87}
88
89static 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
102static int test_r()
103{
104  return toys.optflags & FLAG_r;
105}
106
107static 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 CLEANUP_unshare
114#define FOR_nsenter
115#include <generated/flags.h>
116
117void unshare_main(void)
118{
119  unsigned flags[]={CLONE_NEWUSER, CLONE_NEWUTS, CLONE_NEWPID, CLONE_NEWNET,
120                    CLONE_NEWNS, CLONE_NEWIPC}, f = 0;
121  int i, fd;
122
123  // Create new namespace(s)?
124  if (CFG_UNSHARE && *toys.which->name=='u') {
125    // For -r, we have to save our original [ug]id before calling unshare()
126    int euid = geteuid(), egid = getegid();
127
128    // unshare -U does not imply -r, so we cannot use [+rU]
129    if (test_r()) toys.optflags |= FLAG_U;
130
131    for (i = 0; i<ARRAY_LEN(flags); i++)
132      if (toys.optflags & (1<<i)) f |= flags[i];
133
134    if (unshare(f)) perror_exit(0);
135    if (test_r()) handle_r(euid, egid);
136
137    if (test_f()) {
138      toys.exitval = xrun(toys.optargs);
139
140      return;
141    }
142  // Bind to existing namespace(s)?
143  } else if (CFG_NSENTER) {
144    char *nsnames = "user\0uts\0pid\0net\0mnt\0ipc";
145
146    for (i = 0; i<ARRAY_LEN(flags); i++) {
147      char *filename = TT.nsnames[i];
148
149      if (toys.optflags & (1<<i)) {
150        if (!filename || !*filename) {
151          if (!(toys.optflags & FLAG_t)) error_exit("need -t or =filename");
152          sprintf(toybuf, "/proc/%ld/ns/%s", TT.targetpid, nsnames);
153          filename = toybuf;
154        }
155
156        if (setns(fd = xopenro(filename), flags[i])) 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