Blame view

kernel/user_namespace.c 35.6 KB
b886d83c5   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
acce292c8   Cedric Le Goater   user namespace: a...
2

9984de1a5   Paul Gortmaker   kernel: Map most ...
3
  #include <linux/export.h>
acce292c8   Cedric Le Goater   user namespace: a...
4
  #include <linux/nsproxy.h>
1aeb272cf   Robert P. J. Day   kernel: explicitl...
5
  #include <linux/slab.h>
3f07c0144   Ingo Molnar   sched/headers: Pr...
6
  #include <linux/sched/signal.h>
acce292c8   Cedric Le Goater   user namespace: a...
7
  #include <linux/user_namespace.h>
0bb80f240   David Howells   proc: Split the n...
8
  #include <linux/proc_ns.h>
5c1469de7   Eric W. Biederman   user_ns: Introduc...
9
  #include <linux/highuid.h>
18b6e0414   Serge Hallyn   User namespaces: ...
10
  #include <linux/cred.h>
973c59142   Eric W. Biederman   userns: Start out...
11
  #include <linux/securebits.h>
22d917d80   Eric W. Biederman   userns: Rework th...
12
13
14
15
16
17
18
  #include <linux/keyctl.h>
  #include <linux/key-type.h>
  #include <keys/user-type.h>
  #include <linux/seq_file.h>
  #include <linux/fs.h>
  #include <linux/uaccess.h>
  #include <linux/ctype.h>
f76d207a6   Eric W. Biederman   userns: Add kproj...
19
  #include <linux/projid.h>
e66eded83   Eric W. Biederman   userns: Don't all...
20
  #include <linux/fs_struct.h>
6397fac49   Christian Brauner   userns: bump idma...
21
22
  #include <linux/bsearch.h>
  #include <linux/sort.h>
acce292c8   Cedric Le Goater   user namespace: a...
23

6164281ab   Pavel Emelyanov   user_ns: improve ...
24
  static struct kmem_cache *user_ns_cachep __read_mostly;
f0d62aec9   Eric W. Biederman   userns: Rename id...
25
  static DEFINE_MUTEX(userns_state_mutex);
6164281ab   Pavel Emelyanov   user_ns: improve ...
26

6708075f1   Eric W. Biederman   userns: Don't let...
27
28
  static bool new_idmap_permitted(const struct file *file,
  				struct user_namespace *ns, int cap_setid,
22d917d80   Eric W. Biederman   userns: Rework th...
29
  				struct uid_gid_map *map);
b032132c3   Eric W. Biederman   userns: Free user...
30
  static void free_user_ns(struct work_struct *work);
22d917d80   Eric W. Biederman   userns: Rework th...
31

25f9c0817   Eric W. Biederman   userns: Generaliz...
32
33
34
35
36
37
38
39
40
  static struct ucounts *inc_user_namespaces(struct user_namespace *ns, kuid_t uid)
  {
  	return inc_ucount(ns, uid, UCOUNT_USER_NAMESPACES);
  }
  
  static void dec_user_namespaces(struct ucounts *ucounts)
  {
  	return dec_ucount(ucounts, UCOUNT_USER_NAMESPACES);
  }
cde1975bc   Eric W. Biederman   userns: Implent p...
41
42
43
44
45
46
47
48
49
  static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
  {
  	/* Start with the same capabilities as init but useless for doing
  	 * anything as the capabilities are bound to the new user namespace.
  	 */
  	cred->securebits = SECUREBITS_DEFAULT;
  	cred->cap_inheritable = CAP_EMPTY_SET;
  	cred->cap_permitted = CAP_FULL_SET;
  	cred->cap_effective = CAP_FULL_SET;
58319057b   Andy Lutomirski   capabilities: amb...
50
  	cred->cap_ambient = CAP_EMPTY_SET;
cde1975bc   Eric W. Biederman   userns: Implent p...
51
52
53
54
55
56
57
58
  	cred->cap_bset = CAP_FULL_SET;
  #ifdef CONFIG_KEYS
  	key_put(cred->request_key_auth);
  	cred->request_key_auth = NULL;
  #endif
  	/* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
  	cred->user_ns = user_ns;
  }
6c3d4da8e   Eric W. Biederman   ucounts: Fix syst...
59
60
61
62
63
64
65
66
67
68
69
  static unsigned long enforced_nproc_rlimit(void)
  {
  	unsigned long limit = RLIM_INFINITY;
  
  	/* Is RLIMIT_NPROC currently enforced? */
  	if (!uid_eq(current_uid(), GLOBAL_ROOT_UID) ||
  	    (current_user_ns() != &init_user_ns))
  		limit = rlimit(RLIMIT_NPROC);
  
  	return limit;
  }
77ec739d8   Serge E. Hallyn   user namespace: a...
70
  /*
18b6e0414   Serge Hallyn   User namespaces: ...
71
72
73
74
75
76
   * Create a new user namespace, deriving the creator from the user in the
   * passed credentials, and replacing that user with the new root user for the
   * new namespace.
   *
   * This is called by copy_creds(), which will finish setting the target task's
   * credentials.
77ec739d8   Serge E. Hallyn   user namespace: a...
77
   */
18b6e0414   Serge Hallyn   User namespaces: ...
78
  int create_user_ns(struct cred *new)
