utsname.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_fs.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 = proc_alloc_inum(&ns->proc_inum);
  40. if (err) {
  41. kfree(ns);
  42. return ERR_PTR(err);
  43. }
  44. down_read(&uts_sem);
  45. memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
  46. ns->user_ns = get_user_ns(user_ns);
  47. up_read(&uts_sem);
  48. return ns;
  49. }
  50. /*
  51. * Copy task tsk's utsname namespace, or clone it if flags
  52. * specifies CLONE_NEWUTS. In latter case, changes to the
  53. * utsname of this process won't be seen by parent, and vice
  54. * versa.
  55. */
  56. struct uts_namespace *copy_utsname(unsigned long flags,
  57. struct user_namespace *user_ns, struct uts_namespace *old_ns)
  58. {
  59. struct uts_namespace *new_ns;
  60. BUG_ON(!old_ns);
  61. get_uts_ns(old_ns);
  62. if (!(flags & CLONE_NEWUTS))
  63. return old_ns;
  64. new_ns = clone_uts_ns(user_ns, old_ns);
  65. put_uts_ns(old_ns);
  66. return new_ns;
  67. }
  68. void free_uts_ns(struct kref *kref)
  69. {
  70. struct uts_namespace *ns;
  71. ns = container_of(kref, struct uts_namespace, kref);
  72. put_user_ns(ns->user_ns);
  73. proc_free_inum(ns->proc_inum);
  74. kfree(ns);
  75. }
  76. static void *utsns_get(struct task_struct *task)
  77. {
  78. struct uts_namespace *ns = NULL;
  79. struct nsproxy *nsproxy;
  80. rcu_read_lock();
  81. nsproxy = task_nsproxy(task);
  82. if (nsproxy) {
  83. ns = nsproxy->uts_ns;
  84. get_uts_ns(ns);
  85. }
  86. rcu_read_unlock();
  87. return ns;
  88. }
  89. static void utsns_put(void *ns)
  90. {
  91. put_uts_ns(ns);
  92. }
  93. static int utsns_install(struct nsproxy *nsproxy, void *new)
  94. {
  95. struct uts_namespace *ns = new;
  96. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  97. !nsown_capable(CAP_SYS_ADMIN))
  98. return -EPERM;
  99. get_uts_ns(ns);
  100. put_uts_ns(nsproxy->uts_ns);
  101. nsproxy->uts_ns = ns;
  102. return 0;
  103. }
  104. static unsigned int utsns_inum(void *vp)
  105. {
  106. struct uts_namespace *ns = vp;
  107. return ns->proc_inum;
  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. .inum = utsns_inum,
  116. };