Input: atkbd - fix UAF in atkbd_set_repeat_rate() on disconnect
Subsystems (2)
PATCH v2 1/1 Input: atkbd - fix UAF in atkbd_set_repeat_rate() on disconnect
Commit 0ef7a26af127 ("Input: atkbd - fix canceling event_work in disconnect") moved cancel_delayed_work_sync() after input_unregister_device() on the premise that events may arrive until input_unregister_device returns.
However, this created a UAF as work that may have passed the atkbd->enabled check in atkbd_event_work() may attempt to dereference dev which is freed in input_unregister_device(). Reverting the commit also does not solve the issue as events may still come through and pass the enabled check as atkbd_disable() and event_work() guard with different locks. Fix this by closing the hardware first using serio_close() and then unregistering to prevent work from executing after input_unregister_device(). serio_close() closes the device preventing work from arriving. Additionally add a check for atkbd->enabled in atkbd_event() to prevent userspace from queuing new events after atkbd_disable() sets atkbd->enabled to false.
---
Changelog:
- Changes in v2:
Added a check for atkbd->enabled so userspace cannot queue events after atkbd_disable. Link to v1: https://lore.kernel.org/all/[email protected]/T/#u --- drivers/input/keyboard/atkbd.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/input/keyboard/atkbd.c +11 -7
@@ -672,6 +672,9 @@ static int atkbd_event(struct input_dev *dev,if (!atkbd->write)return -1 ; +if (!atkbd->enabled) +return -1 ; +switch (type) {case EV_LED :atkbd_schedule_event_work (atkbd,ATKBD_LED_EVENT_BIT );
@@ -963,17 +966,18 @@ static void atkbd_disconnect(struct serio *serio)atkbd_disable (atkbd); -input_unregister_device (atkbd->dev); -/* -* Make sure we don't have a command in flight. -* Note that since atkbd->enabled is false event work will keep -* rescheduling itself until it gets canceled and will not try -* accessing freed input device or serio port. +* close serio first so device will not get any data, which prevents +* atkbd_event_work from being rescheduled after cancel_delayed_work_sync +* returns. This ensures no work can dereference atkbd->dev after it has +* been freed. */ -cancel_delayed_work_sync (&atkbd->event_work);serio_close (serio); +cancel_delayed_work_sync (&atkbd->event_work); + +input_unregister_device (atkbd->dev); +serio_set_drvdata (serio,NULL );kfree (atkbd); }
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] A Time-of-Check to Time-of-Use (TOCTOU) race condition in `atkbd_event()` allows delayed work to…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] A Time-of-Check to Time-of-Use (TOCTOU) race condition in atkbd_event() allows delayed work to be queued after cancel_delayed_work_sync(), leading to a Use-After-Free of the atkbd structure. --
commit 0f8148a1f313524a8dd641967f56cd788e4c8ac4
Author: Jeffin Philip <[email protected]>
Input: atkbd - fix UAF in atkbd_set_repeat_rate() on disconnect
This commit attempts to fix a use-after-free issue by closing the serio port before canceling delayed work and adding an enabled check to atkbd_event(). It aims to prevent work from being scheduled after disconnection.
drivers/input/keyboard/atkbd.c static int atkbd_event(struct input_dev *dev, · line 677
Quoted 10 lines
drivers/input/keyboard/atkbd.c +3
-0@@ -672,6 +672,9 @@ static int atkbd_event(struct input_dev *dev,if (!atkbd->write)return -1 ; +if (!atkbd->enabled) +return -1 ; +
[Severity: High]
Can this lockless check introduce a Time-of-Check to Time-of-Use (TOCTOU) race condition?
If userspace calls evdev_write(), which invokes atkbd_event(), a thread could read atkbd->enabled as true here, but then stall or be preempted before actually scheduling the work.
[ ... ]
Quoted 13 lines
drivers/input/keyboard/atkbd.c +2
-6@@ -963,17 +966,18 @@ static void atkbd_disconnect(struct serio *serio)atkbd_disable (atkbd); -input_unregister_device (atkbd->dev); -/* -* Make sure we don't have a command in flight. -* Note that since atkbd->enabled is false event work will keep -* rescheduling itself until it gets canceled and will not try -* accessing freed input device or serio port. +* close serio first so device will not get any data, which prevents +* atkbd_event_work from being rescheduled after cancel_delayed_work_sync
drivers/input/keyboard/atkbd.c static void atkbd_disconnect(struct serio *serio) · line 980
Quoted 10 lines
drivers/input/keyboard/atkbd.c +6
-1+* returns. This ensures no work can dereference atkbd->dev after it has +* been freed. */ -cancel_delayed_work_sync (&atkbd->event_work);serio_close (serio); +cancel_delayed_work_sync (&atkbd->event_work); + +input_unregister_device (atkbd->dev); +
[Severity: High]
If the thread running atkbd_event() is delayed after checking atkbd->enabled, could it resume here and queue work after cancel_delayed_work_sync() has already returned?
Since serio_close() only stops hardware interrupts and does not block userspace from invoking atkbd_event(), atkbd_disconnect() could proceed to unregister the device and free the atkbd structure.
When the delayed thread finally resumes and queues the work, won't that queued work execute later and dereference the freed atkbd structure?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Dmitry Torokhov
maintainerHi Jeffin,
Quoted 4 lines · Jeffin Philip
Commit
0ef7a26af127("Input: atkbd - fix cancelingevent_workin disconnect") movedcancel_delayed_work_sync()afterinput_unregister_device()on the premise that events may arrive untilinput_unregister_devicereturns.
Quoted 11 lines
However, this created a UAF as work that may have passed the
atkbd->enabledcheck inatkbd_event_work()may attempt to dereference dev which is freed ininput_unregister_device(). Reverting the commit also does not solve the issue as events may still come through and pass the enabled check asatkbd_disable()andevent_work()guard with different locks. Fix this by closing the hardware first usingserio_close()and then unregistering to prevent work from executing afterinput_unregister_device().serio_close()closes the device preventing work from arriving. Additionally add a check foratkbd->enabledinatkbd_event()to prevent userspace from queuing new events afteratkbd_disable()setsatkbd->enabledto false.
I believe the proper fix is to replace cancel_delayed_work_sync() with disable_delayed_work_sync() to prevent rescheduling due to atkbd being marked as disabled.
Thanks.
Signature · 1 line
Dmitry