2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999-2003 Robert N. M. Watson
7 * This software was developed by Robert Watson for the TrustedBSD Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Support for POSIX.1e access control lists: UFS-specific support functions.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 #include "opt_quota.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/mount.h>
45 #include <sys/vnode.h>
46 #include <sys/types.h>
48 #include <sys/event.h>
49 #include <sys/extattr.h>
52 #include <ufs/ufs/quota.h>
53 #include <ufs/ufs/inode.h>
54 #include <ufs/ufs/acl.h>
55 #include <ufs/ufs/extattr.h>
56 #include <ufs/ufs/dir.h>
57 #include <ufs/ufs/ufsmount.h>
58 #include <ufs/ufs/ufs_extern.h>
59 #include <ufs/ffs/fs.h>
63 FEATURE(ufs_acl, "ACL support for UFS");
66 * Synchronize an ACL and an inode by copying over appropriate inode fields
67 * to the passed ACL. Assumes an ACL that would satisfy acl_posix1e_check(),
68 * and may panic if not.
71 ufs_sync_acl_from_inode(struct inode *ip, struct acl *acl)
73 struct acl_entry *acl_mask, *acl_group_obj;
77 * Update ACL_USER_OBJ, ACL_OTHER, but simply identify ACL_MASK
78 * and ACL_GROUP_OBJ for use after we know whether ACL_MASK is
83 for (i = 0; i < acl->acl_cnt; i++) {
84 switch (acl->acl_entry[i].ae_tag) {
86 acl->acl_entry[i].ae_perm = acl_posix1e_mode_to_perm(
87 ACL_USER_OBJ, ip->i_mode);
88 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
92 acl_group_obj = &acl->acl_entry[i];
93 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
97 acl->acl_entry[i].ae_perm = acl_posix1e_mode_to_perm(
98 ACL_OTHER, ip->i_mode);
99 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
103 acl_mask = &acl->acl_entry[i];
104 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
112 panic("ufs_sync_acl_from_inode(): bad ae_tag");
116 if (acl_group_obj == NULL)
117 panic("ufs_sync_acl_from_inode(): no ACL_GROUP_OBJ");
119 if (acl_mask == NULL) {
121 * There is no ACL_MASK, so update ACL_GROUP_OBJ.
123 acl_group_obj->ae_perm = acl_posix1e_mode_to_perm(
124 ACL_GROUP_OBJ, ip->i_mode);
127 * Update the ACL_MASK entry instead of ACL_GROUP_OBJ.
129 acl_mask->ae_perm = acl_posix1e_mode_to_perm(ACL_GROUP_OBJ,
135 * Calculate what the inode mode should look like based on an authoritative
136 * ACL for the inode. Replace only the fields in the inode that the ACL
140 ufs_sync_inode_from_acl(struct acl *acl, struct inode *ip)
144 newmode = ip->i_mode & ACL_PRESERVE_MASK;
145 newmode |= acl_posix1e_acl_to_mode(acl);
146 UFS_INODE_SET_MODE(ip, newmode);
147 DIP_SET(ip, i_mode, ip->i_mode);
151 * Retrieve NFSv4 ACL, skipping access checks. Must be used in UFS code
152 * instead of VOP_GETACL() when we don't want to be restricted by the user
153 * not having ACL_READ_ACL permission, e.g. when calculating inherited ACL
154 * or in ufs_vnops.c:ufs_accessx().
157 ufs_getacl_nfs4_internal(struct vnode *vp, struct acl *aclp, struct thread *td)
160 struct inode *ip = VTOI(vp);
165 error = vn_extattr_get(vp, IO_NODELOCKED,
166 NFS4_ACL_EXTATTR_NAMESPACE, NFS4_ACL_EXTATTR_NAME,
167 &len, (char *) aclp, td);
168 aclp->acl_maxcnt = ACL_MAX_ENTRIES;
169 if (error == ENOATTR) {
171 * Legitimately no ACL set on object, purely
172 * emulate it through the inode.
174 acl_nfs4_sync_acl_from_mode(aclp, ip->i_mode, ip->i_uid);
182 if (len != sizeof(*aclp)) {
184 * A short (or long) read, meaning that for
185 * some reason the ACL is corrupted. Return
186 * EPERM since the object DAC protections
189 printf("ufs_getacl_nfs4(): Loaded invalid ACL ("
190 "%d bytes), inumber %ju on %s\n", len,
191 (uintmax_t)ip->i_number, ITOFS(ip)->fs_fsmnt);
196 error = acl_nfs4_check(aclp, vp->v_type == VDIR);
198 printf("ufs_getacl_nfs4(): Loaded invalid ACL "
199 "(failed acl_nfs4_check), inumber %ju on %s\n",
200 (uintmax_t)ip->i_number, ITOFS(ip)->fs_fsmnt);
209 ufs_getacl_nfs4(struct vop_getacl_args *ap)
213 if ((ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS) == 0)
216 error = VOP_ACCESSX(ap->a_vp, VREAD_ACL, ap->a_td->td_ucred, ap->a_td);
220 error = ufs_getacl_nfs4_internal(ap->a_vp, ap->a_aclp, ap->a_td);
226 * Read POSIX.1e ACL from an EA. Return error if its not found
227 * or if any other error has occurred.
230 ufs_get_oldacl(acl_type_t type, struct oldacl *old, struct vnode *vp,
234 struct inode *ip = VTOI(vp);
239 case ACL_TYPE_ACCESS:
240 error = vn_extattr_get(vp, IO_NODELOCKED,
241 POSIX1E_ACL_ACCESS_EXTATTR_NAMESPACE,
242 POSIX1E_ACL_ACCESS_EXTATTR_NAME, &len, (char *) old,
245 case ACL_TYPE_DEFAULT:
246 if (vp->v_type != VDIR)
248 error = vn_extattr_get(vp, IO_NODELOCKED,
249 POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE,
250 POSIX1E_ACL_DEFAULT_EXTATTR_NAME, &len, (char *) old,
260 if (len != sizeof(*old)) {
262 * A short (or long) read, meaning that for some reason
263 * the ACL is corrupted. Return EPERM since the object
264 * DAC protections are unsafe.
266 printf("ufs_get_oldacl(): Loaded invalid ACL "
267 "(len = %d), inumber %ju on %s\n", len,
268 (uintmax_t)ip->i_number, ITOFS(ip)->fs_fsmnt);
276 * Retrieve the ACL on a file.
278 * As part of the ACL is stored in the inode, and the rest in an EA,
279 * assemble both into a final ACL product. Right now this is not done
283 ufs_getacl_posix1e(struct vop_getacl_args *ap)
285 struct inode *ip = VTOI(ap->a_vp);
290 * XXX: If ufs_getacl() should work on file systems not supporting
291 * ACLs, remove this check.
293 if ((ap->a_vp->v_mount->mnt_flag & MNT_ACLS) == 0)
296 old = malloc(sizeof(*old), M_ACL, M_WAITOK | M_ZERO);
299 * Attempt to retrieve the ACL from the extended attributes.
301 error = ufs_get_oldacl(ap->a_type, old, ap->a_vp, ap->a_td);
304 * XXX: If ufs_getacl() should work on filesystems
305 * without the EA configured, add case EOPNOTSUPP here.
308 switch (ap->a_type) {
309 case ACL_TYPE_ACCESS:
311 * Legitimately no ACL set on object, purely
312 * emulate it through the inode. These fields will
313 * be updated when the ACL is synchronized with
317 old->acl_entry[0].ae_tag = ACL_USER_OBJ;
318 old->acl_entry[0].ae_id = ACL_UNDEFINED_ID;
319 old->acl_entry[0].ae_perm = ACL_PERM_NONE;
320 old->acl_entry[1].ae_tag = ACL_GROUP_OBJ;
321 old->acl_entry[1].ae_id = ACL_UNDEFINED_ID;
322 old->acl_entry[1].ae_perm = ACL_PERM_NONE;
323 old->acl_entry[2].ae_tag = ACL_OTHER;
324 old->acl_entry[2].ae_id = ACL_UNDEFINED_ID;
325 old->acl_entry[2].ae_perm = ACL_PERM_NONE;
328 case ACL_TYPE_DEFAULT:
330 * Unlike ACL_TYPE_ACCESS, there is no relationship
331 * between the inode contents and the ACL, and it is
332 * therefore possible for the request for the ACL
333 * to fail since the ACL is undefined. In this
334 * situation, return success and an empty ACL,
335 * as required by POSIX.1e.
342 error = acl_copy_oldacl_into_acl(old, ap->a_aclp);
346 if (ap->a_type == ACL_TYPE_ACCESS)
347 ufs_sync_acl_from_inode(ip, ap->a_aclp);
358 struct vop_getacl_args /* {
367 if ((ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) == 0)
370 if (ap->a_type == ACL_TYPE_NFS4)
371 return (ufs_getacl_nfs4(ap));
373 return (ufs_getacl_posix1e(ap));
377 * Set NFSv4 ACL without doing any access checking. This is required
378 * e.g. by the UFS code that implements ACL inheritance, or from
379 * ufs_vnops.c:ufs_chmod(), as some of the checks have to be skipped
380 * in that case, and others are redundant.
383 ufs_setacl_nfs4_internal(struct vnode *vp, struct acl *aclp, struct thread *td)
386 mode_t mode, newmode;
387 struct inode *ip = VTOI(vp);
389 KASSERT(acl_nfs4_check(aclp, vp->v_type == VDIR) == 0,
390 ("invalid ACL passed to ufs_setacl_nfs4_internal"));
392 if (acl_nfs4_is_trivial(aclp, ip->i_uid)) {
393 error = vn_extattr_rm(vp, IO_NODELOCKED,
394 NFS4_ACL_EXTATTR_NAMESPACE, NFS4_ACL_EXTATTR_NAME, td);
397 * An attempt to remove ACL from a file that didn't have
398 * any extended entries is not an error.
400 if (error == ENOATTR)
404 error = vn_extattr_set(vp, IO_NODELOCKED,
405 NFS4_ACL_EXTATTR_NAMESPACE, NFS4_ACL_EXTATTR_NAME,
406 sizeof(*aclp), (char *) aclp, td);
410 * Map lack of attribute definition in UFS_EXTATTR into lack of
411 * support for ACLs on the filesystem.
413 if (error == ENOATTR)
421 acl_nfs4_sync_mode_from_acl(&mode, aclp);
423 newmode = ip->i_mode & ACL_PRESERVE_MASK;
425 UFS_INODE_SET_MODE(ip, newmode);
426 DIP_SET(ip, i_mode, ip->i_mode);
427 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
429 VN_KNOTE_UNLOCKED(vp, NOTE_ATTRIB);
431 error = UFS_UPDATE(vp, 0);
436 ufs_setacl_nfs4(struct vop_setacl_args *ap)
439 struct inode *ip = VTOI(ap->a_vp);
441 if ((ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS) == 0)
444 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
447 if (ap->a_aclp == NULL)
450 error = VOP_ACLCHECK(ap->a_vp, ap->a_type, ap->a_aclp, ap->a_cred,
456 * Authorize the ACL operation.
458 if (ip->i_flags & (IMMUTABLE | APPEND))
462 * Must hold VWRITE_ACL or have appropriate privilege.
464 if ((error = VOP_ACCESSX(ap->a_vp, VWRITE_ACL, ap->a_cred, ap->a_td)))
468 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
469 * Make sure it has enough room for that - splitting every entry
470 * into two and appending "canonical six" entries at the end.
472 if (ap->a_aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2)
475 error = ufs_setacl_nfs4_internal(ap->a_vp, ap->a_aclp, ap->a_td);
481 * Set the ACL on a file.
483 * As part of the ACL is stored in the inode, and the rest in an EA,
484 * this is necessarily non-atomic, and has complex authorization.
485 * As ufs_setacl() includes elements of ufs_chown() and ufs_chmod(),
486 * a fair number of different access checks may be required to go ahead
487 * with the operation at all.
490 ufs_setacl_posix1e(struct vop_setacl_args *ap)
492 struct inode *ip = VTOI(ap->a_vp);
496 if ((ap->a_vp->v_mount->mnt_flag & MNT_ACLS) == 0)
500 * If this is a set operation rather than a delete operation,
501 * invoke VOP_ACLCHECK() on the passed ACL to determine if it is
502 * valid for the target. This will include a check on ap->a_type.
504 if (ap->a_aclp != NULL) {
508 error = VOP_ACLCHECK(ap->a_vp, ap->a_type, ap->a_aclp,
509 ap->a_cred, ap->a_td);
515 * POSIX.1e allows only deletion of the default ACL on a
516 * directory (ACL_TYPE_DEFAULT).
518 if (ap->a_type != ACL_TYPE_DEFAULT)
520 if (ap->a_vp->v_type != VDIR)
524 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
528 * Authorize the ACL operation.
530 if (ip->i_flags & (IMMUTABLE | APPEND))
534 * Must hold VADMIN (be file owner) or have appropriate privilege.
536 if ((error = VOP_ACCESS(ap->a_vp, VADMIN, ap->a_cred, ap->a_td)))
540 case ACL_TYPE_ACCESS:
541 old = malloc(sizeof(*old), M_ACL, M_WAITOK | M_ZERO);
542 error = acl_copy_acl_into_oldacl(ap->a_aclp, old);
544 error = vn_extattr_set(ap->a_vp, IO_NODELOCKED,
545 POSIX1E_ACL_ACCESS_EXTATTR_NAMESPACE,
546 POSIX1E_ACL_ACCESS_EXTATTR_NAME, sizeof(*old),
547 (char *) old, ap->a_td);
552 case ACL_TYPE_DEFAULT:
553 if (ap->a_aclp == NULL) {
554 error = vn_extattr_rm(ap->a_vp, IO_NODELOCKED,
555 POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE,
556 POSIX1E_ACL_DEFAULT_EXTATTR_NAME, ap->a_td);
558 * Attempting to delete a non-present default ACL
559 * will return success for portability purposes.
562 * XXX: Note that since we can't distinguish
563 * "that EA is not supported" from "that EA is not
564 * defined", the success case here overlaps the
565 * the ENOATTR->EOPNOTSUPP case below.
567 if (error == ENOATTR)
570 old = malloc(sizeof(*old), M_ACL, M_WAITOK | M_ZERO);
571 error = acl_copy_acl_into_oldacl(ap->a_aclp, old);
573 error = vn_extattr_set(ap->a_vp, IO_NODELOCKED,
574 POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE,
575 POSIX1E_ACL_DEFAULT_EXTATTR_NAME,
576 sizeof(*old), (char *) old, ap->a_td);
586 * Map lack of attribute definition in UFS_EXTATTR into lack of
587 * support for ACLs on the filesystem.
589 if (error == ENOATTR)
594 if (ap->a_type == ACL_TYPE_ACCESS) {
596 * Now that the EA is successfully updated, update the
597 * inode and mark it as changed.
599 ufs_sync_inode_from_acl(ap->a_aclp, ip);
600 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
601 error = UFS_UPDATE(ap->a_vp, 0);
604 VN_KNOTE_UNLOCKED(ap->a_vp, NOTE_ATTRIB);
610 struct vop_setacl_args /* {
618 if ((ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) == 0)
621 if (ap->a_type == ACL_TYPE_NFS4)
622 return (ufs_setacl_nfs4(ap));
624 return (ufs_setacl_posix1e(ap));
628 ufs_aclcheck_nfs4(struct vop_aclcheck_args *ap)
630 int is_directory = 0;
632 if ((ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS) == 0)
636 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
637 * Make sure it has enough room for that - splitting every entry
638 * into two and appending "canonical six" entries at the end.
640 if (ap->a_aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2)
643 if (ap->a_vp->v_type == VDIR)
646 return (acl_nfs4_check(ap->a_aclp, is_directory));
650 ufs_aclcheck_posix1e(struct vop_aclcheck_args *ap)
653 if ((ap->a_vp->v_mount->mnt_flag & MNT_ACLS) == 0)
657 * Verify we understand this type of ACL, and that it applies
658 * to this kind of object.
659 * Rely on the acl_posix1e_check() routine to verify the contents.
662 case ACL_TYPE_ACCESS:
665 case ACL_TYPE_DEFAULT:
666 if (ap->a_vp->v_type != VDIR)
674 if (ap->a_aclp->acl_cnt > OLDACL_MAX_ENTRIES)
677 return (acl_posix1e_check(ap->a_aclp));
681 * Check the validity of an ACL for a file.
685 struct vop_aclcheck_args /* {
694 if ((ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) == 0)
697 if (ap->a_type == ACL_TYPE_NFS4)
698 return (ufs_aclcheck_nfs4(ap));
700 return (ufs_aclcheck_posix1e(ap));
703 #endif /* !UFS_ACL */