utsname.c 4.0 KB

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