Blame view

kernel/user.c 5.5 KB
457c89965   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   * 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>
8703e8a46   Ingo Molnar   sched/headers: Pr...
17
  #include <linux/sched/user.h>
4021cb279   Ingo Molnar   [PATCH] fix uidha...
18
  #include <linux/interrupt.h>
9984de1a5   Paul Gortmaker   kernel: Map most ...
19
  #include <linux/export.h>
acce292c8   Cedric Le Goater   user namespace: a...
20
  #include <linux/user_namespace.h>
0bb80f240   David Howells   proc: Split the n...
21
  #include <linux/proc_ns.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22

59607db36   Serge E. Hallyn   userns: add a use...
23
24
25
26
  /*
   * userns count is 1 for root user, 1 for init_uts_ns,
   * and 1 for... ?
   */
aee16ce73   Pavel Emelyanov   namespaces: clean...
27
  struct user_namespace init_user_ns = {
22d917d80   Eric W. Biederman   userns: Rework th...
28
29
  	.uid_map = {
  		.nr_extents = 1,
aa4bf44dc   Christian Brauner   userns: use union...
30
31
32
33
34
35
  		{
  			.extent[0] = {
  				.first = 0,
  				.lower_first = 0,
  				.count = 4294967295U,
  			},
22d917d80   Eric W. Biederman   userns: Rework th...
36
37
38
39
  		},
  	},
  	.gid_map = {
  		.nr_extents = 1,
aa4bf44dc   Christian Brauner   userns: use union...
40
41
42
43
44
45
  		{
  			.extent[0] = {
  				.first = 0,
  				.lower_first = 0,
  				.count = 4294967295U,
  			},
22d917d80   Eric W. Biederman   userns: Rework th...
46
47
  		},
  	},
f76d207a6   Eric W. Biederman   userns: Add kproj...
48
49
  	.projid_map = {
  		.nr_extents = 1,
aa4bf44dc   Christian Brauner   userns: use union...
50
51
52
53
54
55
  		{
  			.extent[0] = {
  				.first = 0,
  				.lower_first = 0,
  				.count = 4294967295U,
  			},
f76d207a6   Eric W. Biederman   userns: Add kproj...
56
57
  		},
  	},
c61a2810a   Eric W. Biederman   userns: Avoid rec...
58
  	.count = ATOMIC_INIT(3),
783291e69   Eric W. Biederman   userns: Simplify ...
59
60
  	.owner = GLOBAL_ROOT_UID,
  	.group = GLOBAL_ROOT_GID,
435d5f4bb   Al Viro   common object emb...
61
  	.ns.inum = PROC_USER_INIT_INO,
33c429405   Al Viro   copy address of p...
62
63
64
  #ifdef CONFIG_USER_NS
  	.ns.ops = &userns_operations,
  #endif
9cc46516d   Eric W. Biederman   userns: Add a kno...
65
  	.flags = USERNS_INIT_FLAGS,
b206f281d   David Howells   keys: Namespace k...
66
67
  #ifdef CONFIG_KEYS
  	.keyring_name_list = LIST_HEAD_INIT(init_user_ns.keyring_name_list),
0f44e4d97   David Howells   keys: Move the us...
68
  	.keyring_sem = __RWSEM_INITIALIZER(init_user_ns.keyring_sem),
f36f8c75a   David Howells   KEYS: Add per-use...
69
  #endif
aee16ce73   Pavel Emelyanov   namespaces: clean...
70
71
  };
  EXPORT_SYMBOL_GPL(init_user_ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
75
  /*
   * UID task count cache, to get fast user lookup in "alloc_uid"
   * when changing user ID's (ie setuid() and friends).
   */
7b44ab978   Eric W. Biederman   userns: Disassoci...
76
77
  #define UIDHASH_BITS	(CONFIG_BASE_SMALL ? 3 : 7)
  #define UIDHASH_SZ	(1 << UIDHASH_BITS)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
  #define UIDHASH_MASK		(UIDHASH_SZ - 1)
  #define __uidhashfn(uid)	(((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
7b44ab978   Eric W. Biederman   userns: Disassoci...
80
  #define uidhashentry(uid)	(uidhash_table + __uidhashfn((__kuid_val(uid))))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81

e18b890bb   Christoph Lameter   [PATCH] slab: rem...
82
  static struct kmem_cache *uid_cachep;
de83dbd97   Jason Yan   user.c: make uidh...
83
  static struct hlist_head uidhash_table[UIDHASH_SZ];
4021cb279   Ingo Molnar   [PATCH] fix uidha...
84
85
86
87
88
  
  /*
   * 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...
89
90
91
92
   * 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...
93
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  static DEFINE_SPINLOCK(uidhash_lock);
783291e69   Eric W. Biederman   userns: Simplify ...
95
  /* root_user.__count is 1, for init task cred */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
  struct user_struct root_user = {
fc3719127   Sebastian Andrzej Siewior   userns: use refco...
97
  	.__count	= REFCOUNT_INIT(1),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
  	.processes	= ATOMIC_INIT(1),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
  	.sigpending	= ATOMIC_INIT(0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
  	.locked_shm     = 0,
7b44ab978   Eric W. Biederman   userns: Disassoci...
101
  	.uid		= GLOBAL_ROOT_UID,
bef3efbeb   Tony Luck   efivarfs: Limit t...
102
  	.ratelimit	= RATELIMIT_STATE_INIT(root_user.ratelimit, 0, 0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
  };
5cb350baf   Dhaval Giani   sched: group sche...
104
105
106
  /*
   * These routines must be called with the uidhash spinlock held!
   */
40aeb400f   Alexey Dobriyan   user.c: deinline
107
  static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
5cb350baf   Dhaval Giani   sched: group sche...
108
109
110
  {
  	hlist_add_head(&up->uidhash_node, hashent);
  }
40aeb400f   Alexey Dobriyan   user.c: deinline
111
  static void uid_hash_remove(struct user_struct *up)
5cb350baf   Dhaval Giani   sched: group sche...
112
113
114
  {
  	hlist_del_init(&up->uidhash_node);
  }
7b44ab978   Eric W. Biederman   userns: Disassoci...
115
  static struct user_struct *uid_hash_find(kuid_t uid, struct hlist_head *hashent)
3959214f9   Kay Sievers   sched: delayed cl...
116
117
  {
  	struct user_struct *user;
3959214f9   Kay Sievers   sched: delayed cl...
118

b67bfe0d4   Sasha Levin   hlist: drop the n...
119
  	hlist_for_each_entry(user, hashent, uidhash_node) {
7b44ab978   Eric W. Biederman   userns: Disassoci...
120
  		if (uid_eq(user->uid, uid)) {
fc3719127   Sebastian Andrzej Siewior   userns: use refco...
121
  			refcount_inc(&user->__count);
3959214f9   Kay Sievers   sched: delayed cl...
122
123
124
125
126
127
  			return user;
  		}
  	}
  
  	return NULL;
  }
5cb350baf   Dhaval Giani   sched: group sche...
128
129
130
131
  /* 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: ...
132
  static void free_user(struct user_struct *up, unsigned long flags)
571428be5   Namhyung Kim   kernel/user.c: ad...
133
  	__releases(&uidhash_lock)
5cb350baf   Dhaval Giani   sched: group sche...
134
135
136
  {
  	uid_hash_remove(up);
  	spin_unlock_irqrestore(&uidhash_lock, flags);
5cb350baf   Dhaval Giani   sched: group sche...
137
138
  	kmem_cache_free(uid_cachep, up);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
143
144
  /*
   * 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.
   */
7b44ab978   Eric W. Biederman   userns: Disassoci...
145
  struct user_struct *find_user(kuid_t uid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
  {
  	struct user_struct *ret;
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
148
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149

3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
150
  	spin_lock_irqsave(&uidhash_lock, flags);
7b44ab978   Eric W. Biederman   userns: Disassoci...
151
  	ret = uid_hash_find(uid, uidhashentry(uid));
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
152
  	spin_unlock_irqrestore(&uidhash_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
154
155
156
157
  	return ret;
  }
  
  void free_uid(struct user_struct *up)
  {
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
158
  	unsigned long flags;
36f574135   Andrew Morton   [PATCH] free_uid(...
159
160
  	if (!up)
  		return;
ce0a568d3   Anna-Maria Gleixner   userns: use irqsa...
161
  	if (refcount_dec_and_lock_irqsave(&up->__count, &uidhash_lock, &flags))
5cb350baf   Dhaval Giani   sched: group sche...
162
  		free_user(up, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
  }
7b44ab978   Eric W. Biederman   userns: Disassoci...
164
  struct user_struct *alloc_uid(kuid_t uid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
  {
7b44ab978   Eric W. Biederman   userns: Disassoci...
166
  	struct hlist_head *hashent = uidhashentry(uid);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
167
  	struct user_struct *up, *new;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168

3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
169
  	spin_lock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  	up = uid_hash_find(uid, hashent);
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
171
  	spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172
173
  
  	if (!up) {
354a1f4d9   Andrew Morton   alloc_uid: cleanup
174
  		new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
175
  		if (!new)
6c4e121fd   Rasmus Villemoes   kernel/user.c: cl...
176
  			return NULL;
5e8869bb6   Pavel Emelyanov   sched: don't forg...
177

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  		new->uid = uid;
fc3719127   Sebastian Andrzej Siewior   userns: use refco...
179
  		refcount_set(&new->__count, 1);
bef3efbeb   Tony Luck   efivarfs: Limit t...
180
181
  		ratelimit_state_init(&new->ratelimit, HZ, 100);
  		ratelimit_set_flags(&new->ratelimit, RATELIMIT_MSG_ON_RELEASE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
186
  
  		/*
  		 * Before adding this, check whether we raced
  		 * on adding the same user already..
  		 */
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
187
  		spin_lock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
  		up = uid_hash_find(uid, hashent);
  		if (up) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
192
193
194
  			kmem_cache_free(uid_cachep, new);
  		} else {
  			uid_hash_insert(new, hashent);
  			up = new;
  		}
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
195
  		spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
  	}
5cb350baf   Dhaval Giani   sched: group sche...
197

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
198
199
  	return up;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
203
204
  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...
205
  			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
  
  	for(n = 0; n < UIDHASH_SZ; ++n)
7b44ab978   Eric W. Biederman   userns: Disassoci...
208
  		INIT_HLIST_HEAD(uidhash_table + n);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
210
  
  	/* Insert the root user immediately (init already runs as root) */
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
211
  	spin_lock_irq(&uidhash_lock);
7b44ab978   Eric W. Biederman   userns: Disassoci...
212
  	uid_hash_insert(&root_user, uidhashentry(GLOBAL_ROOT_UID));
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
213
  	spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
215
216
  
  	return 0;
  }
c96d6660d   Paul Gortmaker   kernel: audit/fix...
217
  subsys_initcall(uid_cache_init);