nsproxy.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 fs_struct *new_fs)
  55. {
  56. struct nsproxy *new_nsp;
  57. int err;
  58. new_nsp = create_nsproxy();
  59. if (!new_nsp)
  60. return ERR_PTR(-ENOMEM);
  61. new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, task_cred_xxx(tsk, user_ns), new_fs);
  62. if (IS_ERR(new_nsp->mnt_ns)) {
  63. err = PTR_ERR(new_nsp->mnt_ns);
  64. goto out_ns;
  65. }
  66. new_nsp->uts_ns = copy_utsname(flags, tsk);
  67. if (IS_ERR(new_nsp->uts_ns)) {
  68. err = PTR_ERR(new_nsp->uts_ns);
  69. goto out_uts;
  70. }
  71. new_nsp->ipc_ns = copy_ipcs(flags, tsk);
  72. if (IS_ERR(new_nsp->ipc_ns)) {
  73. err = PTR_ERR(new_nsp->ipc_ns);
  74. goto out_ipc;
  75. }
  76. new_nsp->pid_ns = copy_pid_ns(flags, task_active_pid_ns(tsk));
  77. if (IS_ERR(new_nsp->pid_ns)) {
  78. err = PTR_ERR(new_nsp->pid_ns);
  79. goto out_pid;
  80. }
  81. new_nsp->net_ns = copy_net_ns(flags, task_cred_xxx(tsk, user_ns), tsk->nsproxy->net_ns);
  82. if (IS_ERR(new_nsp->net_ns)) {
  83. err = PTR_ERR(new_nsp->net_ns);
  84. goto out_net;
  85. }
  86. return new_nsp;
  87. out_net:
  88. if (new_nsp->pid_ns)
  89. put_pid_ns(new_nsp->pid_ns);
  90. out_pid:
  91. if (new_nsp->ipc_ns)
  92. put_ipc_ns(new_nsp->ipc_ns);
  93. out_ipc:
  94. if (new_nsp->uts_ns)
  95. put_uts_ns(new_nsp->uts_ns);
  96. out_uts:
  97. if (new_nsp->mnt_ns)
  98. put_mnt_ns(new_nsp->mnt_ns);
  99. out_ns:
  100. kmem_cache_free(nsproxy_cachep, new_nsp);
  101. return ERR_PTR(err);
  102. }
  103. /*
  104. * called from clone. This now handles copy for nsproxy and all
  105. * namespaces therein.
  106. */
  107. int copy_namespaces(unsigned long flags, struct task_struct *tsk)
  108. {
  109. struct nsproxy *old_ns = tsk->nsproxy;
  110. struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
  111. struct nsproxy *new_ns;
  112. int err = 0;
  113. if (!old_ns)
  114. return 0;
  115. get_nsproxy(old_ns);
  116. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  117. CLONE_NEWPID | CLONE_NEWNET)))
  118. return 0;
  119. if (!ns_capable(user_ns, CAP_SYS_ADMIN)) {
  120. err = -EPERM;
  121. goto out;
  122. }
  123. /*
  124. * CLONE_NEWIPC must detach from the undolist: after switching
  125. * to a new ipc namespace, the semaphore arrays from the old
  126. * namespace are unreachable. In clone parlance, CLONE_SYSVSEM
  127. * means share undolist with parent, so we must forbid using
  128. * it along with CLONE_NEWIPC.
  129. */
  130. if ((flags & CLONE_NEWIPC) && (flags & CLONE_SYSVSEM)) {
  131. err = -EINVAL;
  132. goto out;
  133. }
  134. new_ns = create_new_namespaces(flags, tsk, tsk->fs);
  135. if (IS_ERR(new_ns)) {
  136. err = PTR_ERR(new_ns);
  137. goto out;
  138. }
  139. tsk->nsproxy = new_ns;
  140. out:
  141. put_nsproxy(old_ns);
  142. return err;
  143. }
  144. void free_nsproxy(struct nsproxy *ns)
  145. {
  146. if (ns->mnt_ns)
  147. put_mnt_ns(ns->mnt_ns);
  148. if (ns->uts_ns)
  149. put_uts_ns(ns->uts_ns);
  150. if (ns->ipc_ns)
  151. put_ipc_ns(ns->ipc_ns);
  152. if (ns->pid_ns)
  153. put_pid_ns(ns->pid_ns);
  154. put_net(ns->net_ns);
  155. kmem_cache_free(nsproxy_cachep, ns);
  156. }
  157. /*
  158. * Called from unshare. Unshare all the namespaces part of nsproxy.
  159. * On success, returns the new nsproxy.
  160. */
  161. int unshare_nsproxy_namespaces(unsigned long unshare_flags,
  162. struct nsproxy **new_nsp, struct fs_struct *new_fs)
  163. {
  164. int err = 0;
  165. if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  166. CLONE_NEWNET)))
  167. return 0;
  168. if (!nsown_capable(CAP_SYS_ADMIN))
  169. return -EPERM;
  170. *new_nsp = create_new_namespaces(unshare_flags, current,
  171. new_fs ? new_fs : current->fs);
  172. if (IS_ERR(*new_nsp)) {
  173. err = PTR_ERR(*new_nsp);
  174. goto out;
  175. }
  176. out:
  177. return err;
  178. }
  179. void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
  180. {
  181. struct nsproxy *ns;
  182. might_sleep();
  183. task_lock(p);
  184. ns = p->nsproxy;
  185. p->nsproxy = new;
  186. task_unlock(p);
  187. if (ns && atomic_dec_and_test(&ns->count))
  188. free_nsproxy(ns);
  189. }
  190. void exit_task_namespaces(struct task_struct *p)
  191. {
  192. switch_task_namespaces(p, NULL);
  193. }
  194. SYSCALL_DEFINE2(setns, int, fd, int, nstype)
  195. {
  196. const struct proc_ns_operations *ops;
  197. struct task_struct *tsk = current;
  198. struct nsproxy *new_nsproxy;
  199. struct proc_inode *ei;
  200. struct file *file;
  201. int err;
  202. file = proc_ns_fget(fd);
  203. if (IS_ERR(file))
  204. return PTR_ERR(file);
  205. err = -EINVAL;
  206. ei = PROC_I(file->f_dentry->d_inode);
  207. ops = ei->ns_ops;
  208. if (nstype && (ops->type != nstype))
  209. goto out;
  210. new_nsproxy = create_new_namespaces(0, tsk, tsk->fs);
  211. if (IS_ERR(new_nsproxy)) {
  212. err = PTR_ERR(new_nsproxy);
  213. goto out;
  214. }
  215. err = ops->install(new_nsproxy, ei->ns);
  216. if (err) {
  217. free_nsproxy(new_nsproxy);
  218. goto out;
  219. }
  220. switch_task_namespaces(tsk, new_nsproxy);
  221. out:
  222. fput(file);
  223. return err;
  224. }
  225. int __init nsproxy_cache_init(void)
  226. {
  227. nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
  228. return 0;
  229. }