blob: 3e4648ad9cfcbda429ed52587d42a1d989fb21f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2001-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_inum.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dinode.h"
31#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_attr.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100035#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080037#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/posix_acl_xattr.h>
39
Nathan Scott67fcaa72006-06-09 17:00:52 +100040STATIC int xfs_acl_setmode(bhv_vnode_t *, xfs_acl_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041STATIC void xfs_acl_filter_mode(mode_t, xfs_acl_t *);
42STATIC void xfs_acl_get_endian(xfs_acl_t *);
43STATIC int xfs_acl_access(uid_t, gid_t, xfs_acl_t *, mode_t, cred_t *);
44STATIC int xfs_acl_invalid(xfs_acl_t *);
45STATIC void xfs_acl_sync_mode(mode_t, xfs_acl_t *);
Nathan Scott67fcaa72006-06-09 17:00:52 +100046STATIC void xfs_acl_get_attr(bhv_vnode_t *, xfs_acl_t *, int, int, int *);
47STATIC void xfs_acl_set_attr(bhv_vnode_t *, xfs_acl_t *, int, int *);
48STATIC int xfs_acl_allow_set(bhv_vnode_t *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50kmem_zone_t *xfs_acl_zone;
51
52
53/*
54 * Test for existence of access ACL attribute as efficiently as possible.
55 */
56int
57xfs_acl_vhasacl_access(
Nathan Scott67fcaa72006-06-09 17:00:52 +100058 bhv_vnode_t *vp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 int error;
61
62 xfs_acl_get_attr(vp, NULL, _ACL_TYPE_ACCESS, ATTR_KERNOVAL, &error);
63 return (error == 0);
64}
65
66/*
67 * Test for existence of default ACL attribute as efficiently as possible.
68 */
69int
70xfs_acl_vhasacl_default(
Nathan Scott67fcaa72006-06-09 17:00:52 +100071 bhv_vnode_t *vp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 int error;
74
Christoph Hellwig42173f62008-04-22 17:33:25 +100075 if (!S_ISDIR(vp->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 0;
77 xfs_acl_get_attr(vp, NULL, _ACL_TYPE_DEFAULT, ATTR_KERNOVAL, &error);
78 return (error == 0);
79}
80
81/*
82 * Convert from extended attribute representation to in-memory for XFS.
83 */
84STATIC int
85posix_acl_xattr_to_xfs(
86 posix_acl_xattr_header *src,
87 size_t size,
88 xfs_acl_t *dest)
89{
90 posix_acl_xattr_entry *src_entry;
91 xfs_acl_entry_t *dest_entry;
92 int n;
93
94 if (!src || !dest)
95 return EINVAL;
96
97 if (size < sizeof(posix_acl_xattr_header))
98 return EINVAL;
99
100 if (src->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
101 return EOPNOTSUPP;
102
103 memset(dest, 0, sizeof(xfs_acl_t));
104 dest->acl_cnt = posix_acl_xattr_count(size);
105 if (dest->acl_cnt < 0 || dest->acl_cnt > XFS_ACL_MAX_ENTRIES)
106 return EINVAL;
107
108 /*
109 * acl_set_file(3) may request that we set default ACLs with
110 * zero length -- defend (gracefully) against that here.
111 */
112 if (!dest->acl_cnt)
113 return 0;
114
115 src_entry = (posix_acl_xattr_entry *)((char *)src + sizeof(*src));
116 dest_entry = &dest->acl_entry[0];
117
118 for (n = 0; n < dest->acl_cnt; n++, src_entry++, dest_entry++) {
119 dest_entry->ae_perm = le16_to_cpu(src_entry->e_perm);
120 if (_ACL_PERM_INVALID(dest_entry->ae_perm))
121 return EINVAL;
122 dest_entry->ae_tag = le16_to_cpu(src_entry->e_tag);
123 switch(dest_entry->ae_tag) {
124 case ACL_USER:
125 case ACL_GROUP:
126 dest_entry->ae_id = le32_to_cpu(src_entry->e_id);
127 break;
128 case ACL_USER_OBJ:
129 case ACL_GROUP_OBJ:
130 case ACL_MASK:
131 case ACL_OTHER:
132 dest_entry->ae_id = ACL_UNDEFINED_ID;
133 break;
134 default:
135 return EINVAL;
136 }
137 }
138 if (xfs_acl_invalid(dest))
139 return EINVAL;
140
141 return 0;
142}
143
144/*
Nathan Scott380b5dc2005-11-02 11:43:18 +1100145 * Comparison function called from xfs_sort().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * Primary key is ae_tag, secondary key is ae_id.
147 */
148STATIC int
149xfs_acl_entry_compare(
150 const void *va,
151 const void *vb)
152{
153 xfs_acl_entry_t *a = (xfs_acl_entry_t *)va,
154 *b = (xfs_acl_entry_t *)vb;
155
156 if (a->ae_tag == b->ae_tag)
157 return (a->ae_id - b->ae_id);
158 return (a->ae_tag - b->ae_tag);
159}
160
161/*
162 * Convert from in-memory XFS to extended attribute representation.
163 */
164STATIC int
165posix_acl_xfs_to_xattr(
166 xfs_acl_t *src,
167 posix_acl_xattr_header *dest,
168 size_t size)
169{
170 int n;
171 size_t new_size = posix_acl_xattr_size(src->acl_cnt);
172 posix_acl_xattr_entry *dest_entry;
173 xfs_acl_entry_t *src_entry;
174
175 if (size < new_size)
176 return -ERANGE;
177
178 /* Need to sort src XFS ACL by <ae_tag,ae_id> */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100179 xfs_sort(src->acl_entry, src->acl_cnt, sizeof(src->acl_entry[0]),
180 xfs_acl_entry_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 dest->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
183 dest_entry = &dest->a_entries[0];
184 src_entry = &src->acl_entry[0];
185 for (n = 0; n < src->acl_cnt; n++, dest_entry++, src_entry++) {
186 dest_entry->e_perm = cpu_to_le16(src_entry->ae_perm);
187 if (_ACL_PERM_INVALID(src_entry->ae_perm))
188 return -EINVAL;
189 dest_entry->e_tag = cpu_to_le16(src_entry->ae_tag);
190 switch (src_entry->ae_tag) {
191 case ACL_USER:
192 case ACL_GROUP:
193 dest_entry->e_id = cpu_to_le32(src_entry->ae_id);
194 break;
195 case ACL_USER_OBJ:
196 case ACL_GROUP_OBJ:
197 case ACL_MASK:
198 case ACL_OTHER:
199 dest_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
200 break;
201 default:
202 return -EINVAL;
203 }
204 }
205 return new_size;
206}
207
208int
209xfs_acl_vget(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000210 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 void *acl,
212 size_t size,
213 int kind)
214{
215 int error;
216 xfs_acl_t *xfs_acl = NULL;
217 posix_acl_xattr_header *ext_acl = acl;
218 int flags = 0;
219
220 VN_HOLD(vp);
221 if(size) {
222 if (!(_ACL_ALLOC(xfs_acl))) {
223 error = ENOMEM;
224 goto out;
225 }
226 memset(xfs_acl, 0, sizeof(xfs_acl_t));
227 } else
228 flags = ATTR_KERNOVAL;
229
230 xfs_acl_get_attr(vp, xfs_acl, kind, flags, &error);
231 if (error)
232 goto out;
233
234 if (!size) {
235 error = -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES);
236 } else {
237 if (xfs_acl_invalid(xfs_acl)) {
238 error = EINVAL;
239 goto out;
240 }
Christoph Hellwig4e5dbb32008-04-22 17:33:33 +1000241 if (kind == _ACL_TYPE_ACCESS)
242 xfs_acl_sync_mode(xfs_vtoi(vp)->i_d.di_mode, xfs_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 error = -posix_acl_xfs_to_xattr(xfs_acl, ext_acl, size);
244 }
245out:
246 VN_RELE(vp);
247 if(xfs_acl)
248 _ACL_FREE(xfs_acl);
249 return -error;
250}
251
252int
253xfs_acl_vremove(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000254 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int kind)
256{
257 int error;
258
259 VN_HOLD(vp);
260 error = xfs_acl_allow_set(vp, kind);
261 if (!error) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000262 error = xfs_attr_remove(xfs_vtoi(vp),
263 kind == _ACL_TYPE_DEFAULT?
Nathan Scott67fcaa72006-06-09 17:00:52 +1000264 SGI_ACL_DEFAULT: SGI_ACL_FILE,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000265 ATTR_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (error == ENOATTR)
267 error = 0; /* 'scool */
268 }
269 VN_RELE(vp);
270 return -error;
271}
272
273int
274xfs_acl_vset(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000275 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 void *acl,
277 size_t size,
278 int kind)
279{
280 posix_acl_xattr_header *ext_acl = acl;
281 xfs_acl_t *xfs_acl;
282 int error;
283 int basicperms = 0; /* more than std unix perms? */
284
285 if (!acl)
286 return -EINVAL;
287
288 if (!(_ACL_ALLOC(xfs_acl)))
289 return -ENOMEM;
290
291 error = posix_acl_xattr_to_xfs(ext_acl, size, xfs_acl);
292 if (error) {
293 _ACL_FREE(xfs_acl);
294 return -error;
295 }
296 if (!xfs_acl->acl_cnt) {
297 _ACL_FREE(xfs_acl);
298 return 0;
299 }
300
301 VN_HOLD(vp);
302 error = xfs_acl_allow_set(vp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* Incoming ACL exists, set file mode based on its value */
David Chinner0c928292008-04-10 12:20:58 +1000305 if (!error && kind == _ACL_TYPE_ACCESS)
306 error = xfs_acl_setmode(vp, xfs_acl, &basicperms);
307
308 if (error)
309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /*
312 * If we have more than std unix permissions, set up the actual attr.
313 * Otherwise, delete any existing attr. This prevents us from
314 * having actual attrs for permissions that can be stored in the
315 * standard permission bits.
316 */
317 if (!basicperms) {
318 xfs_acl_set_attr(vp, xfs_acl, kind, &error);
319 } else {
David Chinner5ca1f262008-04-10 12:21:04 +1000320 error = -xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323out:
324 VN_RELE(vp);
325 _ACL_FREE(xfs_acl);
326 return -error;
327}
328
329int
330xfs_acl_iaccess(
331 xfs_inode_t *ip,
332 mode_t mode,
333 cred_t *cr)
334{
335 xfs_acl_t *acl;
336 int rval;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000337 struct xfs_name acl_name = {SGI_ACL_FILE, SGI_ACL_FILE_SIZE};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (!(_ACL_ALLOC(acl)))
340 return -1;
341
342 /* If the file has no ACL return -1. */
343 rval = sizeof(xfs_acl_t);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000344 if (xfs_attr_fetch(ip, &acl_name, (char *)acl, &rval, ATTR_ROOT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 _ACL_FREE(acl);
346 return -1;
347 }
348 xfs_acl_get_endian(acl);
349
350 /* If the file has an empty ACL return -1. */
351 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT) {
352 _ACL_FREE(acl);
353 return -1;
354 }
355
356 /* Synchronize ACL with mode bits */
357 xfs_acl_sync_mode(ip->i_d.di_mode, acl);
358
359 rval = xfs_acl_access(ip->i_d.di_uid, ip->i_d.di_gid, acl, mode, cr);
360 _ACL_FREE(acl);
361 return rval;
362}
363
364STATIC int
365xfs_acl_allow_set(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000366 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int kind)
368{
Christoph Hellwig0a74cd12007-08-29 11:53:12 +1000369 if (vp->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return EPERM;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000371 if (kind == _ACL_TYPE_DEFAULT && !S_ISDIR(vp->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return ENOTDIR;
Christoph Hellwig0a74cd12007-08-29 11:53:12 +1000373 if (vp->i_sb->s_flags & MS_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return EROFS;
Christoph Hellwig4e5dbb32008-04-22 17:33:33 +1000375 if (xfs_vtoi(vp)->i_d.di_uid != current->fsuid && !capable(CAP_FOWNER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return EPERM;
Christoph Hellwig4e5dbb32008-04-22 17:33:33 +1000377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * Note: cr is only used here for the capability check if the ACL test fails.
382 * It is not used to find out the credentials uid or groups etc, as was
383 * done in IRIX. It is assumed that the uid and groups for the current
384 * thread are taken from "current" instead of the cr parameter.
385 */
386STATIC int
387xfs_acl_access(
388 uid_t fuid,
389 gid_t fgid,
390 xfs_acl_t *fap,
391 mode_t md,
392 cred_t *cr)
393{
394 xfs_acl_entry_t matched;
395 int i, allows;
396 int maskallows = -1; /* true, but not 1, either */
397 int seen_userobj = 0;
398
399 matched.ae_tag = 0; /* Invalid type */
Christoph Hellwig5bde1ba2005-11-02 15:06:18 +1100400 matched.ae_perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 for (i = 0; i < fap->acl_cnt; i++) {
403 /*
404 * Break out if we've got a user_obj entry or
405 * a user entry and the mask (and have processed USER_OBJ)
406 */
407 if (matched.ae_tag == ACL_USER_OBJ)
408 break;
409 if (matched.ae_tag == ACL_USER) {
410 if (maskallows != -1 && seen_userobj)
411 break;
412 if (fap->acl_entry[i].ae_tag != ACL_MASK &&
413 fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
414 continue;
415 }
416 /* True if this entry allows the requested access */
417 allows = ((fap->acl_entry[i].ae_perm & md) == md);
418
419 switch (fap->acl_entry[i].ae_tag) {
420 case ACL_USER_OBJ:
421 seen_userobj = 1;
422 if (fuid != current->fsuid)
423 continue;
424 matched.ae_tag = ACL_USER_OBJ;
425 matched.ae_perm = allows;
426 break;
427 case ACL_USER:
428 if (fap->acl_entry[i].ae_id != current->fsuid)
429 continue;
430 matched.ae_tag = ACL_USER;
431 matched.ae_perm = allows;
432 break;
433 case ACL_GROUP_OBJ:
434 if ((matched.ae_tag == ACL_GROUP_OBJ ||
435 matched.ae_tag == ACL_GROUP) && !allows)
436 continue;
437 if (!in_group_p(fgid))
438 continue;
439 matched.ae_tag = ACL_GROUP_OBJ;
440 matched.ae_perm = allows;
441 break;
442 case ACL_GROUP:
443 if ((matched.ae_tag == ACL_GROUP_OBJ ||
444 matched.ae_tag == ACL_GROUP) && !allows)
445 continue;
446 if (!in_group_p(fap->acl_entry[i].ae_id))
447 continue;
448 matched.ae_tag = ACL_GROUP;
449 matched.ae_perm = allows;
450 break;
451 case ACL_MASK:
452 maskallows = allows;
453 break;
454 case ACL_OTHER:
455 if (matched.ae_tag != 0)
456 continue;
457 matched.ae_tag = ACL_OTHER;
458 matched.ae_perm = allows;
459 break;
460 }
461 }
462 /*
463 * First possibility is that no matched entry allows access.
464 * The capability to override DAC may exist, so check for it.
465 */
466 switch (matched.ae_tag) {
467 case ACL_OTHER:
468 case ACL_USER_OBJ:
469 if (matched.ae_perm)
470 return 0;
471 break;
472 case ACL_USER:
473 case ACL_GROUP_OBJ:
474 case ACL_GROUP:
475 if (maskallows && matched.ae_perm)
476 return 0;
477 break;
478 case 0:
479 break;
480 }
481
Christoph Hellwig45767582008-02-05 12:13:24 +1100482 /* EACCES tells generic_permission to check for capability overrides */
483 return EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486/*
487 * ACL validity checker.
488 * This acl validation routine checks each ACL entry read in makes sense.
489 */
490STATIC int
491xfs_acl_invalid(
492 xfs_acl_t *aclp)
493{
494 xfs_acl_entry_t *entry, *e;
495 int user = 0, group = 0, other = 0, mask = 0;
496 int mask_required = 0;
497 int i, j;
498
499 if (!aclp)
500 goto acl_invalid;
501
502 if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
503 goto acl_invalid;
504
505 for (i = 0; i < aclp->acl_cnt; i++) {
506 entry = &aclp->acl_entry[i];
507 switch (entry->ae_tag) {
508 case ACL_USER_OBJ:
509 if (user++)
510 goto acl_invalid;
511 break;
512 case ACL_GROUP_OBJ:
513 if (group++)
514 goto acl_invalid;
515 break;
516 case ACL_OTHER:
517 if (other++)
518 goto acl_invalid;
519 break;
520 case ACL_USER:
521 case ACL_GROUP:
522 for (j = i + 1; j < aclp->acl_cnt; j++) {
523 e = &aclp->acl_entry[j];
524 if (e->ae_id == entry->ae_id &&
525 e->ae_tag == entry->ae_tag)
526 goto acl_invalid;
527 }
528 mask_required++;
529 break;
530 case ACL_MASK:
531 if (mask++)
532 goto acl_invalid;
533 break;
534 default:
535 goto acl_invalid;
536 }
537 }
538 if (!user || !group || !other || (mask_required && !mask))
539 goto acl_invalid;
540 else
541 return 0;
542acl_invalid:
543 return EINVAL;
544}
545
546/*
547 * Do ACL endian conversion.
548 */
549STATIC void
550xfs_acl_get_endian(
551 xfs_acl_t *aclp)
552{
553 xfs_acl_entry_t *ace, *end;
554
555 INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
556 end = &aclp->acl_entry[0]+aclp->acl_cnt;
557 for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
558 INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
559 INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
560 INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
561 }
562}
563
564/*
565 * Get the ACL from the EA and do endian conversion.
566 */
567STATIC void
568xfs_acl_get_attr(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000569 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 xfs_acl_t *aclp,
571 int kind,
572 int flags,
573 int *error)
574{
575 int len = sizeof(xfs_acl_t);
576
577 ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
578 flags |= ATTR_ROOT;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000579 *error = xfs_attr_get(xfs_vtoi(vp),
580 kind == _ACL_TYPE_ACCESS ?
Nathan Scott67fcaa72006-06-09 17:00:52 +1000581 SGI_ACL_FILE : SGI_ACL_DEFAULT,
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000582 (char *)aclp, &len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (*error || (flags & ATTR_KERNOVAL))
584 return;
585 xfs_acl_get_endian(aclp);
586}
587
588/*
589 * Set the EA with the ACL and do endian conversion.
590 */
591STATIC void
592xfs_acl_set_attr(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000593 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 xfs_acl_t *aclp,
595 int kind,
596 int *error)
597{
598 xfs_acl_entry_t *ace, *newace, *end;
599 xfs_acl_t *newacl;
600 int len;
601
602 if (!(_ACL_ALLOC(newacl))) {
603 *error = ENOMEM;
604 return;
605 }
606
607 len = sizeof(xfs_acl_t) -
608 (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
609 end = &aclp->acl_entry[0]+aclp->acl_cnt;
610 for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
611 ace < end;
612 ace++, newace++) {
613 INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
614 INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
615 INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
616 }
617 INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000618 *error = xfs_attr_set(xfs_vtoi(vp),
619 kind == _ACL_TYPE_ACCESS ?
Nathan Scott67fcaa72006-06-09 17:00:52 +1000620 SGI_ACL_FILE: SGI_ACL_DEFAULT,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000621 (char *)newacl, len, ATTR_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 _ACL_FREE(newacl);
623}
624
625int
626xfs_acl_vtoacl(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000627 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 xfs_acl_t *access_acl,
629 xfs_acl_t *default_acl)
630{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int error = 0;
632
633 if (access_acl) {
634 /*
635 * Get the Access ACL and the mode. If either cannot
636 * be obtained for some reason, invalidate the access ACL.
637 */
638 xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (error)
640 access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
641 else /* We have a good ACL and the file mode, synchronize. */
Christoph Hellwig4e5dbb32008-04-22 17:33:33 +1000642 xfs_acl_sync_mode(xfs_vtoi(vp)->i_d.di_mode, access_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 if (default_acl) {
646 xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
647 if (error)
648 default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
649 }
650 return error;
651}
652
653/*
654 * This function retrieves the parent directory's acl, processes it
655 * and lets the child inherit the acl(s) that it should.
656 */
657int
658xfs_acl_inherit(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000659 bhv_vnode_t *vp,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000660 mode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 xfs_acl_t *pdaclp)
662{
663 xfs_acl_t *cacl;
664 int error = 0;
665 int basicperms = 0;
666
667 /*
668 * If the parent does not have a default ACL, or it's an
669 * invalid ACL, we're done.
670 */
671 if (!vp)
672 return 0;
673 if (!pdaclp || xfs_acl_invalid(pdaclp))
674 return 0;
675
676 /*
677 * Copy the default ACL of the containing directory to
678 * the access ACL of the new file and use the mode that
679 * was passed in to set up the correct initial values for
680 * the u::,g::[m::], and o:: entries. This is what makes
681 * umask() "work" with ACL's.
682 */
683
684 if (!(_ACL_ALLOC(cacl)))
685 return ENOMEM;
686
687 memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000688 xfs_acl_filter_mode(mode, cacl);
David Chinner0c928292008-04-10 12:20:58 +1000689 error = xfs_acl_setmode(vp, cacl, &basicperms);
690 if (error)
691 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /*
694 * Set the Default and Access ACL on the file. The mode is already
695 * set on the file, so we don't need to worry about that.
696 *
697 * If the new file is a directory, its default ACL is a copy of
698 * the containing directory's default ACL.
699 */
Christoph Hellwig42173f62008-04-22 17:33:25 +1000700 if (S_ISDIR(vp->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
702 if (!error && !basicperms)
703 xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
David Chinner0c928292008-04-10 12:20:58 +1000704out_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 _ACL_FREE(cacl);
706 return error;
707}
708
709/*
710 * Set up the correct mode on the file based on the supplied ACL. This
711 * makes sure that the mode on the file reflects the state of the
712 * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
713 * the ACL is going to get the permissions for these entries, we must
714 * synchronize the mode whenever we set the ACL on a file.
715 */
716STATIC int
717xfs_acl_setmode(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000718 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 xfs_acl_t *acl,
720 int *basicperms)
721{
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000722 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 xfs_acl_entry_t *ap;
724 xfs_acl_entry_t *gap = NULL;
Christoph Hellwig4e5dbb32008-04-22 17:33:33 +1000725 int i, nomask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 *basicperms = 1;
728
729 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
730 return 0;
731
732 /*
733 * Copy the u::, g::, o::, and m:: bits from the ACL into the
734 * mode. The m:: bits take precedence over the g:: bits.
735 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000736 iattr.ia_valid = ATTR_MODE;
737 iattr.ia_mode = xfs_vtoi(vp)->i_d.di_mode;
738 iattr.ia_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ap = acl->acl_entry;
740 for (i = 0; i < acl->acl_cnt; ++i) {
741 switch (ap->ae_tag) {
742 case ACL_USER_OBJ:
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000743 iattr.ia_mode |= ap->ae_perm << 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
745 case ACL_GROUP_OBJ:
746 gap = ap;
747 break;
748 case ACL_MASK: /* more than just standard modes */
749 nomask = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000750 iattr.ia_mode |= ap->ae_perm << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 *basicperms = 0;
752 break;
753 case ACL_OTHER:
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000754 iattr.ia_mode |= ap->ae_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756 default: /* more than just standard modes */
757 *basicperms = 0;
758 break;
759 }
760 ap++;
761 }
762
763 /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
764 if (gap && nomask)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000765 iattr.ia_mode |= gap->ae_perm << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000767 return xfs_setattr(xfs_vtoi(vp), &iattr, 0, sys_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/*
771 * The permissions for the special ACL entries (u::, g::[m::], o::) are
772 * actually stored in the file mode (if there is both a group and a mask,
773 * the group is stored in the ACL entry and the mask is stored on the file).
774 * This allows the mode to remain automatically in sync with the ACL without
775 * the need for a call-back to the ACL system at every point where the mode
776 * could change. This function takes the permissions from the specified mode
777 * and places it in the supplied ACL.
778 *
779 * This implementation draws its validity from the fact that, when the ACL
780 * was assigned, the mode was copied from the ACL.
781 * If the mode did not change, therefore, the mode remains exactly what was
782 * taken from the special ACL entries at assignment.
783 * If a subsequent chmod() was done, the POSIX spec says that the change in
784 * mode must cause an update to the ACL seen at user level and used for
785 * access checks. Before and after a mode change, therefore, the file mode
786 * most accurately reflects what the special ACL entries should permit/deny.
787 *
788 * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
789 * the existing mode bits will override whatever is in the
790 * ACL. Similarly, if there is a pre-existing ACL that was
791 * never in sync with its mode (owing to a bug in 6.5 and
792 * before), it will now magically (or mystically) be
793 * synchronized. This could cause slight astonishment, but
794 * it is better than inconsistent permissions.
795 *
796 * The supplied ACL is a template that may contain any combination
797 * of special entries. These are treated as place holders when we fill
798 * out the ACL. This routine does not add or remove special entries, it
799 * simply unites each special entry with its associated set of permissions.
800 */
801STATIC void
802xfs_acl_sync_mode(
803 mode_t mode,
804 xfs_acl_t *acl)
805{
806 int i, nomask = 1;
807 xfs_acl_entry_t *ap;
808 xfs_acl_entry_t *gap = NULL;
809
810 /*
811 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
812 * be set instead of the GROUP entry, if there is a MASK.
813 */
814 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
815 switch (ap->ae_tag) {
816 case ACL_USER_OBJ:
817 ap->ae_perm = (mode >> 6) & 0x7;
818 break;
819 case ACL_GROUP_OBJ:
820 gap = ap;
821 break;
822 case ACL_MASK:
823 nomask = 0;
824 ap->ae_perm = (mode >> 3) & 0x7;
825 break;
826 case ACL_OTHER:
827 ap->ae_perm = mode & 0x7;
828 break;
829 default:
830 break;
831 }
832 }
833 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
834 if (gap && nomask)
835 gap->ae_perm = (mode >> 3) & 0x7;
836}
837
838/*
839 * When inheriting an Access ACL from a directory Default ACL,
840 * the ACL bits are set to the intersection of the ACL default
841 * permission bits and the file permission bits in mode. If there
842 * are no permission bits on the file then we must not give them
843 * the ACL. This is what what makes umask() work with ACLs.
844 */
845STATIC void
846xfs_acl_filter_mode(
847 mode_t mode,
848 xfs_acl_t *acl)
849{
850 int i, nomask = 1;
851 xfs_acl_entry_t *ap;
852 xfs_acl_entry_t *gap = NULL;
853
854 /*
855 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
856 * be merged with GROUP entry, if there is a MASK.
857 */
858 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
859 switch (ap->ae_tag) {
860 case ACL_USER_OBJ:
861 ap->ae_perm &= (mode >> 6) & 0x7;
862 break;
863 case ACL_GROUP_OBJ:
864 gap = ap;
865 break;
866 case ACL_MASK:
867 nomask = 0;
868 ap->ae_perm &= (mode >> 3) & 0x7;
869 break;
870 case ACL_OTHER:
871 ap->ae_perm &= mode & 0x7;
872 break;
873 default:
874 break;
875 }
876 }
877 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
878 if (gap && nomask)
879 gap->ae_perm &= (mode >> 3) & 0x7;
880}