pid_namespace.c 12 KB

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