Linux-Media Archive on lore.kernel.org
 help / color / Atom feed
* KASAN: use-after-free Read in v4l2_release (3)
@ 2020-01-08 20:24 syzbot
  2020-01-22 22:58 ` syzbot
       [not found] ` <20200123102707.2596-1-hdanton@sina.com>
  0 siblings, 2 replies; 12+ messages in thread
From: syzbot @ 2020-01-08 20:24 UTC (permalink / raw)
  To: andreyknvl, bnvandana, hverkuil-cisco, laurent.pinchart,
	linux-kernel, linux-media, linux-usb, mchehab, syzkaller-bugs

Hello,

syzbot found the following crash on:

HEAD commit:    ae179410 usb: gadget: add raw-gadget interface
git tree:       https://github.com/google/kasan.git usb-fuzzer
console output: https://syzkaller.appspot.com/x/log.txt?x=132aa915e00000
kernel config:  https://syzkaller.appspot.com/x/.config?x=57adcee0a60380e2
dashboard link: https://syzkaller.appspot.com/bug?extid=75287f75e2fedd69d680
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+75287f75e2fedd69d680@syzkaller.appspotmail.com

usbvision_radio_close: Final disconnect
==================================================================
BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390  
drivers/media/v4l2-core/v4l2-dev.c:459
Read of size 4 at addr ffff8881d13b5068 by task v4l_id/20136

CPU: 1 PID: 20136 Comm: v4l_id Not tainted 5.5.0-rc3-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+0xef/0x16e lib/dump_stack.c:118
  print_address_description.constprop.0+0x16/0x200 mm/kasan/report.c:374
  __kasan_report.cold+0x37/0x7f mm/kasan/report.c:506
  kasan_report+0xe/0x20 mm/kasan/common.c:639
  v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
  __fput+0x2d7/0x840 fs/file_table.c:280
  task_work_run+0x13f/0x1c0 kernel/task_work.c:113
  tracehook_notify_resume include/linux/tracehook.h:188 [inline]
  exit_to_usermode_loop+0x1d2/0x200 arch/x86/entry/common.c:164
  prepare_exit_to_usermode arch/x86/entry/common.c:195 [inline]
  syscall_return_slowpath arch/x86/entry/common.c:278 [inline]
  do_syscall_64+0x4e0/0x5c0 arch/x86/entry/common.c:304
  entry_SYSCALL_64_after_hwframe+0x49/0xbe
RIP: 0033:0x7f70446a32b0
Code: 40 75 0b 31 c0 48 83 c4 08 e9 0c ff ff ff 48 8d 3d c5 32 08 00 e8 c0  
07 02 00 83 3d 45 a3 2b 00 00 75 10 b8 03 00 00 00 0f 05 <48> 3d 01 f0 ff  
ff 73 31 c3 48 83 ec 08 e8 ce 8a 01 00 48 89 04 24
RSP: 002b:00007ffee1722fd8 EFLAGS: 00000246 ORIG_RAX: 0000000000000003
RAX: 0000000000000000 RBX: 0000000000000003 RCX: 00007f70446a32b0
RDX: 0000000000000013 RSI: 0000000080685600 RDI: 0000000000000003
RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000400884
R13: 00007ffee1723130 R14: 0000000000000000 R15: 0000000000000000

Allocated by task 14633:
  save_stack+0x1b/0x80 mm/kasan/common.c:72
  set_track mm/kasan/common.c:80 [inline]
  __kasan_kmalloc mm/kasan/common.c:513 [inline]
  __kasan_kmalloc.constprop.0+0xbf/0xd0 mm/kasan/common.c:486
  kmalloc include/linux/slab.h:556 [inline]
  kzalloc include/linux/slab.h:670 [inline]
  usbvision_alloc drivers/media/usb/usbvision/usbvision-video.c:1315 [inline]
  usbvision_probe.cold+0x5c5/0x1f21  
drivers/media/usb/usbvision/usbvision-video.c:1469
  usb_probe_interface+0x305/0x7a0 drivers/usb/core/driver.c:361
  really_probe+0x281/0x6d0 drivers/base/dd.c:548
  driver_probe_device+0x104/0x210 drivers/base/dd.c:721
  __device_attach_driver+0x1c2/0x220 drivers/base/dd.c:828
  bus_for_each_drv+0x162/0x1e0 drivers/base/bus.c:430
  __device_attach+0x217/0x360 drivers/base/dd.c:894
  bus_probe_device+0x1e4/0x290 drivers/base/bus.c:490
  device_add+0x141f/0x1bc0 drivers/base/core.c:2487
  usb_set_configuration+0xe38/0x16c0 drivers/usb/core/message.c:2023
  generic_probe+0x9d/0xd5 drivers/usb/core/generic.c:210
  usb_probe_device+0x99/0x100 drivers/usb/core/driver.c:266
  really_probe+0x281/0x6d0 drivers/base/dd.c:548
  driver_probe_device+0x104/0x210 drivers/base/dd.c:721
  __device_attach_driver+0x1c2/0x220 drivers/base/dd.c:828
  bus_for_each_drv+0x162/0x1e0 drivers/base/bus.c:430
  __device_attach+0x217/0x360 drivers/base/dd.c:894
  bus_probe_device+0x1e4/0x290 drivers/base/bus.c:490
  device_add+0x141f/0x1bc0 drivers/base/core.c:2487
  usb_new_device.cold+0x6a4/0xe79 drivers/usb/core/hub.c:2537
  hub_port_connect drivers/usb/core/hub.c:5184 [inline]
  hub_port_connect_change drivers/usb/core/hub.c:5324 [inline]
  port_event drivers/usb/core/hub.c:5470 [inline]
  hub_event+0x1e59/0x3860 drivers/usb/core/hub.c:5552
  process_one_work+0x92b/0x1530 kernel/workqueue.c:2264
  process_scheduled_works kernel/workqueue.c:2326 [inline]
  worker_thread+0x7ab/0xe20 kernel/workqueue.c:2412
  kthread+0x318/0x420 kernel/kthread.c:255
  ret_from_fork+0x24/0x30 arch/x86/entry/entry_64.S:352

Freed by task 20136:
  save_stack+0x1b/0x80 mm/kasan/common.c:72
  set_track mm/kasan/common.c:80 [inline]
  kasan_set_free_info mm/kasan/common.c:335 [inline]
  __kasan_slab_free+0x129/0x170 mm/kasan/common.c:474
  slab_free_hook mm/slub.c:1425 [inline]
  slab_free_freelist_hook mm/slub.c:1458 [inline]
  slab_free mm/slub.c:3005 [inline]
  kfree+0xda/0x310 mm/slub.c:3957
  usbvision_release+0x181/0x1c0  
drivers/media/usb/usbvision/usbvision-video.c:1364
  usbvision_radio_close.cold+0x2b/0x74  
drivers/media/usb/usbvision/usbvision-video.c:1130
  v4l2_release+0x2e7/0x390 drivers/media/v4l2-core/v4l2-dev.c:455
  __fput+0x2d7/0x840 fs/file_table.c:280
  task_work_run+0x13f/0x1c0 kernel/task_work.c:113
  tracehook_notify_resume include/linux/tracehook.h:188 [inline]
  exit_to_usermode_loop+0x1d2/0x200 arch/x86/entry/common.c:164
  prepare_exit_to_usermode arch/x86/entry/common.c:195 [inline]
  syscall_return_slowpath arch/x86/entry/common.c:278 [inline]
  do_syscall_64+0x4e0/0x5c0 arch/x86/entry/common.c:304
  entry_SYSCALL_64_after_hwframe+0x49/0xbe

The buggy address belongs to the object at ffff8881d13b4000
  which belongs to the cache kmalloc-8k of size 8192
The buggy address is located 4200 bytes inside of
  8192-byte region [ffff8881d13b4000, ffff8881d13b6000)
