nsproxy.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <linux/module.h>
  12. #include <linux/version.h>
  13. #include <linux/nsproxy.h>
  14. #include <linux/init_task.h>
  15. #include <linux/namespace.h>
  16. #include <linux/utsname.h>
  17. struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
  18. static inline void get_nsproxy(struct nsproxy *ns)
  19. {
  20. atomic_inc(&ns->count);
  21. }
  22. void get_task_namespaces(struct task_struct *tsk)
  23. {
  24. struct nsproxy *ns = tsk->nsproxy;
  25. if (ns) {
  26. get_nsproxy(ns);
  27. }
  28. }
  29. /*
  30. * creates a copy of "orig" with refcount 1.
  31. * This does not grab references to the contained namespaces,
  32. * so that needs to be done by dup_namespaces.
  33. */
  34. static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
  35. {
  36. struct nsproxy *ns;
  37. ns = kmalloc(sizeof(struct nsproxy), GFP_KERNEL);
  38. if (ns) {
  39. memcpy(ns, orig, sizeof(struct nsproxy));
  40. atomic_set(&ns->count, 1);
  41. }
  42. return ns;
  43. }
  44. /*
  45. * copies the nsproxy, setting refcount to 1, and grabbing a
  46. * reference to all contained namespaces. Called from
  47. * sys_unshare()
  48. */
  49. struct nsproxy *dup_namespaces(struct nsproxy *orig)
  50. {
  51. struct nsproxy *ns = clone_namespaces(orig);
  52. if (ns) {
  53. if (ns->namespace)
  54. get_namespace(ns->namespace);
  55. if (ns->uts_ns)
  56. get_uts_ns(ns->uts_ns);
  57. }
  58. return ns;
  59. }
  60. /*
  61. * called from clone. This now handles copy for nsproxy and all
  62. * namespaces therein.
  63. */
  64. int copy_namespaces(int flags, struct task_struct *tsk)
  65. {
  66. struct nsproxy *old_ns = tsk->nsproxy;
  67. struct nsproxy *new_ns;
  68. int err = 0;
  69. if (!old_ns)
  70. return 0;
  71. get_nsproxy(old_ns);
  72. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS)))
  73. return 0;
  74. new_ns = clone_namespaces(old_ns);
  75. if (!new_ns) {
  76. err = -ENOMEM;
  77. goto out;
  78. }
  79. tsk->nsproxy = new_ns;
  80. err = copy_namespace(flags, tsk);
  81. if (err) {
  82. tsk->nsproxy = old_ns;
  83. put_nsproxy(new_ns);
  84. goto out;
  85. }
  86. err = copy_utsname(flags, tsk);
  87. if (err) {
  88. if (new_ns->namespace)
  89. put_namespace(new_ns->namespace);
  90. tsk->nsproxy = old_ns;
  91. put_nsproxy(new_ns);
  92. goto out;
  93. }
  94. out:
  95. put_nsproxy(old_ns);
  96. return err;
  97. }
  98. void free_nsproxy(struct nsproxy *ns)
  99. {
  100. if (ns->namespace)
  101. put_namespace(ns->namespace);
  102. if (ns->uts_ns)
  103. put_uts_ns(ns->uts_ns);
  104. kfree(ns);
  105. }