utsname.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004 IBM Corporation
  4. *
  5. * Author: Serge Hallyn <[email protected]>
  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. refcount_set(&uts_ns->ns.count, 1);
  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 uts_namespace *ns)
  88. {
  89. dec_uts_namespaces(ns->ucounts);
  90. put_user_ns(ns->user_ns);
  91. ns_free_inum(&ns->ns);
  92. kmem_cache_free(uts_ns_cache, ns);
  93. }
  94. static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
  95. {
  96. return container_of(ns, struct uts_namespace, ns);
  97. }
  98. static struct ns_common *utsns_get(struct task_struct *task)
  99. {
  100. struct uts_namespace *ns = NULL;
  101. struct nsproxy *nsproxy;
  102. task_lock(task);
  103. nsproxy = task->nsproxy;
  104. if (nsproxy) {
  105. ns = nsproxy->uts_ns;
  106. get_uts_ns(ns);
  107. }
  108. task_unlock(task);
  109. return ns ? &ns->ns : NULL;
  110. }
  111. static void utsns_put(struct ns_common *ns)
  112. {
  113. put_uts_ns(to_uts_ns(ns));
  114. }
  115. static int utsns_install(struct nsset *nsset, struct ns_common *new)
  116. {
  117. struct nsproxy *nsproxy = nsset->nsproxy;
  118. struct uts_namespace *ns = to_uts_ns(new);
  119. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  120. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  121. return -EPERM;
  122. get_uts_ns(ns);
  123. put_uts_ns(nsproxy->uts_ns);
  124. nsproxy->uts_ns = ns;
  125. return 0;
  126. }
  127. static struct user_namespace *utsns_owner(struct ns_common *ns)
  128. {
  129. return to_uts_ns(ns)->user_ns;
  130. }
  131. const struct proc_ns_operations utsns_operations = {
  132. .name = "uts",
  133. .type = CLONE_NEWUTS,
  134. .get = utsns_get,
  135. .put = utsns_put,
  136. .install = utsns_install,
  137. .owner = utsns_owner,
  138. };
  139. void __init uts_ns_init(void)
  140. {
  141. uts_ns_cache = kmem_cache_create_usercopy(
  142. "uts_namespace", sizeof(struct uts_namespace), 0,
  143. SLAB_PANIC|SLAB_ACCOUNT,
  144. offsetof(struct uts_namespace, name),
  145. sizeof_field(struct uts_namespace, name),
  146. NULL);
  147. }