nsproxy.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2006 IBM Corporation
  3. *
  4. * Author: Serge Hallyn <serue@us.ibm.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2 of the
  9. * License.
  10. *
  11. * Jun 2006 - namespaces support
  12. * OpenVZ, SWsoft Inc.
  13. * Pavel Emelianov <xemul@openvz.org>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/nsproxy.h>
  17. #include <linux/init_task.h>
  18. #include <linux/mnt_namespace.h>
  19. #include <linux/utsname.h>
  20. #include <linux/pid_namespace.h>
  21. #include <net/net_namespace.h>
  22. #include <linux/ipc_namespace.h>
  23. static struct kmem_cache *nsproxy_cachep;
  24. struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
  25. static inline struct nsproxy *create_nsproxy(void)
  26. {
  27. struct nsproxy *nsproxy;
  28. nsproxy = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
  29. if (nsproxy)
  30. atomic_set(&nsproxy->count, 1);
  31. return nsproxy;
  32. }
  33. /*
  34. * Create new nsproxy and all of its the associated namespaces.
  35. * Return the newly created nsproxy. Do not attach this to the task,
  36. * leave it to the caller to do proper locking and attach it to task.
  37. */
  38. static struct nsproxy *create_new_namespaces(unsigned long flags,
  39. struct task_struct *tsk, struct fs_struct *new_fs)
  40. {
  41. struct nsproxy *new_nsp;
  42. int err;
  43. new_nsp = create_nsproxy();
  44. if (!new_nsp)
  45. return ERR_PTR(-ENOMEM);
  46. new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, new_fs);
  47. if (IS_ERR(new_nsp->mnt_ns)) {
  48. err = PTR_ERR(new_nsp->mnt_ns);
  49. goto out_ns;
  50. }
  51. new_nsp->uts_ns = copy_utsname(flags, tsk->nsproxy->uts_ns);
  52. if (IS_ERR(new_nsp->uts_ns)) {
  53. err = PTR_ERR(new_nsp->uts_ns);
  54. goto out_uts;
  55. }
  56. new_nsp->ipc_ns = copy_ipcs(flags, tsk->nsproxy->ipc_ns);
  57. if (IS_ERR(new_nsp->ipc_ns)) {
  58. err = PTR_ERR(new_nsp->ipc_ns);
  59. goto out_ipc;
  60. }
  61. new_nsp->pid_ns = copy_pid_ns(flags, task_active_pid_ns(tsk));
  62. if (IS_ERR(new_nsp->pid_ns)) {
  63. err = PTR_ERR(new_nsp->pid_ns);
  64. goto out_pid;
  65. }
  66. new_nsp->net_ns = copy_net_ns(flags, tsk->nsproxy->net_ns);
  67. if (IS_ERR(new_nsp->net_ns)) {
  68. err = PTR_ERR(new_nsp->net_ns);
  69. goto out_net;
  70. }
  71. return new_nsp;
  72. out_net:
  73. if (new_nsp->pid_ns)
  74. put_pid_ns(new_nsp->pid_ns);
  75. out_pid:
  76. if (new_nsp->ipc_ns)
  77. put_ipc_ns(new_nsp->ipc_ns);
  78. out_ipc:
  79. if (new_nsp->uts_ns)
  80. put_uts_ns(new_nsp->uts_ns);
  81. out_uts:
  82. if (new_nsp->mnt_ns)
  83. put_mnt_ns(new_nsp->mnt_ns);
  84. out_ns:
  85. kmem_cache_free(nsproxy_cachep, new_nsp);
  86. return ERR_PTR(err);
  87. }
  88. /*
  89. * called from clone. This now handles copy for nsproxy and all
  90. * namespaces therein.
  91. */
  92. int copy_namespaces(unsigned long flags, struct task_struct *tsk)
  93. {
  94. struct nsproxy *old_ns = tsk->nsproxy;
  95. struct nsproxy *new_ns;
  96. int err = 0;
  97. if (!old_ns)
  98. return 0;
  99. get_nsproxy(old_ns);
  100. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  101. CLONE_NEWPID | CLONE_NEWNET)))
  102. return 0;
  103. if (!capable(CAP_SYS_ADMIN)) {
  104. err = -EPERM;
  105. goto out;
  106. }
  107. /*
  108. * CLONE_NEWIPC must detach from the undolist: after switching
  109. * to a new ipc namespace, the semaphore arrays from the old
  110. * namespace are unreachable. In clone parlance, CLONE_SYSVSEM
  111. * means share undolist with parent, so we must forbid using
  112. * it along with CLONE_NEWIPC.
  113. */
  114. if ((flags & CLONE_NEWIPC) && (flags & CLONE_SYSVSEM)) {
  115. err = -EINVAL;
  116. goto out;
  117. }
  118. new_ns = create_new_namespaces(flags, tsk, tsk->fs);
  119. if (IS_ERR(new_ns)) {
  120. err = PTR_ERR(new_ns);
  121. goto out;
  122. }
  123. tsk->nsproxy = new_ns;
  124. out:
  125. put_nsproxy(old_ns);
  126. return err;
  127. }
  128. void free_nsproxy(struct nsproxy *ns)
  129. {
  130. if (ns->mnt_ns)
  131. put_mnt_ns(ns->mnt_ns);
  132. if (ns->uts_ns)
  133. put_uts_ns(ns->uts_ns);
  134. if (ns->ipc_ns)
  135. put_ipc_ns(ns->ipc_ns);
  136. if (ns->pid_ns)
  137. put_pid_ns(ns->pid_ns);
  138. put_net(ns->net_ns);
  139. kmem_cache_free(nsproxy_cachep, ns);
  140. }
  141. /*
  142. * Called from unshare. Unshare all the namespaces part of nsproxy.
  143. * On success, returns the new nsproxy.
  144. */
  145. int unshare_nsproxy_namespaces(unsigned long unshare_flags,
  146. struct nsproxy **new_nsp, struct fs_struct *new_fs)
  147. {
  148. int err = 0;
  149. if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  150. CLONE_NEWNET)))
  151. return 0;
  152. if (!capable(CAP_SYS_ADMIN))
  153. return -EPERM;
  154. *new_nsp = create_new_namespaces(unshare_flags, current,
  155. new_fs ? new_fs : current->fs);
  156. if (IS_ERR(*new_nsp)) {
  157. err = PTR_ERR(*new_nsp);
  158. goto out;
  159. }
  160. err = ns_cgroup_clone(current, task_pid(current));
  161. if (err)
  162. put_nsproxy(*new_nsp);
  163. out:
  164. return err;
  165. }
  166. void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
  167. {
  168. struct nsproxy *ns;
  169. might_sleep();
  170. ns = p->nsproxy;
  171. rcu_assign_pointer(p->nsproxy, new);
  172. if (ns && atomic_dec_and_test(&ns->count)) {
  173. /*
  174. * wait for others to get what they want from this nsproxy.
  175. *
  176. * cannot release this nsproxy via the call_rcu() since
  177. * put_mnt_ns() will want to sleep
  178. */
  179. synchronize_rcu();
  180. free_nsproxy(ns);
  181. }
  182. }
  183. void exit_task_namespaces(struct task_struct *p)
  184. {
  185. switch_task_namespaces(p, NULL);
  186. }
  187. static int __init nsproxy_cache_init(void)
  188. {
  189. nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
  190. return 0;
  191. }
  192. module_init(nsproxy_cache_init);