]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/xfs/xfs_acl.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / xfs_acl.c
1 /*
2 * Copyright (c) 2001-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * 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.
13 *
14 * 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
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_inum.h"
23 #include "xfs_ag.h"
24 #include "xfs_dir2.h"
25 #include "xfs_bmap_btree.h"
26 #include "xfs_alloc_btree.h"
27 #include "xfs_ialloc_btree.h"
28 #include "xfs_dir2_sf.h"
29 #include "xfs_attr_sf.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_acl.h"
34 #include "xfs_attr.h"
35 #include "xfs_vnodeops.h"
36
37 #include <linux/capability.h>
38 #include <linux/posix_acl_xattr.h>
39
40 STATIC int xfs_acl_setmode(bhv_vnode_t *, xfs_acl_t *, int *);
41 STATIC void xfs_acl_filter_mode(mode_t, xfs_acl_t *);
42 STATIC void xfs_acl_get_endian(xfs_acl_t *);
43 STATIC int xfs_acl_access(uid_t, gid_t, xfs_acl_t *, mode_t, cred_t *);
44 STATIC int xfs_acl_invalid(xfs_acl_t *);
45 STATIC void xfs_acl_sync_mode(mode_t, xfs_acl_t *);
46 STATIC void xfs_acl_get_attr(bhv_vnode_t *, xfs_acl_t *, int, int, int *);
47 STATIC void xfs_acl_set_attr(bhv_vnode_t *, xfs_acl_t *, int, int *);
48 STATIC int xfs_acl_allow_set(bhv_vnode_t *, int);
49
50 kmem_zone_t *xfs_acl_zone;
51
52
53 /*
54 * Test for existence of access ACL attribute as efficiently as possible.
55 */
56 int
57 xfs_acl_vhasacl_access(
58 bhv_vnode_t *vp)
59 {
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 */
69 int
70 xfs_acl_vhasacl_default(
71 bhv_vnode_t *vp)
72 {
73 int error;
74
75 if (!S_ISDIR(vp->i_mode))
76 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 */
84 STATIC int
85 posix_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 /*
145 * Comparison function called from xfs_sort().
146 * Primary key is ae_tag, secondary key is ae_id.
147 */
148 STATIC int
149 xfs_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 */
164 STATIC int
165 posix_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> */
179 xfs_sort(src->acl_entry, src->acl_cnt, sizeof(src->acl_entry[0]),
180 xfs_acl_entry_compare);
181
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
208 int
209 xfs_acl_vget(
210 bhv_vnode_t *vp,
211 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 }
241 if (kind == _ACL_TYPE_ACCESS)
242 xfs_acl_sync_mode(xfs_vtoi(vp)->i_d.di_mode, xfs_acl);
243 error = -posix_acl_xfs_to_xattr(xfs_acl, ext_acl, size);
244 }
245 out:
246 VN_RELE(vp);
247 if(xfs_acl)
248 _ACL_FREE(xfs_acl);
249 return -error;
250 }
251
252 int
253 xfs_acl_vremove(
254 bhv_vnode_t *vp,
255 int kind)
256 {
257 int error;
258
259 VN_HOLD(vp);
260 error = xfs_acl_allow_set(vp, kind);
261 if (!error) {
262 error = xfs_attr_remove(xfs_vtoi(vp),
263 kind == _ACL_TYPE_DEFAULT?
264 SGI_ACL_DEFAULT: SGI_ACL_FILE,
265 ATTR_ROOT);
266 if (error == ENOATTR)
267 error = 0; /* 'scool */
268 }
269 VN_RELE(vp);
270 return -error;
271 }
272
273 int
274 xfs_acl_vset(
275 bhv_vnode_t *vp,
276 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);
303
304 /* Incoming ACL exists, set file mode based on its value */
305 if (!error && kind == _ACL_TYPE_ACCESS)
306 error = xfs_acl_setmode(vp, xfs_acl, &basicperms);
307
308 if (error)
309 goto out;
310
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 {
320 error = -xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
321 }
322
323 out:
324 VN_RELE(vp);
325 _ACL_FREE(xfs_acl);
326 return -error;
327 }
328
329 int
330 xfs_acl_iaccess(
331 xfs_inode_t *ip,
332 mode_t mode,
333 cred_t *cr)
334 {
335 xfs_acl_t *acl;
336 int rval;
337 struct xfs_name acl_name = {SGI_ACL_FILE, SGI_ACL_FILE_SIZE};
338
339 if (!(_ACL_ALLOC(acl)))
340 return -1;
341
342 /* If the file has no ACL return -1. */
343 rval = sizeof(xfs_acl_t);
344 if (xfs_attr_fetch(ip, &acl_name, (char *)acl, &rval,
345 ATTR_ROOT | ATTR_KERNACCESS)) {
346 _ACL_FREE(acl);
347 return -1;
348 }
349 xfs_acl_get_endian(acl);
350
351 /* If the file has an empty ACL return -1. */
352 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT) {
353 _ACL_FREE(acl);
354 return -1;
355 }
356
357 /* Synchronize ACL with mode bits */
358 xfs_acl_sync_mode(ip->i_d.di_mode, acl);
359
360 rval = xfs_acl_access(ip->i_d.di_uid, ip->i_d.di_gid, acl, mode, cr);
361 _ACL_FREE(acl);
362 return rval;
363 }
364
365 STATIC int
366 xfs_acl_allow_set(
367 bhv_vnode_t *vp,
368 int kind)
369 {
370 if (vp->i_flags & (S_IMMUTABLE|S_APPEND))
371 return EPERM;
372 if (kind == _ACL_TYPE_DEFAULT && !S_ISDIR(vp->i_mode))
373 return ENOTDIR;
374 if (vp->i_sb->s_flags & MS_RDONLY)
375 return EROFS;
376 if (xfs_vtoi(vp)->i_d.di_uid != current->fsuid && !capable(CAP_FOWNER))
377 return EPERM;
378 return 0;
379 }
380
381 /*
382 * Note: cr is only used here for the capability check if the ACL test fails.
383 * It is not used to find out the credentials uid or groups etc, as was
384 * done in IRIX. It is assumed that the uid and groups for the current
385 * thread are taken from "current" instead of the cr parameter.
386 */
387 STATIC int
388 xfs_acl_access(
389 uid_t fuid,
390 gid_t fgid,
391 xfs_acl_t *fap,
392 mode_t md,
393 cred_t *cr)
394 {
395 xfs_acl_entry_t matched;
396 int i, allows;
397 int maskallows = -1; /* true, but not 1, either */
398 int seen_userobj = 0;
399
400 matched.ae_tag = 0; /* Invalid type */
401 matched.ae_perm = 0;
402
403 for (i = 0; i < fap->acl_cnt; i++) {
404 /*
405 * Break out if we've got a user_obj entry or
406 * a user entry and the mask (and have processed USER_OBJ)
407 */
408 if (matched.ae_tag == ACL_USER_OBJ)
409 break;
410 if (matched.ae_tag == ACL_USER) {
411 if (maskallows != -1 && seen_userobj)
412 break;
413 if (fap->acl_entry[i].ae_tag != ACL_MASK &&
414 fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
415 continue;
416 }
417 /* True if this entry allows the requested access */
418 allows = ((fap->acl_entry[i].ae_perm & md) == md);
419
420 switch (fap->acl_entry[i].ae_tag) {
421 case ACL_USER_OBJ:
422 seen_userobj = 1;
423 if (fuid != current->fsuid)
424 continue;
425 matched.ae_tag = ACL_USER_OBJ;
426 matched.ae_perm = allows;
427 break;
428 case ACL_USER:
429 if (fap->acl_entry[i].ae_id != current->fsuid)
430 continue;
431 matched.ae_tag = ACL_USER;
432 matched.ae_perm = allows;
433 break;
434 case ACL_GROUP_OBJ:
435 if ((matched.ae_tag == ACL_GROUP_OBJ ||
436 matched.ae_tag == ACL_GROUP) && !allows)
437 continue;
438 if (!in_group_p(fgid))
439 continue;
440 matched.ae_tag = ACL_GROUP_OBJ;
441 matched.ae_perm = allows;
442 break;
443 case ACL_GROUP:
444 if ((matched.ae_tag == ACL_GROUP_OBJ ||
445 matched.ae_tag == ACL_GROUP) && !allows)
446 continue;
447 if (!in_group_p(fap->acl_entry[i].ae_id))
448 continue;
449 matched.ae_tag = ACL_GROUP;
450 matched.ae_perm = allows;
451 break;
452 case ACL_MASK:
453 maskallows = allows;
454 break;
455 case ACL_OTHER:
456 if (matched.ae_tag != 0)
457 continue;
458 matched.ae_tag = ACL_OTHER;
459 matched.ae_perm = allows;
460 break;
461 }
462 }
463 /*
464 * First possibility is that no matched entry allows access.
465 * The capability to override DAC may exist, so check for it.
466 */
467 switch (matched.ae_tag) {
468 case ACL_OTHER:
469 case ACL_USER_OBJ:
470 if (matched.ae_perm)
471 return 0;
472 break;
473 case ACL_USER:
474 case ACL_GROUP_OBJ:
475 case ACL_GROUP:
476 if (maskallows && matched.ae_perm)
477 return 0;
478 break;
479 case 0:
480 break;
481 }
482
483 /* EACCES tells generic_permission to check for capability overrides */
484 return EACCES;
485 }
486
487 /*
488 * ACL validity checker.
489 * This acl validation routine checks each ACL entry read in makes sense.
490 */
491 STATIC int
492 xfs_acl_invalid(
493 xfs_acl_t *aclp)
494 {
495 xfs_acl_entry_t *entry, *e;
496 int user = 0, group = 0, other = 0, mask = 0;
497 int mask_required = 0;
498 int i, j;
499
500 if (!aclp)
501 goto acl_invalid;
502
503 if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
504 goto acl_invalid;
505
506 for (i = 0; i < aclp->acl_cnt; i++) {
507 entry = &aclp->acl_entry[i];
508 switch (entry->ae_tag) {
509 case ACL_USER_OBJ:
510 if (user++)
511 goto acl_invalid;
512 break;
513 case ACL_GROUP_OBJ:
514 if (group++)
515 goto acl_invalid;
516 break;
517 case ACL_OTHER:
518 if (other++)
519 goto acl_invalid;
520 break;
521 case ACL_USER:
522 case ACL_GROUP:
523 for (j = i + 1; j < aclp->acl_cnt; j++) {
524 e = &aclp->acl_entry[j];
525 if (e->ae_id == entry->ae_id &&
526 e->ae_tag == entry->ae_tag)
527 goto acl_invalid;
528 }
529 mask_required++;
530 break;
531 case ACL_MASK:
532 if (mask++)
533 goto acl_invalid;
534 break;
535 default:
536 goto acl_invalid;
537 }
538 }
539 if (!user || !group || !other || (mask_required && !mask))
540 goto acl_invalid;
541 else
542 return 0;
543 acl_invalid:
544 return EINVAL;
545 }
546
547 /*
548 * Do ACL endian conversion.
549 */
550 STATIC void
551 xfs_acl_get_endian(
552 xfs_acl_t *aclp)
553 {
554 xfs_acl_entry_t *ace, *end;
555
556 INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
557 end = &aclp->acl_entry[0]+aclp->acl_cnt;
558 for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
559 INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
560 INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
561 INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
562 }
563 }
564
565 /*
566 * Get the ACL from the EA and do endian conversion.
567 */
568 STATIC void
569 xfs_acl_get_attr(
570 bhv_vnode_t *vp,
571 xfs_acl_t *aclp,
572 int kind,
573 int flags,
574 int *error)
575 {
576 int len = sizeof(xfs_acl_t);
577
578 ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
579 flags |= ATTR_ROOT;
580 *error = xfs_attr_get(xfs_vtoi(vp),
581 kind == _ACL_TYPE_ACCESS ?
582 SGI_ACL_FILE : SGI_ACL_DEFAULT,
583 (char *)aclp, &len, flags);
584 if (*error || (flags & ATTR_KERNOVAL))
585 return;
586 xfs_acl_get_endian(aclp);
587 }
588
589 /*
590 * Set the EA with the ACL and do endian conversion.
591 */
592 STATIC void
593 xfs_acl_set_attr(
594 bhv_vnode_t *vp,
595 xfs_acl_t *aclp,
596 int kind,
597 int *error)
598 {
599 xfs_acl_entry_t *ace, *newace, *end;
600 xfs_acl_t *newacl;
601 int len;
602
603 if (!(_ACL_ALLOC(newacl))) {
604 *error = ENOMEM;
605 return;
606 }
607
608 len = sizeof(xfs_acl_t) -
609 (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
610 end = &aclp->acl_entry[0]+aclp->acl_cnt;
611 for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
612 ace < end;
613 ace++, newace++) {
614 INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
615 INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
616 INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
617 }
618 INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
619 *error = xfs_attr_set(xfs_vtoi(vp),
620 kind == _ACL_TYPE_ACCESS ?
621 SGI_ACL_FILE: SGI_ACL_DEFAULT,
622 (char *)newacl, len, ATTR_ROOT);
623 _ACL_FREE(newacl);
624 }
625
626 int
627 xfs_acl_vtoacl(
628 bhv_vnode_t *vp,
629 xfs_acl_t *access_acl,
630 xfs_acl_t *default_acl)
631 {
632 int error = 0;
633
634 if (access_acl) {
635 /*
636 * Get the Access ACL and the mode. If either cannot
637 * be obtained for some reason, invalidate the access ACL.
638 */
639 xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
640 if (error)
641 access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
642 else /* We have a good ACL and the file mode, synchronize. */
643 xfs_acl_sync_mode(xfs_vtoi(vp)->i_d.di_mode, access_acl);
644 }
645
646 if (default_acl) {
647 xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
648 if (error)
649 default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
650 }
651 return error;
652 }
653
654 /*
655 * This function retrieves the parent directory's acl, processes it
656 * and lets the child inherit the acl(s) that it should.
657 */
658 int
659 xfs_acl_inherit(
660 bhv_vnode_t *vp,
661 mode_t mode,
662 xfs_acl_t *pdaclp)
663 {
664 xfs_acl_t *cacl;
665 int error = 0;
666 int basicperms = 0;
667
668 /*
669 * If the parent does not have a default ACL, or it's an
670 * invalid ACL, we're done.
671 */
672 if (!vp)
673 return 0;
674 if (!pdaclp || xfs_acl_invalid(pdaclp))
675 return 0;
676
677 /*
678 * Copy the default ACL of the containing directory to
679 * the access ACL of the new file and use the mode that
680 * was passed in to set up the correct initial values for
681 * the u::,g::[m::], and o:: entries. This is what makes
682 * umask() "work" with ACL's.
683 */
684
685 if (!(_ACL_ALLOC(cacl)))
686 return ENOMEM;
687
688 memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
689 xfs_acl_filter_mode(mode, cacl);
690 error = xfs_acl_setmode(vp, cacl, &basicperms);
691 if (error)
692 goto out_error;
693
694 /*
695 * Set the Default and Access ACL on the file. The mode is already
696 * set on the file, so we don't need to worry about that.
697 *
698 * If the new file is a directory, its default ACL is a copy of
699 * the containing directory's default ACL.
700 */
701 if (S_ISDIR(vp->i_mode))
702 xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
703 if (!error && !basicperms)
704 xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
705 out_error:
706 _ACL_FREE(cacl);
707 return error;
708 }
709
710 /*
711 * Set up the correct mode on the file based on the supplied ACL. This
712 * makes sure that the mode on the file reflects the state of the
713 * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
714 * the ACL is going to get the permissions for these entries, we must
715 * synchronize the mode whenever we set the ACL on a file.
716 */
717 STATIC int
718 xfs_acl_setmode(
719 bhv_vnode_t *vp,
720 xfs_acl_t *acl,
721 int *basicperms)
722 {
723 bhv_vattr_t va;
724 xfs_acl_entry_t *ap;
725 xfs_acl_entry_t *gap = NULL;
726 int i, nomask = 1;
727
728 *basicperms = 1;
729
730 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
731 return 0;
732
733 /*
734 * Copy the u::, g::, o::, and m:: bits from the ACL into the
735 * mode. The m:: bits take precedence over the g:: bits.
736 */
737 va.va_mask = XFS_AT_MODE;
738 va.va_mode = xfs_vtoi(vp)->i_d.di_mode;
739 va.va_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
740 ap = acl->acl_entry;
741 for (i = 0; i < acl->acl_cnt; ++i) {
742 switch (ap->ae_tag) {
743 case ACL_USER_OBJ:
744 va.va_mode |= ap->ae_perm << 6;
745 break;
746 case ACL_GROUP_OBJ:
747 gap = ap;
748 break;
749 case ACL_MASK: /* more than just standard modes */
750 nomask = 0;
751 va.va_mode |= ap->ae_perm << 3;
752 *basicperms = 0;
753 break;
754 case ACL_OTHER:
755 va.va_mode |= ap->ae_perm;
756 break;
757 default: /* more than just standard modes */
758 *basicperms = 0;
759 break;
760 }
761 ap++;
762 }
763
764 /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
765 if (gap && nomask)
766 va.va_mode |= gap->ae_perm << 3;
767
768 return xfs_setattr(xfs_vtoi(vp), &va, 0, sys_cred);
769 }
770
771 /*
772 * The permissions for the special ACL entries (u::, g::[m::], o::) are
773 * actually stored in the file mode (if there is both a group and a mask,
774 * the group is stored in the ACL entry and the mask is stored on the file).
775 * This allows the mode to remain automatically in sync with the ACL without
776 * the need for a call-back to the ACL system at every point where the mode
777 * could change. This function takes the permissions from the specified mode
778 * and places it in the supplied ACL.
779 *
780 * This implementation draws its validity from the fact that, when the ACL
781 * was assigned, the mode was copied from the ACL.
782 * If the mode did not change, therefore, the mode remains exactly what was
783 * taken from the special ACL entries at assignment.
784 * If a subsequent chmod() was done, the POSIX spec says that the change in
785 * mode must cause an update to the ACL seen at user level and used for
786 * access checks. Before and after a mode change, therefore, the file mode
787 * most accurately reflects what the special ACL entries should permit/deny.
788 *
789 * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
790 * the existing mode bits will override whatever is in the
791 * ACL. Similarly, if there is a pre-existing ACL that was
792 * never in sync with its mode (owing to a bug in 6.5 and
793 * before), it will now magically (or mystically) be
794 * synchronized. This could cause slight astonishment, but
795 * it is better than inconsistent permissions.
796 *
797 * The supplied ACL is a template that may contain any combination
798 * of special entries. These are treated as place holders when we fill
799 * out the ACL. This routine does not add or remove special entries, it
800 * simply unites each special entry with its associated set of permissions.
801 */
802 STATIC void
803 xfs_acl_sync_mode(
804 mode_t mode,
805 xfs_acl_t *acl)
806 {
807 int i, nomask = 1;
808 xfs_acl_entry_t *ap;
809 xfs_acl_entry_t *gap = NULL;
810
811 /*
812 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
813 * be set instead of the GROUP entry, if there is a MASK.
814 */
815 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
816 switch (ap->ae_tag) {
817 case ACL_USER_OBJ:
818 ap->ae_perm = (mode >> 6) & 0x7;
819 break;
820 case ACL_GROUP_OBJ:
821 gap = ap;
822 break;
823 case ACL_MASK:
824 nomask = 0;
825 ap->ae_perm = (mode >> 3) & 0x7;
826 break;
827 case ACL_OTHER:
828 ap->ae_perm = mode & 0x7;
829 break;
830 default:
831 break;
832 }
833 }
834 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
835 if (gap && nomask)
836 gap->ae_perm = (mode >> 3) & 0x7;
837 }
838
839 /*
840 * When inheriting an Access ACL from a directory Default ACL,
841 * the ACL bits are set to the intersection of the ACL default
842 * permission bits and the file permission bits in mode. If there
843 * are no permission bits on the file then we must not give them
844 * the ACL. This is what what makes umask() work with ACLs.
845 */
846 STATIC void
847 xfs_acl_filter_mode(
848 mode_t mode,
849 xfs_acl_t *acl)
850 {
851 int i, nomask = 1;
852 xfs_acl_entry_t *ap;
853 xfs_acl_entry_t *gap = NULL;
854
855 /*
856 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
857 * be merged with GROUP entry, if there is a MASK.
858 */
859 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
860 switch (ap->ae_tag) {
861 case ACL_USER_OBJ:
862 ap->ae_perm &= (mode >> 6) & 0x7;
863 break;
864 case ACL_GROUP_OBJ:
865 gap = ap;
866 break;
867 case ACL_MASK:
868 nomask = 0;
869 ap->ae_perm &= (mode >> 3) & 0x7;
870 break;
871 case ACL_OTHER:
872 ap->ae_perm &= mode & 0x7;
873 break;
874 default:
875 break;
876 }
877 }
878 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
879 if (gap && nomask)
880 gap->ae_perm &= (mode >> 3) & 0x7;
881 }