nsproxy.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. static struct kmem_cache *nsproxy_cachep;
  24. struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
  25. static inline void get_nsproxy(struct nsproxy *ns)
  26. {
  27. atomic_inc(&ns->count);
  28. }
  29. void get_task_namespaces(struct task_struct *tsk)
  30. {
  31. struct nsproxy *ns = tsk->nsproxy;
  32. if (ns) {
  33. get_nsproxy(ns);
  34. }
  35. }
  36. /*
  37. * creates a copy of "orig" with refcount 1.
  38. */
  39. static inline struct nsproxy *clone_nsproxy(struct nsproxy *orig)
  40. {
  41. struct nsproxy *ns;
  42. ns = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
  43. if (ns) {
  44. memcpy(ns, orig, sizeof(struct nsproxy));
  45. atomic_set(&ns->count, 1);
  46. }
  47. return ns;
  48. }
  49. /*
  50. * Create new nsproxy and all of its the associated namespaces.
  51. * Return the newly created nsproxy. Do not attach this to the task,
  52. * leave it to the caller to do proper locking and attach it to task.
  53. */
  54. static struct nsproxy *create_new_namespaces(unsigned long flags,
  55. struct task_struct *tsk, struct fs_struct *new_fs)
  56. {
  57. struct nsproxy *new_nsp;
  58. int err;
  59. new_nsp = clone_nsproxy(tsk->nsproxy);
  60. if (!new_nsp)
  61. return ERR_PTR(-ENOMEM);
  62. new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, new_fs);
  63. if (IS_ERR(new_nsp->mnt_ns)) {
  64. err = PTR_ERR(new_nsp->mnt_ns);
  65. goto out_ns;
  66. }
  67. new_nsp->uts_ns = copy_utsname(flags, tsk->nsproxy->uts_ns);
  68. if (IS_ERR(new_nsp->uts_ns)) {
  69. err = PTR_ERR(new_nsp->uts_ns);
  70. goto out_uts;
  71. }
  72. new_nsp->ipc_ns = copy_ipcs(flags, tsk->nsproxy->ipc_ns);
  73. if (IS_ERR(new_nsp->ipc_ns)) {
  74. err = PTR_ERR(new_nsp->ipc_ns);
  75. goto out_ipc;
  76. }
  77. new_nsp->pid_ns = copy_pid_ns(flags, tsk->nsproxy->pid_ns);
  78. if (IS_ERR(new_nsp->pid_ns)) {
  79. err = PTR_ERR(new_nsp->pid_ns);
  80. goto out_pid;
  81. }
  82. new_nsp->user_ns = copy_user_ns(flags, tsk->nsproxy->user_ns);
  83. if (IS_ERR(new_nsp->user_ns)) {
  84. err = PTR_ERR(new_nsp->user_ns);
  85. goto out_user;
  86. }
  87. new_nsp->net_ns = copy_net_ns(flags, tsk->nsproxy->net_ns);
  88. if (IS_ERR(new_nsp->net_ns)) {
  89. err = PTR_ERR(new_nsp->net_ns);
  90. goto out_net;
  91. }
  92. return new_nsp;
  93. out_net:
  94. if (new_nsp->user_ns)
  95. put_user_ns(new_nsp->user_ns);
  96. out_user:
  97. if (new_nsp->pid_ns)
  98. put_pid_ns(new_nsp->pid_ns);
  99. out_pid:
  100. if (new_nsp->ipc_ns)
  101. put_ipc_ns(new_nsp->ipc_ns);
  102. out_ipc:
  103. if (new_nsp->uts_ns)
  104. put_uts_ns(new_nsp->uts_ns);
  105. out_uts:
  106. if (new_nsp->mnt_ns)
  107. put_mnt_ns(new_nsp->mnt_ns);
  108. out_ns:
  109. kmem_cache_free(nsproxy_cachep, new_nsp);
  110. return ERR_PTR(err);
  111. }
  112. /*
  113. * called from clone. This now handles copy for nsproxy and all
  114. * namespaces therein.
  115. */
  116. int copy_namespaces(unsigned long flags, struct task_struct *tsk)
  117. {
  118. struct nsproxy *old_ns = tsk->nsproxy;
  119. struct nsproxy *new_ns;
  120. int err = 0;
  121. if (!old_ns)
  122. return 0;
  123. get_nsproxy(old_ns);
  124. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER | CLONE_NEWNET)))
  125. return 0;
  126. if (!capable(CAP_SYS_ADMIN)) {
  127. err = -EPERM;
  128. goto out;
  129. }
  130. new_ns = create_new_namespaces(flags, tsk, tsk->fs);
  131. if (IS_ERR(new_ns)) {
  132. err = PTR_ERR(new_ns);
  133. goto out;
  134. }
  135. tsk->nsproxy = new_ns;
  136. out:
  137. put_nsproxy(old_ns);
  138. return err;
  139. }
  140. void free_nsproxy(struct nsproxy *ns)
  141. {
  142. if (ns->mnt_ns)
  143. put_mnt_ns(ns->mnt_ns);
  144. if (ns->uts_ns)
  145. put_uts_ns(ns->uts_ns);
  146. if (ns->ipc_ns)
  147. put_ipc_ns(ns->ipc_ns);
  148. if (ns->pid_ns)
  149. put_pid_ns(ns->pid_ns);
  150. if (ns->user_ns)
  151. put_user_ns(ns->user_ns);
  152. put_net(ns->net_ns);
  153. kmem_cache_free(nsproxy_cachep, ns);
  154. }
  155. /*
  156. * Called from unshare. Unshare all the namespaces part of nsproxy.
  157. * On success, returns the new nsproxy.
  158. */
  159. int unshare_nsproxy_namespaces(unsigned long unshare_flags,
  160. struct nsproxy **new_nsp, struct fs_struct *new_fs)
  161. {
  162. int err = 0;
  163. if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  164. CLONE_NEWUSER | CLONE_NEWNET)))
  165. return 0;
  166. if (!capable(CAP_SYS_ADMIN))
  167. return -EPERM;
  168. *new_nsp = create_new_namespaces(unshare_flags, current,
  169. new_fs ? new_fs : current->fs);
  170. if (IS_ERR(*new_nsp))
  171. err = PTR_ERR(*new_nsp);
  172. return err;
  173. }
  174. static int __init nsproxy_cache_init(void)
  175. {
  176. nsproxy_cachep = kmem_cache_create("nsproxy", sizeof(struct nsproxy),
  177. 0, SLAB_PANIC, NULL);
  178. return 0;
  179. }
  180. module_init(nsproxy_cache_init);