Blame view

kernel/user.c 4.84 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * The "user cache".
   *
   * (C) Copyright 1991-2000 Linus Torvalds
   *
   * We have a per-user structure to keep track of how many
   * processes, files etc the user has claimed, in order to be
   * able to have per-user limits for system resources. 
   */
  
  #include <linux/init.h>
  #include <linux/sched.h>
  #include <linux/slab.h>
  #include <linux/bitops.h>
  #include <linux/key.h>
4021cb279   Ingo Molnar   [PATCH] fix uidha...
16
  #include <linux/interrupt.h>
9984de1a5   Paul Gortmaker   kernel: Map most ...
17
  #include <linux/export.h>
acce292c8   Cedric Le Goater   user namespace: a...
18
  #include <linux/user_namespace.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19

59607db36   Serge E. Hallyn   userns: add a use...
20
21
22
23
  /*
   * userns count is 1 for root user, 1 for init_uts_ns,
   * and 1 for... ?
   */
aee16ce73   Pavel Emelyanov   namespaces: clean...
24
25
  struct user_namespace init_user_ns = {
  	.kref = {
59607db36   Serge E. Hallyn   userns: add a use...
26
  		.refcount	= ATOMIC_INIT(3),
aee16ce73   Pavel Emelyanov   namespaces: clean...
27
  	},
18b6e0414   Serge Hallyn   User namespaces: ...
28
  	.creator = &root_user,
aee16ce73   Pavel Emelyanov   namespaces: clean...
29
30
  };
  EXPORT_SYMBOL_GPL(init_user_ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
34
  /*
   * UID task count cache, to get fast user lookup in "alloc_uid"
   * when changing user ID's (ie setuid() and friends).
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
  #define UIDHASH_MASK		(UIDHASH_SZ - 1)
  #define __uidhashfn(uid)	(((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
acce292c8   Cedric Le Goater   user namespace: a...
37
  #define uidhashentry(ns, uid)	((ns)->uidhash_table + __uidhashfn((uid)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38

e18b890bb   Christoph Lameter   [PATCH] slab: rem...
39
  static struct kmem_cache *uid_cachep;
4021cb279   Ingo Molnar   [PATCH] fix uidha...
40
41
42
43
44
  
  /*
   * The uidhash_lock is mostly taken from process context, but it is
   * occasionally also taken from softirq/tasklet context, when
   * task-structs get RCU-freed. Hence all locking must be softirq-safe.
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
45
46
47
48
   * But free_uid() is also called with local interrupts disabled, and running
   * local_bh_enable() with local interrupts disabled is an error - we'll run
   * softirq callbacks, and they can unconditionally enable interrupts, and
   * the caller of free_uid() didn't expect that..
4021cb279   Ingo Molnar   [PATCH] fix uidha...
49
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
  static DEFINE_SPINLOCK(uidhash_lock);
59607db36   Serge E. Hallyn   userns: add a use...
51
  /* root_user.__count is 2, 1 for init task cred, 1 for init_user_ns->user_ns */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
  struct user_struct root_user = {
18b6e0414   Serge Hallyn   User namespaces: ...
53
  	.__count	= ATOMIC_INIT(2),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
  	.processes	= ATOMIC_INIT(1),
  	.files		= ATOMIC_INIT(0),
  	.sigpending	= ATOMIC_INIT(0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
  	.locked_shm     = 0,
18b6e0414   Serge Hallyn   User namespaces: ...
58
  	.user_ns	= &init_user_ns,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  };
5cb350baf   Dhaval Giani   sched: group sche...
60
61
62
  /*
   * These routines must be called with the uidhash spinlock held!
   */
40aeb400f   Alexey Dobriyan   user.c: deinline
63
  static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
5cb350baf   Dhaval Giani   sched: group sche...
64
65
66
  {
  	hlist_add_head(&up->uidhash_node, hashent);
  }
40aeb400f   Alexey Dobriyan   user.c: deinline
67
  static void uid_hash_remove(struct user_struct *up)
5cb350baf   Dhaval Giani   sched: group sche...
68
69
  {
  	hlist_del_init(&up->uidhash_node);
fb5ae64fd   Serge E. Hallyn   User namespaces: ...
70
  	put_user_ns(up->user_ns);
5cb350baf   Dhaval Giani   sched: group sche...
71
  }
3959214f9   Kay Sievers   sched: delayed cl...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
  {
  	struct user_struct *user;
  	struct hlist_node *h;
  
  	hlist_for_each_entry(user, h, hashent, uidhash_node) {
  		if (user->uid == uid) {
  			atomic_inc(&user->__count);
  			return user;
  		}
  	}
  
  	return NULL;
  }
5cb350baf   Dhaval Giani   sched: group sche...
86
87
88
89
  /* IRQs are disabled and uidhash_lock is held upon function entry.
   * IRQ state (as stored in flags) is restored and uidhash_lock released
   * upon function exit.
   */
18b6e0414   Serge Hallyn   User namespaces: ...
90
  static void free_user(struct user_struct *up, unsigned long flags)
571428be5   Namhyung Kim   kernel/user.c: ad...
91
  	__releases(&uidhash_lock)
5cb350baf   Dhaval Giani   sched: group sche...
92
93
94
  {
  	uid_hash_remove(up);
  	spin_unlock_irqrestore(&uidhash_lock, flags);
5cb350baf   Dhaval Giani   sched: group sche...
95
96
97
98
  	key_put(up->uid_keyring);
  	key_put(up->session_keyring);
  	kmem_cache_free(uid_cachep, up);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
103
104
105
106
107
  /*
   * Locate the user_struct for the passed UID.  If found, take a ref on it.  The
   * caller must undo that ref with free_uid().
   *
   * If the user_struct could not be found, return NULL.
   */
  struct user_struct *find_user(uid_t uid)
  {
  	struct user_struct *ret;
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
108
  	unsigned long flags;
6ded6ab9b   Serge Hallyn   User namespaces: ...
109
  	struct user_namespace *ns = current_user_ns();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110

3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
111
  	spin_lock_irqsave(&uidhash_lock, flags);
acce292c8   Cedric Le Goater   user namespace: a...
112
  	ret = uid_hash_find(uid, uidhashentry(ns, uid));
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
113
  	spin_unlock_irqrestore(&uidhash_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
116
117
118
  	return ret;
  }
  
  void free_uid(struct user_struct *up)
  {
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
119
  	unsigned long flags;
36f574135   Andrew Morton   [PATCH] free_uid(...
120
121
  	if (!up)
  		return;
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
122
  	local_irq_save(flags);
5cb350baf   Dhaval Giani   sched: group sche...
123
124
125
  	if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  		free_user(up, flags);
  	else
36f574135   Andrew Morton   [PATCH] free_uid(...
126
  		local_irq_restore(flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
  }
354a1f4d9   Andrew Morton   alloc_uid: cleanup
128
  struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
  {
735de2230   Pavel Emelyanov   Convert uid hash ...
130
  	struct hlist_head *hashent = uidhashentry(ns, uid);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
131
  	struct user_struct *up, *new;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132

3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
133
  	spin_lock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
  	up = uid_hash_find(uid, hashent);
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
135
  	spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
  
  	if (!up) {
354a1f4d9   Andrew Morton   alloc_uid: cleanup
138
  		new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
139
140
  		if (!new)
  			goto out_unlock;
5e8869bb6   Pavel Emelyanov   sched: don't forg...
141

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
143
  		new->uid = uid;
  		atomic_set(&new->__count, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144

18b6e0414   Serge Hallyn   User namespaces: ...
145
  		new->user_ns = get_user_ns(ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
148
149
  		/*
  		 * Before adding this, check whether we raced
  		 * on adding the same user already..
  		 */
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
150
  		spin_lock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
  		up = uid_hash_find(uid, hashent);
  		if (up) {
4ef9e11d6   Hillf Danton   fix freeing user_...
153
  			put_user_ns(ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
156
157
158
159
160
  			key_put(new->uid_keyring);
  			key_put(new->session_keyring);
  			kmem_cache_free(uid_cachep, new);
  		} else {
  			uid_hash_insert(new, hashent);
  			up = new;
  		}
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
161
  		spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  	}
5cb350baf   Dhaval Giani   sched: group sche...
163

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
  	return up;
8eb703e4f   Pavel Emelyanov   uids: merge multi...
165

8eb703e4f   Pavel Emelyanov   uids: merge multi...
166
  out_unlock:
8eb703e4f   Pavel Emelyanov   uids: merge multi...
167
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
172
173
  static int __init uid_cache_init(void)
  {
  	int n;
  
  	uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
20c2df83d   Paul Mundt   mm: Remove slab d...
174
  			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
176
  
  	for(n = 0; n < UIDHASH_SZ; ++n)
735de2230   Pavel Emelyanov   Convert uid hash ...
177
  		INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
  
  	/* Insert the root user immediately (init already runs as root) */
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
180
  	spin_lock_irq(&uidhash_lock);
acce292c8   Cedric Le Goater   user namespace: a...
181
  	uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
182
  	spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
185
186
187
  
  	return 0;
  }
  
  module_init(uid_cache_init);