user_namespace.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <linux/securebits.h>
  15. static struct kmem_cache *user_ns_cachep __read_mostly;
  16. /*
  17. * Create a new user namespace, deriving the creator from the user in the
  18. * passed credentials, and replacing that user with the new root user for the
  19. * new namespace.
  20. *
  21. * This is called by copy_creds(), which will finish setting the target task's
  22. * credentials.
  23. */
  24. int create_user_ns(struct cred *new)
  25. {
  26. struct user_namespace *ns, *parent_ns = new->user_ns;
  27. struct user_struct *root_user;
  28. int n;
  29. int ret;
  30. ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
  31. if (!ns)
  32. return -ENOMEM;
  33. ret = proc_alloc_inum(&ns->proc_inum);
  34. if (ret) {
  35. kmem_cache_free(user_ns_cachep, ns);
  36. return ret;
  37. }
  38. kref_init(&ns->kref);
  39. for (n = 0; n < UIDHASH_SZ; ++n)
  40. INIT_HLIST_HEAD(ns->uidhash_table + n);
  41. /* Alloc new root user. */
  42. root_user = alloc_uid(ns, 0);
  43. if (!root_user) {
  44. kmem_cache_free(user_ns_cachep, ns);
  45. return -ENOMEM;
  46. }
  47. /* set the new root user in the credentials under preparation */
  48. ns->parent = parent_ns;
  49. ns->creator = new->user;
  50. new->user = root_user;
  51. new->uid = new->euid = new->suid = new->fsuid = 0;
  52. new->gid = new->egid = new->sgid = new->fsgid = 0;
  53. put_group_info(new->group_info);
  54. new->group_info = get_group_info(&init_groups);
  55. /* Start with the same capabilities as init but useless for doing
  56. * anything as the capabilities are bound to the new user namespace.
  57. */
  58. new->securebits = SECUREBITS_DEFAULT;
  59. new->cap_inheritable = CAP_EMPTY_SET;
  60. new->cap_permitted = CAP_FULL_SET;
  61. new->cap_effective = CAP_FULL_SET;
  62. new->cap_bset = CAP_FULL_SET;
  63. #ifdef CONFIG_KEYS
  64. key_put(new->request_key_auth);
  65. new->request_key_auth = NULL;
  66. #endif
  67. /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
  68. /* Leave the reference to our user_ns with the new cred */
  69. new->user_ns = ns;
  70. return 0;
  71. }
  72. /*
  73. * Deferred destructor for a user namespace. This is required because
  74. * free_user_ns() may be called with uidhash_lock held, but we need to call
  75. * back to free_uid() which will want to take the lock again.
  76. */
  77. static void free_user_ns_work(struct work_struct *work)
  78. {
  79. struct user_namespace *parent, *ns =
  80. container_of(work, struct user_namespace, destroyer);
  81. parent = ns->parent;
  82. free_uid(ns->creator);
  83. proc_free_inum(ns->proc_inum);
  84. kmem_cache_free(user_ns_cachep, ns);
  85. put_user_ns(parent);
  86. }
  87. void free_user_ns(struct kref *kref)
  88. {
  89. struct user_namespace *ns =
  90. container_of(kref, struct user_namespace, kref);
  91. INIT_WORK(&ns->destroyer, free_user_ns_work);
  92. schedule_work(&ns->destroyer);
  93. }
  94. EXPORT_SYMBOL(free_user_ns);
  95. uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
  96. {
  97. struct user_namespace *tmp;
  98. if (likely(to == cred->user_ns))
  99. return uid;
  100. /* Is cred->user the creator of the target user_ns
  101. * or the creator of one of it's parents?
  102. */
  103. for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
  104. if (cred->user == tmp->creator) {
  105. return (uid_t)0;
  106. }
  107. }
  108. /* No useful relationship so no mapping */
  109. return overflowuid;
  110. }
  111. gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
  112. {
  113. struct user_namespace *tmp;
  114. if (likely(to == cred->user_ns))
  115. return gid;
  116. /* Is cred->user the creator of the target user_ns
  117. * or the creator of one of it's parents?
  118. */
  119. for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
  120. if (cred->user == tmp->creator) {
  121. return (gid_t)0;
  122. }
  123. }
  124. /* No useful relationship so no mapping */
  125. return overflowgid;
  126. }
  127. static __init int user_namespaces_init(void)
  128. {
  129. user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
  130. return 0;
  131. }
  132. module_init(user_namespaces_init);