pid_namespace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Pid namespaces
  4. *
  5. * Authors:
  6. * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
  7. * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
  8. * Many thanks to Oleg Nesterov for comments and help
  9. *
  10. */
  11. #include <linux/pid.h>
  12. #include <linux/pid_namespace.h>
  13. #include <linux/user_namespace.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/cred.h>
  16. #include <linux/err.h>
  17. #include <linux/acct.h>
  18. #include <linux/slab.h>
  19. #include <linux/proc_ns.h>
  20. #include <linux/reboot.h>
  21. #include <linux/export.h>
  22. #include <linux/sched/task.h>
  23. #include <linux/sched/signal.h>
  24. #include <linux/idr.h>
  25. #include <uapi/linux/wait.h>
  26. #include "pid_sysctl.h"
  27. static DEFINE_MUTEX(pid_caches_mutex);
  28. static struct kmem_cache *pid_ns_cachep;
  29. /* Write once array, filled from the beginning. */
  30. static struct kmem_cache *pid_cache[MAX_PID_NS_LEVEL];
  31. /*
  32. * creates the kmem cache to allocate pids from.
  33. * @level: pid namespace level
  34. */
  35. static struct kmem_cache *create_pid_cachep(unsigned int level)
  36. {
  37. /* Level 0 is init_pid_ns.pid_cachep */
  38. struct kmem_cache **pkc = &pid_cache[level - 1];
  39. struct kmem_cache *kc;
  40. char name[4 + 10 + 1];
  41. unsigned int len;
  42. kc = READ_ONCE(*pkc);
  43. if (kc)
  44. return kc;
  45. snprintf(name, sizeof(name), "pid_%u", level + 1);
  46. len = struct_size_t(struct pid, numbers, level + 1);
  47. mutex_lock(&pid_caches_mutex);
  48. /* Name collision forces to do allocation under mutex. */
  49. if (!*pkc)
  50. *pkc = kmem_cache_create(name, len, 0,
  51. SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
  52. mutex_unlock(&pid_caches_mutex);
  53. /* current can fail, but someone else can succeed. */
  54. return READ_ONCE(*pkc);
  55. }
  56. static struct ucounts *inc_pid_namespaces(struct user_namespace *ns)
  57. {
  58. return inc_ucount(ns, current_euid(), UCOUNT_PID_NAMESPACES);
  59. }
  60. static void dec_pid_namespaces(struct ucounts *ucounts)
  61. {
  62. dec_ucount(ucounts, UCOUNT_PID_NAMESPACES);
  63. }
  64. static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
  65. struct pid_namespace *parent_pid_ns)
  66. {
  67. struct pid_namespace *ns;
  68. unsigned int level = parent_pid_ns->level + 1;
  69. struct ucounts *ucounts;
  70. int err;
  71. err = -EINVAL;
  72. if (!in_userns(parent_pid_ns->user_ns, user_ns))
  73. goto out;
  74. err = -ENOSPC;
  75. if (level > MAX_PID_NS_LEVEL)
  76. goto out;
  77. ucounts = inc_pid_namespaces(user_ns);
  78. if (!ucounts)
  79. goto out;
  80. err = -ENOMEM;
  81. ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
  82. if (ns == NULL)
  83. goto out_dec;
  84. idr_init(&ns->idr);
  85. ns->pid_cachep = create_pid_cachep(level);
  86. if (ns->pid_cachep == NULL)
  87. goto out_free_idr;
  88. err = ns_alloc_inum(&ns->ns);
  89. if (err)
  90. goto out_free_idr;
  91. ns->ns.ops = &pidns_operations;
  92. refcount_set(&ns->ns.count, 1);
  93. ns->level = level;
  94. ns->parent = get_pid_ns(parent_pid_ns);
  95. ns->user_ns = get_user_ns(user_ns);
  96. ns->ucounts = ucounts;
  97. ns->pid_allocated = PIDNS_ADDING;
  98. #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
  99. ns->memfd_noexec_scope = pidns_memfd_noexec_scope(parent_pid_ns);
  100. #endif
  101. return ns;
  102. out_free_idr:
  103. idr_destroy(&ns->idr);
  104. kmem_cache_free(pid_ns_cachep, ns);
  105. out_dec:
  106. dec_pid_namespaces(ucounts);
  107. out:
  108. return ERR_PTR(err);
  109. }
  110. static void delayed_free_pidns(struct rcu_head *p)
  111. {
  112. struct pid_namespace *ns = container_of(p, struct pid_namespace, rcu);
  113. dec_pid_namespaces(ns->ucounts);
  114. put_user_ns(ns->user_ns);
  115. kmem_cache_free(pid_ns_cachep, ns);
  116. }
  117. static void destroy_pid_namespace(struct pid_namespace *ns)
  118. {
  119. ns_free_inum(&ns->ns);
  120. idr_destroy(&ns->idr);
  121. call_rcu(&ns->rcu, delayed_free_pidns);
  122. }
  123. struct pid_namespace *copy_pid_ns(unsigned long flags,
  124. struct user_namespace *user_ns, struct pid_namespace *old_ns)
  125. {
  126. if (!(flags & CLONE_NEWPID))
  127. return get_pid_ns(old_ns);
  128. if (task_active_pid_ns(current) != old_ns)
  129. return ERR_PTR(-EINVAL);
  130. return create_pid_namespace(user_ns, old_ns);
  131. }
  132. void put_pid_ns(struct pid_namespace *ns)
  133. {
  134. struct pid_namespace *parent;
  135. while (ns != &init_pid_ns) {
  136. parent = ns->parent;
  137. if (!refcount_dec_and_test(&ns->ns.count))
  138. break;
  139. destroy_pid_namespace(ns);
  140. ns = parent;
  141. }
  142. }
  143. EXPORT_SYMBOL_GPL(put_pid_ns);
  144. void zap_pid_ns_processes(struct pid_namespace *pid_ns)
  145. {
  146. int nr;
  147. int rc;
  148. struct task_struct *task, *me = current;
  149. int init_pids = thread_group_leader(me) ? 1 : 2;
  150. struct pid *pid;
  151. /* Don't allow any more processes into the pid namespace */
  152. disable_pid_allocation(pid_ns);
  153. /*
  154. * Ignore SIGCHLD causing any terminated children to autoreap.
  155. * This speeds up the namespace shutdown, plus see the comment
  156. * below.
  157. */
  158. spin_lock_irq(&me->sighand->siglock);
  159. me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
  160. spin_unlock_irq(&me->sighand->siglock);
  161. /*
  162. * The last thread in the cgroup-init thread group is terminating.
  163. * Find remaining pid_ts in the namespace, signal and wait for them
  164. * to exit.
  165. *
  166. * Note: This signals each threads in the namespace - even those that
  167. * belong to the same thread group, To avoid this, we would have
  168. * to walk the entire tasklist looking a processes in this
  169. * namespace, but that could be unnecessarily expensive if the
  170. * pid namespace has just a few processes. Or we need to
  171. * maintain a tasklist for each pid namespace.
  172. *
  173. */
  174. rcu_read_lock();
  175. read_lock(&tasklist_lock);
  176. nr = 2;
  177. idr_for_each_entry_continue(&pid_ns->idr, pid, nr) {
  178. task = pid_task(pid, PIDTYPE_PID);
  179. if (task && !__fatal_signal_pending(task))
  180. group_send_sig_info(SIGKILL, SEND_SIG_PRIV, task, PIDTYPE_MAX);
  181. }
  182. read_unlock(&tasklist_lock);
  183. rcu_read_unlock();
  184. /*
  185. * Reap the EXIT_ZOMBIE children we had before we ignored SIGCHLD.
  186. * kernel_wait4() will also block until our children traced from the
  187. * parent namespace are detached and become EXIT_DEAD.
  188. */
  189. do {
  190. clear_thread_flag(TIF_SIGPENDING);
  191. clear_thread_flag(TIF_NOTIFY_SIGNAL);
  192. rc = kernel_wait4(-1, NULL, __WALL, NULL);
  193. } while (rc != -ECHILD);
  194. /*
  195. * kernel_wait4() misses EXIT_DEAD children, and EXIT_ZOMBIE
  196. * process whose parents processes are outside of the pid
  197. * namespace. Such processes are created with setns()+fork().
  198. *
  199. * If those EXIT_ZOMBIE processes are not reaped by their
  200. * parents before their parents exit, they will be reparented
  201. * to pid_ns->child_reaper. Thus pidns->child_reaper needs to
  202. * stay valid until they all go away.
  203. *
  204. * The code relies on the pid_ns->child_reaper ignoring
  205. * SIGCHILD to cause those EXIT_ZOMBIE processes to be
  206. * autoreaped if reparented.
  207. *
  208. * Semantically it is also desirable to wait for EXIT_ZOMBIE
  209. * processes before allowing the child_reaper to be reaped, as
  210. * that gives the invariant that when the init process of a
  211. * pid namespace is reaped all of the processes in the pid
  212. * namespace are gone.
  213. *
  214. * Once all of the other tasks are gone from the pid_namespace
  215. * free_pid() will awaken this task.
  216. */
  217. for (;;) {
  218. set_current_state(TASK_INTERRUPTIBLE);
  219. if (pid_ns->pid_allocated == init_pids)
  220. break;
  221. schedule();
  222. }
  223. __set_current_state(TASK_RUNNING);
  224. if (pid_ns->reboot)
  225. current->signal->group_exit_code = pid_ns->reboot;
  226. acct_exit_ns(pid_ns);
  227. return;
  228. }
  229. #ifdef CONFIG_CHECKPOINT_RESTORE
  230. static int pid_ns_ctl_handler(const struct ctl_table *table, int write,
  231. void *buffer, size_t *lenp, loff_t *ppos)
  232. {
  233. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  234. struct ctl_table tmp = *table;
  235. int ret, next;
  236. if (write && !checkpoint_restore_ns_capable(pid_ns->user_ns))
  237. return -EPERM;
  238. next = idr_get_cursor(&pid_ns->idr) - 1;
  239. tmp.data = &next;
  240. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  241. if (!ret && write)
  242. idr_set_cursor(&pid_ns->idr, next + 1);
  243. return ret;
  244. }
  245. extern int pid_max;
  246. static struct ctl_table pid_ns_ctl_table[] = {
  247. {
  248. .procname = "ns_last_pid",
  249. .maxlen = sizeof(int),
  250. .mode = 0666, /* permissions are checked in the handler */
  251. .proc_handler = pid_ns_ctl_handler,
  252. .extra1 = SYSCTL_ZERO,
  253. .extra2 = &pid_max,
  254. },
  255. };
  256. #endif /* CONFIG_CHECKPOINT_RESTORE */
  257. int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
  258. {
  259. if (pid_ns == &init_pid_ns)
  260. return 0;
  261. switch (cmd) {
  262. case LINUX_REBOOT_CMD_RESTART2:
  263. case LINUX_REBOOT_CMD_RESTART:
  264. pid_ns->reboot = SIGHUP;
  265. break;
  266. case LINUX_REBOOT_CMD_POWER_OFF:
  267. case LINUX_REBOOT_CMD_HALT:
  268. pid_ns->reboot = SIGINT;
  269. break;
  270. default:
  271. return -EINVAL;
  272. }
  273. read_lock(&tasklist_lock);
  274. send_sig(SIGKILL, pid_ns->child_reaper, 1);
  275. read_unlock(&tasklist_lock);
  276. do_exit(0);
  277. /* Not reached */
  278. return 0;
  279. }
  280. static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
  281. {
  282. return container_of(ns, struct pid_namespace, ns);
  283. }
  284. static struct ns_common *pidns_get(struct task_struct *task)
  285. {
  286. struct pid_namespace *ns;
  287. rcu_read_lock();
  288. ns = task_active_pid_ns(task);
  289. if (ns)
  290. get_pid_ns(ns);
  291. rcu_read_unlock();
  292. return ns ? &ns->ns : NULL;
  293. }
  294. static struct ns_common *pidns_for_children_get(struct task_struct *task)
  295. {
  296. struct pid_namespace *ns = NULL;
  297. task_lock(task);
  298. if (task->nsproxy) {
  299. ns = task->nsproxy->pid_ns_for_children;
  300. get_pid_ns(ns);
  301. }
  302. task_unlock(task);
  303. if (ns) {
  304. read_lock(&tasklist_lock);
  305. if (!ns->child_reaper) {
  306. put_pid_ns(ns);
  307. ns = NULL;
  308. }
  309. read_unlock(&tasklist_lock);
  310. }
  311. return ns ? &ns->ns : NULL;
  312. }
  313. static void pidns_put(struct ns_common *ns)
  314. {
  315. put_pid_ns(to_pid_ns(ns));
  316. }
  317. static int pidns_install(struct nsset *nsset, struct ns_common *ns)
  318. {
  319. struct nsproxy *nsproxy = nsset->nsproxy;
  320. struct pid_namespace *active = task_active_pid_ns(current);
  321. struct pid_namespace *ancestor, *new = to_pid_ns(ns);
  322. if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
  323. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  324. return -EPERM;
  325. /*
  326. * Only allow entering the current active pid namespace
  327. * or a child of the current active pid namespace.
  328. *
  329. * This is required for fork to return a usable pid value and
  330. * this maintains the property that processes and their
  331. * children can not escape their current pid namespace.
  332. */
  333. if (new->level < active->level)
  334. return -EINVAL;
  335. ancestor = new;
  336. while (ancestor->level > active->level)
  337. ancestor = ancestor->parent;
  338. if (ancestor != active)
  339. return -EINVAL;
  340. put_pid_ns(nsproxy->pid_ns_for_children);
  341. nsproxy->pid_ns_for_children = get_pid_ns(new);
  342. return 0;
  343. }
  344. static struct ns_common *pidns_get_parent(struct ns_common *ns)
  345. {
  346. struct pid_namespace *active = task_active_pid_ns(current);
  347. struct pid_namespace *pid_ns, *p;
  348. /* See if the parent is in the current namespace */
  349. pid_ns = p = to_pid_ns(ns)->parent;
  350. for (;;) {
  351. if (!p)
  352. return ERR_PTR(-EPERM);
  353. if (p == active)
  354. break;
  355. p = p->parent;
  356. }
  357. return &get_pid_ns(pid_ns)->ns;
  358. }
  359. static struct user_namespace *pidns_owner(struct ns_common *ns)
  360. {
  361. return to_pid_ns(ns)->user_ns;
  362. }
  363. const struct proc_ns_operations pidns_operations = {
  364. .name = "pid",
  365. .type = CLONE_NEWPID,
  366. .get = pidns_get,
  367. .put = pidns_put,
  368. .install = pidns_install,
  369. .owner = pidns_owner,
  370. .get_parent = pidns_get_parent,
  371. };
  372. const struct proc_ns_operations pidns_for_children_operations = {
  373. .name = "pid_for_children",
  374. .real_ns_name = "pid",
  375. .type = CLONE_NEWPID,
  376. .get = pidns_for_children_get,
  377. .put = pidns_put,
  378. .install = pidns_install,
  379. .owner = pidns_owner,
  380. .get_parent = pidns_get_parent,
  381. };
  382. static __init int pid_namespaces_init(void)
  383. {
  384. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
  385. #ifdef CONFIG_CHECKPOINT_RESTORE
  386. register_sysctl_init("kernel", pid_ns_ctl_table);
  387. #endif
  388. register_pid_ns_sysctl_table_vm();
  389. return 0;
  390. }
  391. __initcall(pid_namespaces_init);