77ec739d8   Serge E. Hallyn   user namespace: a...
79
  {
0093ccb68   Eric W. Biederman   cred: Refcount th...
80
  	struct user_namespace *ns, *parent_ns = new->user_ns;
078de5f70   Eric W. Biederman   userns: Store uid...
81
82
  	kuid_t owner = new->euid;
  	kgid_t group = new->egid;
f6b2db1a3   Eric W. Biederman   userns: Make the ...
83
  	struct ucounts *ucounts;
25f9c0817   Eric W. Biederman   userns: Generaliz...
84
  	int ret, i;
783291e69   Eric W. Biederman   userns: Simplify ...
85

df75e7748   Eric W. Biederman   userns: When the ...
86
  	ret = -ENOSPC;
8742f229b   Oleg Nesterov   userns: limit the...
87
  	if (parent_ns->level > 32)
b376c3e1b   Eric W. Biederman   userns: Add a lim...
88
  		goto fail;
f6b2db1a3   Eric W. Biederman   userns: Make the ...
89
90
  	ucounts = inc_user_namespaces(parent_ns, owner);
  	if (!ucounts)
b376c3e1b   Eric W. Biederman   userns: Add a lim...
91
  		goto fail;
8742f229b   Oleg Nesterov   userns: limit the...
92

3151527ee   Eric W. Biederman   userns: Don't al...
93
94
95
  	/*
  	 * Verify that we can not violate the policy of which files
  	 * may be accessed that is specified by the root directory,
a12f4f85b   Xiaofeng Cao   kernel/user_names...
96
  	 * by verifying that the root directory is at the root of the
3151527ee   Eric W. Biederman   userns: Don't al...
97
98
  	 * mount namespace which allows all files to be accessed.
  	 */
b376c3e1b   Eric W. Biederman   userns: Add a lim...
99
  	ret = -EPERM;
3151527ee   Eric W. Biederman   userns: Don't al...
100
  	if (current_chrooted())
b376c3e1b   Eric W. Biederman   userns: Add a lim...
101
  		goto fail_dec;
3151527ee   Eric W. Biederman   userns: Don't al...
102

783291e69   Eric W. Biederman   userns: Simplify ...
103
104
105
106
  	/* The creator needs a mapping in the parent user namespace
  	 * or else we won't be able to reasonably tell userspace who
  	 * created a user_namespace.
  	 */
b376c3e1b   Eric W. Biederman   userns: Add a lim...
107
  	ret = -EPERM;
783291e69   Eric W. Biederman   userns: Simplify ...
108
109
  	if (!kuid_has_mapping(parent_ns, owner) ||
  	    !kgid_has_mapping(parent_ns, group))
b376c3e1b   Eric W. Biederman   userns: Add a lim...
110
  		goto fail_dec;
77ec739d8   Serge E. Hallyn   user namespace: a...
111

b376c3e1b   Eric W. Biederman   userns: Add a lim...
112
  	ret = -ENOMEM;
22d917d80   Eric W. Biederman   userns: Rework th...
113
  	ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
77ec739d8   Serge E. Hallyn   user namespace: a...
114
  	if (!ns)
b376c3e1b   Eric W. Biederman   userns: Add a lim...
115
  		goto fail_dec;
77ec739d8   Serge E. Hallyn   user namespace: a...
116

db2e718a4   Serge E. Hallyn   capabilities: req...
117
  	ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
6344c433a   Al Viro   new helpers: ns_a...
118
  	ret = ns_alloc_inum(&ns->ns);
b376c3e1b   Eric W. Biederman   userns: Add a lim...
119
120
  	if (ret)
  		goto fail_free;
33c429405   Al Viro   copy address of p...
121
  	ns->ns.ops = &userns_operations;
98f842e67   Eric W. Biederman   proc: Usable inod...
122

265cbd62e   Kirill Tkhai   user: Use generic...
123
  	refcount_set(&ns->ns.count, 1);
cde1975bc   Eric W. Biederman   userns: Implent p...
124
  	/* Leave the new->user_ns reference with the new user namespace. */
aeb3ae9da   Eric W. Biederman   userns: Add an ex...
125
  	ns->parent = parent_ns;
8742f229b   Oleg Nesterov   userns: limit the...
126
  	ns->level = parent_ns->level + 1;
783291e69   Eric W. Biederman   userns: Simplify ...
127
128
  	ns->owner = owner;
  	ns->group = group;
b032132c3   Eric W. Biederman   userns: Free user...
129
  	INIT_WORK(&ns->work, free_user_ns);
21d1c5e38   Alexey Gladkov   Reimplement RLIMI...
130
  	for (i = 0; i < MAX_PER_NAMESPACE_UCOUNTS; i++) {
25f9c0817   Eric W. Biederman   userns: Generaliz...
131
132
  		ns->ucount_max[i] = INT_MAX;
  	}
6c3d4da8e   Eric W. Biederman   ucounts: Fix syst...
133
  	set_rlimit_ucount_max(ns, UCOUNT_RLIMIT_NPROC, enforced_nproc_rlimit());
c1ada3dc7   Alexey Gladkov   ucounts: Set ucou...
134
135
136
  	set_rlimit_ucount_max(ns, UCOUNT_RLIMIT_MSGQUEUE, rlimit(RLIMIT_MSGQUEUE));
  	set_rlimit_ucount_max(ns, UCOUNT_RLIMIT_SIGPENDING, rlimit(RLIMIT_SIGPENDING));
  	set_rlimit_ucount_max(ns, UCOUNT_RLIMIT_MEMLOCK, rlimit(RLIMIT_MEMLOCK));
f6b2db1a3   Eric W. Biederman   userns: Make the ...
137
  	ns->ucounts = ucounts;
22d917d80   Eric W. Biederman   userns: Rework th...
138

9cc46516d   Eric W. Biederman   userns: Add a kno...
139
140
141
142
  	/* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
  	mutex_lock(&userns_state_mutex);
  	ns->flags = parent_ns->flags;
  	mutex_unlock(&userns_state_mutex);
b206f281d   David Howells   keys: Namespace k...
143
144
  #ifdef CONFIG_KEYS
  	INIT_LIST_HEAD(&ns->keyring_name_list);
0f44e4d97   David Howells   keys: Move the us...
145
  	init_rwsem(&ns->keyring_sem);
f36f8c75a   David Howells   KEYS: Add per-use...
146
  #endif
dbec28460   Eric W. Biederman   userns: Add per u...
147
148
149
150
151
  	ret = -ENOMEM;
  	if (!setup_userns_sysctls(ns))
  		goto fail_keyring;
  
  	set_cred_user_ns(new, ns);
18b6e0414   Serge Hallyn   User namespaces: ...
152
  	return 0;
dbec28460   Eric W. Biederman   userns: Add per u...
153
154
155
156
157
  fail_keyring:
  #ifdef CONFIG_PERSISTENT_KEYRINGS
  	key_put(ns->persistent_keyring_register);
  #endif
  	ns_free_inum(&ns->ns);
b376c3e1b   Eric W. Biederman   userns: Add a lim...
158
  fail_free:
dbec28460   Eric W. Biederman   userns: Add per u...
159
  	kmem_cache_free(user_ns_cachep, ns);
b376c3e1b   Eric W. Biederman   userns: Add a lim...
160
  fail_dec:
f6b2db1a3   Eric W. Biederman   userns: Make the ...
161
  	dec_user_namespaces(ucounts);
b376c3e1b   Eric W. Biederman   userns: Add a lim...
162
  fail:
dbec28460   Eric W. Biederman   userns: Add per u...
163
  	return ret;
acce292c8   Cedric Le Goater   user namespace: a...
164
  }
b2e0d9870   Eric W. Biederman   userns: Implement...
165
166
167
  int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
  {
  	struct cred *cred;
6160968ce   Oleg Nesterov   userns: unshare_u...
168
  	int err = -ENOMEM;
b2e0d9870   Eric W. Biederman   userns: Implement...
169
170
171
172
173
  
  	if (!(unshare_flags & CLONE_NEWUSER))
  		return 0;
  
  	cred = prepare_creds();
6160968ce   Oleg Nesterov   userns: unshare_u...
174
175
176
177
178
179
180
  	if (cred) {
  		err = create_user_ns(cred);
  		if (err)
  			put_cred(cred);
  		else
  			*new_cred = cred;
  	}
b2e0d9870   Eric W. Biederman   userns: Implement...
181

6160968ce   Oleg Nesterov   userns: unshare_u...
182
  	return err;
b2e0d9870   Eric W. Biederman   userns: Implement...
183
  }
b032132c3   Eric W. Biederman   userns: Free user...
184
  static void free_user_ns(struct work_struct *work)
acce292c8   Cedric Le Goater   user namespace: a...
185
  {
b032132c3   Eric W. Biederman   userns: Free user...
186
187
  	struct user_namespace *parent, *ns =
  		container_of(work, struct user_namespace, work);
783291e69   Eric W. Biederman   userns: Simplify ...
188

c61a2810a   Eric W. Biederman   userns: Avoid rec...
189
  	do {
f6b2db1a3   Eric W. Biederman   userns: Make the ...
190
  		struct ucounts *ucounts = ns->ucounts;
c61a2810a   Eric W. Biederman   userns: Avoid rec...
191
  		parent = ns->parent;
6397fac49   Christian Brauner   userns: bump idma...
192
193
194
195
196
197
198
199
200
201
202
203
  		if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
  			kfree(ns->gid_map.forward);
  			kfree(ns->gid_map.reverse);
  		}
  		if (ns->uid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
  			kfree(ns->uid_map.forward);
  			kfree(ns->uid_map.reverse);
  		}
  		if (ns->projid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
  			kfree(ns->projid_map.forward);
  			kfree(ns->projid_map.reverse);
  		}
dbec28460   Eric W. Biederman   userns: Add per u...
204
  		retire_userns_sysctls(ns);
b206f281d   David Howells   keys: Namespace k...
205
  		key_free_user_ns(ns);
6344c433a   Al Viro   new helpers: ns_a...
206
  		ns_free_inum(&ns->ns);
c61a2810a   Eric W. Biederman   userns: Avoid rec...
207
  		kmem_cache_free(user_ns_cachep, ns);
f6b2db1a3   Eric W. Biederman   userns: Make the ...
208
  		dec_user_namespaces(ucounts);
c61a2810a   Eric W. Biederman   userns: Avoid rec...
209
  		ns = parent;
265cbd62e   Kirill Tkhai   user: Use generic...
210
  	} while (refcount_dec_and_test(&parent->ns.count));
acce292c8   Cedric Le Goater   user namespace: a...
211
  }
b032132c3   Eric W. Biederman   userns: Free user...
212
213
214
215
216
217
  
  void __put_user_ns(struct user_namespace *ns)
  {
  	schedule_work(&ns->work);
  }
  EXPORT_SYMBOL(__put_user_ns);
5c1469de7   Eric W. Biederman   user_ns: Introduc...
218

6397fac49   Christian Brauner   userns: bump idma...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  /**
   * idmap_key struct holds the information necessary to find an idmapping in a
   * sorted idmap array. It is passed to cmp_map_id() as first argument.
   */
  struct idmap_key {
  	bool map_up; /* true  -> id from kid; false -> kid from id */
  	u32 id; /* id to find */
  	u32 count; /* == 0 unless used with map_id_range_down() */
  };
  
  /**
   * cmp_map_id - Function to be passed to bsearch() to find the requested
   * idmapping. Expects struct idmap_key to be passed via @k.
   */
  static int cmp_map_id(const void *k, const void *e)
  {
  	u32 first, last, id2;
  	const struct idmap_key *key = k;
  	const struct uid_gid_extent *el = e;
11a8b9270   Eric W. Biederman   userns: Don't spe...
238
  	id2 = key->id + key->count - 1;
6397fac49   Christian Brauner   userns: bump idma...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  
  	/* handle map_id_{down,up}() */
  	if (key->map_up)
  		first = el->lower_first;
  	else
  		first = el->first;
  
  	last = first + el->count - 1;
  
  	if (key->id >= first && key->id <= last &&
  	    (id2 >= first && id2 <= last))
  		return 0;
  
  	if (key->id < first || id2 < first)
  		return -1;
  
  	return 1;
  }
  
  /**
   * map_id_range_down_max - Find idmap via binary search in ordered idmap array.
   * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
   */
3edf652fa   Eric W. Biederman   userns: Simplify ...
262
263
  static struct uid_gid_extent *
  map_id_range_down_max(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
5c1469de7   Eric W. Biederman   user_ns: Introduc...
264
  {
6397fac49   Christian Brauner   userns: bump idma...
265
266
267
268
269
  	struct idmap_key key;
  
  	key.map_up = false;
  	key.count = count;
  	key.id = id;
3edf652fa   Eric W. Biederman   userns: Simplify ...
270
271
  	return bsearch(&key, map->forward, extents,
  		       sizeof(struct uid_gid_extent), cmp_map_id);
6397fac49   Christian Brauner   userns: bump idma...
272
273
274
275
276
277
278
  }
  
  /**
   * map_id_range_down_base - Find idmap via binary search in static extent array.
   * Can only be called if number of mappings is equal or less than
   * UID_GID_MAP_MAX_BASE_EXTENTS.
   */
3edf652fa   Eric W. Biederman   userns: Simplify ...
279
280
  static struct uid_gid_extent *
  map_id_range_down_base(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
5c1469de7   Eric W. Biederman   user_ns: Introduc...
281
  {
3edf652fa   Eric W. Biederman   userns: Simplify ...
282
  	unsigned idx;
22d917d80   Eric W. Biederman   userns: Rework th...
283
  	u32 first, last, id2;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
284

22d917d80   Eric W. Biederman   userns: Rework th...
285
  	id2 = id + count - 1;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
286

22d917d80   Eric W. Biederman   userns: Rework th...
287
  	/* Find the matching extent */
22d917d80   Eric W. Biederman   userns: Rework th...
288
289
290
291
292
  	for (idx = 0; idx < extents; idx++) {
  		first = map->extent[idx].first;
  		last = first + map->extent[idx].count - 1;
  		if (id >= first && id <= last &&
  		    (id2 >= first && id2 <= last))
3edf652fa   Eric W. Biederman   userns: Simplify ...
293
  			return &map->extent[idx];
22d917d80   Eric W. Biederman   userns: Rework th...
294
  	}
3edf652fa   Eric W. Biederman   userns: Simplify ...
295
  	return NULL;
22d917d80   Eric W. Biederman   userns: Rework th...
296
  }
6397fac49   Christian Brauner   userns: bump idma...
297
298
  static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
  {
3edf652fa   Eric W. Biederman   userns: Simplify ...
299
300
  	struct uid_gid_extent *extent;
  	unsigned extents = map->nr_extents;
6397fac49   Christian Brauner   userns: bump idma...
301
302
303
  	smp_rmb();
  
  	if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
3edf652fa   Eric W. Biederman   userns: Simplify ...
304
305
306
  		extent = map_id_range_down_base(extents, map, id, count);
  	else
  		extent = map_id_range_down_max(extents, map, id, count);
22d917d80   Eric W. Biederman   userns: Rework th...
307
  	/* Map the id or note failure */
3edf652fa   Eric W. Biederman   userns: Simplify ...
308
309
  	if (extent)
  		id = (id - extent->first) + extent->lower_first;
22d917d80   Eric W. Biederman   userns: Rework th...
310
311
312
313
314
315
316
317
  	else
  		id = (u32) -1;
  
  	return id;
  }
  
  static u32 map_id_down(struct uid_gid_map *map, u32 id)
  {
ece661339   Eric W. Biederman   userns: Make map_...
318
  	return map_id_range_down(map, id, 1);
6397fac49   Christian Brauner   userns: bump idma...
319
320
321
322
323
324
325
  }
  
  /**
   * map_id_up_base - Find idmap via binary search in static extent array.
   * Can only be called if number of mappings is equal or less than
   * UID_GID_MAP_MAX_BASE_EXTENTS.
   */
3edf652fa   Eric W. Biederman   userns: Simplify ...
326
327
  static struct uid_gid_extent *
  map_id_up_base(unsigned extents, struct uid_gid_map *map, u32 id)
22d917d80   Eric W. Biederman   userns: Rework th...
328
  {
3edf652fa   Eric W. Biederman   userns: Simplify ...
329
  	unsigned idx;
22d917d80   Eric W. Biederman   userns: Rework th...
330
331
332
  	u32 first, last;
  
  	/* Find the matching extent */
22d917d80   Eric W. Biederman   userns: Rework th...
333
  	for (idx = 0; idx < extents; idx++) {
22d917d80   Eric W. Biederman   userns: Rework th...
334
  		first = map->extent[idx].lower_first;
22d917d80   Eric W. Biederman   userns: Rework th...
335
336
  		last = first + map->extent[idx].count - 1;
  		if (id >= first && id <= last)
3edf652fa   Eric W. Biederman   userns: Simplify ...
337
  			return &map->extent[idx];
22d917d80   Eric W. Biederman   userns: Rework th...
338
  	}
3edf652fa   Eric W. Biederman   userns: Simplify ...
339
  	return NULL;
22d917d80   Eric W. Biederman   userns: Rework th...
340
  }
22d917d80   Eric W. Biederman   userns: Rework th...
341

22d917d80   Eric W. Biederman   userns: Rework th...
342
  /**
6397fac49   Christian Brauner   userns: bump idma...
343
344
345
   * map_id_up_max - Find idmap via binary search in ordered idmap array.
   * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
   */
3edf652fa   Eric W. Biederman   userns: Simplify ...
346
347
  static struct uid_gid_extent *
  map_id_up_max(unsigned extents, struct uid_gid_map *map, u32 id)
6397fac49   Christian Brauner   userns: bump idma...
348
  {
6397fac49   Christian Brauner   userns: bump idma...
349
350
351
  	struct idmap_key key;
  
  	key.map_up = true;
11a8b9270   Eric W. Biederman   userns: Don't spe...
352
  	key.count = 1;
6397fac49   Christian Brauner   userns: bump idma...
353
  	key.id = id;
3edf652fa   Eric W. Biederman   userns: Simplify ...
354
355
  	return bsearch(&key, map->reverse, extents,
  		       sizeof(struct uid_gid_extent), cmp_map_id);
22d917d80   Eric W. Biederman   userns: Rework th...
356
357
358
359
  }
  
  static u32 map_id_up(struct uid_gid_map *map, u32 id)
  {
3edf652fa   Eric W. Biederman   userns: Simplify ...
360
361
  	struct uid_gid_extent *extent;
  	unsigned extents = map->nr_extents;
e79323bd8   Mikulas Patocka   user namespace: f...
362
  	smp_rmb();
6397fac49   Christian Brauner   userns: bump idma...
363

3edf652fa   Eric W. Biederman   userns: Simplify ...
364
365
366
367
  	if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  		extent = map_id_up_base(extents, map, id);
  	else
  		extent = map_id_up_max(extents, map, id);
22d917d80   Eric W. Biederman   userns: Rework th...
368
  	/* Map the id or note failure */
6397fac49   Christian Brauner   userns: bump idma...
369
370
  	if (extent)
  		id = (id - extent->lower_first) + extent->first;
22d917d80   Eric W. Biederman   userns: Rework th...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  	else
  		id = (u32) -1;
  
  	return id;
  }
  
  /**
   *	make_kuid - Map a user-namespace uid pair into a kuid.
   *	@ns:  User namespace that the uid is in
   *	@uid: User identifier
   *
   *	Maps a user-namespace uid pair into a kernel internal kuid,
   *	and returns that kuid.
   *
   *	When there is no mapping defined for the user-namespace uid
   *	pair INVALID_UID is returned.  Callers are expected to test
b080e047a   Brian Campbell   user_namespace.c:...
387
   *	for and handle INVALID_UID being returned.  INVALID_UID
22d917d80   Eric W. Biederman   userns: Rework th...
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
   *	may be tested for using uid_valid().
   */
  kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
  {
  	/* Map the uid to a global kernel uid */
  	return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
  }
  EXPORT_SYMBOL(make_kuid);
  
  /**
   *	from_kuid - Create a uid from a kuid user-namespace pair.
   *	@targ: The user namespace we want a uid in.
   *	@kuid: The kernel internal uid to start with.
   *
   *	Map @kuid into the user-namespace specified by @targ and
   *	return the resulting uid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	If @kuid has no mapping in @targ (uid_t)-1 is returned.
   */
  uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
  {
  	/* Map the uid from a global kernel uid */
  	return map_id_up(&targ->uid_map, __kuid_val(kuid));
  }
  EXPORT_SYMBOL(from_kuid);
  
  /**
   *	from_kuid_munged - Create a uid from a kuid user-namespace pair.
   *	@targ: The user namespace we want a uid in.
   *	@kuid: The kernel internal uid to start with.
   *
   *	Map @kuid into the user-namespace specified by @targ and
   *	return the resulting uid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	Unlike from_kuid from_kuid_munged never fails and always
   *	returns a valid uid.  This makes from_kuid_munged appropriate
   *	for use in syscalls like stat and getuid where failing the
   *	system call and failing to provide a valid uid are not an
   *	options.
   *
   *	If @kuid has no mapping in @targ overflowuid is returned.
   */
  uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
  {
  	uid_t uid;
  	uid = from_kuid(targ, kuid);
  
  	if (uid == (uid_t) -1)
  		uid = overflowuid;
  	return uid;
  }
  EXPORT_SYMBOL(from_kuid_munged);
  
  /**
   *	make_kgid - Map a user-namespace gid pair into a kgid.
   *	@ns:  User namespace that the gid is in
68a9a435e   Fabian Frederick   kernel/user_names...
448
   *	@gid: group identifier
22d917d80   Eric W. Biederman   userns: Rework th...
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
   *
   *	Maps a user-namespace gid pair into a kernel internal kgid,
   *	and returns that kgid.
   *
   *	When there is no mapping defined for the user-namespace gid
   *	pair INVALID_GID is returned.  Callers are expected to test
   *	for and handle INVALID_GID being returned.  INVALID_GID may be
   *	tested for using gid_valid().
   */
  kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
  {
  	/* Map the gid to a global kernel gid */
  	return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
  }
  EXPORT_SYMBOL(make_kgid);
  
  /**
   *	from_kgid - Create a gid from a kgid user-namespace pair.
   *	@targ: The user namespace we want a gid in.
   *	@kgid: The kernel internal gid to start with.
   *
   *	Map @kgid into the user-namespace specified by @targ and
   *	return the resulting gid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	If @kgid has no mapping in @targ (gid_t)-1 is returned.
   */
  gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
  {
  	/* Map the gid from a global kernel gid */
  	return map_id_up(&targ->gid_map, __kgid_val(kgid));
  }
  EXPORT_SYMBOL(from_kgid);
  
  /**
   *	from_kgid_munged - Create a gid from a kgid user-namespace pair.
   *	@targ: The user namespace we want a gid in.
   *	@kgid: The kernel internal gid to start with.
   *
   *	Map @kgid into the user-namespace specified by @targ and
   *	return the resulting gid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	Unlike from_kgid from_kgid_munged never fails and always
   *	returns a valid gid.  This makes from_kgid_munged appropriate
   *	for use in syscalls like stat and getgid where failing the
   *	system call and failing to provide a valid gid are not options.
   *
   *	If @kgid has no mapping in @targ overflowgid is returned.
   */
  gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
  {
  	gid_t gid;
  	gid = from_kgid(targ, kgid);
  
  	if (gid == (gid_t) -1)
  		gid = overflowgid;
  	return gid;
  }
  EXPORT_SYMBOL(from_kgid_munged);
f76d207a6   Eric W. Biederman   userns: Add kproj...
511
512
513
514
515
516
517
518
519
520
  /**
   *	make_kprojid - Map a user-namespace projid pair into a kprojid.
   *	@ns:  User namespace that the projid is in
   *	@projid: Project identifier
   *
   *	Maps a user-namespace uid pair into a kernel internal kuid,
   *	and returns that kuid.
   *
   *	When there is no mapping defined for the user-namespace projid
   *	pair INVALID_PROJID is returned.  Callers are expected to test
7b7b8a2c9   Randy Dunlap   kernel/: fix repe...
521
   *	for and handle INVALID_PROJID being returned.  INVALID_PROJID
f76d207a6   Eric W. Biederman   userns: Add kproj...
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
   *	may be tested for using projid_valid().
   */
  kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
  {
  	/* Map the uid to a global kernel uid */
  	return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
  }
  EXPORT_SYMBOL(make_kprojid);
  
  /**
   *	from_kprojid - Create a projid from a kprojid user-namespace pair.
   *	@targ: The user namespace we want a projid in.
   *	@kprojid: The kernel internal project identifier to start with.
   *
   *	Map @kprojid into the user-namespace specified by @targ and
   *	return the resulting projid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	If @kprojid has no mapping in @targ (projid_t)-1 is returned.
   */
  projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
  {
  	/* Map the uid from a global kernel uid */
  	return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
  }
  EXPORT_SYMBOL(from_kprojid);
  
  /**
   *	from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
   *	@targ: The user namespace we want a projid in.
   *	@kprojid: The kernel internal projid to start with.
   *
   *	Map @kprojid into the user-namespace specified by @targ and
   *	return the resulting projid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	Unlike from_kprojid from_kprojid_munged never fails and always
   *	returns a valid projid.  This makes from_kprojid_munged
   *	appropriate for use in syscalls like stat and where
   *	failing the system call and failing to provide a valid projid are
   *	not an options.
   *
   *	If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
   */
  projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
  {
  	projid_t projid;
  	projid = from_kprojid(targ, kprojid);
  
  	if (projid == (projid_t) -1)
  		projid = OVERFLOW_PROJID;
  	return projid;
  }
  EXPORT_SYMBOL(from_kprojid_munged);
22d917d80   Eric W. Biederman   userns: Rework th...
578
579
580
581
582
583
  static int uid_m_show(struct seq_file *seq, void *v)
  {
  	struct user_namespace *ns = seq->private;
  	struct uid_gid_extent *extent = v;
  	struct user_namespace *lower_ns;
  	uid_t lower;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
584

c450f371d   Eric W. Biederman   userns: For /proc...
585
  	lower_ns = seq_user_ns(seq);
22d917d80   Eric W. Biederman   userns: Rework th...
586
587
588
589
590
591
592
593
594
595
596
597
  	if ((lower_ns == ns) && lower_ns->parent)
  		lower_ns = lower_ns->parent;
  
  	lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
  
  	seq_printf(seq, "%10u %10u %10u
  ",
  		extent->first,
  		lower,
  		extent->count);
  
  	return 0;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
598
  }
22d917d80   Eric W. Biederman   userns: Rework th...
599
  static int gid_m_show(struct seq_file *seq, void *v)
5c1469de7   Eric W. Biederman   user_ns: Introduc...
600
  {
22d917d80   Eric W. Biederman   userns: Rework th...
601
602
603
604
  	struct user_namespace *ns = seq->private;
  	struct uid_gid_extent *extent = v;
  	struct user_namespace *lower_ns;
  	gid_t lower;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
605

c450f371d   Eric W. Biederman   userns: For /proc...
606
  	lower_ns = seq_user_ns(seq);
22d917d80   Eric W. Biederman   userns: Rework th...
607
608
  	if ((lower_ns == ns) && lower_ns->parent)
  		lower_ns = lower_ns->parent;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
609

22d917d80   Eric W. Biederman   userns: Rework th...
610
611
612
613
614
615
616
617
618
619
  	lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
  
  	seq_printf(seq, "%10u %10u %10u
  ",
  		extent->first,
  		lower,
  		extent->count);
  
  	return 0;
  }
f76d207a6   Eric W. Biederman   userns: Add kproj...
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
  static int projid_m_show(struct seq_file *seq, void *v)
  {
  	struct user_namespace *ns = seq->private;
  	struct uid_gid_extent *extent = v;
  	struct user_namespace *lower_ns;
  	projid_t lower;
  
  	lower_ns = seq_user_ns(seq);
  	if ((lower_ns == ns) && lower_ns->parent)
  		lower_ns = lower_ns->parent;
  
  	lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
  
  	seq_printf(seq, "%10u %10u %10u
  ",
  		extent->first,
  		lower,
  		extent->count);
  
  	return 0;
  }
68a9a435e   Fabian Frederick   kernel/user_names...
641
642
  static void *m_start(struct seq_file *seq, loff_t *ppos,
  		     struct uid_gid_map *map)
22d917d80   Eric W. Biederman   userns: Rework th...
643
  {
22d917d80   Eric W. Biederman   userns: Rework th...
644
  	loff_t pos = *ppos;
d5e7b3c5f   Eric W. Biederman   userns: Don't rea...
645
646
  	unsigned extents = map->nr_extents;
  	smp_rmb();
22d917d80   Eric W. Biederman   userns: Rework th...
647

d5e7b3c5f   Eric W. Biederman   userns: Don't rea...
648
  	if (pos >= extents)
6397fac49   Christian Brauner   userns: bump idma...
649
  		return NULL;
22d917d80   Eric W. Biederman   userns: Rework th...
650

d5e7b3c5f   Eric W. Biederman   userns: Don't rea...
651
  	if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
6397fac49   Christian Brauner   userns: bump idma...
652
  		return &map->extent[pos];
22d917d80   Eric W. Biederman   userns: Rework th...
653

6397fac49   Christian Brauner   userns: bump idma...
654
  	return &map->forward[pos];
22d917d80   Eric W. Biederman   userns: Rework th...
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
  }
  
  static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
  {
  	struct user_namespace *ns = seq->private;
  
  	return m_start(seq, ppos, &ns->uid_map);
  }
  
  static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
  {
  	struct user_namespace *ns = seq->private;
  
  	return m_start(seq, ppos, &ns->gid_map);
  }
f76d207a6   Eric W. Biederman   userns: Add kproj...
670
671
672
673
674
675
  static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
  {
  	struct user_namespace *ns = seq->private;
  
  	return m_start(seq, ppos, &ns->projid_map);
  }
22d917d80   Eric W. Biederman   userns: Rework th...
676
677
678
679
680
681
682
683
684
685
  static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
  {
  	(*pos)++;
  	return seq->op->start(seq, pos);
  }
  
  static void m_stop(struct seq_file *seq, void *v)
  {
  	return;
  }
ccf94f1b4   Fabian Frederick   proc: constify se...
686
  const struct seq_operations proc_uid_seq_operations = {
22d917d80   Eric W. Biederman   userns: Rework th...
687
688
689
690
691
  	.start = uid_m_start,
  	.stop = m_stop,
  	.next = m_next,
  	.show = uid_m_show,
  };
ccf94f1b4   Fabian Frederick   proc: constify se...
692
  const struct seq_operations proc_gid_seq_operations = {
22d917d80   Eric W. Biederman   userns: Rework th...
693
694
695
696
697
  	.start = gid_m_start,
  	.stop = m_stop,
  	.next = m_next,
  	.show = gid_m_show,
  };
ccf94f1b4   Fabian Frederick   proc: constify se...
698
  const struct seq_operations proc_projid_seq_operations = {
f76d207a6   Eric W. Biederman   userns: Add kproj...
699
700
701
702
703
  	.start = projid_m_start,
  	.stop = m_stop,
  	.next = m_next,
  	.show = projid_m_show,
  };
68a9a435e   Fabian Frederick   kernel/user_names...
704
705
  static bool mappings_overlap(struct uid_gid_map *new_map,
  			     struct uid_gid_extent *extent)
0bd14b4fd   Eric W. Biederman   userns: Allow any...
706
707
708
709
710
711
712
713
714
715
716
717
718
  {
  	u32 upper_first, lower_first, upper_last, lower_last;
  	unsigned idx;
  
  	upper_first = extent->first;
  	lower_first = extent->lower_first;
  	upper_last = upper_first + extent->count - 1;
  	lower_last = lower_first + extent->count - 1;
  
  	for (idx = 0; idx < new_map->nr_extents; idx++) {
  		u32 prev_upper_first, prev_lower_first;
  		u32 prev_upper_last, prev_lower_last;
  		struct uid_gid_extent *prev;
6397fac49   Christian Brauner   userns: bump idma...
719
720
721
722
  		if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  			prev = &new_map->extent[idx];
  		else
  			prev = &new_map->forward[idx];
0bd14b4fd   Eric W. Biederman   userns: Allow any...
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
  
  		prev_upper_first = prev->first;
  		prev_lower_first = prev->lower_first;
  		prev_upper_last = prev_upper_first + prev->count - 1;
  		prev_lower_last = prev_lower_first + prev->count - 1;
  
  		/* Does the upper range intersect a previous extent? */
  		if ((prev_upper_first <= upper_last) &&
  		    (prev_upper_last >= upper_first))
  			return true;
  
  		/* Does the lower range intersect a previous extent? */
  		if ((prev_lower_first <= lower_last) &&
  		    (prev_lower_last >= lower_first))
  			return true;
  	}
  	return false;
  }
6397fac49   Christian Brauner   userns: bump idma...
741
742
743
744
745
746
747
  /**
   * insert_extent - Safely insert a new idmap extent into struct uid_gid_map.
   * Takes care to allocate a 4K block of memory if the number of mappings exceeds
   * UID_GID_MAP_MAX_BASE_EXTENTS.
   */
  static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
  {
3fda0e737   Eric W. Biederman   userns: Simplify ...
748
  	struct uid_gid_extent *dest;
6397fac49   Christian Brauner   userns: bump idma...
749
750
751
752
753
  
  	if (map->nr_extents == UID_GID_MAP_MAX_BASE_EXTENTS) {
  		struct uid_gid_extent *forward;
  
  		/* Allocate memory for 340 mappings. */
6da2ec560   Kees Cook   treewide: kmalloc...
754
755
756
  		forward = kmalloc_array(UID_GID_MAP_MAX_EXTENTS,
  					sizeof(struct uid_gid_extent),
  					GFP_KERNEL);
6397fac49   Christian Brauner   userns: bump idma...
757
758
759
760
761
762
763
764
765
766
767
768
  		if (!forward)
  			return -ENOMEM;
  
  		/* Copy over memory. Only set up memory for the forward pointer.
  		 * Defer the memory setup for the reverse pointer.
  		 */
  		memcpy(forward, map->extent,
  		       map->nr_extents * sizeof(map->extent[0]));
  
  		map->forward = forward;
  		map->reverse = NULL;
  	}
3fda0e737   Eric W. Biederman   userns: Simplify ...
769
770
771
772
773
774
775
  	if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
  		dest = &map->extent[map->nr_extents];
  	else
  		dest = &map->forward[map->nr_extents];
  
  	*dest = *extent;
  	map->nr_extents++;
6397fac49   Christian Brauner   userns: bump idma...
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
  	return 0;
  }
  
  /* cmp function to sort() forward mappings */
  static int cmp_extents_forward(const void *a, const void *b)
  {
  	const struct uid_gid_extent *e1 = a;
  	const struct uid_gid_extent *e2 = b;
  
  	if (e1->first < e2->first)
  		return -1;
  
  	if (e1->first > e2->first)
  		return 1;
  
  	return 0;
  }
  
  /* cmp function to sort() reverse mappings */
  static int cmp_extents_reverse(const void *a, const void *b)
  {
  	const struct uid_gid_extent *e1 = a;
  	const struct uid_gid_extent *e2 = b;
  
  	if (e1->lower_first < e2->lower_first)
  		return -1;
  
  	if (e1->lower_first > e2->lower_first)
  		return 1;
  
  	return 0;
  }
  
  /**
   * sort_idmaps - Sorts an array of idmap entries.
   * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
   */
  static int sort_idmaps(struct uid_gid_map *map)
  {
  	if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  		return 0;
  
  	/* Sort forward array. */
  	sort(map->forward, map->nr_extents, sizeof(struct uid_gid_extent),
  	     cmp_extents_forward, NULL);
  
  	/* Only copy the memory from forward we actually need. */
  	map->reverse = kmemdup(map->forward,
  			       map->nr_extents * sizeof(struct uid_gid_extent),
  			       GFP_KERNEL);
  	if (!map->reverse)
  		return -ENOMEM;
  
  	/* Sort reverse array. */
  	sort(map->reverse, map->nr_extents, sizeof(struct uid_gid_extent),
  	     cmp_extents_reverse, NULL);
  
  	return 0;
  }
db2e718a4   Serge E. Hallyn   capabilities: req...
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
  /**
   * verify_root_map() - check the uid 0 mapping
   * @file: idmapping file
   * @map_ns: user namespace of the target process
   * @new_map: requested idmap
   *
   * If a process requests mapping parent uid 0 into the new ns, verify that the
   * process writing the map had the CAP_SETFCAP capability as the target process
   * will be able to write fscaps that are valid in ancestor user namespaces.
   *
   * Return: true if the mapping is allowed, false if not.
   */
  static bool verify_root_map(const struct file *file,
  			    struct user_namespace *map_ns,
  			    struct uid_gid_map *new_map)
  {
  	int idx;
  	const struct user_namespace *file_ns = file->f_cred->user_ns;
  	struct uid_gid_extent *extent0 = NULL;
  
  	for (idx = 0; idx < new_map->nr_extents; idx++) {
  		if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  			extent0 = &new_map->extent[idx];
  		else
  			extent0 = &new_map->forward[idx];
  		if (extent0->lower_first == 0)
  			break;
  
  		extent0 = NULL;
  	}
  
  	if (!extent0)
  		return true;
  
  	if (map_ns == file_ns) {
  		/* The process unshared its ns and is writing to its own
  		 * /proc/self/uid_map.  User already has full capabilites in
  		 * the new namespace.  Verify that the parent had CAP_SETFCAP
  		 * when it unshared.
  		 * */
  		if (!file_ns->parent_could_setfcap)
  			return false;
  	} else {
  		/* Process p1 is writing to uid_map of p2, who is in a child
  		 * user namespace to p1's.  Verify that the opener of the map
  		 * file has CAP_SETFCAP against the parent of the new map
  		 * namespace */
  		if (!file_ns_capable(file, map_ns->parent, CAP_SETFCAP))
  			return false;
  	}
  
  	return true;
  }
22d917d80   Eric W. Biederman   userns: Rework th...
888
889
890
891
892
893
894
  static ssize_t map_write(struct file *file, const char __user *buf,
  			 size_t count, loff_t *ppos,
  			 int cap_setid,
  			 struct uid_gid_map *map,
  			 struct uid_gid_map *parent_map)
  {
  	struct seq_file *seq = file->private_data;
db2e718a4   Serge E. Hallyn   capabilities: req...
895
  	struct user_namespace *map_ns = seq->private;
22d917d80   Eric W. Biederman   userns: Rework th...
896
897
  	struct uid_gid_map new_map;
  	unsigned idx;
6397fac49   Christian Brauner   userns: bump idma...
898
  	struct uid_gid_extent extent;
70f6cbb6f   Al Viro   kernel/*: switch ...
899
  	char *kbuf = NULL, *pos, *next_line;
5820f140e   Jann Horn   userns: move user...
900
901
902
903
904
905
906
907
908
909
  	ssize_t ret;
  
  	/* Only allow < page size writes at the beginning of the file */
  	if ((*ppos != 0) || (count >= PAGE_SIZE))
  		return -EINVAL;
  
  	/* Slurp in the user data */
  	kbuf = memdup_user_nul(buf, count);
  	if (IS_ERR(kbuf))
  		return PTR_ERR(kbuf);
22d917d80   Eric W. Biederman   userns: Rework th...
910
911
  
  	/*
f0d62aec9   Eric W. Biederman   userns: Rename id...
912
  	 * The userns_state_mutex serializes all writes to any given map.
22d917d80   Eric W. Biederman   userns: Rework th...
913
914
915
916
917
918
919
920
921
922
923
924
925
926
  	 *
  	 * Any map is only ever written once.
  	 *
  	 * An id map fits within 1 cache line on most architectures.
  	 *
  	 * On read nothing needs to be done unless you are on an
  	 * architecture with a crazy cache coherency model like alpha.
  	 *
  	 * There is a one time data dependency between reading the
  	 * count of the extents and the values of the extents.  The
  	 * desired behavior is to see the values of the extents that
  	 * were written before the count of the extents.
  	 *
  	 * To achieve this smp_wmb() is used on guarantee the write
e79323bd8   Mikulas Patocka   user namespace: f...
927
928
  	 * order and smp_rmb() is guaranteed that we don't have crazy
  	 * architectures returning stale data.
22d917d80   Eric W. Biederman   userns: Rework th...
929
  	 */
f0d62aec9   Eric W. Biederman   userns: Rename id...
930
  	mutex_lock(&userns_state_mutex);
22d917d80   Eric W. Biederman   userns: Rework th...
931

6397fac49   Christian Brauner   userns: bump idma...
932
  	memset(&new_map, 0, sizeof(struct uid_gid_map));
22d917d80   Eric W. Biederman   userns: Rework th...
933
934
935
936
  	ret = -EPERM;
  	/* Only allow one successful write to the map */
  	if (map->nr_extents != 0)
  		goto out;
41c21e351   Andy Lutomirski   userns: Changing ...
937
938
  	/*
  	 * Adjusting namespace settings requires capabilities on the target.
5c1469de7   Eric W. Biederman   user_ns: Introduc...
939
  	 */
db2e718a4   Serge E. Hallyn   capabilities: req...
940
  	if (cap_valid(cap_setid) && !file_ns_capable(file, map_ns, CAP_SYS_ADMIN))
22d917d80   Eric W. Biederman   userns: Rework th...
941
  		goto out;
22d917d80   Eric W. Biederman   userns: Rework th...
942
943
944
  	/* Parse the user data */
  	ret = -EINVAL;
  	pos = kbuf;
68a9a435e   Fabian Frederick   kernel/user_names...
945
  	for (; pos; pos = next_line) {
22d917d80   Eric W. Biederman   userns: Rework th...
946
947
948
949
950
951
952
953
954
  
  		/* Find the end of line and ensure I don't look past it */
  		next_line = strchr(pos, '
  ');
  		if (next_line) {
  			*next_line = '\0';
  			next_line++;
  			if (*next_line == '\0')
  				next_line = NULL;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
955
  		}
22d917d80   Eric W. Biederman   userns: Rework th...
956
957
  
  		pos = skip_spaces(pos);
6397fac49   Christian Brauner   userns: bump idma...
958
  		extent.first = simple_strtoul(pos, &pos, 10);
22d917d80   Eric W. Biederman   userns: Rework th...
959
960
961
962
  		if (!isspace(*pos))
  			goto out;
  
  		pos = skip_spaces(pos);
6397fac49   Christian Brauner   userns: bump idma...
963
  		extent.lower_first = simple_strtoul(pos, &pos, 10);
22d917d80   Eric W. Biederman   userns: Rework th...
964
965
966
967
  		if (!isspace(*pos))
  			goto out;
  
  		pos = skip_spaces(pos);
6397fac49   Christian Brauner   userns: bump idma...
968
  		extent.count = simple_strtoul(pos, &pos, 10);
22d917d80   Eric W. Biederman   userns: Rework th...
969
970
971
972
973
974
975
976
977
  		if (*pos && !isspace(*pos))
  			goto out;
  
  		/* Verify there is not trailing junk on the line */
  		pos = skip_spaces(pos);
  		if (*pos != '\0')
  			goto out;
  
  		/* Verify we have been given valid starting values */
6397fac49   Christian Brauner   userns: bump idma...
978
979
  		if ((extent.first == (u32) -1) ||
  		    (extent.lower_first == (u32) -1))
22d917d80   Eric W. Biederman   userns: Rework th...
980
  			goto out;
68a9a435e   Fabian Frederick   kernel/user_names...
981
982
983
  		/* Verify count is not zero and does not cause the
  		 * extent to wrap
  		 */
6397fac49   Christian Brauner   userns: bump idma...
984
  		if ((extent.first + extent.count) <= extent.first)
22d917d80   Eric W. Biederman   userns: Rework th...
985
  			goto out;
6397fac49   Christian Brauner   userns: bump idma...
986
987
  		if ((extent.lower_first + extent.count) <=
  		     extent.lower_first)
22d917d80   Eric W. Biederman   userns: Rework th...
988
  			goto out;
0bd14b4fd   Eric W. Biederman   userns: Allow any...
989
  		/* Do the ranges in extent overlap any previous extents? */
6397fac49   Christian Brauner   userns: bump idma...
990
  		if (mappings_overlap(&new_map, &extent))
22d917d80   Eric W. Biederman   userns: Rework th...
991
  			goto out;
6397fac49   Christian Brauner   userns: bump idma...
992
  		if ((new_map.nr_extents + 1) == UID_GID_MAP_MAX_EXTENTS &&
22d917d80   Eric W. Biederman   userns: Rework th...
993
994
  		    (next_line != NULL))
  			goto out;
6397fac49   Christian Brauner   userns: bump idma...
995
996
997
998
999
  
  		ret = insert_extent(&new_map, &extent);
  		if (ret < 0)
  			goto out;
  		ret = -EINVAL;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
1000
  	}
a12f4f85b   Xiaofeng Cao   kernel/user_names...
1001
  	/* Be very certain the new map actually exists */
22d917d80   Eric W. Biederman   userns: Rework th...
1002
1003
1004
1005
1006
  	if (new_map.nr_extents == 0)
  		goto out;
  
  	ret = -EPERM;
  	/* Validate the user is allowed to use user id's mapped to. */
db2e718a4   Serge E. Hallyn   capabilities: req...
1007
  	if (!new_idmap_permitted(file, map_ns, cap_setid, &new_map))
22d917d80   Eric W. Biederman   userns: Rework th...
1008
  		goto out;
6397fac49   Christian Brauner   userns: bump idma...
1009
  	ret = -EPERM;
22d917d80   Eric W. Biederman   userns: Rework th...
1010
1011
1012
1013
  	/* Map the lower ids from the parent user namespace to the
  	 * kernel global id space.
  	 */
  	for (idx = 0; idx < new_map.nr_extents; idx++) {
6397fac49   Christian Brauner   userns: bump idma...
1014
  		struct uid_gid_extent *e;
22d917d80   Eric W. Biederman   userns: Rework th...
1015
  		u32 lower_first;
6397fac49   Christian Brauner   userns: bump idma...
1016
1017
1018
1019
1020
  
  		if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  			e = &new_map.extent[idx];
  		else
  			e = &new_map.forward[idx];
22d917d80   Eric W. Biederman   userns: Rework th...
1021
1022
  
  		lower_first = map_id_range_down(parent_map,
6397fac49   Christian Brauner   userns: bump idma...
1023
1024
  						e->lower_first,
  						e->count);
22d917d80   Eric W. Biederman   userns: Rework th...
1025
1026
1027
1028
1029
1030
  
  		/* Fail if we can not map the specified extent to
  		 * the kernel global id space.
  		 */
  		if (lower_first == (u32) -1)
  			goto out;
6397fac49   Christian Brauner   userns: bump idma...
1031
  		e->lower_first = lower_first;
22d917d80   Eric W. Biederman   userns: Rework th...
1032
  	}
d2f007dbe   Jann Horn   userns: also map ...
1033
1034
1035
1036
1037
1038
1039
  	/*
  	 * If we want to use binary search for lookup, this clones the extent
  	 * array and sorts both copies.
  	 */
  	ret = sort_idmaps(&new_map);
  	if (ret < 0)
  		goto out;
22d917d80   Eric W. Biederman   userns: Rework th...
1040
  	/* Install the map */
6397fac49   Christian Brauner   userns: bump idma...
1041
1042
1043
1044
1045
1046
1047
  	if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) {
  		memcpy(map->extent, new_map.extent,
  		       new_map.nr_extents * sizeof(new_map.extent[0]));
  	} else {
  		map->forward = new_map.forward;
  		map->reverse = new_map.reverse;
  	}
22d917d80   Eric W. Biederman   userns: Rework th...
1048
1049
1050
1051
1052
1053
  	smp_wmb();
  	map->nr_extents = new_map.nr_extents;
  
  	*ppos = count;
  	ret = count;
  out:
6397fac49   Christian Brauner   userns: bump idma...
1054
1055
1056
1057
1058
1059
1060
  	if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
  		kfree(new_map.forward);
  		kfree(new_map.reverse);
  		map->forward = NULL;
  		map->reverse = NULL;
  		map->nr_extents = 0;
  	}
f0d62aec9   Eric W. Biederman   userns: Rename id...
1061
  	mutex_unlock(&userns_state_mutex);
70f6cbb6f   Al Viro   kernel/*: switch ...
1062
  	kfree(kbuf);
22d917d80   Eric W. Biederman   userns: Rework th...
1063
1064
  	return ret;
  }
68a9a435e   Fabian Frederick   kernel/user_names...
1065
1066
  ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
  			   size_t size, loff_t *ppos)
22d917d80   Eric W. Biederman   userns: Rework th...
1067
1068
1069
  {
  	struct seq_file *seq = file->private_data;
  	struct user_namespace *ns = seq->private;
c450f371d   Eric W. Biederman   userns: For /proc...
1070
  	struct user_namespace *seq_ns = seq_user_ns(seq);
22d917d80   Eric W. Biederman   userns: Rework th...
1071
1072
1073
  
  	if (!ns->parent)
  		return -EPERM;
c450f371d   Eric W. Biederman   userns: For /proc...
1074
1075
  	if ((seq_ns != ns) && (seq_ns != ns->parent))
  		return -EPERM;
22d917d80   Eric W. Biederman   userns: Rework th...
1076
1077
1078
  	return map_write(file, buf, size, ppos, CAP_SETUID,
  			 &ns->uid_map, &ns->parent->uid_map);
  }
68a9a435e   Fabian Frederick   kernel/user_names...
1079
1080
  ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
  			   size_t size, loff_t *ppos)
22d917d80   Eric W. Biederman   userns: Rework th...
1081
1082
1083
  {
  	struct seq_file *seq = file->private_data;
  	struct user_namespace *ns = seq->private;
c450f371d   Eric W. Biederman   userns: For /proc...
1084
  	struct user_namespace *seq_ns = seq_user_ns(seq);
22d917d80   Eric W. Biederman   userns: Rework th...
1085
1086
1087
  
  	if (!ns->parent)
  		return -EPERM;
c450f371d   Eric W. Biederman   userns: For /proc...
1088
1089
  	if ((seq_ns != ns) && (seq_ns != ns->parent))
  		return -EPERM;
22d917d80   Eric W. Biederman   userns: Rework th...
1090
1091
1092
  	return map_write(file, buf, size, ppos, CAP_SETGID,
  			 &ns->gid_map, &ns->parent->gid_map);
  }
68a9a435e   Fabian Frederick   kernel/user_names...
1093
1094
  ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
  			      size_t size, loff_t *ppos)
f76d207a6   Eric W. Biederman   userns: Add kproj...
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
  {
  	struct seq_file *seq = file->private_data;
  	struct user_namespace *ns = seq->private;
  	struct user_namespace *seq_ns = seq_user_ns(seq);
  
  	if (!ns->parent)
  		return -EPERM;
  
  	if ((seq_ns != ns) && (seq_ns != ns->parent))
  		return -EPERM;
  
  	/* Anyone can set any valid project id no capability needed */
  	return map_write(file, buf, size, ppos, -1,
  			 &ns->projid_map, &ns->parent->projid_map);
  }
68a9a435e   Fabian Frederick   kernel/user_names...
1110
  static bool new_idmap_permitted(const struct file *file,
6708075f1   Eric W. Biederman   userns: Don't let...
1111
  				struct user_namespace *ns, int cap_setid,
22d917d80   Eric W. Biederman   userns: Rework th...
1112
1113
  				struct uid_gid_map *new_map)
  {
f95d7918b   Eric W. Biederman   userns: Only allo...
1114
  	const struct cred *cred = file->f_cred;
db2e718a4   Serge E. Hallyn   capabilities: req...
1115
1116
1117
  
  	if (cap_setid == CAP_SETUID && !verify_root_map(file, ns, new_map))
  		return false;
0542f17bf   Eric W. Biederman   userns: Document ...
1118
1119
1120
  	/* Don't allow mappings that would allow anything that wouldn't
  	 * be allowed without the establishment of unprivileged mappings.
  	 */
f95d7918b   Eric W. Biederman   userns: Only allo...
1121
1122
  	if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
  	    uid_eq(ns->owner, cred->euid)) {
37657da3c   Eric W. Biederman   userns: Allow set...
1123
1124
1125
  		u32 id = new_map->extent[0].lower_first;
  		if (cap_setid == CAP_SETUID) {
  			kuid_t uid = make_kuid(ns->parent, id);
f95d7918b   Eric W. Biederman   userns: Only allo...
1126
  			if (uid_eq(uid, cred->euid))
37657da3c   Eric W. Biederman   userns: Allow set...
1127
  				return true;
68a9a435e   Fabian Frederick   kernel/user_names...
1128
  		} else if (cap_setid == CAP_SETGID) {
37657da3c   Eric W. Biederman   userns: Allow set...
1129
  			kgid_t gid = make_kgid(ns->parent, id);
66d2f338e   Eric W. Biederman   userns: Allow set...
1130
1131
  			if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
  			    gid_eq(gid, cred->egid))
37657da3c   Eric W. Biederman   userns: Allow set...
1132
1133
1134
  				return true;
  		}
  	}
f76d207a6   Eric W. Biederman   userns: Add kproj...
1135
1136
1137
  	/* Allow anyone to set a mapping that doesn't require privilege */
  	if (!cap_valid(cap_setid))
  		return true;
22d917d80   Eric W. Biederman   userns: Rework th...
1138
1139
  	/* Allow the specified ids if we have the appropriate capability
  	 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
a12f4f85b   Xiaofeng Cao   kernel/user_names...
1140
  	 * And the opener of the id file also has the appropriate capability.
22d917d80   Eric W. Biederman   userns: Rework th...
1141
  	 */
6708075f1   Eric W. Biederman   userns: Don't let...
1142
1143
  	if (ns_capable(ns->parent, cap_setid) &&
  	    file_ns_capable(file, ns->parent, cap_setid))
22d917d80   Eric W. Biederman   userns: Rework th...
1144
  		return true;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
1145

22d917d80   Eric W. Biederman   userns: Rework th...
1146
  	return false;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
1147
  }
