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