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

2020-05-29 Thread Jason Gunthorpe
On Thu, May 28, 2020 at 07:33:16AM -0700, syzbot wrote:
> Hello,
> 
> syzbot found the following crash on:
> 
> HEAD commit:c11d28ab Add linux-next specific files for 20200522
> git tree:   linux-next
> console output: https://syzkaller.appspot.com/x/log.txt?x=12fc3e7210
> kernel config:  https://syzkaller.appspot.com/x/.config?x=3f6dbdea4159fb66
> dashboard link: https://syzkaller.appspot.com/bug?extid=478fd0d54412b8759e0d
> compiler:   gcc (GCC) 9.0.0 20181231 (experimental)
> 
> Unfortunately, I don't have any reproducer for this crash yet.
> 
> IMPORTANT: if you fix the bug, please add the following tag to the commit:
> Reported-by: syzbot+478fd0d54412b8759...@syzkaller.appspotmail.com
> 
> ==
> BUG: KASAN: use-after-free in ib_uverbs_remove_one+0x411/0x4e0 
> drivers/infiniband/core/uverbs_main.c:1210
> Read of size 4 at addr 888096324578 by task syz-executor.2/15847
> 
> CPU: 1 PID: 15847 Comm: syz-executor.2 Not tainted 
> 5.7.0-rc6-next-20200522-syzkaller #0
> 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+0x18f/0x20d lib/dump_stack.c:118
>  print_address_description.constprop.0.cold+0xd3/0x413 mm/kasan/report.c:383
>  __kasan_report mm/kasan/report.c:513 [inline]
>  kasan_report.cold+0x1f/0x37 mm/kasan/report.c:530
>  ib_uverbs_remove_one+0x411/0x4e0 drivers/infiniband/core/uverbs_main.c:1210
>  remove_client_context+0xbe/0x110 drivers/infiniband/core/device.c:732
>  disable_device+0x13b/0x230 drivers/infiniband/core/device.c:1278
>  __ib_unregister_device+0x91/0x180 drivers/infiniband/core/device.c:1445
>  ib_unregister_device_and_put+0x57/0x80 drivers/infiniband/core/device.c:1508
>  nldev_dellink+0x20a/0x310 drivers/infiniband/core/nldev.c:1571
>  rdma_nl_rcv_msg drivers/infiniband/core/netlink.c:195 [inline]
>  rdma_nl_rcv_skb drivers/infiniband/core/netlink.c:239 [inline]
>  rdma_nl_rcv+0x586/0x900 drivers/infiniband/core/netlink.c:259
>  netlink_unicast_kernel net/netlink/af_netlink.c:1303 [inline]
>  netlink_unicast+0x537/0x740 net/netlink/af_netlink.c:1329
>  netlink_sendmsg+0x882/0xe10 net/netlink/af_netlink.c:1918
>  sock_sendmsg_nosec net/socket.c:652 [inline]
>  sock_sendmsg+0xcf/0x120 net/socket.c:672
>  sys_sendmsg+0x6e6/0x810 net/socket.c:2352
>  ___sys_sendmsg+0x100/0x170 net/socket.c:2406
>  __sys_sendmsg+0xe5/0x1b0 net/socket.c:2439
>  do_syscall_64+0xf6/0x7d0 arch/x86/entry/common.c:295
>  entry_SYSCALL_64_after_hwframe+0x49/0xb3
> RIP: 0033:0x45ca29
> Code: 0d b7 fb ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 89 f8 48 89 f7 48 
> 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 0f 
> 83 db b6 fb ff c3 66 2e 0f 1f 84 00 00 00 00
> RSP: 002b:7fd6764d1c78 EFLAGS: 0246 ORIG_RAX: 002e
> RAX: ffda RBX: 004ffa00 RCX: 0045ca29
> RDX:  RSI: 2180 RDI: 0003
> RBP: 0078bf00 R08:  R09: 
> R10:  R11: 0246 R12: 
> R13: 09af R14: 004d61b0 R15: 7fd6764d26d4
> 
> Allocated by task 9061:
>  save_stack+0x1b/0x40 mm/kasan/common.c:48
>  set_track mm/kasan/common.c:56 [inline]
>  __kasan_kmalloc mm/kasan/common.c:494 [inline]
>  __kasan_kmalloc.constprop.0+0xbf/0xd0 mm/kasan/common.c:467
>  kmem_cache_alloc_trace+0x153/0x7d0 mm/slab.c:3551
>  kmalloc include/linux/slab.h:555 [inline]
>  kzalloc include/linux/slab.h:669 [inline]
>  ib_uverbs_add_one+0x80/0x7c0 drivers/infiniband/core/uverbs_main.c:1107
>  add_client_context+0x400/0x5e0 drivers/infiniband/core/device.c:677
>  enable_device_and_get+0x1cd/0x3b0 drivers/infiniband/core/device.c:1326
>  ib_register_device drivers/infiniband/core/device.c:1392 [inline]
>  ib_register_device+0xa12/0xda0 drivers/infiniband/core/device.c:1353
>  siw_device_register drivers/infiniband/sw/siw/siw_main.c:70 [inline]
>  siw_newlink drivers/infiniband/sw/siw/siw_main.c:565 [inline]
>  siw_newlink+0xd37/0x1240 drivers/infiniband/sw/siw/siw_main.c:542
>  nldev_newlink+0x29e/0x420 drivers/infiniband/core/nldev.c:1541
>  rdma_nl_rcv_msg drivers/infiniband/core/netlink.c:195 [inline]
>  rdma_nl_rcv_skb drivers/infiniband/core/netlink.c:239 [inline]
>  rdma_nl_rcv+0x586/0x900 drivers/infiniband/core/netlink.c:259
>  netlink_unicast_kernel net/netlink/af_netlink.c:1303 [inline]
>  netlink_unicast+0x537/0x740 net/netlink/af_netlink.c:1329
>  netlink_sendmsg+0x882/0xe10 net/netlink/af_netlink.c:1918
>  sock_sendmsg_nosec net/socket.c:652 [inline]
>  sock_sendmsg+0xcf/0x120 net/socket.c:672
>  sys_sendmsg+0x6e6/0x810 net/socket.c:2352
>  ___sys_sendmsg+0x100/0x170 net/socket.c:2406
>  __sys_sendmsg+0xe5/0x1b0 net/socket.c:2439
>  do_syscall_64+0xf6/0x7d0 arch/x86/entry/common.c:295
>  entry_SYSCALL_64_after_hwframe+0x49/0xb3
> 
> Freed by task 

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

