pid_namespace.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_fs.h>
  18. #include <linux/reboot.h>
  19. #include <linux/export.h>
  20. #define BITS_PER_PAGE (PAGE_SIZE*8)
  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 pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
  71. struct pid_namespace *parent_pid_ns)
  72. {
  73. struct pid_namespace *ns;
  74. unsigned int level = parent_pid_ns->level + 1;
  75. int i;
  76. int err;
  77. if (level > MAX_PID_NS_LEVEL) {
  78. err = -EINVAL;
  79. goto out;
  80. }
  81. err = -ENOMEM;
  82. ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
  83. if (ns == NULL)
  84. goto out;
  85. ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  86. if (!ns->pidmap[0].page)
  87. goto out_free;
  88. ns->pid_cachep = create_pid_cachep(level + 1);
  89. if (ns->pid_cachep == NULL)
  90. goto out_free_map;
  91. err = proc_alloc_inum(&ns->proc_inum);
  92. if (err)
  93. goto out_free_map;
  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 destroy_pid_namespace(struct pid_namespace *ns)
  113. {
  114. int i;
  115. proc_free_inum(ns->proc_inum);
  116. for (i = 0; i < PIDMAP_ENTRIES; i++)
  117. kfree(ns->pidmap[i].page);
  118. put_user_ns(ns->user_ns);
  119. kmem_cache_free(pid_ns_cachep, ns);
  120. }
  121. struct pid_namespace *copy_pid_ns(unsigned long flags,
  122. struct user_namespace *user_ns, struct pid_namespace *old_ns)
  123. {
  124. if (!(flags & CLONE_NEWPID))
  125. return get_pid_ns(old_ns);
  126. if (task_active_pid_ns(current) != old_ns)
  127. return ERR_PTR(-EINVAL);
  128. return create_pid_namespace(user_ns, old_ns);
  129. }
  130. static void free_pid_ns(struct kref *kref)
  131. {
  132. struct pid_namespace *ns;
  133. ns = container_of(kref, struct pid_namespace, kref);
  134. destroy_pid_namespace(ns);
  135. }
  136. void put_pid_ns(struct pid_namespace *ns)
  137. {
  138. struct pid_namespace *parent;
  139. while (ns != &init_pid_ns) {
  140. parent = ns->parent;
  141. if (!kref_put(&ns->kref, free_pid_ns))
  142. break;
  143. ns = parent;
  144. }
  145. }
  146. EXPORT_SYMBOL_GPL(put_pid_ns);
  147. void zap_pid_ns_processes(struct pid_namespace *pid_ns)
  148. {
  149. int nr;
  150. int rc;
  151. struct task_struct *task, *me = current;
  152. int init_pids = thread_group_leader(me) ? 1 : 2;
  153. /* Don't allow any more processes into the pid namespace */
  154. disable_pid_allocation(pid_ns);
  155. /* Ignore SIGCHLD causing any terminated children to autoreap */
  156. spin_lock_irq(&me->sighand->siglock);
  157. me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
  158. spin_unlock_irq(&me->sighand->siglock);
  159. /*
  160. * The last thread in the cgroup-init thread group is terminating.
  161. * Find remaining pid_ts in the namespace, signal and wait for them
  162. * to exit.
  163. *
  164. * Note: This signals each threads in the namespace - even those that
  165. * belong to the same thread group, To avoid this, we would have
  166. * to walk the entire tasklist looking a processes in this
  167. * namespace, but that could be unnecessarily expensive if the
  168. * pid namespace has just a few processes. Or we need to
  169. * maintain a tasklist for each pid namespace.
  170. *
  171. */
  172. read_lock(&tasklist_lock);
  173. nr = next_pidmap(pid_ns, 1);
  174. while (nr > 0) {
  175. rcu_read_lock();
  176. task = pid_task(find_vpid(nr), PIDTYPE_PID);
  177. if (task && !__fatal_signal_pending(task))
  178. send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
  179. rcu_read_unlock();
  180. nr = next_pidmap(pid_ns, nr);
  181. }
  182. read_unlock(&tasklist_lock);
  183. /* Firstly reap the EXIT_ZOMBIE children we may have. */
  184. do {
  185. clear_thread_flag(TIF_SIGPENDING);
  186. rc = sys_wait4(-1, NULL, __WALL, NULL);
  187. } while (rc != -ECHILD);
  188. /*
  189. * sys_wait4() above can't reap the TASK_DEAD children.
  190. * Make sure they all go away, see free_pid().
  191. */
  192. for (;;) {
  193. set_current_state(TASK_UNINTERRUPTIBLE);
  194. if (pid_ns->nr_hashed == init_pids)
  195. break;
  196. schedule();
  197. }
  198. __set_current_state(TASK_RUNNING);
  199. if (pid_ns->reboot)
  200. current->signal->group_exit_code = pid_ns->reboot;
  201. acct_exit_ns(pid_ns);
  202. return;
  203. }
  204. #ifdef CONFIG_CHECKPOINT_RESTORE
  205. static int pid_ns_ctl_handler(struct ctl_table *table, int write,
  206. void __user *buffer, size_t *lenp, loff_t *ppos)
  207. {
  208. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  209. struct ctl_table tmp = *table;
  210. if (write && !ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))
  211. return -EPERM;
  212. /*
  213. * Writing directly to ns' last_pid field is OK, since this field
  214. * is volatile in a living namespace anyway and a code writing to
  215. * it should synchronize its usage with external means.
  216. */
  217. tmp.data = &pid_ns->last_pid;
  218. return proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  219. }
  220. extern int pid_max;
  221. static int zero = 0;
  222. static struct ctl_table pid_ns_ctl_table[] = {
  223. {
  224. .procname = "ns_last_pid",
  225. .maxlen = sizeof(int),
  226. .mode = 0666, /* permissions are checked in the handler */
  227. .proc_handler = pid_ns_ctl_handler,
  228. .extra1 = &zero,
  229. .extra2 = &pid_max,
  230. },
  231. { }
  232. };
  233. static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
  234. #endif /* CONFIG_CHECKPOINT_RESTORE */
  235. int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
  236. {
  237. if (pid_ns == &init_pid_ns)
  238. return 0;
  239. switch (cmd) {
  240. case LINUX_REBOOT_CMD_RESTART2:
  241. case LINUX_REBOOT_CMD_RESTART:
  242. pid_ns->reboot = SIGHUP;
  243. break;
  244. case LINUX_REBOOT_CMD_POWER_OFF:
  245. case LINUX_REBOOT_CMD_HALT:
  246. pid_ns->reboot = SIGINT;
  247. break;
  248. default:
  249. return -EINVAL;
  250. }
  251. read_lock(&tasklist_lock);
  252. force_sig(SIGKILL, pid_ns->child_reaper);
  253. read_unlock(&tasklist_lock);
  254. do_exit(0);
  255. /* Not reached */
  256. return 0;
  257. }
  258. static void *pidns_get(struct task_struct *task)
  259. {
  260. struct pid_namespace *ns;
  261. rcu_read_lock();
  262. ns = get_pid_ns(task_active_pid_ns(task));
  263. rcu_read_unlock();
  264. return ns;
  265. }
  266. static void pidns_put(void *ns)
  267. {
  268. put_pid_ns(ns);
  269. }
  270. static int pidns_install(struct nsproxy *nsproxy, void *ns)
  271. {
  272. struct pid_namespace *active = task_active_pid_ns(current);
  273. struct pid_namespace *ancestor, *new = ns;
  274. if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
  275. !nsown_capable(CAP_SYS_ADMIN))
  276. return -EPERM;
  277. /*
  278. * Only allow entering the current active pid namespace
  279. * or a child of the current active pid namespace.
  280. *
  281. * This is required for fork to return a usable pid value and
  282. * this maintains the property that processes and their
  283. * children can not escape their current pid namespace.
  284. */
  285. if (new->level < active->level)
  286. return -EINVAL;
  287. ancestor = new;
  288. while (ancestor->level > active->level)
  289. ancestor = ancestor->parent;
  290. if (ancestor != active)
  291. return -EINVAL;
  292. put_pid_ns(nsproxy->pid_ns);
  293. nsproxy->pid_ns = get_pid_ns(new);
  294. return 0;
  295. }
  296. static unsigned int pidns_inum(void *ns)
  297. {
  298. struct pid_namespace *pid_ns = ns;
  299. return pid_ns->proc_inum;
  300. }
  301. const struct proc_ns_operations pidns_operations = {
  302. .name = "pid",
  303. .type = CLONE_NEWPID,
  304. .get = pidns_get,
  305. .put = pidns_put,
  306. .install = pidns_install,
  307. .inum = pidns_inum,
  308. };
  309. static __init int pid_namespaces_init(void)
  310. {
  311. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
  312. #ifdef CONFIG_CHECKPOINT_RESTORE
  313. register_sysctl_paths(kern_path, pid_ns_ctl_table);
  314. #endif
  315. return 0;
  316. }
  317. __initcall(pid_namespaces_init);