pid_namespace.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. kref_init(&ns->kref);
  76. ns->level = level;
  77. ns->parent = get_pid_ns(parent_pid_ns);
  78. set_bit(0, ns->pidmap[0].page);
  79. atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
  80. for (i = 1; i < PIDMAP_ENTRIES; i++)
  81. atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
  82. err = pid_ns_prepare_proc(ns);
  83. if (err)
  84. goto out_put_parent_pid_ns;
  85. return ns;
  86. out_put_parent_pid_ns:
  87. put_pid_ns(parent_pid_ns);
  88. out_free_map:
  89. kfree(ns->pidmap[0].page);
  90. out_free:
  91. kmem_cache_free(pid_ns_cachep, ns);
  92. out:
  93. return ERR_PTR(err);
  94. }
  95. static void destroy_pid_namespace(struct pid_namespace *ns)
  96. {
  97. int i;
  98. for (i = 0; i < PIDMAP_ENTRIES; i++)
  99. kfree(ns->pidmap[i].page);
  100. kmem_cache_free(pid_ns_cachep, ns);
  101. }
  102. struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
  103. {
  104. if (!(flags & CLONE_NEWPID))
  105. return get_pid_ns(old_ns);
  106. if (flags & (CLONE_THREAD|CLONE_PARENT))
  107. return ERR_PTR(-EINVAL);
  108. return create_pid_namespace(old_ns);
  109. }
  110. void free_pid_ns(struct kref *kref)
  111. {
  112. struct pid_namespace *ns, *parent;
  113. ns = container_of(kref, struct pid_namespace, kref);
  114. parent = ns->parent;
  115. destroy_pid_namespace(ns);
  116. if (parent != NULL)
  117. put_pid_ns(parent);
  118. }
  119. void zap_pid_ns_processes(struct pid_namespace *pid_ns)
  120. {
  121. int nr;
  122. int rc;
  123. struct task_struct *task;
  124. /*
  125. * The last thread in the cgroup-init thread group is terminating.
  126. * Find remaining pid_ts in the namespace, signal and wait for them
  127. * to exit.
  128. *
  129. * Note: This signals each threads in the namespace - even those that
  130. * belong to the same thread group, To avoid this, we would have
  131. * to walk the entire tasklist looking a processes in this
  132. * namespace, but that could be unnecessarily expensive if the
  133. * pid namespace has just a few processes. Or we need to
  134. * maintain a tasklist for each pid namespace.
  135. *
  136. */
  137. read_lock(&tasklist_lock);
  138. nr = next_pidmap(pid_ns, 1);
  139. while (nr > 0) {
  140. rcu_read_lock();
  141. task = pid_task(find_vpid(nr), PIDTYPE_PID);
  142. if (task && !__fatal_signal_pending(task))
  143. send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
  144. rcu_read_unlock();
  145. nr = next_pidmap(pid_ns, nr);
  146. }
  147. read_unlock(&tasklist_lock);
  148. do {
  149. clear_thread_flag(TIF_SIGPENDING);
  150. rc = sys_wait4(-1, NULL, __WALL, NULL);
  151. } while (rc != -ECHILD);
  152. if (pid_ns->reboot)
  153. current->signal->group_exit_code = pid_ns->reboot;
  154. acct_exit_ns(pid_ns);
  155. return;
  156. }
  157. static int pid_ns_ctl_handler(struct ctl_table *table, int write,
  158. void __user *buffer, size_t *lenp, loff_t *ppos)
  159. {
  160. struct ctl_table tmp = *table;
  161. if (write && !capable(CAP_SYS_ADMIN))
  162. return -EPERM;
  163. /*
  164. * Writing directly to ns' last_pid field is OK, since this field
  165. * is volatile in a living namespace anyway and a code writing to
  166. * it should synchronize its usage with external means.
  167. */
  168. tmp.data = &current->nsproxy->pid_ns->last_pid;
  169. return proc_dointvec(&tmp, write, buffer, lenp, ppos);
  170. }
  171. static struct ctl_table pid_ns_ctl_table[] = {
  172. {
  173. .procname = "ns_last_pid",
  174. .maxlen = sizeof(int),
  175. .mode = 0666, /* permissions are checked in the handler */
  176. .proc_handler = pid_ns_ctl_handler,
  177. },
  178. { }
  179. };
  180. static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
  181. int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
  182. {
  183. if (pid_ns == &init_pid_ns)
  184. return 0;
  185. switch (cmd) {
  186. case LINUX_REBOOT_CMD_RESTART2:
  187. case LINUX_REBOOT_CMD_RESTART:
  188. pid_ns->reboot = SIGHUP;
  189. break;
  190. case LINUX_REBOOT_CMD_POWER_OFF:
  191. case LINUX_REBOOT_CMD_HALT:
  192. pid_ns->reboot = SIGINT;
  193. break;
  194. default:
  195. return -EINVAL;
  196. }
  197. read_lock(&tasklist_lock);
  198. force_sig(SIGKILL, pid_ns->child_reaper);
  199. read_unlock(&tasklist_lock);
  200. do_exit(0);
  201. /* Not reached */
  202. return 0;
  203. }
  204. static __init int pid_namespaces_init(void)
  205. {
  206. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
  207. register_sysctl_paths(kern_path, pid_ns_ctl_table);
  208. return 0;
  209. }
  210. __initcall(pid_namespaces_init);