KASAN: use-after-free Read in handle_userfault

41 views
Skip to first unread message

syzbot

unread,
Oct 27, 2017, 5:44:01 AM10/27/17
to linux-...@vger.kernel.org, linux-...@vger.kernel.org, syzkall...@googlegroups.com, vi...@zeniv.linux.org.uk
Hello,

syzkaller hit the following crash on
a31cc455c512f3f1dd5f79cac8e29a7c8a617af8
git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/master
compiler: gcc (GCC) 7.1.1 20170620
.config is attached
Raw console output is attached.
C reproducer is attached
syzkaller reproducer is attached. See https://goo.gl/kgGztJ
for information about syzkaller reproducers


BUG: KASAN: use-after-free in handle_userfault+0x206f/0x2390
fs/userfaultfd.c:371
Read of size 8 at addr ffff8801ce9cad88 by task syzkaller547101/2993

CPU: 0 PID: 2993 Comm: syzkaller547101 Not tainted 4.13.0-next-20170911+ #19
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS
Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:16 [inline]
dump_stack+0x194/0x257 lib/dump_stack.c:52
print_address_description+0x73/0x250 mm/kasan/report.c:252
kasan_report_error mm/kasan/report.c:351 [inline]
kasan_report+0x24e/0x340 mm/kasan/report.c:409
__asan_report_load8_noabort+0x14/0x20 mm/kasan/report.c:430
handle_userfault+0x206f/0x2390 fs/userfaultfd.c:371
do_anonymous_page mm/memory.c:3095 [inline]
handle_pte_fault mm/memory.c:3908 [inline]
__handle_mm_fault+0x2d46/0x39c0 mm/memory.c:4034
handle_mm_fault+0x334/0x8d0 mm/memory.c:4071
__do_page_fault+0x4f6/0xb60 arch/x86/mm/fault.c:1445
do_page_fault+0xee/0x720 arch/x86/mm/fault.c:1520
page_fault+0x22/0x30 arch/x86/entry/entry_64.S:1066
RIP: 0033:0x445455
RSP: 002b:0000000020013000 EFLAGS: 00010217
RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000445449
RDX: 0000000020059ffc RSI: 0000000020013000 RDI: 0000000000000400
RBP: 0000000000000000 R08: 0000000020058ffd R09: 00007f14ef18c700
R10: 0000000020058ffc R11: 0000000000000202 R12: 0000000000000000
R13: 00007ffca43554df R14: 00007f14ef18c9c0 R15: 0000000000000000

Allocated by task 2991:
save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59
save_stack+0x43/0xd0 mm/kasan/kasan.c:447
set_track mm/kasan/kasan.c:459 [inline]
kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:551
kasan_slab_alloc+0x12/0x20 mm/kasan/kasan.c:489
kmem_cache_alloc+0x12e/0x760 mm/slab.c:3561
dup_userfaultfd+0x21c/0x890 fs/userfaultfd.c:653
dup_mmap kernel/fork.c:658 [inline]
dup_mm kernel/fork.c:1179 [inline]
copy_mm+0xa38/0x1310 kernel/fork.c:1233
copy_process.part.36+0x1eae/0x4af0 kernel/fork.c:1735
copy_process kernel/fork.c:1548 [inline]
_do_fork+0x1ef/0xfe0 kernel/fork.c:2027
SYSC_clone kernel/fork.c:2137 [inline]
SyS_clone+0x37/0x50 kernel/fork.c:2131
do_syscall_64+0x26c/0x8c0 arch/x86/entry/common.c:287
return_from_SYSCALL_64+0x0/0x7a

Freed by task 2991:
save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59
save_stack+0x43/0xd0 mm/kasan/kasan.c:447
set_track mm/kasan/kasan.c:459 [inline]
kasan_slab_free+0x71/0xc0 mm/kasan/kasan.c:524
__cache_free mm/slab.c:3503 [inline]
kmem_cache_free+0x77/0x280 mm/slab.c:3763
userfaultfd_ctx_put+0x50c/0x740 fs/userfaultfd.c:165
userfaultfd_event_wait_completion+0x754/0x910 fs/userfaultfd.c:599
dup_fctx fs/userfaultfd.c:687 [inline]
dup_userfaultfd_complete+0x2de/0x480 fs/userfaultfd.c:695
dup_mmap kernel/fork.c:726 [inline]
dup_mm kernel/fork.c:1179 [inline]
copy_mm+0xe9b/0x1310 kernel/fork.c:1233
copy_process.part.36+0x1eae/0x4af0 kernel/fork.c:1735
copy_process kernel/fork.c:1548 [inline]
_do_fork+0x1ef/0xfe0 kernel/fork.c:2027
SYSC_clone kernel/fork.c:2137 [inline]
SyS_clone+0x37/0x50 kernel/fork.c:2131
do_syscall_64+0x26c/0x8c0 arch/x86/entry/common.c:287
return_from_SYSCALL_64+0x0/0x7a

