blob: 3542f1f814e2a966d33429f61e9f78ab5404a18c [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 Sharma600634972011-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;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -080081 kuid_t prev_uid = INVALID_UID;
82 kgid_t prev_gid = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int needs_mask = 0;
84
85 FOREACH_ACL_ENTRY(pa, acl, pe) {
86 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
87 return -EINVAL;
88 switch (pa->e_tag) {
89 case ACL_USER_OBJ:
90 if (state == ACL_USER_OBJ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 state = ACL_USER;
92 break;
93 }
94 return -EINVAL;
95
96 case ACL_USER:
97 if (state != ACL_USER)
98 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -080099 if (!uid_valid(pa->e_uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800101 if (uid_valid(prev_uid) &&
102 uid_lte(pa->e_uid, prev_uid))
103 return -EINVAL;
104 prev_uid = pa->e_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 needs_mask = 1;
106 break;
107
108 case ACL_GROUP_OBJ:
109 if (state == ACL_USER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 state = ACL_GROUP;
111 break;
112 }
113 return -EINVAL;
114
115 case ACL_GROUP:
116 if (state != ACL_GROUP)
117 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800118 if (!gid_valid(pa->e_gid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800120 if (gid_valid(prev_gid) &&
121 gid_lte(pa->e_gid, prev_gid))
122 return -EINVAL;
123 prev_gid = pa->e_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 needs_mask = 1;
125 break;
126
127 case ACL_MASK:
128 if (state != ACL_GROUP)
129 return -EINVAL;
130 state = ACL_OTHER;
131 break;
132
133 case ACL_OTHER:
134 if (state == ACL_OTHER ||
135 (state == ACL_GROUP && !needs_mask)) {
136 state = 0;
137 break;
138 }
139 return -EINVAL;
140
141 default:
142 return -EINVAL;
143 }
144 }
145 if (state == 0)
146 return 0;
147 return -EINVAL;
148}
149
150/*
151 * Returns 0 if the acl can be exactly represented in the traditional
152 * file mode permission bits, or else 1. Returns -E... on error.
153 */
154int
Al Virod6952122011-07-23 18:56:36 -0400155posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 const struct posix_acl_entry *pa, *pe;
Al Virod6952122011-07-23 18:56:36 -0400158 umode_t mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int not_equiv = 0;
160
Christoph Hellwig3d3da402014-05-04 13:03:32 +0200161 /*
162 * A null ACL can always be presented as mode bits.
163 */
164 if (!acl)
165 return 0;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 FOREACH_ACL_ENTRY(pa, acl, pe) {
168 switch (pa->e_tag) {
169 case ACL_USER_OBJ:
170 mode |= (pa->e_perm & S_IRWXO) << 6;
171 break;
172 case ACL_GROUP_OBJ:
173 mode |= (pa->e_perm & S_IRWXO) << 3;
174 break;
175 case ACL_OTHER:
176 mode |= pa->e_perm & S_IRWXO;
177 break;
178 case ACL_MASK:
179 mode = (mode & ~S_IRWXG) |
180 ((pa->e_perm & S_IRWXO) << 3);
181 not_equiv = 1;
182 break;
183 case ACL_USER:
184 case ACL_GROUP:
185 not_equiv = 1;
186 break;
187 default:
188 return -EINVAL;
189 }
190 }
191 if (mode_p)
192 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
193 return not_equiv;
194}
195
196/*
197 * Create an ACL representing the file mode permission bits of an inode.
198 */
199struct posix_acl *
Al Viro3a5fba12011-07-23 19:01:48 -0400200posix_acl_from_mode(umode_t mode, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct posix_acl *acl = posix_acl_alloc(3, flags);
203 if (!acl)
204 return ERR_PTR(-ENOMEM);
205
206 acl->a_entries[0].e_tag = ACL_USER_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
208
209 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
211
212 acl->a_entries[2].e_tag = ACL_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 acl->a_entries[2].e_perm = (mode & S_IRWXO);
214 return acl;
215}
216
217/*
218 * Return 0 if current is granted want access to the inode
219 * by the acl. Returns -E... otherwise.
220 */
221int
222posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
223{
224 const struct posix_acl_entry *pa, *pe, *mask_obj;
225 int found = 0;
226
Andreas Gruenbacherd124b602011-10-23 23:13:32 +0530227 want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 FOREACH_ACL_ENTRY(pa, acl, pe) {
230 switch(pa->e_tag) {
231 case ACL_USER_OBJ:
232 /* (May have been checked already) */
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800233 if (uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto check_perm;
235 break;
236 case ACL_USER:
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800237 if (uid_eq(pa->e_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto mask;
239 break;
240 case ACL_GROUP_OBJ:
241 if (in_group_p(inode->i_gid)) {
242 found = 1;
243 if ((pa->e_perm & want) == want)
244 goto mask;
245 }
246 break;
247 case ACL_GROUP:
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800248 if (in_group_p(pa->e_gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 found = 1;
250 if ((pa->e_perm & want) == want)
251 goto mask;
252 }
253 break;
254 case ACL_MASK:
255 break;
256 case ACL_OTHER:
257 if (found)
258 return -EACCES;
259 else
260 goto check_perm;
261 default:
262 return -EIO;
263 }
264 }
265 return -EIO;
266
267mask:
268 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
269 if (mask_obj->e_tag == ACL_MASK) {
270 if ((pa->e_perm & mask_obj->e_perm & want) == want)
271 return 0;
272 return -EACCES;
273 }
274 }
275
276check_perm:
277 if ((pa->e_perm & want) == want)
278 return 0;
279 return -EACCES;
280}
281
282/*
283 * Modify acl when creating a new inode. The caller must ensure the acl is
284 * only referenced once.
285 *
286 * mode_p initially must contain the mode parameter to the open() / creat()
287 * system calls. All permissions that are not granted by the acl are removed.
288 * The permissions in the acl are changed to reflect the mode_p parameter.
289 */
Al Virod3fb6122011-07-23 18:37:50 -0400290static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct posix_acl_entry *pa, *pe;
293 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
Al Virod3fb6122011-07-23 18:37:50 -0400294 umode_t mode = *mode_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int not_equiv = 0;
296
297 /* assert(atomic_read(acl->a_refcount) == 1); */
298
299 FOREACH_ACL_ENTRY(pa, acl, pe) {
300 switch(pa->e_tag) {
301 case ACL_USER_OBJ:
302 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
303 mode &= (pa->e_perm << 6) | ~S_IRWXU;
304 break;
305
306 case ACL_USER:
307 case ACL_GROUP:
308 not_equiv = 1;
309 break;
310
311 case ACL_GROUP_OBJ:
312 group_obj = pa;
313 break;
314
315 case ACL_OTHER:
316 pa->e_perm &= mode | ~S_IRWXO;
317 mode &= pa->e_perm | ~S_IRWXO;
318 break;
319
320 case ACL_MASK:
321 mask_obj = pa;
322 not_equiv = 1;
323 break;
324
325 default:
326 return -EIO;
327 }
328 }
329
330 if (mask_obj) {
331 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
332 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
333 } else {
334 if (!group_obj)
335 return -EIO;
336 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
337 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
338 }
339
340 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
341 return not_equiv;
342}
343
344/*
345 * Modify the ACL for the chmod syscall.
346 */
Al Viro86bc7042011-07-23 19:03:11 -0400347static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
350 struct posix_acl_entry *pa, *pe;
351
352 /* assert(atomic_read(acl->a_refcount) == 1); */
353
354 FOREACH_ACL_ENTRY(pa, acl, pe) {
355 switch(pa->e_tag) {
356 case ACL_USER_OBJ:
357 pa->e_perm = (mode & S_IRWXU) >> 6;
358 break;
359
360 case ACL_USER:
361 case ACL_GROUP:
362 break;
363
364 case ACL_GROUP_OBJ:
365 group_obj = pa;
366 break;
367
368 case ACL_MASK:
369 mask_obj = pa;
370 break;
371
372 case ACL_OTHER:
373 pa->e_perm = (mode & S_IRWXO);
374 break;
375
376 default:
377 return -EIO;
378 }
379 }
380
381 if (mask_obj) {
382 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
383 } else {
384 if (!group_obj)
385 return -EIO;
386 group_obj->e_perm = (mode & S_IRWXG) >> 3;
387 }
388
389 return 0;
390}
Al Virobc26ab52011-07-23 00:18:02 -0400391
392int
Al Virod3fb6122011-07-23 18:37:50 -0400393posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
Al Viro826cae22011-07-23 03:10:32 -0400394{
395 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
396 int err = -ENOMEM;
397 if (clone) {
398 err = posix_acl_create_masq(clone, mode_p);
399 if (err < 0) {
400 posix_acl_release(clone);
401 clone = NULL;
402 }
403 }
404 posix_acl_release(*acl);
405 *acl = clone;
406 return err;
407}
408EXPORT_SYMBOL(posix_acl_create);
409
410int
Al Viro86bc7042011-07-23 19:03:11 -0400411posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
Al Virobc26ab52011-07-23 00:18:02 -0400412{
413 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
414 int err = -ENOMEM;
415 if (clone) {
416 err = posix_acl_chmod_masq(clone, mode);
417 if (err) {
418 posix_acl_release(clone);
419 clone = NULL;
420 }
421 }
422 posix_acl_release(*acl);
423 *acl = clone;
424 return err;
425}
426EXPORT_SYMBOL(posix_acl_chmod);