nsproxy.c 2.9 KB

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