The buggy address belongs to the object at ffff8801ce9cac00
which belongs to the cache userfaultfd_ctx_cache of size 400
The buggy address is located 392 bytes inside of
400-byte region [ffff8801ce9cac00, ffff8801ce9cad90)
The buggy address belongs to the page:
page:ffffea00073a7280 count:1 mapcount:0 mapping:ffff8801ce9ca000
index:0xffff8801cf2f5300
flags: 0x200000000000100(slab)
raw: 0200000000000100 ffff8801ce9ca000 ffff8801cf2f5300 0000000100000008
raw: ffff8801d56d6f50 ffff8801d56d6f50 ffff8801d56d2600 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
ffff8801ce9cac80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff8801ce9cad00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ffff8801ce9cad80: fb fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc
^
ffff8801ce9cae00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff8801ce9cae80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================


---
This bug is generated by a dumb bot. It may contain errors.
See https://goo.gl/tpsmEJ for details.
Direct all questions to syzk...@googlegroups.com.

syzbot will keep track of this bug report.
Once a fix for this bug is committed, please reply to this email with:
#syz fix: exact-commit-title
To mark this as a duplicate of another syzbot report, please reply with:
#syz dup: exact-subject-of-another-report
If it's a one-off invalid bug report, please reply with:
#syz invalid
Note: if the crash happens again, it will cause creation of a new bug
report.
config.txt
raw.log
repro.txt
repro.c

Dmitry Vyukov

unread,
Oct 27, 2017, 5:46:35 AM10/27/17
to syzbot, linux-...@vger.kernel.org, LKML, syzkall...@googlegroups.com, Al Viro
On Fri, Oct 27, 2017 at 11:44 AM, syzbot
<bot+998c483ca801a50e3c...@syzkaller.appspotmail.com>
wrote:
> Hello,
>
> syzkaller hit the following crash on
> a31cc455c512f3f1dd5f79cac8e29a7c8a617af8
> git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/master
> compiler: gcc (GCC) 7.1.1 20170620
> .config is attached
> Raw console output is attached.
> C reproducer is attached
> syzkaller reproducer is attached. See https://goo.gl/kgGztJ
> for information about syzkaller reproducers


Last happened on upstream f34157878d3b17641ad2366988600c23c89d98b2 (Oct 26)

==================================================================
BUG: KASAN: use-after-free in handle_userfault+0x2076/0x23a0
fs/userfaultfd.c:371
Read of size 8 at addr ffff8801c9f33160 by task syz-executor5/13866

