nsproxy.c 6.4 KB

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