user.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. new->mq_bytes = 0;
  105. new->locked_shm = 0;
  106. if (alloc_uid_keyring(new) < 0) {
  107. kmem_cache_free(uid_cachep, new);
  108. return NULL;
  109. }
  110. /*
  111. * Before adding this, check whether we raced
  112. * on adding the same user already..
  113. */
  114. spin_lock(&uidhash_lock);
  115. up = uid_hash_find(uid, hashent);
  116. if (up) {
  117. key_put(new->uid_keyring);
  118. key_put(new->session_keyring);
  119. kmem_cache_free(uid_cachep, new);
  120. } else {
  121. uid_hash_insert(new, hashent);
  122. up = new;
  123. }
  124. spin_unlock(&uidhash_lock);
  125. }
  126. return up;
  127. }
  128. void switch_uid(struct user_struct *new_user)
  129. {
  130. struct user_struct *old_user;
  131. /* What if a process setreuid()'s and this brings the
  132. * new uid over his NPROC rlimit? We can check this now
  133. * cheaply with the new uid cache, so if it matters
  134. * we should be checking for it. -DaveM
  135. */
  136. old_user = current->user;
  137. atomic_inc(&new_user->processes);
  138. atomic_dec(&old_user->processes);
  139. switch_uid_keyring(new_user);
  140. current->user = new_user;
  141. free_uid(old_user);
  142. suid_keys(current);
  143. }
  144. static int __init uid_cache_init(void)
  145. {
  146. int n;
  147. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  148. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  149. for(n = 0; n < UIDHASH_SZ; ++n)
  150. INIT_LIST_HEAD(uidhash_table + n);
  151. /* Insert the root user immediately (init already runs as root) */
  152. spin_lock(&uidhash_lock);
  153. uid_hash_insert(&root_user, uidhashentry(0));
  154. spin_unlock(&uidhash_lock);
  155. return 0;
  156. }
  157. module_init(uid_cache_init);