Skip to content

Commit addf0cb

Browse files
torvaldsKali-
authored andcommitted
mm: avoid wrapping vm_pgoff in mremap()
commit 982134ba62618c2d69fbbbd166d0a11ee3b7e3d8 upstream. The normal mmap paths all avoid creating a mapping where the pgoff inside the mapping could wrap around due to overflow. However, an expanding mremap() can take such a non-wrapping mapping and make it bigger and cause a wrapping condition. Noticed by Robert Swiecki when running a system call fuzzer, where it caused a BUG_ON() due to terminally confusing the vma_prio_tree code. A vma dumping patch by Hugh then pinpointed the crazy wrapped case. Reported-and-tested-by: Robert Swiecki <[email protected]> Acked-by: Hugh Dickins <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Andi Kleen <[email protected]>
1 parent 6fe39c3 commit addf0cb

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

mm/mremap.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,16 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
274274
if (old_len > vma->vm_end - addr)
275275
goto Efault;
276276

277-
if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) {
278-
if (new_len > old_len)
277+
/* Need to be careful about a growing mapping */
278+
if (new_len > old_len) {
279+
unsigned long pgoff;
280+
281+
if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
279282
goto Efault;
283+
pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
284+
pgoff += vma->vm_pgoff;
285+
if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
286+
goto Einval;
280287
}
281288

282289
if (vma->vm_flags & VM_LOCKED) {

0 commit comments

Comments
 (0)