2 * Copyright (c) 2001-2002,2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.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"
37 #include <linux/capability.h>
38 #include <linux/posix_acl_xattr.h>
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);
50 kmem_zone_t
*xfs_acl_zone
;
54 * Test for existence of access ACL attribute as efficiently as possible.
57 xfs_acl_vhasacl_access(
62 xfs_acl_get_attr(vp
, NULL
, _ACL_TYPE_ACCESS
, ATTR_KERNOVAL
, &error
);
67 * Test for existence of default ACL attribute as efficiently as possible.
70 xfs_acl_vhasacl_default(
77 xfs_acl_get_attr(vp
, NULL
, _ACL_TYPE_DEFAULT
, ATTR_KERNOVAL
, &error
);
82 * Convert from extended attribute representation to in-memory for XFS.
85 posix_acl_xattr_to_xfs(
86 posix_acl_xattr_header
*src
,
90 posix_acl_xattr_entry
*src_entry
;
91 xfs_acl_entry_t
*dest_entry
;
97 if (size
< sizeof(posix_acl_xattr_header
))
100 if (src
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
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
)
109 * acl_set_file(3) may request that we set default ACLs with
110 * zero length -- defend (gracefully) against that here.
115 src_entry
= (posix_acl_xattr_entry
*)((char *)src
+ sizeof(*src
));
116 dest_entry
= &dest
->acl_entry
[0];
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
))
122 dest_entry
->ae_tag
= le16_to_cpu(src_entry
->e_tag
);
123 switch(dest_entry
->ae_tag
) {
126 dest_entry
->ae_id
= le32_to_cpu(src_entry
->e_id
);
132 dest_entry
->ae_id
= ACL_UNDEFINED_ID
;
138 if (xfs_acl_invalid(dest
))
145 * Comparison function called from xfs_sort().
146 * Primary key is ae_tag, secondary key is ae_id.
149 xfs_acl_entry_compare(
153 xfs_acl_entry_t
*a
= (xfs_acl_entry_t
*)va
,
154 *b
= (xfs_acl_entry_t
*)vb
;
156 if (a
->ae_tag
== b
->ae_tag
)
157 return (a
->ae_id
- b
->ae_id
);
158 return (a
->ae_tag
- b
->ae_tag
);
162 * Convert from in-memory XFS to extended attribute representation.
165 posix_acl_xfs_to_xattr(
167 posix_acl_xattr_header
*dest
,
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
;
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
);
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
))
189 dest_entry
->e_tag
= cpu_to_le16(src_entry
->ae_tag
);
190 switch (src_entry
->ae_tag
) {
193 dest_entry
->e_id
= cpu_to_le32(src_entry
->ae_id
);
199 dest_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
216 xfs_acl_t
*xfs_acl
= NULL
;
217 posix_acl_xattr_header
*ext_acl
= acl
;
222 if (!(_ACL_ALLOC(xfs_acl
))) {
226 memset(xfs_acl
, 0, sizeof(xfs_acl_t
));
228 flags
= ATTR_KERNOVAL
;
230 xfs_acl_get_attr(vp
, xfs_acl
, kind
, flags
, &error
);
235 error
= -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES
);
237 if (xfs_acl_invalid(xfs_acl
)) {
241 if (kind
== _ACL_TYPE_ACCESS
) {
244 va
.va_mask
= XFS_AT_MODE
;
245 error
= bhv_vop_getattr(vp
, &va
, 0, sys_cred
);
248 xfs_acl_sync_mode(va
.va_mode
, xfs_acl
);
250 error
= -posix_acl_xfs_to_xattr(xfs_acl
, ext_acl
, size
);
267 error
= xfs_acl_allow_set(vp
, kind
);
269 error
= bhv_vop_attr_remove(vp
, kind
== _ACL_TYPE_DEFAULT
?
270 SGI_ACL_DEFAULT
: SGI_ACL_FILE
,
271 ATTR_ROOT
, sys_cred
);
272 if (error
== ENOATTR
)
273 error
= 0; /* 'scool */
286 posix_acl_xattr_header
*ext_acl
= acl
;
289 int basicperms
= 0; /* more than std unix perms? */
294 if (!(_ACL_ALLOC(xfs_acl
)))
297 error
= posix_acl_xattr_to_xfs(ext_acl
, size
, xfs_acl
);
302 if (!xfs_acl
->acl_cnt
) {
308 error
= xfs_acl_allow_set(vp
, kind
);
312 /* Incoming ACL exists, set file mode based on its value */
313 if (kind
== _ACL_TYPE_ACCESS
)
314 xfs_acl_setmode(vp
, xfs_acl
, &basicperms
);
317 * If we have more than std unix permissions, set up the actual attr.
318 * Otherwise, delete any existing attr. This prevents us from
319 * having actual attrs for permissions that can be stored in the
320 * standard permission bits.
323 xfs_acl_set_attr(vp
, xfs_acl
, kind
, &error
);
325 xfs_acl_vremove(vp
, _ACL_TYPE_ACCESS
);
343 if (!(_ACL_ALLOC(acl
)))
346 /* If the file has no ACL return -1. */
347 rval
= sizeof(xfs_acl_t
);
348 if (xfs_attr_fetch(ip
, SGI_ACL_FILE
, SGI_ACL_FILE_SIZE
,
349 (char *)acl
, &rval
, ATTR_ROOT
| ATTR_KERNACCESS
, cr
)) {
353 xfs_acl_get_endian(acl
);
355 /* If the file has an empty ACL return -1. */
356 if (acl
->acl_cnt
== XFS_ACL_NOT_PRESENT
) {
361 /* Synchronize ACL with mode bits */
362 xfs_acl_sync_mode(ip
->i_d
.di_mode
, acl
);
364 rval
= xfs_acl_access(ip
->i_d
.di_uid
, ip
->i_d
.di_gid
, acl
, mode
, cr
);
377 if (vp
->v_inode
.i_flags
& (S_IMMUTABLE
|S_APPEND
))
379 if (kind
== _ACL_TYPE_DEFAULT
&& !VN_ISDIR(vp
))
381 if (vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
)
383 va
.va_mask
= XFS_AT_UID
;
384 error
= bhv_vop_getattr(vp
, &va
, 0, NULL
);
387 if (va
.va_uid
!= current
->fsuid
&& !capable(CAP_FOWNER
))
393 * The access control process to determine the access permission:
394 * if uid == file owner id, use the file owner bits.
395 * if gid == file owner group id, use the file group bits.
396 * scan ACL for a matching user or group, and use matched entry
397 * permission. Use total permissions of all matching group entries,
398 * until all acl entries are exhausted. The final permission produced
399 * by matching acl entry or entries needs to be & with group permission.
400 * if not owner, owning group, or matching entry in ACL, use file
404 xfs_acl_capability_check(
408 if ((mode
& ACL_READ
) && !capable_cred(cr
, CAP_DAC_READ_SEARCH
))
410 if ((mode
& ACL_WRITE
) && !capable_cred(cr
, CAP_DAC_OVERRIDE
))
412 if ((mode
& ACL_EXECUTE
) && !capable_cred(cr
, CAP_DAC_OVERRIDE
))
419 * Note: cr is only used here for the capability check if the ACL test fails.
420 * It is not used to find out the credentials uid or groups etc, as was
421 * done in IRIX. It is assumed that the uid and groups for the current
422 * thread are taken from "current" instead of the cr parameter.
432 xfs_acl_entry_t matched
;
434 int maskallows
= -1; /* true, but not 1, either */
435 int seen_userobj
= 0;
437 matched
.ae_tag
= 0; /* Invalid type */
439 md
>>= 6; /* Normalize the bits for comparison */
441 for (i
= 0; i
< fap
->acl_cnt
; i
++) {
443 * Break out if we've got a user_obj entry or
444 * a user entry and the mask (and have processed USER_OBJ)
446 if (matched
.ae_tag
== ACL_USER_OBJ
)
448 if (matched
.ae_tag
== ACL_USER
) {
449 if (maskallows
!= -1 && seen_userobj
)
451 if (fap
->acl_entry
[i
].ae_tag
!= ACL_MASK
&&
452 fap
->acl_entry
[i
].ae_tag
!= ACL_USER_OBJ
)
455 /* True if this entry allows the requested access */
456 allows
= ((fap
->acl_entry
[i
].ae_perm
& md
) == md
);
458 switch (fap
->acl_entry
[i
].ae_tag
) {
461 if (fuid
!= current
->fsuid
)
463 matched
.ae_tag
= ACL_USER_OBJ
;
464 matched
.ae_perm
= allows
;
467 if (fap
->acl_entry
[i
].ae_id
!= current
->fsuid
)
469 matched
.ae_tag
= ACL_USER
;
470 matched
.ae_perm
= allows
;
473 if ((matched
.ae_tag
== ACL_GROUP_OBJ
||
474 matched
.ae_tag
== ACL_GROUP
) && !allows
)
476 if (!in_group_p(fgid
))
478 matched
.ae_tag
= ACL_GROUP_OBJ
;
479 matched
.ae_perm
= allows
;
482 if ((matched
.ae_tag
== ACL_GROUP_OBJ
||
483 matched
.ae_tag
== ACL_GROUP
) && !allows
)
485 if (!in_group_p(fap
->acl_entry
[i
].ae_id
))
487 matched
.ae_tag
= ACL_GROUP
;
488 matched
.ae_perm
= allows
;
494 if (matched
.ae_tag
!= 0)
496 matched
.ae_tag
= ACL_OTHER
;
497 matched
.ae_perm
= allows
;
502 * First possibility is that no matched entry allows access.
503 * The capability to override DAC may exist, so check for it.
505 switch (matched
.ae_tag
) {
514 if (maskallows
&& matched
.ae_perm
)
521 return xfs_acl_capability_check(md
, cr
);
525 * ACL validity checker.
526 * This acl validation routine checks each ACL entry read in makes sense.
532 xfs_acl_entry_t
*entry
, *e
;
533 int user
= 0, group
= 0, other
= 0, mask
= 0;
534 int mask_required
= 0;
540 if (aclp
->acl_cnt
> XFS_ACL_MAX_ENTRIES
)
543 for (i
= 0; i
< aclp
->acl_cnt
; i
++) {
544 entry
= &aclp
->acl_entry
[i
];
545 switch (entry
->ae_tag
) {
560 for (j
= i
+ 1; j
< aclp
->acl_cnt
; j
++) {
561 e
= &aclp
->acl_entry
[j
];
562 if (e
->ae_id
== entry
->ae_id
&&
563 e
->ae_tag
== entry
->ae_tag
)
576 if (!user
|| !group
|| !other
|| (mask_required
&& !mask
))
585 * Do ACL endian conversion.
591 xfs_acl_entry_t
*ace
, *end
;
593 INT_SET(aclp
->acl_cnt
, ARCH_CONVERT
, aclp
->acl_cnt
);
594 end
= &aclp
->acl_entry
[0]+aclp
->acl_cnt
;
595 for (ace
= &aclp
->acl_entry
[0]; ace
< end
; ace
++) {
596 INT_SET(ace
->ae_tag
, ARCH_CONVERT
, ace
->ae_tag
);
597 INT_SET(ace
->ae_id
, ARCH_CONVERT
, ace
->ae_id
);
598 INT_SET(ace
->ae_perm
, ARCH_CONVERT
, ace
->ae_perm
);
603 * Get the ACL from the EA and do endian conversion.
613 int len
= sizeof(xfs_acl_t
);
615 ASSERT((flags
& ATTR_KERNOVAL
) ? (aclp
== NULL
) : 1);
617 *error
= bhv_vop_attr_get(vp
, kind
== _ACL_TYPE_ACCESS
?
618 SGI_ACL_FILE
: SGI_ACL_DEFAULT
,
619 (char *)aclp
, &len
, flags
, sys_cred
);
620 if (*error
|| (flags
& ATTR_KERNOVAL
))
622 xfs_acl_get_endian(aclp
);
626 * Set the EA with the ACL and do endian conversion.
635 xfs_acl_entry_t
*ace
, *newace
, *end
;
639 if (!(_ACL_ALLOC(newacl
))) {
644 len
= sizeof(xfs_acl_t
) -
645 (sizeof(xfs_acl_entry_t
) * (XFS_ACL_MAX_ENTRIES
- aclp
->acl_cnt
));
646 end
= &aclp
->acl_entry
[0]+aclp
->acl_cnt
;
647 for (ace
= &aclp
->acl_entry
[0], newace
= &newacl
->acl_entry
[0];
650 INT_SET(newace
->ae_tag
, ARCH_CONVERT
, ace
->ae_tag
);
651 INT_SET(newace
->ae_id
, ARCH_CONVERT
, ace
->ae_id
);
652 INT_SET(newace
->ae_perm
, ARCH_CONVERT
, ace
->ae_perm
);
654 INT_SET(newacl
->acl_cnt
, ARCH_CONVERT
, aclp
->acl_cnt
);
655 *error
= bhv_vop_attr_set(vp
, kind
== _ACL_TYPE_ACCESS
?
656 SGI_ACL_FILE
: SGI_ACL_DEFAULT
,
657 (char *)newacl
, len
, ATTR_ROOT
, sys_cred
);
664 xfs_acl_t
*access_acl
,
665 xfs_acl_t
*default_acl
)
672 * Get the Access ACL and the mode. If either cannot
673 * be obtained for some reason, invalidate the access ACL.
675 xfs_acl_get_attr(vp
, access_acl
, _ACL_TYPE_ACCESS
, 0, &error
);
677 /* Got the ACL, need the mode... */
678 va
.va_mask
= XFS_AT_MODE
;
679 error
= bhv_vop_getattr(vp
, &va
, 0, sys_cred
);
683 access_acl
->acl_cnt
= XFS_ACL_NOT_PRESENT
;
684 else /* We have a good ACL and the file mode, synchronize. */
685 xfs_acl_sync_mode(va
.va_mode
, access_acl
);
689 xfs_acl_get_attr(vp
, default_acl
, _ACL_TYPE_DEFAULT
, 0, &error
);
691 default_acl
->acl_cnt
= XFS_ACL_NOT_PRESENT
;
697 * This function retrieves the parent directory's acl, processes it
698 * and lets the child inherit the acl(s) that it should.
711 * If the parent does not have a default ACL, or it's an
712 * invalid ACL, we're done.
716 if (!pdaclp
|| xfs_acl_invalid(pdaclp
))
720 * Copy the default ACL of the containing directory to
721 * the access ACL of the new file and use the mode that
722 * was passed in to set up the correct initial values for
723 * the u::,g::[m::], and o:: entries. This is what makes
724 * umask() "work" with ACL's.
727 if (!(_ACL_ALLOC(cacl
)))
730 memcpy(cacl
, pdaclp
, sizeof(xfs_acl_t
));
731 xfs_acl_filter_mode(vap
->va_mode
, cacl
);
732 xfs_acl_setmode(vp
, cacl
, &basicperms
);
735 * Set the Default and Access ACL on the file. The mode is already
736 * set on the file, so we don't need to worry about that.
738 * If the new file is a directory, its default ACL is a copy of
739 * the containing directory's default ACL.
742 xfs_acl_set_attr(vp
, pdaclp
, _ACL_TYPE_DEFAULT
, &error
);
743 if (!error
&& !basicperms
)
744 xfs_acl_set_attr(vp
, cacl
, _ACL_TYPE_ACCESS
, &error
);
750 * Set up the correct mode on the file based on the supplied ACL. This
751 * makes sure that the mode on the file reflects the state of the
752 * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
753 * the ACL is going to get the permissions for these entries, we must
754 * synchronize the mode whenever we set the ACL on a file.
764 xfs_acl_entry_t
*gap
= NULL
;
765 int i
, error
, nomask
= 1;
769 if (acl
->acl_cnt
== XFS_ACL_NOT_PRESENT
)
773 * Copy the u::, g::, o::, and m:: bits from the ACL into the
774 * mode. The m:: bits take precedence over the g:: bits.
776 va
.va_mask
= XFS_AT_MODE
;
777 error
= bhv_vop_getattr(vp
, &va
, 0, sys_cred
);
781 va
.va_mask
= XFS_AT_MODE
;
782 va
.va_mode
&= ~(S_IRWXU
|S_IRWXG
|S_IRWXO
);
784 for (i
= 0; i
< acl
->acl_cnt
; ++i
) {
785 switch (ap
->ae_tag
) {
787 va
.va_mode
|= ap
->ae_perm
<< 6;
792 case ACL_MASK
: /* more than just standard modes */
794 va
.va_mode
|= ap
->ae_perm
<< 3;
798 va
.va_mode
|= ap
->ae_perm
;
800 default: /* more than just standard modes */
807 /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
809 va
.va_mode
|= gap
->ae_perm
<< 3;
811 return bhv_vop_setattr(vp
, &va
, 0, sys_cred
);
815 * The permissions for the special ACL entries (u::, g::[m::], o::) are
816 * actually stored in the file mode (if there is both a group and a mask,
817 * the group is stored in the ACL entry and the mask is stored on the file).
818 * This allows the mode to remain automatically in sync with the ACL without
819 * the need for a call-back to the ACL system at every point where the mode
820 * could change. This function takes the permissions from the specified mode
821 * and places it in the supplied ACL.
823 * This implementation draws its validity from the fact that, when the ACL
824 * was assigned, the mode was copied from the ACL.
825 * If the mode did not change, therefore, the mode remains exactly what was
826 * taken from the special ACL entries at assignment.
827 * If a subsequent chmod() was done, the POSIX spec says that the change in
828 * mode must cause an update to the ACL seen at user level and used for
829 * access checks. Before and after a mode change, therefore, the file mode
830 * most accurately reflects what the special ACL entries should permit/deny.
832 * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
833 * the existing mode bits will override whatever is in the
834 * ACL. Similarly, if there is a pre-existing ACL that was
835 * never in sync with its mode (owing to a bug in 6.5 and
836 * before), it will now magically (or mystically) be
837 * synchronized. This could cause slight astonishment, but
838 * it is better than inconsistent permissions.
840 * The supplied ACL is a template that may contain any combination
841 * of special entries. These are treated as place holders when we fill
842 * out the ACL. This routine does not add or remove special entries, it
843 * simply unites each special entry with its associated set of permissions.
852 xfs_acl_entry_t
*gap
= NULL
;
855 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
856 * be set instead of the GROUP entry, if there is a MASK.
858 for (ap
= acl
->acl_entry
, i
= 0; i
< acl
->acl_cnt
; ap
++, i
++) {
859 switch (ap
->ae_tag
) {
861 ap
->ae_perm
= (mode
>> 6) & 0x7;
868 ap
->ae_perm
= (mode
>> 3) & 0x7;
871 ap
->ae_perm
= mode
& 0x7;
877 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
879 gap
->ae_perm
= (mode
>> 3) & 0x7;
883 * When inheriting an Access ACL from a directory Default ACL,
884 * the ACL bits are set to the intersection of the ACL default
885 * permission bits and the file permission bits in mode. If there
886 * are no permission bits on the file then we must not give them
887 * the ACL. This is what what makes umask() work with ACLs.
896 xfs_acl_entry_t
*gap
= NULL
;
899 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
900 * be merged with GROUP entry, if there is a MASK.
902 for (ap
= acl
->acl_entry
, i
= 0; i
< acl
->acl_cnt
; ap
++, i
++) {
903 switch (ap
->ae_tag
) {
905 ap
->ae_perm
&= (mode
>> 6) & 0x7;
912 ap
->ae_perm
&= (mode
>> 3) & 0x7;
915 ap
->ae_perm
&= mode
& 0x7;
921 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
923 gap
->ae_perm
&= (mode
>> 3) & 0x7;