pid_namespace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. 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, 0);
  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. kref_init(&ns->kref);
  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. static void free_pid_ns(struct kref *kref)
  128. {
  129. struct pid_namespace *ns;
  130. ns = container_of(kref, struct pid_namespace, kref);
  131. destroy_pid_namespace(ns);
  132. }
  133. void put_pid_ns(struct pid_namespace *ns)
  134. {
  135. struct pid_namespace *parent;
  136. while (ns != &init_pid_ns) {
  137. parent = ns->parent;
  138. if (!kref_put(&ns->kref, free_pid_ns))
  139. break;
  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. rc = kernel_wait4(-1, NULL, __WALL, NULL);
  192. } while (rc != -ECHILD);
  193. /*
  194. * kernel_wait4() misses EXIT_DEAD children, and EXIT_ZOMBIE
  195. * process whose parents processes are outside of the pid
  196. * namespace. Such processes are created with setns()+fork().
  197. *
  198. * If those EXIT_ZOMBIE processes are not reaped by their
  199. * parents before their parents exit, they will be reparented
  200. * to pid_ns->child_reaper. Thus pidns->child_reaper needs to
  201. * stay valid until they all go away.
  202. *
  203. * The code relies on the pid_ns->child_reaper ignoring
  204. * SIGCHILD to cause those EXIT_ZOMBIE processes to be
  205. * autoreaped if reparented.
  206. *
  207. * Semantically it is also desirable to wait for EXIT_ZOMBIE
  208. * processes before allowing the child_reaper to be reaped, as
  209. * that gives the invariant that when the init process of a
  210. * pid namespace is reaped all of the processes in the pid
  211. * namespace are gone.
  212. *
  213. * Once all of the other tasks are gone from the pid_namespace
  214. * free_pid() will awaken this task.
  215. */
  216. for (;;) {
  217. set_current_state(TASK_INTERRUPTIBLE);
  218. if (pid_ns->pid_allocated == init_pids)
  219. break;
  220. schedule();
  221. }
  222. __set_current_state(TASK_RUNNING);
  223. if (pid_ns->reboot)
  224. current->signal->group_exit_code = pid_ns->reboot;
  225. acct_exit_ns(pid_ns);
  226. return;
  227. }
  228. #ifdef CONFIG_CHECKPOINT_RESTORE
  229. static int pid_ns_ctl_handler(struct ctl_table *table, int write,
  230. void *buffer, size_t *lenp, loff_t *ppos)
  231. {
  232. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  233. struct ctl_table tmp = *table;
  234. int ret, next;
  235. if (write && !checkpoint_restore_ns_capable(pid_ns->user_ns))
  236. return -EPERM;
  237. /*
  238. * Writing directly to ns' last_pid field is OK, since this field
  239. * is volatile in a living namespace anyway and a code writing to
  240. * it should synchronize its usage with external means.
  241. */
  242. next = idr_get_cursor(&pid_ns->idr) - 1;
  243. tmp.data = &next;
  244. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  245. if (!ret && write)
  246. idr_set_cursor(&pid_ns->idr, next + 1);
  247. return ret;
  248. }
  249. extern int pid_max;
  250. static struct ctl_table pid_ns_ctl_table[] = {
  251. {
  252. .procname = "ns_last_pid",
  253. .maxlen = sizeof(int),
  254. .mode = 0666, /* permissions are checked in the handler */
  255. .proc_handler = pid_ns_ctl_handler,
  256. .extra1 = SYSCTL_ZERO,
  257. .extra2 = &pid_max,
  258. },
  259. { }
  260. };
  261. static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
  262. #endif /* CONFIG_CHECKPOINT_RESTORE */
  263. int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
  264. {
  265. if (pid_ns == &init_pid_ns)
  266. return 0;
  267. switch (cmd) {
  268. case LINUX_REBOOT_CMD_RESTART2:
  269. case LINUX_REBOOT_CMD_RESTART:
  270. pid_ns->reboot = SIGHUP;
  271. break;
  272. case LINUX_REBOOT_CMD_POWER_OFF:
  273. case LINUX_REBOOT_CMD_HALT:
  274. pid_ns->reboot = SIGINT;
  275. break;
  276. default:
  277. return -EINVAL;
  278. }
  279. read_lock(&tasklist_lock);
  280. send_sig(SIGKILL, pid_ns->child_reaper, 1);
  281. read_unlock(&tasklist_lock);
  282. do_exit(0);
  283. /* Not reached */
  284. return 0;
  285. }
  286. static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
  287. {
  288. return container_of(ns, struct pid_namespace, ns);
  289. }
  290. static struct ns_common *pidns_get(struct task_struct *task)
  291. {
  292. struct pid_namespace *ns;
  293. rcu_read_lock();
  294. ns = task_active_pid_ns(task);
  295. if (ns)
  296. get_pid_ns(ns);
  297. rcu_read_unlock();
  298. return ns ? &ns->ns : NULL;
  299. }
  300. static struct ns_common *pidns_for_children_get(struct task_struct *task)
  301. {
  302. struct pid_namespace *ns = NULL;
  303. task_lock(task);
  304. if (task->nsproxy) {
  305. ns = task->nsproxy->pid_ns_for_children;
  306. get_pid_ns(ns);
  307. }
  308. task_unlock(task);
  309. if (ns) {
  310. read_lock(&tasklist_lock);
  311. if (!ns->child_reaper) {
  312. put_pid_ns(ns);
  313. ns = NULL;
  314. }
  315. read_unlock(&tasklist_lock);
  316. }
  317. return ns ? &ns->ns : NULL;
  318. }
  319. static void pidns_put(struct ns_common *ns)
  320. {
  321. put_pid_ns(to_pid_ns(ns));
  322. }
  323. static int pidns_install(struct nsset *nsset, struct ns_common *ns)
  324. {
  325. struct nsproxy *nsproxy = nsset->nsproxy;
  326. struct pid_namespace *active = task_active_pid_ns(current);
  327. struct pid_namespace *ancestor, *new = to_pid_ns(ns);
  328. if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
  329. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  330. return -EPERM;
  331. /*
  332. * Only allow entering the current active pid namespace
  333. * or a child of the current active pid namespace.
  334. *
  335. * This is required for fork to return a usable pid value and
  336. * this maintains the property that processes and their
  337. * children can not escape their current pid namespace.
  338. */
  339. if (new->level < active->level)
  340. return -EINVAL;
  341. ancestor = new;
  342. while (ancestor->level > active->level)
  343. ancestor = ancestor->parent;
  344. if (ancestor != active)
  345. return -EINVAL;
  346. put_pid_ns(nsproxy->pid_ns_for_children);
  347. nsproxy->pid_ns_for_children = get_pid_ns(new);
  348. return 0;
  349. }
  350. static struct ns_common *pidns_get_parent(struct ns_common *ns)
  351. {
  352. struct pid_namespace *active = task_active_pid_ns(current);
  353. struct pid_namespace *pid_ns, *p;
  354. /* See if the parent is in the current namespace */
  355. pid_ns = p = to_pid_ns(ns)->parent;
  356. for (;;) {
  357. if (!p)
  358. return ERR_PTR(-EPERM);
  359. if (p == active)
  360. break;
  361. p = p->parent;
  362. }
  363. return &get_pid_ns(pid_ns)->ns;
  364. }
  365. static struct user_namespace *pidns_owner(struct ns_common *ns)
  366. {
  367. return to_pid_ns(ns)->user_ns;
  368. }
  369. const struct proc_ns_operations pidns_operations = {
  370. .name = "pid",
  371. .type = CLONE_NEWPID,
  372. .get = pidns_get,
  373. .put = pidns_put,
  374. .install = pidns_install,
  375. .owner = pidns_owner,
  376. .get_parent = pidns_get_parent,
  377. };
  378. const struct proc_ns_operations pidns_for_children_operations = {
  379. .name = "pid_for_children",
  380. .real_ns_name = "pid",
  381. .type = CLONE_NEWPID,
  382. .get = pidns_for_children_get,
  383. .put = pidns_put,
  384. .install = pidns_install,
  385. .owner = pidns_owner,
  386. .get_parent = pidns_get_parent,
  387. };
  388. static __init int pid_namespaces_init(void)
  389. {
  390. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
  391. #ifdef CONFIG_CHECKPOINT_RESTORE
  392. register_sysctl_paths(kern_path, pid_ns_ctl_table);
  393. #endif
  394. return 0;
  395. }
  396. __initcall(pid_namespaces_init);