pid_namespace.c 11 KB

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