Blame view

fs/posix_acl.c 24.1 KB
457c89965   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
3
   * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
   *
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
5
6
   * Fixes from William Schumacher incorporated on 15 March 2001.
   *    (Reported by Charles Bertsch, <CBertsch@microtest.com>).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
13
14
15
   */
  
  /*
   *  This file contains generic functions for manipulating
   *  POSIX 1003.1e draft standard 17 ACLs.
   */
  
  #include <linux/kernel.h>
  #include <linux/slab.h>
60063497a   Arun Sharma   atomic: use <linu...
16
  #include <linux/atomic.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
  #include <linux/fs.h>
  #include <linux/sched.h>
5b825c3af   Ingo Molnar   sched/headers: Pr...
19
  #include <linux/cred.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
  #include <linux/posix_acl.h>
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
21
  #include <linux/posix_acl_xattr.h>
2aeccbe95   Christoph Hellwig   fs: add generic x...
22
  #include <linux/xattr.h>
630d9c472   Paul Gortmaker   fs: reduce the us...
23
  #include <linux/export.h>
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
24
  #include <linux/user_namespace.h>
332f606b3   Miklos Szeredi   ovl: enable RCU'd...
25
  #include <linux/namei.h>
7bc23abcb   Christian Brauner   fs: move mapping ...
26
  #include <linux/mnt_idmapping.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27

04c57f450   Andreas Gruenbacher   posix_acl: Unexpo...
28
  static struct posix_acl **acl_by_type(struct inode *inode, int type)
