nsproxy.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/version.h>
  17. #include <linux/nsproxy.h>
  18. #include <linux/init_task.h>
  19. #include <linux/mnt_namespace.h>
  20. #include <linux/utsname.h>
  21. #include <linux/pid_namespace.h>
  22. #include <net/net_namespace.h>
  23. #include <linux/ipc_namespace.h>
  24. static struct kmem_cache *nsproxy_cachep;
  25. struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
  26. /*
  27. * creates a copy of "orig" with refcount 1.
  28. */
  29. static inline struct nsproxy *clone_nsproxy(struct nsproxy *orig)
  30. {
  31. struct nsproxy *ns;
  32. ns = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
  33. if (ns) {
  34. memcpy(ns, orig, sizeof(struct nsproxy));
  35. atomic_set(&ns->count, 1);
  36. }
  37. return ns;
  38. }
  39. /*
  40. * Create new nsproxy and all of its the associated namespaces.
  41. * Return the newly created nsproxy. Do not attach this to the task,
  42. * leave it to the caller to do proper locking and attach it to task.
  43. */
  44. static struct nsproxy *create_new_namespaces(unsigned long flags,
  45. struct task_struct *tsk, struct fs_struct *new_fs)
  46. {
  47. struct nsproxy *new_nsp;
  48. int err;
  49. new_nsp = clone_nsproxy(tsk->nsproxy);
  50. if (!new_nsp)
  51. return ERR_PTR(-ENOMEM);
  52. new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, new_fs);
  53. if (IS_ERR(new_nsp->mnt_ns)) {
  54. err = PTR_ERR(new_nsp->mnt_ns);
  55. goto out_ns;
  56. }
  57. new_nsp->uts_ns = copy_utsname(flags, tsk->nsproxy->uts_ns);
  58. if (IS_ERR(new_nsp->uts_ns)) {
  59. err = PTR_ERR(new_nsp->uts_ns);
  60. goto out_uts;
  61. }
  62. new_nsp->ipc_ns = copy_ipcs(flags, tsk->nsproxy->ipc_ns);
  63. if (IS_ERR(new_nsp->ipc_ns)) {
  64. err = PTR_ERR(new_nsp->ipc_ns);
  65. goto out_ipc;
  66. }
  67. new_nsp->pid_ns = copy_pid_ns(flags, task_active_pid_ns(tsk));
  68. if (IS_ERR(new_nsp->pid_ns)) {
  69. err = PTR_ERR(new_nsp->pid_ns);
  70. goto out_pid;
  71. }
  72. new_nsp->user_ns = copy_user_ns(flags, tsk->nsproxy->user_ns);
  73. if (IS_ERR(new_nsp->user_ns)) {
  74. err = PTR_ERR(new_nsp->user_ns);
  75. goto out_user;
  76. }
  77. new_nsp->net_ns = copy_net_ns(flags, tsk->nsproxy->net_ns);
  78. if (IS_ERR(new_nsp->net_ns)) {
  79. err = PTR_ERR(new_nsp->net_ns);
  80. goto out_net;
  81. }
  82. return new_nsp;
  83. out_net:
  84. if (new_nsp->user_ns)
  85. put_user_ns(new_nsp->user_ns);
  86. out_user:
  87. if (new_nsp->pid_ns)
  88. put_pid_ns(new_nsp->pid_ns);
  89. out_pid:
  90. if (new_nsp->ipc_ns)
  91. put_ipc_ns(new_nsp->ipc_ns);
  92. out_ipc:
  93. if (new_nsp->uts_ns)
  94. put_uts_ns(new_nsp->uts_ns);
  95. out_uts:
  96. if (new_nsp->mnt_ns)
  97. put_mnt_ns(new_nsp->mnt_ns);
  98. out_ns:
  99. kmem_cache_free(nsproxy_cachep, new_nsp);
  100. return ERR_PTR(err);
  101. }
  102. /*
  103. * called from clone. This now handles copy for nsproxy and all
  104. * namespaces therein.
  105. */
  106. int copy_namespaces(unsigned long flags, struct task_struct *tsk)
  107. {
  108. struct nsproxy *old_ns = tsk->nsproxy;
  109. struct nsproxy *new_ns;
  110. int err = 0;
  111. if (!old_ns)
  112. return 0;
  113. get_nsproxy(old_ns);
  114. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  115. CLONE_NEWUSER | CLONE_NEWPID | CLONE_NEWNET)))
  116. return 0;
  117. if (!capable(CAP_SYS_ADMIN)) {
  118. err = -EPERM;
  119. goto out;
  120. }
  121. /*
  122. * CLONE_NEWIPC must detach from the undolist: after switching
  123. * to a new ipc namespace, the semaphore arrays from the old
  124. * namespace are unreachable. In clone parlance, CLONE_SYSVSEM
  125. * means share undolist with parent, so we must forbid using
  126. * it along with CLONE_NEWIPC.
  127. */
  128. if ((flags & CLONE_NEWIPC) && (flags & CLONE_SYSVSEM)) {
  129. err = -EINVAL;
  130. goto out;
  131. }
  132. new_ns = create_new_namespaces(flags, tsk, tsk->fs);
  133. if (IS_ERR(new_ns)) {
  134. err = PTR_ERR(new_ns);
  135. goto out;
  136. }
  137. err = ns_cgroup_clone(tsk);
  138. if (err) {
  139. put_nsproxy(new_ns);
  140. goto out;
  141. }
  142. tsk->nsproxy = new_ns;
  143. out:
  144. put_nsproxy(old_ns);
  145. return err;
  146. }
  147. void free_nsproxy(struct nsproxy *ns)
  148. {
  149. if (ns->mnt_ns)
  150. put_mnt_ns(ns->mnt_ns);
  151. if (ns->uts_ns)
  152. put_uts_ns(ns->uts_ns);
  153. if (ns->ipc_ns)
  154. put_ipc_ns(ns->ipc_ns);
  155. if (ns->pid_ns)
  156. put_pid_ns(ns->pid_ns);
  157. if (ns->user_ns)
  158. put_user_ns(ns->user_ns);
  159. put_net(ns->net_ns);
  160. kmem_cache_free(nsproxy_cachep, ns);
  161. }
  162. /*
  163. * Called from unshare. Unshare all the namespaces part of nsproxy.
  164. * On success, returns the new nsproxy.
  165. */
  166. int unshare_nsproxy_namespaces(unsigned long unshare_flags,
  167. struct nsproxy **new_nsp, struct fs_struct *new_fs)
  168. {
  169. int err = 0;
  170. if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  171. CLONE_NEWUSER | CLONE_NEWNET)))
  172. return 0;
  173. if (!capable(CAP_SYS_ADMIN))
  174. return -EPERM;
  175. *new_nsp = create_new_namespaces(unshare_flags, current,
  176. new_fs ? new_fs : current->fs);
  177. if (IS_ERR(*new_nsp)) {
  178. err = PTR_ERR(*new_nsp);
  179. goto out;
  180. }
  181. err = ns_cgroup_clone(current);
  182. if (err)
  183. put_nsproxy(*new_nsp);
  184. out:
  185. return err;
  186. }
  187. void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
  188. {
  189. struct nsproxy *ns;
  190. might_sleep();
  191. ns = p->nsproxy;
  192. rcu_assign_pointer(p->nsproxy, new);
  193. if (ns && atomic_dec_and_test(&ns->count)) {
  194. /*
  195. * wait for others to get what they want from this nsproxy.
  196. *
  197. * cannot release this nsproxy via the call_rcu() since
  198. * put_mnt_ns() will want to sleep
  199. */
  200. synchronize_rcu();
  201. free_nsproxy(ns);
  202. }
  203. }
  204. void exit_task_namespaces(struct task_struct *p)
  205. {
  206. switch_task_namespaces(p, NULL);
  207. }
  208. static int __init nsproxy_cache_init(void)
  209. {
  210. nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
  211. return 0;
  212. }
  213. module_init(nsproxy_cache_init);