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

2018-12-28 Thread Dmitry Vyukov
On Sat, Dec 29, 2018 at 12:51 AM Kirill A. Shutemov
 wrote:
>
> On Fri, Dec 28, 2018 at 01:09:38PM -0800, Andrew Morton wrote:
> > On Fri, 28 Dec 2018 12:51:04 -0800 syzbot 
> >  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".

If you are going to squash this, please add:

Tested-by: syzbot+b437b5a429d680cf2...@syzkaller.appspotmail.com


> 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
>
> --
> You received this message because you are subscribed to the Google Groups 
> "syzkaller-bugs" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to syzkaller-bugs+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/syzkaller-bugs/20181228235106.okk3oastsnpxusxs%40kshutemo-mobl1.
> For more options, visit https://groups.google.com/d/optout.


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

2018-12-28 Thread Kirill A. Shutemov
On Fri, Dec 28, 2018 at 01:09:38PM -0800, Andrew Morton wrote:
> On Fri, 28 Dec 2018 12:51:04 -0800 syzbot 
>  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


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

2018-12-28 Thread Kirill A. Shutemov
On Sat, Dec 29, 2018 at 01:01:52AM +0300, Kirill A. Shutemov wrote:
> On Fri, Dec 28, 2018 at 12:51:04PM -0800, syzbot wrote:
> > Allocated by task 8196:
> 
> ...
> 
> > Freed by task 8197:
> 
> Hm. VMA allocated by one process (I don't see threads in the test case)
> gets freed by another one. Looks fishy to me.

Ignore this. I need some to go sleep %)

-- 
 Kirill A. Shutemov


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

2018-12-28 Thread Kirill A. Shutemov
On Fri, Dec 28, 2018 at 12:51:04PM -0800, syzbot wrote:
> Allocated by task 8196:

...

> Freed by task 8197:

Hm. VMA allocated by one process (I don't see threads in the test case)
gets freed by another one. Looks fishy to me.

-- 
 Kirill A. Shutemov


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

2018-12-28 Thread Andrew Morton
On Fri, 28 Dec 2018 12:51:04 -0800 syzbot 
 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);
:   }
:   }



KASAN: use-after-free Read in filemap_fault

2018-12-28 Thread syzbot

Hello,

syzbot found the following crash on:

HEAD commit:6a1d293238c1 Add linux-next specific files for 20181224
git tree:   linux-next
console output: https://syzkaller.appspot.com/x/log.txt?x=102ca56740
kernel config:  https://syzkaller.appspot.com/x/.config?x=f9369d117d073843
dashboard link: https://syzkaller.appspot.com/bug?extid=b437b5a429d680cf2217
compiler:   gcc (GCC) 8.0.1 20180413 (experimental)
syz repro:  https://syzkaller.appspot.com/x/repro.syz?x=15f059b340
C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=12ac602d40

IMPORTANT: if you fix the bug, please add the following tag to the commit:
Reported-by: syzbot+b437b5a429d680cf2...@syzkaller.appspotmail.com

sshd (8177) used greatest stack depth: 15720 bytes left
hrtimer: interrupt took 27544 ns
==
BUG: KASAN: use-after-free in filemap_fault+0x2818/0x2a70 mm/filemap.c:2559
Read of size 8 at addr 8881b15026b0 by task syz-executor997/8196

CPU: 0 PID: 8196 Comm: syz-executor997 Not tainted 4.20.0-rc7-next-20181224  
#188
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS  
Google 01/01/2011

Call Trace:
 __dump_stack lib/dump_stack.c:77 [inline]
 dump_stack+0x1d3/0x2c6 lib/dump_stack.c:113
 print_address_description.cold.5+0x9/0x1ff mm/kasan/report.c:187
 kasan_report.cold.6+0x1b/0x39 mm/kasan/report.c:317
 __asan_report_load8_noabort+0x14/0x20 mm/kasan/generic_report.c:135
 filemap_fault+0x2818/0x2a70 mm/filemap.c:2559
 ext4_filemap_fault+0x82/0xad fs/ext4/inode.c:6326
 __do_fault+0x176/0x6f0 mm/memory.c:3013
 do_shared_fault mm/memory.c:3479 [inline]
 do_fault mm/memory.c:3554 [inline]
 handle_pte_fault mm/memory.c:3781 [inline]
 __handle_mm_fault+0x373b/0x55f0 mm/memory.c:3905
 handle_mm_fault+0x54f/0xc70 mm/memory.c:3942
 do_user_addr_fault arch/x86/mm/fault.c:1475 [inline]
 __do_page_fault+0x5f6/0xd70 arch/x86/mm/fault.c:1541
 do_page_fault+0xf2/0x7e0 arch/x86/mm/fault.c:1572
 page_fault+0x1e/0x30 arch/x86/entry/entry_64.S:1143
