git: 538106a3bf21 - main - fdescfs: check the target file for FWRITE access when truncating

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 17:02:03 UTC
The branch main has been updated by markj:

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

commit 538106a3bf21135f61b7d1bb96a479d543d5edf7
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-12 19:29:20 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-17 16:57:01 +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 1dd3cc385e9f..523700ea81cc 100644
--- a/sys/fs/fdescfs/fdesc_vnops.c
+++ b/sys/fs/fdescfs/fdesc_vnops.c
@@ -540,19 +540,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
@@ -573,7 +578,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);
 }