6164281ab   Pavel Emelyanov   user_ns: improve ...
1148

9cc46516d   Eric W. Biederman   userns: Add a kno...
1149
1150
1151
  int proc_setgroups_show(struct seq_file *seq, void *v)
  {
  	struct user_namespace *ns = seq->private;
6aa7de059   Mark Rutland   locking/atomics: ...
1152
  	unsigned long userns_flags = READ_ONCE(ns->flags);
9cc46516d   Eric W. Biederman   userns: Add a kno...
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
  
  	seq_printf(seq, "%s
  ",
  		   (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
  		   "allow" : "deny");
  	return 0;
  }
  
  ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
  			     size_t count, loff_t *ppos)
  {
  	struct seq_file *seq = file->private_data;
  	struct user_namespace *ns = seq->private;
  	char kbuf[8], *pos;
  	bool setgroups_allowed;
  	ssize_t ret;
  
  	/* Only allow a very narrow range of strings to be written */
  	ret = -EINVAL;
  	if ((*ppos != 0) || (count >= sizeof(kbuf)))
  		goto out;
  
  	/* What was written? */
  	ret = -EFAULT;
  	if (copy_from_user(kbuf, buf, count))
  		goto out;
  	kbuf[count] = '\0';
  	pos = kbuf;
  
  	/* What is being requested? */
  	ret = -EINVAL;
  	if (strncmp(pos, "allow", 5) == 0) {
  		pos += 5;
  		setgroups_allowed = true;
  	}
  	else if (strncmp(pos, "deny", 4) == 0) {
  		pos += 4;
  		setgroups_allowed = false;
  	}
  	else
  		goto out;
  
  	/* Verify there is not trailing junk on the line */
  	pos = skip_spaces(pos);
  	if (*pos != '\0')
  		goto out;
  
  	ret = -EPERM;
  	mutex_lock(&userns_state_mutex);
  	if (setgroups_allowed) {
  		/* Enabling setgroups after setgroups has been disabled
  		 * is not allowed.
  		 */
  		if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
  			goto out_unlock;
  	} else {
  		/* Permanently disabling setgroups after setgroups has
  		 * been enabled by writing the gid_map is not allowed.
  		 */
  		if (ns->gid_map.nr_extents != 0)
  			goto out_unlock;
  		ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
  	}
  	mutex_unlock(&userns_state_mutex);
  
  	/* Report a successful write */
  	*ppos = count;
  	ret = count;
  out:
  	return ret;
  out_unlock:
  	mutex_unlock(&userns_state_mutex);
  	goto out;
  }
