Subject: KASAN: use-after-free Read in tls_sk_proto_close

From: syzbot <[email-masked]>
Date: Mon, 15 Jan 2018 16:01:01 -0800

syzkaller has found reproducer for the following crash on  
594831a8aba3fd045c3212a3e3bb9788c77b989d
git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-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

IMPORTANT: if you fix the bug, please add the following tag to the commit:
Reported-by: [email-masked]
It will help syzbot understand when the bug is fixed.

TCP: request_sock_TCP: Possible SYN flooding on port 20000. Sending  
cookies.  Check SNMP counters.
==================================================================
BUG: KASAN: use-after-free in tls_sk_proto_close+0x7a0/0x800  
net/tls/tls_main.c:229
Read of size 8 at addr ffff8801d3329c88 by task syzkaller186931/3663

CPU: 1 PID: 3663 Comm: syzkaller186931 Not tainted 4.15.0-rc7+ #190
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS  
Google 01/01/2011
Call Trace:
  __dump_stack lib/dump_stack.c:17 [inline]
  dump_stack+0x194/0x257 lib/dump_stack.c:53
  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
  tls_sk_proto_close+0x7a0/0x800 net/tls/tls_main.c:229
  inet_release+0xed/0x1c0 net/ipv4/af_inet.c:427
  sock_release+0x8d/0x1e0 net/socket.c:595
  sock_close+0x16/0x20 net/socket.c:1123
  __fput+0x327/0x7e0 fs/file_table.c:210
  ____fput+0x15/0x20 fs/file_table.c:244
  task_work_run+0x199/0x270 kernel/task_work.c:113
  exit_task_work include/linux/task_work.h:22 [inline]
  do_exit+0x9bb/0x1ad0 kernel/exit.c:865
  do_group_exit+0x149/0x400 kernel/exit.c:968
  get_signal+0x73f/0x16c0 kernel/signal.c:2335
  do_signal+0x90/0x1eb0 arch/x86/kernel/signal.c:809
  exit_to_usermode_loop+0x214/0x310 arch/x86/entry/common.c:158
  prepare_exit_to_usermode arch/x86/entry/common.c:195 [inline]
  syscall_return_slowpath+0x490/0x550 arch/x86/entry/common.c:264
  entry_SYSCALL_64_fastpath+0x98/0x9a
RIP: 0033:0x4457a9
RSP: 002b:00007f715c685db8 EFLAGS: 00000246 ORIG_RAX: 00000000000000ca
RAX: fffffffffffffe00 RBX: 00000000006dac24 RCX: 00000000004457a9
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 00000000006dac24
RBP: 00000000006dac20 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007fff9cf582af R14: 00007f715c6869c0 R15: 0000000000000001

Allocated by task 3663:
  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
  kmem_cache_alloc_trace+0x136/0x750 mm/slab.c:3610
  kmalloc include/linux/slab.h:499 [inline]
  kzalloc include/linux/slab.h:688 [inline]
  tls_init+0x4b/0x240 net/tls/tls_main.c:458
  tcp_set_ulp+0x159/0x3e0 net/ipv4/tcp_ulp.c:127
  do_tcp_setsockopt.isra.37+0x316/0x2130 net/ipv4/tcp.c:2571
  tcp_setsockopt+0xb0/0xd0 net/ipv4/tcp.c:2874
  sock_common_setsockopt+0x95/0xd0 net/core/sock.c:2978
  SYSC_setsockopt net/socket.c:1823 [inline]
  SyS_setsockopt+0x189/0x360 net/socket.c:1802
  entry_SYSCALL_64_fastpath+0x23/0x9a

Freed by task 3663:
  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:3488 [inline]
  kfree+0xd6/0x260 mm/slab.c:3803
  tls_sk_proto_close+0x4bf/0x800 net/tls/tls_main.c:232
  inet_release+0xed/0x1c0 net/ipv4/af_inet.c:427
  sock_release+0x8d/0x1e0 net/socket.c:595
  sock_close+0x16/0x20 net/socket.c:1123
  __fput+0x327/0x7e0 fs/file_table.c:210
  ____fput+0x15/0x20 fs/file_table.c:244
  task_work_run+0x199/0x270 kernel/task_work.c:113
  exit_task_work include/linux/task_work.h:22 [inline]
  do_exit+0x9bb/0x1ad0 kernel/exit.c:865
  do_group_exit+0x149/0x400 kernel/exit.c:968
  get_signal+0x73f/0x16c0 kernel/signal.c:2335
  do_signal+0x90/0x1eb0 arch/x86/kernel/signal.c:809
  exit_to_usermode_loop+0x214/0x310 arch/x86/entry/common.c:158
  prepare_exit_to_usermode arch/x86/entry/common.c:195 [inline]
  syscall_return_slowpath+0x490/0x550 arch/x86/entry/common.c:264
  entry_SYSCALL_64_fastpath+0x98/0x9a

The buggy address belongs to the object at ffff8801d3329c00
  which belongs to the cache kmalloc-192 of size 192
The buggy address is located 136 bytes inside of
  192-byte region [ffff8801d3329c00, ffff8801d3329cc0)
The buggy address belongs to the page:
page:ffffea00074cca40 count:1 mapcount:0 mapping:ffff8801d3329000  
index:0xffff8801d3329d00
flags: 0x2fffc0000000100(slab)
raw: 02fffc0000000100 ffff8801d3329000 ffff8801d3329d00 000000010000000e
raw: ffffea00075198a0 ffffea0007474160 ffff8801dac00040 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
  ffff8801d3329b80: 00 00 00 00 00 00 00 00 fc fc fc fc fc fc fc fc
  ffff8801d3329c00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ffff8801d3329c80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
                       ^
  ffff8801d3329d00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
  ffff8801d3329d80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
==================================================================