pid_namespace.c 12 KB

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