nsproxy.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * This does not grab references to the contained namespaces,
  37. * so that needs to be done by dup_namespaces.
  38. */
  39. static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
  40. {
  41. struct nsproxy *ns;
  42. ns = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL);
  43. if (ns)
  44. atomic_set(&ns->count, 1);
  45. return ns;
  46. }
  47. /*
  48. * copies the nsproxy, setting refcount to 1, and grabbing a
  49. * reference to all contained namespaces. Called from
  50. * sys_unshare()
  51. */
  52. struct nsproxy *dup_namespaces(struct nsproxy *orig)
  53. {
  54. struct nsproxy *ns = clone_namespaces(orig);
  55. if (ns) {
  56. if (ns->mnt_ns)
  57. get_mnt_ns(ns->mnt_ns);
  58. if (ns->uts_ns)
  59. get_uts_ns(ns->uts_ns);
  60. if (ns->ipc_ns)
  61. get_ipc_ns(ns->ipc_ns);
  62. if (ns->pid_ns)
  63. get_pid_ns(ns->pid_ns);
  64. }
  65. return ns;
  66. }
  67. /*
  68. * called from clone. This now handles copy for nsproxy and all
  69. * namespaces therein.
  70. */
  71. int copy_namespaces(int flags, struct task_struct *tsk)
  72. {
  73. struct nsproxy *old_ns = tsk->nsproxy;
  74. struct nsproxy *new_ns;
  75. int err = 0;
  76. if (!old_ns)
  77. return 0;
  78. get_nsproxy(old_ns);
  79. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
  80. return 0;
  81. new_ns = clone_namespaces(old_ns);
  82. if (!new_ns) {
  83. err = -ENOMEM;
  84. goto out;
  85. }
  86. tsk->nsproxy = new_ns;
  87. err = copy_mnt_ns(flags, tsk);
  88. if (err)
  89. goto out_ns;
  90. err = copy_utsname(flags, tsk);
  91. if (err)
  92. goto out_uts;
  93. err = copy_ipcs(flags, tsk);
  94. if (err)
  95. goto out_ipc;
  96. err = copy_pid_ns(flags, tsk);
  97. if (err)
  98. goto out_pid;
  99. out:
  100. put_nsproxy(old_ns);
  101. return err;
  102. out_pid:
  103. if (new_ns->ipc_ns)
  104. put_ipc_ns(new_ns->ipc_ns);
  105. out_ipc:
  106. if (new_ns->uts_ns)
  107. put_uts_ns(new_ns->uts_ns);
  108. out_uts:
  109. if (new_ns->mnt_ns)
  110. put_mnt_ns(new_ns->mnt_ns);
  111. out_ns:
  112. tsk->nsproxy = old_ns;
  113. kfree(new_ns);
  114. goto out;
  115. }
  116. void free_nsproxy(struct nsproxy *ns)
  117. {
  118. if (ns->mnt_ns)
  119. put_mnt_ns(ns->mnt_ns);
  120. if (ns->uts_ns)
  121. put_uts_ns(ns->uts_ns);
  122. if (ns->ipc_ns)
  123. put_ipc_ns(ns->ipc_ns);
  124. if (ns->pid_ns)
  125. put_pid_ns(ns->pid_ns);
  126. kfree(ns);
  127. }