pid_namespace.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/syscalls.h>
  13. #include <linux/err.h>
  14. #include <linux/acct.h>
  15. #include <linux/slab.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/reboot.h>
  18. #define BITS_PER_PAGE (PAGE_SIZE*8)
  19. struct pid_cache {
  20. int nr_ids;
  21. char name[16];
  22. struct kmem_cache *cachep;
  23. struct list_head list;
  24. };
  25. static LIST_HEAD(pid_caches_lh);
  26. static DEFINE_MUTEX(pid_caches_mutex);
  27. static struct kmem_cache *pid_ns_cachep;
  28. /*
  29. * creates the kmem cache to allocate pids from.
  30. * @nr_ids: the number of numerical ids this pid will have to carry
  31. */
  32. static struct kmem_cache *create_pid_cachep(int nr_ids)
  33. {
  34. struct pid_cache *pcache;
  35. struct kmem_cache *cachep;
  36. mutex_lock(&pid_caches_mutex);
  37. list_for_each_entry(pcache, &pid_caches_lh, list)
  38. if (pcache->nr_ids == nr_ids)
  39. goto out;
  40. pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
  41. if (pcache == NULL)
  42. goto err_alloc;
  43. snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
  44. cachep = kmem_cache_create(pcache->name,
  45. sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
  46. 0, SLAB_HWCACHE_ALIGN, NULL);
  47. if (cachep == NULL)
  48. goto err_cachep;
  49. pcache->nr_ids = nr_ids;
  50. pcache->cachep = cachep;
  51. list_add(&pcache->list, &pid_caches_lh);
  52. out:
  53. mutex_unlock(&pid_caches_mutex);
  54. return pcache->cachep;
  55. err_cachep:
  56. kfree(pcache);
  57. err_alloc:
  58. mutex_unlock(&pid_caches_mutex);
  59. return NULL;
  60. }
  61. static struct pid_namespace *create_pid_namespace(struct pid_namespace *parent_pid_ns)
  62. {
  63. struct pid_namespace *ns;
  64. unsigned int level = parent_pid_ns->level + 1;
  65. int i, err = -ENOMEM;
  66. ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
  67. if (ns == NULL)
  68. goto out;
  69. ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  70. if (!ns->pidmap[0].page)
  71. goto out_free;
  72. ns->pid_cachep = create_pid_cachep(level + 1);
  73. if (ns->pid_cachep == NULL)
  74. goto out_free_map;
  75. err = proc_alloc_inum(&ns->proc_inum);
  76. if (err)
  77. goto out_free_map;
  78. kref_init(&ns->kref);
  79. ns->level = level;
  80. ns->parent = get_pid_ns(parent_pid_ns);
  81. set_bit(0, ns->pidmap[0].page);
  82. atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
  83. for (i = 1; i < PIDMAP_ENTRIES; i++)
  84. atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
  85. err = pid_ns_prepare_proc(ns);
  86. if (err)
  87. goto out_put_parent_pid_ns;
  88. return ns;
  89. out_put_parent_pid_ns:
  90. put_pid_ns(parent_pid_ns);
  91. out_free_map:
  92. kfree(ns->pidmap[0].page);
  93. out_free:
  94. kmem_cache_free(pid_ns_cachep, ns);
  95. out:
  96. return ERR_PTR(err);
  97. }
  98. static void destroy_pid_namespace(struct pid_namespace *ns)
  99. {
  100. int i;
  101. proc_free_inum(ns->proc_inum);
  102. for (i = 0; i < PIDMAP_ENTRIES; i++)
  103. kfree(ns->pidmap[i].page);
  104. kmem_cache_free(pid_ns_cachep, ns);
  105. }
  106. struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
  107. {
  108. if (!(flags & CLONE_NEWPID))
  109. return get_pid_ns(old_ns);
  110. if (flags & (CLONE_THREAD|CLONE_PARENT))
  111. return ERR_PTR(-EINVAL);
  112. return create_pid_namespace(old_ns);
  113. }
  114. void free_pid_ns(struct kref *kref)
  115. {
  116. struct pid_namespace *ns, *parent;
  117. ns = container_of(kref, struct pid_namespace, kref);
  118. parent = ns->parent;
  119. destroy_pid_namespace(ns);
  120. if (parent != NULL)
  121. put_pid_ns(parent);
  122. }
  123. void zap_pid_ns_processes(struct pid_namespace *pid_ns)
  124. {
  125. int nr;
  126. int rc;
  127. struct task_struct *task;
  128. /*
  129. * The last thread in the cgroup-init thread group is terminating.
  130. * Find remaining pid_ts in the namespace, signal and wait for them
  131. * to exit.
  132. *
  133. * Note: This signals each threads in the namespace - even those that
  134. * belong to the same thread group, To avoid this, we would have
  135. * to walk the entire tasklist looking a processes in this
  136. * namespace, but that could be unnecessarily expensive if the
  137. * pid namespace has just a few processes. Or we need to
  138. * maintain a tasklist for each pid namespace.
  139. *
  140. */
  141. read_lock(&tasklist_lock);
  142. nr = next_pidmap(pid_ns, 1);
  143. while (nr > 0) {
  144. rcu_read_lock();
  145. task = pid_task(find_vpid(nr), PIDTYPE_PID);
  146. if (task && !__fatal_signal_pending(task))
  147. send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
  148. rcu_read_unlock();
  149. nr = next_pidmap(pid_ns, nr);
  150. }
  151. read_unlock(&tasklist_lock);
  152. do {
  153. clear_thread_flag(TIF_SIGPENDING);
  154. rc = sys_wait4(-1, NULL, __WALL, NULL);
  155. } while (rc != -ECHILD);
  156. if (pid_ns->reboot)
  157. current->signal->group_exit_code = pid_ns->reboot;
  158. acct_exit_ns(pid_ns);
  159. return;
  160. }
  161. static int pid_ns_ctl_handler(struct ctl_table *table, int write,
  162. void __user *buffer, size_t *lenp, loff_t *ppos)
  163. {
  164. struct ctl_table tmp = *table;
  165. if (write && !capable(CAP_SYS_ADMIN))
  166. return -EPERM;
  167. /*
  168. * Writing directly to ns' last_pid field is OK, since this field
  169. * is volatile in a living namespace anyway and a code writing to
  170. * it should synchronize its usage with external means.
  171. */
  172. tmp.data = &current->nsproxy->pid_ns->last_pid;
  173. return proc_dointvec(&tmp, write, buffer, lenp, ppos);
  174. }
  175. static struct ctl_table pid_ns_ctl_table[] = {
  176. {
  177. .procname = "ns_last_pid",
  178. .maxlen = sizeof(int),
  179. .mode = 0666, /* permissions are checked in the handler */
  180. .proc_handler = pid_ns_ctl_handler,
  181. },
  182. { }
  183. };
  184. static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
  185. int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
  186. {
  187. if (pid_ns == &init_pid_ns)
  188. return 0;
  189. switch (cmd) {
  190. case LINUX_REBOOT_CMD_RESTART2:
  191. case LINUX_REBOOT_CMD_RESTART:
  192. pid_ns->reboot = SIGHUP;
  193. break;
  194. case LINUX_REBOOT_CMD_POWER_OFF:
  195. case LINUX_REBOOT_CMD_HALT:
  196. pid_ns->reboot = SIGINT;
  197. break;
  198. default:
  199. return -EINVAL;
  200. }
  201. read_lock(&tasklist_lock);
  202. force_sig(SIGKILL, pid_ns->child_reaper);
  203. read_unlock(&tasklist_lock);
  204. do_exit(0);
  205. /* Not reached */
  206. return 0;
  207. }
  208. static __init int pid_namespaces_init(void)
  209. {
  210. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
  211. register_sysctl_paths(kern_path, pid_ns_ctl_table);
  212. return 0;
  213. }
  214. __initcall(pid_namespaces_init);