Skip to content

Commit

Permalink
mm: fix use-after-free in sys_remap_file_pages
Browse files Browse the repository at this point in the history
commit 4eb9198 upstream.

remap_file_pages calls mmap_region, which may merge the VMA with other
existing VMAs, and free "vma".  This can lead to a use-after-free bug.
Avoid the bug by remembering vm_flags before calling mmap_region, and
not trying to dereference vma later.

Signed-off-by: Rik van Riel <[email protected]>
Reported-by: Dmitry Vyukov <[email protected]>
Cc: PaX Team <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Michel Lespinasse <[email protected]>
Cc: Cyrill Gorcunov <[email protected]>
Cc: Hugh Dickins <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
Rik van Riel authored and gregkh committed Jan 9, 2014
1 parent 1f86532 commit e910af6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions mm/fremap.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,18 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
if (mapping_cap_account_dirty(mapping)) {
unsigned long addr;
struct file *file = get_file(vma->vm_file);
/* mmap_region may free vma; grab the info now */
vm_flags = vma->vm_flags;

addr = mmap_region(file, start, size,
vma->vm_flags, pgoff);
addr = mmap_region(file, start, size, vm_flags, pgoff);
fput(file);
if (IS_ERR_VALUE(addr)) {
err = addr;
} else {
BUG_ON(addr != start);
err = 0;
}
goto out;
goto out_freed;
}
mutex_lock(&mapping->i_mmap_mutex);
flush_dcache_mmap_lock(mapping);
Expand Down Expand Up @@ -253,6 +254,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
out:
if (vma)
vm_flags = vma->vm_flags;
out_freed:
if (likely(!has_write_lock))
up_read(&mm->mmap_sem);
else
Expand Down

0 comments on commit e910af6

Please sign in to comment.