273d2c67c   Eric W. Biederman   userns: Don't all...
1227
1228
1229
  bool userns_may_setgroups(const struct user_namespace *ns)
  {
  	bool allowed;
f0d62aec9   Eric W. Biederman   userns: Rename id...
1230
  	mutex_lock(&userns_state_mutex);
273d2c67c   Eric W. Biederman   userns: Don't all...
1231
1232
1233
1234
  	/* It is not safe to use setgroups until a gid mapping in
  	 * the user namespace has been established.
  	 */
  	allowed = ns->gid_map.nr_extents != 0;
9cc46516d   Eric W. Biederman   userns: Add a kno...
1235
1236
  	/* Is setgroups allowed? */
  	allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
f0d62aec9   Eric W. Biederman   userns: Rename id...
1237
  	mutex_unlock(&userns_state_mutex);
273d2c67c   Eric W. Biederman   userns: Don't all...
1238
1239
1240
  
  	return allowed;
  }
d07b846f6   Seth Forshee   fs: Limit file ca...
1241
  /*
a2b426267   Eric W. Biederman   userns,pidns: Ver...
1242
1243
   * Returns true if @child is the same namespace or a descendant of
   * @ancestor.
d07b846f6   Seth Forshee   fs: Limit file ca...
1244
   */
a2b426267   Eric W. Biederman   userns,pidns: Ver...
1245
1246
1247
1248
1249
1250
1251
1252
  bool in_userns(const struct user_namespace *ancestor,
  	       const struct user_namespace *child)
  {
  	const struct user_namespace *ns;
  	for (ns = child; ns->level > ancestor->level; ns = ns->parent)
  		;
  	return (ns == ancestor);
  }
