2 * Copyright (c) 1999-2003 Robert N. M. Watson
5 * This software was developed by Robert Watson for the TrustedBSD Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Support for POSIX.1e access control lists: UFS-specific support functions.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 #include "opt_quota.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/types.h>
46 #include <sys/event.h>
47 #include <sys/extattr.h>
49 #include <ufs/ufs/quota.h>
50 #include <ufs/ufs/inode.h>
51 #include <ufs/ufs/acl.h>
52 #include <ufs/ufs/extattr.h>
53 #include <ufs/ufs/dir.h>
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ufs/ufs_extern.h>
56 #include <ufs/ffs/fs.h>
60 FEATURE(ufs_acl, "ACL support for UFS");
63 * Synchronize an ACL and an inode by copying over appropriate inode fields
64 * to the passed ACL. Assumes an ACL that would satisfy acl_posix1e_check(),
65 * and may panic if not.
68 ufs_sync_acl_from_inode(struct inode *ip, struct acl *acl)
70 struct acl_entry *acl_mask, *acl_group_obj;
74 * Update ACL_USER_OBJ, ACL_OTHER, but simply identify ACL_MASK
75 * and ACL_GROUP_OBJ for use after we know whether ACL_MASK is
80 for (i = 0; i < acl->acl_cnt; i++) {
81 switch (acl->acl_entry[i].ae_tag) {
83 acl->acl_entry[i].ae_perm = acl_posix1e_mode_to_perm(
84 ACL_USER_OBJ, ip->i_mode);
85 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
89 acl_group_obj = &acl->acl_entry[i];
90 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
94 acl->acl_entry[i].ae_perm = acl_posix1e_mode_to_perm(
95 ACL_OTHER, ip->i_mode);
96 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
100 acl_mask = &acl->acl_entry[i];
101 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
109 panic("ufs_sync_acl_from_inode(): bad ae_tag");
113 if (acl_group_obj == NULL)
114 panic("ufs_sync_acl_from_inode(): no ACL_GROUP_OBJ");
116 if (acl_mask == NULL) {
118 * There is no ACL_MASK, so update ACL_GROUP_OBJ.
120 acl_group_obj->ae_perm = acl_posix1e_mode_to_perm(
121 ACL_GROUP_OBJ, ip->i_mode);
124 * Update the ACL_MASK entry instead of ACL_GROUP_OBJ.
126 acl_mask->ae_perm = acl_posix1e_mode_to_perm(ACL_GROUP_OBJ,
132 * Calculate what the inode mode should look like based on an authoritative
133 * ACL for the inode. Replace only the fields in the inode that the ACL
137 ufs_sync_inode_from_acl(struct acl *acl, struct inode *ip)
140 ip->i_mode &= ACL_PRESERVE_MASK;
141 ip->i_mode |= acl_posix1e_acl_to_mode(acl);
142 DIP_SET(ip, i_mode, ip->i_mode);
146 * Retrieve NFSv4 ACL, skipping access checks. Must be used in UFS code
147 * instead of VOP_GETACL() when we don't want to be restricted by the user
148 * not having ACL_READ_ACL permission, e.g. when calculating inherited ACL
149 * or in ufs_vnops.c:ufs_accessx().
152 ufs_getacl_nfs4_internal(struct vnode *vp, struct acl *aclp, struct thread *td)
155 struct inode *ip = VTOI(vp);
160 error = vn_extattr_get(vp, IO_NODELOCKED,
161 NFS4_ACL_EXTATTR_NAMESPACE, NFS4_ACL_EXTATTR_NAME,
162 &len, (char *) aclp, td);
163 aclp->acl_maxcnt = ACL_MAX_ENTRIES;
164 if (error == ENOATTR) {
166 * Legitimately no ACL set on object, purely
167 * emulate it through the inode.
169 acl_nfs4_sync_acl_from_mode(aclp, ip->i_mode, ip->i_uid);
177 if (len != sizeof(*aclp)) {
179 * A short (or long) read, meaning that for
180 * some reason the ACL is corrupted. Return
181 * EPERM since the object DAC protections
184 printf("ufs_getacl_nfs4(): Loaded invalid ACL ("
185 "%d bytes), inumber %d on %s\n", len,
186 ip->i_number, ip->i_fs->fs_fsmnt);
191 error = acl_nfs4_check(aclp, vp->v_type == VDIR);
193 printf("ufs_getacl_nfs4(): Loaded invalid ACL "
194 "(failed acl_nfs4_check), inumber %d on %s\n",
195 ip->i_number, ip->i_fs->fs_fsmnt);
204 ufs_getacl_nfs4(struct vop_getacl_args *ap)
208 if ((ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS) == 0)
211 error = VOP_ACCESSX(ap->a_vp, VREAD_ACL, ap->a_td->td_ucred, ap->a_td);
215 error = ufs_getacl_nfs4_internal(ap->a_vp, ap->a_aclp, ap->a_td);
221 * Read POSIX.1e ACL from an EA. Return error if its not found
222 * or if any other error has occured.
225 ufs_get_oldacl(acl_type_t type, struct oldacl *old, struct vnode *vp,
229 struct inode *ip = VTOI(vp);
234 case ACL_TYPE_ACCESS:
235 error = vn_extattr_get(vp, IO_NODELOCKED,
236 POSIX1E_ACL_ACCESS_EXTATTR_NAMESPACE,
237 POSIX1E_ACL_ACCESS_EXTATTR_NAME, &len, (char *) old,
240 case ACL_TYPE_DEFAULT:
241 if (vp->v_type != VDIR)
243 error = vn_extattr_get(vp, IO_NODELOCKED,
244 POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE,
245 POSIX1E_ACL_DEFAULT_EXTATTR_NAME, &len, (char *) old,
255 if (len != sizeof(*old)) {
257 * A short (or long) read, meaning that for some reason
258 * the ACL is corrupted. Return EPERM since the object
259 * DAC protections are unsafe.
261 printf("ufs_get_oldacl(): Loaded invalid ACL "
262 "(len = %d), inumber %d on %s\n", len,
263 ip->i_number, ip->i_fs->fs_fsmnt);
271 * Retrieve the ACL on a file.
273 * As part of the ACL is stored in the inode, and the rest in an EA,
274 * assemble both into a final ACL product. Right now this is not done
278 ufs_getacl_posix1e(struct vop_getacl_args *ap)
280 struct inode *ip = VTOI(ap->a_vp);
285 * XXX: If ufs_getacl() should work on file systems not supporting
286 * ACLs, remove this check.
288 if ((ap->a_vp->v_mount->mnt_flag & MNT_ACLS) == 0)
291 old = malloc(sizeof(*old), M_ACL, M_WAITOK | M_ZERO);
294 * Attempt to retrieve the ACL from the extended attributes.
296 error = ufs_get_oldacl(ap->a_type, old, ap->a_vp, ap->a_td);
299 * XXX: If ufs_getacl() should work on filesystems
300 * without the EA configured, add case EOPNOTSUPP here.
303 switch (ap->a_type) {
304 case ACL_TYPE_ACCESS:
306 * Legitimately no ACL set on object, purely
307 * emulate it through the inode. These fields will
308 * be updated when the ACL is synchronized with
312 old->acl_entry[0].ae_tag = ACL_USER_OBJ;
313 old->acl_entry[0].ae_id = ACL_UNDEFINED_ID;
314 old->acl_entry[0].ae_perm = ACL_PERM_NONE;
315 old->acl_entry[1].ae_tag = ACL_GROUP_OBJ;
316 old->acl_entry[1].ae_id = ACL_UNDEFINED_ID;
317 old->acl_entry[1].ae_perm = ACL_PERM_NONE;
318 old->acl_entry[2].ae_tag = ACL_OTHER;
319 old->acl_entry[2].ae_id = ACL_UNDEFINED_ID;
320 old->acl_entry[2].ae_perm = ACL_PERM_NONE;
323 case ACL_TYPE_DEFAULT:
325 * Unlike ACL_TYPE_ACCESS, there is no relationship
326 * between the inode contents and the ACL, and it is
327 * therefore possible for the request for the ACL
328 * to fail since the ACL is undefined. In this
329 * situation, return success and an empty ACL,
330 * as required by POSIX.1e.
337 error = acl_copy_oldacl_into_acl(old, ap->a_aclp);
341 if (ap->a_type == ACL_TYPE_ACCESS)
342 ufs_sync_acl_from_inode(ip, ap->a_aclp);
353 struct vop_getacl_args /* {
362 if ((ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) == 0)
365 if (ap->a_type == ACL_TYPE_NFS4)
366 return (ufs_getacl_nfs4(ap));
368 return (ufs_getacl_posix1e(ap));
372 * Set NFSv4 ACL without doing any access checking. This is required
373 * e.g. by the UFS code that implements ACL inheritance, or from
374 * ufs_vnops.c:ufs_chmod(), as some of the checks have to be skipped
375 * in that case, and others are redundant.
378 ufs_setacl_nfs4_internal(struct vnode *vp, struct acl *aclp, struct thread *td)
382 struct inode *ip = VTOI(vp);
384 KASSERT(acl_nfs4_check(aclp, vp->v_type == VDIR) == 0,
385 ("invalid ACL passed to ufs_setacl_nfs4_internal"));
387 if (acl_nfs4_is_trivial(aclp, ip->i_uid)) {
388 error = vn_extattr_rm(vp, IO_NODELOCKED,
389 NFS4_ACL_EXTATTR_NAMESPACE, NFS4_ACL_EXTATTR_NAME, td);
392 * An attempt to remove ACL from a file that didn't have
393 * any extended entries is not an error.
395 if (error == ENOATTR)
399 error = vn_extattr_set(vp, IO_NODELOCKED,
400 NFS4_ACL_EXTATTR_NAMESPACE, NFS4_ACL_EXTATTR_NAME,
401 sizeof(*aclp), (char *) aclp, td);
405 * Map lack of attribute definition in UFS_EXTATTR into lack of
406 * support for ACLs on the filesystem.
408 if (error == ENOATTR)
416 acl_nfs4_sync_mode_from_acl(&mode, aclp);
418 ip->i_mode &= ACL_PRESERVE_MASK;
420 DIP_SET(ip, i_mode, ip->i_mode);
421 ip->i_flag |= IN_CHANGE;
423 VN_KNOTE_UNLOCKED(vp, NOTE_ATTRIB);
429 ufs_setacl_nfs4(struct vop_setacl_args *ap)
432 struct inode *ip = VTOI(ap->a_vp);
434 if ((ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS) == 0)
437 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
440 if (ap->a_aclp == NULL)
443 error = VOP_ACLCHECK(ap->a_vp, ap->a_type, ap->a_aclp, ap->a_cred,
449 * Authorize the ACL operation.
451 if (ip->i_flags & (IMMUTABLE | APPEND))
455 * Must hold VWRITE_ACL or have appropriate privilege.
457 if ((error = VOP_ACCESSX(ap->a_vp, VWRITE_ACL, ap->a_cred, ap->a_td)))
461 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
462 * Make sure it has enough room for that - splitting every entry
463 * into two and appending "canonical six" entries at the end.
465 if (ap->a_aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2)
468 error = ufs_setacl_nfs4_internal(ap->a_vp, ap->a_aclp, ap->a_td);
474 * Set the ACL on a file.
476 * As part of the ACL is stored in the inode, and the rest in an EA,
477 * this is necessarily non-atomic, and has complex authorization.
478 * As ufs_setacl() includes elements of ufs_chown() and ufs_chmod(),
479 * a fair number of different access checks may be required to go ahead
480 * with the operation at all.
483 ufs_setacl_posix1e(struct vop_setacl_args *ap)
485 struct inode *ip = VTOI(ap->a_vp);
489 if ((ap->a_vp->v_mount->mnt_flag & MNT_ACLS) == 0)
493 * If this is a set operation rather than a delete operation,
494 * invoke VOP_ACLCHECK() on the passed ACL to determine if it is
495 * valid for the target. This will include a check on ap->a_type.
497 if (ap->a_aclp != NULL) {
501 error = VOP_ACLCHECK(ap->a_vp, ap->a_type, ap->a_aclp,
502 ap->a_cred, ap->a_td);
508 * POSIX.1e allows only deletion of the default ACL on a
509 * directory (ACL_TYPE_DEFAULT).
511 if (ap->a_type != ACL_TYPE_DEFAULT)
513 if (ap->a_vp->v_type != VDIR)
517 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
521 * Authorize the ACL operation.
523 if (ip->i_flags & (IMMUTABLE | APPEND))
527 * Must hold VADMIN (be file owner) or have appropriate privilege.
529 if ((error = VOP_ACCESS(ap->a_vp, VADMIN, ap->a_cred, ap->a_td)))
533 case ACL_TYPE_ACCESS:
534 old = malloc(sizeof(*old), M_ACL, M_WAITOK | M_ZERO);
535 error = acl_copy_acl_into_oldacl(ap->a_aclp, old);
537 error = vn_extattr_set(ap->a_vp, IO_NODELOCKED,
538 POSIX1E_ACL_ACCESS_EXTATTR_NAMESPACE,
539 POSIX1E_ACL_ACCESS_EXTATTR_NAME, sizeof(*old),
540 (char *) old, ap->a_td);
545 case ACL_TYPE_DEFAULT:
546 if (ap->a_aclp == NULL) {
547 error = vn_extattr_rm(ap->a_vp, IO_NODELOCKED,
548 POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE,
549 POSIX1E_ACL_DEFAULT_EXTATTR_NAME, ap->a_td);
551 * Attempting to delete a non-present default ACL
552 * will return success for portability purposes.
555 * XXX: Note that since we can't distinguish
556 * "that EA is not supported" from "that EA is not
557 * defined", the success case here overlaps the
558 * the ENOATTR->EOPNOTSUPP case below.
560 if (error == ENOATTR)
563 old = malloc(sizeof(*old), M_ACL, M_WAITOK | M_ZERO);
564 error = acl_copy_acl_into_oldacl(ap->a_aclp, old);
566 error = vn_extattr_set(ap->a_vp, IO_NODELOCKED,
567 POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE,
568 POSIX1E_ACL_DEFAULT_EXTATTR_NAME,
569 sizeof(*old), (char *) old, ap->a_td);
579 * Map lack of attribute definition in UFS_EXTATTR into lack of
580 * support for ACLs on the filesystem.
582 if (error == ENOATTR)
587 if (ap->a_type == ACL_TYPE_ACCESS) {
589 * Now that the EA is successfully updated, update the
590 * inode and mark it as changed.
592 ufs_sync_inode_from_acl(ap->a_aclp, ip);
593 ip->i_flag |= IN_CHANGE;
596 VN_KNOTE_UNLOCKED(ap->a_vp, NOTE_ATTRIB);
602 struct vop_setacl_args /* {
610 if ((ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) == 0)
613 if (ap->a_type == ACL_TYPE_NFS4)
614 return (ufs_setacl_nfs4(ap));
616 return (ufs_setacl_posix1e(ap));
620 ufs_aclcheck_nfs4(struct vop_aclcheck_args *ap)
622 int is_directory = 0;
624 if ((ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS) == 0)
628 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
629 * Make sure it has enough room for that - splitting every entry
630 * into two and appending "canonical six" entries at the end.
632 if (ap->a_aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2)
635 if (ap->a_vp->v_type == VDIR)
638 return (acl_nfs4_check(ap->a_aclp, is_directory));
642 ufs_aclcheck_posix1e(struct vop_aclcheck_args *ap)
645 if ((ap->a_vp->v_mount->mnt_flag & MNT_ACLS) == 0)
649 * Verify we understand this type of ACL, and that it applies
650 * to this kind of object.
651 * Rely on the acl_posix1e_check() routine to verify the contents.
654 case ACL_TYPE_ACCESS:
657 case ACL_TYPE_DEFAULT:
658 if (ap->a_vp->v_type != VDIR)
666 if (ap->a_aclp->acl_cnt > OLDACL_MAX_ENTRIES)
669 return (acl_posix1e_check(ap->a_aclp));
673 * Check the validity of an ACL for a file.
677 struct vop_aclcheck_args /* {
686 if ((ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) == 0)
689 if (ap->a_type == ACL_TYPE_NFS4)
690 return (ufs_aclcheck_nfs4(ap));
692 return (ufs_aclcheck_posix1e(ap));
695 #endif /* !UFS_ACL */