fs, zonefs: ignore RLIMIT_FSIZE values above LLONG_MAX
Subsystems (3)
cover fs, zonefs: ignore RLIMIT_FSIZE values above LLONG_MAX
generic_write_check_limits() and its zonefs copy store rlimit(RLIMIT_FSIZE), an unsigned long, in a loff_t. A soft limit between 2^63 and RLIM_INFINITY turns negative, and every write then fails with -EFBIG and SIGXFSZ:
$ bash -c 'ulimit -f 9007199254740992; echo hi > /tmp/f' File size limit exceeded
Patch 1 fixes the VFS helper (write, pwrite, writev and copy_file_range paths). Patch 2 applies the same fix to zonefs.
Jun-ichi Takimoto reported this in 2020 [1]; no fix followed. It came up again while proving pwrite(2) against POSIX with Frama-C/WP.
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.
[1] https://lore.kernel.org/linux-fsdevel/[email protected]/
Ding Feng (2):
fs: ignore RLIMIT_FSIZE above LLONG_MAX in
generic_write_check_limits()
zonefs: ignore RLIMIT_FSIZE above LLONG_MAX in
zonefs_write_check_limits()
fs/read_write.c | 9 +++++---- fs/zonefs/file.c | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/2 fs: ignore RLIMIT_FSIZE above LLONG_MAX in generic_write_check_limits()
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.
---
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(-)
fs/read_write.c +5 -4
@@ -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 ))
Signature · 1 line
2.55.0
Re: (subset) [PATCH 1/2] fs: ignore RLIMIT_FSIZE above LLONG_MAX in generic_write_check_limits()
Quoted 10 lines · Ding Feng
generic_write_check_limits()storesrlimit(RLIMIT_FSIZE), an unsigned long, in aloff_t. A soft limit between 2^63 andRLIM_INFINITYbecomes negative there, so "pos >= limit" holds for every position: each write throughgeneric_write_checks()orcopy_file_range()fails with-EFBIGand 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):
[...]
Applied to the vfs-7.5.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.5.misc branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.5.misc
[1/2] fs: ignore RLIMIT_FSIZE above LLONG_MAX in generic_write_check_limits()
https://git.kernel.org/vfs/vfs/c/bc0dc4379463
Re: [PATCH 1/2] fs: ignore RLIMIT_FSIZE above LLONG_MAX in generic_write_check_limits()
In reply to Ding FengQuoted 23 lines · Ding Feng
generic_write_check_limits()storesrlimit(RLIMIT_FSIZE), an unsigned long, in aloff_t. A soft limit between 2^63 andRLIM_INFINITYbecomes negative there, so "pos >= limit" holds for every position: each write throughgeneric_write_checks()orcopy_file_range()fails with-EFBIGand 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 153Before commit
9fd91a90cb98("vfs: strengthen checking of file range inputs togeneric_remap_checks") the limit was an unsigned long ingeneric_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 existingloff_tarithmetic, so their behaviour, including for negative positions, is unchanged.
Quoted 6 lines
Fixes 9fd91a90cb98 ("vfs: strengthen checking of file range inputs to generic_remap_checks")Reported-by Jun-ichi Takimoto <[email protected]>Assisted-by Claude:claude-opus-5-5 Frama-CSigned-off-by Ding Feng <[email protected]>
Looks sane. Feel free to add:
Honza
Quoted 47 lines
---
Notes:
Tested on v6.12 (arm64 tinyconfig, QEMU virt, initramfs on ramfs) with a static test program that sets the softRLIMIT_FSIZE, writes 2 bytes and copies 10 bytes withcopy_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()andcopy_file_range()on tmpfs and overlayfs. Compile-tested forx86_64defconfig with clang; not run onx86_64.fs/read_write.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)fs/read_write.c +5
-4@@ -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 ))Signature · 1 line
2.55.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH 2/2 zonefs: ignore RLIMIT_FSIZE above LLONG_MAX in zonefs_write_check_limits()
In reply to Ding Fengzonefs_write_check_limits() follows generic_write_check_limits() and has the same problem: it stores rlimit(RLIMIT_FSIZE), an unsigned long, in a loff_t, so a soft limit between 2^63 and RLIM_INFINITY is negative and every write fails with -EFBIG and raises SIGXFSZ.
Keep the limit unsigned and apply it only when it is at most LLONG_MAX, as the generic helper now does.
--- fs/zonefs/file.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
fs/zonefs/file.c +5 -4
@@ -399,15 +399,16 @@ static loff_t zonefs_write_check_limits(struct file *file, loff_t pos, {struct inode *inode =file_inode (file);struct zonefs_zone *z =zonefs_inode_zone (inode); -loff_t limit =rlimit (RLIMIT_FSIZE ); +unsigned long limit =rlimit (RLIMIT_FSIZE );loff_t max_size = z->z_capacity; -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 ))
Signature · 1 line
2.55.0
Re: [PATCH 2/2] zonefs: ignore RLIMIT_FSIZE above LLONG_MAX in zonefs_write_check_limits()
Quoted 5 lines · Ding Feng
zonefs_write_check_limits()followsgeneric_write_check_limits()and has the same problem: it storesrlimit(RLIMIT_FSIZE), an unsigned long, in aloff_t, so a soft limit between 2^63 andRLIM_INFINITYis negative and every write fails with-EFBIGand raises SIGXFSZ.
Quoted 7 lines
Keep the limit unsigned and apply it only when it is at most
LLONG_MAX, as the generic helper now does.Fixes ebfd68cd0c1e ("zonefs: Fix O_APPEND async write handling")Assisted-by Claude:claude-opus-5-5 Frama-CSigned-off-by Ding Feng <[email protected]>
Applied to zonefs for-7.4-fixes. Thanks!
Signature · 2 lines
Damien Le Moal Western Digital Research
Damien Le Moal
maintainerRe: [PATCH 2/2] zonefs: ignore RLIMIT_FSIZE above LLONG_MAX in zonefs_write_check_limits()
In reply to Ding FengQuoted 22 lines · Ding Feng
zonefs_write_check_limits()followsgeneric_write_check_limits()and has the same problem: it storesrlimit(RLIMIT_FSIZE), an unsigned long, in aloff_t, so a soft limit between 2^63 andRLIM_INFINITYis negative and every write fails with-EFBIGand raises SIGXFSZ.Keep the limit unsigned and apply it only when it is at most
LLONG_MAX, as the generic helper now does.Fixes ebfd68cd0c1e ("zonefs: Fix O_APPEND async write handling")Assisted-by Claude:claude-opus-5-5 Frama-CSigned-off-by Ding Feng <[email protected]>--- fs/zonefs/file.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)fs/zonefs/file.c +0
-0@@ -399,15 +399,16 @@ static loff_t zonefs_write_check_limits(struct file *file, loff_t pos, {
fs/zonefs/file.c static loff_t zonefs_write_check_limits(struct file *file, loff_t pos, · line 406
Quoted 10 lines
fs/zonefs/file.c +3
-3struct inode *inode =file_inode (file);struct zonefs_zone *z =zonefs_inode_zone (inode); -loff_t limit =rlimit (RLIMIT_FSIZE ); +unsigned long limit =rlimit (RLIMIT_FSIZE );loff_t max_size = z->z_capacity; -if (limit !=RLIM_INFINITY ) { -if (pos >= limit) { +/* No file position reaches a limit above LLONG_MAX. */ +if (limit !=RLIM_INFINITY && limit <=LLONG_MAX ) {
As the kernel test robot pointed out, limit <= LLONG_MAX is always true on 32-bits arch because limit is an unsigned long. So we need limit to be an unsigned long long I think. Same thing on patch 1.
I am pulling this patch until this is fixed.
Quoted 9 lines
fs/zonefs/file.c +2
-1+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 ))
Signature · 2 lines
Damien Le Moal Western Digital Research