d07b846f6   Seth Forshee   fs: Limit file ca...
1253
1254
  bool current_in_userns(const struct user_namespace *target_ns)
  {
a2b426267   Eric W. Biederman   userns,pidns: Ver...
1255
  	return in_userns(target_ns, current_user_ns());
d07b846f6   Seth Forshee   fs: Limit file ca...
1256
  }
73f03c2b4   Seth Forshee   fuse: Restrict al...
1257
  EXPORT_SYMBOL(current_in_userns);
d07b846f6   Seth Forshee   fs: Limit file ca...
1258

3c0411846   Al Viro   switch the rest o...
1259
1260
1261
1262
  static inline struct user_namespace *to_user_ns(struct ns_common *ns)
  {
  	return container_of(ns, struct user_namespace, ns);
  }
64964528b   Al Viro   make proc_ns_oper...
1263
  static struct ns_common *userns_get(struct task_struct *task)
cde1975bc   Eric W. Biederman   userns: Implent p...
1264
1265
1266
1267
1268
1269
  {
  	struct user_namespace *user_ns;
  
  	rcu_read_lock();
  	user_ns = get_user_ns(__task_cred(task)->user_ns);
  	rcu_read_unlock();
3c0411846   Al Viro   switch the rest o...
1270
  	return user_ns ? &user_ns->ns : NULL;
cde1975bc   Eric W. Biederman   userns: Implent p...
1271
  }
