4 * (C) Copyright 1991-2000 Linus Torvalds
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.
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>
18 * UID task count cache, to get fast user lookup in "alloc_uid"
19 * when changing user ID's (ie setuid() and friends).
22 #define UIDHASH_BITS (CONFIG_BASE_SMALL ? 3 : 8)
23 #define UIDHASH_SZ (1 << UIDHASH_BITS)
24 #define UIDHASH_MASK (UIDHASH_SZ - 1)
25 #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
26 #define uidhashentry(uid) (uidhash_table + __uidhashfn((uid)))
28 static kmem_cache_t *uid_cachep;
29 static struct list_head uidhash_table[UIDHASH_SZ];
30 static DEFINE_SPINLOCK(uidhash_lock);
32 struct user_struct root_user = {
33 .__count = ATOMIC_INIT(1),
34 .processes = ATOMIC_INIT(1),
35 .files = ATOMIC_INIT(0),
36 .sigpending = ATOMIC_INIT(0),
40 .uid_keyring = &root_user_keyring,
41 .session_keyring = &root_session_keyring,
46 * These routines must be called with the uidhash spinlock held!
48 static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent)
50 list_add(&up->uidhash_list, hashent);
53 static inline void uid_hash_remove(struct user_struct *up)
55 list_del(&up->uidhash_list);
58 static inline struct user_struct *uid_hash_find(uid_t uid, struct list_head *hashent)
62 list_for_each(up, hashent) {
63 struct user_struct *user;
65 user = list_entry(up, struct user_struct, uidhash_list);
67 if(user->uid == uid) {
68 atomic_inc(&user->__count);
77 * Locate the user_struct for the passed UID. If found, take a ref on it. The
78 * caller must undo that ref with free_uid().
80 * If the user_struct could not be found, return NULL.
82 struct user_struct *find_user(uid_t uid)
84 struct user_struct *ret;
86 spin_lock(&uidhash_lock);
87 ret = uid_hash_find(uid, uidhashentry(uid));
88 spin_unlock(&uidhash_lock);
92 void free_uid(struct user_struct *up)
94 if (up && atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
96 key_put(up->uid_keyring);
97 key_put(up->session_keyring);
98 kmem_cache_free(uid_cachep, up);
99 spin_unlock(&uidhash_lock);
103 struct user_struct * alloc_uid(uid_t uid)
105 struct list_head *hashent = uidhashentry(uid);
106 struct user_struct *up;
108 spin_lock(&uidhash_lock);
109 up = uid_hash_find(uid, hashent);
110 spin_unlock(&uidhash_lock);
113 struct user_struct *new;
115 new = kmem_cache_alloc(uid_cachep, SLAB_KERNEL);
119 atomic_set(&new->__count, 1);
120 atomic_set(&new->processes, 0);
121 atomic_set(&new->files, 0);
122 atomic_set(&new->sigpending, 0);
127 if (alloc_uid_keyring(new) < 0) {
128 kmem_cache_free(uid_cachep, new);
133 * Before adding this, check whether we raced
134 * on adding the same user already..
136 spin_lock(&uidhash_lock);
137 up = uid_hash_find(uid, hashent);
139 key_put(new->uid_keyring);
140 key_put(new->session_keyring);
141 kmem_cache_free(uid_cachep, new);
143 uid_hash_insert(new, hashent);
146 spin_unlock(&uidhash_lock);
152 void switch_uid(struct user_struct *new_user)
154 struct user_struct *old_user;
156 /* What if a process setreuid()'s and this brings the
157 * new uid over his NPROC rlimit? We can check this now
158 * cheaply with the new uid cache, so if it matters
159 * we should be checking for it. -DaveM
161 old_user = current->user;
162 atomic_inc(&new_user->processes);
163 atomic_dec(&old_user->processes);
164 switch_uid_keyring(new_user);
165 current->user = new_user;
171 static int __init uid_cache_init(void)
175 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
176 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
178 for(n = 0; n < UIDHASH_SZ; ++n)
179 INIT_LIST_HEAD(uidhash_table + n);
181 /* Insert the root user immediately (init already runs as root) */
182 spin_lock(&uidhash_lock);
183 uid_hash_insert(&root_user, uidhashentry(0));
184 spin_unlock(&uidhash_lock);
189 module_init(uid_cache_init);