[PATCH] f2fs: reject immutable and append-only files in atomic replace

From: Hungyu Lin

Date: Fri Nov 20 2026 - 03:39:14 EST


F2FS_IOC_START_ATOMIC_REPLACE truncates the file when the atomic write
starts, and the commit drops the old blocks. It only checks that the
file is open for writing and that the caller owns it. It does not look
at the inode flags.

An append-only file can be opened for writing with O_APPEND, so the
owner of such a file can remove the data that is already there, which
ftruncate() and open() with O_TRUNC do not allow. For an immutable file
the same works through a file descriptor that was opened for writing
before the flag was set, although f2fs_write_checks() refuses writes
through it.

Return -EPERM if the file is immutable or append-only.
F2FS_IOC_START_ATOMIC_WRITE does not truncate the file and is not
changed.

Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE")
Cc: stable@xxxxxxxxxx
Signed-off-by: Hungyu Lin <dennylin0707@xxxxxxxxx>
---
fs/f2fs/file.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index a150de19b60f..ef7dc420fdde 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -2597,6 +2597,11 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)

inode_lock(inode);

+ if (truncate && (IS_IMMUTABLE(inode) || IS_APPEND(inode))) {
+ ret = -EPERM;
+ goto out;
+ }
+
if (!f2fs_disable_compressed_file(inode) ||
f2fs_is_pinned_file(inode)) {
ret = -EINVAL;
--
2.43.0