64964528b   Al Viro   make proc_ns_oper...
1272
  static void userns_put(struct ns_common *ns)
cde1975bc   Eric W. Biederman   userns: Implent p...
1273
  {
3c0411846   Al Viro   switch the rest o...
1274
  	put_user_ns(to_user_ns(ns));
cde1975bc   Eric W. Biederman   userns: Implent p...
1275
  }
f2a8d52e0   Christian Brauner   nsproxy: add stru...
1276
  static int userns_install(struct nsset *nsset, struct ns_common *ns)
cde1975bc   Eric W. Biederman   userns: Implent p...
1277
  {
3c0411846   Al Viro   switch the rest o...
1278
  	struct user_namespace *user_ns = to_user_ns(ns);
cde1975bc   Eric W. Biederman   userns: Implent p...
1279
1280
1281
1282
1283
1284
1285
  	struct cred *cred;
  
  	/* Don't allow gaining capabilities by reentering
  	 * the same user namespace.
  	 */
  	if (user_ns == current_user_ns())
  		return -EINVAL;
faf00da54   Eric W. Biederman   userns,pidns: For...
1286
1287
  	/* Tasks that share a thread group must share a user namespace */
  	if (!thread_group_empty(current))
cde1975bc   Eric W. Biederman   userns: Implent p...
1288
  		return -EINVAL;
e66eded83   Eric W. Biederman   userns: Don't all...
1289
1290
  	if (current->fs->users != 1)
  		return -EINVAL;
cde1975bc   Eric W. Biederman   userns: Implent p...
1291
1292
  	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  		return -EPERM;
f2a8d52e0   Christian Brauner   nsproxy: add stru...
1293
  	cred = nsset_cred(nsset);
cde1975bc   Eric W. Biederman   userns: Implent p...
1294
  	if (!cred)
f2a8d52e0   Christian Brauner   nsproxy: add stru...
1295
  		return -EINVAL;
cde1975bc   Eric W. Biederman   userns: Implent p...
1296
1297
1298
  
  	put_user_ns(cred->user_ns);
  	set_cred_user_ns(cred, get_user_ns(user_ns));
905ae01c4   Alexey Gladkov   Add a reference t...
1299
1300
  	if (set_cred_ucounts(cred) < 0)
  		return -EINVAL;
f2a8d52e0   Christian Brauner   nsproxy: add stru...
1301
  	return 0;
cde1975bc   Eric W. Biederman   userns: Implent p...
1302
  }
