blob: 48c5075da6c2e56cd80c73fbde2f6fbf5390e0df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/posix_acl.c
3 *
4 * Copyright (C) 2002 by Andreas Gruenbacher <a.gruenbacher@computer.org>
5 *
6 * Fixes from William Schumacher incorporated on 15 March 2001.
7 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
8 */
9
10/*
11 * This file contains generic functions for manipulating
12 * POSIX 1003.1e draft standard 17 ACLs.
13 */
14
15#include <linux/kernel.h>
16#include <linux/slab.h>
Arun Sharma60063492011-07-26 16:09:06 -070017#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/fs.h>
19#include <linux/sched.h>
20#include <linux/posix_acl.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050021#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <linux/errno.h>
24
Chuck Leverf61f6da2011-01-21 03:05:38 +000025EXPORT_SYMBOL(posix_acl_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026EXPORT_SYMBOL(posix_acl_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027EXPORT_SYMBOL(posix_acl_valid);
28EXPORT_SYMBOL(posix_acl_equiv_mode);
29EXPORT_SYMBOL(posix_acl_from_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
Chuck Leverf61f6da2011-01-21 03:05:38 +000032 * Init a fresh posix_acl
33 */
34void
35posix_acl_init(struct posix_acl *acl, int count)
36{
37 atomic_set(&acl->a_refcount, 1);
38 acl->a_count = count;
39}
40
41/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * Allocate a new ACL with the specified number of entries.
43 */
44struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +010045posix_acl_alloc(int count, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 const size_t size = sizeof(struct posix_acl) +
48 count * sizeof(struct posix_acl_entry);
49 struct posix_acl *acl = kmalloc(size, flags);
Chuck Leverf61f6da2011-01-21 03:05:38 +000050 if (acl)
51 posix_acl_init(acl, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 return acl;
53}
54
55/*
56 * Clone an ACL.
57 */
Al Viroedde8542011-07-23 03:27:37 -040058static struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +010059posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 struct posix_acl *clone = NULL;
62
63 if (acl) {
64 int size = sizeof(struct posix_acl) + acl->a_count *
65 sizeof(struct posix_acl_entry);
Alexey Dobriyan52978be2006-09-30 23:27:21 -070066 clone = kmemdup(acl, size, flags);
67 if (clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 atomic_set(&clone->a_refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70 return clone;
71}
72
73/*
74 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
75 */
76int
77posix_acl_valid(const struct posix_acl *acl)
78{
79 const struct posix_acl_entry *pa, *pe;
80 int state = ACL_USER_OBJ;
81 unsigned int id = 0; /* keep gcc happy */
82 int needs_mask = 0;
83
84 FOREACH_ACL_ENTRY(pa, acl, pe) {
85 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
86 return -EINVAL;
87 switch (pa->e_tag) {
88 case ACL_USER_OBJ:
89 if (state == ACL_USER_OBJ) {
90 id = 0;
91 state = ACL_USER;
92 break;
93 }
94 return -EINVAL;
95
96 case ACL_USER:
97 if (state != ACL_USER)
98 return -EINVAL;
99 if (pa->e_id == ACL_UNDEFINED_ID ||
100 pa->e_id < id)
101 return -EINVAL;
102 id = pa->e_id + 1;
103 needs_mask = 1;
104 break;
105
106 case ACL_GROUP_OBJ:
107 if (state == ACL_USER) {
108 id = 0;
109 state = ACL_GROUP;
110 break;
111 }
112 return -EINVAL;
113
114 case ACL_GROUP:
115 if (state != ACL_GROUP)
116 return -EINVAL;
117 if (pa->e_id == ACL_UNDEFINED_ID ||
118 pa->e_id < id)
119 return -EINVAL;
120 id = pa->e_id + 1;
121 needs_mask = 1;
122 break;
123
124 case ACL_MASK:
125 if (state != ACL_GROUP)
126 return -EINVAL;
127 state = ACL_OTHER;
128 break;
129
130 case ACL_OTHER:
131 if (state == ACL_OTHER ||
132 (state == ACL_GROUP && !needs_mask)) {
133 state = 0;
134 break;
135 }
136 return -EINVAL;
137
138 default:
139 return -EINVAL;
140 }
141 }
142 if (state == 0)
143 return 0;
144 return -EINVAL;
145}
146
147/*
148 * Returns 0 if the acl can be exactly represented in the traditional
149 * file mode permission bits, or else 1. Returns -E... on error.
150 */
151int
Al Virod6952122011-07-23 18:56:36 -0400152posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 const struct posix_acl_entry *pa, *pe;
Al Virod6952122011-07-23 18:56:36 -0400155 umode_t mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 int not_equiv = 0;
157
158 FOREACH_ACL_ENTRY(pa, acl, pe) {
159 switch (pa->e_tag) {
160 case ACL_USER_OBJ:
161 mode |= (pa->e_perm & S_IRWXO) << 6;
162 break;
163 case ACL_GROUP_OBJ:
164 mode |= (pa->e_perm & S_IRWXO) << 3;
165 break;
166 case ACL_OTHER:
167 mode |= pa->e_perm & S_IRWXO;
168 break;
169 case ACL_MASK:
170 mode = (mode & ~S_IRWXG) |
171 ((pa->e_perm & S_IRWXO) << 3);
172 not_equiv = 1;
173 break;
174 case ACL_USER:
175 case ACL_GROUP:
176 not_equiv = 1;
177 break;
178 default:
179 return -EINVAL;
180 }
181 }
182 if (mode_p)
183 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
184 return not_equiv;
185}
186
187/*
188 * Create an ACL representing the file mode permission bits of an inode.
189 */
190struct posix_acl *
Al Viro3a5fba12011-07-23 19:01:48 -0400191posix_acl_from_mode(umode_t mode, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct posix_acl *acl = posix_acl_alloc(3, flags);
194 if (!acl)
195 return ERR_PTR(-ENOMEM);
196
197 acl->a_entries[0].e_tag = ACL_USER_OBJ;
198 acl->a_entries[0].e_id = ACL_UNDEFINED_ID;
199 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
200
201 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
202 acl->a_entries[1].e_id = ACL_UNDEFINED_ID;
203 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
204
205 acl->a_entries[2].e_tag = ACL_OTHER;
206 acl->a_entries[2].e_id = ACL_UNDEFINED_ID;
207 acl->a_entries[2].e_perm = (mode & S_IRWXO);
208 return acl;
209}
210
211/*
212 * Return 0 if current is granted want access to the inode
213 * by the acl. Returns -E... otherwise.
214 */
215int
216posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
217{
218 const struct posix_acl_entry *pa, *pe, *mask_obj;
219 int found = 0;
220
Andreas Gruenbacherd124b602011-10-23 23:13:32 +0530221 want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 FOREACH_ACL_ENTRY(pa, acl, pe) {
224 switch(pa->e_tag) {
225 case ACL_USER_OBJ:
226 /* (May have been checked already) */
David Howellsda9592e2008-11-14 10:39:05 +1100227 if (inode->i_uid == current_fsuid())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto check_perm;
229 break;
230 case ACL_USER:
David Howellsda9592e2008-11-14 10:39:05 +1100231 if (pa->e_id == current_fsuid())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto mask;
233 break;
234 case ACL_GROUP_OBJ:
235 if (in_group_p(inode->i_gid)) {
236 found = 1;
237 if ((pa->e_perm & want) == want)
238 goto mask;
239 }
240 break;
241 case ACL_GROUP:
242 if (in_group_p(pa->e_id)) {
243 found = 1;
244 if ((pa->e_perm & want) == want)
245 goto mask;
246 }
247 break;
248 case ACL_MASK:
249 break;
250 case ACL_OTHER:
251 if (found)
252 return -EACCES;
253 else
254 goto check_perm;
255 default:
256 return -EIO;
257 }
258 }
259 return -EIO;
260
261mask:
262 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
263 if (mask_obj->e_tag == ACL_MASK) {
264 if ((pa->e_perm & mask_obj->e_perm & want) == want)
265 return 0;
266 return -EACCES;
267 }
268 }
269
270check_perm:
271 if ((pa->e_perm & want) == want)
272 return 0;
273 return -EACCES;
274}
275
276/*
277 * Modify acl when creating a new inode. The caller must ensure the acl is
278 * only referenced once.
279 *
280 * mode_p initially must contain the mode parameter to the open() / creat()
281 * system calls. All permissions that are not granted by the acl are removed.
282 * The permissions in the acl are changed to reflect the mode_p parameter.
283 */
Al Virod3fb6122011-07-23 18:37:50 -0400284static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct posix_acl_entry *pa, *pe;
287 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
Al Virod3fb6122011-07-23 18:37:50 -0400288 umode_t mode = *mode_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int not_equiv = 0;
290
291 /* assert(atomic_read(acl->a_refcount) == 1); */
292
293 FOREACH_ACL_ENTRY(pa, acl, pe) {
294 switch(pa->e_tag) {
295 case ACL_USER_OBJ:
296 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
297 mode &= (pa->e_perm << 6) | ~S_IRWXU;
298 break;
299
300 case ACL_USER:
301 case ACL_GROUP:
302 not_equiv = 1;
303 break;
304
305 case ACL_GROUP_OBJ:
306 group_obj = pa;
307 break;
308
309 case ACL_OTHER:
310 pa->e_perm &= mode | ~S_IRWXO;
311 mode &= pa->e_perm | ~S_IRWXO;
312 break;
313
314 case ACL_MASK:
315 mask_obj = pa;
316 not_equiv = 1;
317 break;
318
319 default:
320 return -EIO;
321 }
322 }
323
324 if (mask_obj) {
325 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
326 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
327 } else {
328 if (!group_obj)
329 return -EIO;
330 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
331 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
332 }
333
334 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
335 return not_equiv;
336}
337
Jan Kara9451fcb2016-09-19 17:39:09 +0200338/**
339 * posix_acl_update_mode - update mode in set_acl
340 *
341 * Update the file mode when setting an ACL: compute the new file permission
342 * bits based on the ACL. In addition, if the ACL is equivalent to the new
343 * file mode, set *acl to NULL to indicate that no ACL should be set.
344 *
345 * As with chmod, clear the setgit bit if the caller is not in the owning group
346 * or capable of CAP_FSETID (see inode_change_ok).
347 *
348 * Called from set_acl inode operations.
349 */
350int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
351 struct posix_acl **acl)
352{
353 umode_t mode = inode->i_mode;
354 int error;
355
356 error = posix_acl_equiv_mode(*acl, &mode);
357 if (error < 0)
358 return error;
359 if (error == 0)
360 *acl = NULL;
361 if (!in_group_p(inode->i_gid) &&
362 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
363 mode &= ~S_ISGID;
364 *mode_p = mode;
365 return 0;
366}
367EXPORT_SYMBOL(posix_acl_update_mode);
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/*
370 * Modify the ACL for the chmod syscall.
371 */
Al Viro86bc7042011-07-23 19:03:11 -0400372static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
375 struct posix_acl_entry *pa, *pe;
376
377 /* assert(atomic_read(acl->a_refcount) == 1); */
378
379 FOREACH_ACL_ENTRY(pa, acl, pe) {
380 switch(pa->e_tag) {
381 case ACL_USER_OBJ:
382 pa->e_perm = (mode & S_IRWXU) >> 6;
383 break;
384
385 case ACL_USER:
386 case ACL_GROUP:
387 break;
388
389 case ACL_GROUP_OBJ:
390 group_obj = pa;
391 break;
392
393 case ACL_MASK:
394 mask_obj = pa;
395 break;
396
397 case ACL_OTHER:
398 pa->e_perm = (mode & S_IRWXO);
399 break;
400
401 default:
402 return -EIO;
403 }
404 }
405
406 if (mask_obj) {
407 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
408 } else {
409 if (!group_obj)
410 return -EIO;
411 group_obj->e_perm = (mode & S_IRWXG) >> 3;
412 }
413
414 return 0;
415}
Al Virobc26ab52011-07-23 00:18:02 -0400416
417int
Al Virod3fb6122011-07-23 18:37:50 -0400418posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
Al Viro826cae22011-07-23 03:10:32 -0400419{
420 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
421 int err = -ENOMEM;
422 if (clone) {
423 err = posix_acl_create_masq(clone, mode_p);
424 if (err < 0) {
425 posix_acl_release(clone);
426 clone = NULL;
427 }
428 }
429 posix_acl_release(*acl);
430 *acl = clone;
431 return err;
432}
433EXPORT_SYMBOL(posix_acl_create);
434
435int
Al Viro86bc7042011-07-23 19:03:11 -0400436posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
Al Virobc26ab52011-07-23 00:18:02 -0400437{
438 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
439 int err = -ENOMEM;
440 if (clone) {
441 err = posix_acl_chmod_masq(clone, mode);
442 if (err) {
443 posix_acl_release(clone);
444 clone = NULL;
445 }
446 }
447 posix_acl_release(*acl);
448 *acl = clone;
449 return err;
450}
451EXPORT_SYMBOL(posix_acl_chmod);