Skip to content

Commit

Permalink
mm: Hold a file reference in madvise_remove
Browse files Browse the repository at this point in the history
commit 9ab4233 upstream.

Otherwise the code races with munmap (causing a use-after-free
of the vma) or with close (causing a use-after-free of the struct
file).

The bug was introduced by commit 90ed52e ("[PATCH] holepunch: fix
mmap_sem i_mutex deadlock")

Cc: Hugh Dickins <hugh@veritas.com>
Cc: Miklos Szeredi <mszeredi@suse.cz>
Cc: Badari Pulavarty <pbadari@us.ibm.com>
Cc: Nick Piggin <npiggin@suse.de>
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
[bwh: Backported to 3.2:
 - Adjust context
 - madvise_remove() calls vmtruncate_range(), not do_fallocate()]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
amluto authored and gregkh committed Jul 16, 2012
1 parent c9bb51f commit 0c4ad5c
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions mm/madvise.c
Expand Up @@ -13,6 +13,7 @@
#include <linux/hugetlb.h>
#include <linux/sched.h>
#include <linux/ksm.h>
#include <linux/file.h>

/*
* Any behaviour which results in changes to the vma->vm_flags needs to
Expand Down Expand Up @@ -203,14 +204,16 @@ static long madvise_remove(struct vm_area_struct *vma,
struct address_space *mapping;
loff_t offset, endoff;
int error;
struct file *f;

*prev = NULL; /* tell sys_madvise we drop mmap_sem */

if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB))
return -EINVAL;

if (!vma->vm_file || !vma->vm_file->f_mapping
|| !vma->vm_file->f_mapping->host) {
f = vma->vm_file;

if (!f || !f->f_mapping || !f->f_mapping->host) {
return -EINVAL;
}

Expand All @@ -224,9 +227,16 @@ static long madvise_remove(struct vm_area_struct *vma,
endoff = (loff_t)(end - vma->vm_start - 1)
+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);

/* vmtruncate_range needs to take i_mutex */
/*
* vmtruncate_range may need to take i_mutex. We need to
* explicitly grab a reference because the vma (and hence the
* vma's reference to the file) can go away as soon as we drop
* mmap_sem.
*/
get_file(f);
up_read(&current->mm->mmap_sem);
error = vmtruncate_range(mapping->host, offset, endoff);
fput(f);
down_read(&current->mm->mmap_sem);
return error;
}
Expand Down

0 comments on commit 0c4ad5c

Please sign in to comment.