Blame view

kernel/user.c 5.09 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>
acce292c8   Cedric Le Goater   user namespace: a...
17
18
  #include <linux/module.h>
  #include <linux/user_namespace.h>
d84f4f992   David Howells   CRED: Inaugurate ...
19
  #include "cred-internals.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20

aee16ce73   Pavel Emelyanov   namespaces: clean...
21
22
  struct user_namespace init_user_ns = {
  	.kref = {
1d1e97562   Serge E. Hallyn   keys: distinguish...
23
  		.refcount	= ATOMIC_INIT(2),
aee16ce73   Pavel Emelyanov   namespaces: clean...
24
  	},
18b6e0414   Serge Hallyn   User namespaces: ...
25
  	.creator = &root_user,
aee16ce73   Pavel Emelyanov   namespaces: clean...
26
27
  };
  EXPORT_SYMBOL_GPL(init_user_ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
  /*
   * 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
32
33
  #define UIDHASH_MASK		(UIDHASH_SZ - 1)
  #define __uidhashfn(uid)	(((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
acce292c8   Cedric Le Goater   user namespace: a...
34
  #define uidhashentry(ns, uid)	((ns)->uidhash_table + __uidhashfn((uid)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35

e18b890bb   Christoph Lameter   [PATCH] slab: rem...
36
  static struct kmem_cache *uid_cachep;
4021cb279   Ingo Molnar   [PATCH] fix uidha...
37
38
39
40
41
  
  /*
   * 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...
42
43
44
45
   * 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...
46
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  static DEFINE_SPINLOCK(uidhash_lock);
18b6e0414   Serge Hallyn   User namespaces: ...
48
  /* root_user.__count is 2, 1 for init task cred, 1 for init_user_ns->creator */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
  struct user_struct root_user = {
18b6e0414   Serge Hallyn   User namespaces: ...
50
  	.__count	= ATOMIC_INIT(2),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
  	.processes	= ATOMIC_INIT(1),
  	.files		= ATOMIC_INIT(0),
  	.sigpending	= ATOMIC_INIT(0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
  	.locked_shm     = 0,
18b6e0414   Serge Hallyn   User namespaces: ...
55
  	.user_ns	= &init_user_ns,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
  };
5cb350baf   Dhaval Giani   sched: group sche...
57
58
59
  /*
   * These routines must be called with the uidhash spinlock held!
   */
40aeb400f   Alexey Dobriyan   user.c: deinline
60
  static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
5cb350baf   Dhaval Giani   sched: group sche...
61
62
63
  {
  	hlist_add_head(&up->uidhash_node, hashent);
  }
40aeb400f   Alexey Dobriyan   user.c: deinline
64
  static void uid_hash_remove(struct user_struct *up)
5cb350baf   Dhaval Giani   sched: group sche...
65
66
  {
  	hlist_del_init(&up->uidhash_node);
fb5ae64fd   Serge E. Hallyn   User namespaces: ...
67
  	put_user_ns(up->user_ns);
5cb350baf   Dhaval Giani   sched: group sche...
68
  }
3959214f9   Kay Sievers   sched: delayed cl...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  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...
83
84
85
86
  /* 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: ...
87
  static void free_user(struct user_struct *up, unsigned long flags)
5cb350baf   Dhaval Giani   sched: group sche...
88
89
90
  {
  	uid_hash_remove(up);
  	spin_unlock_irqrestore(&uidhash_lock, flags);
5cb350baf   Dhaval Giani   sched: group sche...
91
92
93
94
  	key_put(up->uid_keyring);
  	key_put(up->session_keyring);
  	kmem_cache_free(uid_cachep, up);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
99
100
101
102
103
  /*
   * 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...
104
  	unsigned long flags;
6ded6ab9b   Serge Hallyn   User namespaces: ...
105
  	struct user_namespace *ns = current_user_ns();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106

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

eb41d9465   Kay Sievers   fix struct user_i...
129
  	/* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
5cb350baf   Dhaval Giani   sched: group sche...
130
131
  	 * atomic.
  	 */
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
132
  	spin_lock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
  	up = uid_hash_find(uid, hashent);
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
134
  	spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
136
  
  	if (!up) {
354a1f4d9   Andrew Morton   alloc_uid: cleanup
137
  		new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
138
139
  		if (!new)
  			goto out_unlock;
5e8869bb6   Pavel Emelyanov   sched: don't forg...
140

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

18b6e0414   Serge Hallyn   User namespaces: ...
144
  		new->user_ns = get_user_ns(ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
147
148
  		/*
  		 * Before adding this, check whether we raced
  		 * on adding the same user already..
  		 */
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
149
  		spin_lock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
  		up = uid_hash_find(uid, hashent);
  		if (up) {
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
152
  			/* This case is not possible when CONFIG_USER_SCHED
5cb350baf   Dhaval Giani   sched: group sche...
153
154
155
156
  			 * is defined, since we serialize alloc_uid() using
  			 * uids_mutex. Hence no need to call
  			 * sched_destroy_user() or remove_user_sysfs_dir().
  			 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
161
162
163
  			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...
164
  		spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
  	}
5cb350baf   Dhaval Giani   sched: group sche...
166

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

18b6e0414   Serge Hallyn   User namespaces: ...
169
  	put_user_ns(new->user_ns);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
170
171
  	kmem_cache_free(uid_cachep, new);
  out_unlock:
8eb703e4f   Pavel Emelyanov   uids: merge multi...
172
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
176
177
178
  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...
179
  			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
  
  	for(n = 0; n < UIDHASH_SZ; ++n)
735de2230   Pavel Emelyanov   Convert uid hash ...
182
  		INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
  
  	/* Insert the root user immediately (init already runs as root) */
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
185
  	spin_lock_irq(&uidhash_lock);
acce292c8   Cedric Le Goater   user namespace: a...
186
  	uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
187
  	spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
190
191
192
  
  	return 0;
  }
  
  module_init(uid_cache_init);