git: db2e6660e205 - releng/14.4 - fdescfs: check the target file for FWRITE access when truncating

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 17:04:27 UTC
The branch releng/14.4 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=db2e6660e205b60dfe6614fd8c56d3dfe6431696

commit db2e6660e205b60dfe6614fd8c56d3dfe6431696
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-12 19:29:20 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-16 21:16:23 +0000

    fdescfs: check the target file for FWRITE access when truncating
    
    Truncation is done by calling fdesc_setattr(), and fdesc_setattr() looks
    up fd N again, so a concurrent dup2() can swap the file between the
    access check and the truncate.
    
    Correct the rights checked for the setattr().
    Unlock the fdescfs vnode around fdrop() of the underlying file.
    
    Approved by:    so
    Security:       FreeBSD-SA-26:91.fdescfs
    Sponsored by:   The FreeBSD Foundation
---
 sys/fs/fdescfs/fdesc_vnops.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/sys/fs/fdescfs/fdesc_vnops.c b/sys/fs/fdescfs/fdesc_vnops.c
index 8e66267901c2..2776b99b49f2 100644
--- a/sys/fs/fdescfs/fdesc_vnops.c
+++ b/sys/fs/fdescfs/fdesc_vnops.c
@@ -528,19 +528,24 @@ fdesc_setattr(struct vop_setattr_args *ap)
 		return (EACCES);
 
 	fd = VTOFDESC(ap->a_vp)->fd_fd;
+	cap_rights_init(&rights, CAP_FUTIMES, CAP_FCHMOD, CAP_FCHOWN,
+	    CAP_FCHFLAGS);
 
 	/*
 	 * Allow setattr where there is an underlying vnode.
 	 * For O_PATH descriptors, disallow truncate.
 	 */
 	if (vap->va_size != VNOVAL) {
-		error = getvnode(td, fd,
-		    cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
+		cap_rights_set(&rights, CAP_FTRUNCATE);
+		error = getvnode(td, fd, &rights, &fp);
+		if (error == 0 && (fp->f_flag & FWRITE) == 0) {
+			error = EACCES;
+			goto out;
+		}
 	} else {
-		error = getvnode_path(td, fd,
-		    cap_rights_init_one(&rights, CAP_EXTATTR_SET), NULL, &fp);
+		error = getvnode_path(td, fd, &rights, NULL, &fp);
 	}
-	if (error) {
+	if (error != 0) {
 		/*
 		 * getvnode() returns EINVAL if the file descriptor is not
 		 * backed by a vnode.  Silently drop all changes except
@@ -561,7 +566,10 @@ fdesc_setattr(struct vop_setattr_args *ap)
 		VOP_UNLOCK(vp);
 		vn_finished_write(mp);
 	}
+out:
+	VOP_UNLOCK(ap->a_vp);
 	fdrop(fp, td);
+	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
 	return (error);
 }