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"
35 #include "xfs_vnodeops.h"
37 #include <linux/capability.h>
38 #include <linux/posix_acl_xattr.h>
40 STATIC
int xfs_acl_setmode(struct inode
*, 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(struct inode
*, xfs_acl_t
*, int, int, int *);
47 STATIC
void xfs_acl_set_attr(struct inode
*, xfs_acl_t
*, int, int *);
48 STATIC
int xfs_acl_allow_set(struct inode
*, 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(
75 if (!S_ISDIR(vp
->i_mode
))
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
;
221 if (!(_ACL_ALLOC(xfs_acl
))) {
225 memset(xfs_acl
, 0, sizeof(xfs_acl_t
));
227 flags
= ATTR_KERNOVAL
;
229 xfs_acl_get_attr(vp
, xfs_acl
, kind
, flags
, &error
);
234 error
= -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES
);
236 if (xfs_acl_invalid(xfs_acl
)) {
240 if (kind
== _ACL_TYPE_ACCESS
)
241 xfs_acl_sync_mode(XFS_I(vp
)->i_d
.di_mode
, xfs_acl
);
242 error
= -posix_acl_xfs_to_xattr(xfs_acl
, ext_acl
, size
);
257 error
= xfs_acl_allow_set(vp
, kind
);
259 error
= xfs_attr_remove(XFS_I(vp
),
260 kind
== _ACL_TYPE_DEFAULT
?
261 SGI_ACL_DEFAULT
: SGI_ACL_FILE
,
263 if (error
== ENOATTR
)
264 error
= 0; /* 'scool */
276 posix_acl_xattr_header
*ext_acl
= acl
;
279 int basicperms
= 0; /* more than std unix perms? */
284 if (!(_ACL_ALLOC(xfs_acl
)))
287 error
= posix_acl_xattr_to_xfs(ext_acl
, size
, xfs_acl
);
292 if (!xfs_acl
->acl_cnt
) {
297 error
= xfs_acl_allow_set(vp
, kind
);
299 /* Incoming ACL exists, set file mode based on its value */
300 if (!error
&& kind
== _ACL_TYPE_ACCESS
)
301 error
= xfs_acl_setmode(vp
, xfs_acl
, &basicperms
);
307 * If we have more than std unix permissions, set up the actual attr.
308 * Otherwise, delete any existing attr. This prevents us from
309 * having actual attrs for permissions that can be stored in the
310 * standard permission bits.
313 xfs_acl_set_attr(vp
, xfs_acl
, kind
, &error
);
315 error
= -xfs_acl_vremove(vp
, _ACL_TYPE_ACCESS
);
331 struct xfs_name acl_name
= {SGI_ACL_FILE
, SGI_ACL_FILE_SIZE
};
333 if (!(_ACL_ALLOC(acl
)))
336 /* If the file has no ACL return -1. */
337 rval
= sizeof(xfs_acl_t
);
338 if (xfs_attr_fetch(ip
, &acl_name
, (char *)acl
, &rval
, ATTR_ROOT
)) {
342 xfs_acl_get_endian(acl
);
344 /* If the file has an empty ACL return -1. */
345 if (acl
->acl_cnt
== XFS_ACL_NOT_PRESENT
) {
350 /* Synchronize ACL with mode bits */
351 xfs_acl_sync_mode(ip
->i_d
.di_mode
, acl
);
353 rval
= xfs_acl_access(ip
->i_d
.di_uid
, ip
->i_d
.di_gid
, acl
, mode
, cr
);
363 if (vp
->i_flags
& (S_IMMUTABLE
|S_APPEND
))
365 if (kind
== _ACL_TYPE_DEFAULT
&& !S_ISDIR(vp
->i_mode
))
367 if (vp
->i_sb
->s_flags
& MS_RDONLY
)
369 if (XFS_I(vp
)->i_d
.di_uid
!= current
->fsuid
&& !capable(CAP_FOWNER
))
375 * Note: cr is only used here for the capability check if the ACL test fails.
376 * It is not used to find out the credentials uid or groups etc, as was
377 * done in IRIX. It is assumed that the uid and groups for the current
378 * thread are taken from "current" instead of the cr parameter.
388 xfs_acl_entry_t matched
;
390 int maskallows
= -1; /* true, but not 1, either */
391 int seen_userobj
= 0;
393 matched
.ae_tag
= 0; /* Invalid type */
396 for (i
= 0; i
< fap
->acl_cnt
; i
++) {
398 * Break out if we've got a user_obj entry or
399 * a user entry and the mask (and have processed USER_OBJ)
401 if (matched
.ae_tag
== ACL_USER_OBJ
)
403 if (matched
.ae_tag
== ACL_USER
) {
404 if (maskallows
!= -1 && seen_userobj
)
406 if (fap
->acl_entry
[i
].ae_tag
!= ACL_MASK
&&
407 fap
->acl_entry
[i
].ae_tag
!= ACL_USER_OBJ
)
410 /* True if this entry allows the requested access */
411 allows
= ((fap
->acl_entry
[i
].ae_perm
& md
) == md
);
413 switch (fap
->acl_entry
[i
].ae_tag
) {
416 if (fuid
!= current
->fsuid
)
418 matched
.ae_tag
= ACL_USER_OBJ
;
419 matched
.ae_perm
= allows
;
422 if (fap
->acl_entry
[i
].ae_id
!= current
->fsuid
)
424 matched
.ae_tag
= ACL_USER
;
425 matched
.ae_perm
= allows
;
428 if ((matched
.ae_tag
== ACL_GROUP_OBJ
||
429 matched
.ae_tag
== ACL_GROUP
) && !allows
)
431 if (!in_group_p(fgid
))
433 matched
.ae_tag
= ACL_GROUP_OBJ
;
434 matched
.ae_perm
= allows
;
437 if ((matched
.ae_tag
== ACL_GROUP_OBJ
||
438 matched
.ae_tag
== ACL_GROUP
) && !allows
)
440 if (!in_group_p(fap
->acl_entry
[i
].ae_id
))
442 matched
.ae_tag
= ACL_GROUP
;
443 matched
.ae_perm
= allows
;
449 if (matched
.ae_tag
!= 0)
451 matched
.ae_tag
= ACL_OTHER
;
452 matched
.ae_perm
= allows
;
457 * First possibility is that no matched entry allows access.
458 * The capability to override DAC may exist, so check for it.
460 switch (matched
.ae_tag
) {
469 if (maskallows
&& matched
.ae_perm
)
476 /* EACCES tells generic_permission to check for capability overrides */
481 * ACL validity checker.
482 * This acl validation routine checks each ACL entry read in makes sense.
488 xfs_acl_entry_t
*entry
, *e
;
489 int user
= 0, group
= 0, other
= 0, mask
= 0;
490 int mask_required
= 0;
496 if (aclp
->acl_cnt
> XFS_ACL_MAX_ENTRIES
)
499 for (i
= 0; i
< aclp
->acl_cnt
; i
++) {
500 entry
= &aclp
->acl_entry
[i
];
501 switch (entry
->ae_tag
) {
516 for (j
= i
+ 1; j
< aclp
->acl_cnt
; j
++) {
517 e
= &aclp
->acl_entry
[j
];
518 if (e
->ae_id
== entry
->ae_id
&&
519 e
->ae_tag
== entry
->ae_tag
)
532 if (!user
|| !group
|| !other
|| (mask_required
&& !mask
))
541 * Do ACL endian conversion.
547 xfs_acl_entry_t
*ace
, *end
;
549 INT_SET(aclp
->acl_cnt
, ARCH_CONVERT
, aclp
->acl_cnt
);
550 end
= &aclp
->acl_entry
[0]+aclp
->acl_cnt
;
551 for (ace
= &aclp
->acl_entry
[0]; ace
< end
; ace
++) {
552 INT_SET(ace
->ae_tag
, ARCH_CONVERT
, ace
->ae_tag
);
553 INT_SET(ace
->ae_id
, ARCH_CONVERT
, ace
->ae_id
);
554 INT_SET(ace
->ae_perm
, ARCH_CONVERT
, ace
->ae_perm
);
559 * Get the ACL from the EA and do endian conversion.
569 int len
= sizeof(xfs_acl_t
);
571 ASSERT((flags
& ATTR_KERNOVAL
) ? (aclp
== NULL
) : 1);
573 *error
= xfs_attr_get(XFS_I(vp
),
574 kind
== _ACL_TYPE_ACCESS
?
575 SGI_ACL_FILE
: SGI_ACL_DEFAULT
,
576 (char *)aclp
, &len
, flags
);
577 if (*error
|| (flags
& ATTR_KERNOVAL
))
579 xfs_acl_get_endian(aclp
);
583 * Set the EA with the ACL and do endian conversion.
592 xfs_acl_entry_t
*ace
, *newace
, *end
;
596 if (!(_ACL_ALLOC(newacl
))) {
601 len
= sizeof(xfs_acl_t
) -
602 (sizeof(xfs_acl_entry_t
) * (XFS_ACL_MAX_ENTRIES
- aclp
->acl_cnt
));
603 end
= &aclp
->acl_entry
[0]+aclp
->acl_cnt
;
604 for (ace
= &aclp
->acl_entry
[0], newace
= &newacl
->acl_entry
[0];
607 INT_SET(newace
->ae_tag
, ARCH_CONVERT
, ace
->ae_tag
);
608 INT_SET(newace
->ae_id
, ARCH_CONVERT
, ace
->ae_id
);
609 INT_SET(newace
->ae_perm
, ARCH_CONVERT
, ace
->ae_perm
);
611 INT_SET(newacl
->acl_cnt
, ARCH_CONVERT
, aclp
->acl_cnt
);
612 *error
= xfs_attr_set(XFS_I(vp
),
613 kind
== _ACL_TYPE_ACCESS
?
614 SGI_ACL_FILE
: SGI_ACL_DEFAULT
,
615 (char *)newacl
, len
, ATTR_ROOT
);
622 xfs_acl_t
*access_acl
,
623 xfs_acl_t
*default_acl
)
629 * Get the Access ACL and the mode. If either cannot
630 * be obtained for some reason, invalidate the access ACL.
632 xfs_acl_get_attr(vp
, access_acl
, _ACL_TYPE_ACCESS
, 0, &error
);
634 access_acl
->acl_cnt
= XFS_ACL_NOT_PRESENT
;
635 else /* We have a good ACL and the file mode, synchronize. */
636 xfs_acl_sync_mode(XFS_I(vp
)->i_d
.di_mode
, access_acl
);
640 xfs_acl_get_attr(vp
, default_acl
, _ACL_TYPE_DEFAULT
, 0, &error
);
642 default_acl
->acl_cnt
= XFS_ACL_NOT_PRESENT
;
648 * This function retrieves the parent directory's acl, processes it
649 * and lets the child inherit the acl(s) that it should.
662 * If the parent does not have a default ACL, or it's an
663 * invalid ACL, we're done.
667 if (!pdaclp
|| xfs_acl_invalid(pdaclp
))
671 * Copy the default ACL of the containing directory to
672 * the access ACL of the new file and use the mode that
673 * was passed in to set up the correct initial values for
674 * the u::,g::[m::], and o:: entries. This is what makes
675 * umask() "work" with ACL's.
678 if (!(_ACL_ALLOC(cacl
)))
681 memcpy(cacl
, pdaclp
, sizeof(xfs_acl_t
));
682 xfs_acl_filter_mode(mode
, cacl
);
683 error
= xfs_acl_setmode(vp
, cacl
, &basicperms
);
688 * Set the Default and Access ACL on the file. The mode is already
689 * set on the file, so we don't need to worry about that.
691 * If the new file is a directory, its default ACL is a copy of
692 * the containing directory's default ACL.
694 if (S_ISDIR(vp
->i_mode
))
695 xfs_acl_set_attr(vp
, pdaclp
, _ACL_TYPE_DEFAULT
, &error
);
696 if (!error
&& !basicperms
)
697 xfs_acl_set_attr(vp
, cacl
, _ACL_TYPE_ACCESS
, &error
);
704 * Set up the correct mode on the file based on the supplied ACL. This
705 * makes sure that the mode on the file reflects the state of the
706 * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
707 * the ACL is going to get the permissions for these entries, we must
708 * synchronize the mode whenever we set the ACL on a file.
718 xfs_acl_entry_t
*gap
= NULL
;
723 if (acl
->acl_cnt
== XFS_ACL_NOT_PRESENT
)
727 * Copy the u::, g::, o::, and m:: bits from the ACL into the
728 * mode. The m:: bits take precedence over the g:: bits.
730 iattr
.ia_valid
= ATTR_MODE
;
731 iattr
.ia_mode
= XFS_I(vp
)->i_d
.di_mode
;
732 iattr
.ia_mode
&= ~(S_IRWXU
|S_IRWXG
|S_IRWXO
);
734 for (i
= 0; i
< acl
->acl_cnt
; ++i
) {
735 switch (ap
->ae_tag
) {
737 iattr
.ia_mode
|= ap
->ae_perm
<< 6;
742 case ACL_MASK
: /* more than just standard modes */
744 iattr
.ia_mode
|= ap
->ae_perm
<< 3;
748 iattr
.ia_mode
|= ap
->ae_perm
;
750 default: /* more than just standard modes */
757 /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
759 iattr
.ia_mode
|= gap
->ae_perm
<< 3;
761 return xfs_setattr(XFS_I(vp
), &iattr
, 0, sys_cred
);
765 * The permissions for the special ACL entries (u::, g::[m::], o::) are
766 * actually stored in the file mode (if there is both a group and a mask,
767 * the group is stored in the ACL entry and the mask is stored on the file).
768 * This allows the mode to remain automatically in sync with the ACL without
769 * the need for a call-back to the ACL system at every point where the mode
770 * could change. This function takes the permissions from the specified mode
771 * and places it in the supplied ACL.
773 * This implementation draws its validity from the fact that, when the ACL
774 * was assigned, the mode was copied from the ACL.
775 * If the mode did not change, therefore, the mode remains exactly what was
776 * taken from the special ACL entries at assignment.
777 * If a subsequent chmod() was done, the POSIX spec says that the change in
778 * mode must cause an update to the ACL seen at user level and used for
779 * access checks. Before and after a mode change, therefore, the file mode
780 * most accurately reflects what the special ACL entries should permit/deny.
782 * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
783 * the existing mode bits will override whatever is in the
784 * ACL. Similarly, if there is a pre-existing ACL that was
785 * never in sync with its mode (owing to a bug in 6.5 and
786 * before), it will now magically (or mystically) be
787 * synchronized. This could cause slight astonishment, but
788 * it is better than inconsistent permissions.
790 * The supplied ACL is a template that may contain any combination
791 * of special entries. These are treated as place holders when we fill
792 * out the ACL. This routine does not add or remove special entries, it
793 * simply unites each special entry with its associated set of permissions.
802 xfs_acl_entry_t
*gap
= NULL
;
805 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
806 * be set instead of the GROUP entry, if there is a MASK.
808 for (ap
= acl
->acl_entry
, i
= 0; i
< acl
->acl_cnt
; ap
++, i
++) {
809 switch (ap
->ae_tag
) {
811 ap
->ae_perm
= (mode
>> 6) & 0x7;
818 ap
->ae_perm
= (mode
>> 3) & 0x7;
821 ap
->ae_perm
= mode
& 0x7;
827 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
829 gap
->ae_perm
= (mode
>> 3) & 0x7;
833 * When inheriting an Access ACL from a directory Default ACL,
834 * the ACL bits are set to the intersection of the ACL default
835 * permission bits and the file permission bits in mode. If there
836 * are no permission bits on the file then we must not give them
837 * the ACL. This is what what makes umask() work with ACLs.
846 xfs_acl_entry_t
*gap
= NULL
;
849 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
850 * be merged with GROUP entry, if there is a MASK.
852 for (ap
= acl
->acl_entry
, i
= 0; i
< acl
->acl_cnt
; ap
++, i
++) {
853 switch (ap
->ae_tag
) {
855 ap
->ae_perm
&= (mode
>> 6) & 0x7;
862 ap
->ae_perm
&= (mode
>> 3) & 0x7;
865 ap
->ae_perm
&= mode
& 0x7;
871 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
873 gap
->ae_perm
&= (mode
>> 3) & 0x7;