nsproxy.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. static struct kmem_cache *nsproxy_cachep;
  23. struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
  24. static inline void get_nsproxy(struct nsproxy *ns)
  25. {
  26. atomic_inc(&ns->count);
  27. }
  28. void get_task_namespaces(struct task_struct *tsk)
  29. {
  30. struct nsproxy *ns = tsk->nsproxy;
  31. if (ns) {
  32. get_nsproxy(ns);
  33. }
  34. }
  35. /*
  36. * creates a copy of "orig" with refcount 1.
  37. */
  38. static inline struct nsproxy *clone_nsproxy(struct nsproxy *orig)
  39. {
  40. struct nsproxy *ns;
  41. ns = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
  42. if (ns) {
  43. memcpy(ns, orig, sizeof(struct nsproxy));
  44. atomic_set(&ns->count, 1);
  45. }
  46. return ns;
  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 = clone_nsproxy(tsk->nsproxy);
  59. if (!new_nsp)
  60. return ERR_PTR(-ENOMEM);
  61. new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_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->nsproxy->uts_ns);
  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->nsproxy->ipc_ns);
  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, tsk->nsproxy->pid_ns);
  77. if (IS_ERR(new_nsp->pid_ns)) {
  78. err = PTR_ERR(new_nsp->pid_ns);
  79. goto out_pid;
  80. }
  81. new_nsp->user_ns = copy_user_ns(flags, tsk->nsproxy->user_ns);
  82. if (IS_ERR(new_nsp->user_ns)) {
  83. err = PTR_ERR(new_nsp->user_ns);
  84. goto out_user;
  85. }
  86. return new_nsp;
  87. out_user:
  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 nsproxy *new_ns;
  111. int err = 0;
  112. if (!old_ns)
  113. return 0;
  114. get_nsproxy(old_ns);
  115. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER)))
  116. return 0;
  117. if (!capable(CAP_SYS_ADMIN)) {
  118. err = -EPERM;
  119. goto out;
  120. }
  121. new_ns = create_new_namespaces(flags, tsk, tsk->fs);
  122. if (IS_ERR(new_ns)) {
  123. err = PTR_ERR(new_ns);
  124. goto out;
  125. }
  126. tsk->nsproxy = new_ns;
  127. out:
  128. put_nsproxy(old_ns);
  129. return err;
  130. }
  131. void free_nsproxy(struct nsproxy *ns)
  132. {
  133. if (ns->mnt_ns)
  134. put_mnt_ns(ns->mnt_ns);
  135. if (ns->uts_ns)
  136. put_uts_ns(ns->uts_ns);
  137. if (ns->ipc_ns)
  138. put_ipc_ns(ns->ipc_ns);
  139. if (ns->pid_ns)
  140. put_pid_ns(ns->pid_ns);
  141. if (ns->user_ns)
  142. put_user_ns(ns->user_ns);
  143. kmem_cache_free(nsproxy_cachep, ns);
  144. }
  145. /*
  146. * Called from unshare. Unshare all the namespaces part of nsproxy.
  147. * On success, returns the new nsproxy.
  148. */
  149. int unshare_nsproxy_namespaces(unsigned long unshare_flags,
  150. struct nsproxy **new_nsp, struct fs_struct *new_fs)
  151. {
  152. int err = 0;
  153. if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  154. CLONE_NEWUSER)))
  155. return 0;
  156. if (!capable(CAP_SYS_ADMIN))
  157. return -EPERM;
  158. *new_nsp = create_new_namespaces(unshare_flags, current,
  159. new_fs ? new_fs : current->fs);
  160. if (IS_ERR(*new_nsp))
  161. err = PTR_ERR(*new_nsp);
  162. return err;
  163. }
  164. static int __init nsproxy_cache_init(void)
  165. {
  166. nsproxy_cachep = kmem_cache_create("nsproxy", sizeof(struct nsproxy),
  167. 0, SLAB_PANIC, NULL, NULL);
  168. return 0;
  169. }
  170. module_init(nsproxy_cache_init);