RIP: 0033:0x400a57
Code: 00 00 00 00 e8 ba 59 04 00 8b 03 85 c0 74 d8 c7 45 08 00 00 00 00 83  
7d 04 05 0f 87 49 02 00 00 8b 45 04 ff 24 c5 e8 e4 4a 00  04 25 fa ff  
00 20 2e 2f 62 75 66 c7 04 25 fe ff 00 20 73 00 b9

RSP: 002b:7f48cd9c0dc0 EFLAGS: 00010293
RAX:  RBX: 006dbc28 RCX: 00446409
RDX: 00446409 RSI: 0081 RDI: 006dbc2c
RBP: 006dbc20 R08:  R09: 
R10:  R11: 0246 R12: 006dbc2c
R13: 7ffd0bb6676f R14: 7f48cd9c19c0 R15: 006dbd2c

Allocated by task 8196:
 save_stack+0x43/0xd0 mm/kasan/common.c:73
 set_track mm/kasan/common.c:85 [inline]
 kasan_kmalloc+0xcb/0xd0 mm/kasan/common.c:482
 kasan_slab_alloc+0x12/0x20 mm/kasan/common.c:397
 kmem_cache_alloc+0x130/0x730 mm/slab.c:3541
 vm_area_alloc+0x7a/0x1d0 kernel/fork.c:331
 mmap_region+0x9d7/0x1cd0 mm/mmap.c:1756
 do_mmap+0xa22/0x1230 mm/mmap.c:1559
 do_mmap_pgoff include/linux/mm.h:2421 [inline]
 vm_mmap_pgoff+0x213/0x2c0 mm/util.c:350
 ksys_mmap_pgoff+0x4da/0x660 mm/mmap.c:1609
 __do_sys_mmap arch/x86/kernel/sys_x86_64.c:99 [inline]
 __se_sys_mmap arch/x86/kernel/sys_x86_64.c:90 [inline]
 __x64_sys_mmap+0xe9/0x1b0 arch/x86/kernel/sys_x86_64.c:90
 do_syscall_64+0x1b9/0x820 arch/x86/entry/common.c:290
 entry_SYSCALL_64_after_hwframe+0x49/0xbe

Freed by task 8197:
 save_stack+0x43/0xd0 mm/kasan/common.c:73
 set_track mm/kasan/common.c:85 [inline]
 __kasan_slab_free+0x102/0x150 mm/kasan/common.c:444
 kasan_slab_free+0xe/0x10 mm/kasan/common.c:452
 __cache_free mm/slab.c:3485 [inline]
 kmem_cache_free+0x83/0x290 mm/slab.c:3747
 vm_area_free+0x1c/0x20 kernel/fork.c:350
 remove_vma+0x13a/0x180 mm/mmap.c:185
 remove_vma_list mm/mmap.c:2585 [inline]
 __do_munmap+0x729/0xf50 mm/mmap.c:2822
 do_munmap mm/mmap.c:2830 [inline]
 mmap_region+0x6a7/0x1cd0 mm/mmap.c:1729
 do_mmap+0xa22/0x1230 mm/mmap.c:1559
 do_mmap_pgoff include/linux/mm.h:2421 [inline]
 vm_mmap_pgoff+0x213/0x2c0 mm/util.c:350
 ksys_mmap_pgoff+0x4da/0x660 mm/mmap.c:1609
 __do_sys_mmap arch/x86/kernel/sys_x86_64.c:99 [inline]
 __se_sys_mmap arch/x86/kernel/sys_x86_64.c:90 [inline]
 __x64_sys_mmap+0xe9/0x1b0 arch/x86/kernel/sys_x86_64.c:90
 do_syscall_64+0x1b9/0x820 arch/x86/entry/common.c:290
 entry_SYSCALL_64_after_hwframe+0x49/0xbe

The buggy address belongs to the object at 8881b1502670
 which belongs to the cache vm_area_struct of size 200
The buggy address is located 64 bytes inside of
 200-byte region [8881b1502670, 8881b1502738)
The buggy address belongs to the page:
page:ea0006c54080 count:1 mapcount:0 mapping:8881da9827c0  
index:0x8881b1502eb0

flags: 0x2fffc000200(slab)
raw: 02fffc000200 ea0007477408 ea0006c5ec48 8881da9827c0
raw: 8881b1502eb0 8881b1502040 00010004 
page dumped because: kasan: bad access detected

Memory state around the buggy