Skip to content

Commit d7af031

Browse files
kirylgregkh
authored andcommitted
shmem: pin the file in shmem_fault() if mmap_sem is dropped
[ Upstream commit 8897c1b ] syzbot found the following crash: BUG: KASAN: use-after-free in perf_trace_lock_acquire+0x401/0x530 include/trace/events/lock.h:13 Read of size 8 at addr ffff8880a5cf2c50 by task syz-executor.0/26173 CPU: 0 PID: 26173 Comm: syz-executor.0 Not tainted 5.3.0-rc6 #146 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: perf_trace_lock_acquire+0x401/0x530 include/trace/events/lock.h:13 trace_lock_acquire include/trace/events/lock.h:13 [inline] lock_acquire+0x2de/0x410 kernel/locking/lockdep.c:4411 __raw_spin_lock include/linux/spinlock_api_smp.h:142 [inline] _raw_spin_lock+0x2f/0x40 kernel/locking/spinlock.c:151 spin_lock include/linux/spinlock.h:338 [inline] shmem_fault+0x5ec/0x7b0 mm/shmem.c:2034 __do_fault+0x111/0x540 mm/memory.c:3083 do_shared_fault mm/memory.c:3535 [inline] do_fault mm/memory.c:3613 [inline] handle_pte_fault mm/memory.c:3840 [inline] __handle_mm_fault+0x2adf/0x3f20 mm/memory.c:3964 handle_mm_fault+0x1b5/0x6b0 mm/memory.c:4001 do_user_addr_fault arch/x86/mm/fault.c:1441 [inline] __do_page_fault+0x536/0xdd0 arch/x86/mm/fault.c:1506 do_page_fault+0x38/0x590 arch/x86/mm/fault.c:1530 page_fault+0x39/0x40 arch/x86/entry/entry_64.S:1202 It happens if the VMA got unmapped under us while we dropped mmap_sem and inode got freed. Pinning the file if we drop mmap_sem fixes the issue. Link: http://lkml.kernel.org/r/20190927083908.rhifa4mmaxefc24r@box Signed-off-by: Kirill A. Shutemov <[email protected]> Reported-by: [email protected] Acked-by: Johannes Weiner <[email protected]> Reviewed-by: Matthew Wilcox (Oracle) <[email protected]> Cc: Hillf Danton <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Josef Bacik <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent d56c69c commit d7af031

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

mm/shmem.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,16 +2022,14 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf)
20222022
shmem_falloc->waitq &&
20232023
vmf->pgoff >= shmem_falloc->start &&
20242024
vmf->pgoff < shmem_falloc->next) {
2025+
struct file *fpin;
20252026
wait_queue_head_t *shmem_falloc_waitq;
20262027
DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
20272028

20282029
ret = VM_FAULT_NOPAGE;
2029-
if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
2030-
!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
2031-
/* It's polite to up mmap_sem if we can */
2032-
up_read(&vma->vm_mm->mmap_sem);
2030+
fpin = maybe_unlock_mmap_for_io(vmf, NULL);
2031+
if (fpin)
20332032
ret = VM_FAULT_RETRY;
2034-
}
20352033

20362034
shmem_falloc_waitq = shmem_falloc->waitq;
20372035
prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
@@ -2049,6 +2047,9 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf)
20492047
spin_lock(&inode->i_lock);
20502048
finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
20512049
spin_unlock(&inode->i_lock);
2050+
2051+
if (fpin)
2052+
fput(fpin);
20522053
return ret;
20532054
}
20542055
spin_unlock(&inode->i_lock);

0 commit comments

Comments
 (0)