git: 4a7bf62ec173 - main - tmpfs: fix append-only file handling for writes and hole-punching

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 21:11:24 UTC
The branch main has been updated by kevans:

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

commit 4a7bf62ec173e87b07409b03c73fbc203e854358
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-11-17 21:10:38 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-11-17 21:11:05 +0000

    tmpfs: fix append-only file handling for writes and hole-punching
    
    Write protection assumed that O_APPEND handling at open time is
    sufficient, but the flag can be cleared with F_SETFL.  That's maybe
    worth rejecting, but that's harder to do without some clear layering
    violations.
    
    Hole-punching also violates the spirit of append-only, so reject that
    on files that are append-only.
    
    Note that we don't reject IMMUTABLE here- UFS seems to historically
    not care if a file transitions to IMMUTABLE after it's opened, so follow
    suit here instead of trying to tighten it up.  ZFS will apparently use
    the more constraining logic, but that's OK for now.
    
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D61491
---
 sys/fs/tmpfs/tmpfs_vnops.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/sys/fs/tmpfs/tmpfs_vnops.c b/sys/fs/tmpfs/tmpfs_vnops.c
index fe8507b4936a..895d1c7dc9f9 100644
--- a/sys/fs/tmpfs/tmpfs_vnops.c
+++ b/sys/fs/tmpfs/tmpfs_vnops.c
@@ -678,6 +678,15 @@ tmpfs_write(struct vop_write_args *v)
 		return (0);
 	if (ioflag & IO_APPEND)
 		uio->uio_offset = node->tn_size;
+
+	/*
+	 * For better or worse, O_APPEND can be cleared after the file has been
+	 * opened with F_SETFL.  Only allow writes at the end for append-only
+	 * files.
+	 */
+	if ((node->tn_flags & APPEND) != 0 && uio->uio_offset != node->tn_size)
+		return (EPERM);
+
 	error = vn_rlimit_fsizex(vp, uio, VFS_TO_TMPFS(vp->v_mount)->
 	    tm_maxfilesize, &r, uio->uio_td);
 	if (error != 0) {
@@ -717,6 +726,13 @@ out:
 static int
 tmpfs_deallocate(struct vop_deallocate_args *v)
 {
+	struct tmpfs_node *node;
+
+	/* Deallocation would zero existing data in the file. */
+	node = VP_TO_TMPFS_NODE(v->a_vp);
+	if ((node->tn_flags & APPEND) != 0)
+		return (EPERM);
+
 	return (tmpfs_reg_punch_hole(v->a_vp, v->a_offset, v->a_len));
 }