2020-05-29 Thread Jason Gunthorpe
On Fri, May 29, 2020 at 04:31:26PM +0800, Hillf Danton wrote:
> Hold another grab to dev to prevent it from going home before work gets
> done with it.
> 
> +++ b/drivers/infiniband/core/uverbs_main.c
> @@ -1152,6 +1152,8 @@ static int ib_uverbs_add_one(struct ib_d
> device->ops.mmap ? _mmap_fops : _fops);
>   uverbs_dev->cdev.owner = THIS_MODULE;
>  
> + /* pair with put_device() in ib_uverbs_remove_one() */
> + get_device(_dev->dev);
>   ret = cdev_device_add(_dev->cdev, _dev->dev);
>   if (ret)
>   goto err_uapi;

Doesn't look right, the put_device() in uverbs_remove_one() pairs with
the device_initialize() in this function.

The only thing I can think of is we called remove_once twice
somehow or had an extra put on some error path. But I couldn't find
any flow that would do either of those things

Jason


KASAN: use-after-free Read in ib_uverbs_remove_one

2020-05-28 Thread syzbot
Hello,

syzbot found the following crash on:

HEAD commit:c11d28ab Add linux-next specific files for 20200522
git tree:   linux-next
console output: https://syzkaller.appspot.com/x/log.txt?x=12fc3e7210
kernel config:  https://syzkaller.appspot.com/x/.config?x=3f6dbdea4159fb66
dashboard link: https://syzkaller.appspot.com/bug?extid=478fd0d54412b8759e0d
compiler:   gcc (GCC) 9.0.0 20181231 (experimental)

Unfortunately, I don't have any reproducer for this crash yet.

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

==
BUG: KASAN: use-after-free in ib_uverbs_remove_one+0x411/0x4e0 
drivers/infiniband/core/uverbs_main.c:1210
Read of size 4 at addr 888096324578 by task syz-executor.2/15847

