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>
16 #include <linux/interrupt.h>
19 * UID task count cache, to get fast user lookup in "alloc_uid"
20 * when changing user ID's (ie setuid() and friends).
23 #define UIDHASH_BITS (CONFIG_BASE_SMALL ? 3 : 8)
24 #define UIDHASH_SZ (1 << UIDHASH_BITS)
25 #define UIDHASH_MASK (UIDHASH_SZ - 1)
26 #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
27 #define uidhashentry(uid) (uidhash_table + __uidhashfn((uid)))
29 static kmem_cache_t
*uid_cachep
;
30 static struct list_head uidhash_table
[UIDHASH_SZ
];
33 * The uidhash_lock is mostly taken from process context, but it is
34 * occasionally also taken from softirq/tasklet context, when
35 * task-structs get RCU-freed. Hence all locking must be softirq-safe.
37 static DEFINE_SPINLOCK(uidhash_lock
);
39 struct user_struct root_user
= {
40 .__count
= ATOMIC_INIT(1),
41 .processes
= ATOMIC_INIT(1),
42 .files
= ATOMIC_INIT(0),
43 .sigpending
= ATOMIC_INIT(0),
47 .uid_keyring
= &root_user_keyring
,
48 .session_keyring
= &root_session_keyring
,
53 * These routines must be called with the uidhash spinlock held!
55 static inline void uid_hash_insert(struct user_struct
*up
, struct list_head
*hashent
)
57 list_add(&up
->uidhash_list
, hashent
);
60 static inline void uid_hash_remove(struct user_struct
*up
)
62 list_del(&up
->uidhash_list
);
65 static inline struct user_struct
*uid_hash_find(uid_t uid
, struct list_head
*hashent
)
69 list_for_each(up
, hashent
) {
70 struct user_struct
*user
;
72 user
= list_entry(up
, struct user_struct
, uidhash_list
);
74 if(user
->uid
== uid
) {
75 atomic_inc(&user
->__count
);
84 * Locate the user_struct for the passed UID. If found, take a ref on it. The
85 * caller must undo that ref with free_uid().
87 * If the user_struct could not be found, return NULL.
89 struct user_struct
*find_user(uid_t uid
)
91 struct user_struct
*ret
;
93 spin_lock_bh(&uidhash_lock
);
94 ret
= uid_hash_find(uid
, uidhashentry(uid
));
95 spin_unlock_bh(&uidhash_lock
);
99 void free_uid(struct user_struct
*up
)
102 if (up
&& atomic_dec_and_lock(&up
->__count
, &uidhash_lock
)) {
104 key_put(up
->uid_keyring
);
105 key_put(up
->session_keyring
);
106 kmem_cache_free(uid_cachep
, up
);
107 spin_unlock(&uidhash_lock
);
112 struct user_struct
* alloc_uid(uid_t uid
)
114 struct list_head
*hashent
= uidhashentry(uid
);
115 struct user_struct
*up
;
117 spin_lock_bh(&uidhash_lock
);
118 up
= uid_hash_find(uid
, hashent
);
119 spin_unlock_bh(&uidhash_lock
);
122 struct user_struct
*new;
124 new = kmem_cache_alloc(uid_cachep
, SLAB_KERNEL
);
128 atomic_set(&new->__count
, 1);
129 atomic_set(&new->processes
, 0);
130 atomic_set(&new->files
, 0);
131 atomic_set(&new->sigpending
, 0);
132 #ifdef CONFIG_INOTIFY
133 atomic_set(&new->inotify_watches
, 0);
134 atomic_set(&new->inotify_devs
, 0);
140 if (alloc_uid_keyring(new) < 0) {
141 kmem_cache_free(uid_cachep
, new);
146 * Before adding this, check whether we raced
147 * on adding the same user already..
149 spin_lock_bh(&uidhash_lock
);
150 up
= uid_hash_find(uid
, hashent
);
152 key_put(new->uid_keyring
);
153 key_put(new->session_keyring
);
154 kmem_cache_free(uid_cachep
, new);
156 uid_hash_insert(new, hashent
);
159 spin_unlock_bh(&uidhash_lock
);
165 void switch_uid(struct user_struct
*new_user
)
167 struct user_struct
*old_user
;
169 /* What if a process setreuid()'s and this brings the
170 * new uid over his NPROC rlimit? We can check this now
171 * cheaply with the new uid cache, so if it matters
172 * we should be checking for it. -DaveM
174 old_user
= current
->user
;
175 atomic_inc(&new_user
->processes
);
176 atomic_dec(&old_user
->processes
);
177 switch_uid_keyring(new_user
);
178 current
->user
= new_user
;
184 static int __init
uid_cache_init(void)
188 uid_cachep
= kmem_cache_create("uid_cache", sizeof(struct user_struct
),
189 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
, NULL
);
191 for(n
= 0; n
< UIDHASH_SZ
; ++n
)
192 INIT_LIST_HEAD(uidhash_table
+ n
);
194 /* Insert the root user immediately (init already runs as root) */
195 spin_lock_bh(&uidhash_lock
);
196 uid_hash_insert(&root_user
, uidhashentry(0));
197 spin_unlock_bh(&uidhash_lock
);
202 module_init(uid_cache_init
);