Skip to content

USB: gadget: f_hid: fix lifetime issues

John Keeping linux-usb lkml oe-kbuild-all 2 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest6 patches 0 repliesNew
  2. v16 patches 0 repliesSuperseded

Changes in v2 from the cover letter

- Fix compiling as a module

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/6USB: gadget: f_hid: move chardev setup to module initdrivers/usb/gadget/function/f_hid.c drivers/usb/gadget/function/u_hid.h+20 -240 replies
2/6USB: gadget: f_hid: switch to IDR for tracking minorsdrivers/usb/gadget/function/f_hid.c+9 -90 replies
3/6USB: gadget: f_hid: find f_hidg by IDR lookup on opendrivers/usb/gadget/function/f_hid.c+12 -20 replies
4/6USB: gadget: f_hid: decouple cdev from f_hidg lifetimedrivers/usb/gadget/function/f_hid.c+31 -110 replies
5/6USB: gadget: f_hid: refcount f_hidg structuredrivers/usb/gadget/function/f_hid.c+17 -20 replies
6/6USB: gadget: f_hid: return ENODEV from read/write after deletiondrivers/usb/gadget/function/f_hid.c+15 -30 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/6] USB: gadget: f_hid: fix lifetime issues

File descriptors referencing the /dev/hidgN device created by the HID gadget can outlive the underlying gadget function, which creates easy to trigger use-after-frees in the kernel.

A simple reproduction for this using the libusbgx example programs is:

	$ gadget-hid
	$ exec 3<> /dev/hidg0
	$ gadget-vid-pid-remove
	$ exec 3<&-

Closing the file descriptor on the last line triggers a use-after-free which can be seen immediately with slub_debug=P.

This series fixes this by making the struct cdev associated with the module rather than dynamically allocated for the gadget and changing struct f_hidg to be refcounted instead of tied to the gadget lifetime.

v2:
- Fix compiling as a module

John Keeping (6):
USB: gadget: f_hid: move chardev setup to module init
USB: gadget: f_hid: switch to IDR for tracking minors
USB: gadget: f_hid: find f_hidg by IDR lookup on open
USB: gadget: f_hid: decouple cdev from f_hidg lifetime
USB: gadget: f_hid: refcount f_hidg structure
USB: gadget: f_hid: return ENODEV from read/write after deletion

 drivers/usb/gadget/function/f_hid.c | 150 +++++++++++++++++++---------
 drivers/usb/gadget/function/u_hid.h |   3 -
 2 files changed, 103 insertions(+), 50 deletions(-)
Signature · 1 line
2.23.0