CPU: 1 PID: 13866 Comm: syz-executor5 Not tainted 4.14.0-rc6+ #147
Hardware name: Google Google Compute Engine/Google Compute Engine,
BIOS Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:16 [inline]
dump_stack+0x194/0x257 lib/dump_stack.c:52
print_address_description+0x73/0x250 mm/kasan/report.c:252
kasan_report_error mm/kasan/report.c:351 [inline]
kasan_report+0x25b/0x340 mm/kasan/report.c:409
__asan_report_load8_noabort+0x14/0x20 mm/kasan/report.c:430
handle_userfault+0x2076/0x23a0 fs/userfaultfd.c:371
do_anonymous_page mm/memory.c:3135 [inline]
handle_pte_fault mm/memory.c:3908 [inline]
__handle_mm_fault+0x3823/0x39c0 mm/memory.c:4034
handle_mm_fault+0x334/0x8d0 mm/memory.c:4071
__do_page_fault+0x5bd/0xd60 arch/x86/mm/fault.c:1444
do_page_fault+0xee/0x720 arch/x86/mm/fault.c:1520
page_fault+0x22/0x30 arch/x86/entry/entry_64.S:1066
RIP: 0010:__clear_user+0x42/0x70 arch/x86/lib/usercopy_64.c:23
RSP: 0000:ffff8801cbe3fb88 EFLAGS: 00010202
RAX: 0000000000000000 RBX: 0000000000000008 RCX: 0000000000000008
RDX: 0000000000000000 RSI: 0000000000000008 RDI: 0000000020038e40
RBP: ffff8801cbe3fb98 R08: 0000000000000001 R09: 1ffff100397c7f4d
R10: ffff8801cbe3fa30 R11: 0000000000000000 R12: 0000000020038e40
R13: ffff8801cc622580 R14: 0000000020038c40 R15: 00007ffffffff000
copy_xregs_to_user arch/x86/include/asm/fpu/internal.h:371 [inline]
copy_fpregs_to_sigframe arch/x86/kernel/fpu/signal.c:125 [inline]
copy_fpstate_to_sigframe+0x196/0x470 arch/x86/kernel/fpu/signal.c:176
get_sigframe arch/x86/kernel/signal.c:283 [inline]
__setup_rt_frame arch/x86/kernel/signal.c:464 [inline]
setup_rt_frame arch/x86/kernel/signal.c:698 [inline]
handle_signal arch/x86/kernel/signal.c:742 [inline]
do_signal+0x9f6/0x1ee0 arch/x86/kernel/signal.c:810
exit_to_usermode_loop+0x214/0x310 arch/x86/entry/common.c:158
prepare_exit_to_usermode+0x273/0x2d0 arch/x86/entry/common.c:197
retint_user+0x8/0x20
RIP: 0033: (null)
RSP: 002b:0000000020039008 EFLAGS: 00010217
RAX: 0000000000000000 RBX: 0000000000758020 RCX: 0000000000452869
RDX: 0000000020059ffc RSI: 0000000020039000 RDI: 0000000000000400
RBP: 0000000000000045 R08: 0000000020058ffd R09: 0000000000000000
R10: 0000000020058ffc R11: 0000000000000212 R12: 00000000006ee718
R13: 00000000ffffffff R14: 00007f94a95396d4 R15: 0000000000000000

Allocated by task 13844:
save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59
save_stack+0x43/0xd0 mm/kasan/kasan.c:447
set_track mm/kasan/kasan.c:459 [inline]
kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:551
kasan_slab_alloc+0x12/0x20 mm/kasan/kasan.c:489
kmem_cache_alloc+0x12e/0x760 mm/slab.c:3561
dup_userfaultfd+0x21c/0x890 fs/userfaultfd.c:659
dup_mmap kernel/fork.c:662 [inline]
dup_mm kernel/fork.c:1201 [inline]
copy_mm+0xa38/0x1310 kernel/fork.c:1255
copy_process.part.39+0x1ea3/0x4af0 kernel/fork.c:1757
copy_process kernel/fork.c:1570 [inline]
_do_fork+0x1ef/0xfb0 kernel/fork.c:2049
SYSC_clone kernel/fork.c:2159 [inline]
SyS_clone+0x37/0x50 kernel/fork.c:2153
do_syscall_64+0x26c/0x8d0 arch/x86/entry/common.c:287
return_from_SYSCALL_64+0x0/0x7a

Freed by task 13844:
save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59
save_stack+0x43/0xd0 mm/kasan/kasan.c:447
set_track mm/kasan/kasan.c:459 [inline]
kasan_slab_free+0x71/0xc0 mm/kasan/kasan.c:524
__cache_free mm/slab.c:3503 [inline]
kmem_cache_free+0x77/0x280 mm/slab.c:3763
userfaultfd_ctx_put+0x50c/0x740 fs/userfaultfd.c:165
userfaultfd_event_wait_completion+0x788/0x9c0 fs/userfaultfd.c:605
dup_fctx fs/userfaultfd.c:693 [inline]
dup_userfaultfd_complete+0x2de/0x480 fs/userfaultfd.c:701
dup_mmap kernel/fork.c:730 [inline]
dup_mm kernel/fork.c:1201 [inline]
copy_mm+0xe9b/0x1310 kernel/fork.c:1255
copy_process.part.39+0x1ea3/0x4af0 kernel/fork.c:1757
copy_process kernel/fork.c:1570 [inline]
_do_fork+0x1ef/0xfb0 kernel/fork.c:2049
SYSC_clone kernel/fork.c:2159 [inline]
SyS_clone+0x37/0x50 kernel/fork.c:2153
do_syscall_64+0x26c/0x8d0 arch/x86/entry/common.c:287
return_from_SYSCALL_64+0x0/0x7a

