Skip to contentSkip to conversation

Input: atkbd - fix uaf in atkbd_set_repeat_rate

PATCH Superseded Jeffin Philip 2 participants 2 messages Aug 28 – Aug 29

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Input: atkbd - fix uaf in atkbd_set_repeat_rate

atkbd_disable() marks atkbd as disabled to prevent any work event to be executed, however this can race with atkbd_event_work(). If a pending work passes the atkbd->enabled check and atkbd_disable() runs after in disconnect, it could dereference dev which is freed in disconnect following atkbd being marked disabled. Fix this by adding disable_delayed_work_sync() which drops all work events preventing any rescheduling after atkbd is marked disabled.

Reported-by [email protected]
Fixes 0ef7a26af127 ("Input: atkbd - fix canceling event_work in disconnect")

---
Changelog:
- Changes in v3:
Rewrote the patch to include disable_delayed_work_sync() as per
Dmitry's suggestion.

- Changes in v2:

     Added a check for atkbd->enabled so userspace cannot queue events after
   atkbd_disable.
---
 drivers/input/keyboard/atkbd.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/input/keyboard/atkbd.c +4 -7
index 5736f4bc5a50..a92a04c005aa 100644
@@ -963,15 +963,12 @@ 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.
+	 * Prevent work from being rescheduled after atkbd is marked disabled
 	 */
-	cancel_delayed_work_sync(&atkbd->event_work);
+	disable_delayed_work_sync(&atkbd->event_work);
+
+	input_unregister_device(atkbd->dev);
 
 	serio_close(serio);
 	serio_set_drvdata(serio, NULL);
Signature · 1 line
2.55.0
maintainer
#1 lore raw

Hi Jeffin,

Quoted 36 lines · Jeffin Philip

atkbd_disable() marks atkbd as disabled to prevent any work event to be executed, however this can race with atkbd_event_work(). If a pending work passes the atkbd->enabled check and atkbd_disable() runs after in disconnect, it could dereference dev which is freed in disconnect following atkbd being marked disabled. Fix this by adding disable_delayed_work_sync() which drops all work events preventing any rescheduling after atkbd is marked disabled.

Reported-by [email protected]
Fixes 0ef7a26af127 ("Input: atkbd - fix canceling event_work in disconnect")

---
Changelog:
- Changes in v3:
Rewrote the patch to include disable_delayed_work_sync() as per
Dmitry's suggestion.

- Changes in v2:

     Added a check for atkbd->enabled so userspace cannot queue events after
   atkbd_disable.
---
 drivers/input/keyboard/atkbd.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/input/keyboard/atkbd.c +0 -2
index 5736f4bc5a50..a92a04c005aa 100644
@@ -963,15 +963,12 @@ static void atkbd_disconnect(struct serio *serio)
 
 	atkbd_disable(atkbd);
 
-	input_unregister_device(atkbd->dev);
-
 	/*

drivers/input/keyboard/atkbd.c static void atkbd_disconnect(struct serio *serio) · line 971

Quoted 10 lines
drivers/input/keyboard/atkbd.c +4 -5
-	 * 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.
+	 * Prevent work from being rescheduled after atkbd is marked disabled
 	 */
-	cancel_delayed_work_sync(&atkbd->event_work);
+	disable_delayed_work_sync(&atkbd->event_work);
+
+	input_unregister_device(atkbd->dev);

There is no need to move the call to input_unregister_device(). When atkbd->disabled is true the work handler will not attempt to access input device or serio structures. The only change that is needed is replacing cancel_delayed_work_sync with disable_delayed_work_sync.

Thanks.

Signature · 1 line
Dmitry