]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/user.c
tg3: Update version to 3.109
[net-next-2.6.git] / kernel / user.c
CommitLineData
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>
acce292c
CLG
17#include <linux/module.h>
18#include <linux/user_namespace.h>
d84f4f99 19#include "cred-internals.h"
1da177e4 20
aee16ce7
PE
21struct user_namespace init_user_ns = {
22 .kref = {
1d1e9756 23 .refcount = ATOMIC_INIT(2),
aee16ce7 24 },
18b6e041 25 .creator = &root_user,
aee16ce7
PE
26};
27EXPORT_SYMBOL_GPL(init_user_ns);
28
1da177e4
LT
29/*
30 * UID task count cache, to get fast user lookup in "alloc_uid"
31 * when changing user ID's (ie setuid() and friends).
32 */
33
1da177e4
LT
34#define UIDHASH_MASK (UIDHASH_SZ - 1)
35#define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
acce292c 36#define uidhashentry(ns, uid) ((ns)->uidhash_table + __uidhashfn((uid)))
1da177e4 37
e18b890b 38static struct kmem_cache *uid_cachep;
4021cb27
IM
39
40/*
41 * The uidhash_lock is mostly taken from process context, but it is
42 * occasionally also taken from softirq/tasklet context, when
43 * task-structs get RCU-freed. Hence all locking must be softirq-safe.
3fa97c9d
AM
44 * But free_uid() is also called with local interrupts disabled, and running
45 * local_bh_enable() with local interrupts disabled is an error - we'll run
46 * softirq callbacks, and they can unconditionally enable interrupts, and
47 * the caller of free_uid() didn't expect that..
4021cb27 48 */
1da177e4
LT
49static DEFINE_SPINLOCK(uidhash_lock);
50
18b6e041 51/* root_user.__count is 2, 1 for init task cred, 1 for init_user_ns->creator */
1da177e4 52struct user_struct root_user = {
18b6e041 53 .__count = ATOMIC_INIT(2),
1da177e4
LT
54 .processes = ATOMIC_INIT(1),
55 .files = ATOMIC_INIT(0),
56 .sigpending = ATOMIC_INIT(0),
1da177e4 57 .locked_shm = 0,
18b6e041 58 .user_ns = &init_user_ns,
1da177e4
LT
59};
60
5cb350ba
DG
61/*
62 * These routines must be called with the uidhash spinlock held!
63 */
40aeb400 64static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
5cb350ba
DG
65{
66 hlist_add_head(&up->uidhash_node, hashent);
67}
68
40aeb400 69static void uid_hash_remove(struct user_struct *up)
5cb350ba
DG
70{
71 hlist_del_init(&up->uidhash_node);
fb5ae64f 72 put_user_ns(up->user_ns);
5cb350ba
DG
73}
74
3959214f
KS
75static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
76{
77 struct user_struct *user;
78 struct hlist_node *h;
79
80 hlist_for_each_entry(user, h, hashent, uidhash_node) {
81 if (user->uid == uid) {
82 atomic_inc(&user->__count);
83 return user;
84 }
85 }
86
87 return NULL;
88}
89
5cb350ba
DG
90/* IRQs are disabled and uidhash_lock is held upon function entry.
91 * IRQ state (as stored in flags) is restored and uidhash_lock released
92 * upon function exit.
93 */
18b6e041 94static void free_user(struct user_struct *up, unsigned long flags)
5cb350ba
DG
95{
96 uid_hash_remove(up);
97 spin_unlock_irqrestore(&uidhash_lock, flags);
5cb350ba
DG
98 key_put(up->uid_keyring);
99 key_put(up->session_keyring);
100 kmem_cache_free(uid_cachep, up);
101}
102
1da177e4
LT
103/*
104 * Locate the user_struct for the passed UID. If found, take a ref on it. The
105 * caller must undo that ref with free_uid().
106 *
107 * If the user_struct could not be found, return NULL.
108 */
109struct user_struct *find_user(uid_t uid)
110{
111 struct user_struct *ret;
3fa97c9d 112 unsigned long flags;
6ded6ab9 113 struct user_namespace *ns = current_user_ns();
1da177e4 114
3fa97c9d 115 spin_lock_irqsave(&uidhash_lock, flags);
acce292c 116 ret = uid_hash_find(uid, uidhashentry(ns, uid));
3fa97c9d 117 spin_unlock_irqrestore(&uidhash_lock, flags);
1da177e4
LT
118 return ret;
119}
120
121void free_uid(struct user_struct *up)
122{
3fa97c9d
AM
123 unsigned long flags;
124
36f57413
AM
125 if (!up)
126 return;
127
3fa97c9d 128 local_irq_save(flags);
5cb350ba
DG
129 if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
130 free_user(up, flags);
131 else
36f57413 132 local_irq_restore(flags);
1da177e4
LT
133}
134
354a1f4d 135struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid)
1da177e4 136{
735de223 137 struct hlist_head *hashent = uidhashentry(ns, uid);
8eb703e4 138 struct user_struct *up, *new;
1da177e4 139
eb41d946 140 /* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
5cb350ba
DG
141 * atomic.
142 */
3fa97c9d 143 spin_lock_irq(&uidhash_lock);
1da177e4 144 up = uid_hash_find(uid, hashent);
3fa97c9d 145 spin_unlock_irq(&uidhash_lock);
1da177e4
LT
146
147 if (!up) {
354a1f4d 148 new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
8eb703e4
PE
149 if (!new)
150 goto out_unlock;
5e8869bb 151
1da177e4
LT
152 new->uid = uid;
153 atomic_set(&new->__count, 1);
1da177e4 154
18b6e041
SH
155 new->user_ns = get_user_ns(ns);
156
1da177e4
LT
157 /*
158 * Before adding this, check whether we raced
159 * on adding the same user already..
160 */
3fa97c9d 161 spin_lock_irq(&uidhash_lock);
1da177e4
LT
162 up = uid_hash_find(uid, hashent);
163 if (up) {
052f1dc7 164 /* This case is not possible when CONFIG_USER_SCHED
5cb350ba
DG
165 * is defined, since we serialize alloc_uid() using
166 * uids_mutex. Hence no need to call
167 * sched_destroy_user() or remove_user_sysfs_dir().
168 */
1da177e4
LT
169 key_put(new->uid_keyring);
170 key_put(new->session_keyring);
171 kmem_cache_free(uid_cachep, new);
172 } else {
173 uid_hash_insert(new, hashent);
174 up = new;
175 }
3fa97c9d 176 spin_unlock_irq(&uidhash_lock);
1da177e4 177 }
5cb350ba 178
1da177e4 179 return up;
8eb703e4 180
18b6e041 181 put_user_ns(new->user_ns);
8eb703e4
PE
182 kmem_cache_free(uid_cachep, new);
183out_unlock:
8eb703e4 184 return NULL;
1da177e4
LT
185}
186
1da177e4
LT
187static int __init uid_cache_init(void)
188{
189 int n;
190
191 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
20c2df83 192 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1da177e4
LT
193
194 for(n = 0; n < UIDHASH_SZ; ++n)
735de223 195 INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
1da177e4
LT
196
197 /* Insert the root user immediately (init already runs as root) */
3fa97c9d 198 spin_lock_irq(&uidhash_lock);
acce292c 199 uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
3fa97c9d 200 spin_unlock_irq(&uidhash_lock);
1da177e4
LT
201
202 return 0;
203}
204
205module_init(uid_cache_init);