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"
26 #include "xfs_bmap_btree.h"
27 #include "xfs_alloc_btree.h"
28 #include "xfs_ialloc_btree.h"
29 #include "xfs_dir_sf.h"
30 #include "xfs_dir2_sf.h"
31 #include "xfs_attr_sf.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_btree.h"
39 #include <linux/posix_acl_xattr.h>
41 STATIC
int xfs_acl_setmode(vnode_t
*, xfs_acl_t
*, int *);
42 STATIC
void xfs_acl_filter_mode(mode_t
, xfs_acl_t
*);
43 STATIC
void xfs_acl_get_endian(xfs_acl_t
*);
44 STATIC
int xfs_acl_access(uid_t
, gid_t
, xfs_acl_t
*, mode_t
, cred_t
*);
45 STATIC
int xfs_acl_invalid(xfs_acl_t
*);
46 STATIC
void xfs_acl_sync_mode(mode_t
, xfs_acl_t
*);
47 STATIC
void xfs_acl_get_attr(vnode_t
*, xfs_acl_t
*, int, int, int *);
48 STATIC
void xfs_acl_set_attr(vnode_t
*, xfs_acl_t
*, int, int *);
49 STATIC
int xfs_acl_allow_set(vnode_t
*, int);
51 kmem_zone_t
*xfs_acl_zone
;
55 * Test for existence of access ACL attribute as efficiently as possible.
58 xfs_acl_vhasacl_access(
63 xfs_acl_get_attr(vp
, NULL
, _ACL_TYPE_ACCESS
, ATTR_KERNOVAL
, &error
);
68 * Test for existence of default ACL attribute as efficiently as possible.
71 xfs_acl_vhasacl_default(
78 xfs_acl_get_attr(vp
, NULL
, _ACL_TYPE_DEFAULT
, ATTR_KERNOVAL
, &error
);
83 * Convert from extended attribute representation to in-memory for XFS.
86 posix_acl_xattr_to_xfs(
87 posix_acl_xattr_header
*src
,
91 posix_acl_xattr_entry
*src_entry
;
92 xfs_acl_entry_t
*dest_entry
;
98 if (size
< sizeof(posix_acl_xattr_header
))
101 if (src
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
104 memset(dest
, 0, sizeof(xfs_acl_t
));
105 dest
->acl_cnt
= posix_acl_xattr_count(size
);
106 if (dest
->acl_cnt
< 0 || dest
->acl_cnt
> XFS_ACL_MAX_ENTRIES
)
110 * acl_set_file(3) may request that we set default ACLs with
111 * zero length -- defend (gracefully) against that here.
116 src_entry
= (posix_acl_xattr_entry
*)((char *)src
+ sizeof(*src
));
117 dest_entry
= &dest
->acl_entry
[0];
119 for (n
= 0; n
< dest
->acl_cnt
; n
++, src_entry
++, dest_entry
++) {
120 dest_entry
->ae_perm
= le16_to_cpu(src_entry
->e_perm
);
121 if (_ACL_PERM_INVALID(dest_entry
->ae_perm
))
123 dest_entry
->ae_tag
= le16_to_cpu(src_entry
->e_tag
);
124 switch(dest_entry
->ae_tag
) {
127 dest_entry
->ae_id
= le32_to_cpu(src_entry
->e_id
);
133 dest_entry
->ae_id
= ACL_UNDEFINED_ID
;
139 if (xfs_acl_invalid(dest
))
146 * Comparison function called from xfs_sort().
147 * Primary key is ae_tag, secondary key is ae_id.
150 xfs_acl_entry_compare(
154 xfs_acl_entry_t
*a
= (xfs_acl_entry_t
*)va
,
155 *b
= (xfs_acl_entry_t
*)vb
;
157 if (a
->ae_tag
== b
->ae_tag
)
158 return (a
->ae_id
- b
->ae_id
);
159 return (a
->ae_tag
- b
->ae_tag
);
163 * Convert from in-memory XFS to extended attribute representation.
166 posix_acl_xfs_to_xattr(
168 posix_acl_xattr_header
*dest
,
172 size_t new_size
= posix_acl_xattr_size(src
->acl_cnt
);
173 posix_acl_xattr_entry
*dest_entry
;
174 xfs_acl_entry_t
*src_entry
;
179 /* Need to sort src XFS ACL by <ae_tag,ae_id> */
180 xfs_sort(src
->acl_entry
, src
->acl_cnt
, sizeof(src
->acl_entry
[0]),
181 xfs_acl_entry_compare
);
183 dest
->a_version
= cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
184 dest_entry
= &dest
->a_entries
[0];
185 src_entry
= &src
->acl_entry
[0];
186 for (n
= 0; n
< src
->acl_cnt
; n
++, dest_entry
++, src_entry
++) {
187 dest_entry
->e_perm
= cpu_to_le16(src_entry
->ae_perm
);
188 if (_ACL_PERM_INVALID(src_entry
->ae_perm
))
190 dest_entry
->e_tag
= cpu_to_le16(src_entry
->ae_tag
);
191 switch (src_entry
->ae_tag
) {
194 dest_entry
->e_id
= cpu_to_le32(src_entry
->ae_id
);
200 dest_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
217 xfs_acl_t
*xfs_acl
= NULL
;
218 posix_acl_xattr_header
*ext_acl
= acl
;
223 if (!(_ACL_ALLOC(xfs_acl
))) {
227 memset(xfs_acl
, 0, sizeof(xfs_acl_t
));
229 flags
= ATTR_KERNOVAL
;
231 xfs_acl_get_attr(vp
, xfs_acl
, kind
, flags
, &error
);
236 error
= -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES
);
238 if (xfs_acl_invalid(xfs_acl
)) {
242 if (kind
== _ACL_TYPE_ACCESS
) {
245 va
.va_mask
= XFS_AT_MODE
;
246 VOP_GETATTR(vp
, &va
, 0, sys_cred
, error
);
249 xfs_acl_sync_mode(va
.va_mode
, xfs_acl
);
251 error
= -posix_acl_xfs_to_xattr(xfs_acl
, ext_acl
, size
);
268 error
= xfs_acl_allow_set(vp
, kind
);
270 VOP_ATTR_REMOVE(vp
, kind
== _ACL_TYPE_DEFAULT
?
271 SGI_ACL_DEFAULT
: SGI_ACL_FILE
,
272 ATTR_ROOT
, sys_cred
, error
);
273 if (error
== ENOATTR
)
274 error
= 0; /* 'scool */
287 posix_acl_xattr_header
*ext_acl
= acl
;
290 int basicperms
= 0; /* more than std unix perms? */
295 if (!(_ACL_ALLOC(xfs_acl
)))
298 error
= posix_acl_xattr_to_xfs(ext_acl
, size
, xfs_acl
);
303 if (!xfs_acl
->acl_cnt
) {
309 error
= xfs_acl_allow_set(vp
, kind
);
313 /* Incoming ACL exists, set file mode based on its value */
314 if (kind
== _ACL_TYPE_ACCESS
)
315 xfs_acl_setmode(vp
, xfs_acl
, &basicperms
);
318 * If we have more than std unix permissions, set up the actual attr.
319 * Otherwise, delete any existing attr. This prevents us from
320 * having actual attrs for permissions that can be stored in the
321 * standard permission bits.
324 xfs_acl_set_attr(vp
, xfs_acl
, kind
, &error
);
326 xfs_acl_vremove(vp
, _ACL_TYPE_ACCESS
);
344 if (!(_ACL_ALLOC(acl
)))
347 /* If the file has no ACL return -1. */
348 rval
= sizeof(xfs_acl_t
);
349 if (xfs_attr_fetch(ip
, SGI_ACL_FILE
, SGI_ACL_FILE_SIZE
,
350 (char *)acl
, &rval
, ATTR_ROOT
| ATTR_KERNACCESS
, cr
)) {
354 xfs_acl_get_endian(acl
);
356 /* If the file has an empty ACL return -1. */
357 if (acl
->acl_cnt
== XFS_ACL_NOT_PRESENT
) {
362 /* Synchronize ACL with mode bits */
363 xfs_acl_sync_mode(ip
->i_d
.di_mode
, acl
);
365 rval
= xfs_acl_access(ip
->i_d
.di_uid
, ip
->i_d
.di_gid
, acl
, mode
, cr
);
378 if (vp
->v_inode
.i_flags
& (S_IMMUTABLE
|S_APPEND
))
380 if (kind
== _ACL_TYPE_DEFAULT
&& !VN_ISDIR(vp
))
382 if (vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
)
384 va
.va_mask
= XFS_AT_UID
;
385 VOP_GETATTR(vp
, &va
, 0, NULL
, error
);
388 if (va
.va_uid
!= current
->fsuid
&& !capable(CAP_FOWNER
))
394 * The access control process to determine the access permission:
395 * if uid == file owner id, use the file owner bits.
396 * if gid == file owner group id, use the file group bits.
397 * scan ACL for a maching user or group, and use matched entry
398 * permission. Use total permissions of all matching group entries,
399 * until all acl entries are exhausted. The final permission produced
400 * by matching acl entry or entries needs to be & with group permission.
401 * if not owner, owning group, or matching entry in ACL, use file
405 xfs_acl_capability_check(
409 if ((mode
& ACL_READ
) && !capable_cred(cr
, CAP_DAC_READ_SEARCH
))
411 if ((mode
& ACL_WRITE
) && !capable_cred(cr
, CAP_DAC_OVERRIDE
))
413 if ((mode
& ACL_EXECUTE
) && !capable_cred(cr
, CAP_DAC_OVERRIDE
))
420 * Note: cr is only used here for the capability check if the ACL test fails.
421 * It is not used to find out the credentials uid or groups etc, as was
422 * done in IRIX. It is assumed that the uid and groups for the current
423 * thread are taken from "current" instead of the cr parameter.
433 xfs_acl_entry_t matched
;
435 int maskallows
= -1; /* true, but not 1, either */
436 int seen_userobj
= 0;
438 matched
.ae_tag
= 0; /* Invalid type */
440 md
>>= 6; /* Normalize the bits for comparison */
442 for (i
= 0; i
< fap
->acl_cnt
; i
++) {
444 * Break out if we've got a user_obj entry or
445 * a user entry and the mask (and have processed USER_OBJ)
447 if (matched
.ae_tag
== ACL_USER_OBJ
)
449 if (matched
.ae_tag
== ACL_USER
) {
450 if (maskallows
!= -1 && seen_userobj
)
452 if (fap
->acl_entry
[i
].ae_tag
!= ACL_MASK
&&
453 fap
->acl_entry
[i
].ae_tag
!= ACL_USER_OBJ
)
456 /* True if this entry allows the requested access */
457 allows
= ((fap
->acl_entry
[i
].ae_perm
& md
) == md
);
459 switch (fap
->acl_entry
[i
].ae_tag
) {
462 if (fuid
!= current
->fsuid
)
464 matched
.ae_tag
= ACL_USER_OBJ
;
465 matched
.ae_perm
= allows
;
468 if (fap
->acl_entry
[i
].ae_id
!= current
->fsuid
)
470 matched
.ae_tag
= ACL_USER
;
471 matched
.ae_perm
= allows
;
474 if ((matched
.ae_tag
== ACL_GROUP_OBJ
||
475 matched
.ae_tag
== ACL_GROUP
) && !allows
)
477 if (!in_group_p(fgid
))
479 matched
.ae_tag
= ACL_GROUP_OBJ
;
480 matched
.ae_perm
= allows
;
483 if ((matched
.ae_tag
== ACL_GROUP_OBJ
||
484 matched
.ae_tag
== ACL_GROUP
) && !allows
)
486 if (!in_group_p(fap
->acl_entry
[i
].ae_id
))
488 matched
.ae_tag
= ACL_GROUP
;
489 matched
.ae_perm
= allows
;
495 if (matched
.ae_tag
!= 0)
497 matched
.ae_tag
= ACL_OTHER
;
498 matched
.ae_perm
= allows
;
503 * First possibility is that no matched entry allows access.
504 * The capability to override DAC may exist, so check for it.
506 switch (matched
.ae_tag
) {
515 if (maskallows
&& matched
.ae_perm
)
522 return xfs_acl_capability_check(md
, cr
);
526 * ACL validity checker.
527 * This acl validation routine checks each ACL entry read in makes sense.
533 xfs_acl_entry_t
*entry
, *e
;
534 int user
= 0, group
= 0, other
= 0, mask
= 0;
535 int mask_required
= 0;
541 if (aclp
->acl_cnt
> XFS_ACL_MAX_ENTRIES
)
544 for (i
= 0; i
< aclp
->acl_cnt
; i
++) {
545 entry
= &aclp
->acl_entry
[i
];
546 switch (entry
->ae_tag
) {
561 for (j
= i
+ 1; j
< aclp
->acl_cnt
; j
++) {
562 e
= &aclp
->acl_entry
[j
];
563 if (e
->ae_id
== entry
->ae_id
&&
564 e
->ae_tag
== entry
->ae_tag
)
577 if (!user
|| !group
|| !other
|| (mask_required
&& !mask
))
586 * Do ACL endian conversion.
592 xfs_acl_entry_t
*ace
, *end
;
594 INT_SET(aclp
->acl_cnt
, ARCH_CONVERT
, aclp
->acl_cnt
);
595 end
= &aclp
->acl_entry
[0]+aclp
->acl_cnt
;
596 for (ace
= &aclp
->acl_entry
[0]; ace
< end
; ace
++) {
597 INT_SET(ace
->ae_tag
, ARCH_CONVERT
, ace
->ae_tag
);
598 INT_SET(ace
->ae_id
, ARCH_CONVERT
, ace
->ae_id
);
599 INT_SET(ace
->ae_perm
, ARCH_CONVERT
, ace
->ae_perm
);
604 * Get the ACL from the EA and do endian conversion.
614 int len
= sizeof(xfs_acl_t
);
616 ASSERT((flags
& ATTR_KERNOVAL
) ? (aclp
== NULL
) : 1);
619 kind
== _ACL_TYPE_ACCESS
? SGI_ACL_FILE
: SGI_ACL_DEFAULT
,
620 (char *)aclp
, &len
, flags
, sys_cred
, *error
);
621 if (*error
|| (flags
& ATTR_KERNOVAL
))
623 xfs_acl_get_endian(aclp
);
627 * Set the EA with the ACL and do endian conversion.
636 xfs_acl_entry_t
*ace
, *newace
, *end
;
640 if (!(_ACL_ALLOC(newacl
))) {
645 len
= sizeof(xfs_acl_t
) -
646 (sizeof(xfs_acl_entry_t
) * (XFS_ACL_MAX_ENTRIES
- aclp
->acl_cnt
));
647 end
= &aclp
->acl_entry
[0]+aclp
->acl_cnt
;
648 for (ace
= &aclp
->acl_entry
[0], newace
= &newacl
->acl_entry
[0];
651 INT_SET(newace
->ae_tag
, ARCH_CONVERT
, ace
->ae_tag
);
652 INT_SET(newace
->ae_id
, ARCH_CONVERT
, ace
->ae_id
);
653 INT_SET(newace
->ae_perm
, ARCH_CONVERT
, ace
->ae_perm
);
655 INT_SET(newacl
->acl_cnt
, ARCH_CONVERT
, aclp
->acl_cnt
);
657 kind
== _ACL_TYPE_ACCESS
? SGI_ACL_FILE
: SGI_ACL_DEFAULT
,
658 (char *)newacl
, len
, ATTR_ROOT
, sys_cred
, *error
);
665 xfs_acl_t
*access_acl
,
666 xfs_acl_t
*default_acl
)
673 * Get the Access ACL and the mode. If either cannot
674 * be obtained for some reason, invalidate the access ACL.
676 xfs_acl_get_attr(vp
, access_acl
, _ACL_TYPE_ACCESS
, 0, &error
);
678 /* Got the ACL, need the mode... */
679 va
.va_mask
= XFS_AT_MODE
;
680 VOP_GETATTR(vp
, &va
, 0, sys_cred
, error
);
684 access_acl
->acl_cnt
= XFS_ACL_NOT_PRESENT
;
685 else /* We have a good ACL and the file mode, synchronize. */
686 xfs_acl_sync_mode(va
.va_mode
, access_acl
);
690 xfs_acl_get_attr(vp
, default_acl
, _ACL_TYPE_DEFAULT
, 0, &error
);
692 default_acl
->acl_cnt
= XFS_ACL_NOT_PRESENT
;
698 * This function retrieves the parent directory's acl, processes it
699 * and lets the child inherit the acl(s) that it should.
712 * If the parent does not have a default ACL, or it's an
713 * invalid ACL, we're done.
717 if (!pdaclp
|| xfs_acl_invalid(pdaclp
))
721 * Copy the default ACL of the containing directory to
722 * the access ACL of the new file and use the mode that
723 * was passed in to set up the correct initial values for
724 * the u::,g::[m::], and o:: entries. This is what makes
725 * umask() "work" with ACL's.
728 if (!(_ACL_ALLOC(cacl
)))
731 memcpy(cacl
, pdaclp
, sizeof(xfs_acl_t
));
732 xfs_acl_filter_mode(vap
->va_mode
, cacl
);
733 xfs_acl_setmode(vp
, cacl
, &basicperms
);
736 * Set the Default and Access ACL on the file. The mode is already
737 * set on the file, so we don't need to worry about that.
739 * If the new file is a directory, its default ACL is a copy of
740 * the containing directory's default ACL.
743 xfs_acl_set_attr(vp
, pdaclp
, _ACL_TYPE_DEFAULT
, &error
);
744 if (!error
&& !basicperms
)
745 xfs_acl_set_attr(vp
, cacl
, _ACL_TYPE_ACCESS
, &error
);
751 * Set up the correct mode on the file based on the supplied ACL. This
752 * makes sure that the mode on the file reflects the state of the
753 * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
754 * the ACL is going to get the permissions for these entries, we must
755 * synchronize the mode whenever we set the ACL on a file.
765 xfs_acl_entry_t
*gap
= NULL
;
766 int i
, error
, nomask
= 1;
770 if (acl
->acl_cnt
== XFS_ACL_NOT_PRESENT
)
774 * Copy the u::, g::, o::, and m:: bits from the ACL into the
775 * mode. The m:: bits take precedence over the g:: bits.
777 va
.va_mask
= XFS_AT_MODE
;
778 VOP_GETATTR(vp
, &va
, 0, sys_cred
, error
);
782 va
.va_mask
= XFS_AT_MODE
;
783 va
.va_mode
&= ~(S_IRWXU
|S_IRWXG
|S_IRWXO
);
785 for (i
= 0; i
< acl
->acl_cnt
; ++i
) {
786 switch (ap
->ae_tag
) {
788 va
.va_mode
|= ap
->ae_perm
<< 6;
793 case ACL_MASK
: /* more than just standard modes */
795 va
.va_mode
|= ap
->ae_perm
<< 3;
799 va
.va_mode
|= ap
->ae_perm
;
801 default: /* more than just standard modes */
808 /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
810 va
.va_mode
|= gap
->ae_perm
<< 3;
812 VOP_SETATTR(vp
, &va
, 0, sys_cred
, error
);
817 * The permissions for the special ACL entries (u::, g::[m::], o::) are
818 * actually stored in the file mode (if there is both a group and a mask,
819 * the group is stored in the ACL entry and the mask is stored on the file).
820 * This allows the mode to remain automatically in sync with the ACL without
821 * the need for a call-back to the ACL system at every point where the mode
822 * could change. This function takes the permissions from the specified mode
823 * and places it in the supplied ACL.
825 * This implementation draws its validity from the fact that, when the ACL
826 * was assigned, the mode was copied from the ACL.
827 * If the mode did not change, therefore, the mode remains exactly what was
828 * taken from the special ACL entries at assignment.
829 * If a subsequent chmod() was done, the POSIX spec says that the change in
830 * mode must cause an update to the ACL seen at user level and used for
831 * access checks. Before and after a mode change, therefore, the file mode
832 * most accurately reflects what the special ACL entries should permit/deny.
834 * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
835 * the existing mode bits will override whatever is in the
836 * ACL. Similarly, if there is a pre-existing ACL that was
837 * never in sync with its mode (owing to a bug in 6.5 and
838 * before), it will now magically (or mystically) be
839 * synchronized. This could cause slight astonishment, but
840 * it is better than inconsistent permissions.
842 * The supplied ACL is a template that may contain any combination
843 * of special entries. These are treated as place holders when we fill
844 * out the ACL. This routine does not add or remove special entries, it
845 * simply unites each special entry with its associated set of permissions.
854 xfs_acl_entry_t
*gap
= NULL
;
857 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
858 * be set instead of the GROUP entry, if there is a MASK.
860 for (ap
= acl
->acl_entry
, i
= 0; i
< acl
->acl_cnt
; ap
++, i
++) {
861 switch (ap
->ae_tag
) {
863 ap
->ae_perm
= (mode
>> 6) & 0x7;
870 ap
->ae_perm
= (mode
>> 3) & 0x7;
873 ap
->ae_perm
= mode
& 0x7;
879 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
881 gap
->ae_perm
= (mode
>> 3) & 0x7;
885 * When inheriting an Access ACL from a directory Default ACL,
886 * the ACL bits are set to the intersection of the ACL default
887 * permission bits and the file permission bits in mode. If there
888 * are no permission bits on the file then we must not give them
889 * the ACL. This is what what makes umask() work with ACLs.
898 xfs_acl_entry_t
*gap
= NULL
;
901 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
902 * be merged with GROUP entry, if there is a MASK.
904 for (ap
= acl
->acl_entry
, i
= 0; i
< acl
->acl_cnt
; ap
++, i
++) {
905 switch (ap
->ae_tag
) {
907 ap
->ae_perm
&= (mode
>> 6) & 0x7;
914 ap
->ae_perm
&= (mode
>> 3) & 0x7;
917 ap
->ae_perm
&= mode
& 0x7;
923 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
925 gap
->ae_perm
&= (mode
>> 3) & 0x7;