nsproxy.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. new_nsp = clone_nsproxy(tsk->nsproxy);
  55. if (!new_nsp)
  56. return ERR_PTR(-ENOMEM);
  57. new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, new_fs);
  58. if (IS_ERR(new_nsp->mnt_ns))
  59. goto out_ns;
  60. new_nsp->uts_ns = copy_utsname(flags, tsk->nsproxy->uts_ns);
  61. if (IS_ERR(new_nsp->uts_ns))
  62. goto out_uts;
  63. new_nsp->ipc_ns = copy_ipcs(flags, tsk->nsproxy->ipc_ns);
  64. if (IS_ERR(new_nsp->ipc_ns))
  65. goto out_ipc;
  66. new_nsp->pid_ns = copy_pid_ns(flags, tsk->nsproxy->pid_ns);
  67. if (IS_ERR(new_nsp->pid_ns))
  68. goto out_pid;
  69. return new_nsp;
  70. out_pid:
  71. if (new_nsp->ipc_ns)
  72. put_ipc_ns(new_nsp->ipc_ns);
  73. out_ipc:
  74. if (new_nsp->uts_ns)
  75. put_uts_ns(new_nsp->uts_ns);
  76. out_uts:
  77. if (new_nsp->mnt_ns)
  78. put_mnt_ns(new_nsp->mnt_ns);
  79. out_ns:
  80. kfree(new_nsp);
  81. return ERR_PTR(-ENOMEM);
  82. }
  83. /*
  84. * called from clone. This now handles copy for nsproxy and all
  85. * namespaces therein.
  86. */
  87. int copy_namespaces(int flags, struct task_struct *tsk)
  88. {
  89. struct nsproxy *old_ns = tsk->nsproxy;
  90. struct nsproxy *new_ns;
  91. int err = 0;
  92. if (!old_ns)
  93. return 0;
  94. get_nsproxy(old_ns);
  95. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
  96. return 0;
  97. if (!capable(CAP_SYS_ADMIN)) {
  98. err = -EPERM;
  99. goto out;
  100. }
  101. new_ns = create_new_namespaces(flags, tsk, tsk->fs);
  102. if (IS_ERR(new_ns)) {
  103. err = PTR_ERR(new_ns);
  104. goto out;
  105. }
  106. tsk->nsproxy = new_ns;
  107. out:
  108. put_nsproxy(old_ns);
  109. return err;
  110. }
  111. void free_nsproxy(struct nsproxy *ns)
  112. {
  113. if (ns->mnt_ns)
  114. put_mnt_ns(ns->mnt_ns);
  115. if (ns->uts_ns)
  116. put_uts_ns(ns->uts_ns);
  117. if (ns->ipc_ns)
  118. put_ipc_ns(ns->ipc_ns);
  119. if (ns->pid_ns)
  120. put_pid_ns(ns->pid_ns);
  121. kfree(ns);
  122. }
  123. /*
  124. * Called from unshare. Unshare all the namespaces part of nsproxy.
  125. * On sucess, returns the new nsproxy and a reference to old nsproxy
  126. * to make sure it stays around.
  127. */
  128. int unshare_nsproxy_namespaces(unsigned long unshare_flags,
  129. struct nsproxy **new_nsp, struct fs_struct *new_fs)
  130. {
  131. struct nsproxy *old_ns = current->nsproxy;
  132. int err = 0;
  133. if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
  134. return 0;
  135. #ifndef CONFIG_IPC_NS
  136. if (unshare_flags & CLONE_NEWIPC)
  137. return -EINVAL;
  138. #endif
  139. #ifndef CONFIG_UTS_NS
  140. if (unshare_flags & CLONE_NEWUTS)
  141. return -EINVAL;
  142. #endif
  143. if (!capable(CAP_SYS_ADMIN))
  144. return -EPERM;
  145. get_nsproxy(old_ns);
  146. *new_nsp = create_new_namespaces(unshare_flags, current,
  147. new_fs ? new_fs : current->fs);
  148. if (IS_ERR(*new_nsp)) {
  149. err = PTR_ERR(*new_nsp);
  150. put_nsproxy(old_ns);
  151. }
  152. return err;
  153. }