Re: [PATCH] Input: psmouse - fix use-after-free bugs due to rescheduled delayed works
From: Dmitry Torokhov
Date: Sat Nov 08 2025 - 00:43:41 EST
Hi Duoming,
On Sat, Nov 08, 2025 at 12:56:09PM +0800, Duoming Zhou wrote:
> The flush_workqueue() in psmouse_disconnect() only blocks and waits for
> work items that were already queued to the workqueue prior to its
> invocation. Any work items submitted after flush_workqueue() is called
> are not included in the set of tasks that the flush operation awaits.
> This means that after flush_workqueue() has finished executing, the
> resync_work and dev3_register_work could be rescheduled again, resulting
> in the following two use-after-free scenarios:
>
> 1. The psmouse structure is deallocated in psmouse_disconnect(), while
> resync_work remains active and attempts to dereference the already
> freed psmouse in psmouse_resync().
>
> CPU 0 | CPU 1
> psmouse_disconnect() | psmouse_receive_byte()
> | if(psmouse->state == ...)
> psmouse_set_state() |
> flush_workqueue() |
> | psmouse_queue_work() //reschedule
> kfree(psmouse); //FREE|
> | psmouse_resync()
> | psmouse = container_of(); //USE
> | psmouse-> //USE
Before flushing the workqueue we set psmouse state to PSMOUSE_CMD_MODE,
but psmouse_queue_work() is only called from psmouse_receive_byte() if
the mouse is PSMOUSE_ACTIVE. Therefore there is no chance that work will
be scheduled while psmouse instance is being freed.
For ALPS, the work is a "single shot", so will not get rescheduled.
However I think that the changes are improvement to the code. Please
split in 2 (for psmouse-base and alps separately) and drop mentions of
UAF in psmouse but rather mention that disable_delayed_work_sync() is
more robust and efficient. I think if we switch to it we should be able
to get rid of kpsmoused workqueue and use default system workqueue.
Thanks.
--
Dmitry