generic_write_check_limits() stores rlimit(RLIMIT_FSIZE), an unsigned long, in a loff_t. A soft limit between 2^63 and RLIM_INFINITY becomes negative there, so "pos >= limit" holds for every position: each write through generic_write_checks() or copy_file_range() fails with -EFBIG and raises SIGXFSZ, although no file position can reach such a limit. Reproducer (bash counts ulimit -f in 1024-byte blocks, so 2^53 blocks is 2^63 bytes): $ bash -c 'ulimit -f 9007199254740991; echo hi > /tmp/f'; echo $? 0 $ bash -c 'ulimit -f 9007199254740992; echo hi > /tmp/f'; echo $? File size limit exceeded 153 Before commit 9fd91a90cb98 ("vfs: strengthen checking of file range inputs to generic_remap_checks") the limit was an unsigned long in generic_write_checks(), and such limits had no effect. Keep the limit unsigned and apply it only when it is at most LLONG_MAX. Smaller limits keep the existing loff_t arithmetic, so their behaviour, including for negative positions, is unchanged. Fixes: 9fd91a90cb98 ("vfs: strengthen checking of file range inputs to generic_remap_checks") Reported-by: Jun-ichi Takimoto Closes: https://lore.kernel.org/linux-fsdevel/8F9EB913-3AC3-4D89-9DA3-427029F6D211@kba.biglobe.ne.jp/ Cc: stable@vger.kernel.org Assisted-by: Claude:claude-opus-5-5 Frama-C Signed-off-by: Ding Feng --- Notes: Tested on v6.12 (arm64 tinyconfig, QEMU virt, initramfs on ramfs) with a static test program that sets the soft RLIMIT_FSIZE, writes 2 bytes and copies 10 bytes with copy_file_range(): soft limit unpatched patched 1024, 2^63-1 ok ok 2^63, 2^63+4096, 2^64-2 EFBIG (both) ok (both) 1 short write, then EFBIG (both kernels) Also reproduced on 6.10.14 (arm64) with write(), pwrite() and copy_file_range() on tmpfs and overlayfs. Compile-tested for x86_64 defconfig with clang; not run on x86_64. fs/read_write.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index b15522c..18a217d 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -1693,14 +1693,15 @@ int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count) { struct inode *inode = file->f_mapping->host; loff_t max_size = inode->i_sb->s_maxbytes; - loff_t limit = rlimit(RLIMIT_FSIZE); + unsigned long limit = rlimit(RLIMIT_FSIZE); - if (limit != RLIM_INFINITY) { - if (pos >= limit) { + /* No file position reaches a limit above LLONG_MAX. */ + if (limit != RLIM_INFINITY && limit <= LLONG_MAX) { + if (pos >= (loff_t)limit) { send_sig(SIGXFSZ, current, 0); return -EFBIG; } - *count = min(*count, limit - pos); + *count = min(*count, (loff_t)limit - pos); } if (!(file->f_flags & O_LARGEFILE)) -- 2.55.0