utsname.c 3.6 KB

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