user.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include <linux/module.h>
  17. #include <linux/user_namespace.h>
  18. /*
  19. * UID task count cache, to get fast user lookup in "alloc_uid"
  20. * when changing user ID's (ie setuid() and friends).
  21. */
  22. #define UIDHASH_MASK (UIDHASH_SZ - 1)
  23. #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
  24. #define uidhashentry(ns, uid) ((ns)->uidhash_table + __uidhashfn((uid)))
  25. static struct kmem_cache *uid_cachep;
  26. /*
  27. * The uidhash_lock is mostly taken from process context, but it is
  28. * occasionally also taken from softirq/tasklet context, when
  29. * task-structs get RCU-freed. Hence all locking must be softirq-safe.
  30. * But free_uid() is also called with local interrupts disabled, and running
  31. * local_bh_enable() with local interrupts disabled is an error - we'll run
  32. * softirq callbacks, and they can unconditionally enable interrupts, and
  33. * the caller of free_uid() didn't expect that..
  34. */
  35. static DEFINE_SPINLOCK(uidhash_lock);
  36. struct user_struct root_user = {
  37. .__count = ATOMIC_INIT(1),
  38. .processes = ATOMIC_INIT(1),
  39. .files = ATOMIC_INIT(0),
  40. .sigpending = ATOMIC_INIT(0),
  41. .mq_bytes = 0,
  42. .locked_shm = 0,
  43. #ifdef CONFIG_KEYS
  44. .uid_keyring = &root_user_keyring,
  45. .session_keyring = &root_session_keyring,
  46. #endif
  47. #ifdef CONFIG_FAIR_USER_SCHED
  48. .tg = &init_task_group,
  49. #endif
  50. };
  51. #ifdef CONFIG_FAIR_USER_SCHED
  52. static void sched_destroy_user(struct user_struct *up)
  53. {
  54. sched_destroy_group(up->tg);
  55. }
  56. static int sched_create_user(struct user_struct *up)
  57. {
  58. int rc = 0;
  59. up->tg = sched_create_group();
  60. if (IS_ERR(up->tg))
  61. rc = -ENOMEM;
  62. return rc;
  63. }
  64. static void sched_switch_user(struct task_struct *p)
  65. {
  66. sched_move_task(p);
  67. }
  68. #else /* CONFIG_FAIR_USER_SCHED */
  69. static void sched_destroy_user(struct user_struct *up) { }
  70. static int sched_create_user(struct user_struct *up) { return 0; }
  71. static void sched_switch_user(struct task_struct *p) { }
  72. #endif /* CONFIG_FAIR_USER_SCHED */
  73. /*
  74. * These routines must be called with the uidhash spinlock held!
  75. */
  76. static inline void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
  77. {
  78. hlist_add_head(&up->uidhash_node, hashent);
  79. }
  80. static inline void uid_hash_remove(struct user_struct *up)
  81. {
  82. hlist_del_init(&up->uidhash_node);
  83. }
  84. static inline struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
  85. {
  86. struct user_struct *user;
  87. struct hlist_node *h;
  88. hlist_for_each_entry(user, h, hashent, uidhash_node) {
  89. if(user->uid == uid) {
  90. atomic_inc(&user->__count);
  91. return user;
  92. }
  93. }
  94. return NULL;
  95. }
  96. /*
  97. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  98. * caller must undo that ref with free_uid().
  99. *
  100. * If the user_struct could not be found, return NULL.
  101. */
  102. struct user_struct *find_user(uid_t uid)
  103. {
  104. struct user_struct *ret;
  105. unsigned long flags;
  106. struct user_namespace *ns = current->nsproxy->user_ns;
  107. spin_lock_irqsave(&uidhash_lock, flags);
  108. ret = uid_hash_find(uid, uidhashentry(ns, uid));
  109. spin_unlock_irqrestore(&uidhash_lock, flags);
  110. return ret;
  111. }
  112. void free_uid(struct user_struct *up)
  113. {
  114. unsigned long flags;
  115. if (!up)
  116. return;
  117. local_irq_save(flags);
  118. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  119. uid_hash_remove(up);
  120. spin_unlock_irqrestore(&uidhash_lock, flags);
  121. sched_destroy_user(up);
  122. key_put(up->uid_keyring);
  123. key_put(up->session_keyring);
  124. kmem_cache_free(uid_cachep, up);
  125. } else {
  126. local_irq_restore(flags);
  127. }
  128. }
  129. struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
  130. {
  131. struct hlist_head *hashent = uidhashentry(ns, uid);
  132. struct user_struct *up;
  133. spin_lock_irq(&uidhash_lock);
  134. up = uid_hash_find(uid, hashent);
  135. spin_unlock_irq(&uidhash_lock);
  136. if (!up) {
  137. struct user_struct *new;
  138. new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
  139. if (!new)
  140. return NULL;
  141. new->uid = uid;
  142. atomic_set(&new->__count, 1);
  143. atomic_set(&new->processes, 0);
  144. atomic_set(&new->files, 0);
  145. atomic_set(&new->sigpending, 0);
  146. #ifdef CONFIG_INOTIFY_USER
  147. atomic_set(&new->inotify_watches, 0);
  148. atomic_set(&new->inotify_devs, 0);
  149. #endif
  150. new->mq_bytes = 0;
  151. new->locked_shm = 0;
  152. if (alloc_uid_keyring(new, current) < 0) {
  153. kmem_cache_free(uid_cachep, new);
  154. return NULL;
  155. }
  156. if (sched_create_user(new) < 0) {
  157. key_put(new->uid_keyring);
  158. key_put(new->session_keyring);
  159. kmem_cache_free(uid_cachep, new);
  160. return NULL;
  161. }
  162. /*
  163. * Before adding this, check whether we raced
  164. * on adding the same user already..
  165. */
  166. spin_lock_irq(&uidhash_lock);
  167. up = uid_hash_find(uid, hashent);
  168. if (up) {
  169. sched_destroy_user(new);
  170. key_put(new->uid_keyring);
  171. key_put(new->session_keyring);
  172. kmem_cache_free(uid_cachep, new);
  173. } else {
  174. uid_hash_insert(new, hashent);
  175. up = new;
  176. }
  177. spin_unlock_irq(&uidhash_lock);
  178. }
  179. return up;
  180. }
  181. void switch_uid(struct user_struct *new_user)
  182. {
  183. struct user_struct *old_user;
  184. /* What if a process setreuid()'s and this brings the
  185. * new uid over his NPROC rlimit? We can check this now
  186. * cheaply with the new uid cache, so if it matters
  187. * we should be checking for it. -DaveM
  188. */
  189. old_user = current->user;
  190. atomic_inc(&new_user->processes);
  191. atomic_dec(&old_user->processes);
  192. switch_uid_keyring(new_user);
  193. current->user = new_user;
  194. sched_switch_user(current);
  195. /*
  196. * We need to synchronize with __sigqueue_alloc()
  197. * doing a get_uid(p->user).. If that saw the old
  198. * user value, we need to wait until it has exited
  199. * its critical region before we can free the old
  200. * structure.
  201. */
  202. smp_mb();
  203. spin_unlock_wait(&current->sighand->siglock);
  204. free_uid(old_user);
  205. suid_keys(current);
  206. }
  207. void release_uids(struct user_namespace *ns)
  208. {
  209. int i;
  210. unsigned long flags;
  211. struct hlist_head *head;
  212. struct hlist_node *nd;
  213. spin_lock_irqsave(&uidhash_lock, flags);
  214. /*
  215. * collapse the chains so that the user_struct-s will
  216. * be still alive, but not in hashes. subsequent free_uid()
  217. * will free them.
  218. */
  219. for (i = 0; i < UIDHASH_SZ; i++) {
  220. head = ns->uidhash_table + i;
  221. while (!hlist_empty(head)) {
  222. nd = head->first;
  223. hlist_del_init(nd);
  224. }
  225. }
  226. spin_unlock_irqrestore(&uidhash_lock, flags);
  227. free_uid(ns->root_user);
  228. }
  229. static int __init uid_cache_init(void)
  230. {
  231. int n;
  232. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  233. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  234. for(n = 0; n < UIDHASH_SZ; ++n)
  235. INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
  236. /* Insert the root user immediately (init already runs as root) */
  237. spin_lock_irq(&uidhash_lock);
  238. uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
  239. spin_unlock_irq(&uidhash_lock);
  240. return 0;
  241. }
  242. module_init(uid_cache_init);