Skip to contentSkip to conversation

Input: sur40 - fix use-after-free in sur40_open()

PATCH In review Deepanshu Kartikey 2 participants 5 messages Aug 6 – Oct 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: sur40 - fix use-after-free in sur40_open()

If any of the registration steps after input_register_device() fail (v4l2_device_register(), vb2_queue_init(), ctrl handler setup, video_register_device()), the error path frees sur40 via input_free_device() + kfree() without ever calling input_unregister_device(). Since the input device was already registered, it remains live and openable, so a racing open() (e.g. from udevd) can dereference the freed sur40 via sur40_open().

Split the unwind so that once input_register_device() has succeeded, the failure path properly calls input_unregister_device() before freeing sur40 and its buffer.

Fixes 08b936012964 ("Input: sur40 - switch to using polled mode of input devices")
Reported-by [email protected]
---
 drivers/input/touchscreen/sur40.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/input/touchscreen/sur40.c +4 -0
index fe63d53d56db..b0958422ff76 100644
@@ -808,6 +808,10 @@ static int sur40_probe(struct usb_interface *interface,
 	video_unregister_device(&sur40->vdev);
 err_unreg_v4l2:
 	v4l2_device_unregister(&sur40->v4l2);
+	input_unregister_device(input);
+	kfree(sur40->bulk_in_buffer);
+	kfree(sur40);
+	return error;
 err_free_buffer:
 	kfree(sur40->bulk_in_buffer);
 err_free_input:
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Premature free of the sur40 struct in sur40_disconnect() leading to a Use-After-Free when V4L2 file handles are closed.
- [Medium] Missing v4l2_ctrl_handler_free() in the video_register_device() failure path.
--

--- Patch [1]: [PATCH] Input: sur40 - fix use-after-free in sur40_open() --- commit e0be966f67bc10328835ca2aa6f5d5b458a14ed3
Author: Deepanshu Kartikey <[email protected]>

Input: sur40 - fix use-after-free in sur40_open()

If any of the registration steps after input_register_device() fail, the error path frees sur40 via input_free_device() + kfree() without ever calling input_unregister_device(). This patch splits the unwind so that once input_register_device() has succeeded, the failure path properly calls input_unregister_device() before freeing sur40 and its buffer.

drivers/input/touchscreen/sur40.c static int sur40_probe(struct usb_interface *interface, · line 813

Quoted 11 lines
drivers/input/touchscreen/sur40.c +3 -0
index 77ec2c94b91fd..4b3a4d5386e4c 100644
@@ -808,6 +808,10 @@ static int sur40_probe(struct usb_interface *interface,
 	video_unregister_device(&sur40->vdev);
 err_unreg_v4l2:
 	v4l2_device_unregister(&sur40->v4l2);
+	input_unregister_device(input);
+	kfree(sur40->bulk_in_buffer);
+	kfree(sur40);

[Severity: Medium]
This is a pre-existing issue, but if video_register_device() fails, the code jumps to err_unreg_video and falls through to err_unreg_v4l2 here.

Unlike the failure path for v4l2_ctrl_handler_setup(), does this path skip calling v4l2_ctrl_handler_free(&sur40->hdl) before freeing the sur40 structure?

Could this lead to a memory leak of the dynamically allocated structures within the V4L2 control handler?

drivers/input/touchscreen/sur40.c static int sur40_probe(struct usb_interface *interface, · line 817

Quoted 4 lines
drivers/input/touchscreen/sur40.c +1 -0
+	return error;
 err_free_buffer:
 	kfree(sur40->bulk_in_buffer);
 err_free_input:

[Severity: High]
This isn't a bug introduced by this patch, but looking at sur40_disconnect():

drivers/input/touchscreen/sur40.c:sur40_disconnect() {
	...
	video_unregister_device(&sur40->vdev);
	v4l2_device_unregister(&sur40->v4l2);

	input_unregister_device(sur40->input);
	kfree(sur40->bulk_in_buffer);
	kfree(sur40);
	...
}

Does freeing the sur40 context immediately without waiting for V4L2 file handles to be fully released destroy the still-referenced vdev?

The sur40_state structure embeds a struct video_device using the .release = video_device_release_empty dummy callback, which opts out of V4L2 internal refcounting.

Since video_unregister_device() does not block until open file handles are closed, if an unprivileged userspace process holds an open file descriptor to the V4L2 device when it is disconnected, the sur40 memory is freed while actively referenced.

When the application later closes the fd, the V4L2 core calls vb2_fop_release(), which dereferences vdev->queue (located inside the freed sur40 struct). Can this trigger a use-after-free kernel panic?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Deepanshu Kartikey
Quoted 38 lines · Deepanshu Kartikey

If any of the registration steps after input_register_device() fail (v4l2_device_register(), vb2_queue_init(), ctrl handler setup, video_register_device()), the error path frees sur40 via input_free_device() + kfree() without ever calling input_unregister_device(). Since the input device was already registered, it remains live and openable, so a racing open() (e.g. from udevd) can dereference the freed sur40 via sur40_open().

Split the unwind so that once input_register_device() has succeeded, the failure path properly calls input_unregister_device() before freeing sur40 and its buffer.

Fixes 08b936012964 ("Input: sur40 - switch to using polled mode of input devices")
Reported-by [email protected]
---
 drivers/input/touchscreen/sur40.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/input/touchscreen/sur40.c +4 -1
index fe63d53d56db..b0958422ff76 100644
@@ -808,6 +808,10 @@ static int sur40_probe(struct usb_interface *interface,
        video_unregister_device(&sur40->vdev);
 err_unreg_v4l2:
        v4l2_device_unregister(&sur40->v4l2);
+       input_unregister_device(input);
+       kfree(sur40->bulk_in_buffer);
+       kfree(sur40);
+       return error;
 err_free_buffer:
        kfree(sur40->bulk_in_buffer);
 err_free_input:
--
Quoted 2 lines

2.34.1

Gentle Reminder. Please let me know the status of this patch.

Thanks

Deepanshu

#3 lore raw
In reply to Deepanshu Kartikey
Quoted 38 lines · Deepanshu Kartikey

If any of the registration steps after input_register_device() fail (v4l2_device_register(), vb2_queue_init(), ctrl handler setup, video_register_device()), the error path frees sur40 via input_free_device() + kfree() without ever calling input_unregister_device(). Since the input device was already registered, it remains live and openable, so a racing open() (e.g. from udevd) can dereference the freed sur40 via sur40_open().

Split the unwind so that once input_register_device() has succeeded, the failure path properly calls input_unregister_device() before freeing sur40 and its buffer.

Fixes 08b936012964 ("Input: sur40 - switch to using polled mode of input devices")
Reported-by [email protected]
---
 drivers/input/touchscreen/sur40.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/input/touchscreen/sur40.c +4 -1
index fe63d53d56db..b0958422ff76 100644
@@ -808,6 +808,10 @@ static int sur40_probe(struct usb_interface *interface,
        video_unregister_device(&sur40->vdev);
 err_unreg_v4l2:
        v4l2_device_unregister(&sur40->v4l2);
+       input_unregister_device(input);
+       kfree(sur40->bulk_in_buffer);
+       kfree(sur40);
+       return error;
 err_free_buffer:
        kfree(sur40->bulk_in_buffer);
 err_free_input:
--
Quoted 2 lines

2.34.1

Gentle Reminder. Please let me know the status of this patch.

Thanks

Deepanshu

#4 lore raw
In reply to Deepanshu Kartikey
Quoted 38 lines · Deepanshu Kartikey

If any of the registration steps after input_register_device() fail (v4l2_device_register(), vb2_queue_init(), ctrl handler setup, video_register_device()), the error path frees sur40 via input_free_device() + kfree() without ever calling input_unregister_device(). Since the input device was already registered, it remains live and openable, so a racing open() (e.g. from udevd) can dereference the freed sur40 via sur40_open().

Split the unwind so that once input_register_device() has succeeded, the failure path properly calls input_unregister_device() before freeing sur40 and its buffer.

Fixes 08b936012964 ("Input: sur40 - switch to using polled mode of input devices")
Reported-by [email protected]
---
 drivers/input/touchscreen/sur40.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/input/touchscreen/sur40.c +4 -1
index fe63d53d56db..b0958422ff76 100644
@@ -808,6 +808,10 @@ static int sur40_probe(struct usb_interface *interface,
        video_unregister_device(&sur40->vdev);
 err_unreg_v4l2:
        v4l2_device_unregister(&sur40->v4l2);
+       input_unregister_device(input);
+       kfree(sur40->bulk_in_buffer);
+       kfree(sur40);
+       return error;
 err_free_buffer:
        kfree(sur40->bulk_in_buffer);
 err_free_input:
--
Quoted 2 lines

2.34.1

Gentle Reminder. Please let me know the status of this patch.

Thanks

Deepanshu