unmapped_area_topdown() suffers from a class of bug arising from a mismatch between how maple tree functions and VMA iterator functions express ranges - the former inclusively, the latter exclusively. This is problematic with an exclusive upper bound of 0, as it translates to an inclusive upper bound of ULONG_MAX, that is a range which extends to the end of the address space, and start < end or equivalent comparisons evaluate false in a situation where in fact the expression should evaluate true. unmapped_area_topdown() calls vma_iter_area_highest() to position the VMA iterator into the next highest empty range spanning [low_limit, high_limit), iteratively reducing high_limit without checking whether it has reached 0. This can result in a [low_limit, 0) pair being passed to vma_iter_area_highest() and thus unexpected underflow. Consider: vm_start = 0x30000 stack_guard_gap = 0x100000 info->length = 0x1000 high_limit = vm_start_gap() = 0 high_limit - 1 = ULONG_MAX window = [low_limit, ULONG_MAX] vma_iter_end() = ULONG_MAX + 1 = 0 gap = 0 - info->length = 0xfffffffffffff000 The result is page aligned and below the IS_ERR_VALUE() range, so it does not look like an error. What follows depends on the caller: - mmap() with no hint can fail with -ENOMEM while space is free. The bottom-up fallback is skipped, and __get_unmapped_area() refuses the address as above TASK_SIZE. - On x86-64, find_nearest_trampoline() can pick the address for the uprobe trampoline. _install_special_mapping() then inserts a VMA whose vm_end is 0, and exit_mmap() frees kernel page tables that every mm shares. - On powerpc64 with the hash MMU, slice_get_unmapped_area() can pass the address to alloc_extended_context(), which then writes out of bounds of mm->context.extended_id[]. mmap() can succeed with an address from the wrapped window where the range above TASK_SIZE is too short for the request and its alignment padding. On s390 that range is one page, so this takes no more than the default stack_guard_gap. Fix the issue by converting the loop into one that explicitly tests low_limit < high_limit. Additionally, simplify the logic by declaring the VMA iterator in the loop (it is reset on each iteration anyway) and exiting with the correct output value on success rather than performing a goto when an iteration is required. Reported-by: Chris Mason Fixes: 58c5d0d6d522 ("mm/mmap: regression fix for unmapped_area{_topdown}") Cc: Signed-off-by: Lorenzo Stoakes (ARM) --- mm/vma.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/mm/vma.c b/mm/vma.c index c82e9306b11a..09bbcdb4971b 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -3369,8 +3369,6 @@ unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) { unsigned long length, gap, gap_end; unsigned long low_limit, high_limit; - struct vm_area_struct *tmp; - VMA_ITERATOR(vmi, current->mm, 0); /* Adjust search length to account for worst case alignment overhead */ length = info->length + info->align_mask + info->start_gap; @@ -3381,31 +3379,31 @@ unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) if (low_limit < mmap_min_addr) low_limit = mmap_min_addr; high_limit = info->high_limit; -retry: - if (vma_iter_area_highest(&vmi, low_limit, high_limit, length)) - return -ENOMEM; - gap = vma_iter_end(&vmi) - info->length; - gap -= (gap - info->align_offset) & info->align_mask; - gap_end = vma_iter_end(&vmi); - tmp = vma_next(&vmi); - /* Avoid prev check if possible */ - if (tmp && vma_test_any_mask(tmp, VMA_STARTGAP_FLAGS)) { - if (vm_start_gap(tmp) < gap_end) { + while (low_limit < high_limit) { + VMA_ITERATOR(vmi, current->mm, 0); + struct vm_area_struct *tmp; + + if (vma_iter_area_highest(&vmi, low_limit, high_limit, length)) + break; + + gap = vma_iter_end(&vmi) - info->length; + gap -= (gap - info->align_offset) & info->align_mask; + gap_end = vma_iter_end(&vmi); + tmp = vma_next(&vmi); + /* Avoid prev check if possible */ + if (tmp && vma_test_any_mask(tmp, VMA_STARTGAP_FLAGS)) { + if (vm_start_gap(tmp) >= gap_end) + return gap; high_limit = vm_start_gap(tmp); - vma_iter_reset(&vmi); - goto retry; - } - } else { - tmp = vma_prev(&vmi); - if (tmp && vm_end_gap(tmp) > gap) { + } else { + tmp = vma_prev(&vmi); + if (!tmp || vm_end_gap(tmp) <= gap) + return gap; high_limit = tmp->vm_start; - vma_iter_reset(&vmi); - goto retry; } } - - return gap; + return -ENOMEM; } /* -- 2.56.0