CPU: 1 PID: 15847 Comm: syz-executor.2 Not tainted 
5.7.0-rc6-next-20200522-syzkaller #0
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+0x18f/0x20d lib/dump_stack.c:118
 print_address_description.constprop.0.cold+0xd3/0x413 mm/kasan/report.c:383
 __kasan_report mm/kasan/report.c:513 [inline]
 kasan_report.cold+0x1f/0x37 mm/kasan/report.c:530
 ib_uverbs_remove_one+0x411/0x4e0 drivers/infiniband/core/uverbs_main.c:1210
 remove_client_context+0xbe/0x110 drivers/infiniband/core/device.c:732
 disable_device+0x13b/0x230 drivers/infiniband/core/device.c:1278
 __ib_unregister_device+0x91/0x180 drivers/infiniband/core/device.c:1445
 ib_unregister_device_and_put+0x57/0x80 drivers/infiniband/core/device.c:1508
 nldev_dellink+0x20a/0x310 drivers/infiniband/core/nldev.c:1571
 rdma_nl_rcv_msg drivers/infiniband/core/netlink.c:195 [inline]
 rdma_nl_rcv_skb drivers/infiniband/core/netlink.c:239 [inline]
 rdma_nl_rcv+0x586/0x900 drivers/infiniband/core/netlink.c:259
 netlink_unicast_kernel net/netlink/af_netlink.c:1303 [inline]
 netlink_unicast+0x537/0x740 net/netlink/af_netlink.c:1329
 netlink_sendmsg+0x882/0xe10 net/netlink/af_netlink.c:1918
 sock_sendmsg_nosec net/socket.c:652 [inline]
 sock_sendmsg+0xcf/0x120 net/socket.c:672
 sys_sendmsg+0x6e6/0x810 net/socket.c:2352
 ___sys_sendmsg+0x100/0x170 net/socket.c:2406
 __sys_sendmsg+0xe5/0x1b0 net/socket.c:2439
 do_syscall_64+0xf6/0x7d0 arch/x86/entry/common.c:295
 entry_SYSCALL_64_after_hwframe+0x49/0xb3
RIP: 0033:0x45ca29
Code: 0d b7 fb ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 89 f8 48 89 f7 48 
89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 0f 83 
db b6 fb ff c3 66 2e 0f 1f 84 00 00 00 00
RSP: 002b:7fd6764d1c78 EFLAGS: 0246 ORIG_RAX: 002e
RAX: ffda RBX: 004ffa00 RCX: 0045ca29
RDX:  RSI: 2180 RDI: 0003
RBP: 0078bf00 R08:  R09: 
R10:  R11: 0246 R12: 
R13: 09af R14: 004d61b0 R15: 7fd6764d26d4

Allocated by task 9061:
 save_stack+0x1b/0x40 mm/kasan/common.c:48
 set_track mm/kasan/common.c:56 [inline]
 __kasan_kmalloc mm/kasan/common.c:494 [inline]
 __kasan_kmalloc.constprop.0+0xbf/0xd0 mm/kasan/common.c:467
 kmem_cache_alloc_trace+0x153/0x7d0 mm/slab.c:3551
 kmalloc include/linux/slab.h:555 [inline]
 kzalloc include/linux/slab.h:669 [inline]
 ib_uverbs_add_one+0x80/0x7c0 drivers/infiniband/core/uverbs_main.c:1107
 add_client_context+0x400/0x5e0 drivers/infiniband/core/device.c:677
 enable_device_and_get+0x1cd/0x3b0 drivers/infiniband/core/device.c:1326
 ib_register_device drivers/infiniband/core/device.c:1392 [inline]
 ib_register_device+0xa12/0xda0 drivers/infiniband/core/device.c:1353
 siw_device_register drivers/infiniband/sw/siw/siw_main.c:70 [inline]
 siw_newlink drivers/infiniband/sw/siw/siw_main.c:565 [inline]
 siw_newlink+0xd37/0x1240 drivers/infiniband/sw/siw/siw_main.c:542
 nldev_newlink+0x29e/0x420 drivers/infiniband/core/nldev.c:1541
 rdma_nl_rcv_msg drivers/infiniband/core/netlink.c:195 [inline]
 rdma_nl_rcv_skb drivers/infiniband/core/netlink.c:239 [inline]
 rdma_nl_rcv+0x586/0x900 drivers/infiniband/core/netlink.c:259
 netlink_unicast_kernel net/netlink/af_netlink.c:1303 [inline]
 netlink_unicast+0x537/0x740 net/netlink/af_netlink.c:1329
 netlink_sendmsg+0x882/0xe10 net/netlink/af_netlink.c:1918
 sock_sendmsg_nosec net/socket.c:652 [inline]
 sock_sendmsg+0xcf/0x120 net/socket.c:672
 sys_sendmsg+0x6e6/0x810 net/socket.c:2352
 ___sys_sendmsg+0x100/0x170 net/socket.c:2406
 __sys_sendmsg+0xe5/0x1b0 net/socket.c:2439
 do_syscall_64+0xf6/0x7d0 arch/x86/entry/common.c:295
 entry_SYSCALL_64_after_hwframe+0x49/0xb3

Freed by task 15847:
 save_stack+0x1b/0x40 mm/kasan/common.c:48
 set_track mm/kasan/common.c:56 [inline]
 kasan_set_free_info mm/kasan/common.c:316 [inline]
 __kasan_slab_free+0xf7/0x140 mm/kasan/common.c:455
 __cache_free mm/slab.c:3426 [inline]