Skip to content

Commit 42c4e4b

Browse files
ljskernelakpm00
authored andcommitted
mm: correctly reference merged VMA
On second merge attempt on mmap() we incorrectly discard the possibly merged VMA, resulting in a possible use-after-free (and most certainly a reference to the wrong VMA) in this instance in the subsequent __mmap_complete() invocation. Correct this mistake by reassigning vma correctly if a merge succeeds in this case. Link: https://lkml.kernel.org/r/20241206215229.244413-1-lorenzo.stoakes@oracle.com Fixes: 5ac87a8 ("mm: defer second attempt at merge on mmap()") Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Suggested-by: Jann Horn <jannh@google.com> Reported-by: syzbot+91cf8da9401355f946c3@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/67536a25.050a0220.a30f1.0149.GAE@google.com/ Reviewed-by: Liam R. Howlett <Liam.Howlett@Oracle.com> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent f5d09de commit 42c4e4b

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

mm/vma.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2460,10 +2460,13 @@ unsigned long __mmap_region(struct file *file, unsigned long addr,
24602460

24612461
/* If flags changed, we might be able to merge, so try again. */
24622462
if (map.retry_merge) {
2463+
struct vm_area_struct *merged;
24632464
VMG_MMAP_STATE(vmg, &map, vma);
24642465

24652466
vma_iter_config(map.vmi, map.addr, map.end);
2466-
vma_merge_existing_range(&vmg);
2467+
merged = vma_merge_existing_range(&vmg);
2468+
if (merged)
2469+
vma = merged;
24672470
}
24682471

24692472
__mmap_complete(&map, vma);

0 commit comments

Comments
 (0)