Re: [PATCH] tty/vt: Fix possible deadlock in input_inject_event
From: Dmitry Torokhov
Date: Mon Sep 29 2025 - 00:54:35 EST
Hi,
On Sun, Sep 28, 2025 at 09:08:19PM +0800, pengyu wrote:
> syzkaller testing revealed a potential deadlock involving keyboard
> handling:
>
> CPU0 CPU1 CPU2
> ---- ---- ----
> read_lock(tasklist_lock); evdev_write
> input_inject_event write_lock(tasklist_lock);
> lock(&dev->event_lock);
> read_lock(tasklist_lock);
> <Interrupt>
> kbd_bh() / kd_sound_helper()
> input_inject_event
> lock(&dev->event_lock); // Deadlock risk
>
> The deadlock occurs because:
> 1. Both kbd_bh and kd_sound_helper run in interrupt context
> 2. tasklist_lock is interrupt-unsafe
> 3. When evdev_write holds both dev->event_lock and tasklist_lock,
> interrupt context attempts to acquire dev->event_lock create deadlock
> risks
>
> Convert both kbd_bh and kd_sound_helper to use workqueues. This moves
> input_inject_event execution to process context, where it's safe to
> acquire locks that may be held by code using interrupt-unsafe locks.
So if we ignore the input code and instead look at the send_sigio()
(which input core ends up calling) and do_wait() we see that
send_sigio() disables interrupts and takes the owner's spinlock
before taking the tasklist_lock, while do_wait() takes the tasklist_lock
first, without disabling interrupts. This is root of the issue as far as
I can tell and no amount of changes to the keyboard handler (which is
just happens to be in the middle) will not solve for all potential cases
and code paths.
I believe either do_exit() or send_sigio() have to be changed to fix
this properly.
Thanks.
--
Dmitry