user_namespace.c 3.4 KB

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