All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] NFC: port100: fix use-after-free in port100_send_complete
@ 2022-03-08 18:50 Pavel Skripkin
  2022-03-09  9:52 ` Krzysztof Kozlowski
  2022-03-10  4:10 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 5+ messages in thread
From: Pavel Skripkin @ 2022-03-08 18:50 UTC (permalink / raw)
  To: krzysztof.kozlowski, sameo, thierry.escande
  Cc: netdev, linux-kernel, Pavel Skripkin, syzbot+16bcb127fb73baeecb14

Syzbot reported UAF in port100_send_complete(). The root case is in
missing usb_kill_urb() calls on error handling path of ->probe function.

port100_send_complete() accesses devm allocated memory which will be
freed on probe failure. We should kill this urbs before returning an
error from probe function to prevent reported use-after-free

Fail log:

BUG: KASAN: use-after-free in port100_send_complete+0x16e/0x1a0 drivers/nfc/port100.c:935
Read of size 1 at addr ffff88801bb59540 by task ksoftirqd/2/26
...
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:88 [inline]
 dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106
 print_address_description.constprop.0.cold+0x8d/0x303 mm/kasan/report.c:255
 __kasan_report mm/kasan/report.c:442 [inline]
 kasan_report.cold+0x83/0xdf mm/kasan/report.c:459
 port100_send_complete+0x16e/0x1a0 drivers/nfc/port100.c:935
 __usb_hcd_giveback_urb+0x2b0/0x5c0 drivers/usb/core/hcd.c:1670

...

Allocated by task 1255:
 kasan_save_stack+0x1e/0x40 mm/kasan/common.c:38
 kasan_set_track mm/kasan/common.c:45 [inline]
 set_alloc_info mm/kasan/common.c:436 [inline]
 ____kasan_kmalloc mm/kasan/common.c:515 [inline]
 ____kasan_kmalloc mm/kasan/common.c:474 [inline]
 __kasan_kmalloc+0xa6/0xd0 mm/kasan/common.c:524
 alloc_dr drivers/base/devres.c:116 [inline]
 devm_kmalloc+0x96/0x1d0 drivers/base/devres.c:823
 devm_kzalloc include/linux/device.h:209 [inline]
 port100_probe+0x8a/0x1320 drivers/nfc/port100.c:1502

Freed by task 1255:
 kasan_save_stack+0x1e/0x40 mm/kasan/common.c:38
 kasan_set_track+0x21/0x30 mm/kasan/common.c:45
 kasan_set_free_info+0x20/0x30 mm/kasan/generic.c:370
 ____kasan_slab_free mm/kasan/common.c:366 [inline]
 ____kasan_slab_free+0xff/0x140 mm/kasan/common.c:328
 kasan_slab_free include/linux/kasan.h:236 [inline]
 __cache_free mm/slab.c:3437 [inline]
 kfree+0xf8/0x2b0 mm/slab.c:3794
 release_nodes+0x112/0x1a0 drivers/base/devres.c:501
 devres_release_all+0x114/0x190 drivers/base/devres.c:530
 really_probe+0x626/0xcc0 drivers/base/dd.c:670

Reported-and-tested-by: syzbot+16bcb127fb73baeecb14@syzkaller.appspotmail.com
Fixes: 0347a6ab300a ("NFC: port100: Commands mechanism implementation")
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
---
 drivers/nfc/port100.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/nfc/port100.c b/drivers/nfc/port100.c
index d7db1a0e6be1..00d8ea6dcb5d 100644
--- a/drivers/nfc/port100.c
+++ b/drivers/nfc/port100.c
@@ -1612,7 +1612,9 @@ static int port100_probe(struct usb_interface *interface,
 	nfc_digital_free_device(dev->nfc_digital_dev);
 
 error:
+	usb_kill_urb(dev->in_urb);
 	usb_free_urb(dev->in_urb);
+	usb_kill_urb(dev->out_urb);
 	usb_free_urb(dev->out_urb);
 	usb_put_dev(dev->udev);
 
-- 
2.35.1


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

* Re: [PATCH] NFC: port100: fix use-after-free in port100_send_complete
  2022-03-08 18:50 [PATCH] NFC: port100: fix use-after-free in port100_send_complete Pavel Skripkin
@ 2022-03-09  9:52 ` Krzysztof Kozlowski
  2022-03-09 18:27   ` Pavel Skripkin
  2022-03-10  4:10 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 5+ messages in thread
From: Krzysztof Kozlowski @ 2022-03-09  9:52 UTC (permalink / raw)
  To: Pavel Skripkin, sameo, thierry.escande
  Cc: netdev, linux-kernel, syzbot+16bcb127fb73baeecb14

On 08/03/2022 19:50, Pavel Skripkin wrote:
> Syzbot reported UAF in port100_send_complete(). The root case is in
> missing usb_kill_urb() calls on error handling path of ->probe function.
> 
> port100_send_complete() accesses devm allocated memory which will be
> freed on probe failure. We should kill this urbs before returning an
> error from probe function to prevent reported use-after-free
> 
> Fail log:
> 
> BUG: KASAN: use-after-free in port100_send_complete+0x16e/0x1a0 drivers/nfc/port100.c:935
> Read of size 1 at addr ffff88801bb59540 by task ksoftirqd/2/26
> ...
> Call Trace:
>  <TASK>
>  __dump_stack lib/dump_stack.c:88 [inline]
>  dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106
>  print_address_description.constprop.0.cold+0x8d/0x303 mm/kasan/report.c:255
>  __kasan_report mm/kasan/report.c:442 [inline]
>  kasan_report.cold+0x83/0xdf mm/kasan/report.c:459
>  port100_send_complete+0x16e/0x1a0 drivers/nfc/port100.c:935
>  __usb_hcd_giveback_urb+0x2b0/0x5c0 drivers/usb/core/hcd.c:1670
> 
> ...
> 
> Allocated by task 1255:
>  kasan_save_stack+0x1e/0x40 mm/kasan/common.c:38
>  kasan_set_track mm/kasan/common.c:45 [inline]
>  set_alloc_info mm/kasan/common.c:436 [inline]
>  ____kasan_kmalloc mm/kasan/common.c:515 [inline]
>  ____kasan_kmalloc mm/kasan/common.c:474 [inline]
>  __kasan_kmalloc+0xa6/0xd0 mm/kasan/common.c:524
>  alloc_dr drivers/base/devres.c:116 [inline]
>  devm_kmalloc+0x96/0x1d0 drivers/base/devres.c:823
>  devm_kzalloc include/linux/device.h:209 [inline]
>  port100_probe+0x8a/0x1320 drivers/nfc/port100.c:1502
> 
> Freed by task 1255:
>  kasan_save_stack+0x1e/0x40 mm/kasan/common.c:38
>  kasan_set_track+0x21/0x30 mm/kasan/common.c:45
>  kasan_set_free_info+0x20/0x30 mm/kasan/generic.c:370
>  ____kasan_slab_free mm/kasan/common.c:366 [inline]
>  ____kasan_slab_free+0xff/0x140 mm/kasan/common.c:328
>  kasan_slab_free include/linux/kasan.h:236 [inline]
>  __cache_free mm/slab.c:3437 [inline]
>  kfree+0xf8/0x2b0 mm/slab.c:3794
>  release_nodes+0x112/0x1a0 drivers/base/devres.c:501
>  devres_release_all+0x114/0x190 drivers/base/devres.c:530
>  really_probe+0x626/0xcc0 drivers/base/dd.c:670
> 
> Reported-and-tested-by: syzbot+16bcb127fb73baeecb14@syzkaller.appspotmail.com
> Fixes: 0347a6ab300a ("NFC: port100: Commands mechanism implementation")
> Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
> ---
>  drivers/nfc/port100.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/nfc/port100.c b/drivers/nfc/port100.c
> index d7db1a0e6be1..00d8ea6dcb5d 100644
> --- a/drivers/nfc/port100.c
> +++ b/drivers/nfc/port100.c
> @@ -1612,7 +1612,9 @@ static int port100_probe(struct usb_interface *interface,
>  	nfc_digital_free_device(dev->nfc_digital_dev);
>  
>  error:
> +	usb_kill_urb(dev->in_urb);
>  	usb_free_urb(dev->in_urb);
> +	usb_kill_urb(dev->out_urb);
>  	usb_free_urb(dev->out_urb);
>  	usb_put_dev(dev->udev);


Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>

Thanks, this looks good. I think I saw similar patterns also in other
drivers, e.g. pn533. I will check it later, but if you have spare time,
feel free to investigate.

Similar cases (unresolved):
https://syzkaller.appspot.com/bug?extid=1dc8b460d6d48d7ef9ca
https://syzkaller.appspot.com/bug?extid=abd2e0dafb481b621869
https://syzkaller.appspot.com/bug?extid=dbec6695a6565a9c6bc0


Best regards,
Krzysztof

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

* Re: [PATCH] NFC: port100: fix use-after-free in port100_send_complete
  2022-03-09  9:52 ` Krzysztof Kozlowski
