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