blob: c48dcf885bc69a564c5f5a524f738ef2117cdf8e [file] [log] [blame]
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +02001/* vi: set sw=4 ts=4: */
2/*
3 * Mini nsenter 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 */
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +02009//config:config NSENTER
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "nsenter (6.5 kb)"
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +020011//config: default y
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +020012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Run program with namespaces of other processes.
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +020014
15//applet:IF_NSENTER(APPLET(nsenter, BB_DIR_USR_BIN, BB_SUID_DROP))
16
17//kbuild:lib-$(CONFIG_NSENTER) += nsenter.o
18
19//usage:#define nsenter_trivial_usage
20//usage: "[OPTIONS] [PROG [ARGS]]"
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +020021//usage:#define nsenter_full_usage "\n"
22//usage: "\n -t PID Target process to get namespaces from"
23//usage: "\n -m[FILE] Enter mount namespace"
24//usage: "\n -u[FILE] Enter UTS namespace (hostname etc)"
25//usage: "\n -i[FILE] Enter System V IPC namespace"
26//usage: "\n -n[FILE] Enter network namespace"
27//usage: "\n -p[FILE] Enter pid namespace"
28//usage: "\n -U[FILE] Enter user namespace"
29//usage: "\n -S UID Set uid in entered namespace"
30//usage: "\n -G GID Set gid in entered namespace"
Denys Vlasenko036585a2017-08-08 16:38:18 +020031//usage: IF_LONG_OPTS(
32//usage: "\n --preserve-credentials Don't touch uids or gids"
33//usage: )
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +020034//usage: "\n -r[DIR] Set root directory"
35//usage: "\n -w[DIR] Set working directory"
36//usage: "\n -F Don't fork before exec'ing PROG"
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +020037
38#include <sched.h>
Denys Vlasenko8b0f4592016-04-02 19:00:44 +020039#ifndef CLONE_NEWUTS
40# define CLONE_NEWUTS 0x04000000
41#endif
42#ifndef CLONE_NEWIPC
43# define CLONE_NEWIPC 0x08000000
44#endif
45#ifndef CLONE_NEWUSER
46# define CLONE_NEWUSER 0x10000000
47#endif
48#ifndef CLONE_NEWPID
49# define CLONE_NEWPID 0x20000000
50#endif
51#ifndef CLONE_NEWNET
52# define CLONE_NEWNET 0x40000000
53#endif
54
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +020055#include "libbb.h"
56
57struct namespace_descr {
58 int flag; /* value passed to setns() */
59 char ns_nsfile8[8]; /* "ns/" + namespace file in process' procfs entry */
60};
61
62struct namespace_ctx {
63 char *path; /* optional path to a custom ns file */
64 int fd; /* opened namespace file descriptor */
65};
66
67enum {
68 OPT_user = 1 << 0,
69 OPT_ipc = 1 << 1,
70 OPT_uts = 1 << 2,
71 OPT_network = 1 << 3,
72 OPT_pid = 1 << 4,
73 OPT_mount = 1 << 5,
74 OPT_target = 1 << 6,
75 OPT_setuid = 1 << 7,
76 OPT_setgid = 1 << 8,
77 OPT_root = 1 << 9,
78 OPT_wd = 1 << 10,
79 OPT_nofork = 1 << 11,
Denys Vlasenko036585a2017-08-08 16:38:18 +020080 OPT_prescred = (1 << 12) * ENABLE_LONG_OPTS,
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +020081};
82enum {
83 NS_USR_POS = 0,
84 NS_IPC_POS,
85 NS_UTS_POS,
86 NS_NET_POS,
87 NS_PID_POS,
88 NS_MNT_POS,
89 NS_COUNT,
90};
91/*
92 * The order is significant in nsenter.
93 * The user namespace comes first, so that it is entered first.
94 * This gives an unprivileged user the potential to enter other namespaces.
95 */
96static const struct namespace_descr ns_list[] = {
97 { CLONE_NEWUSER, "ns/user", },
98 { CLONE_NEWIPC, "ns/ipc", },
99 { CLONE_NEWUTS, "ns/uts", },
100 { CLONE_NEWNET, "ns/net", },
101 { CLONE_NEWPID, "ns/pid", },
102 { CLONE_NEWNS, "ns/mnt", },
103};
104/*
105 * Upstream nsenter doesn't support the short option for --preserve-credentials
106 */
Euan Harris254e4732018-05-04 16:18:47 +0100107static const char opt_str[] ALIGN1 = "U::i::u::n::p::m::""t:+S:+G:+r::w::F";
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200108
Denys Vlasenko036585a2017-08-08 16:38:18 +0200109#if ENABLE_LONG_OPTS
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200110static const char nsenter_longopts[] ALIGN1 =
111 "user\0" Optional_argument "U"
112 "ipc\0" Optional_argument "i"
113 "uts\0" Optional_argument "u"
Euan Harris40394cb2018-05-03 13:34:46 +0100114 "net\0" Optional_argument "n"
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200115 "pid\0" Optional_argument "p"
116 "mount\0" Optional_argument "m"
117 "target\0" Required_argument "t"
118 "setuid\0" Required_argument "S"
119 "setgid\0" Required_argument "G"
120 "root\0" Optional_argument "r"
121 "wd\0" Optional_argument "w"
122 "no-fork\0" No_argument "F"
123 "preserve-credentials\0" No_argument "\xff"
124 ;
125#endif
126
127/*
128 * Open a file and return the new descriptor. If a full path is provided in
129 * fs_path, then the file to which it points is opened. Otherwise (fd_path is
130 * NULL) the routine builds a path to a procfs file using the following
131 * template: '/proc/<target_pid>/<target_file>'.
132 */
133static int open_by_path_or_target(const char *path,
134 pid_t target_pid, const char *target_file)
135{
136 char proc_path_buf[sizeof("/proc/%u/1234567890") + sizeof(int)*3];
137
138 if (!path) {
139 if (target_pid == 0) {
140 /* Example:
141 * "nsenter -p PROG" - neither -pFILE nor -tPID given.
142 */
143 bb_show_usage();
144 }
145 snprintf(proc_path_buf, sizeof(proc_path_buf),
146 "/proc/%u/%s", (unsigned)target_pid, target_file);
147 path = proc_path_buf;
148 }
149
150 return xopen(path, O_RDONLY);
151}
152
153int nsenter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
154int nsenter_main(int argc UNUSED_PARAM, char **argv)
155{
156 int i;
157 unsigned int opts;
158 const char *root_dir_str = NULL;
159 const char *wd_str = NULL;
160 struct namespace_ctx ns_ctx_list[NS_COUNT];
161 int setgroups_failed;
162 int root_fd, wd_fd;
163 int target_pid = 0;
164 int uid = 0;
165 int gid = 0;
166
167 memset(ns_ctx_list, 0, sizeof(ns_ctx_list));
168
Denys Vlasenko036585a2017-08-08 16:38:18 +0200169 opts = getopt32long(argv, opt_str, nsenter_longopts,
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200170 &ns_ctx_list[NS_USR_POS].path,
171 &ns_ctx_list[NS_IPC_POS].path,
172 &ns_ctx_list[NS_UTS_POS].path,
173 &ns_ctx_list[NS_NET_POS].path,
174 &ns_ctx_list[NS_PID_POS].path,
175 &ns_ctx_list[NS_MNT_POS].path,
176 &target_pid, &uid, &gid,
177 &root_dir_str, &wd_str
178 );
179 argv += optind;
180
181 root_fd = wd_fd = -1;
182 if (opts & OPT_root)
183 root_fd = open_by_path_or_target(root_dir_str,
184 target_pid, "root");
185 if (opts & OPT_wd)
186 wd_fd = open_by_path_or_target(wd_str, target_pid, "cwd");
187
188 for (i = 0; i < NS_COUNT; i++) {
189 const struct namespace_descr *ns = &ns_list[i];
190 struct namespace_ctx *ns_ctx = &ns_ctx_list[i];
191
192 ns_ctx->fd = -1;
193 if (opts & (1 << i))
194 ns_ctx->fd = open_by_path_or_target(ns_ctx->path,
195 target_pid, ns->ns_nsfile8);
196 }
197
198 /*
199 * Entering the user namespace without --preserve-credentials implies
200 * --setuid & --setgid and clearing root's groups.
201 */
202 setgroups_failed = 0;
203 if ((opts & OPT_user) && !(opts & OPT_prescred)) {
204 opts |= (OPT_setuid | OPT_setgid);
205 /*
206 * We call setgroups() before and after setns() and only
207 * bail-out if it fails twice.
208 */
209 setgroups_failed = (setgroups(0, NULL) < 0);
210 }
211
212 for (i = 0; i < NS_COUNT; i++) {
213 const struct namespace_descr *ns = &ns_list[i];
214 struct namespace_ctx *ns_ctx = &ns_ctx_list[i];
215
216 if (ns_ctx->fd < 0)
217 continue;
218 if (setns(ns_ctx->fd, ns->flag)) {
219 bb_perror_msg_and_die(
220 "setns(): can't reassociate to namespace '%s'",
221 ns->ns_nsfile8 + 3 /* skip over "ns/" */
222 );
223 }
Denys Vlasenko82203992016-04-02 18:06:24 +0200224 close(ns_ctx->fd); /* should close fds, to not confuse exec'ed PROG */
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200225 /*ns_ctx->fd = -1;*/
226 }
227
228 if (root_fd >= 0) {
229 if (wd_fd < 0) {
230 /*
231 * Save the current working directory if we're not
232 * changing it.
233 */
234 wd_fd = xopen(".", O_RDONLY);
235 }
236 xfchdir(root_fd);
237 xchroot(".");
Denys Vlasenko82203992016-04-02 18:06:24 +0200238 close(root_fd);
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200239 /*root_fd = -1;*/
240 }
241
242 if (wd_fd >= 0) {
243 xfchdir(wd_fd);
Denys Vlasenko82203992016-04-02 18:06:24 +0200244 close(wd_fd);
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200245 /*wd_fd = -1;*/
246 }
247
248 /*
249 * Entering the pid namespace implies forking unless it's been
250 * explicitly requested by the user not to.
251 */
252 if (!(opts & OPT_nofork) && (opts & OPT_pid)) {
Denys Vlasenko82203992016-04-02 18:06:24 +0200253 xvfork_parent_waits_and_exits();
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200254 /* Child continues */
255 }
256
257 if (opts & OPT_setgid) {
258 if (setgroups(0, NULL) < 0 && setgroups_failed)
James Byrne69374872019-07-02 11:35:03 +0200259 bb_simple_perror_msg_and_die("setgroups");
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200260 xsetgid(gid);
261 }
262 if (opts & OPT_setuid)
263 xsetuid(uid);
264
Denys Vlasenko82203992016-04-02 18:06:24 +0200265 exec_prog_or_SHELL(argv);
Bartosz Golaszewski80c934a2016-04-01 22:17:25 +0200266}