Re: KASAN: use-after-free Read in filemap_fault

From: Kirill A. Shutemov
Date: Fri Dec 28 2018 - 18:51:36 EST


On Fri, Dec 28, 2018 at 01:09:38PM -0800, Andrew Morton wrote:
> On Fri, 28 Dec 2018 12:51:04 -0800 syzbot <syzbot+b437b5a429d680cf2217@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> > Hello,
> >
> > syzbot found the following crash on:
>
> uh-oh. Josef, could you please take a look?
>
> : page = find_get_page(mapping, offset);
> : if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
> : /*
> : * We found the page, so try async readahead before
> : * waiting for the lock.
> : */
> : fpin = do_async_mmap_readahead(vmf, page);
> : } else if (!page) {
> : /* No page in the page cache at all */
> : fpin = do_sync_mmap_readahead(vmf);
> : count_vm_event(PGMAJFAULT);
> : count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
>
> vmf->vma has been freed at this point.
>
> : ret = VM_FAULT_MAJOR;
> : retry_find:
> : page = pagecache_get_page(mapping, offset,
> : FGP_CREAT|FGP_FOR_MMAP,
> : vmf->gfp_mask);
> : if (!page) {
> : if (fpin)
> : goto out_retry;
> : return vmf_error(-ENOMEM);
> : }
> : }
>

Here's a fixup for "filemap: drop the mmap_sem for all blocking operations".

do_sync_mmap_readahead() drops mmap_sem now, so by the time of
dereferencing vmf->vma for count_memcg_event_mm() the VMA can be gone.

diff --git a/mm/filemap.c b/mm/filemap.c
index 00a9315f45d4..65c85c47bdb1 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2554,10 +2554,10 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
fpin = do_async_mmap_readahead(vmf, page);
} else if (!page) {
/* No page in the page cache at all */
- fpin = do_sync_mmap_readahead(vmf);
count_vm_event(PGMAJFAULT);
count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
ret = VM_FAULT_MAJOR;
+ fpin = do_sync_mmap_readahead(vmf);
retry_find:
page = pagecache_get_page(mapping, offset,
FGP_CREAT|FGP_FOR_MMAP,
--
Kirill A. Shutemov