Blame view

fs/posix_acl.c 9.21 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   * linux/fs/posix_acl.c
   *
   *  Copyright (C) 2002 by Andreas Gruenbacher <a.gruenbacher@computer.org>
   *
   *  Fixes from William Schumacher incorporated on 15 March 2001.
   *     (Reported by Charles Bertsch, <CBertsch@microtest.com>).
   */
  
  /*
   *  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...
17
  #include <linux/atomic.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
  #include <linux/fs.h>
  #include <linux/sched.h>
  #include <linux/posix_acl.h>
630d9c472   Paul Gortmaker   fs: reduce the us...
21
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
  
  #include <linux/errno.h>
f61f6da0d   Chuck Lever   NFS: Prevent memo...
24
  EXPORT_SYMBOL(posix_acl_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
  EXPORT_SYMBOL(posix_acl_alloc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
  EXPORT_SYMBOL(posix_acl_valid);
  EXPORT_SYMBOL(posix_acl_equiv_mode);
  EXPORT_SYMBOL(posix_acl_from_mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
  
  /*
f61f6da0d   Chuck Lever   NFS: Prevent memo...
31
32
33
34
35
36
37
38
39
40
   * 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;
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
   * Allocate a new ACL with the specified number of entries.
   */
  struct posix_acl *
dd0fc66fb   Al Viro   [PATCH] gfp flags...
44
  posix_acl_alloc(int count, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
  {
  	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...
49
50
  	if (acl)
  		posix_acl_init(acl, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
55
56
  	return acl;
  }
  
  /*
   * Clone an ACL.
   */
edde854e8   Al Viro   bury posix_acl_.....
57
  static struct posix_acl *
dd0fc66fb   Al Viro   [PATCH] gfp flags...
58
  posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
63
64
  {
  	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: ...
65
66
  		clone = kmemdup(acl, size, flags);
  		if (clone)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
  			atomic_set(&clone->a_refcount, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
70
71
72
73
74
75
76
77
78
79
  	}
  	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;
2f6f0654a   Eric W. Biederman   userns: Convert v...
80
81
  	kuid_t prev_uid = INVALID_UID;
  	kgid_t prev_gid = INVALID_GID;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
86
87
88
89
  	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
90
91
92
93
94
95
96
97
  					state = ACL_USER;
  					break;
  				}
  				return -EINVAL;
  
  			case ACL_USER:
  				if (state != ACL_USER)
  					return -EINVAL;
2f6f0654a   Eric W. Biederman   userns: Convert v...
98
  				if (!uid_valid(pa->e_uid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
  					return -EINVAL;
2f6f0654a   Eric W. Biederman   userns: Convert v...
100
101
102
103
  				if (uid_valid(prev_uid) &&
  				    uid_lte(pa->e_uid, prev_uid))
  					return -EINVAL;
  				prev_uid = pa->e_uid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
107
108
  				needs_mask = 1;
  				break;
  
  			case ACL_GROUP_OBJ:
  				if (state == ACL_USER) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
113
114
115
116
  					state = ACL_GROUP;
  					break;
  				}
  				return -EINVAL;
  
  			case ACL_GROUP:
  				if (state != ACL_GROUP)
  					return -EINVAL;
2f6f0654a   Eric W. Biederman   userns: Convert v...
117
118
119
120
  				if (!gid_valid(pa->e_gid))
  					return -EINVAL;
  				if (gid_valid(prev_gid) &&
  				    gid_lte(pa->e_gid, prev_gid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
  					return -EINVAL;
2f6f0654a   Eric W. Biederman   userns: Convert v...
122
  				prev_gid = pa->e_gid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  				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;
  }
  
  /*
   * 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_...
154
  posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
  {
  	const struct posix_acl_entry *pa, *pe;
d6952123b   Al Viro   switch posix_acl_...
157
  	umode_t mode = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  	int not_equiv = 0;
  
  	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;
  }
  
  /*
   * Create an ACL representing the file mode permission bits of an inode.
   */
  struct posix_acl *
3a5fba19b   Al Viro   switch posix_acl_...
193
  posix_acl_from_mode(umode_t mode, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
196
197
198
199
  {
  	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
200
201
202
  	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
203
204
205
  	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
206
207
208
209
210
211
212
213
214
215
216
217
218
  	acl->a_entries[2].e_perm = (mode & S_IRWXO);
  	return acl;
  }
  
  /*
   * 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...
219
  	want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
  	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...
224
  				if (uid_eq(inode->i_uid, current_fsuid()))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
226
227
                                          goto check_perm;
                                  break;
                          case ACL_USER:
2f6f0654a   Eric W. Biederman   userns: Convert v...
228
  				if (uid_eq(pa->e_uid, current_fsuid()))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
233
234
235
236
237
238
                                          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...
239
  				if (in_group_p(pa->e_gid)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
241
242
243
244
245
246
247
248
249
250
251
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
280
  					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_...
281
  static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282
283
284
  {
  	struct posix_acl_entry *pa, *pe;
  	struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
d3fb61207   Al Viro   switch posix_acl_...
285
  	umode_t mode = *mode_p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286
287
288
289
290
291
292
293
294
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
  	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.
   */
86bc704db   Al Viro   switch posix_acl_...
338
  static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
375
376
377
378
379
380
381
  {
  	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 ...
382
383
  
  int
d3fb61207   Al Viro   switch posix_acl_...
384
  posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
826cae2f2   Al Viro   kill boilerplates...
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  {
  	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;
  }
  EXPORT_SYMBOL(posix_acl_create);
  
  int
86bc704db   Al Viro   switch posix_acl_...
402
  posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
bc26ab5f6   Al Viro   kill boilerplate ...
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
  {
  	struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  	int err = -ENOMEM;
  	if (clone) {
  		err = posix_acl_chmod_masq(clone, mode);
  		if (err) {
  			posix_acl_release(clone);
  			clone = NULL;
  		}
  	}
  	posix_acl_release(*acl);
  	*acl = clone;
  	return err;
  }
  EXPORT_SYMBOL(posix_acl_chmod);