user.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * The "user cache".
  3. *
  4. * (C) Copyright 1991-2000 Linus Torvalds
  5. *
  6. * We have a per-user structure to keep track of how many
  7. * processes, files etc the user has claimed, in order to be
  8. * able to have per-user limits for system resources.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/bitops.h>
  14. #include <linux/key.h>
  15. #include <linux/interrupt.h>
  16. /*
  17. * UID task count cache, to get fast user lookup in "alloc_uid"
  18. * when changing user ID's (ie setuid() and friends).
  19. */
  20. #define UIDHASH_BITS (CONFIG_BASE_SMALL ? 3 : 8)
  21. #define UIDHASH_SZ (1 << UIDHASH_BITS)
  22. #define UIDHASH_MASK (UIDHASH_SZ - 1)
  23. #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
  24. #define uidhashentry(uid) (uidhash_table + __uidhashfn((uid)))
  25. static struct kmem_cache *uid_cachep;
  26. static struct list_head uidhash_table[UIDHASH_SZ];
  27. /*
  28. * The uidhash_lock is mostly taken from process context, but it is
  29. * occasionally also taken from softirq/tasklet context, when
  30. * task-structs get RCU-freed. Hence all locking must be softirq-safe.
  31. * But free_uid() is also called with local interrupts disabled, and running
  32. * local_bh_enable() with local interrupts disabled is an error - we'll run
  33. * softirq callbacks, and they can unconditionally enable interrupts, and
  34. * the caller of free_uid() didn't expect that..
  35. */
  36. static DEFINE_SPINLOCK(uidhash_lock);
  37. struct user_struct root_user = {
  38. .__count = ATOMIC_INIT(1),
  39. .processes = ATOMIC_INIT(1),
  40. .files = ATOMIC_INIT(0),
  41. .sigpending = ATOMIC_INIT(0),
  42. .mq_bytes = 0,
  43. .locked_shm = 0,
  44. #ifdef CONFIG_KEYS
  45. .uid_keyring = &root_user_keyring,
  46. .session_keyring = &root_session_keyring,
  47. #endif
  48. };
  49. /*
  50. * These routines must be called with the uidhash spinlock held!
  51. */
  52. static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent)
  53. {
  54. list_add(&up->uidhash_list, hashent);
  55. }
  56. static inline void uid_hash_remove(struct user_struct *up)
  57. {
  58. list_del(&up->uidhash_list);
  59. }
  60. static inline struct user_struct *uid_hash_find(uid_t uid, struct list_head *hashent)
  61. {
  62. struct list_head *up;
  63. list_for_each(up, hashent) {
  64. struct user_struct *user;
  65. user = list_entry(up, struct user_struct, uidhash_list);
  66. if(user->uid == uid) {
  67. atomic_inc(&user->__count);
  68. return user;
  69. }
  70. }
  71. return NULL;
  72. }
  73. /*
  74. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  75. * caller must undo that ref with free_uid().
  76. *
  77. * If the user_struct could not be found, return NULL.
  78. */
  79. struct user_struct *find_user(uid_t uid)
  80. {
  81. struct user_struct *ret;
  82. unsigned long flags;
  83. spin_lock_irqsave(&uidhash_lock, flags);
  84. ret = uid_hash_find(uid, uidhashentry(uid));
  85. spin_unlock_irqrestore(&uidhash_lock, flags);
  86. return ret;
  87. }
  88. void free_uid(struct user_struct *up)
  89. {
  90. unsigned long flags;
  91. if (!up)
  92. return;
  93. local_irq_save(flags);
  94. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  95. uid_hash_remove(up);
  96. spin_unlock_irqrestore(&uidhash_lock, flags);
  97. key_put(up->uid_keyring);
  98. key_put(up->session_keyring);
  99. kmem_cache_free(uid_cachep, up);
  100. } else {
  101. local_irq_restore(flags);
  102. }
  103. }
  104. struct user_struct * alloc_uid(uid_t uid)
  105. {
  106. struct list_head *hashent = uidhashentry(uid);
  107. struct user_struct *up;
  108. spin_lock_irq(&uidhash_lock);
  109. up = uid_hash_find(uid, hashent);
  110. spin_unlock_irq(&uidhash_lock);
  111. if (!up) {
  112. struct user_struct *new;
  113. new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
  114. if (!new)
  115. return NULL;
  116. new->uid = uid;
  117. atomic_set(&new->__count, 1);
  118. atomic_set(&new->processes, 0);
  119. atomic_set(&new->files, 0);
  120. atomic_set(&new->sigpending, 0);
  121. #ifdef CONFIG_INOTIFY_USER
  122. atomic_set(&new->inotify_watches, 0);
  123. atomic_set(&new->inotify_devs, 0);
  124. #endif
  125. new->mq_bytes = 0;
  126. new->locked_shm = 0;
  127. if (alloc_uid_keyring(new, current) < 0) {
  128. kmem_cache_free(uid_cachep, new);
  129. return NULL;
  130. }
  131. /*
  132. * Before adding this, check whether we raced
  133. * on adding the same user already..
  134. */
  135. spin_lock_irq(&uidhash_lock);
  136. up = uid_hash_find(uid, hashent);
  137. if (up) {
  138. key_put(new->uid_keyring);
  139. key_put(new->session_keyring);
  140. kmem_cache_free(uid_cachep, new);
  141. } else {
  142. uid_hash_insert(new, hashent);
  143. up = new;
  144. }
  145. spin_unlock_irq(&uidhash_lock);
  146. }
  147. return up;
  148. }
  149. void switch_uid(struct user_struct *new_user)
  150. {
  151. struct user_struct *old_user;
  152. /* What if a process setreuid()'s and this brings the
  153. * new uid over his NPROC rlimit? We can check this now
  154. * cheaply with the new uid cache, so if it matters
  155. * we should be checking for it. -DaveM
  156. */
  157. old_user = current->user;
  158. atomic_inc(&new_user->processes);
  159. atomic_dec(&old_user->processes);
  160. switch_uid_keyring(new_user);
  161. current->user = new_user;
  162. /*
  163. * We need to synchronize with __sigqueue_alloc()
  164. * doing a get_uid(p->user).. If that saw the old
  165. * user value, we need to wait until it has exited
  166. * its critical region before we can free the old
  167. * structure.
  168. */
  169. smp_mb();
  170. spin_unlock_wait(&current->sighand->siglock);
  171. free_uid(old_user);
  172. suid_keys(current);
  173. }
  174. static int __init uid_cache_init(void)
  175. {
  176. int n;
  177. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  178. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  179. for(n = 0; n < UIDHASH_SZ; ++n)
  180. INIT_LIST_HEAD(uidhash_table + n);
  181. /* Insert the root user immediately (init already runs as root) */
  182. spin_lock_irq(&uidhash_lock);
  183. uid_hash_insert(&root_user, uidhashentry(0));
  184. spin_unlock_irq(&uidhash_lock);
  185. return 0;
  186. }
  187. module_init(uid_cache_init);