utsname.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004 IBM Corporation
  4. *
  5. * Author: Serge Hallyn <serue@us.ibm.com>
  6. */
  7. #include <linux/export.h>
  8. #include <linux/uts.h>
  9. #include <linux/utsname.h>
  10. #include <linux/err.h>
  11. #include <linux/slab.h>
  12. #include <linux/cred.h>
  13. #include <linux/user_namespace.h>
  14. #include <linux/proc_ns.h>
  15. #include <linux/sched/task.h>
  16. static struct kmem_cache *uts_ns_cache __ro_after_init;
  17. static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
  18. {
  19. return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
  20. }
  21. static void dec_uts_namespaces(struct ucounts *ucounts)
  22. {
  23. dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
  24. }
  25. static struct uts_namespace *create_uts_ns(void)
  26. {
  27. struct uts_namespace *uts_ns;
  28. uts_ns = kmem_cache_alloc(uts_ns_cache, GFP_KERNEL);
  29. if (uts_ns)
  30. kref_init(&uts_ns->kref);
  31. return uts_ns;
  32. }
  33. /*
  34. * Clone a new ns copying an original utsname, setting refcount to 1
  35. * @old_ns: namespace to clone
  36. * Return ERR_PTR(-ENOMEM) on error (failure to allocate), new ns otherwise
  37. */
  38. static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
  39. struct uts_namespace *old_ns)
  40. {
  41. struct uts_namespace *ns;
  42. struct ucounts *ucounts;
  43. int err;
  44. err = -ENOSPC;
  45. ucounts = inc_uts_namespaces(user_ns);
  46. if (!ucounts)
  47. goto fail;
  48. err = -ENOMEM;
  49. ns = create_uts_ns();
  50. if (!ns)
  51. goto fail_dec;
  52. err = ns_alloc_inum(&ns->ns);
  53. if (err)
  54. goto fail_free;
  55. ns->ucounts = ucounts;
  56. ns->ns.ops = &utsns_operations;
  57. down_read(&uts_sem);
  58. memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
  59. ns->user_ns = get_user_ns(user_ns);
  60. up_read(&uts_sem);
  61. return ns;
  62. fail_free:
  63. kmem_cache_free(uts_ns_cache, ns);
  64. fail_dec:
  65. dec_uts_namespaces(ucounts);
  66. fail:
  67. return ERR_PTR(err);
  68. }
  69. /*
  70. * Copy task tsk's utsname namespace, or clone it if flags
  71. * specifies CLONE_NEWUTS. In latter case, changes to the
  72. * utsname of this process won't be seen by parent, and vice
  73. * versa.
  74. */
  75. struct uts_namespace *copy_utsname(unsigned long flags,
  76. struct user_namespace *user_ns, struct uts_namespace *old_ns)
  77. {
  78. struct uts_namespace *new_ns;
  79. BUG_ON(!old_ns);
  80. get_uts_ns(old_ns);
  81. if (!(flags & CLONE_NEWUTS))
  82. return old_ns;
  83. new_ns = clone_uts_ns(user_ns, old_ns);
  84. put_uts_ns(old_ns);
  85. return new_ns;
  86. }
  87. void free_uts_ns(struct kref *kref)
  88. {
  89. struct uts_namespace *ns;
  90. ns = container_of(kref, struct uts_namespace, kref);
  91. dec_uts_namespaces(ns->ucounts);
  92. put_user_ns(ns->user_ns);
  93. ns_free_inum(&ns->ns);
  94. kmem_cache_free(uts_ns_cache, ns);
  95. }
  96. static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
  97. {
  98. return container_of(ns, struct uts_namespace, ns);
  99. }
  100. static struct ns_common *utsns_get(struct task_struct *task)
  101. {
  102. struct uts_namespace *ns = NULL;
  103. struct nsproxy *nsproxy;
  104. task_lock(task);
  105. nsproxy = task->nsproxy;
  106. if (nsproxy) {
  107. ns = nsproxy->uts_ns;
  108. get_uts_ns(ns);
  109. }
  110. task_unlock(task);
  111. return ns ? &ns->ns : NULL;
  112. }
  113. static void utsns_put(struct ns_common *ns)
  114. {
  115. put_uts_ns(to_uts_ns(ns));
  116. }
  117. static int utsns_install(struct nsset *nsset, struct ns_common *new)
  118. {
  119. struct nsproxy *nsproxy = nsset->nsproxy;
  120. struct uts_namespace *ns = to_uts_ns(new);
  121. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  122. !ns_capable(nsset->cred->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. };
  141. void __init uts_ns_init(void)
  142. {
  143. uts_ns_cache = kmem_cache_create_usercopy(
  144. "uts_namespace", sizeof(struct uts_namespace), 0,
  145. SLAB_PANIC|SLAB_ACCOUNT,
  146. offsetof(struct uts_namespace, name),
  147. sizeof_field(struct uts_namespace, name),
  148. NULL);
  149. }