chrt: add support for SCHED_BATCH
[oweals/busybox.git] / util-linux / unshare.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini unshare implementation for busybox.
4  *
5  * Copyright (C) 2016 by Bartosz Golaszewski <bartekgola@gmail.com>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 //config:config UNSHARE
10 //config:       bool "unshare (9.2 kb)"
11 //config:       default y
12 //config:       depends on !NOMMU
13 //config:       select PLATFORM_LINUX
14 //config:       select LONG_OPTS
15 //config:       help
16 //config:       Run program with some namespaces unshared from parent.
17
18 // needs LONG_OPTS: it is awkward to exclude code which handles --propagation
19 // and --setgroups based on LONG_OPTS, so instead applet requires LONG_OPTS.
20 // depends on !NOMMU: we need fork()
21
22 //applet:IF_UNSHARE(APPLET(unshare, BB_DIR_USR_BIN, BB_SUID_DROP))
23
24 //kbuild:lib-$(CONFIG_UNSHARE) += unshare.o
25
26 //usage:#define unshare_trivial_usage
27 //usage:       "[OPTIONS] [PROG [ARGS]]"
28 //usage:#define unshare_full_usage "\n"
29 //usage:     "\n        -m,--mount[=FILE]       Unshare mount namespace"
30 //usage:     "\n        -u,--uts[=FILE]         Unshare UTS namespace (hostname etc.)"
31 //usage:     "\n        -i,--ipc[=FILE]         Unshare System V IPC namespace"
32 //usage:     "\n        -n,--net[=FILE]         Unshare network namespace"
33 //usage:     "\n        -p,--pid[=FILE]         Unshare PID namespace"
34 //usage:     "\n        -U,--user[=FILE]        Unshare user namespace"
35 //usage:     "\n        -f,--fork               Fork before execing PROG"
36 //usage:     "\n        -r,--map-root-user      Map current user to root (implies -U)"
37 //usage:     "\n        --mount-proc[=DIR]      Mount /proc filesystem first (implies -m)"
38 //usage:     "\n        --propagation slave|shared|private|unchanged"
39 //usage:     "\n                                Modify mount propagation in mount namespace"
40 //usage:     "\n        --setgroups allow|deny  Control the setgroups syscall in user namespaces"
41
42 #include <sched.h>
43 #ifndef CLONE_NEWUTS
44 # define CLONE_NEWUTS  0x04000000
45 #endif
46 #ifndef CLONE_NEWIPC
47 # define CLONE_NEWIPC  0x08000000
48 #endif
49 #ifndef CLONE_NEWUSER
50 # define CLONE_NEWUSER 0x10000000
51 #endif
52 #ifndef CLONE_NEWPID
53 # define CLONE_NEWPID  0x20000000
54 #endif
55 #ifndef CLONE_NEWNET
56 # define CLONE_NEWNET  0x40000000
57 #endif
58
59 #include <sys/mount.h>
60 #ifndef MS_REC
61 # define MS_REC     (1 << 14)
62 #endif
63 #ifndef MS_PRIVATE
64 # define MS_PRIVATE (1 << 18)
65 #endif
66 #ifndef MS_SLAVE
67 # define MS_SLAVE   (1 << 19)
68 #endif
69 #ifndef MS_SHARED
70 # define MS_SHARED  (1 << 20)
71 #endif
72
73 #include "libbb.h"
74
75 static void mount_or_die(const char *source, const char *target,
76                  const char *fstype, unsigned long mountflags)
77 {
78         if (mount(source, target, fstype, mountflags, NULL)) {
79                 bb_perror_msg_and_die("can't mount %s on %s (flags:0x%lx)",
80                         source, target, mountflags);
81                 /* fstype is always either NULL or "proc".
82                  * "proc" is only used to mount /proc.
83                  * No need to clutter up error message with fstype,
84                  * it is easily deductible.
85                  */
86         }
87 }
88
89 #define PATH_PROC_SETGROUPS     "/proc/self/setgroups"
90 #define PATH_PROC_UIDMAP        "/proc/self/uid_map"
91 #define PATH_PROC_GIDMAP        "/proc/self/gid_map"
92
93 struct namespace_descr {
94         int flag;
95         const char nsfile4[4];
96 };
97
98 struct namespace_ctx {
99         char *path;
100 };
101
102 enum {
103         OPT_mount       = 1 << 0,
104         OPT_uts         = 1 << 1,
105         OPT_ipc         = 1 << 2,
106         OPT_net         = 1 << 3,
107         OPT_pid         = 1 << 4,
108         OPT_user        = 1 << 5, /* OPT_user, NS_USR_POS, and ns_list[] index must match! */
109         OPT_fork        = 1 << 6,
110         OPT_map_root    = 1 << 7,
111         OPT_mount_proc  = 1 << 8,
112         OPT_propagation = 1 << 9,
113         OPT_setgroups   = 1 << 10,
114 };
115 enum {
116         NS_MNT_POS = 0,
117         NS_UTS_POS,
118         NS_IPC_POS,
119         NS_NET_POS,
120         NS_PID_POS,
121         NS_USR_POS, /* OPT_user, NS_USR_POS, and ns_list[] index must match! */
122         NS_COUNT,
123 };
124 static const struct namespace_descr ns_list[] = {
125         { CLONE_NEWNS,   "mnt"  },
126         { CLONE_NEWUTS,  "uts"  },
127         { CLONE_NEWIPC,  "ipc"  },
128         { CLONE_NEWNET,  "net"  },
129         { CLONE_NEWPID,  "pid"  },
130         { CLONE_NEWUSER, "user" }, /* OPT_user, NS_USR_POS, and ns_list[] index must match! */
131 };
132
133 /*
134  * Upstream unshare doesn't support short options for --mount-proc,
135  * --propagation, --setgroups.
136  * Optional arguments (namespace mountpoints) exist only for long opts,
137  * we are forced to use "fake" letters for them.
138  * '+': stop at first non-option.
139  */
140 #define OPT_STR "+muinpU""fr""\xfd::""\xfe:""\xff:"
141 static const char unshare_longopts[] ALIGN1 =
142         "mount\0"               Optional_argument       "\xf0"
143         "uts\0"                 Optional_argument       "\xf1"
144         "ipc\0"                 Optional_argument       "\xf2"
145         "net\0"                 Optional_argument       "\xf3"
146         "pid\0"                 Optional_argument       "\xf4"
147         "user\0"                Optional_argument       "\xf5"
148         "fork\0"                No_argument             "f"
149         "map-root-user\0"       No_argument             "r"
150         "mount-proc\0"          Optional_argument       "\xfd"
151         "propagation\0"         Required_argument       "\xfe"
152         "setgroups\0"           Required_argument       "\xff"
153 ;
154
155 /* Ugly-looking string reuse trick */
156 #define PRIVATE_STR   "private\0""unchanged\0""shared\0""slave\0"
157 #define PRIVATE_UNCHANGED_SHARED_SLAVE   PRIVATE_STR
158
159 static unsigned long parse_propagation(const char *prop_str)
160 {
161         int i = index_in_strings(PRIVATE_UNCHANGED_SHARED_SLAVE, prop_str);
162         if (i < 0)
163                 bb_error_msg_and_die("unrecognized: --%s=%s", "propagation", prop_str);
164         if (i == 0)
165                 return MS_REC | MS_PRIVATE;
166         if (i == 1)
167                 return 0;
168         if (i == 2)
169                 return MS_REC | MS_SHARED;
170         return MS_REC | MS_SLAVE;
171 }
172
173 static void mount_namespaces(pid_t pid, struct namespace_ctx *ns_ctx_list)
174 {
175         const struct namespace_descr *ns;
176         struct namespace_ctx *ns_ctx;
177         int i;
178
179         for (i = 0; i < NS_COUNT; i++) {
180                 char nsf[sizeof("/proc/%u/ns/AAAA") + sizeof(int)*3];
181
182                 ns = &ns_list[i];
183                 ns_ctx = &ns_ctx_list[i];
184                 if (!ns_ctx->path)
185                         continue;
186                 sprintf(nsf, "/proc/%u/ns/%.4s", (unsigned)pid, ns->nsfile4);
187                 mount_or_die(nsf, ns_ctx->path, NULL, MS_BIND);
188         }
189 }
190
191 int unshare_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
192 int unshare_main(int argc UNUSED_PARAM, char **argv)
193 {
194         int i;
195         unsigned int opts;
196         int unsflags;
197         uintptr_t need_mount;
198         const char *proc_mnt_target;
199         const char *prop_str;
200         const char *setgrp_str;
201         unsigned long prop_flags;
202         uid_t reuid = geteuid();
203         gid_t regid = getegid();
204         struct fd_pair fdp;
205         pid_t child = child; /* for compiler */
206         struct namespace_ctx ns_ctx_list[NS_COUNT];
207
208         memset(ns_ctx_list, 0, sizeof(ns_ctx_list));
209         proc_mnt_target = "/proc";
210         prop_str = PRIVATE_STR;
211         setgrp_str = NULL;
212
213         opts = getopt32long(argv, "^" OPT_STR "\0"
214                 "\xf0""m" /* long opts (via their "fake chars") imply short opts */
215                 ":\xf1""u"
216                 ":\xf2""i"
217                 ":\xf3""n"
218                 ":\xf4""p"
219                 ":\xf5""U"
220                 ":rU"      /* --map-root-user or -r implies -U */
221                 ":\xfd""m" /* --mount-proc implies -m */
222                 , unshare_longopts,
223                 &proc_mnt_target, &prop_str, &setgrp_str,
224                 &ns_ctx_list[NS_MNT_POS].path,
225                 &ns_ctx_list[NS_UTS_POS].path,
226                 &ns_ctx_list[NS_IPC_POS].path,
227                 &ns_ctx_list[NS_NET_POS].path,
228                 &ns_ctx_list[NS_PID_POS].path,
229                 &ns_ctx_list[NS_USR_POS].path
230         );
231         argv += optind;
232         //bb_error_msg("opts:0x%x", opts);
233         //bb_error_msg("mount:%s", ns_ctx_list[NS_MNT_POS].path);
234         //bb_error_msg("proc_mnt_target:%s", proc_mnt_target);
235         //bb_error_msg("prop_str:%s", prop_str);
236         //bb_error_msg("setgrp_str:%s", setgrp_str);
237         //exit(1);
238
239         if (setgrp_str) {
240                 if (strcmp(setgrp_str, "allow") == 0) {
241                         if (opts & OPT_map_root) {
242                                 bb_error_msg_and_die(
243                                         "--setgroups=allow and --map-root-user "
244                                         "are mutually exclusive"
245                                 );
246                         }
247                 } else {
248                         /* It's not "allow", must be "deny" */
249                         if (strcmp(setgrp_str, "deny") != 0)
250                                 bb_error_msg_and_die("unrecognized: --%s=%s",
251                                         "setgroups", setgrp_str);
252                 }
253         }
254
255         unsflags = 0;
256         need_mount = 0;
257         for (i = 0; i < NS_COUNT; i++) {
258                 const struct namespace_descr *ns = &ns_list[i];
259                 struct namespace_ctx *ns_ctx = &ns_ctx_list[i];
260
261                 if (opts & (1 << i))
262                         unsflags |= ns->flag;
263
264                 need_mount |= (uintptr_t)(ns_ctx->path);
265         }
266         /* need_mount != 0 if at least one FILE was given */
267
268         prop_flags = MS_REC | MS_PRIVATE;
269         /* Silently ignore --propagation if --mount is not requested. */
270         if (opts & OPT_mount)
271                 prop_flags = parse_propagation(prop_str);
272
273         /*
274          * Special case: if we were requested to unshare the mount namespace
275          * AND to make any namespace persistent (by bind mounting it) we need
276          * to spawn a child process which will wait for the parent to call
277          * unshare(), then mount parent's namespaces while still in the
278          * previous namespace.
279          */
280         fdp.wr = -1;
281         if (need_mount && (opts & OPT_mount)) {
282                 /*
283                  * Can't use getppid() in child, as we can be unsharing the
284                  * pid namespace.
285                  */
286                 pid_t ppid = getpid();
287
288                 xpiped_pair(fdp);
289
290                 child = xfork();
291                 if (child == 0) {
292                         /* Child */
293                         close(fdp.wr);
294
295                         /* Wait until parent calls unshare() */
296                         read(fdp.rd, ns_ctx_list, 1); /* ...using bogus buffer */
297                         /*close(fdp.rd);*/
298
299                         /* Mount parent's unshared namespaces. */
300                         mount_namespaces(ppid, ns_ctx_list);
301                         return EXIT_SUCCESS;
302                 }
303                 /* Parent continues */
304         }
305
306         if (unshare(unsflags) != 0)
307                 bb_perror_msg_and_die("unshare(0x%x)", unsflags);
308
309         if (fdp.wr >= 0) {
310                 close(fdp.wr); /* Release child */
311                 close(fdp.rd); /* should close fd, to not confuse exec'ed PROG */
312         }
313
314         if (need_mount) {
315                 /* Wait for the child to finish mounting the namespaces. */
316                 if (opts & OPT_mount) {
317                         int exit_status = wait_for_exitstatus(child);
318                         if (WIFEXITED(exit_status) &&
319                             WEXITSTATUS(exit_status) != EXIT_SUCCESS)
320                                 return WEXITSTATUS(exit_status);
321                 } else {
322                         /*
323                          * Regular way - we were requested to mount some other
324                          * namespaces: mount them after the call to unshare().
325                          */
326                         mount_namespaces(getpid(), ns_ctx_list);
327                 }
328         }
329
330         /*
331          * When we're unsharing the pid namespace, it's not the process that
332          * calls unshare() that is put into the new namespace, but its first
333          * child. The user may want to use this option to spawn a new process
334          * that'll become PID 1 in this new namespace.
335          */
336         if (opts & OPT_fork) {
337                 xvfork_parent_waits_and_exits();
338                 /* Child continues */
339         }
340
341         if (opts & OPT_map_root) {
342                 char uidmap_buf[sizeof("0 %u 1") + sizeof(int)*3];
343
344                 /*
345                  * Since Linux 3.19 unprivileged writing of /proc/self/gid_map
346                  * has been disabled unless /proc/self/setgroups is written
347                  * first to permanently disable the ability to call setgroups
348                  * in that user namespace.
349                  */
350                 xopen_xwrite_close(PATH_PROC_SETGROUPS, "deny");
351                 sprintf(uidmap_buf, "0 %u 1", (unsigned)reuid);
352                 xopen_xwrite_close(PATH_PROC_UIDMAP, uidmap_buf);
353                 sprintf(uidmap_buf, "0 %u 1", (unsigned)regid);
354                 xopen_xwrite_close(PATH_PROC_GIDMAP, uidmap_buf);
355         } else
356         if (setgrp_str) {
357                 /* Write "allow" or "deny" */
358                 xopen_xwrite_close(PATH_PROC_SETGROUPS, setgrp_str);
359         }
360
361         if (opts & OPT_mount) {
362                 mount_or_die("none", "/", NULL, prop_flags);
363         }
364
365         if (opts & OPT_mount_proc) {
366                 /*
367                  * When creating a new pid namespace, we might want the pid
368                  * subdirectories in /proc to remain consistent with the new
369                  * process IDs. Without --mount-proc the pids in /proc would
370                  * still reflect the old pid namespace. This is why we make
371                  * /proc private here and then do a fresh mount.
372                  */
373                 mount_or_die("none", proc_mnt_target, NULL, MS_PRIVATE | MS_REC);
374                 mount_or_die("proc", proc_mnt_target, "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV);
375         }
376
377         exec_prog_or_SHELL(argv);
378 }