From: Chris Mason Commit be83bbf80682 ("mmap: introduce sane default mmap limits") made do_mmap() reject file mappings which extend beyond the maximum offset the file can support, via file_mmap_ok(). However mremap() can expand a file mapping and only checks that the page offset does not wrap, so a mapping can be grown past the limit that mmap() would have enforced. Fault handlers rely on that limit. For instance dax_iomap_pte_fault() converts vmf->pgoff into a loff_t, which overflows for such an offset and defeats its i_size check. Fix the issue by applying file_mmap_ok() to the expanded range in check_prep_vma(), failing the mremap() with -EINVAL if it exceeds the limit. This means that mremap() now refuses expansions it previously permitted, however no file can have data at these offsets, and mmap() already refuses the equivalent mapping. Fixes: be83bbf80682 ("mmap: introduce sane default mmap limits") Cc: Signed-off-by: Chris Mason [ ljs: fixed up commit msg ] Signed-off-by: Lorenzo Stoakes (ARM) --- mm/internal.h | 2 ++ mm/mmap.c | 4 ++-- mm/mremap.c | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index a09dbdfd615d..aa7fba0a1c41 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -941,6 +941,8 @@ extern long faultin_page_range(struct mm_struct *mm, unsigned long start, unsigned long end, bool write, int *locked); bool mlock_future_ok(const struct mm_struct *mm, bool is_vma_locked, unsigned long bytes); +bool file_mmap_ok(struct file *file, struct inode *inode, unsigned long pgoff, + unsigned long len); /* * NOTE: This function can't tell whether the folio is "fully mapped" in the diff --git a/mm/mmap.c b/mm/mmap.c index 7618e72766a5..79702841c06c 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -264,8 +264,8 @@ static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) return ULONG_MAX; } -static inline bool file_mmap_ok(struct file *file, struct inode *inode, - unsigned long pgoff, unsigned long len) +bool file_mmap_ok(struct file *file, struct inode *inode, unsigned long pgoff, + unsigned long len) { u64 maxsize = file_mmap_size_max(file, inode); diff --git a/mm/mremap.c b/mm/mremap.c index fff0465f5e06..8043c425b5df 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -1791,6 +1791,7 @@ static int check_prep_vma(struct vma_remap_struct *vrm) struct mm_struct *mm = current->mm; unsigned long addr = vrm->addr; unsigned long old_len, new_len, pgoff; + struct file *file; if (!vma) return -EFAULT; @@ -1870,6 +1871,11 @@ static int check_prep_vma(struct vma_remap_struct *vrm) if (vma_is_fixed_mapping(vma)) return -EFAULT; + /* The expanded range must obey the same offset limits as mmap(). */ + file = vma->vm_file; + if (file && !file_mmap_ok(file, file_inode(file), pgoff, new_len)) + return -EINVAL; + if (!mlock_future_ok(mm, vma_test(vma, VMA_LOCKED_BIT), vrm->delta)) return -EAGAIN; -- 2.56.0