3
0

nsenter.c 6.9 KB

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