nsenter.c 8.1 KB

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