mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	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 90ed52ebe4 ("[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>
			
			
This commit is contained in:
		
							parent
							
								
									1b7fa4c271
								
							
						
					
					
						commit
						9ab4233dd0
					
				
					 1 changed files with 14 additions and 4 deletions
				
			
		
							
								
								
									
										18
									
								
								mm/madvise.c
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								mm/madvise.c
									
									
									
									
									
								
							| 
						 | 
					@ -15,6 +15,7 @@
 | 
				
			||||||
#include <linux/sched.h>
 | 
					#include <linux/sched.h>
 | 
				
			||||||
#include <linux/ksm.h>
 | 
					#include <linux/ksm.h>
 | 
				
			||||||
#include <linux/fs.h>
 | 
					#include <linux/fs.h>
 | 
				
			||||||
 | 
					#include <linux/file.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Any behaviour which results in changes to the vma->vm_flags needs to
 | 
					 * Any behaviour which results in changes to the vma->vm_flags needs to
 | 
				
			||||||
| 
						 | 
					@ -204,14 +205,16 @@ static long madvise_remove(struct vm_area_struct *vma,
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	loff_t offset;
 | 
						loff_t offset;
 | 
				
			||||||
	int error;
 | 
						int error;
 | 
				
			||||||
 | 
						struct file *f;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	*prev = NULL;	/* tell sys_madvise we drop mmap_sem */
 | 
						*prev = NULL;	/* tell sys_madvise we drop mmap_sem */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB))
 | 
						if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB))
 | 
				
			||||||
		return -EINVAL;
 | 
							return -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!vma->vm_file || !vma->vm_file->f_mapping
 | 
						f = vma->vm_file;
 | 
				
			||||||
		|| !vma->vm_file->f_mapping->host) {
 | 
					
 | 
				
			||||||
 | 
						if (!f || !f->f_mapping || !f->f_mapping->host) {
 | 
				
			||||||
			return -EINVAL;
 | 
								return -EINVAL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -221,11 +224,18 @@ static long madvise_remove(struct vm_area_struct *vma,
 | 
				
			||||||
	offset = (loff_t)(start - vma->vm_start)
 | 
						offset = (loff_t)(start - vma->vm_start)
 | 
				
			||||||
			+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
 | 
								+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* filesystem's fallocate may need to take i_mutex */
 | 
						/*
 | 
				
			||||||
 | 
						 * Filesystem's fallocate 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(¤t->mm->mmap_sem);
 | 
						up_read(¤t->mm->mmap_sem);
 | 
				
			||||||
	error = do_fallocate(vma->vm_file,
 | 
						error = do_fallocate(f,
 | 
				
			||||||
				FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
 | 
									FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
 | 
				
			||||||
				offset, end - start);
 | 
									offset, end - start);
 | 
				
			||||||
 | 
						fput(f);
 | 
				
			||||||
	down_read(¤t->mm->mmap_sem);
 | 
						down_read(¤t->mm->mmap_sem);
 | 
				
			||||||
	return error;
 | 
						return error;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue