Skip to contentSkip to conversation

Input: atkbd - fix uaf in atkbd_set_repeat_rate

PATCH In review Jeffin Philip 3 participants 5 messages Aug 29 – Aug 30

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

PATCH v4 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:
- Changed in v4:
Changed location of disable_pending_work_sync() after input_unregister_device()
as per Dmitry's suggestion.

- 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 | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/input/keyboard/atkbd.c +2 -5
index 5736f4bc5a50..41a0d55dae0e 100644
@@ -966,12 +966,9 @@ static void atkbd_disconnect(struct serio *serio)
 	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);
 
 	serio_close(serio);
 	serio_set_drvdata(serio, NULL);
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

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

Pre-existing issues:
- [High] The patch fails to resolve the Use-After-Free (UAF) race condition it intends to fix because disable_delayed_work_sync() is incorrectly placed after input_unregister_device(). --

commit 364595b57a843b20a975005b27712754f014825e
Author: Jeffin Philip <[email protected]>

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")
Quoted 6 lines
drivers/input/keyboard/atkbd.c +0 -0
index 3509b58e64920..0d75a7ba48d3c 100644
@@ -966,12 +966,9 @@ static void atkbd_disconnect(struct serio *serio)
 	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 +2 -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);

[Severity: High]
This isn't a bug introduced by this patch, but does calling disable_delayed_work_sync() after input_unregister_device() leave a Use-After-Free race window open for the background work?

If atkbd_event_work() executes concurrently with atkbd_disconnect() and passes the atkbd->enabled check, it can access atkbd->dev after it has been freed.

For example, if atkbd_disconnect() calls input_unregister_device(atkbd->dev) which drops the last reference and frees the device, the background work could then access the freed dev in atkbd_set_leds():

drivers/input/keyboard/atkbd.c:atkbd_set_leds() {
    ...
	param[0] = (test_bit(LED_SCROLLL, dev->led) ? 1 : 0)
    ...
}

Should disable_delayed_work_sync() be moved above input_unregister_device() to fully close this race window and ensure the work item cannot execute after the device is freed?

Quoted 3 lines
drivers/input/keyboard/atkbd.c +0 -0
 	serio_close(serio);
 	serio_set_drvdata(serio, NULL);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Jeffin Philip
Quoted 12 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")
Quoted 5 lines

---
Changelog:
- Changed in v4:
Changed location of disable_pending_work_sync() after input_unregister_device()
as per Dmitry's suggestion.

Actually I think I was wrong and Sashiko was right. I will probably pick your v3 but I do not like that we do not clean up/reset LEDs properly on disconnect. I will have to think about it some more...

Thanks.

Signature · 1 line
Dmitry
#3 lore raw
Quoted 3 lines · Dmitry Torokhov

Actually I think I was wrong and Sashiko was right. I will probably pick your v3 but I do not like that we do not clean up/reset LEDs properly on disconnect.

Would cleaning up leds cause any weird issues on reconnect? We could reset LEDs though. Compiled only, but this?

drivers/input/keyboard/atkbd.c +5 -2
index 41a0d55dae0e..3d0e58be5ba2 100644
@@ -963,13 +963,16 @@ static void atkbd_disconnect(struct serio *serio)
 
 	atkbd_disable(atkbd);
 
-	input_unregister_device(atkbd->dev);
-
 	/*
 	 * Prevent work from being rescheduled after atkbd is marked disabled
 	 */
 	disable_delayed_work_sync(&atkbd->event_work);
 
+	if (atkbd->write)
+		atkbd_set_leds(atkbd);
+
+	input_unregister_device(atkbd->dev);
+
 	serio_close(serio);
 	serio_set_drvdata(serio, NULL);
 	kfree(atkbd);

Thanks,
Jeffin.

#4 lore raw
Quoted 1 line · Jeffin Philip

Would cleaning up leds cause any weird issues on reconnect?

Correction: Would _NOT_ cleaning up leds cause any weird issues on reconnect?

Thanks,
Jeffin.