user.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /*
  16. * UID task count cache, to get fast user lookup in "alloc_uid"
  17. * when changing user ID's (ie setuid() and friends).
  18. */
  19. #define UIDHASH_BITS (CONFIG_BASE_SMALL ? 3 : 8)
  20. #define UIDHASH_SZ (1 << UIDHASH_BITS)
  21. #define UIDHASH_MASK (UIDHASH_SZ - 1)
  22. #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
  23. #define uidhashentry(uid) (uidhash_table + __uidhashfn((uid)))
  24. static kmem_cache_t *uid_cachep;
  25. static struct list_head uidhash_table[UIDHASH_SZ];
  26. static DEFINE_SPINLOCK(uidhash_lock);
  27. struct user_struct root_user = {
  28. .__count = ATOMIC_INIT(1),
  29. .processes = ATOMIC_INIT(1),
  30. .files = ATOMIC_INIT(0),
  31. .sigpending = ATOMIC_INIT(0),
  32. .mq_bytes = 0,
  33. .locked_shm = 0,
  34. #ifdef CONFIG_KEYS
  35. .uid_keyring = &root_user_keyring,
  36. .session_keyring = &root_session_keyring,
  37. #endif
  38. };
  39. /*
  40. * These routines must be called with the uidhash spinlock held!
  41. */
  42. static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent)
  43. {
  44. list_add(&up->uidhash_list, hashent);
  45. }
  46. static inline void uid_hash_remove(struct user_struct *up)
  47. {
  48. list_del(&up->uidhash_list);
  49. }
  50. static inline struct user_struct *uid_hash_find(uid_t uid, struct list_head *hashent)
  51. {
  52. struct list_head *up;
  53. list_for_each(up, hashent) {
  54. struct user_struct *user;
  55. user = list_entry(up, struct user_struct, uidhash_list);
  56. if(user->uid == uid) {
  57. atomic_inc(&user->__count);
  58. return user;
  59. }
  60. }
  61. return NULL;
  62. }
  63. /*
  64. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  65. * caller must undo that ref with free_uid().
  66. *
  67. * If the user_struct could not be found, return NULL.
  68. */
  69. struct user_struct *find_user(uid_t uid)
  70. {
  71. struct user_struct *ret;
  72. spin_lock(&uidhash_lock);
  73. ret = uid_hash_find(uid, uidhashentry(uid));
  74. spin_unlock(&uidhash_lock);
  75. return ret;
  76. }
  77. void free_uid(struct user_struct *up)
  78. {
  79. if (up && atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  80. uid_hash_remove(up);
  81. key_put(up->uid_keyring);
  82. key_put(up->session_keyring);
  83. kmem_cache_free(uid_cachep, up);
  84. spin_unlock(&uidhash_lock);
  85. }
  86. }
  87. struct user_struct * alloc_uid(uid_t uid)
  88. {
  89. struct list_head *hashent = uidhashentry(uid);
  90. struct user_struct *up;
  91. spin_lock(&uidhash_lock);
  92. up = uid_hash_find(uid, hashent);
  93. spin_unlock(&uidhash_lock);
  94. if (!up) {
  95. struct user_struct *new;
  96. new = kmem_cache_alloc(uid_cachep, SLAB_KERNEL);
  97. if (!new)
  98. return NULL;
  99. new->uid = uid;
  100. atomic_set(&new->__count, 1);
  101. atomic_set(&new->processes, 0);
  102. atomic_set(&new->files, 0);
  103. atomic_set(&new->sigpending, 0);
  104. #ifdef CONFIG_INOTIFY
  105. atomic_set(&new->inotify_watches, 0);
  106. atomic_set(&new->inotify_devs, 0);
  107. #endif
  108. new->mq_bytes = 0;
  109. new->locked_shm = 0;
  110. if (alloc_uid_keyring(new) < 0) {
  111. kmem_cache_free(uid_cachep, new);
  112. return NULL;
  113. }
  114. /*
  115. * Before adding this, check whether we raced
  116. * on adding the same user already..
  117. */
  118. spin_lock(&uidhash_lock);
  119. up = uid_hash_find(uid, hashent);
  120. if (up) {
  121. key_put(new->uid_keyring);
  122. key_put(new->session_keyring);
  123. kmem_cache_free(uid_cachep, new);
  124. } else {
  125. uid_hash_insert(new, hashent);
  126. up = new;
  127. }
  128. spin_unlock(&uidhash_lock);
  129. }
  130. return up;
  131. }
  132. void switch_uid(struct user_struct *new_user)
  133. {
  134. struct user_struct *old_user;
  135. /* What if a process setreuid()'s and this brings the
  136. * new uid over his NPROC rlimit? We can check this now
  137. * cheaply with the new uid cache, so if it matters
  138. * we should be checking for it. -DaveM
  139. */
  140. old_user = current->user;
  141. atomic_inc(&new_user->processes);
  142. atomic_dec(&old_user->processes);
  143. switch_uid_keyring(new_user);
  144. current->user = new_user;
  145. free_uid(old_user);
  146. suid_keys(current);
  147. }
  148. static int __init uid_cache_init(void)
  149. {
  150. int n;
  151. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  152. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  153. for(n = 0; n < UIDHASH_SZ; ++n)
  154. INIT_LIST_HEAD(uidhash_table + n);
  155. /* Insert the root user immediately (init already runs as root) */
  156. spin_lock(&uidhash_lock);
  157. uid_hash_insert(&root_user, uidhashentry(0));
  158. spin_unlock(&uidhash_lock);
  159. return 0;
  160. }
  161. module_init(uid_cache_init);