]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
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> | |
4021cb27 | 16 | #include <linux/interrupt.h> |
1da177e4 LT |
17 | |
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 | ||
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))) | |
28 | ||
29 | static kmem_cache_t *uid_cachep; | |
30 | static struct list_head uidhash_table[UIDHASH_SZ]; | |
4021cb27 IM |
31 | |
32 | /* | |
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. | |
36 | */ | |
1da177e4 LT |
37 | static DEFINE_SPINLOCK(uidhash_lock); |
38 | ||
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), | |
44 | .mq_bytes = 0, | |
45 | .locked_shm = 0, | |
46 | #ifdef CONFIG_KEYS | |
47 | .uid_keyring = &root_user_keyring, | |
48 | .session_keyring = &root_session_keyring, | |
49 | #endif | |
50 | }; | |
51 | ||
52 | /* | |
53 | * These routines must be called with the uidhash spinlock held! | |
54 | */ | |
55 | static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent) | |
56 | { | |
57 | list_add(&up->uidhash_list, hashent); | |
58 | } | |
59 | ||
60 | static inline void uid_hash_remove(struct user_struct *up) | |
61 | { | |
62 | list_del(&up->uidhash_list); | |
63 | } | |
64 | ||
65 | static inline struct user_struct *uid_hash_find(uid_t uid, struct list_head *hashent) | |
66 | { | |
67 | struct list_head *up; | |
68 | ||
69 | list_for_each(up, hashent) { | |
70 | struct user_struct *user; | |
71 | ||
72 | user = list_entry(up, struct user_struct, uidhash_list); | |
73 | ||
74 | if(user->uid == uid) { | |
75 | atomic_inc(&user->__count); | |
76 | return user; | |
77 | } | |
78 | } | |
79 | ||
80 | return NULL; | |
81 | } | |
82 | ||
83 | /* | |
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(). | |
86 | * | |
87 | * If the user_struct could not be found, return NULL. | |
88 | */ | |
89 | struct user_struct *find_user(uid_t uid) | |
90 | { | |
91 | struct user_struct *ret; | |
92 | ||
4021cb27 | 93 | spin_lock_bh(&uidhash_lock); |
1da177e4 | 94 | ret = uid_hash_find(uid, uidhashentry(uid)); |
4021cb27 | 95 | spin_unlock_bh(&uidhash_lock); |
1da177e4 LT |
96 | return ret; |
97 | } | |
98 | ||
99 | void free_uid(struct user_struct *up) | |
100 | { | |
4021cb27 | 101 | local_bh_disable(); |
1da177e4 LT |
102 | if (up && atomic_dec_and_lock(&up->__count, &uidhash_lock)) { |
103 | uid_hash_remove(up); | |
104 | key_put(up->uid_keyring); | |
105 | key_put(up->session_keyring); | |
106 | kmem_cache_free(uid_cachep, up); | |
107 | spin_unlock(&uidhash_lock); | |
108 | } | |
4021cb27 | 109 | local_bh_enable(); |
1da177e4 LT |
110 | } |
111 | ||
112 | struct user_struct * alloc_uid(uid_t uid) | |
113 | { | |
114 | struct list_head *hashent = uidhashentry(uid); | |
115 | struct user_struct *up; | |
116 | ||
4021cb27 | 117 | spin_lock_bh(&uidhash_lock); |
1da177e4 | 118 | up = uid_hash_find(uid, hashent); |
4021cb27 | 119 | spin_unlock_bh(&uidhash_lock); |
1da177e4 LT |
120 | |
121 | if (!up) { | |
122 | struct user_struct *new; | |
123 | ||
124 | new = kmem_cache_alloc(uid_cachep, SLAB_KERNEL); | |
125 | if (!new) | |
126 | return NULL; | |
127 | new->uid = uid; | |
128 | atomic_set(&new->__count, 1); | |
129 | atomic_set(&new->processes, 0); | |
130 | atomic_set(&new->files, 0); | |
131 | atomic_set(&new->sigpending, 0); | |
0eeca283 RL |
132 | #ifdef CONFIG_INOTIFY |
133 | atomic_set(&new->inotify_watches, 0); | |
134 | atomic_set(&new->inotify_devs, 0); | |
135 | #endif | |
1da177e4 LT |
136 | |
137 | new->mq_bytes = 0; | |
138 | new->locked_shm = 0; | |
139 | ||
140 | if (alloc_uid_keyring(new) < 0) { | |
141 | kmem_cache_free(uid_cachep, new); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | /* | |
146 | * Before adding this, check whether we raced | |
147 | * on adding the same user already.. | |
148 | */ | |
4021cb27 | 149 | spin_lock_bh(&uidhash_lock); |
1da177e4 LT |
150 | up = uid_hash_find(uid, hashent); |
151 | if (up) { | |
152 | key_put(new->uid_keyring); | |
153 | key_put(new->session_keyring); | |
154 | kmem_cache_free(uid_cachep, new); | |
155 | } else { | |
156 | uid_hash_insert(new, hashent); | |
157 | up = new; | |
158 | } | |
4021cb27 | 159 | spin_unlock_bh(&uidhash_lock); |
1da177e4 LT |
160 | |
161 | } | |
162 | return up; | |
163 | } | |
164 | ||
165 | void switch_uid(struct user_struct *new_user) | |
166 | { | |
167 | struct user_struct *old_user; | |
168 | ||
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 | |
173 | */ | |
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; | |
179 | free_uid(old_user); | |
180 | suid_keys(current); | |
181 | } | |
182 | ||
183 | ||
184 | static int __init uid_cache_init(void) | |
185 | { | |
186 | int n; | |
187 | ||
188 | uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct), | |
189 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); | |
190 | ||
191 | for(n = 0; n < UIDHASH_SZ; ++n) | |
192 | INIT_LIST_HEAD(uidhash_table + n); | |
193 | ||
194 | /* Insert the root user immediately (init already runs as root) */ | |
4021cb27 | 195 | spin_lock_bh(&uidhash_lock); |
1da177e4 | 196 | uid_hash_insert(&root_user, uidhashentry(0)); |
4021cb27 | 197 | spin_unlock_bh(&uidhash_lock); |
1da177e4 LT |
198 | |
199 | return 0; | |
200 | } | |
201 | ||
202 | module_init(uid_cache_init); |