Skip to content

Input: sur40 - fix UAF/hang on closing the video node after unplug

Nguyen Ngoc Thang linux-input linux-media lkml stable 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 patches 2 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2Input: sur40 - don't wait for buffers nothing will completedrivers/input/touchscreen/sur40.c+3 -31 reply
2/2Input: sur40 - keep device state alive until the video node is releaseddrivers/input/touchscreen/sur40.c+12 -31 reply

Reviews trailers per patch

1 of 2 patches has a Reviewed-by or Acked-by.

PatchHans Verkuil
1/2
2/2R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH v1 0/2] Input: sur40 - fix UAF/hang on closing the video node after unplug

Hi,

syzbot reported a slab-use-after-free in vb2_core_queue_release() [1]:
closing /dev/v4l-touch* after the SUR40 was unplugged reads freed memory.

Cause: sur40_disconnect() kfree()s struct sur40_state, which embeds the video_device, v4l2_device and vb2_queue, even though a video node can still be open. v4l2_release() and vb2_fop_release() then dereference freed memory. Patch 2 fixes this by giving the v4l2_device a release() callback that frees the state, and dropping the disconnect path's reference with v4l2_device_put(), so the last close does the freeing. The probe error paths never expose the node and still free directly.

Patch 1 is needed first: once the state outlives disconnect, closing a node that is still streaming hangs forever, because sur40_stop_streaming() waits (vb2_wait_for_all_buffers) for buffers that only the input poll callback completes, and that is gone after unplug. Returning the queued buffers before the wait fixes it. This was masked by the UAF above.

Testing: no hardware, so I emulated a SUR40 (045e:0775) with raw-gadget on dummy_hcd in QEMU with KASAN. The reproducer enumerates the device, starts a non-blocking read() on the video node (making the fd the queue owner), disconnects the gadget, then close()s the fd.
- before: KASAN: slab-use-after-free in v4l2_release(), allocated in sur40_probe(), freed in sur40_disconnect() (same alloc/free stacks as the syzbot report)
- patch 1 only: no KASAN, but close() blocks in vb2_wait_for_all_buffers() [only meaningful with patch 2 applied]
- both patches: 5 consecutive enumerate/disconnect/close cycles, no KASAN, no hang.
(The dma_map_sg WARNING during read() in the log comes from dummy_hcd having no DMA mask; it is unrelated.)

Nguyen Ngoc Thang (2):
Input: sur40 - don't wait for buffers nothing will complete
Input: sur40 - keep device state alive until the video node is
released

 drivers/input/touchscreen/sur40.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0