Input: matrix_keypad - use flush_delayed_work()
Subsystems (2)
Dmitry Torokhov
maintainerPATCH 1/1 Input: matrix_keypad - use flush_delayed_work()
We should be using flush_delayed_work() instead of flush_work() in matrix_keypad_stop() to ensure that we are not missing work that is scheduled but not yet put in the workqueue (i.e. its delay timer has not expired yet).
--- drivers/input/keyboard/matrix_keypad.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/keyboard/matrix_keypad.c +1 -1
@@ -222,7 +222,7 @@ static void matrix_keypad_stop(struct input_dev *dev) keypad->stopped = true;spin_unlock_irq (&keypad->lock); -flush_work (&keypad->work.work); +flush_delayed_work (&keypad->work);/* * matrix_keypad_scan() will leave IRQs enabled; * we should disable them now.
Signature · 5 lines
2.20.1.611.gfbb209baf1-goog -- Dmitry
Hi Dmitry,
Quoted 6 lines · Dmitry Torokhov
We should be using
flush_delayed_work()instead offlush_work()inmatrix_keypad_stop()to ensure that we are not missing work that is scheduled but not yet put in the workqueue (i.e. its delay timer has not expired yet).
Could the following scenario cause a use-after-free? (I am adding comments on lines starting with -->)
a) user closes the device handle:
static void matrix_keypad_stop(struct input_dev *dev)
{
struct matrix_keypad *keypad = input_get_drvdata(dev);
spin_lock_irq(&keypad->lock);
keypad->stopped = true;
spin_unlock_irq(&keypad->lock);
flush_work(&keypad->work.work);
-->
--> new interrupt comes in, and schedules new delayed keypad->work (1)
-->
/*
* matrix_keypad_scan() will leave IRQs enabled;
* we should disable them now.
*/
disable_row_irqs(keypad);
}b) user removes the keypad, or unloads the module:
static int matrix_keypad_remove(struct platform_device *pdev)
{
struct matrix_keypad *keypad = platform_get_drvdata(pdev);
matrix_keypad_free_gpio(keypad);
input_unregister_device(keypad->input_dev);
kfree(keypad);-->
--> delayed keypad->work scheduled at (1) above executes anywhere past here, --> and causes a user-after-free.
-->
return 0; }
Dmitry Torokhov
maintainerHi Sven,
Quoted 25 lines · Sven Van Asbroeck
Hi Dmitry,
Quoted 6 lines · Dmitry Torokhov
We should be using
flush_delayed_work()instead offlush_work()inmatrix_keypad_stop()to ensure that we are not missing work that is scheduled but not yet put in the workqueue (i.e. its delay timer has not expired yet).Could the following scenario cause a use-after-free? (I am adding comments on lines starting with -->)
a) user closes the device handle:
static void
matrix_keypad_stop(struct input_dev *dev){ struct matrix_keypad *keypad = input_get_drvdata(dev); spin_lock_irq(&keypad->lock); keypad->stopped = true; spin_unlock_irq(&keypad->lock); flush_work(&keypad->work.work);
Quoted 2 lines
-->
--> new interrupt comes in, and schedules new delayedkeypad->work(1)
It will not schedule new work because we check keypad->stopped flag in ISR.
Thanks.
Signature · 1 line
Dmitry
Sven Van Asbroeck
Reviewed-byQuoted 4 lines · Dmitry Torokhov
It will not schedule new work because we check
keypad->stoppedflag in ISR.
Yes, you are correct. Sorry for the confusion.