The buggy address belongs to the page:
page:ffffea000744ec00 refcount:1 mapcount:0 mapping:ffff8881da40c500  
index:0x0 compound_mapcount: 0
raw: 0200000000010200 ffffea0007392a00 0000000300000003 ffff8881da40c500
raw: 0000000000000000 0000000000020002 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
  ffff8881d13b4f00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
  ffff8881d13b4f80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ffff8881d13b5000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                                           ^
  ffff8881d13b5080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
  ffff8881d13b5100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================


---
This bug is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzkaller@googlegroups.com.

syzbot will keep track of this bug report. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: KASAN: use-after-free Read in v4l2_release (3)
  2020-01-08 20:24 KASAN: use-after-free Read in v4l2_release (3) syzbot
@ 2020-01-22 22:58 ` syzbot
       [not found] ` <20200123102707.2596-1-hdanton@sina.com>
  1 sibling, 0 replies; 12+ messages in thread
From: syzbot @ 2020-01-22 22:58 UTC (permalink / raw)
  To: andreyknvl, bnvandana, hverkuil-cisco, laurent.pinchart,
	linux-kernel, linux-media, linux-usb, mchehab, syzkaller-bugs

syzbot has found a reproducer for the following crash on:

HEAD commit:    4cc301ee usb: gadget: add raw-gadget interface
git tree:       https://github.com/google/kasan.git usb-fuzzer
console output: https://syzkaller.appspot.com/x/log.txt?x=17f5a721e00000
kernel config:  https://syzkaller.appspot.com/x/.config?x=9ba75825443d54bd
dashboard link: https://syzkaller.appspot.com/bug?extid=75287f75e2fedd69d680
compiler:       gcc (GCC) 9.0.0 20181231 (experimental)
syz repro:      https://syzkaller.appspot.com/x/repro.syz?x=16a0b6f1e00000
C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=1327dd76e00000

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

usbvision_set_audio: can't write iopin register for audio switching
usbvision_radio_close: Final disconnect
==================================================================
BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
Read of size 4 at addr ffff8881caba1068 by task v4l_id/1913

CPU: 1 PID: 1913 Comm: v4l_id Not tainted 5.5.0-rc6-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+0xef/0x16e lib/dump_stack.c:118
 print_address_description.constprop.0.cold+0xd3/0x314 mm/kasan/report.c:374
 __kasan_report.cold+0x37/0x85 mm/kasan/report.c:506
 kasan_report+0xe/0x20 mm/kasan/common.c:639
 v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
 __fput+0x2d7/0x840 fs/file_table.c:280
 task_work_run+0x13f/0x1c0 kernel/task_work.c:113
 tracehook_notify_resume include/linux/tracehook.h:188 [inline]
 exit_to_usermode_loop+0x1d2/0x200 arch/x86/entry/common.c:164
 prepare_exit_to_usermode arch/x86/entry/common.c:195 [inline]
 syscall_return_slowpath arch/x86/entry/common.c:278 [inline]
 do_syscall_64+0x4e0/0x5a0 arch/x86/entry/common.c:304
 entry_SYSCALL_64_after_hwframe+0x49/0xbe
RIP: 0033:0x7fbeb0b822b0
Code: 40 75 0b 31 c0 48 83 c4 08 e9 0c ff ff ff 48 8d 3d c5 32 08 00 e8 c0 07 02 00 83 3d 45 a3 2b 00 00 75 10 b8 03 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 31 c3 48 83 ec 08 e8 ce 8a 01 00 48 89 04 24
RSP: 002b:00007ffe52d6b158 EFLAGS: 00000246 ORIG_RAX: 0000000000000003
RAX: 0000000000000000 RBX: 0000000000000003 RCX: 00007fbeb0b822b0
RDX: 0000000000000013 RSI: 0000000080685600 RDI: 0000000000000003
RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000400884
R13: 00007ffe52d6b2b0 R14: 0000000000000000 R15: 0000000000000000

Allocated by task 94:
 save_stack+0x1b/0x80 mm/kasan/common.c:72
 set_track mm/kasan/common.c:80 [inline]
 __kasan_kmalloc mm/kasan/common.c:513 [inline]
 __kasan_kmalloc.constprop.0+0xbf/0xd0 mm/kasan/common.c:486
 kmalloc include/linux/slab.h:556 [inline]
 kzalloc include/linux/slab.h:670 [inline]
 usbvision_alloc drivers/media/usb/usbvision/usbvision-video.c:1315 [inline]
 usbvision_probe.cold+0x5c5/0x1f21 drivers/media/usb/usbvision/usbvision-video.c:1469
 usb_probe_interface+0x310/0x800 drivers/usb/core/driver.c:361
 really_probe+0x290/0xad0 drivers/base/dd.c:548
 driver_probe_device+0x223/0x350 drivers/base/dd.c:721
 __device_attach_driver+0x1d1/0x290 drivers/base/dd.c:828
 bus_for_each_drv+0x162/0x1e0 drivers/base/bus.c:430
 __device_attach+0x217/0x390 drivers/base/dd.c:894
 bus_probe_device+0x1e4/0x290 drivers/base/bus.c:490
 device_add+0x1459/0x1bf0 drivers/base/core.c:2487
 usb_set_configuration+0xe47/0x17d0 drivers/usb/core/message.c:2023
 generic_probe+0x9d/0xd5 drivers/usb/core/generic.c:210
 usb_probe_device+0xaf/0x140 drivers/usb/core/driver.c:266
 really_probe+0x290/0xad0 drivers/base/dd.c:548
 driver_probe_device+0x223/0x350 drivers/base/dd.c:721
 __device_attach_driver+0x1d1/0x290 drivers/base/dd.c:828
 bus_for_each_drv+0x162/0x1e0 drivers/base/bus.c:430
 __device_attach+0x217/0x390 drivers/base/dd.c:894
 bus_probe_device+0x1e4/0x290 drivers/base/bus.c:490
 device_add+0x1459/0x1bf0 drivers/base/core.c:2487
 usb_new_device.cold+0x540/0xcd0 drivers/usb/core/hub.c:2537
 hub_port_connect drivers/usb/core/hub.c:5184 [inline]
 hub_port_connect_change drivers/usb/core/hub.c:5324 [inline]
 port_event drivers/usb/core/hub.c:5470 [inline]
 hub_event+0x21cb/0x4300 drivers/usb/core/hub.c:5552
 process_one_work+0x945/0x15c0 kernel/workqueue.c:2264
 process_scheduled_works kernel/workqueue.c:2326 [inline]
 worker_thread+0x7ab/0xe20 kernel/workqueue.c:2412
 kthread+0x318/0x420 kernel/kthread.c:255
 ret_from_fork+0x24/0x30 arch/x86/entry/entry_64.S:352

Freed by task 1913:
 save_stack+0x1b/0x80 mm/kasan/common.c:72
 set_track mm/kasan/common.c:80 [inline]
 kasan_set_free_info mm/kasan/common.c:335 [inline]
 __kasan_slab_free+0x117/0x160 mm/kasan/common.c:474
 slab_free_hook mm/slub.c:1425 [inline]
 slab_free_freelist_hook mm/slub.c:1458 [inline]
 slab_free mm/slub.c:3005 [inline]
 kfree+0xd5/0x300 mm/slub.c:3957
 usbvision_release+0x181/0x1c0 drivers/media/usb/usbvision/usbvision-video.c:1364
 usbvision_radio_close.cold+0x2b/0x74 drivers/media/usb/usbvision/usbvision-video.c:1130
 v4l2_release+0x2e7/0x390 drivers/media/v4l2-core/v4l2-dev.c:455
 __fput+0x2d7/0x840 fs/file_table.c:280
 task_work_run+0x13f/0x1c0 kernel/task_work.c:113
 tracehook_notify_resume include/linux/tracehook.h:188 [inline]
 exit_to_usermode_loop+0x1d2/0x200 arch/x86/entry/common.c:164
 prepare_exit_to_usermode arch/x86/entry/common.c:195 [inline]
 syscall_return_slowpath arch/x86/entry/common.c:278 [inline]
 do_syscall_64+0x4e0/0x5a0 arch/x86/entry/common.c:304
 entry_SYSCALL_64_after_hwframe+0x49/0xbe

