utsname.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 NULL on error (failure to kmalloc), new ns otherwise
  30. */
  31. static struct uts_namespace *clone_uts_ns(struct task_struct *tsk,
  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(task_cred_xxx(tsk, 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 task_struct *tsk)
  58. {
  59. struct uts_namespace *old_ns = tsk->nsproxy->uts_ns;
  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(tsk, 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. proc_free_inum(ns->proc_inum);
  75. kfree(ns);
  76. }
  77. static void *utsns_get(struct task_struct *task)
  78. {
  79. struct uts_namespace *ns = NULL;
  80. struct nsproxy *nsproxy;
  81. task_lock(task);
  82. nsproxy = task->nsproxy;
  83. if (nsproxy) {
  84. ns = nsproxy->uts_ns;
  85. get_uts_ns(ns);
  86. }
  87. task_unlock(task);
  88. return ns;
  89. }
  90. static void utsns_put(void *ns)
  91. {
  92. put_uts_ns(ns);
  93. }
  94. static int utsns_install(struct nsproxy *nsproxy, void *new)
  95. {
  96. struct uts_namespace *ns = new;
  97. if (!ns_capable(ns->user_ns, 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. };