user.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * The "user cache".
  4. *
  5. * (C) Copyright 1991-2000 Linus Torvalds
  6. *
  7. * We have a per-user structure to keep track of how many
  8. * processes, files etc the user has claimed, in order to be
  9. * able to have per-user limits for system resources.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/bitops.h>
  15. #include <linux/key.h>
  16. #include <linux/sched/user.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/export.h>
  19. #include <linux/user_namespace.h>
  20. #include <linux/binfmts.h>
  21. #include <linux/proc_ns.h>
  22. #if IS_ENABLED(CONFIG_BINFMT_MISC)
  23. struct binfmt_misc init_binfmt_misc = {
  24. .entries = LIST_HEAD_INIT(init_binfmt_misc.entries),
  25. .enabled = true,
  26. .entries_lock = __RW_LOCK_UNLOCKED(init_binfmt_misc.entries_lock),
  27. };
  28. EXPORT_SYMBOL_GPL(init_binfmt_misc);
  29. #endif
  30. /*
  31. * userns count is 1 for root user, 1 for init_uts_ns,
  32. * and 1 for... ?
  33. */
  34. struct user_namespace init_user_ns = {
  35. .uid_map = {
  36. {
  37. .extent[0] = {
  38. .first = 0,
  39. .lower_first = 0,
  40. .count = 4294967295U,
  41. },
  42. .nr_extents = 1,
  43. },
  44. },
  45. .gid_map = {
  46. {
  47. .extent[0] = {
  48. .first = 0,
  49. .lower_first = 0,
  50. .count = 4294967295U,
  51. },
  52. .nr_extents = 1,
  53. },
  54. },
  55. .projid_map = {
  56. {
  57. .extent[0] = {
  58. .first = 0,
  59. .lower_first = 0,
  60. .count = 4294967295U,
  61. },
  62. .nr_extents = 1,
  63. },
  64. },
  65. .ns.count = REFCOUNT_INIT(3),
  66. .owner = GLOBAL_ROOT_UID,
  67. .group = GLOBAL_ROOT_GID,
  68. .ns.inum = PROC_USER_INIT_INO,
  69. #ifdef CONFIG_USER_NS
  70. .ns.ops = &userns_operations,
  71. #endif
  72. .flags = USERNS_INIT_FLAGS,
  73. #ifdef CONFIG_KEYS
  74. .keyring_name_list = LIST_HEAD_INIT(init_user_ns.keyring_name_list),
  75. .keyring_sem = __RWSEM_INITIALIZER(init_user_ns.keyring_sem),
  76. #endif
  77. #if IS_ENABLED(CONFIG_BINFMT_MISC)
  78. .binfmt_misc = &init_binfmt_misc,
  79. #endif
  80. };
  81. EXPORT_SYMBOL_GPL(init_user_ns);
  82. /*
  83. * UID task count cache, to get fast user lookup in "alloc_uid"
  84. * when changing user ID's (ie setuid() and friends).
  85. */
  86. #define UIDHASH_BITS (IS_ENABLED(CONFIG_BASE_SMALL) ? 3 : 7)
  87. #define UIDHASH_SZ (1 << UIDHASH_BITS)
  88. #define UIDHASH_MASK (UIDHASH_SZ - 1)
  89. #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
  90. #define uidhashentry(uid) (uidhash_table + __uidhashfn((__kuid_val(uid))))
  91. static struct kmem_cache *uid_cachep;
  92. static struct hlist_head uidhash_table[UIDHASH_SZ];
  93. /*
  94. * The uidhash_lock is mostly taken from process context, but it is
  95. * occasionally also taken from softirq/tasklet context, when
  96. * task-structs get RCU-freed. Hence all locking must be softirq-safe.
  97. * But free_uid() is also called with local interrupts disabled, and running
  98. * local_bh_enable() with local interrupts disabled is an error - we'll run
  99. * softirq callbacks, and they can unconditionally enable interrupts, and
  100. * the caller of free_uid() didn't expect that..
  101. */
  102. static DEFINE_SPINLOCK(uidhash_lock);
  103. /* root_user.__count is 1, for init task cred */
  104. struct user_struct root_user = {
  105. .__count = REFCOUNT_INIT(1),
  106. .uid = GLOBAL_ROOT_UID,
  107. .ratelimit = RATELIMIT_STATE_INIT(root_user.ratelimit, 0, 0),
  108. };
  109. /*
  110. * These routines must be called with the uidhash spinlock held!
  111. */
  112. static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
  113. {
  114. hlist_add_head(&up->uidhash_node, hashent);
  115. }
  116. static void uid_hash_remove(struct user_struct *up)
  117. {
  118. hlist_del_init(&up->uidhash_node);
  119. }
  120. static struct user_struct *uid_hash_find(kuid_t uid, struct hlist_head *hashent)
  121. {
  122. struct user_struct *user;
  123. hlist_for_each_entry(user, hashent, uidhash_node) {
  124. if (uid_eq(user->uid, uid)) {
  125. refcount_inc(&user->__count);
  126. return user;
  127. }
  128. }
  129. return NULL;
  130. }
  131. static int user_epoll_alloc(struct user_struct *up)
  132. {
  133. #ifdef CONFIG_EPOLL
  134. return percpu_counter_init(&up->epoll_watches, 0, GFP_KERNEL);
  135. #else
  136. return 0;
  137. #endif
  138. }
  139. static void user_epoll_free(struct user_struct *up)
  140. {
  141. #ifdef CONFIG_EPOLL
  142. percpu_counter_destroy(&up->epoll_watches);
  143. #endif
  144. }
  145. /* IRQs are disabled and uidhash_lock is held upon function entry.
  146. * IRQ state (as stored in flags) is restored and uidhash_lock released
  147. * upon function exit.
  148. */
  149. static void free_user(struct user_struct *up, unsigned long flags)
  150. __releases(&uidhash_lock)
  151. {
  152. uid_hash_remove(up);
  153. spin_unlock_irqrestore(&uidhash_lock, flags);
  154. user_epoll_free(up);
  155. kmem_cache_free(uid_cachep, up);
  156. }
  157. /*
  158. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  159. * caller must undo that ref with free_uid().
  160. *
  161. * If the user_struct could not be found, return NULL.
  162. */
  163. struct user_struct *find_user(kuid_t uid)
  164. {
  165. struct user_struct *ret;
  166. unsigned long flags;
  167. spin_lock_irqsave(&uidhash_lock, flags);
  168. ret = uid_hash_find(uid, uidhashentry(uid));
  169. spin_unlock_irqrestore(&uidhash_lock, flags);
  170. return ret;
  171. }
  172. void free_uid(struct user_struct *up)
  173. {
  174. unsigned long flags;
  175. if (!up)
  176. return;
  177. if (refcount_dec_and_lock_irqsave(&up->__count, &uidhash_lock, &flags))
  178. free_user(up, flags);
  179. }
  180. EXPORT_SYMBOL_GPL(free_uid);
  181. struct user_struct *alloc_uid(kuid_t uid)
  182. {
  183. struct hlist_head *hashent = uidhashentry(uid);
  184. struct user_struct *up, *new;
  185. spin_lock_irq(&uidhash_lock);
  186. up = uid_hash_find(uid, hashent);
  187. spin_unlock_irq(&uidhash_lock);
  188. if (!up) {
  189. new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
  190. if (!new)
  191. return NULL;
  192. new->uid = uid;
  193. refcount_set(&new->__count, 1);
  194. if (user_epoll_alloc(new)) {
  195. kmem_cache_free(uid_cachep, new);
  196. return NULL;
  197. }
  198. ratelimit_state_init(&new->ratelimit, HZ, 100);
  199. ratelimit_set_flags(&new->ratelimit, RATELIMIT_MSG_ON_RELEASE);
  200. /*
  201. * Before adding this, check whether we raced
  202. * on adding the same user already..
  203. */
  204. spin_lock_irq(&uidhash_lock);
  205. up = uid_hash_find(uid, hashent);
  206. if (up) {
  207. user_epoll_free(new);
  208. kmem_cache_free(uid_cachep, new);
  209. } else {
  210. uid_hash_insert(new, hashent);
  211. up = new;
  212. }
  213. spin_unlock_irq(&uidhash_lock);
  214. }
  215. return up;
  216. }
  217. static int __init uid_cache_init(void)
  218. {
  219. int n;
  220. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  221. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  222. for(n = 0; n < UIDHASH_SZ; ++n)
  223. INIT_HLIST_HEAD(uidhash_table + n);
  224. if (user_epoll_alloc(&root_user))
  225. panic("root_user epoll percpu counter alloc failed");
  226. /* Insert the root user immediately (init already runs as root) */
  227. spin_lock_irq(&uidhash_lock);
  228. uid_hash_insert(&root_user, uidhashentry(GLOBAL_ROOT_UID));
  229. spin_unlock_irq(&uidhash_lock);
  230. return 0;
  231. }
  232. subsys_initcall(uid_cache_init);