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>
16
17 /*
18 * UID task count cache, to get fast user lookup in "alloc_uid"
19 * when changing user ID's (ie setuid() and friends).
20 */
21 #define UIDHASH_BITS 8
22 #define UIDHASH_SZ (1 << UIDHASH_BITS)
23 #define UIDHASH_MASK (UIDHASH_SZ - 1)
24 #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
25 #define uidhashentry(uid) (uidhash_table + __uidhashfn((uid)))
26
27 static kmem_cache_t *uid_cachep;
28 static struct list_head uidhash_table[UIDHASH_SZ];
29 static DEFINE_SPINLOCK(uidhash_lock);
30
31 struct user_struct root_user = {
32 .__count = ATOMIC_INIT(1),
33 .processes = ATOMIC_INIT(1),
34 .files = ATOMIC_INIT(0),
35 .sigpending = ATOMIC_INIT(0),
36 .mq_bytes = 0,
37 .locked_shm = 0,
38 #ifdef CONFIG_KEYS
39 .uid_keyring = &root_user_keyring,
40 .session_keyring = &root_session_keyring,
41 #endif
42 };
43
44 /*
45 * These routines must be called with the uidhash spinlock held!
46 */
47 static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent)
48 {
49 list_add(&up->uidhash_list, hashent);
50 }
51
52 static inline void uid_hash_remove(struct user_struct *up)
53 {
54 list_del(&up->uidhash_list);
55 }
56
57 static inline struct user_struct *uid_hash_find(uid_t uid, struct list_head *hashent)
58 {
59 struct list_head *up;
60
61 list_for_each(up, hashent) {
62 struct user_struct *user;
63
64 user = list_entry(up, struct user_struct, uidhash_list);
65
66 if(user->uid == uid) {
67 atomic_inc(&user->__count);
68 return user;
69 }
70 }
71
72 return NULL;
73 }
74
75 /*
76 * Locate the user_struct for the passed UID. If found, take a ref on it. The
77 * caller must undo that ref with free_uid().
78 *
79 * If the user_struct could not be found, return NULL.
80 */
81 struct user_struct *find_user(uid_t uid)
82 {
83 struct user_struct *ret;
84
85 spin_lock(&uidhash_lock);
86 ret = uid_hash_find(uid, uidhashentry(uid));
87 spin_unlock(&uidhash_lock);
88 return ret;
89 }
90
91 void free_uid(struct user_struct *up)
92 {
93 if (up && atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
94 uid_hash_remove(up);
95 key_put(up->uid_keyring);
96 key_put(up->session_keyring);
97 kmem_cache_free(uid_cachep, up);
98 spin_unlock(&uidhash_lock);
99 }
100 }
101
102 struct user_struct * alloc_uid(uid_t uid)
103 {
104 struct list_head *hashent = uidhashentry(uid);
105 struct user_struct *up;
106
107 spin_lock(&uidhash_lock);
108 up = uid_hash_find(uid, hashent);
109 spin_unlock(&uidhash_lock);
110
111 if (!up) {
112 struct user_struct *new;
113
114 new = kmem_cache_alloc(uid_cachep, SLAB_KERNEL);
115 if (!new)
116 return NULL;
117 new->uid = uid;
118 atomic_set(&new->__count, 1);
119 atomic_set(&new->processes, 0);
120 atomic_set(&new->files, 0);
121 atomic_set(&new->sigpending, 0);
122
123 new->mq_bytes = 0;
124 new->locked_shm = 0;
125
126 if (alloc_uid_keyring(new) < 0) {
127 kmem_cache_free(uid_cachep, new);
128 return NULL;
129 }
130
131 /*
132 * Before adding this, check whether we raced
133 * on adding the same user already..
134 */
135 spin_lock(&uidhash_lock);
136 up = uid_hash_find(uid, hashent);
137 if (up) {
138 key_put(new->uid_keyring);
139 key_put(new->session_keyring);
140 kmem_cache_free(uid_cachep, new);
141 } else {
142 uid_hash_insert(new, hashent);
143 up = new;
144 }
145 spin_unlock(&uidhash_lock);
146
147 }
148 return up;
149 }
150
151 void switch_uid(struct user_struct *new_user)
152 {
153 struct user_struct *old_user;
154
155 /* What if a process setreuid()'s and this brings the
156 * new uid over his NPROC rlimit? We can check this now
157 * cheaply with the new uid cache, so if it matters
158 * we should be checking for it. -DaveM
159 */
160 old_user = current->user;
161 atomic_inc(&new_user->processes);
162 atomic_dec(&old_user->processes);
163 switch_uid_keyring(new_user);
164 current->user = new_user;
165 free_uid(old_user);
166 suid_keys(current);
167 }
168
169
170 static int __init uid_cache_init(void)
171 {
172 int n;
173
174 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
175 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
176
177 for(n = 0; n < UIDHASH_SZ; ++n)
178 INIT_LIST_HEAD(uidhash_table + n);
179
180 /* Insert the root user immediately (init already runs as root) */
181 spin_lock(&uidhash_lock);
182 uid_hash_insert(&root_user, uidhashentry(0));
183 spin_unlock(&uidhash_lock);
184
185 return 0;
186 }
187
188 module_init(uid_cache_init);
189
|
This page was automatically generated by the
LXR engine.
|