The buggy address belongs to the object at ffff8881caba0000
 which belongs to the cache kmalloc-8k of size 8192
The buggy address is located 4200 bytes inside of
 8192-byte region [ffff8881caba0000, ffff8881caba2000)
The buggy address belongs to the page:
page:ffffea00072ae800 refcount:1 mapcount:0 mapping:ffff8881da00c500 index:0x0 compound_mapcount: 0
raw: 0200000000010200 dead000000000100 dead000000000122 ffff8881da00c500
raw: 0000000000000000 0000000080020002 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 ffff8881caba0f00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff8881caba0f80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff8881caba1000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                                          ^
 ffff8881caba1080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff8881caba1100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: KASAN: use-after-free Read in v4l2_release (3)
       [not found] ` <20200123102707.2596-1-hdanton@sina.com>
@ 2020-01-23 12:19   ` Laurent Pinchart
       [not found]   ` <20200124022847.11244-1-hdanton@sina.com>
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: Laurent Pinchart @ 2020-01-23 12:19 UTC (permalink / raw)
  To: Hillf Danton
  Cc: syzbot, andreyknvl, bnvandana, hverkuil-cisco, linux-kernel,
	linux-media, linux-usb, mchehab, syzkaller-bugs

Hi Hillf,

On Thu, Jan 23, 2020 at 06:27:07PM +0800, Hillf Danton wrote:
> Wed, 22 Jan 2020 14:58:08 -0800 (PST)
> > syzbot has found a reproducer for the following crash on:
> > 
> > HEAD commit:    4cc301ee usb: gadget: add raw-gadget interface
> > git tree:       https://github.com/google/kasan.git usb-fuzzer
> > console output: https://syzkaller.appspot.com/x/log.txt?x=17f5a721e00000
> > kernel config:  https://syzkaller.appspot.com/x/.config?x=9ba75825443d54bd
> > dashboard link: https://syzkaller.appspot.com/bug?extid=75287f75e2fedd69d680
> > compiler:       gcc (GCC) 9.0.0 20181231 (experimental)
> > syz repro:      https://syzkaller.appspot.com/x/repro.syz?x=16a0b6f1e00000
> > C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=1327dd76e00000
> > 
> > IMPORTANT: if you fix the bug, please add the following tag to the commit:
> > Reported-by: syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com
> > 
> > usbvision_set_audio: can't write iopin register for audio switching
> > usbvision_radio_close: Final disconnect
> > ==================================================================
> > BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
> > Read of size 4 at addr ffff8881caba1068 by task v4l_id/1913
> > 
> > CPU: 1 PID: 1913 Comm: v4l_id Not tainted 5.5.0-rc6-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+0xef/0x16e lib/dump_stack.c:118
> >  print_address_description.constprop.0.cold+0xd3/0x314 mm/kasan/report.c:374
> >  __kasan_report.cold+0x37/0x85 mm/kasan/report.c:506
> >  kasan_report+0xe/0x20 mm/kasan/common.c:639
> >  v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
> >  __fput+0x2d7/0x840 fs/file_table.c:280
> >  task_work_run+0x13f/0x1c0 kernel/task_work.c:113
> >  tracehook_notify_resume include/linux/tracehook.h:188 [inline]
> >  exit_to_usermode_loop+0x1d2/0x200 arch/x86/entry/common.c:164
> >  prepare_exit_to_usermode arch/x86/entry/common.c:195 [inline]
> >  syscall_return_slowpath arch/x86/entry/common.c:278 [inline]
> >  do_syscall_64+0x4e0/0x5a0 arch/x86/entry/common.c:304
> >  entry_SYSCALL_64_after_hwframe+0x49/0xbe
> > RIP: 0033:0x7fbeb0b822b0
> > Code: 40 75 0b 31 c0 48 83 c4 08 e9 0c ff ff ff 48 8d 3d c5 32 08 00 e8 c0 07 02 00 83 3d 45 a3 2b 00 00 75 10 b8 03 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 31 c3 48 83 ec 08 e8 ce 8a 01 00 48 89 04 24
> > RSP: 002b:00007ffe52d6b158 EFLAGS: 00000246 ORIG_RAX: 0000000000000003
> > RAX: 0000000000000000 RBX: 0000000000000003 RCX: 00007fbeb0b822b0
> > RDX: 0000000000000013 RSI: 0000000080685600 RDI: 0000000000000003
> > RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
> > R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000400884
> > R13: 00007ffe52d6b2b0 R14: 0000000000000000 R15: 0000000000000000
> > 
> > Allocated by task 94:
> >  save_stack+0x1b/0x80 mm/kasan/common.c:72
> >  set_track mm/kasan/common.c:80 [inline]
> >  __kasan_kmalloc mm/kasan/common.c:513 [inline]
> >  __kasan_kmalloc.constprop.0+0xbf/0xd0 mm/kasan/common.c:486
> >  kmalloc include/linux/slab.h:556 [inline]
> >  kzalloc include/linux/slab.h:670 [inline]
> >  usbvision_alloc drivers/media/usb/usbvision/usbvision-video.c:1315 [inline]
> >  usbvision_probe.cold+0x5c5/0x1f21 drivers/media/usb/usbvision/usbvision-video.c:1469
> >  usb_probe_interface+0x310/0x800 drivers/usb/core/driver.c:361
> >  really_probe+0x290/0xad0 drivers/base/dd.c:548
> >  driver_probe_device+0x223/0x350 drivers/base/dd.c:721
> >  __device_attach_driver+0x1d1/0x290 drivers/base/dd.c:828
> >  bus_for_each_drv+0x162/0x1e0 drivers/base/bus.c:430
> >  __device_attach+0x217/0x390 drivers/base/dd.c:894
> >  bus_probe_device+0x1e4/0x290 drivers/base/bus.c:490
> >  device_add+0x1459/0x1bf0 drivers/base/core.c:2487
> >  usb_set_configuration+0xe47/0x17d0 drivers/usb/core/message.c:2023
> >  generic_probe+0x9d/0xd5 drivers/usb/core/generic.c:210
> >  usb_probe_device+0xaf/0x140 drivers/usb/core/driver.c:266
> >  really_probe+0x290/0xad0 drivers/base/dd.c:548
> >  driver_probe_device+0x223/0x350 drivers/base/dd.c:721
> >  __device_attach_driver+0x1d1/0x290 drivers/base/dd.c:828
> >  bus_for_each_drv+0x162/0x1e0 drivers/base/bus.c:430
> >  __device_attach+0x217/0x390 drivers/base/dd.c:894
> >  bus_probe_device+0x1e4/0x290 drivers/base/bus.c:490
> >  device_add+0x1459/0x1bf0 drivers/base/core.c:2487
> >  usb_new_device.cold+0x540/0xcd0 drivers/usb/core/hub.c:2537
> >  hub_port_connect drivers/usb/core/hub.c:5184 [inline]
> >  hub_port_connect_change drivers/usb/core/hub.c:5324 [inline]
> >  port_event drivers/usb/core/hub.c:5470 [inline]
> >  hub_event+0x21cb/0x4300 drivers/usb/core/hub.c:5552
> >  process_one_work+0x945/0x15c0 kernel/workqueue.c:2264
> >  process_scheduled_works kernel/workqueue.c:2326 [inline]
> >  worker_thread+0x7ab/0xe20 kernel/workqueue.c:2412
> >  kthread+0x318/0x420 kernel/kthread.c:255
> >  ret_from_fork+0x24/0x30 arch/x86/entry/entry_64.S:352
> > 
> > Freed by task 1913:
> >  save_stack+0x1b/0x80 mm/kasan/common.c:72
> >  set_track mm/kasan/common.c:80 [inline]
> >  kasan_set_free_info mm/kasan/common.c:335 [inline]
> >  __kasan_slab_free+0x117/0x160 mm/kasan/common.c:474
> >  slab_free_hook mm/slub.c:1425 [inline]
> >  slab_free_freelist_hook mm/slub.c:1458 [inline]
> >  slab_free mm/slub.c:3005 [inline]
> >  kfree+0xd5/0x300 mm/slub.c:3957
> >  usbvision_release+0x181/0x1c0 drivers/media/usb/usbvision/usbvision-video.c:1364
> >  usbvision_radio_close.cold+0x2b/0x74 drivers/media/usb/usbvision/usbvision-video.c:1130
> >  v4l2_release+0x2e7/0x390 drivers/media/v4l2-core/v4l2-dev.c:455
> >  __fput+0x2d7/0x840 fs/file_table.c:280
> >  task_work_run+0x13f/0x1c0 kernel/task_work.c:113
> >  tracehook_notify_resume include/linux/tracehook.h:188 [inline]
> >  exit_to_usermode_loop+0x1d2/0x200 arch/x86/entry/common.c:164
> >  prepare_exit_to_usermode arch/x86/entry/common.c:195 [inline]
> >  syscall_return_slowpath arch/x86/entry/common.c:278 [inline]
> >  do_syscall_64+0x4e0/0x5a0 arch/x86/entry/common.c:304
> >  entry_SYSCALL_64_after_hwframe+0x49/0xbe
> > 
> > The buggy address belongs to the object at ffff8881caba0000
> >  which belongs to the cache kmalloc-8k of size 8192
> > The buggy address is located 4200 bytes inside of
> >  8192-byte region [ffff8881caba0000, ffff8881caba2000)
> > The buggy address belongs to the page:
> > page:ffffea00072ae800 refcount:1 mapcount:0 mapping:ffff8881da00c500 index:0x0 compound_mapcount: 0
> > raw: 0200000000010200 dead000000000100 dead000000000122 ffff8881da00c500
> > raw: 0000000000000000 0000000080020002 00000001ffffffff 0000000000000000
> > page dumped because: kasan: bad access detected
> > 
> > Memory state around the buggy address:
> >  ffff8881caba0f00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> >  ffff8881caba0f80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> > >ffff8881caba1000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> >                                                           ^
> >  ffff8881caba1080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> >  ffff8881caba1100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> > ==================================================================
> 
> 
> Add the release callback for usbvision video device and use it to release
> resources when the last reference to the device goes away.

Would you be able to submit this with a commit message and your
Signed-off-by line ?

> --- a/drivers/media/usb/usbvision/usbvision-video.c
> +++ b/drivers/media/usb/usbvision/usbvision-video.c
> @@ -401,7 +401,6 @@ static int usbvision_v4l2_close(struct f
>  
>  	if (r) {
>  		printk(KERN_INFO "%s: Final disconnect\n", __func__);
> -		usbvision_release(usbvision);
>  		return 0;
>  	}
>  
> @@ -409,6 +408,11 @@ static int usbvision_v4l2_close(struct f
>  	return v4l2_fh_release(file);
>  }
>  
> +static void usbvision_video_device_release(struct video_device *vdev)
> +{
> +	struct usb_usbvision *usbvision = video_get_drvdata(vdev);
> +	usbvision_release(usbvision);
> +}
>  
>  /*
>   * usbvision_ioctl()
> @@ -1181,7 +1185,7 @@ static struct video_device usbvision_vid
>  	.fops		= &usbvision_fops,
>  	.ioctl_ops	= &usbvision_ioctl_ops,
>  	.name           = "usbvision-video",
> -	.release	= video_device_release_empty,
> +	.release	= usbvision_video_device_release,
>  	.tvnorms        = USBVISION_NORMS,
>  };
>  
> --- a/drivers/media/v4l2-core/v4l2-dev.c
> +++ b/drivers/media/v4l2-core/v4l2-dev.c
> @@ -206,7 +206,10 @@ static void v4l2_device_release(struct d
>  	}
>  #endif
>  
> -	/* Do not call v4l2_device_put if there is no release callback set.
> +	/*
> +	 * Decrease v4l2_device refcount
> +	 *
> +	 * Do not call v4l2_device_put if there is no release callback set.
>  	 * Drivers that have no v4l2_device release callback might free the
>  	 * v4l2_dev instance in the video_device release callback below, so we
>  	 * must perform this check here.
> @@ -214,16 +217,12 @@ static void v4l2_device_release(struct d
>  	 * TODO: In the long run all drivers that use v4l2_device should use the
>  	 * v4l2_device release callback. This check will then be unnecessary.
>  	 */
> -	if (v4l2_dev->release == NULL)
> -		v4l2_dev = NULL;
> +	if (v4l2_dev->release)
> +		v4l2_device_put(v4l2_dev);
>  
>  	/* Release video_device and perform other
>  	   cleanups as needed. */
>  	vdev->release(vdev);
> -
> -	/* Decrease v4l2_device refcount */
> -	if (v4l2_dev)
> -		v4l2_device_put(v4l2_dev);
>  }
>  
>  static struct class video_class = {

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: KASAN: use-after-free Read in v4l2_release (3)
       [not found]   ` <20200124022847.11244-1-hdanton@sina.com>