0afaa1204   Andrew Morton   posix_acl: uninli...
29
30
31
32
33
34
35
36
37
38
  {
  	switch (type) {
  	case ACL_TYPE_ACCESS:
  		return &inode->i_acl;
  	case ACL_TYPE_DEFAULT:
  		return &inode->i_default_acl;
  	default:
  		BUG();
  	}
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
39
40
41
42
  
  struct posix_acl *get_cached_acl(struct inode *inode, int type)
  {
  	struct posix_acl **p = acl_by_type(inode, type);
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
43
44
45
46
47
48
  	struct posix_acl *acl;
  
  	for (;;) {
  		rcu_read_lock();
  		acl = rcu_dereference(*p);
  		if (!acl || is_uncached_acl(acl) ||
667172605   Elena Reshetova   posix_acl: conver...
49
  		    refcount_inc_not_zero(&acl->a_refcount))
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
50
51
52
  			break;
  		rcu_read_unlock();
  		cpu_relax();
0afaa1204   Andrew Morton   posix_acl: uninli...
53
  	}
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
54
  	rcu_read_unlock();
0afaa1204   Andrew Morton   posix_acl: uninli...
55
56
57
58
59
60
  	return acl;
  }
  EXPORT_SYMBOL(get_cached_acl);
  
  struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
  {
332f606b3   Miklos Szeredi   ovl: enable RCU'd...
61
62
63
64
65
66
67
68
69
70
71
  	struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
  
  	if (acl == ACL_DONT_CACHE) {
  		struct posix_acl *ret;
  
  		ret = inode->i_op->get_acl(inode, type, LOOKUP_RCU);
  		if (!IS_ERR(ret))
  			acl = ret;
  	}
  
  	return acl;
0afaa1204   Andrew Morton   posix_acl: uninli...
72
73
74
75
76
77
78
  }
  EXPORT_SYMBOL(get_cached_acl_rcu);
  
  void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
  {
  	struct posix_acl **p = acl_by_type(inode, type);
  	struct posix_acl *old;
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
79
80
81
  
  	old = xchg(p, posix_acl_dup(acl));
  	if (!is_uncached_acl(old))
0afaa1204   Andrew Morton   posix_acl: uninli...
82
83
84
  		posix_acl_release(old);
  }
  EXPORT_SYMBOL(set_cached_acl);
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
85
  static void __forget_cached_acl(struct posix_acl **p)
0afaa1204   Andrew Morton   posix_acl: uninli...
86
  {
0afaa1204   Andrew Morton   posix_acl: uninli...
87
  	struct posix_acl *old;
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
88
89
90
  
  	old = xchg(p, ACL_NOT_CACHED);
  	if (!is_uncached_acl(old))
0afaa1204   Andrew Morton   posix_acl: uninli...
91
92
  		posix_acl_release(old);
  }
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
93
94
95
96
97
  
  void forget_cached_acl(struct inode *inode, int type)
  {
  	__forget_cached_acl(acl_by_type(inode, type));
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
98
99
100
101
  EXPORT_SYMBOL(forget_cached_acl);
  
  void forget_all_cached_acls(struct inode *inode)
  {
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
102
103
  	__forget_cached_acl(&inode->i_acl);
  	__forget_cached_acl(&inode->i_default_acl);
0afaa1204   Andrew Morton   posix_acl: uninli...
104
105
  }
  EXPORT_SYMBOL(forget_all_cached_acls);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106

2982baa2a   Christoph Hellwig   fs: add get_acl h...
107
108
  struct posix_acl *get_acl(struct inode *inode, int type)
  {
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
109
110
  	void *sentinel;
  	struct posix_acl **p;
2982baa2a   Christoph Hellwig   fs: add get_acl h...
111
  	struct posix_acl *acl;
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
112
113
114
115
116
  	/*
  	 * The sentinel is used to detect when another operation like
  	 * set_cached_acl() or forget_cached_acl() races with get_acl().
  	 * It is guaranteed that is_uncached_acl(sentinel) is true.
  	 */
2982baa2a   Christoph Hellwig   fs: add get_acl h...
117
  	acl = get_cached_acl(inode, type);
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
118
  	if (!is_uncached_acl(acl))
2982baa2a   Christoph Hellwig   fs: add get_acl h...
119
120
121
122
  		return acl;
  
  	if (!IS_POSIXACL(inode))
  		return NULL;
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
123
124
125
126
127
128
129
130
131
132
133
134
135
  	sentinel = uncached_acl_sentinel(current);
  	p = acl_by_type(inode, type);
  
  	/*
  	 * If the ACL isn't being read yet, set our sentinel.  Otherwise, the
  	 * current value of the ACL will not be ACL_NOT_CACHED and so our own
  	 * sentinel will not be set; another task will update the cache.  We
  	 * could wait for that other task to complete its job, but it's easier
  	 * to just call ->get_acl to fetch the ACL ourself.  (This is going to
  	 * be an unlikely race.)
  	 */
  	if (cmpxchg(p, ACL_NOT_CACHED, sentinel) != ACL_NOT_CACHED)
  		/* fall through */ ;
2982baa2a   Christoph Hellwig   fs: add get_acl h...
136
  	/*
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
137
138
139
  	 * Normally, the ACL returned by ->get_acl will be cached.
  	 * A filesystem can prevent that by calling
  	 * forget_cached_acl(inode, type) in ->get_acl.
2982baa2a   Christoph Hellwig   fs: add get_acl h...
140
141
142
143
144
145
146
147
  	 *
  	 * If the filesystem doesn't have a get_acl() function at all, we'll
  	 * just create the negative cache entry.
  	 */
  	if (!inode->i_op->get_acl) {
  		set_cached_acl(inode, type, NULL);
  		return NULL;
  	}
0cad62466   Miklos Szeredi   vfs: add rcu argu...
148
  	acl = inode->i_op->get_acl(inode, type, false);
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  
  	if (IS_ERR(acl)) {
  		/*
  		 * Remove our sentinel so that we don't block future attempts
  		 * to cache the ACL.
  		 */
  		cmpxchg(p, sentinel, ACL_NOT_CACHED);
  		return acl;
  	}
  
  	/*
  	 * Cache the result, but only if our sentinel is still in place.
  	 */
  	posix_acl_dup(acl);
  	if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
  		posix_acl_release(acl);
  	return acl;
2982baa2a   Christoph Hellwig   fs: add get_acl h...
166
167
  }
  EXPORT_SYMBOL(get_acl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
  /*
f61f6da0d   Chuck Lever   NFS: Prevent memo...
169
170
171
172
173
   * Init a fresh posix_acl
   */
  void
  posix_acl_init(struct posix_acl *acl, int count)
  {
667172605   Elena Reshetova   posix_acl: conver...
174
  	refcount_set(&acl->a_refcount, 1);
f61f6da0d   Chuck Lever   NFS: Prevent memo...
175
176
  	acl->a_count = count;
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
177
  EXPORT_SYMBOL(posix_acl_init);
f61f6da0d   Chuck Lever   NFS: Prevent memo...
178
179
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
182
   * Allocate a new ACL with the specified number of entries.
   */
  struct posix_acl *
dd0fc66fb   Al Viro   [PATCH] gfp flags...
183
  posix_acl_alloc(int count, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
186
187
  {
  	const size_t size = sizeof(struct posix_acl) +
  	                    count * sizeof(struct posix_acl_entry);
  	struct posix_acl *acl = kmalloc(size, flags);
f61f6da0d   Chuck Lever   NFS: Prevent memo...
188
189
  	if (acl)
  		posix_acl_init(acl, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
  	return acl;
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
192
  EXPORT_SYMBOL(posix_acl_alloc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
194
195
196
  
  /*
   * Clone an ACL.
   */
edde854e8   Al Viro   bury posix_acl_.....
197
  static struct posix_acl *
dd0fc66fb   Al Viro   [PATCH] gfp flags...
198
  posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
200
201
202
203
204
  {
  	struct posix_acl *clone = NULL;
  
  	if (acl) {
  		int size = sizeof(struct posix_acl) + acl->a_count *
  		           sizeof(struct posix_acl_entry);
52978be63   Alexey Dobriyan   [PATCH] kmemdup: ...
205
206
  		clone = kmemdup(acl, size, flags);
  		if (clone)
667172605   Elena Reshetova   posix_acl: conver...
207
  			refcount_set(&clone->a_refcount, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
213
214
215
  	}
  	return clone;
  }
  
  /*
   * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
   */
  int
0d4d717f2   Eric W. Biederman   vfs: Verify acls ...
216
  posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
218
219
  {
  	const struct posix_acl_entry *pa, *pe;
  	int state = ACL_USER_OBJ;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
224
225
226
227
  	int needs_mask = 0;
  
  	FOREACH_ACL_ENTRY(pa, acl, pe) {
  		if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
  			return -EINVAL;
  		switch (pa->e_tag) {
  			case ACL_USER_OBJ:
  				if (state == ACL_USER_OBJ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
228
229
230
231
232
233
234
235
  					state = ACL_USER;
  					break;
  				}
  				return -EINVAL;
  
  			case ACL_USER:
  				if (state != ACL_USER)
  					return -EINVAL;
0d4d717f2   Eric W. Biederman   vfs: Verify acls ...
236
  				if (!kuid_has_mapping(user_ns, pa->e_uid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
  					return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
239
240
241
242
  				needs_mask = 1;
  				break;
  
  			case ACL_GROUP_OBJ:
  				if (state == ACL_USER) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
244
245
246
247
248
249
250
  					state = ACL_GROUP;
  					break;
  				}
  				return -EINVAL;
  
  			case ACL_GROUP:
  				if (state != ACL_GROUP)
  					return -EINVAL;
0d4d717f2   Eric W. Biederman   vfs: Verify acls ...
251
  				if (!kgid_has_mapping(user_ns, pa->e_gid))
2f6f0654a   Eric W. Biederman   userns: Convert v...
252
  					return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  				needs_mask = 1;
  				break;
  
  			case ACL_MASK:
  				if (state != ACL_GROUP)
  					return -EINVAL;
  				state = ACL_OTHER;
  				break;
  
  			case ACL_OTHER:
  				if (state == ACL_OTHER ||
  				    (state == ACL_GROUP && !needs_mask)) {
  					state = 0;
  					break;
  				}
  				return -EINVAL;
  
  			default:
  				return -EINVAL;
  		}
  	}
  	if (state == 0)
  		return 0;
  	return -EINVAL;
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
278
  EXPORT_SYMBOL(posix_acl_valid);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
282
283
284
  
  /*
   * Returns 0 if the acl can be exactly represented in the traditional
   * file mode permission bits, or else 1. Returns -E... on error.
   */
  int
d6952123b   Al Viro   switch posix_acl_...
285
  posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286
287
  {
  	const struct posix_acl_entry *pa, *pe;
d6952123b   Al Viro   switch posix_acl_...
288
  	umode_t mode = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  	int not_equiv = 0;
50c6e282b   Christoph Hellwig   posix_acl: handle...
290
291
292
293
294
  	/*
  	 * A null ACL can always be presented as mode bits.
  	 */
  	if (!acl)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  	FOREACH_ACL_ENTRY(pa, acl, pe) {
  		switch (pa->e_tag) {
  			case ACL_USER_OBJ:
  				mode |= (pa->e_perm & S_IRWXO) << 6;
  				break;
  			case ACL_GROUP_OBJ:
  				mode |= (pa->e_perm & S_IRWXO) << 3;
  				break;
  			case ACL_OTHER:
  				mode |= pa->e_perm & S_IRWXO;
  				break;
  			case ACL_MASK:
  				mode = (mode & ~S_IRWXG) |
  				       ((pa->e_perm & S_IRWXO) << 3);
  				not_equiv = 1;
  				break;
  			case ACL_USER:
  			case ACL_GROUP:
  				not_equiv = 1;
  				break;
  			default:
  				return -EINVAL;
  		}
  	}
          if (mode_p)
                  *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
          return not_equiv;
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
323
  EXPORT_SYMBOL(posix_acl_equiv_mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
325
326
327
328
  
  /*
   * Create an ACL representing the file mode permission bits of an inode.
   */
  struct posix_acl *
3a5fba19b   Al Viro   switch posix_acl_...
329
  posix_acl_from_mode(umode_t mode, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
332
333
334
335
  {
  	struct posix_acl *acl = posix_acl_alloc(3, flags);
  	if (!acl)
  		return ERR_PTR(-ENOMEM);
  
  	acl->a_entries[0].e_tag  = ACL_USER_OBJ;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
  	acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
  
  	acl->a_entries[1].e_tag  = ACL_GROUP_OBJ;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
340
341
  	acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
  
  	acl->a_entries[2].e_tag  = ACL_OTHER;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342
343
344
  	acl->a_entries[2].e_perm = (mode & S_IRWXO);
  	return acl;
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
345
  EXPORT_SYMBOL(posix_acl_from_mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
348
349
350
351
  
  /*
   * Return 0 if current is granted want access to the inode
   * by the acl. Returns -E... otherwise.
   */
  int
47291baa8   Christian Brauner   namei: make permi...
352
353
  posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode,
  		     const struct posix_acl *acl, int want)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
356
  {
  	const struct posix_acl_entry *pa, *pe, *mask_obj;
  	int found = 0;
47291baa8   Christian Brauner   namei: make permi...
357
358
  	kuid_t uid;
  	kgid_t gid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359

63d72b93f   Linus Torvalds   vfs: clean up pos...
360
  	want &= MAY_READ | MAY_WRITE | MAY_EXEC;
d124b60a8   Andreas Gruenbacher   vfs: pass all mas...
361

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
362
363
364
365
  	FOREACH_ACL_ENTRY(pa, acl, pe) {
                  switch(pa->e_tag) {
                          case ACL_USER_OBJ:
  				/* (May have been checked already) */
47291baa8   Christian Brauner   namei: make permi...
366
367
  				uid = i_uid_into_mnt(mnt_userns, inode);
  				if (uid_eq(uid, current_fsuid()))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
370
                                          goto check_perm;
                                  break;
                          case ACL_USER:
f895d0ff4   Christian Brauner   fs: use low-level...
371
  				uid = mapped_kuid_fs(mnt_userns,
38753e917   Christian Brauner   fs: support mappe...
372
373
  						     i_user_ns(inode),
  						     pa->e_uid);
47291baa8   Christian Brauner   namei: make permi...
374
  				if (uid_eq(uid, current_fsuid()))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
376
377
                                          goto mask;
  				break;
                          case ACL_GROUP_OBJ:
47291baa8   Christian Brauner   namei: make permi...
378
379
  				gid = i_gid_into_mnt(mnt_userns, inode);
  				if (in_group_p(gid)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
381
382
383
384
385
  					found = 1;
  					if ((pa->e_perm & want) == want)
  						goto mask;
                                  }
  				break;
                          case ACL_GROUP:
f895d0ff4   Christian Brauner   fs: use low-level...
386
  				gid = mapped_kgid_fs(mnt_userns,
38753e917   Christian Brauner   fs: support mappe...
387
388
  						     i_user_ns(inode),
  						     pa->e_gid);
47291baa8   Christian Brauner   namei: make permi...
389
  				if (in_group_p(gid)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  					found = 1;
  					if ((pa->e_perm & want) == want)
  						goto mask;
                                  }
                                  break;
                          case ACL_MASK:
                                  break;
                          case ACL_OTHER:
  				if (found)
  					return -EACCES;
  				else
  					goto check_perm;
  			default:
  				return -EIO;
                  }
          }
  	return -EIO;
  
  mask:
  	for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
  		if (mask_obj->e_tag == ACL_MASK) {
  			if ((pa->e_perm & mask_obj->e_perm & want) == want)
  				return 0;
  			return -EACCES;
  		}
  	}
  
  check_perm:
  	if ((pa->e_perm & want) == want)
  		return 0;
  	return -EACCES;
  }
  
  /*
   * Modify acl when creating a new inode. The caller must ensure the acl is
   * only referenced once.
   *
   * mode_p initially must contain the mode parameter to the open() / creat()
   * system calls. All permissions that are not granted by the acl are removed.
   * The permissions in the acl are changed to reflect the mode_p parameter.
   */
d3fb61207   Al Viro   switch posix_acl_...
431
  static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
434
  {
  	struct posix_acl_entry *pa, *pe;
  	struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
d3fb61207   Al Viro   switch posix_acl_...
435
  	umode_t mode = *mode_p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
437
438
439
440
441
442
443
444
445
446
447
448
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
  	int not_equiv = 0;
  
  	/* assert(atomic_read(acl->a_refcount) == 1); */
  
  	FOREACH_ACL_ENTRY(pa, acl, pe) {
                  switch(pa->e_tag) {
                          case ACL_USER_OBJ:
  				pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  				mode &= (pa->e_perm << 6) | ~S_IRWXU;
  				break;
  
  			case ACL_USER:
  			case ACL_GROUP:
  				not_equiv = 1;
  				break;
  
                          case ACL_GROUP_OBJ:
  				group_obj = pa;
                                  break;
  
                          case ACL_OTHER:
  				pa->e_perm &= mode | ~S_IRWXO;
  				mode &= pa->e_perm | ~S_IRWXO;
                                  break;
  
                          case ACL_MASK:
  				mask_obj = pa;
  				not_equiv = 1;
                                  break;
  
  			default:
  				return -EIO;
                  }
          }
  
  	if (mask_obj) {
  		mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  		mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  	} else {
  		if (!group_obj)
  			return -EIO;
  		group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  		mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  	}
  
  	*mode_p = (*mode_p & ~S_IRWXUGO) | mode;
          return not_equiv;
  }
  
  /*
   * Modify the ACL for the chmod syscall.
   */
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
488
  static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
  {
  	struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  	struct posix_acl_entry *pa, *pe;
  
  	/* assert(atomic_read(acl->a_refcount) == 1); */
  
  	FOREACH_ACL_ENTRY(pa, acl, pe) {
  		switch(pa->e_tag) {
  			case ACL_USER_OBJ:
  				pa->e_perm = (mode & S_IRWXU) >> 6;
  				break;
  
  			case ACL_USER:
  			case ACL_GROUP:
  				break;
  
  			case ACL_GROUP_OBJ:
  				group_obj = pa;
  				break;
  
  			case ACL_MASK:
  				mask_obj = pa;
  				break;
  
  			case ACL_OTHER:
  				pa->e_perm = (mode & S_IRWXO);
  				break;
  
  			default:
  				return -EIO;
  		}
  	}
  
  	if (mask_obj) {
  		mask_obj->e_perm = (mode & S_IRWXG) >> 3;
  	} else {
  		if (!group_obj)
  			return -EIO;
  		group_obj->e_perm = (mode & S_IRWXG) >> 3;
  	}
  
  	return 0;
  }
bc26ab5f6   Al Viro   kill boilerplate ...
532
533
  
  int
37bc15392   Christoph Hellwig   fs: make posix_ac...
534
  __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
826cae2f2   Al Viro   kill boilerplates...
535
536
537
538
539
540
541
542
543
544
545
546
547
548
  {
  	struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  	int err = -ENOMEM;
  	if (clone) {
  		err = posix_acl_create_masq(clone, mode_p);
  		if (err < 0) {
  			posix_acl_release(clone);
  			clone = NULL;
  		}
  	}
  	posix_acl_release(*acl);
  	*acl = clone;
  	return err;
  }
37bc15392   Christoph Hellwig   fs: make posix_ac...
549
  EXPORT_SYMBOL(__posix_acl_create);
826cae2f2   Al Viro   kill boilerplates...
550
551
  
  int
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
552
  __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
bc26ab5f6   Al Viro   kill boilerplate ...
553
554
555
556
  {
  	struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  	int err = -ENOMEM;
  	if (clone) {
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
557
  		err = __posix_acl_chmod_masq(clone, mode);
bc26ab5f6   Al Viro   kill boilerplate ...
558
559
560
561
562
563
564
565
566
  		if (err) {
  			posix_acl_release(clone);
  			clone = NULL;
  		}
  	}
  	posix_acl_release(*acl);
  	*acl = clone;
  	return err;
  }
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
567
  EXPORT_SYMBOL(__posix_acl_chmod);
e65ce2a50   Christian Brauner   acl: handle idmap...
568
569
570
571
572
573
574
575
576
577
578
579
580
  /**
   * posix_acl_chmod - chmod a posix acl
   *
   * @mnt_userns:	user namespace of the mount @inode was found from
   * @inode:	inode to check permissions on
   * @mode:	the new mode of @inode
   *
   * If the inode has been found through an idmapped mount the user namespace of
   * the vfsmount must be passed through @mnt_userns. This function will then
   * take care to map the inode according to @mnt_userns before checking
   * permissions. On non-idmapped mounts or if permission checking is to be
   * performed on the raw inode simply passs init_user_ns.
   */
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
581
  int
e65ce2a50   Christian Brauner   acl: handle idmap...
582
583
   posix_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode,
  		    umode_t mode)
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
584
585
586
587
588
589
590
591
592
593
  {
  	struct posix_acl *acl;
  	int ret = 0;
  
  	if (!IS_POSIXACL(inode))
  		return 0;
  	if (!inode->i_op->set_acl)
  		return -EOPNOTSUPP;
  
  	acl = get_acl(inode, ACL_TYPE_ACCESS);
789b663ae   Trond Myklebust   fs: get_acl() mus...
594
595
596
  	if (IS_ERR_OR_NULL(acl)) {
  		if (acl == ERR_PTR(-EOPNOTSUPP))
  			return 0;
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
597
  		return PTR_ERR(acl);
789b663ae   Trond Myklebust   fs: get_acl() mus...
598
  	}
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
599

37bc15392   Christoph Hellwig   fs: make posix_ac...
600
  	ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
601
602
  	if (ret)
  		return ret;
549c72977   Christian Brauner   fs: make helpers ...
603
  	ret = inode->i_op->set_acl(mnt_userns, inode, acl, ACL_TYPE_ACCESS);
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
604
605
606
  	posix_acl_release(acl);
  	return ret;
  }
bc26ab5f6   Al Viro   kill boilerplate ...
607
  EXPORT_SYMBOL(posix_acl_chmod);
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
608

37bc15392   Christoph Hellwig   fs: make posix_ac...
609
610
611
612
613
  int
  posix_acl_create(struct inode *dir, umode_t *mode,
  		struct posix_acl **default_acl, struct posix_acl **acl)
  {
  	struct posix_acl *p;
c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
614
  	struct posix_acl *clone;
37bc15392   Christoph Hellwig   fs: make posix_ac...
615
  	int ret;
c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
616
617
  	*acl = NULL;
  	*default_acl = NULL;
37bc15392   Christoph Hellwig   fs: make posix_ac...
618
  	if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
619
  		return 0;
37bc15392   Christoph Hellwig   fs: make posix_ac...
620
621
  
  	p = get_acl(dir, ACL_TYPE_DEFAULT);
c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
622
623
624
  	if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
  		*mode &= ~current_umask();
  		return 0;
37bc15392   Christoph Hellwig   fs: make posix_ac...
625
  	}
c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
626
627
  	if (IS_ERR(p))
  		return PTR_ERR(p);
37bc15392   Christoph Hellwig   fs: make posix_ac...
628

beaf226b8   Miklos Szeredi   posix_acl: don't ...
629
  	ret = -ENOMEM;
c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
630
631
  	clone = posix_acl_clone(p, GFP_NOFS);
  	if (!clone)
beaf226b8   Miklos Szeredi   posix_acl: don't ...
632
  		goto err_release;
37bc15392   Christoph Hellwig   fs: make posix_ac...
633

c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
634
  	ret = posix_acl_create_masq(clone, mode);
fed0b588b   Omar Sandoval   posix_acl: fix re...
635
  	if (ret < 0)
beaf226b8   Miklos Szeredi   posix_acl: don't ...
636
  		goto err_release_clone;
37bc15392   Christoph Hellwig   fs: make posix_ac...
637

c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
638
639
640
641
  	if (ret == 0)
  		posix_acl_release(clone);
  	else
  		*acl = clone;
37bc15392   Christoph Hellwig   fs: make posix_ac...
642

c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
643
  	if (!S_ISDIR(*mode))
37bc15392   Christoph Hellwig   fs: make posix_ac...
644
  		posix_acl_release(p);
c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
645
  	else
37bc15392   Christoph Hellwig   fs: make posix_ac...
646
  		*default_acl = p;
37bc15392   Christoph Hellwig   fs: make posix_ac...
647

37bc15392   Christoph Hellwig   fs: make posix_ac...
648
  	return 0;
fed0b588b   Omar Sandoval   posix_acl: fix re...
649

beaf226b8   Miklos Szeredi   posix_acl: don't ...
650
  err_release_clone:
c0c3a718e   Dan Carpenter   fs/posix_acl.c: m...
651
  	posix_acl_release(clone);
beaf226b8   Miklos Szeredi   posix_acl: don't ...
652
  err_release:
fed0b588b   Omar Sandoval   posix_acl: fix re...
653
  	posix_acl_release(p);
beaf226b8   Miklos Szeredi   posix_acl: don't ...
654
  	return ret;
37bc15392   Christoph Hellwig   fs: make posix_ac...
655
656
  }
  EXPORT_SYMBOL_GPL(posix_acl_create);
073931017   Jan Kara   posix_acl: Clear ...
657
658
  /**
   * posix_acl_update_mode  -  update mode in set_acl
e65ce2a50   Christian Brauner   acl: handle idmap...
659
660
661
662
   * @mnt_userns:	user namespace of the mount @inode was found from
   * @inode:	target inode
   * @mode_p:	mode (pointer) for update
   * @acl:	acl pointer
073931017   Jan Kara   posix_acl: Clear ...
663
664
665
   *
   * Update the file mode when setting an ACL: compute the new file permission
   * bits based on the ACL.  In addition, if the ACL is equivalent to the new
e39e773ad   Randy Dunlap   fs/posix_acl.c: f...
666
   * file mode, set *@acl to NULL to indicate that no ACL should be set.
073931017   Jan Kara   posix_acl: Clear ...
667
   *
e39e773ad   Randy Dunlap   fs/posix_acl.c: f...
668
   * As with chmod, clear the setgid bit if the caller is not in the owning group
073931017   Jan Kara   posix_acl: Clear ...
669
670
   * or capable of CAP_FSETID (see inode_change_ok).
   *
e65ce2a50   Christian Brauner   acl: handle idmap...
671
672
673
674
675
676
   * If the inode has been found through an idmapped mount the user namespace of
   * the vfsmount must be passed through @mnt_userns. This function will then
   * take care to map the inode according to @mnt_userns before checking
   * permissions. On non-idmapped mounts or if permission checking is to be
   * performed on the raw inode simply passs init_user_ns.
   *
073931017   Jan Kara   posix_acl: Clear ...
677
678
   * Called from set_acl inode operations.
   */
e65ce2a50   Christian Brauner   acl: handle idmap...
679
680
  int posix_acl_update_mode(struct user_namespace *mnt_userns,
  			  struct inode *inode, umode_t *mode_p,
073931017   Jan Kara   posix_acl: Clear ...
681
682
683
684
685
686
687
688
689
690
  			  struct posix_acl **acl)
  {
  	umode_t mode = inode->i_mode;
  	int error;
  
  	error = posix_acl_equiv_mode(*acl, &mode);
  	if (error < 0)
  		return error;
  	if (error == 0)
  		*acl = NULL;
e65ce2a50   Christian Brauner   acl: handle idmap...
691
692
  	if (!in_group_p(i_gid_into_mnt(mnt_userns, inode)) &&
  	    !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
073931017   Jan Kara   posix_acl: Clear ...
693
694
695
696
697
  		mode &= ~S_ISGID;
  	*mode_p = mode;
  	return 0;
  }
  EXPORT_SYMBOL(posix_acl_update_mode);
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
698
699
700
701
702
  /*
   * Fix up the uids and gids in posix acl extended attributes in place.
   */
  static void posix_acl_fix_xattr_userns(
  	struct user_namespace *to, struct user_namespace *from,
e65ce2a50   Christian Brauner   acl: handle idmap...
703
704
  	struct user_namespace *mnt_userns,
  	void *value, size_t size, bool from_user)
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
705
  {
2211d5ba5   Andreas Gruenbacher   posix_acl: xattr ...
706
707
  	struct posix_acl_xattr_header *header = value;
  	struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
708
709
710
711
712
713
  	int count;
  	kuid_t uid;
  	kgid_t gid;
  
  	if (!value)
  		return;
2211d5ba5   Andreas Gruenbacher   posix_acl: xattr ...
714
  	if (size < sizeof(struct posix_acl_xattr_header))
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
715
716
717
718
719
720
721
722
723
724
725
726
727
728
  		return;
  	if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  		return;
  
  	count = posix_acl_xattr_count(size);
  	if (count < 0)
  		return;
  	if (count == 0)
  		return;
  
  	for (end = entry + count; entry != end; entry++) {
  		switch(le16_to_cpu(entry->e_tag)) {
  		case ACL_USER:
  			uid = make_kuid(from, le32_to_cpu(entry->e_id));
e65ce2a50   Christian Brauner   acl: handle idmap...
729
  			if (from_user)
f895d0ff4   Christian Brauner   fs: use low-level...
730
  				uid = mapped_kuid_user(mnt_userns, &init_user_ns, uid);
e65ce2a50   Christian Brauner   acl: handle idmap...
731
  			else
f895d0ff4   Christian Brauner   fs: use low-level...
732
  				uid = mapped_kuid_fs(mnt_userns, &init_user_ns, uid);
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
733
734
735
736
  			entry->e_id = cpu_to_le32(from_kuid(to, uid));
  			break;
  		case ACL_GROUP:
  			gid = make_kgid(from, le32_to_cpu(entry->e_id));
e65ce2a50   Christian Brauner   acl: handle idmap...
737
  			if (from_user)
f895d0ff4   Christian Brauner   fs: use low-level...
738
  				gid = mapped_kgid_user(mnt_userns, &init_user_ns, gid);
e65ce2a50   Christian Brauner   acl: handle idmap...
739
  			else
f895d0ff4   Christian Brauner   fs: use low-level...
740
  				gid = mapped_kgid_fs(mnt_userns, &init_user_ns, gid);
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
741
742
743
744
745
746
747
  			entry->e_id = cpu_to_le32(from_kgid(to, gid));
  			break;
  		default:
  			break;
  		}
  	}
  }
e65ce2a50   Christian Brauner   acl: handle idmap...
748
  void posix_acl_fix_xattr_from_user(struct user_namespace *mnt_userns,
dc85bc24f   Christian Brauner   fs: fix acl trans...
749
  				   struct inode *inode,
e65ce2a50   Christian Brauner   acl: handle idmap...
750
  				   void *value, size_t size)
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
751
752
  {
  	struct user_namespace *user_ns = current_user_ns();
dc85bc24f   Christian Brauner   fs: fix acl trans...
753
754
755
756
  
  	/* Leave ids untouched on non-idmapped mounts. */
  	if (no_idmapping(mnt_userns, i_user_ns(inode)))
  		mnt_userns = &init_user_ns;
e65ce2a50   Christian Brauner   acl: handle idmap...
757
  	if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns))
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
758
  		return;
e65ce2a50   Christian Brauner   acl: handle idmap...
759
760
  	posix_acl_fix_xattr_userns(&init_user_ns, user_ns, mnt_userns, value,
  				   size, true);
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
761
  }
e65ce2a50   Christian Brauner   acl: handle idmap...
762
  void posix_acl_fix_xattr_to_user(struct user_namespace *mnt_userns,
dc85bc24f   Christian Brauner   fs: fix acl trans...
763
  				 struct inode *inode,
e65ce2a50   Christian Brauner   acl: handle idmap...
764
  				 void *value, size_t size)
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
765
766
  {
  	struct user_namespace *user_ns = current_user_ns();
dc85bc24f   Christian Brauner   fs: fix acl trans...
767
768
769
770
  
  	/* Leave ids untouched on non-idmapped mounts. */
  	if (no_idmapping(mnt_userns, i_user_ns(inode)))
  		mnt_userns = &init_user_ns;
e65ce2a50   Christian Brauner   acl: handle idmap...
771
  	if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns))
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
772
  		return;
e65ce2a50   Christian Brauner   acl: handle idmap...
773
774
  	posix_acl_fix_xattr_userns(user_ns, &init_user_ns, mnt_userns, value,
  				   size, false);
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
775
776
777
778
779
780
781
782
783
  }
  
  /*
   * Convert from extended attribute to in-memory representation.
   */
  struct posix_acl *
  posix_acl_from_xattr(struct user_namespace *user_ns,
  		     const void *value, size_t size)
  {
2211d5ba5   Andreas Gruenbacher   posix_acl: xattr ...
784
785
  	const struct posix_acl_xattr_header *header = value;
  	const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
786
787
788
789
790
791
  	int count;
  	struct posix_acl *acl;
  	struct posix_acl_entry *acl_e;
  
  	if (!value)
  		return NULL;
2211d5ba5   Andreas Gruenbacher   posix_acl: xattr ...
792
  	if (size < sizeof(struct posix_acl_xattr_header))
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
  		 return ERR_PTR(-EINVAL);
  	if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  		return ERR_PTR(-EOPNOTSUPP);
  
  	count = posix_acl_xattr_count(size);
  	if (count < 0)
  		return ERR_PTR(-EINVAL);
  	if (count == 0)
  		return NULL;
  	
  	acl = posix_acl_alloc(count, GFP_NOFS);
  	if (!acl)
  		return ERR_PTR(-ENOMEM);
  	acl_e = acl->a_entries;
  	
  	for (end = entry + count; entry != end; acl_e++, entry++) {
  		acl_e->e_tag  = le16_to_cpu(entry->e_tag);
  		acl_e->e_perm = le16_to_cpu(entry->e_perm);
  
  		switch(acl_e->e_tag) {
  			case ACL_USER_OBJ:
  			case ACL_GROUP_OBJ:
  			case ACL_MASK:
  			case ACL_OTHER:
  				break;
  
  			case ACL_USER:
  				acl_e->e_uid =
  					make_kuid(user_ns,
  						  le32_to_cpu(entry->e_id));
  				if (!uid_valid(acl_e->e_uid))
  					goto fail;
  				break;
  			case ACL_GROUP:
  				acl_e->e_gid =
  					make_kgid(user_ns,
  						  le32_to_cpu(entry->e_id));
  				if (!gid_valid(acl_e->e_gid))
  					goto fail;
  				break;
  
  			default:
  				goto fail;
  		}
  	}
  	return acl;
  
  fail:
  	posix_acl_release(acl);
  	return ERR_PTR(-EINVAL);
  }
  EXPORT_SYMBOL (posix_acl_from_xattr);
  
  /*
   * Convert from in-memory to extended attribute representation.
   */
  int
  posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
  		   void *buffer, size_t size)
  {
2211d5ba5   Andreas Gruenbacher   posix_acl: xattr ...
853
854
  	struct posix_acl_xattr_header *ext_acl = buffer;
  	struct posix_acl_xattr_entry *ext_entry;
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
855
856
857
858
859
860
861
  	int real_size, n;
  
  	real_size = posix_acl_xattr_size(acl->a_count);
  	if (!buffer)
  		return real_size;
  	if (real_size > size)
  		return -ERANGE;
47ba97344   Dan Carpenter   fs: NULL derefere...
862

2211d5ba5   Andreas Gruenbacher   posix_acl: xattr ...
863
  	ext_entry = (void *)(ext_acl + 1);
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
  	ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  
  	for (n=0; n < acl->a_count; n++, ext_entry++) {
  		const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  		ext_entry->e_tag  = cpu_to_le16(acl_e->e_tag);
  		ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
  		switch(acl_e->e_tag) {
  		case ACL_USER:
  			ext_entry->e_id =
  				cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
  			break;
  		case ACL_GROUP:
  			ext_entry->e_id =
  				cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
  			break;
  		default:
  			ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  			break;
  		}
  	}
  	return real_size;
  }
  EXPORT_SYMBOL (posix_acl_to_xattr);
2aeccbe95   Christoph Hellwig   fs: add generic x...
887
888
  
  static int
d9a82a040   Andreas Gruenbacher   xattr handlers: P...
889
  posix_acl_xattr_get(const struct xattr_handler *handler,
b296821a7   Al Viro   xattr_handler: pa...
890
891
  		    struct dentry *unused, struct inode *inode,
  		    const char *name, void *value, size_t size)
2aeccbe95   Christoph Hellwig   fs: add generic x...
892
893
894
  {
  	struct posix_acl *acl;
  	int error;
b296821a7   Al Viro   xattr_handler: pa...
895
  	if (!IS_POSIXACL(inode))
2aeccbe95   Christoph Hellwig   fs: add generic x...
896
  		return -EOPNOTSUPP;
b296821a7   Al Viro   xattr_handler: pa...
897
  	if (S_ISLNK(inode->i_mode))
2aeccbe95   Christoph Hellwig   fs: add generic x...
898
  		return -EOPNOTSUPP;
b296821a7   Al Viro   xattr_handler: pa...
899
  	acl = get_acl(inode, handler->flags);
2aeccbe95   Christoph Hellwig   fs: add generic x...
900
901
902
903
904
905
906
907
908
909
  	if (IS_ERR(acl))
  		return PTR_ERR(acl);
  	if (acl == NULL)
  		return -ENODATA;
  
  	error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  	posix_acl_release(acl);
  
  	return error;
  }
485e71e8f   Andreas Gruenbacher   posix_acl: Add se...
910
  int
e65ce2a50   Christian Brauner   acl: handle idmap...
911
912
  set_posix_acl(struct user_namespace *mnt_userns, struct inode *inode,
  	      int type, struct posix_acl *acl)
2aeccbe95   Christoph Hellwig   fs: add generic x...
913
  {
2aeccbe95   Christoph Hellwig   fs: add generic x...
914
915
916
917
  	if (!IS_POSIXACL(inode))
  		return -EOPNOTSUPP;
  	if (!inode->i_op->set_acl)
  		return -EOPNOTSUPP;
485e71e8f   Andreas Gruenbacher   posix_acl: Add se...
918
919
  	if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  		return acl ? -EACCES : 0;
e65ce2a50   Christian Brauner   acl: handle idmap...
920
  	if (!inode_owner_or_capable(mnt_userns, inode))
2aeccbe95   Christoph Hellwig   fs: add generic x...
921
  		return -EPERM;
485e71e8f   Andreas Gruenbacher   posix_acl: Add se...
922
  	if (acl) {
a867d7349   Linus Torvalds   Merge branch 'for...
923
  		int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
485e71e8f   Andreas Gruenbacher   posix_acl: Add se...
924
925
926
  		if (ret)
  			return ret;
  	}
549c72977   Christian Brauner   fs: make helpers ...
927
  	return inode->i_op->set_acl(mnt_userns, inode, acl, type);
485e71e8f   Andreas Gruenbacher   posix_acl: Add se...
928
929
930
931
932
  }
  EXPORT_SYMBOL(set_posix_acl);
  
  static int
  posix_acl_xattr_set(const struct xattr_handler *handler,
e65ce2a50   Christian Brauner   acl: handle idmap...
933
934
935
936
  			   struct user_namespace *mnt_userns,
  			   struct dentry *unused, struct inode *inode,
  			   const char *name, const void *value, size_t size,
  			   int flags)
485e71e8f   Andreas Gruenbacher   posix_acl: Add se...
937
938
939
  {
  	struct posix_acl *acl = NULL;
  	int ret;
2aeccbe95   Christoph Hellwig   fs: add generic x...
940
941
942
943
  	if (value) {
  		acl = posix_acl_from_xattr(&init_user_ns, value, size);
  		if (IS_ERR(acl))
  			return PTR_ERR(acl);
2aeccbe95   Christoph Hellwig   fs: add generic x...
944
  	}
e65ce2a50   Christian Brauner   acl: handle idmap...
945
  	ret = set_posix_acl(mnt_userns, inode, handler->flags, acl);
2aeccbe95   Christoph Hellwig   fs: add generic x...
946
947
948
  	posix_acl_release(acl);
  	return ret;
  }
764a5c6b1   Andreas Gruenbacher   xattr handlers: S...
949
950
  static bool
  posix_acl_xattr_list(struct dentry *dentry)
2aeccbe95   Christoph Hellwig   fs: add generic x...
951
  {
764a5c6b1   Andreas Gruenbacher   xattr handlers: S...
952
  	return IS_POSIXACL(d_backing_inode(dentry));
2aeccbe95   Christoph Hellwig   fs: add generic x...
953
954
955
  }
  
  const struct xattr_handler posix_acl_access_xattr_handler = {
98e9cb571   Andreas Gruenbacher   vfs: Distinguish ...
956
  	.name = XATTR_NAME_POSIX_ACL_ACCESS,
2aeccbe95   Christoph Hellwig   fs: add generic x...
957
958
959
960
961
962
963
964
  	.flags = ACL_TYPE_ACCESS,
  	.list = posix_acl_xattr_list,
  	.get = posix_acl_xattr_get,
  	.set = posix_acl_xattr_set,
  };
  EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
  
  const struct xattr_handler posix_acl_default_xattr_handler = {
98e9cb571   Andreas Gruenbacher   vfs: Distinguish ...
965
  	.name = XATTR_NAME_POSIX_ACL_DEFAULT,
2aeccbe95   Christoph Hellwig   fs: add generic x...
966
967
968
969
970
971
  	.flags = ACL_TYPE_DEFAULT,
  	.list = posix_acl_xattr_list,
  	.get = posix_acl_xattr_get,
  	.set = posix_acl_xattr_set,
  };
  EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
feda821e7   Christoph Hellwig   fs: remove generi...
972

549c72977   Christian Brauner   fs: make helpers ...
973
974
  int simple_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
  		   struct posix_acl *acl, int type)
feda821e7   Christoph Hellwig   fs: remove generi...
975
976
977
978
  {
  	int error;
  
  	if (type == ACL_TYPE_ACCESS) {
549c72977   Christian Brauner   fs: make helpers ...
979
  		error = posix_acl_update_mode(mnt_userns, inode,
497de07d8   Gu Zheng   tmpfs: clear S_IS...
980
981
982
  				&inode->i_mode, &acl);
  		if (error)
  			return error;
feda821e7   Christoph Hellwig   fs: remove generi...
983
  	}
078cd8279   Deepa Dinamani   fs: Replace CURRE...
984
  	inode->i_ctime = current_time(inode);
feda821e7   Christoph Hellwig   fs: remove generi...
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  	set_cached_acl(inode, type, acl);
  	return 0;
  }
  
  int simple_acl_create(struct inode *dir, struct inode *inode)
  {
  	struct posix_acl *default_acl, *acl;
  	int error;
  
  	error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  	if (error)
  		return error;
  
  	set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  	set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  
  	if (default_acl)
  		posix_acl_release(default_acl);
  	if (acl)
  		posix_acl_release(acl);
  	return 0;
  }