utsname.c 3.0 KB

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