@ 2020-01-24 12:41     ` Andrey Konovalov
  2020-01-24 12:54       ` syzbot
  0 siblings, 1 reply; 12+ messages in thread
From: Andrey Konovalov @ 2020-01-24 12:41 UTC (permalink / raw)
  To: Hillf Danton
  Cc: Laurent Pinchart, syzbot, bnvandana, hverkuil-cisco, LKML,
	linux-media, USB list, Mauro Carvalho Chehab, syzkaller-bugs,
	Hans Verkuil

[-- Attachment #1: Type: text/plain, Size: 4824 bytes --]

On Fri, Jan 24, 2020 at 3:29 AM Hillf Danton <hdanton@sina.com> wrote:
>
>
> On Thu, 23 Jan 2020 14:19:47 +0200 Laurent Pinchart wrote:
> > On Thu, Jan 23, 2020 at 06:27:07PM +0800, Hillf Danton wrote:
> > > Wed, 22 Jan 2020 14:58:08 -0800 (PST)
> > > > syzbot has found a reproducer for the following crash on:
> > > >
> > > > HEAD commit:    4cc301ee usb: gadget: add raw-gadget interface
> > > > git tree:       https://github.com/google/kasan.git usb-fuzzer
> > > > console output: https://syzkaller.appspot.com/x/log.txt?x=17f5a721e00000
> > > > kernel config:  https://syzkaller.appspot.com/x/.config?x=9ba75825443d54bd
> > > > dashboard link: https://syzkaller.appspot.com/bug?extid=75287f75e2fedd69d680
> > > > compiler:       gcc (GCC) 9.0.0 20181231 (experimental)
> > > > syz repro:      https://syzkaller.appspot.com/x/repro.syz?x=16a0b6f1e00000
> > > > C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=1327dd76e00000
> > > >
> > > > IMPORTANT: if you fix the bug, please add the following tag to the commit:
> > > > Reported-by: syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com
> > > >
> > > > usbvision_set_audio: can't write iopin register for audio switching
> > > > usbvision_radio_close: Final disconnect
> > > > ==================================================================
> > > > BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
> > > > Read of size 4 at addr ffff8881caba1068 by task v4l_id/1913
>
> <snip>
>
> > > Add the release callback for usbvision video device and use it to release
> > > resources when the last reference to the device goes away.
> >
> > Would you be able to submit this with a commit message and your
> > Signed-off-by line ?
>
> ---8<---
> Subject: [PATCH] media: usbvision: add the release callback for video device
> From: Hillf Danton <hdanton@sina.com>
>
> To fix the UAF syzbot reported,
>
> BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
>
> a release cb which is a simple wrapper of usbvision_release() is added
> for releasing resources as the last reference to the usbvision video
> device goes away.
>
> Reported-by: syzbot <syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com>
> Fixes: 2aa689dd8057 ("[media] usbvision: embed video_device")
> Cc: Hans Verkuil <hans.verkuil@cisco.com>
> Signed-off-by: Hillf Danton <hdanton@sina.com>
> ---
>
> --- a/drivers/media/usb/usbvision/usbvision-video.c
> +++ b/drivers/media/usb/usbvision/usbvision-video.c
> @@ -401,7 +401,6 @@ static int usbvision_v4l2_close(struct f
>
>         if (r) {
>                 printk(KERN_INFO "%s: Final disconnect\n", __func__);
> -               usbvision_release(usbvision);
>                 return 0;
>         }
>
> @@ -409,6 +408,11 @@ static int usbvision_v4l2_close(struct f
>         return v4l2_fh_release(file);
>  }
>
> +static void usbvision_video_device_release(struct video_device *vdev)
> +{
> +       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
> +       usbvision_release(usbvision);
> +}
>
>  /*
>   * usbvision_ioctl()
> @@ -1181,7 +1185,7 @@ static struct video_device usbvision_vid
>         .fops           = &usbvision_fops,
>         .ioctl_ops      = &usbvision_ioctl_ops,
>         .name           = "usbvision-video",
> -       .release        = video_device_release_empty,
> +       .release        = usbvision_video_device_release,
>         .tvnorms        = USBVISION_NORMS,
>  };
>
> --- a/drivers/media/v4l2-core/v4l2-dev.c
> +++ b/drivers/media/v4l2-core/v4l2-dev.c
> @@ -206,7 +206,10 @@ static void v4l2_device_release(struct d
>         }
>  #endif
>
> -       /* Do not call v4l2_device_put if there is no release callback set.
> +       /*
> +        * Decrease v4l2_device refcount
> +        *
> +        * Do not call v4l2_device_put if there is no release callback set.
>          * Drivers that have no v4l2_device release callback might free the
>          * v4l2_dev instance in the video_device release callback below, so we
>          * must perform this check here.
> @@ -214,16 +217,12 @@ static void v4l2_device_release(struct d
>          * TODO: In the long run all drivers that use v4l2_device should use the
>          * v4l2_device release callback. This check will then be unnecessary.
>          */
> -       if (v4l2_dev->release == NULL)
> -               v4l2_dev = NULL;
> +       if (v4l2_dev->release)
> +               v4l2_device_put(v4l2_dev);
>
>         /* Release video_device and perform other
>            cleanups as needed. */
>         vdev->release(vdev);
> -
> -       /* Decrease v4l2_device refcount */
> -       if (v4l2_dev)
> -               v4l2_device_put(v4l2_dev);
>  }
>
>  static struct class video_class = {
> --

#syz test: https://github.com/google/kasan.git ae179410

[-- Attachment #2: usbvision.patch --]
[-- Type: application/x-patch, Size: 2585 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: KASAN: use-after-free Read in v4l2_release (3)
  2020-01-24 12:41     ` Andrey Konovalov
@ 2020-01-24 12:54       ` syzbot
  0 siblings, 0 replies; 12+ messages in thread
From: syzbot @ 2020-01-24 12:54 UTC (permalink / raw)
  To: andreyknvl, bnvandana, hans.verkuil, hdanton, hverkuil-cisco,
	laurent.pinchart, linux-kernel, linux-media, linux-usb, mchehab,
	syzkaller-bugs

Hello,

syzbot has tested the proposed patch but the reproducer still triggered crash:
WARNING in kernfs_remove_by_name_ns

------------[ cut here ]------------
kernfs: can not remove 'version', no directory
WARNING: CPU: 1 PID: 94 at fs/kernfs/dir.c:1507 kernfs_remove_by_name_ns+0x98/0xb0 fs/kernfs/dir.c:1507
Kernel panic - not syncing: panic_on_warn set ...
CPU: 1 PID: 94 Comm: kworker/1:2 Not tainted 5.5.0-rc3-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Workqueue: usb_hub_wq hub_event
Call Trace:
 __dump_stack lib/dump_stack.c:77 [inline]
 dump_stack+0xef/0x16e lib/dump_stack.c:118
 panic+0x2aa/0x6e1 kernel/panic.c:221
 __warn.cold+0x2f/0x30 kernel/panic.c:582
 report_bug+0x27b/0x2f0 lib/bug.c:195
 fixup_bug arch/x86/kernel/traps.c:174 [inline]
 fixup_bug arch/x86/kernel/traps.c:169 [inline]
 do_error_trap+0x12b/0x1e0 arch/x86/kernel/traps.c:267
 do_invalid_op+0x32/0x40 arch/x86/kernel/traps.c:286
 invalid_op+0x23/0x30 arch/x86/entry/entry_64.S:1027
RIP: 0010:kernfs_remove_by_name_ns+0x98/0xb0 fs/kernfs/dir.c:1507
Code: b1 ff 48 c7 c7 20 13 1d 87 41 bc fe ff ff ff e8 2e fe fe 03 eb d9 e8 47 4d b1 ff 4c 89 e6 48 c7 c7 c0 51 f1 85 e8 20 33 86 ff <0f> 0b 41 bc fe ff ff ff eb bb 0f 1f 40 00 66 2e 0f 1f 84 00 00 00
RSP: 0018:ffff8881d5d47708 EFLAGS: 00010286
RAX: 0000000000000000 RBX: ffff8881cba58390 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffffffff812959ad RDI: ffffed103aba8ed3
RBP: 0000000000000000 R08: ffff8881d6d2c980 R09: fffffbfff1269aae
R10: fffffbfff1269aad R11: ffffffff8934d56f R12: ffffffff8671eb40
R13: 0000000000000000 R14: 0000000000000000 R15: 000000000000000c
 sysfs_remove_file include/linux/sysfs.h:536 [inline]
 device_remove_file+0x25/0x30 drivers/base/core.c:1869
 usbvision_remove_sysfs drivers/media/usb/usbvision/usbvision-video.c:287 [inline]
 usbvision_release+0x88/0x1c0 drivers/media/usb/usbvision/usbvision-video.c:1360
 v4l2_device_release+0x29a/0x3e0 drivers/media/v4l2-core/v4l2-dev.c:225
 device_release+0x71/0x200 drivers/base/core.c:1358
 kobject_cleanup lib/kobject.c:693 [inline]
 kobject_release lib/kobject.c:722 [inline]
 kref_put include/linux/kref.h:65 [inline]
 kobject_put+0x256/0x550 lib/kobject.c:739
 put_device drivers/base/core.c:2586 [inline]
 device_unregister+0x34/0xc0 drivers/base/core.c:2697
 video_unregister_device+0xa2/0xc0 drivers/media/v4l2-core/v4l2-dev.c:1075
 usbvision_unregister_video drivers/media/usb/usbvision/usbvision-video.c:1255 [inline]
 usbvision_unregister_video+0xfb/0x120 drivers/media/usb/usbvision/usbvision-video.c:1242
 usbvision_release+0x10d/0x1c0 drivers/media/usb/usbvision/usbvision-video.c:1361
 usbvision_disconnect+0x171/0x1e0 drivers/media/usb/usbvision/usbvision-video.c:1593
 usb_unbind_interface+0x1bd/0x8a0 drivers/usb/core/driver.c:423
 __device_release_driver drivers/base/dd.c:1134 [inline]
 device_release_driver_internal+0x42f/0x500 drivers/base/dd.c:1165
 bus_remove_device+0x2eb/0x5a0 drivers/base/bus.c:532
 device_del+0x481/0xd30 drivers/base/core.c:2664
 usb_disable_device+0x23d/0x790 drivers/usb/core/message.c:1237
 usb_disconnect+0x293/0x900 drivers/usb/core/hub.c:2200
 hub_port_connect drivers/usb/core/hub.c:5035 [inline]
 hub_port_connect_change drivers/usb/core/hub.c:5324 [inline]
 port_event drivers/usb/core/hub.c:5470 [inline]
 hub_event+0x1a1d/0x4300 drivers/usb/core/hub.c:5552
 process_one_work+0x945/0x15c0 kernel/workqueue.c:2264
 worker_thread+0x96/0xe20 kernel/workqueue.c:2410
 kthread+0x318/0x420 kernel/kthread.c:255
 ret_from_fork+0x24/0x30 arch/x86/entry/entry_64.S:352
Kernel Offset: disabled
Rebooting in 86400 seconds..


Tested on:

commit:         ae179410 usb: gadget: add raw-gadget interface
git tree:       https://github.com/google/kasan.git
console output: https://syzkaller.appspot.com/x/log.txt?x=133b3611e00000
kernel config:  https://syzkaller.appspot.com/x/.config?x=ad1d751a3a72ae57
dashboard link: https://syzkaller.appspot.com/bug?extid=75287f75e2fedd69d680
compiler:       gcc (GCC) 9.0.0 20181231 (experimental)
patch:          https://syzkaller.appspot.com/x/patch.diff?x=15921b69e00000


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: KASAN: use-after-free Read in v4l2_release (3)
       [not found] ` <20200123102707.2596-1-hdanton@sina.com>
  2020-01-23 12:19   ` Laurent Pinchart
       [not found]   ` <20200124022847.11244-1-hdanton@sina.com>
@ 2020-01-24 14:10   ` Dan Carpenter
  2020-01-24 14:13   ` [PATCH] media: usbvision: Fix a use after free in v4l2_release() Dan Carpenter
  3 siblings, 0 replies; 12+ messages in thread
From: Dan Carpenter @ 2020-01-24 14:10 UTC (permalink / raw)
  To: Hillf Danton
  Cc: syzbot, andreyknvl, bnvandana, hverkuil-cisco, laurent.pinchart,
	linux-kernel, linux-media, linux-usb, mchehab, syzkaller-bugs

This would work but another approach would be to to allocate "usbvision"
with devm_kzalloc(&dev->dev,...  I believe that would postpone the free
enough to solve the use after free but I hadn't tested it yet.  But
other drivers do that so hopefully it works.

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH] media: usbvision: Fix a use after free in v4l2_release()
       [not found] ` <20200123102707.2596-1-hdanton@sina.com>
                     ` (2 preceding siblings ...)
  2020-01-24 14:10   ` Dan Carpenter
@ 2020-01-24 14:13   ` Dan Carpenter
  2020-02-14 10:06     ` Hans Verkuil
  3 siblings, 1 reply; 12+ messages in thread
From: Dan Carpenter @ 2020-01-24 14:13 UTC (permalink / raw)
  To: Hans Verkuil, syzbot, Hillf Danton
  Cc: Alan Stern, Allison Randal, Greg Kroah-Hartman, Souptick Joarder,
	andreyknvl, bnvandana, laurent.pinchart, linux-kernel,
	linux-media, linux-usb, mchehab, syzkaller-bugs

Syzbot triggered a use after free in v5.5-rc6:

BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459

Allocated by task 94:
 usbvision_alloc drivers/media/usb/usbvision/usbvision-video.c:1315 [inline]
 usbvision_probe.cold+0x5c5/0x1f21 drivers/media/usb/usbvision/usbvision-video.c:1469

Freed by task 1913:
 kfree+0xd5/0x300 mm/slub.c:3957
 usbvision_release+0x181/0x1c0 drivers/media/usb/usbvision/usbvision-video.c:1364
 usbvision_radio_close.cold+0x2b/0x74 drivers/media/usb/usbvision/usbvision-video.c:1130
 v4l2_release+0x2e7/0x390 drivers/media/v4l2-core/v4l2-dev.c:455

The problem is that the v4l2_release() calls usbvision_release() which
frees "usbvision" but v4l2_release() still wants to use
"usbvision->vdev".  One solution is to make this devm_ allocated memory
so the memory isn't freed until later.

Reported-by: syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
I copied this idea from a different driver, but I haven't tested it.
I wanted to try the #syz fix command to see if it works.

 drivers/media/usb/usbvision/usbvision-video.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/media/usb/usbvision/usbvision-video.c b/drivers/media/usb/usbvision/usbvision-video.c
index 93d36aab824f..07b4763062c4 100644
--- a/drivers/media/usb/usbvision/usbvision-video.c
+++ b/drivers/media/usb/usbvision/usbvision-video.c
@@ -1312,7 +1312,7 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
 {
 	struct usb_usbvision *usbvision;
 
-	usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
+	usbvision = devm_kzalloc(&dev->dev, sizeof(*usbvision), GFP_KERNEL);
 	if (!usbvision)
 		return NULL;
 
@@ -1336,7 +1336,6 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
 	v4l2_ctrl_handler_free(&usbvision->hdl);
 	v4l2_device_unregister(&usbvision->v4l2_dev);
 err_free:
-	kfree(usbvision);
 	return NULL;
 }
 
@@ -1361,7 +1360,6 @@ static void usbvision_release(struct usb_usbvision *usbvision)
 
 	v4l2_ctrl_handler_free(&usbvision->hdl);
 	v4l2_device_unregister(&usbvision->v4l2_dev);
-	kfree(usbvision);
 
 	PDEBUG(DBG_PROBE, "success");
 }
-- 
2.11.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] media: usbvision: Fix a use after free in v4l2_release()
  2020-01-24 14:13   ` [PATCH] media: usbvision: Fix a use after free in v4l2_release() Dan Carpenter
@ 2020-02-14 10:06     ` Hans Verkuil
  2020-02-14 11:22       ` Laurent Pinchart
  0 siblings, 1 reply; 12+ messages in thread
From: Hans Verkuil @ 2020-02-14 10:06 UTC (permalink / raw)
  To: Dan Carpenter, syzbot, Hillf Danton
  Cc: Alan Stern, Allison Randal, Greg Kroah-Hartman, Souptick Joarder,
	andreyknvl, bnvandana, laurent.pinchart, linux-kernel,
	linux-media, linux-usb, mchehab, syzkaller-bugs

On 1/24/20 3:13 PM, Dan Carpenter wrote:
> Syzbot triggered a use after free in v5.5-rc6:
> 
> BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
> 
> Allocated by task 94:
>  usbvision_alloc drivers/media/usb/usbvision/usbvision-video.c:1315 [inline]
>  usbvision_probe.cold+0x5c5/0x1f21 drivers/media/usb/usbvision/usbvision-video.c:1469
> 
> Freed by task 1913:
>  kfree+0xd5/0x300 mm/slub.c:3957
>  usbvision_release+0x181/0x1c0 drivers/media/usb/usbvision/usbvision-video.c:1364
>  usbvision_radio_close.cold+0x2b/0x74 drivers/media/usb/usbvision/usbvision-video.c:1130
>  v4l2_release+0x2e7/0x390 drivers/media/v4l2-core/v4l2-dev.c:455
> 
> The problem is that the v4l2_release() calls usbvision_release() which
> frees "usbvision" but v4l2_release() still wants to use
> "usbvision->vdev".  One solution is to make this devm_ allocated memory
> so the memory isn't freed until later.

devm_ allocated memory is freed after disconnect, so I doubt this will help, or at
best it will just move the problem elsewhere.

The right approach would be to use the release() callback from struct v4l2_device:
that's called when the very last open filehandle is closed.

But I'm not sure if it is worth the effort. The usbvision driver is a mess and
personally I think it should be deprecated.

Regards,

	Hans

> 
> Reported-by: syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> I copied this idea from a different driver, but I haven't tested it.
> I wanted to try the #syz fix command to see if it works.
> 
>  drivers/media/usb/usbvision/usbvision-video.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/media/usb/usbvision/usbvision-video.c b/drivers/media/usb/usbvision/usbvision-video.c
> index 93d36aab824f..07b4763062c4 100644
> --- a/drivers/media/usb/usbvision/usbvision-video.c
> +++ b/drivers/media/usb/usbvision/usbvision-video.c
> @@ -1312,7 +1312,7 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
>  {
>  	struct usb_usbvision *usbvision;
>  
> -	usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
> +	usbvision = devm_kzalloc(&dev->dev, sizeof(*usbvision), GFP_KERNEL);
>  	if (!usbvision)
>  		return NULL;
>  
> @@ -1336,7 +1336,6 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
>  	v4l2_ctrl_handler_free(&usbvision->hdl);
>  	v4l2_device_unregister(&usbvision->v4l2_dev);
>  err_free:
> -	kfree(usbvision);
>  	return NULL;
>  }
>  
> @@ -1361,7 +1360,6 @@ static void usbvision_release(struct usb_usbvision *usbvision)
>  
>  	v4l2_ctrl_handler_free(&usbvision->hdl);
>  	v4l2_device_unregister(&usbvision->v4l2_dev);
> -	kfree(usbvision);
>  
>  	PDEBUG(DBG_PROBE, "success");
>  }
> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] media: usbvision: Fix a use after free in v4l2_release()
  2020-02-14 10:06     ` Hans Verkuil
@ 2020-02-14 11:22       ` Laurent Pinchart
  2020-02-14 11:30         ` Hans Verkuil
       [not found]         ` <20200214121447.13612-1-hdanton@sina.com>
  0 siblings, 2 replies; 12+ messages in thread
From: Laurent Pinchart @ 2020-02-14 11:22 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Dan Carpenter, syzbot, Hillf Danton, Alan Stern, Allison Randal,
	Greg Kroah-Hartman, Souptick Joarder, andreyknvl, bnvandana,
	linux-kernel, linux-media, linux-usb, mchehab, syzkaller-bugs

Hi Hans,

On Fri, Feb 14, 2020 at 11:06:36AM +0100, Hans Verkuil wrote:
> On 1/24/20 3:13 PM, Dan Carpenter wrote:
> > Syzbot triggered a use after free in v5.5-rc6:
> > 
> > BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
> > 
> > Allocated by task 94:
> >  usbvision_alloc drivers/media/usb/usbvision/usbvision-video.c:1315 [inline]
> >  usbvision_probe.cold+0x5c5/0x1f21 drivers/media/usb/usbvision/usbvision-video.c:1469
> > 
> > Freed by task 1913:
> >  kfree+0xd5/0x300 mm/slub.c:3957
> >  usbvision_release+0x181/0x1c0 drivers/media/usb/usbvision/usbvision-video.c:1364
> >  usbvision_radio_close.cold+0x2b/0x74 drivers/media/usb/usbvision/usbvision-video.c:1130
> >  v4l2_release+0x2e7/0x390 drivers/media/v4l2-core/v4l2-dev.c:455
> > 
> > The problem is that the v4l2_release() calls usbvision_release() which
> > frees "usbvision" but v4l2_release() still wants to use
> > "usbvision->vdev".  One solution is to make this devm_ allocated memory
> > so the memory isn't freed until later.
> 
> devm_ allocated memory is freed after disconnect, so I doubt this will help, or at
> best it will just move the problem elsewhere.

Yes, devm_*alloc is evil :-( It has spread to many drivers and is used
incorrectly in most cases.

> The right approach would be to use the release() callback from struct v4l2_device:
> that's called when the very last open filehandle is closed.

Hillf Danton has sent a patch to do so in the "Re: KASAN: use-after-free
Read in v4l2_release (3)" thread. Have you seen it ?

> But I'm not sure if it is worth the effort. The usbvision driver is a mess and
> personally I think it should be deprecated.

I agree.

> > Reported-by: syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> > I copied this idea from a different driver, but I haven't tested it.
> > I wanted to try the #syz fix command to see if it works.
> > 
> >  drivers/media/usb/usbvision/usbvision-video.c | 4 +---
> >  1 file changed, 1 insertion(+), 3 deletions(-)
> > 
> > diff --git a/drivers/media/usb/usbvision/usbvision-video.c b/drivers/media/usb/usbvision/usbvision-video.c
> > index 93d36aab824f..07b4763062c4 100644
> > --- a/drivers/media/usb/usbvision/usbvision-video.c
> > +++ b/drivers/media/usb/usbvision/usbvision-video.c
> > @@ -1312,7 +1312,7 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
> >  {
> >  	struct usb_usbvision *usbvision;
> >  
> > -	usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
> > +	usbvision = devm_kzalloc(&dev->dev, sizeof(*usbvision), GFP_KERNEL);
> >  	if (!usbvision)
> >  		return NULL;
> >  
> > @@ -1336,7 +1336,6 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
> >  	v4l2_ctrl_handler_free(&usbvision->hdl);
> >  	v4l2_device_unregister(&usbvision->v4l2_dev);
> >  err_free:
> > -	kfree(usbvision);
> >  	return NULL;
> >  }
> >  
> > @@ -1361,7 +1360,6 @@ static void usbvision_release(struct usb_usbvision *usbvision)
> >  
> >  	v4l2_ctrl_handler_free(&usbvision->hdl);
> >  	v4l2_device_unregister(&usbvision->v4l2_dev);
> > -	kfree(usbvision);
> >  
> >  	PDEBUG(DBG_PROBE, "success");
> >  }

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] media: usbvision: Fix a use after free in v4l2_release()
  2020-02-14 11:22       ` Laurent Pinchart
@ 2020-02-14 11:30         ` Hans Verkuil
       [not found]         ` <20200214121447.13612-1-hdanton@sina.com>
  1 sibling, 0 replies; 12+ messages in thread
From: Hans Verkuil @ 2020-02-14 11:30 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: Dan Carpenter, syzbot, Hillf Danton, Alan Stern, Allison Randal,
	Greg Kroah-Hartman, Souptick Joarder, andreyknvl, bnvandana,
	linux-kernel, linux-media, linux-usb, mchehab, syzkaller-bugs

On 2/14/20 12:22 PM, Laurent Pinchart wrote:
> Hi Hans,
> 
> On Fri, Feb 14, 2020 at 11:06:36AM +0100, Hans Verkuil wrote:
>> On 1/24/20 3:13 PM, Dan Carpenter wrote:
>>> Syzbot triggered a use after free in v5.5-rc6:
>>>
>>> BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
>>>
>>> Allocated by task 94:
>>>  usbvision_alloc drivers/media/usb/usbvision/usbvision-video.c:1315 [inline]
>>>  usbvision_probe.cold+0x5c5/0x1f21 drivers/media/usb/usbvision/usbvision-video.c:1469
>>>
>>> Freed by task 1913:
>>>  kfree+0xd5/0x300 mm/slub.c:3957
>>>  usbvision_release+0x181/0x1c0 drivers/media/usb/usbvision/usbvision-video.c:1364
>>>  usbvision_radio_close.cold+0x2b/0x74 drivers/media/usb/usbvision/usbvision-video.c:1130
>>>  v4l2_release+0x2e7/0x390 drivers/media/v4l2-core/v4l2-dev.c:455
>>>
>>> The problem is that the v4l2_release() calls usbvision_release() which
>>> frees "usbvision" but v4l2_release() still wants to use
>>> "usbvision->vdev".  One solution is to make this devm_ allocated memory
>>> so the memory isn't freed until later.
>>
>> devm_ allocated memory is freed after disconnect, so I doubt this will help, or at
>> best it will just move the problem elsewhere.
> 
> Yes, devm_*alloc is evil :-( It has spread to many drivers and is used
> incorrectly in most cases.
> 
>> The right approach would be to use the release() callback from struct v4l2_device:
>> that's called when the very last open filehandle is closed.
> 
> Hillf Danton has sent a patch to do so in the "Re: KASAN: use-after-free
> Read in v4l2_release (3)" thread. Have you seen it ?

Ah, that was never mailed to linux-media, so never ended up in patchwork. And
if it ain't in patchwork, then I don't know about it :-)

Hillf, if you want your patch to be merged, then make sure it is CC-ed to
linux-media as well.

Regards,

	Hans

> 
>> But I'm not sure if it is worth the effort. The usbvision driver is a mess and
>> personally I think it should be deprecated.
> 
> I agree.
> 
>>> Reported-by: syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com
>>> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
>>> ---
>>> I copied this idea from a different driver, but I haven't tested it.
>>> I wanted to try the #syz fix command to see if it works.
>>>
>>>  drivers/media/usb/usbvision/usbvision-video.c | 4 +---
>>>  1 file changed, 1 insertion(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/media/usb/usbvision/usbvision-video.c b/drivers/media/usb/usbvision/usbvision-video.c
>>> index 93d36aab824f..07b4763062c4 100644
>>> --- a/drivers/media/usb/usbvision/usbvision-video.c
>>> +++ b/drivers/media/usb/usbvision/usbvision-video.c
>>> @@ -1312,7 +1312,7 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
>>>  {
>>>  	struct usb_usbvision *usbvision;
>>>  
>>> -	usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
>>> +	usbvision = devm_kzalloc(&dev->dev, sizeof(*usbvision), GFP_KERNEL);
>>>  	if (!usbvision)
>>>  		return NULL;
>>>  
>>> @@ -1336,7 +1336,6 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
>>>  	v4l2_ctrl_handler_free(&usbvision->hdl);
>>>  	v4l2_device_unregister(&usbvision->v4l2_dev);
>>>  err_free:
>>> -	kfree(usbvision);
>>>  	return NULL;
>>>  }
>>>  
>>> @@ -1361,7 +1360,6 @@ static void usbvision_release(struct usb_usbvision *usbvision)
>>>  
>>>  	v4l2_ctrl_handler_free(&usbvision->hdl);
>>>  	v4l2_device_unregister(&usbvision->v4l2_dev);
>>> -	kfree(usbvision);
>>>  
>>>  	PDEBUG(DBG_PROBE, "success");
>>>  }
> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] media: usbvision: Fix a use after free in v4l2_release()
       [not found]         ` <20200214121447.13612-1-hdanton@sina.com>
@ 2020-02-14 12:21           ` Hans Verkuil
       [not found]           ` <20200214124825.12568-1-hdanton@sina.com>
  1 sibling, 0 replies; 12+ messages in thread
From: Hans Verkuil @ 2020-02-14 12:21 UTC (permalink / raw)
  To: Hillf Danton
  Cc: Laurent Pinchart, Dan Carpenter, syzbot, Alan Stern,
	Allison Randal, Greg Kroah-Hartman, Souptick Joarder, andreyknvl,
	bnvandana, linux-kernel, linux-media, linux-usb, mchehab,
	syzkaller-bugs

On 2/14/20 1:14 PM, Hillf Danton wrote:
> 
> On Fri, 14 Feb 2020 12:30:29 +0100 Hans Verkuil wrote:
>>
>> Hillf, if you want your patch to be merged, then make sure it is CC-ed to
>> linux-media as well.
> 
> Please pick it up if it makes a sense to you and it was sent with
> linux-media added on the Cc list as it is known for a while that
> my mail agent is rejected @vger.kernel.org for what I am not clear
> about. It makes my day occasionally :P

I don't I ever received the full patch, only replies to your patch.

Please mail your patch directly to me so I have a clean version.

Regards,

	Hans

> 
> Thanks
> Hillf
> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] media: usbvision: Fix a use after free in v4l2_release()
       [not found]           ` <20200214124825.12568-1-hdanton@sina.com>
@ 2020-02-14 13:34             ` Hans Verkuil
  0 siblings, 0 replies; 12+ messages in thread
From: Hans Verkuil @ 2020-02-14 13:34 UTC (permalink / raw)
  To: Hillf Danton
  Cc: Laurent Pinchart, Dan Carpenter, syzbot, Alan Stern,
	Allison Randal, Greg Kroah-Hartman, Souptick Joarder, andreyknvl,
	bnvandana, linux-kernel, linux-media, linux-usb, mchehab,
	syzkaller-bugs

On 2/14/20 1:48 PM, Hillf Danton wrote:
> 
> On Fri, 14 Feb 2020 13:21:03 +0100 Hans Verkuil wrote:
>> On 2/14/20 1:14 PM, Hillf Danton wrote:
>>>
>>> On Fri, 14 Feb 2020 12:30:29 +0100 Hans Verkuil wrote:
>>>>
>>>> Hillf, if you want your patch to be merged, then make sure it is CC-ed to
>>>> linux-media as well.
>>>
>>> Please pick it up if it makes a sense to you and it was sent with
>>> linux-media added on the Cc list as it is known for a while that
>>> my mail agent is rejected @vger.kernel.org for what I am not clear
>>> about. It makes my day occasionally :P
>>
>> I don't I ever received the full patch, only replies to your patch.
>>
> 
> =========
> Please take a look at the Cc tag in the snippet from
> https://lore.kernel.org/lkml/b1d071e2-0428-a08c-392d-3ca5d4a7e710@xs4all.nl/T/#m13f2c16b7cd1892f6a0b58b72ce37979cb6940b9
> 
> 
>> Subject: [PATCH] media: usbvision: add the release callback for video device
>> From: Hillf Danton <hdanton@sina.com>
>>
>> To fix the UAF syzbot reported,
>>
>> BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
>>
>> a release cb which is a simple wrapper of usbvision_release() is added
>> for releasing resources as the last reference to the usbvision video
>> device goes away.
>>
>> Reported-by: syzbot <syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com>
>> Fixes: 2aa689dd8057 ("[media] usbvision: embed video_device")
>> Cc: Hans Verkuil <hans.verkuil@cisco.com>

Ah, my work email. And I always delete posts mailed to that if it is also CC-ed
to linux-media. Which didn't actually arrive there in your case. And it's no longer
in my trash folder either.

So please mail me a properly formatted patch so I can forward it to linux-media
for you.

Regards,

	Hans

>> Signed-off-by: Hillf Danton <hdanton@sina.com>
>> ---
> =========
> 
>> Please mail your patch directly to me so I have a clean version.
> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, back to index

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-08 20:24 KASAN: use-after-free Read in v4l2_release (3) syzbot
2020-01-22 22:58 ` syzbot
     [not found] ` <20200123102707.2596-1-hdanton@sina.com>
2020-01-23 12:19   ` Laurent Pinchart
     [not found]   ` <20200124022847.11244-1-hdanton@sina.com>
2020-01-24 12:41     ` Andrey Konovalov
2020-01-24 12:54       ` syzbot
2020-01-24 14:10   ` Dan Carpenter
2020-01-24 14:13   ` [PATCH] media: usbvision: Fix a use after free in v4l2_release() Dan Carpenter
2020-02-14 10:06     ` Hans Verkuil
2020-02-14 11:22       ` Laurent Pinchart
2020-02-14 11:30         ` Hans Verkuil
     [not found]         ` <20200214121447.13612-1-hdanton@sina.com>
2020-02-14 12:21           ` Hans Verkuil
     [not found]           ` <20200214124825.12568-1-hdanton@sina.com>
2020-02-14 13:34             ` Hans Verkuil

Linux-Media Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-media/0 linux-media/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-media linux-media/ https://lore.kernel.org/linux-media \
		linux-media@vger.kernel.org
	public-inbox-index linux-media

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-media


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git