nsproxy.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/slab.h>
  16. #include <linux/export.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. #include <linux/proc_fs.h>
  25. #include <linux/file.h>
  26. #include <linux/syscalls.h>
  27. static struct kmem_cache *nsproxy_cachep;
  28. struct nsproxy init_nsproxy = {
  29. .count = ATOMIC_INIT(1),
  30. .uts_ns = &init_uts_ns,
  31. #if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC)
  32. .ipc_ns = &init_ipc_ns,
  33. #endif
  34. .mnt_ns = NULL,
  35. .pid_ns = &init_pid_ns,
  36. #ifdef CONFIG_NET
  37. .net_ns = &init_net,
  38. #endif
  39. };
  40. static inline struct nsproxy *create_nsproxy(void)
  41. {
  42. struct nsproxy *nsproxy;
  43. nsproxy = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
  44. if (nsproxy)
  45. atomic_set(&nsproxy->count, 1);
  46. return nsproxy;
  47. }
  48. /*
  49. * Create new nsproxy and all of its the associated namespaces.
  50. * Return the newly created nsproxy. Do not attach this to the task,
  51. * leave it to the caller to do proper locking and attach it to task.
  52. */
  53. static struct nsproxy *create_new_namespaces(unsigned long flags,
  54. struct task_struct *tsk, struct user_namespace *user_ns,
  55. struct fs_struct *new_fs)
  56. {
  57. struct nsproxy *new_nsp;
  58. int err;
  59. new_nsp = create_nsproxy();
  60. if (!new_nsp)
  61. return ERR_PTR(-ENOMEM);
  62. new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, user_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, user_ns, 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, user_ns, 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, user_ns, 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->net_ns = copy_net_ns(flags, user_ns, tsk->nsproxy->net_ns);
  83. if (IS_ERR(new_nsp->net_ns)) {
  84. err = PTR_ERR(new_nsp->net_ns);
  85. goto out_net;
  86. }
  87. return new_nsp;
  88. out_net:
  89. if (new_nsp->pid_ns)
  90. put_pid_ns(new_nsp->pid_ns);
  91. out_pid:
  92. if (new_nsp->ipc_ns)
  93. put_ipc_ns(new_nsp->ipc_ns);
  94. out_ipc:
  95. if (new_nsp->uts_ns)
  96. put_uts_ns(new_nsp->uts_ns);
  97. out_uts:
  98. if (new_nsp->mnt_ns)
  99. put_mnt_ns(new_nsp->mnt_ns);
  100. out_ns:
  101. kmem_cache_free(nsproxy_cachep, new_nsp);
  102. return ERR_PTR(err);
  103. }
  104. /*
  105. * called from clone. This now handles copy for nsproxy and all
  106. * namespaces therein.
  107. */
  108. int copy_namespaces(unsigned long flags, struct task_struct *tsk)
  109. {
  110. struct nsproxy *old_ns = tsk->nsproxy;
  111. struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
  112. struct nsproxy *new_ns;
  113. int err = 0;
  114. if (!old_ns)
  115. return 0;
  116. get_nsproxy(old_ns);
  117. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  118. CLONE_NEWPID | CLONE_NEWNET)))
  119. return 0;
  120. if (!ns_capable(user_ns, CAP_SYS_ADMIN)) {
  121. err = -EPERM;
  122. goto out;
  123. }
  124. /*
  125. * CLONE_NEWIPC must detach from the undolist: after switching
  126. * to a new ipc namespace, the semaphore arrays from the old
  127. * namespace are unreachable. In clone parlance, CLONE_SYSVSEM
  128. * means share undolist with parent, so we must forbid using
  129. * it along with CLONE_NEWIPC.
  130. */
  131. if ((flags & CLONE_NEWIPC) && (flags & CLONE_SYSVSEM)) {
  132. err = -EINVAL;
  133. goto out;
  134. }
  135. new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
  136. if (IS_ERR(new_ns)) {
  137. err = PTR_ERR(new_ns);
  138. goto out;
  139. }
  140. tsk->nsproxy = new_ns;
  141. out:
  142. put_nsproxy(old_ns);
  143. return err;
  144. }
  145. void free_nsproxy(struct nsproxy *ns)
  146. {
  147. if (ns->mnt_ns)
  148. put_mnt_ns(ns->mnt_ns);
  149. if (ns->uts_ns)
  150. put_uts_ns(ns->uts_ns);
  151. if (ns->ipc_ns)
  152. put_ipc_ns(ns->ipc_ns);
  153. if (ns->pid_ns)
  154. put_pid_ns(ns->pid_ns);
  155. put_net(ns->net_ns);
  156. kmem_cache_free(nsproxy_cachep, ns);
  157. }
  158. /*
  159. * Called from unshare. Unshare all the namespaces part of nsproxy.
  160. * On success, returns the new nsproxy.
  161. */
  162. int unshare_nsproxy_namespaces(unsigned long unshare_flags,
  163. struct nsproxy **new_nsp, struct cred *new_cred, struct fs_struct *new_fs)
  164. {
  165. struct user_namespace *user_ns;
  166. int err = 0;
  167. if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  168. CLONE_NEWNET | CLONE_NEWPID)))
  169. return 0;
  170. user_ns = new_cred ? new_cred->user_ns : current_user_ns();
  171. if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  172. return -EPERM;
  173. *new_nsp = create_new_namespaces(unshare_flags, current, user_ns,
  174. new_fs ? new_fs : current->fs);
  175. if (IS_ERR(*new_nsp)) {
  176. err = PTR_ERR(*new_nsp);
  177. goto out;
  178. }
  179. out:
  180. return err;
  181. }
  182. void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
  183. {
  184. struct nsproxy *ns;
  185. might_sleep();
  186. ns = p->nsproxy;
  187. rcu_assign_pointer(p->nsproxy, new);
  188. if (ns && atomic_dec_and_test(&ns->count)) {
  189. /*
  190. * wait for others to get what they want from this nsproxy.
  191. *
  192. * cannot release this nsproxy via the call_rcu() since
  193. * put_mnt_ns() will want to sleep
  194. */
  195. synchronize_rcu();
  196. free_nsproxy(ns);
  197. }
  198. }
  199. void exit_task_namespaces(struct task_struct *p)
  200. {
  201. switch_task_namespaces(p, NULL);
  202. }
  203. SYSCALL_DEFINE2(setns, int, fd, int, nstype)
  204. {
  205. const struct proc_ns_operations *ops;
  206. struct task_struct *tsk = current;
  207. struct nsproxy *new_nsproxy;
  208. struct proc_inode *ei;
  209. struct file *file;
  210. int err;
  211. file = proc_ns_fget(fd);
  212. if (IS_ERR(file))
  213. return PTR_ERR(file);
  214. err = -EINVAL;
  215. ei = PROC_I(file->f_dentry->d_inode);
  216. ops = ei->ns_ops;
  217. if (nstype && (ops->type != nstype))
  218. goto out;
  219. new_nsproxy = create_new_namespaces(0, tsk, current_user_ns(), tsk->fs);
  220. if (IS_ERR(new_nsproxy)) {
  221. err = PTR_ERR(new_nsproxy);
  222. goto out;
  223. }
  224. err = ops->install(new_nsproxy, ei->ns);
  225. if (err) {
  226. free_nsproxy(new_nsproxy);
  227. goto out;
  228. }
  229. switch_task_namespaces(tsk, new_nsproxy);
  230. out:
  231. fput(file);
  232. return err;
  233. }
  234. int __init nsproxy_cache_init(void)
  235. {
  236. nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
  237. return 0;
  238. }