mirror-linux/drivers/usb/gadget/function
Norihiko Hama 6334b8e455 usb: gadget: f_ncm: Fix UAF ncm object at re-bind after usb ep transport error
When ncm function is working and then stop usb0 interface for link down,
eth_stop() is called. At this piont, accidentally if usb transport error
should happen in usb_ep_enable(), 'in_ep' and/or 'out_ep' may not be enabled.

After that, ncm_disable() is called to disable for ncm unbind
but gether_disconnect() is never called since 'in_ep' is not enabled.

As the result, ncm object is released in ncm unbind
but 'dev->port_usb' associated to 'ncm->port' is not NULL.

And when ncm bind again to recover netdev, ncm object is reallocated
but usb0 interface is already associated to previous released ncm object.

Therefore, once usb0 interface is up and eth_start_xmit() is called,
released ncm object is dereferrenced and it might cause use-after-free memory.

[function unlink via configfs]
  usb0: eth_stop dev->port_usb=ffffff9b179c3200
  --> error happens in usb_ep_enable().
  NCM: ncm_disable: ncm=ffffff9b179c3200
  --> no gether_disconnect() since ncm->port.in_ep->enabled is false.
  NCM: ncm_unbind: ncm unbind ncm=ffffff9b179c3200
  NCM: ncm_free: ncm free ncm=ffffff9b179c3200   <-- released ncm

[function link via configfs]
  NCM: ncm_alloc: ncm alloc ncm=ffffff9ac4f8a000
  NCM: ncm_bind: ncm bind ncm=ffffff9ac4f8a000
  NCM: ncm_set_alt: ncm=ffffff9ac4f8a000 alt=0
  usb0: eth_open dev->port_usb=ffffff9b179c3200  <-- previous released ncm
  usb0: eth_start dev->port_usb=ffffff9b179c3200 <--
  eth_start_xmit()
  --> dev->wrap()
  Unable to handle kernel paging request at virtual address dead00000000014f

This patch addresses the issue by checking if 'ncm->netdev' is not NULL at
ncm_disable() to call gether_disconnect() to deassociate 'dev->port_usb'.
It's more reasonable to check 'ncm->netdev' to call gether_connect/disconnect
rather than check 'ncm->port.in_ep->enabled' since it might not be enabled
but the gether connection might be established.

Signed-off-by: Norihiko Hama <Norihiko.Hama@alpsalpine.com>
Cc: stable <stable@kernel.org>
Link: https://lore.kernel.org/r/20240327023550.51214-1-Norihiko.Hama@alpsalpine.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-04-04 16:55:53 +02:00
..
Makefile
f_acm.c
f_ecm.c
f_eem.c
f_fs.c usb: gadget: functionfs: Wait for fences before enqueueing DMABUF 2024-04-04 15:20:30 +02:00
f_hid.c usb: gadget: f_hid: fix report descriptor allocation 2023-12-07 10:12:05 +09:00
f_loopback.c
f_mass_storage.c usb: f_mass_storage: forbid async queue when shutdown happen 2024-01-27 16:40:07 -08:00
f_mass_storage.h
f_midi.c usb: gadget: f_midi: Replace strlcpy() with strscpy() 2023-11-22 12:02:53 +00:00
f_midi2.c
f_ncm.c usb: gadget: f_ncm: Fix UAF ncm object at re-bind after usb ep transport error 2024-04-04 16:55:53 +02:00
f_obex.c
f_phonet.c
f_printer.c
f_rndis.c
f_serial.c
f_sourcesink.c
f_subset.c
f_tcm.c usb: gadget: f_tcm: Remove snprintf() from sysfs call-backs and replace with sysfs_emit() 2023-12-04 22:25:17 +09:00
f_uac1.c usb: gadget: f_uac1: Replace snprintf() with the safer scnprintf() variant 2023-12-15 13:55:29 +01:00
f_uac1_legacy.c
f_uac2.c usb: gadget: f_uac2: Replace snprintf() with the safer scnprintf() variant 2023-12-15 13:55:29 +01:00
f_uvc.c Revert "usb: gadget: f_uvc: change endpoint allocation in uvc_function_bind()" 2024-01-04 16:01:45 +01:00
f_uvc.h usb: gadget: uvc: prevent use of disabled endpoint 2023-11-23 12:32:44 +00:00
g_zero.h
ndis.h
rndis.c
rndis.h
storage_common.c
storage_common.h
tcm.h
u_audio.c
u_audio.h
u_ecm.h
u_eem.h
u_ether.c usb: gadget: constify the struct device_type usage 2024-02-19 09:35:46 +01:00
u_ether.h
u_ether_configfs.h
u_fs.h
u_gether.h
u_hid.h
u_midi.h
u_midi2.h
u_ncm.h usb: gadget: ncm: Add support to update wMaxSegmentSize via configfs 2024-01-04 16:01:14 +01:00
u_phonet.h
u_printer.h
u_rndis.h
u_serial.c
u_serial.h
u_tcm.h
u_uac1.h
u_uac1_legacy.c
u_uac1_legacy.h
u_uac2.h
u_uvc.h usb: gadget: webcam: Make g_webcam loadable again 2023-12-15 18:27:38 +01:00
uac_common.h
uvc.h usb:gadget:uvc Do not use worker thread to pump isoc usb requests 2023-11-23 12:32:44 +00:00
uvc_configfs.c usb: gadget: uvc: Replace snprintf() with the safer scnprintf() variant 2023-12-15 13:55:29 +01:00
uvc_configfs.h
uvc_queue.c
uvc_queue.h
uvc_v4l2.c usb: gadget: uvc: Fix use-after-free for inflight usb_requests 2023-11-23 12:32:44 +00:00
uvc_v4l2.h
uvc_video.c usb: gadget: uvc: mark incomplete frames with UVC_STREAM_ERR 2024-02-17 17:08:45 +01:00
uvc_video.h usb: gadget: uvc: move video disable logic to its own function 2023-11-23 12:32:44 +00:00