@ 2022-03-09 18:27   ` Pavel Skripkin
  2022-03-09 21:22     ` Pavel Skripkin
  0 siblings, 1 reply; 5+ messages in thread
From: Pavel Skripkin @ 2022-03-09 18:27 UTC (permalink / raw)
  To: Krzysztof Kozlowski, sameo, thierry.escande
  Cc: netdev, linux-kernel, syzbot+16bcb127fb73baeecb14

Hi Krzysztof,

On 3/9/22 12:52, Krzysztof Kozlowski wrote:
> 
> 
> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
> 
> Thanks, this looks good. I think I saw similar patterns also in other
> drivers, e.g. pn533. I will check it later, but if you have spare time,
> feel free to investigate.
> 
> Similar cases (unresolved):
> https://syzkaller.appspot.com/bug?extid=1dc8b460d6d48d7ef9ca
> https://syzkaller.appspot.com/bug?extid=abd2e0dafb481b621869
> https://syzkaller.appspot.com/bug?extid=dbec6695a6565a9c6bc0
> 

Thanks for reviewing and pointing out to these bugs! Will take a look


With regards,
Pavel Skripkin

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

* Re: [PATCH] NFC: port100: fix use-after-free in port100_send_complete
  2022-03-09 18:27   ` Pavel Skripkin
@ 2022-03-09 21:22     ` Pavel Skripkin
  0 siblings, 0 replies; 5+ messages in thread
