pid_namespace.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
  70. struct pid_namespace *parent_pid_ns)
  71. {
  72. struct pid_namespace *ns;
  73. unsigned int level = parent_pid_ns->level + 1;
  74. int i;
  75. int err;
  76. if (level > MAX_PID_NS_LEVEL) {
  77. err = -EINVAL;
  78. goto out;
  79. }
  80. err = -ENOMEM;
  81. ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
  82. if (ns == NULL)
  83. goto out;
  84. ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  85. if (!ns->pidmap[0].page)
  86. goto out_free;
  87. ns->pid_cachep = create_pid_cachep(level + 1);
  88. if (ns->pid_cachep == NULL)
  89. goto out_free_map;
  90. err = ns_alloc_inum(&ns->ns);
  91. if (err)
  92. goto out_free_map;
  93. ns->ns.ops = &pidns_operations;
  94. kref_init(&ns->kref);
  95. ns->level = level;
  96. ns->parent = get_pid_ns(parent_pid_ns);
  97. ns->user_ns = get_user_ns(user_ns);
  98. ns->nr_hashed = PIDNS_HASH_ADDING;
  99. INIT_WORK(&ns->proc_work, proc_cleanup_work);
  100. set_bit(0, ns->pidmap[0].page);
  101. atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
  102. for (i = 1; i < PIDMAP_ENTRIES; i++)
  103. atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
  104. return ns;
  105. out_free_map:
  106. kfree(ns->pidmap[0].page);
  107. out_free:
  108. kmem_cache_free(pid_ns_cachep, ns);
  109. out:
  110. return ERR_PTR(err);
  111. }
  112. static void delayed_free_pidns(struct rcu_head *p)
  113. {
  114. kmem_cache_free(pid_ns_cachep,
  115. container_of(p, struct pid_namespace, rcu));
  116. }
  117. static void destroy_pid_namespace(struct pid_namespace *ns)
  118. {
  119. int i;
  120. ns_free_inum(&ns->ns);
  121. for (i = 0; i < PIDMAP_ENTRIES; i++)
  122. kfree(ns->pidmap[i].page);
  123. put_user_ns(ns->user_ns);
  124. call_rcu(&ns->rcu, delayed_free_pidns);
  125. }
  126. struct pid_namespace *copy_pid_ns(unsigned long flags,
  127. struct user_namespace *user_ns, struct pid_namespace *old_ns)
  128. {
  129. if (!(flags & CLONE_NEWPID))
  130. return get_pid_ns(old_ns);
  131. if (task_active_pid_ns(current) != old_ns)
  132. return ERR_PTR(-EINVAL);
  133. return create_pid_namespace(user_ns, old_ns);
  134. }
  135. static void free_pid_ns(struct kref *kref)
  136. {
  137. struct pid_namespace *ns;
  138. ns = container_of(kref, struct pid_namespace, kref);
  139. destroy_pid_namespace(ns);
  140. }
  141. void put_pid_ns(struct pid_namespace *ns)
  142. {
  143. struct pid_namespace *parent;
  144. while (ns != &init_pid_ns) {
  145. parent = ns->parent;
  146. if (!kref_put(&ns->kref, free_pid_ns))
  147. break;
  148. ns = parent;
  149. }
  150. }
  151. EXPORT_SYMBOL_GPL(put_pid_ns);
  152. void zap_pid_ns_processes(struct pid_namespace *pid_ns)
  153. {
  154. int nr;
  155. int rc;
  156. struct task_struct *task, *me = current;
  157. int init_pids = thread_group_leader(me) ? 1 : 2;
  158. /* Don't allow any more processes into the pid namespace */
  159. disable_pid_allocation(pid_ns);
  160. /*
  161. * Ignore SIGCHLD causing any terminated children to autoreap.
  162. * This speeds up the namespace shutdown, plus see the comment
  163. * below.
  164. */
  165. spin_lock_irq(&me->sighand->siglock);
  166. me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
  167. spin_unlock_irq(&me->sighand->siglock);
  168. /*
  169. * The last thread in the cgroup-init thread group is terminating.
  170. * Find remaining pid_ts in the namespace, signal and wait for them
  171. * to exit.
  172. *
  173. * Note: This signals each threads in the namespace - even those that
  174. * belong to the same thread group, To avoid this, we would have
  175. * to walk the entire tasklist looking a processes in this
  176. * namespace, but that could be unnecessarily expensive if the
  177. * pid namespace has just a few processes. Or we need to
  178. * maintain a tasklist for each pid namespace.
  179. *
  180. */
  181. read_lock(&tasklist_lock);
  182. nr = next_pidmap(pid_ns, 1);
  183. while (nr > 0) {
  184. rcu_read_lock();
  185. task = pid_task(find_vpid(nr), PIDTYPE_PID);
  186. if (task && !__fatal_signal_pending(task))
  187. send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
  188. rcu_read_unlock();
  189. nr = next_pidmap(pid_ns, nr);
  190. }
  191. read_unlock(&tasklist_lock);
  192. /*
  193. * Reap the EXIT_ZOMBIE children we had before we ignored SIGCHLD.
  194. * sys_wait4() will also block until our children traced from the
  195. * parent namespace are detached and become EXIT_DEAD.
  196. */
  197. do {
  198. clear_thread_flag(TIF_SIGPENDING);
  199. rc = sys_wait4(-1, NULL, __WALL, NULL);
  200. } while (rc != -ECHILD);
  201. /*
  202. * sys_wait4() above can't reap the EXIT_DEAD children but we do not
  203. * really care, we could reparent them to the global init. We could
  204. * exit and reap ->child_reaper even if it is not the last thread in
  205. * this pid_ns, free_pid(nr_hashed == 0) calls proc_cleanup_work(),
  206. * pid_ns can not go away until proc_kill_sb() drops the reference.
  207. *
  208. * But this ns can also have other tasks injected by setns()+fork().
  209. * Again, ignoring the user visible semantics we do not really need
  210. * to wait until they are all reaped, but they can be reparented to
  211. * us and thus we need to ensure that pid->child_reaper stays valid
  212. * until they all go away. See free_pid()->wake_up_process().
  213. *
  214. * We rely on ignored SIGCHLD, an injected zombie must be autoreaped
  215. * if reparented.
  216. */
  217. for (;;) {
  218. set_current_state(TASK_INTERRUPTIBLE);
  219. if (pid_ns->nr_hashed == init_pids)
  220. break;
  221. schedule();
  222. }
  223. __set_current_state(TASK_RUNNING);
  224. if (pid_ns->reboot)
  225. current->signal->group_exit_code = pid_ns->reboot;
  226. acct_exit_ns(pid_ns);
  227. return;
  228. }
  229. #ifdef CONFIG_CHECKPOINT_RESTORE
  230. static int pid_ns_ctl_handler(struct ctl_table *table, int write,
  231. void __user *buffer, size_t *lenp, loff_t *ppos)
  232. {
  233. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  234. struct ctl_table tmp = *table;
  235. if (write && !ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))
  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. tmp.data = &pid_ns->last_pid;
  243. return proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  244. }
  245. extern int pid_max;
  246. static int zero = 0;
  247. static struct ctl_table pid_ns_ctl_table[] = {
  248. {
  249. .procname = "ns_last_pid",
  250. .maxlen = sizeof(int),
  251. .mode = 0666, /* permissions are checked in the handler */
  252. .proc_handler = pid_ns_ctl_handler,
  253. .extra1 = &zero,
  254. .extra2 = &pid_max,
  255. },
  256. { }
  257. };
  258. static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
  259. #endif /* CONFIG_CHECKPOINT_RESTORE */
  260. int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
  261. {
  262. if (pid_ns == &init_pid_ns)
  263. return 0;
  264. switch (cmd) {
  265. case LINUX_REBOOT_CMD_RESTART2:
  266. case LINUX_REBOOT_CMD_RESTART:
  267. pid_ns->reboot = SIGHUP;
  268. break;
  269. case LINUX_REBOOT_CMD_POWER_OFF:
  270. case LINUX_REBOOT_CMD_HALT:
  271. pid_ns->reboot = SIGINT;
  272. break;
  273. default:
  274. return -EINVAL;
  275. }
  276. read_lock(&tasklist_lock);
  277. force_sig(SIGKILL, pid_ns->child_reaper);
  278. read_unlock(&tasklist_lock);
  279. do_exit(0);
  280. /* Not reached */
  281. return 0;
  282. }
  283. static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
  284. {
  285. return container_of(ns, struct pid_namespace, ns);
  286. }
  287. static struct ns_common *pidns_get(struct task_struct *task)
  288. {
  289. struct pid_namespace *ns;
  290. rcu_read_lock();
  291. ns = task_active_pid_ns(task);
  292. if (ns)
  293. get_pid_ns(ns);
  294. rcu_read_unlock();
  295. return ns ? &ns->ns : NULL;
  296. }
  297. static void pidns_put(struct ns_common *ns)
  298. {
  299. put_pid_ns(to_pid_ns(ns));
  300. }
  301. static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  302. {
  303. struct pid_namespace *active = task_active_pid_ns(current);
  304. struct pid_namespace *ancestor, *new = to_pid_ns(ns);
  305. if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
  306. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  307. return -EPERM;
  308. /*
  309. * Only allow entering the current active pid namespace
  310. * or a child of the current active pid namespace.
  311. *
  312. * This is required for fork to return a usable pid value and
  313. * this maintains the property that processes and their
  314. * children can not escape their current pid namespace.
  315. */
  316. if (new->level < active->level)
  317. return -EINVAL;
  318. ancestor = new;
  319. while (ancestor->level > active->level)
  320. ancestor = ancestor->parent;
  321. if (ancestor != active)
  322. return -EINVAL;
  323. put_pid_ns(nsproxy->pid_ns_for_children);
  324. nsproxy->pid_ns_for_children = get_pid_ns(new);
  325. return 0;
  326. }
  327. const struct proc_ns_operations pidns_operations = {
  328. .name = "pid",
  329. .type = CLONE_NEWPID,
  330. .get = pidns_get,
  331. .put = pidns_put,
  332. .install = pidns_install,
  333. };
  334. static __init int pid_namespaces_init(void)
  335. {
  336. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
  337. #ifdef CONFIG_CHECKPOINT_RESTORE
  338. register_sysctl_paths(kern_path, pid_ns_ctl_table);
  339. #endif
  340. return 0;
  341. }
  342. __initcall(pid_namespaces_init);