nsproxy.c 4.0 KB

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