user_namespace.c 3.2 KB

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