The buggy address belongs to the object at ffff8801c9f33000
which belongs to the cache userfaultfd_ctx_cache of size 360
The buggy address is located 352 bytes inside of
360-byte region [ffff8801c9f33000, ffff8801c9f33168)
The buggy address belongs to the page:
page:ffffea000727ccc0 count:1 mapcount:0 mapping:ffff8801c9f33000
index:0xffff8801c9f33ff7
flags: 0x200000000000100(slab)
raw: 0200000000000100 ffff8801c9f33000 ffff8801c9f33ff7 0000000100000007
raw: ffffea0007473820 ffffea00071fb220 ffff8801d64d2300 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
ffff8801c9f33000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff8801c9f33080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff8801c9f33100: fb fb fb fb fb fb fb fb fb fb fb fb fb fc fc fc
^
ffff8801c9f33180: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb
ffff8801c9f33200: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> --
> 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-bug...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/syzkaller-bugs/001a114c9224e34a49055c842032%40google.com.
> For more options, visit https://groups.google.com/d/optout.

Eric Biggers

unread,
Nov 27, 2017, 1:15:21 AM11/27/17
to Dmitry Vyukov, syzbot, linux-...@vger.kernel.org, LKML, syzkall...@googlegroups.com, Al Viro, Andrea Arcangeli, Pavel Emelyanov, linu...@kvack.org
+Cc aarc...@redhat.com, xe...@parallels.com, linu...@kvack.org

On Fri, Oct 27, 2017 at 11:46:13AM +0200, Dmitry Vyukov wrote:
> On Fri, Oct 27, 2017 at 11:44 AM, syzbot
> <bot+998c483ca801a50e3c...@syzkaller.appspotmail.com>
> wrote:
> > Hello,
> >
> > syzkaller hit the following crash on
> > a31cc455c512f3f1dd5f79cac8e29a7c8a617af8
> > git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/master
> > compiler: gcc (GCC) 7.1.1 20170620
> > .config is attached
> > Raw console output is attached.
> > C reproducer is attached
> > syzkaller reproducer is attached. See https://goo.gl/kgGztJ
> > for information about syzkaller reproducers
>

Andrea or Pavel, can one of you please fix this? It's another use-after-free
related to userfaultfd "fork events", and it can easily be triggered by an
unprivileged user. It was reported a month ago already; the original report is
here: https://groups.google.com/forum/#!topic/syzkaller-bugs/sS99S-Z-9No.
(Please consider adding yourself and/or linux-mm to the MAINTAINERS file for
fs/userfaultfd.c, so that you are Cc'ed on userfaultfd bug reports.) In
userfaultfd_event_wait_completion(), called from dup_fctx(), the kernel is
freeing the the new userfaultfd_ctx because the old one had all its fd's closed,
but actually the new one is still in use by the new mm_struct.

Also, I've simplified the C reproducer:

#include <linux/sched.h>
#include <linux/userfaultfd.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>

static int userfaultfd;
static void *page;

static void *close_fd_proc(void *arg)
{
usleep(1000);
close(userfaultfd);
return NULL;
}

int main()
{
pthread_t t;
struct uffdio_api api = { 0 };
struct uffdio_register reg = { 0 };

page = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);

userfaultfd = syscall(__NR_userfaultfd, 0);

api.api = UFFDIO;
api.features = UFFD_FEATURE_EVENT_FORK;
ioctl(userfaultfd, UFFDIO_API, &api);

reg.range.start = (__u64)page;
reg.range.len = 4096;
reg.mode = UFFDIO_REGISTER_MODE_MISSING;
ioctl(userfaultfd, UFFDIO_REGISTER, &reg);

pthread_create(&t, NULL, close_fd_proc, NULL);

syscall(__NR_clone, CLONE_FILES, page, NULL, NULL, NULL);

return 0;
}

Eric Biggers

unread,
Jan 9, 2018, 4:59:44 PM1/9/18
to Dmitry Vyukov, syzbot, linux-...@vger.kernel.org, LKML, syzkall...@googlegroups.com, Al Viro, Andrea Arcangeli, Pavel Emelyanov, linu...@kvack.org
Fixed now:

#syz fix: userfaultfd: clear the vma->vm_userfaultfd_ctx if UFFD_EVENT_FORK fails
Reply all
Reply to author
Forward
0 new messages