user_namespace.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License as
  4. * published by the Free Software Foundation, version 2 of the
  5. * License.
  6. */
  7. #include <linux/export.h>
  8. #include <linux/nsproxy.h>
  9. #include <linux/slab.h>
  10. #include <linux/user_namespace.h>
  11. #include <linux/highuid.h>
  12. #include <linux/cred.h>
  13. #include <linux/proc_fs.h>
  14. static struct kmem_cache *user_ns_cachep __read_mostly;
  15. /*
  16. * Create a new user namespace, deriving the creator from the user in the
  17. * passed credentials, and replacing that user with the new root user for the
  18. * new namespace.
  19. *
  20. * This is called by copy_creds(), which will finish setting the target task's
  21. * credentials.
  22. */
  23. int create_user_ns(struct cred *new)
  24. {
  25. struct user_namespace *ns;
  26. struct user_struct *root_user;
  27. int n;
  28. int ret;
  29. ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
  30. if (!ns)
  31. return -ENOMEM;
  32. ret = proc_alloc_inum(&ns->proc_inum);
  33. if (ret) {
  34. kmem_cache_free(user_ns_cachep, ns);
  35. return ret;
  36. }
  37. kref_init(&ns->kref);
  38. for (n = 0; n < UIDHASH_SZ; ++n)
  39. INIT_HLIST_HEAD(ns->uidhash_table + n);
  40. /* Alloc new root user. */
  41. root_user = alloc_uid(ns, 0);
  42. if (!root_user) {
  43. kmem_cache_free(user_ns_cachep, ns);
  44. return -ENOMEM;
  45. }
  46. /* set the new root user in the credentials under preparation */
  47. ns->creator = new->user;
  48. new->user = root_user;
  49. new->uid = new->euid = new->suid = new->fsuid = 0;
  50. new->gid = new->egid = new->sgid = new->fsgid = 0;
  51. put_group_info(new->group_info);
  52. new->group_info = get_group_info(&init_groups);
  53. #ifdef CONFIG_KEYS
  54. key_put(new->request_key_auth);
  55. new->request_key_auth = NULL;
  56. #endif
  57. /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
  58. /* root_user holds a reference to ns, our reference can be dropped */
  59. put_user_ns(ns);
  60. return 0;
  61. }
  62. /*
  63. * Deferred destructor for a user namespace. This is required because
  64. * free_user_ns() may be called with uidhash_lock held, but we need to call
  65. * back to free_uid() which will want to take the lock again.
  66. */
  67. static void free_user_ns_work(struct work_struct *work)
  68. {
  69. struct user_namespace *ns =
  70. container_of(work, struct user_namespace, destroyer);
  71. free_uid(ns->creator);
  72. proc_free_inum(ns->proc_inum);
  73. kmem_cache_free(user_ns_cachep, ns);
  74. }
  75. void free_user_ns(struct kref *kref)
  76. {
  77. struct user_namespace *ns =
  78. container_of(kref, struct user_namespace, kref);
  79. INIT_WORK(&ns->destroyer, free_user_ns_work);
  80. schedule_work(&ns->destroyer);
  81. }
  82. EXPORT_SYMBOL(free_user_ns);
  83. uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
  84. {
  85. struct user_namespace *tmp;
  86. if (likely(to == cred->user->user_ns))
  87. return uid;
  88. /* Is cred->user the creator of the target user_ns
  89. * or the creator of one of it's parents?
  90. */
  91. for ( tmp = to; tmp != &init_user_ns;
  92. tmp = tmp->creator->user_ns ) {
  93. if (cred->user == tmp->creator) {
  94. return (uid_t)0;
  95. }
  96. }
  97. /* No useful relationship so no mapping */
  98. return overflowuid;
  99. }
  100. gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
  101. {
  102. struct user_namespace *tmp;
  103. if (likely(to == cred->user->user_ns))
  104. return gid;
  105. /* Is cred->user the creator of the target user_ns
  106. * or the creator of one of it's parents?
  107. */
  108. for ( tmp = to; tmp != &init_user_ns;
  109. tmp = tmp->creator->user_ns ) {
  110. if (cred->user == tmp->creator) {
  111. return (gid_t)0;
  112. }
  113. }
  114. /* No useful relationship so no mapping */
  115. return overflowgid;
  116. }
  117. static __init int user_namespaces_init(void)
  118. {
  119. user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
  120. return 0;
  121. }
  122. module_init(user_namespaces_init);