Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm: Hold a file reference in madvise_remove

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 90ed52ebe481 ("[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>
Cc: stable@vger.kernel.org
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Andy Lutomirski and committed by
Linus Torvalds
9ab4233d 1b7fa4c2

+14 -4
+14 -4
mm/madvise.c
··· 15 15 #include <linux/sched.h> 16 16 #include <linux/ksm.h> 17 17 #include <linux/fs.h> 18 + #include <linux/file.h> 18 19 19 20 /* 20 21 * Any behaviour which results in changes to the vma->vm_flags needs to ··· 205 204 { 206 205 loff_t offset; 207 206 int error; 207 + struct file *f; 208 208 209 209 *prev = NULL; /* tell sys_madvise we drop mmap_sem */ 210 210 211 211 if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB)) 212 212 return -EINVAL; 213 213 214 - if (!vma->vm_file || !vma->vm_file->f_mapping 215 - || !vma->vm_file->f_mapping->host) { 214 + f = vma->vm_file; 215 + 216 + if (!f || !f->f_mapping || !f->f_mapping->host) { 216 217 return -EINVAL; 217 218 } 218 219 ··· 224 221 offset = (loff_t)(start - vma->vm_start) 225 222 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); 226 223 227 - /* filesystem's fallocate may need to take i_mutex */ 224 + /* 225 + * Filesystem's fallocate may need to take i_mutex. We need to 226 + * explicitly grab a reference because the vma (and hence the 227 + * vma's reference to the file) can go away as soon as we drop 228 + * mmap_sem. 229 + */ 230 + get_file(f); 228 231 up_read(&current->mm->mmap_sem); 229 - error = do_fallocate(vma->vm_file, 232 + error = do_fallocate(f, 230 233 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 231 234 offset, end - start); 235 + fput(f); 232 236 down_read(&current->mm->mmap_sem); 233 237 return error; 234 238 }