pid_namespace.c 12 KB

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