utsname.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2004 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/export.h>
  12. #include <linux/uts.h>
  13. #include <linux/utsname.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/user_namespace.h>
  17. #include <linux/proc_ns.h>
  18. static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
  19. {
  20. return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
  21. }
  22. static void dec_uts_namespaces(struct ucounts *ucounts)
  23. {
  24. dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
  25. }
  26. static struct uts_namespace *create_uts_ns(void)
  27. {
  28. struct uts_namespace *uts_ns;
  29. uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
  30. if (uts_ns)
  31. kref_init(&uts_ns->kref);
  32. return uts_ns;
  33. }
  34. /*
  35. * Clone a new ns copying an original utsname, setting refcount to 1
  36. * @old_ns: namespace to clone
  37. * Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
  38. */
  39. static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
  40. struct uts_namespace *old_ns)
  41. {
  42. struct uts_namespace *ns;
  43. struct ucounts *ucounts;
  44. int err;
  45. err = -ENOSPC;
  46. ucounts = inc_uts_namespaces(user_ns);
  47. if (!ucounts)
  48. goto fail;
  49. err = -ENOMEM;
  50. ns = create_uts_ns();
  51. if (!ns)
  52. goto fail_dec;
  53. err = ns_alloc_inum(&ns->ns);
  54. if (err)
  55. goto fail_free;
  56. ns->ucounts = ucounts;
  57. ns->ns.ops = &utsns_operations;
  58. down_read(&uts_sem);
  59. memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
  60. ns->user_ns = get_user_ns(user_ns);
  61. up_read(&uts_sem);
  62. return ns;
  63. fail_free:
  64. kfree(ns);
  65. fail_dec:
  66. dec_uts_namespaces(ucounts);
  67. fail:
  68. return ERR_PTR(err);
  69. }
  70. /*
  71. * Copy task tsk's utsname namespace, or clone it if flags
  72. * specifies CLONE_NEWUTS. In latter case, changes to the
  73. * utsname of this process won't be seen by parent, and vice
  74. * versa.
  75. */
  76. struct uts_namespace *copy_utsname(unsigned long flags,
  77. struct user_namespace *user_ns, struct uts_namespace *old_ns)
  78. {
  79. struct uts_namespace *new_ns;
  80. BUG_ON(!old_ns);
  81. get_uts_ns(old_ns);
  82. if (!(flags & CLONE_NEWUTS))
  83. return old_ns;
  84. new_ns = clone_uts_ns(user_ns, old_ns);
  85. put_uts_ns(old_ns);
  86. return new_ns;
  87. }
  88. void free_uts_ns(struct kref *kref)
  89. {
  90. struct uts_namespace *ns;
  91. ns = container_of(kref, struct uts_namespace, kref);
  92. dec_uts_namespaces(ns->ucounts);
  93. put_user_ns(ns->user_ns);
  94. ns_free_inum(&ns->ns);
  95. kfree(ns);
  96. }
  97. static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
  98. {
  99. return container_of(ns, struct uts_namespace, ns);
  100. }
  101. static struct ns_common *utsns_get(struct task_struct *task)
  102. {
  103. struct uts_namespace *ns = NULL;
  104. struct nsproxy *nsproxy;
  105. task_lock(task);
  106. nsproxy = task->nsproxy;
  107. if (nsproxy) {
  108. ns = nsproxy->uts_ns;
  109. get_uts_ns(ns);
  110. }
  111. task_unlock(task);
  112. return ns ? &ns->ns : NULL;
  113. }
  114. static void utsns_put(struct ns_common *ns)
  115. {
  116. put_uts_ns(to_uts_ns(ns));
  117. }
  118. static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
  119. {
  120. struct uts_namespace *ns = to_uts_ns(new);
  121. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  122. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  123. return -EPERM;
  124. get_uts_ns(ns);
  125. put_uts_ns(nsproxy->uts_ns);
  126. nsproxy->uts_ns = ns;
  127. return 0;
  128. }
  129. static struct user_namespace *utsns_owner(struct ns_common *ns)
  130. {
  131. return to_uts_ns(ns)->user_ns;
  132. }
  133. const struct proc_ns_operations utsns_operations = {
  134. .name = "uts",
  135. .type = CLONE_NEWUTS,
  136. .get = utsns_get,
  137. .put = utsns_put,
  138. .install = utsns_install,
  139. .owner = utsns_owner,
  140. };