Blame view

fs/posix_acl.c 19.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
2
   * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
   *
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
4
5
   * Fixes from William Schumacher incorporated on 15 March 2001.
   *    (Reported by Charles Bertsch, <CBertsch@microtest.com>).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
9
10
11
12
13
14
   */
  
  /*
   *  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...
15
  #include <linux/atomic.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
17
18
  #include <linux/fs.h>
  #include <linux/sched.h>
  #include <linux/posix_acl.h>
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
19
  #include <linux/posix_acl_xattr.h>
2aeccbe95   Christoph Hellwig   fs: add generic x...
20
  #include <linux/xattr.h>
630d9c472   Paul Gortmaker   fs: reduce the us...
21
  #include <linux/export.h>
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
22
  #include <linux/user_namespace.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23

0afaa1204   Andrew Morton   posix_acl: uninli...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  struct posix_acl **acl_by_type(struct inode *inode, int type)
  {
  	switch (type) {
  	case ACL_TYPE_ACCESS:
  		return &inode->i_acl;
  	case ACL_TYPE_DEFAULT:
  		return &inode->i_default_acl;
  	default:
  		BUG();
  	}
  }
  EXPORT_SYMBOL(acl_by_type);
  
  struct posix_acl *get_cached_acl(struct inode *inode, int type)
  {
  	struct posix_acl **p = acl_by_type(inode, type);
  	struct posix_acl *acl = ACCESS_ONCE(*p);
  	if (acl) {
  		spin_lock(&inode->i_lock);
  		acl = *p;
  		if (acl != ACL_NOT_CACHED)
  			acl = posix_acl_dup(acl);
  		spin_unlock(&inode->i_lock);
  	}
  	return acl;
  }
  EXPORT_SYMBOL(get_cached_acl);
  
  struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
  {
  	return rcu_dereference(*acl_by_type(inode, type));
  }
  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;
  	spin_lock(&inode->i_lock);
  	old = *p;
  	rcu_assign_pointer(*p, posix_acl_dup(acl));
  	spin_unlock(&inode->i_lock);
  	if (old != ACL_NOT_CACHED)
  		posix_acl_release(old);
  }
  EXPORT_SYMBOL(set_cached_acl);
  
  void forget_cached_acl(struct inode *inode, int type)
  {
  	struct posix_acl **p = acl_by_type(inode, type);
  	struct posix_acl *old;
  	spin_lock(&inode->i_lock);
  	old = *p;
  	*p = ACL_NOT_CACHED;
  	spin_unlock(&inode->i_lock);
  	if (old != ACL_NOT_CACHED)
  		posix_acl_release(old);
  }
  EXPORT_SYMBOL(forget_cached_acl);
  
  void forget_all_cached_acls(struct inode *inode)
  {
  	struct posix_acl *old_access, *old_default;
  	spin_lock(&inode->i_lock);
  	old_access = inode->i_acl;
  	old_default = inode->i_default_acl;
  	inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  	spin_unlock(&inode->i_lock);
  	if (old_access != ACL_NOT_CACHED)
  		posix_acl_release(old_access);
  	if (old_default != ACL_NOT_CACHED)
  		posix_acl_release(old_default);
  }
  EXPORT_SYMBOL(forget_all_cached_acls);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98

2982baa2a   Christoph Hellwig   fs: add get_acl h...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  struct posix_acl *get_acl(struct inode *inode, int type)
  {
  	struct posix_acl *acl;
  
  	acl = get_cached_acl(inode, type);
  	if (acl != ACL_NOT_CACHED)
  		return acl;
  
  	if (!IS_POSIXACL(inode))
  		return NULL;
  
  	/*
  	 * A filesystem can force a ACL callback by just never filling the
  	 * ACL cache. But normally you'd fill the cache either at inode
  	 * instantiation time, or on the first ->get_acl call.
  	 *
  	 * 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;
  	}
  	return inode->i_op->get_acl(inode, type);
  }
  EXPORT_SYMBOL(get_acl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
  /*
f61f6da0d   Chuck Lever   NFS: Prevent memo...
126
127
128
129
130
131
132
133
   * Init a fresh posix_acl
   */
  void
  posix_acl_init(struct posix_acl *acl, int count)
  {
  	atomic_set(&acl->a_refcount, 1);
  	acl->a_count = count;
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
134
  EXPORT_SYMBOL(posix_acl_init);
f61f6da0d   Chuck Lever   NFS: Prevent memo...
135
136
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
139
   * Allocate a new ACL with the specified number of entries.
   */
  struct posix_acl *
dd0fc66fb   Al Viro   [PATCH] gfp flags...
140
  posix_acl_alloc(int count, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
143
144
  {
  	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...
145
146
  	if (acl)
  		posix_acl_init(acl, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
  	return acl;
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
149
  EXPORT_SYMBOL(posix_acl_alloc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
152
153
  
  /*
   * Clone an ACL.
   */
edde854e8   Al Viro   bury posix_acl_.....
154
  static struct posix_acl *
dd0fc66fb   Al Viro   [PATCH] gfp flags...
155
  posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
161
  {
  	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: ...
162
163
  		clone = kmemdup(acl, size, flags);
  		if (clone)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
  			atomic_set(&clone->a_refcount, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
167
168
169
170
171
172
173
174
175
176
  	}
  	return clone;
  }
  
  /*
   * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
   */
  int
  posix_acl_valid(const struct posix_acl *acl)
  {
  	const struct posix_acl_entry *pa, *pe;
  	int state = ACL_USER_OBJ;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
179
180
181
182
183
184
  	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
185
186
187
188
189
190
191
192
  					state = ACL_USER;
  					break;
  				}
  				return -EINVAL;
  
  			case ACL_USER:
  				if (state != ACL_USER)
  					return -EINVAL;
2f6f0654a   Eric W. Biederman   userns: Convert v...
193
  				if (!uid_valid(pa->e_uid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
  					return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
198
199
  				needs_mask = 1;
  				break;
  
  			case ACL_GROUP_OBJ:
  				if (state == ACL_USER) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
203
204
205
206
207
  					state = ACL_GROUP;
  					break;
  				}
  				return -EINVAL;
  
  			case ACL_GROUP:
  				if (state != ACL_GROUP)
  					return -EINVAL;
2f6f0654a   Eric W. Biederman   userns: Convert v...
208
209
  				if (!gid_valid(pa->e_gid))
  					return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  				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...
235
  EXPORT_SYMBOL(posix_acl_valid);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
237
238
239
240
241
  
  /*
   * 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_...
242
  posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
244
  {
  	const struct posix_acl_entry *pa, *pe;
d6952123b   Al Viro   switch posix_acl_...
245
  	umode_t mode = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
  	int not_equiv = 0;
50c6e282b   Christoph Hellwig   posix_acl: handle...
247
248
249
250
251
  	/*
  	 * A null ACL can always be presented as mode bits.
  	 */
  	if (!acl)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
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
278
279
  	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...
280
  EXPORT_SYMBOL(posix_acl_equiv_mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
282
283
284
285
  
  /*
   * Create an ACL representing the file mode permission bits of an inode.
   */
  struct posix_acl *
3a5fba19b   Al Viro   switch posix_acl_...
286
  posix_acl_from_mode(umode_t mode, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
292
  {
  	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
293
294
295
  	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
296
297
298
  	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
299
300
301
  	acl->a_entries[2].e_perm = (mode & S_IRWXO);
  	return acl;
  }
0afaa1204   Andrew Morton   posix_acl: uninli...
302
  EXPORT_SYMBOL(posix_acl_from_mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
304
305
306
307
308
309
310
311
312
  
  /*
   * Return 0 if current is granted want access to the inode
   * by the acl. Returns -E... otherwise.
   */
  int
  posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
  {
  	const struct posix_acl_entry *pa, *pe, *mask_obj;
  	int found = 0;
d124b60a8   Andreas Gruenbacher   vfs: pass all mas...
313
  	want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
  	FOREACH_ACL_ENTRY(pa, acl, pe) {
                  switch(pa->e_tag) {
                          case ACL_USER_OBJ:
  				/* (May have been checked already) */
2f6f0654a   Eric W. Biederman   userns: Convert v...
318
  				if (uid_eq(inode->i_uid, current_fsuid()))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
                                          goto check_perm;
                                  break;
                          case ACL_USER:
2f6f0654a   Eric W. Biederman   userns: Convert v...
322
  				if (uid_eq(pa->e_uid, current_fsuid()))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
324
325
326
327
328
329
330
331
332
                                          goto mask;
  				break;
                          case ACL_GROUP_OBJ:
                                  if (in_group_p(inode->i_gid)) {
  					found = 1;
  					if ((pa->e_perm & want) == want)
  						goto mask;
                                  }
  				break;
                          case ACL_GROUP:
2f6f0654a   Eric W. Biederman   userns: Convert v...
333
  				if (in_group_p(pa->e_gid)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  					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_...
375
  static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
377
378
  {
  	struct posix_acl_entry *pa, *pe;
  	struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
d3fb61207   Al Viro   switch posix_acl_...
379
  	umode_t mode = *mode_p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
381
382
383
384
385
386
387
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
  	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...
432
  static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
434
435
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
  {
  	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 ...
476
477
  
  int
37bc15392   Christoph Hellwig   fs: make posix_ac...
478
  __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
826cae2f2   Al Viro   kill boilerplates...
479
480
481
482
483
484
485
486
487
488
489
490
491
492
  {
  	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...
493
  EXPORT_SYMBOL(__posix_acl_create);
826cae2f2   Al Viro   kill boilerplates...
494
495
  
  int
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
496
  __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
bc26ab5f6   Al Viro   kill boilerplate ...
497
498
499
500
  {
  	struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  	int err = -ENOMEM;
  	if (clone) {
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
501
  		err = __posix_acl_chmod_masq(clone, mode);
bc26ab5f6   Al Viro   kill boilerplate ...
502
503
504
505
506
507
508
509
510
  		if (err) {
  			posix_acl_release(clone);
  			clone = NULL;
  		}
  	}
  	posix_acl_release(*acl);
  	*acl = clone;
  	return err;
  }
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
511
512
513
  EXPORT_SYMBOL(__posix_acl_chmod);
  
  int
37bc15392   Christoph Hellwig   fs: make posix_ac...
514
  posix_acl_chmod(struct inode *inode, umode_t mode)
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
515
516
517
518
519
520
521
522
523
524
  {
  	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...
525
526
527
  	if (IS_ERR_OR_NULL(acl)) {
  		if (acl == ERR_PTR(-EOPNOTSUPP))
  			return 0;
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
528
  		return PTR_ERR(acl);
789b663ae   Trond Myklebust   fs: get_acl() mus...
529
  	}
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
530

37bc15392   Christoph Hellwig   fs: make posix_ac...
531
  	ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
532
533
534
535
536
537
  	if (ret)
  		return ret;
  	ret = inode->i_op->set_acl(inode, acl, ACL_TYPE_ACCESS);
  	posix_acl_release(acl);
  	return ret;
  }
bc26ab5f6   Al Viro   kill boilerplate ...
538
  EXPORT_SYMBOL(posix_acl_chmod);
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
539

37bc15392   Christoph Hellwig   fs: make posix_ac...
540
541
542
543
544
545
546
547
548
549
550
  int
  posix_acl_create(struct inode *dir, umode_t *mode,
  		struct posix_acl **default_acl, struct posix_acl **acl)
  {
  	struct posix_acl *p;
  	int ret;
  
  	if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
  		goto no_acl;
  
  	p = get_acl(dir, ACL_TYPE_DEFAULT);
789b663ae   Trond Myklebust   fs: get_acl() mus...
551
552
553
  	if (IS_ERR(p)) {
  		if (p == ERR_PTR(-EOPNOTSUPP))
  			goto apply_umask;
37bc15392   Christoph Hellwig   fs: make posix_ac...
554
  		return PTR_ERR(p);
37bc15392   Christoph Hellwig   fs: make posix_ac...
555
  	}
789b663ae   Trond Myklebust   fs: get_acl() mus...
556
557
  	if (!p)
  		goto apply_umask;
37bc15392   Christoph Hellwig   fs: make posix_ac...
558
559
  	*acl = posix_acl_clone(p, GFP_NOFS);
  	if (!*acl)
fed0b588b   Omar Sandoval   posix_acl: fix re...
560
  		goto no_mem;
37bc15392   Christoph Hellwig   fs: make posix_ac...
561
562
  
  	ret = posix_acl_create_masq(*acl, mode);
fed0b588b   Omar Sandoval   posix_acl: fix re...
563
564
  	if (ret < 0)
  		goto no_mem_clone;
37bc15392   Christoph Hellwig   fs: make posix_ac...
565
566
567
568
569
570
571
572
573
574
575
576
577
  
  	if (ret == 0) {
  		posix_acl_release(*acl);
  		*acl = NULL;
  	}
  
  	if (!S_ISDIR(*mode)) {
  		posix_acl_release(p);
  		*default_acl = NULL;
  	} else {
  		*default_acl = p;
  	}
  	return 0;
789b663ae   Trond Myklebust   fs: get_acl() mus...
578
579
  apply_umask:
  	*mode &= ~current_umask();
37bc15392   Christoph Hellwig   fs: make posix_ac...
580
581
582
583
  no_acl:
  	*default_acl = NULL;
  	*acl = NULL;
  	return 0;
fed0b588b   Omar Sandoval   posix_acl: fix re...
584
585
586
587
588
589
  
  no_mem_clone:
  	posix_acl_release(*acl);
  no_mem:
  	posix_acl_release(p);
  	return -ENOMEM;
37bc15392   Christoph Hellwig   fs: make posix_ac...
590
591
  }
  EXPORT_SYMBOL_GPL(posix_acl_create);
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
  /*
   * 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,
  	void *value, size_t size)
  {
  	posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
  	posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
  	int count;
  	kuid_t uid;
  	kgid_t gid;
  
  	if (!value)
  		return;
  	if (size < sizeof(posix_acl_xattr_header))
  		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));
  			entry->e_id = cpu_to_le32(from_kuid(to, uid));
  			break;
  		case ACL_GROUP:
  			gid = make_kgid(from, le32_to_cpu(entry->e_id));
  			entry->e_id = cpu_to_le32(from_kgid(to, gid));
  			break;
  		default:
  			break;
  		}
  	}
  }
  
  void posix_acl_fix_xattr_from_user(void *value, size_t size)
  {
  	struct user_namespace *user_ns = current_user_ns();
  	if (user_ns == &init_user_ns)
  		return;
  	posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
  }
  
  void posix_acl_fix_xattr_to_user(void *value, size_t size)
  {
  	struct user_namespace *user_ns = current_user_ns();
  	if (user_ns == &init_user_ns)
  		return;
  	posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
  }
  
  /*
   * 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)
  {
  	posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
  	posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
  	int count;
  	struct posix_acl *acl;
  	struct posix_acl_entry *acl_e;
  
  	if (!value)
  		return NULL;
  	if (size < sizeof(posix_acl_xattr_header))
  		 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)
  {
  	posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer;
47ba97344   Dan Carpenter   fs: NULL derefere...
727
  	posix_acl_xattr_entry *ext_entry;
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
728
729
730
731
732
733
734
  	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...
735
736
  
  	ext_entry = ext_acl->a_entries;
5c8ebd57b   Christoph Hellwig   fs: merge xattr_a...
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
  	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...
760
761
762
763
764
765
766
  
  static int
  posix_acl_xattr_get(struct dentry *dentry, const char *name,
  		void *value, size_t size, int type)
  {
  	struct posix_acl *acl;
  	int error;
bb668734c   David Howells   VFS: assorted d_b...
767
  	if (!IS_POSIXACL(d_backing_inode(dentry)))
2aeccbe95   Christoph Hellwig   fs: add generic x...
768
  		return -EOPNOTSUPP;
e36cb0b89   David Howells   VFS: (Scripted) C...
769
  	if (d_is_symlink(dentry))
2aeccbe95   Christoph Hellwig   fs: add generic x...
770
  		return -EOPNOTSUPP;
bb668734c   David Howells   VFS: assorted d_b...
771
  	acl = get_acl(d_backing_inode(dentry), type);
2aeccbe95   Christoph Hellwig   fs: add generic x...
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
  	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;
  }
  
  static int
  posix_acl_xattr_set(struct dentry *dentry, const char *name,
  		const void *value, size_t size, int flags, int type)
  {
bb668734c   David Howells   VFS: assorted d_b...
787
  	struct inode *inode = d_backing_inode(dentry);
2aeccbe95   Christoph Hellwig   fs: add generic x...
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
  	struct posix_acl *acl = NULL;
  	int ret;
  
  	if (!IS_POSIXACL(inode))
  		return -EOPNOTSUPP;
  	if (!inode->i_op->set_acl)
  		return -EOPNOTSUPP;
  
  	if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  		return value ? -EACCES : 0;
  	if (!inode_owner_or_capable(inode))
  		return -EPERM;
  
  	if (value) {
  		acl = posix_acl_from_xattr(&init_user_ns, value, size);
  		if (IS_ERR(acl))
  			return PTR_ERR(acl);
  
  		if (acl) {
  			ret = posix_acl_valid(acl);
  			if (ret)
  				goto out;
  		}
  	}
  
  	ret = inode->i_op->set_acl(inode, acl, type);
  out:
  	posix_acl_release(acl);
  	return ret;
  }
  
  static size_t
  posix_acl_xattr_list(struct dentry *dentry, char *list, size_t list_size,
  		const char *name, size_t name_len, int type)
  {
  	const char *xname;
  	size_t size;
bb668734c   David Howells   VFS: assorted d_b...
825
  	if (!IS_POSIXACL(d_backing_inode(dentry)))
2aeccbe95   Christoph Hellwig   fs: add generic x...
826
  		return -EOPNOTSUPP;
e36cb0b89   David Howells   VFS: (Scripted) C...
827
  	if (d_is_symlink(dentry))
2aeccbe95   Christoph Hellwig   fs: add generic x...
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
853
854
855
856
857
  		return -EOPNOTSUPP;
  
  	if (type == ACL_TYPE_ACCESS)
  		xname = POSIX_ACL_XATTR_ACCESS;
  	else
  		xname = POSIX_ACL_XATTR_DEFAULT;
  
  	size = strlen(xname) + 1;
  	if (list && size <= list_size)
  		memcpy(list, xname, size);
  	return size;
  }
  
  const struct xattr_handler posix_acl_access_xattr_handler = {
  	.prefix = POSIX_ACL_XATTR_ACCESS,
  	.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 = {
  	.prefix = POSIX_ACL_XATTR_DEFAULT,
  	.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...
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
888
889
890
891
892
893
  
  int simple_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  {
  	int error;
  
  	if (type == ACL_TYPE_ACCESS) {
  		error = posix_acl_equiv_mode(acl, &inode->i_mode);
  		if (error < 0)
  			return 0;
  		if (error == 0)
  			acl = NULL;
  	}
  
  	inode->i_ctime = CURRENT_TIME;
  	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;
  }