Commit 14611508 authored by Jann Horn's avatar Jann Horn Committed by Andrew Morton
Browse files

mm: mark mas allocation in vms_abort_munmap_vmas as __GFP_NOFAIL

vms_abort_munmap_vmas() is a recovery path where, on entry, some VMAs have
already been torn down halfway (in a way we can't undo) but are still
present in the maple tree.

At this point, we *must* remove the VMAs from the VMA tree, otherwise we
get UAF.

Because removing VMA tree nodes can require memory allocation, the
existing code has an error path which tries to handle this by reattaching
the VMAs; but that can't be done safely.

A nicer way to fix it would probably be to preallocate enough maple tree
nodes for the removal before the point of no return, or something like
that; but for now, fix it the easy and kinda ugly way, by marking this
allocation __GFP_NOFAIL.

Link: https://lkml.kernel.org/r/20241016-fix-munmap-abort-v1-1-601c94b2240d@google.com


Fixes: 4f87153e ("mm: change failure of MAP_FIXED to restoring the gap on failure")
Signed-off-by: default avatarJann Horn <jannh@google.com>
Reviewed-by: default avatarLiam R. Howlett <Liam.Howlett@Oracle.com>
Acked-by: default avatarVlastimil Babka <vbabka@suse.cz>
Reviewed-by: default avatarLorenzo Stoakes <lorenzo.stoakes@oracle.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 1db27286
Loading
Loading
Loading
Loading
+3 −9
Original line number Diff line number Diff line
@@ -241,16 +241,10 @@ static inline void vms_abort_munmap_vmas(struct vma_munmap_struct *vms,
	 * failure method of leaving a gap where the MAP_FIXED mapping failed.
	 */
	mas_set_range(mas, vms->start, vms->end - 1);
	if (unlikely(mas_store_gfp(mas, NULL, GFP_KERNEL))) {
		pr_warn_once("%s: (%d) Unable to abort munmap() operation\n",
			     current->comm, current->pid);
		/* Leaving vmas detached and in-tree may hamper recovery */
		reattach_vmas(mas_detach);
	} else {
	mas_store_gfp(mas, NULL, GFP_KERNEL|__GFP_NOFAIL);
	/* Clean up the insertion of the unfortunate gap */
	vms_complete_munmap_vmas(vms, mas_detach);
}
}

int
do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,