From: Pavel Skripkin @ 2022-03-09 21:22 UTC (permalink / raw)
  To: Krzysztof Kozlowski; +Cc: netdev, linux-kernel, syzbot+16bcb127fb73baeecb14

On 3/9/22 21:27, Pavel Skripkin wrote:
> Hi Krzysztof,
> 
> On 3/9/22 12:52, Krzysztof Kozlowski wrote:
>> 
>> 
>> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
>> 
>> Thanks, this looks good. I think I saw similar patterns also in other
>> drivers, e.g. pn533. I will check it later, but if you have spare time,
>> feel free to investigate.
>> 
>> Similar cases (unresolved):
>> https://syzkaller.appspot.com/bug?extid=1dc8b460d6d48d7ef9ca

This one is crazy :) No logs from driver at all. Even can't find where 
probe failure comes from (or even is there any failures...)

>> https://syzkaller.appspot.com/bug?extid=abd2e0dafb481b621869

Looks like this patch fixes it.

>> https://syzkaller.appspot.com/bug?extid=dbec6695a6565a9c6bc0
>> 

This one is already fixed. Fix bisection is bogus, but this bug is not 
reproducible anymore.



With regards,
Pavel Skripkin

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

* Re: [PATCH] NFC: port100: fix use-after-free in port100_send_complete
  2022-03-08 18:50 [PATCH] NFC: port100: fix use-after-free in port100_send_complete Pavel Skripkin
  2022-03-09  9:52 ` Krzysztof Kozlowski
@ 2022-03-10  4:10 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-03-10  4:10 UTC (permalink / raw)
  To: Pavel Skripkin
  Cc: krzysztof.kozlowski, sameo, thierry.escande, netdev,
	linux-kernel, syzbot+16bcb127fb73baeecb14

Hello:

This patch was applied to netdev/net.git (master)
by Jakub Kicinski <kuba@kernel.org>:

On Tue,  8 Mar 2022 21:50:07 +0300 you wrote:
> Syzbot reported UAF in port100_send_complete(). The root case is in
> missing usb_kill_urb() calls on error handling path of ->probe function.
> 
> port100_send_complete() accesses devm allocated memory which will be
> freed on probe failure. We should kill this urbs before returning an
> error from probe function to prevent reported use-after-free
> 
> [...]

Here is the summary with links:
  - NFC: port100: fix use-after-free in port100_send_complete
    https://git.kernel.org/netdev/net/c/f80cfe2f2658

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2022-03-10  4:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-08 18:50 [PATCH] NFC: port100: fix use-after-free in port100_send_complete Pavel Skripkin
2022-03-09  9:52 ` Krzysztof Kozlowski
2022-03-09 18:27   ` Pavel Skripkin
2022-03-09 21:22     ` Pavel Skripkin
2022-03-10  4:10 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.