From: Zhiling Zou shmem_fallocate() validates offset + len with inode_newsize_ok(), but then rounds the end offset up to a page boundary by adding PAGE_SIZE - 1. For a valid request ending at MAX_LFS_FILESIZE, the extra addition can overflow loff_t before the value is shifted into a page index. On the internal tmpfs mount used by memfd_create(), sbinfo->max_blocks is zero, so the later max_blocks guard is skipped. An unprivileged memfd user can therefore pass a length near MAX_LFS_FILESIZE and make shmem_fallocate() iterate over a huge wrapped page range, consuming CPU and memory until OOM. Cache the validated end position and cast it to u64 before rounding it up to a page index. Then reject ranges that are larger than the tmpfs instance limit, LONG_MAX, or the system's RAM plus swap before the allocation loop. This preserves the existing file size and seal checks while preventing the memfd path from entering the allocation loop for impossible ranges. Fixes: e2d12e22c59c ("tmpfs: support fallocate preallocation") Cc: stable@vger.kernel.org Reported-by: Vega Assisted-by: Codex:gpt-5.4 Signed-off-by: Zhiling Zou Signed-off-by: Ren Wei --- mm/shmem.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index b51f83c970bb3..d626561d46db7 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3601,7 +3601,8 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); struct shmem_inode_info *info = SHMEM_I(inode); struct shmem_falloc shmem_falloc; - pgoff_t start, index, end, undo_fallocend; + pgoff_t start, index, end, nr_pages, undo_fallocend; + loff_t end_pos; int error; if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) @@ -3649,23 +3650,29 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, } /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ - error = inode_newsize_ok(inode, offset + len); + end_pos = offset + len; + error = inode_newsize_ok(inode, end_pos); if (error) goto out; - if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { + if ((info->seals & F_SEAL_GROW) && end_pos > inode->i_size) { error = -EPERM; goto out; } start = offset >> PAGE_SHIFT; - end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; + end = ((u64)end_pos + PAGE_SIZE - 1) >> PAGE_SHIFT; + nr_pages = end - start; /* Try to avoid a swapstorm if len is impossible to satisfy */ - if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { + if (sbinfo->max_blocks && nr_pages > sbinfo->max_blocks) { + error = -ENOSPC; + goto out; + } + if (nr_pages > LONG_MAX || + nr_pages > totalram_pages() + total_swap_pages) { error = -ENOSPC; goto out; } - shmem_falloc.waitq = NULL; shmem_falloc.start = start; shmem_falloc.next = start; @@ -3699,7 +3706,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) error = -ENOMEM; else - error = shmem_get_folio(inode, index, offset + len, + error = shmem_get_folio(inode, index, end_pos, &folio, SGP_FALLOC); if (error) { info->fallocend = undo_fallocend; @@ -3743,8 +3750,8 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, cond_resched(); } - if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) - i_size_write(inode, offset + len); + if (!(mode & FALLOC_FL_KEEP_SIZE) && end_pos > inode->i_size) + i_size_write(inode, end_pos); undone: spin_lock(&inode->i_lock); inode->i_private = NULL; -- 2.43.0