tty: n_gsm: tx_work can be queued after gsm_cleanup_mux() flushed it
Hi Greg, Jiri,
Before anything else, a question. In September you said that n_gsm should only be touched by people who can test it and have a system that uses it. I do not have such hardware; I only tested with a pty. Does that still apply to bug reports like this one? If it does, please ignore this mail and I will not send more n_gsm reports.
The issue needs CAP_NET_ADMIN to attach N_GSM and a gsmtty close racing with the ldisc being removed from the base tty. The window is a few instructions and I could only hit it after adding delays, so it may not matter much in practice.
gsm_send() adds the frame to tx_ctrl_list under tx_lock, drops the lock, and then calls gsmld_write_trigger(), which checks dlci[0]->dead and calls schedule_work(&gsm->tx_work). If gsm_cleanup_mux() runs between that check and schedule_work(), it sets dlci[0]->dead and calls flush_work(&gsm->tx_work) while nothing is queued yet. The work is queued after that, nothing cancels it before gsm_free_mux() frees the mux, and gsmld_write_task() then runs on freed memory.
On v7.3 with KASAN I added a udelay() between the check and schedule_work() in gsmld_write_trigger(), and an msleep() at the start of gsmld_write_task() so that the worker runs after the free. Closing /dev/gsmtty1, which sends DISC through gsm_send(), while another thread set the base tty back to N_TTY gave:
BUG: KASAN: slab-use-after-free in _raw_spin_lock_irqsave+0x40/0x60 Read of size 1 at addr ffff888111b183d8 by task kworker/0:2/791 Workqueue: events gsmld_write_task _raw_spin_lock_irqsave gsmld_write_task process_scheduled_works worker_thread Allocated by task 9384: gsmld_open tty_ldisc_open tty_set_ldisc Freed by task 10: kfree mux_put release_one_tty Last potentially related work creation: queue_work_on gsmtty_close tty_release
That was one hit in 85 attempts over 60 seconds. Without the delays I did not hit it. n_gsm.c is the same in next-20261113.
cancel_work_sync() cannot go into gsm_free_mux(), because mux_put() calls it under gsm_mux_lock with interrupts disabled. One option is to set dlci[0]->dead under tx_lock in gsm_cleanup_mux(), and to make the dead check and schedule_work() happen under tx_lock too, so that the flush_work() after it is the last one. __gsm_data_queue() already calls gsmld_write_trigger() with tx_lock held, but gsm_send(), the CMD_FCON and XOFF paths and gsmld_write_wakeup() do not, so the lock cannot simply be taken inside gsmld_write_trigger(). I have not written or tested a fix.
Thanks,
Cheng Yongkang
Greg Kroah-Hartman
maintainerQuoted 7 lines · Cheng Yongkang
Hi Greg, Jiri,
Before anything else, a question. In September you said that
n_gsmshould only be touched by people who can test it and have a system that uses it. I do not have such hardware; I only tested with a pty. Does that still apply to bug reports like this one? If it does, please ignore this mail and I will not send moren_gsmreports.
What has changed from my original statement that would allow such changes to be accepted?
And again, please do not send "here is a potential problem" reports without a real patch. Your llm can provide that just as easily.
thanks,
greg k-h