bcac25a58   Andrey Vagin   kernel: add a hel...
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
  struct ns_common *ns_get_owner(struct ns_common *ns)
  {
  	struct user_namespace *my_user_ns = current_user_ns();
  	struct user_namespace *owner, *p;
  
  	/* See if the owner is in the current user namespace */
  	owner = p = ns->ops->owner(ns);
  	for (;;) {
  		if (!p)
  			return ERR_PTR(-EPERM);
  		if (p == my_user_ns)
  			break;
  		p = p->parent;
  	}
  
  	return &get_user_ns(owner)->ns;
  }
  
  static struct user_namespace *userns_owner(struct ns_common *ns)
  {
  	return to_user_ns(ns)->parent;
  }
cde1975bc   Eric W. Biederman   userns: Implent p...
1325
1326
1327
1328
1329
1330
  const struct proc_ns_operations userns_operations = {
  	.name		= "user",
  	.type		= CLONE_NEWUSER,
  	.get		= userns_get,
  	.put		= userns_put,
  	.install	= userns_install,
bcac25a58   Andrey Vagin   kernel: add a hel...
1331
  	.owner		= userns_owner,
a7306ed8d   Andrey Vagin   nsfs: add ioctl t...
1332
  	.get_parent	= ns_get_owner,
cde1975bc   Eric W. Biederman   userns: Implent p...
1333
  };
6164281ab   Pavel Emelyanov   user_ns: improve ...
1334
1335
  static __init int user_namespaces_init(void)
  {
30acd0bdf   Vasily Averin   memcg: enable acc...
1336
  	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC | SLAB_ACCOUNT);
6164281ab   Pavel Emelyanov   user_ns: improve ...
1337
1338
  	return 0;
  }
c96d6660d   Paul Gortmaker   kernel: audit/fix...
1339
  subsys_initcall(user_namespaces_init);