[PATCH] Input: powermate - fix use-after-free in powermate_config_complete

From: Javier Carrasco
Date: Sat Sep 16 2023 - 17:29:07 EST


syzbot has found a use-after-free bug [1] in the powermate driver. This
happens when the device is disconnected, which leads to a memory free
from the powermate_device struct.
When an asynchronous control message completes after the kfree and its
callback is invoked, the lock does not exist anymore and hence the bug.

Return immediately if the URB status is -ESHUTDOWN (the actual status
that triggered this bug) or -ENOENT, avoiding any access to potentially
freed memory.

[1] https://syzkaller.appspot.com/bug?extid=0434ac83f907a1dbdd1e

Signed-off-by: Javier Carrasco <javier.carrasco.cruz@xxxxxxxxx>
Reported-by: syzbot+0434ac83f907a1dbdd1e@xxxxxxxxxxxxxxxxxxxxxxxxx
---
drivers/input/misc/powermate.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/input/misc/powermate.c b/drivers/input/misc/powermate.c
index c1c733a9cb89..f61333fea35f 100644
--- a/drivers/input/misc/powermate.c
+++ b/drivers/input/misc/powermate.c
@@ -196,8 +196,11 @@ static void powermate_config_complete(struct urb *urb)
struct powermate_device *pm = urb->context;
unsigned long flags;

- if (urb->status)
+ if (urb->status) {
printk(KERN_ERR "powermate: config urb returned %d\n", urb->status);
+ if (status == -ENOENT || status == -ESHUTDOWN)
+ return;
+ }

spin_lock_irqsave(&pm->lock, flags);
powermate_sync_state(pm);

---
base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d
change-id: 20230916-topic-powermate_use_after_free-c703c7969c91

Best regards,
--
Javier Carrasco <javier.carrasco.cruz@xxxxxxxxx>