[PATCH 1/3] serial: max310x: fix use-after-free on remove

From: Tapio Reijonen

Date: Fri Nov 20 2026 - 02:33:58 EST


max310x_remove() cancels a port's timer and work items first and
unregisters the port afterwards. Until uart_remove_one_port() has shut
the port down, an open port keeps queuing its tx_work from the
interrupt handler and from start_tx(), and the RS485 code re-arms the
delay timer and rts_work from those, so a work item or the timer can
still run after the cancels, on a port that is being torn down, and
fire after the port memory has been freed.

The unregister itself queues work too: the hangup it triggers clears
DTR and RTS through set_mctrl(), which schedules md_work, so a cancel
done before uart_remove_one_port() cannot cover it. Tested with the
port memory zeroed on free, md_work ran after the unbind on the freed
port and oopsed in max310x_md_proc().

The interrupt handler is a source of its own. The IRQ enable bits
only gate the pin, the status bits stay latched regardless, so on the
single-port parts, where the handler reads the port status on every
event of the shared line, a status bit left behind by shutdown() can
still queue tx_work after the cancels. With the IRQ managed by devres
it was released only after remove() had returned.

Similar to commit 36672c8d7d14 ("serial: amba-pl011: cancel RS485
hrtimers after freeing IRQ"), stop the sources before the cancels:
unregister the ports, so that the shutdown() this triggers stops the
TX path, then free the IRQ, then cancel the timers and work items. The
IRQ goes second because the handler loops while a port has status or
RX data pending and free_irq() waits for it: freed with the ports
still open under traffic, the unbind blocked until the traffic paused.
Keep the two per-port steps in helpers; the probe error path needs
them too, with no IRQ handling since the request is probe's last step.

Fixes: 10d8b34a4217 ("serial: max310x: Driver rework")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Tapio Reijonen <tapio.reijonen@xxxxxxxxxxx>
---
drivers/tty/serial/max310x.c | 47 +++++++++++++++++++++++++++++++++-----------
1 file changed, 36 insertions(+), 11 deletions(-)

diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index a0ed70185555eed745d905b36c8b8f162468dd6f..f5b32303353208d9f85ae0b04d38a9abdd398d24 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -1856,6 +1856,28 @@ static const struct serial_rs485 max310x_rs485_supported = {
.delay_rts_after_send = 1,
};

+/*
+ * Unregister the port before cancelling its work: until the port is
+ * gone, an open port can queue the work and arm the timer again, and
+ * the hangup queues md_work itself. The caller frees the IRQ in
+ * between where there is one. A port that was never registered has
+ * nothing queued.
+ */
+static void max310x_port_unregister(struct max310x_one *one)
+{
+ if (test_and_clear_bit(one->port.line, max310x_lines))
+ uart_remove_one_port(&max310x_uart, &one->port);
+}
+
+static void max310x_port_cancel_work(struct max310x_one *one)
+{
+ hrtimer_cancel(&one->tx_delay_tmr);
+ cancel_work_sync(&one->rts_work);
+ cancel_work_sync(&one->tx_work);
+ cancel_work_sync(&one->md_work);
+ cancel_work_sync(&one->rs_work);
+}
+
static int max310x_probe(struct device *dev, const struct max310x_devtype *devtype,
const struct max310x_if_cfg *if_cfg,
struct regmap *regmaps[], int irq)
@@ -2068,9 +2090,9 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
max310x_power(&s->p[i].port, 0);
}

- /* Setup interrupt */
- ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist,
- IRQF_ONESHOT | IRQF_SHARED, dev_name(dev), s);
+ /* Setup interrupt, freed by max310x_remove() */
+ ret = request_threaded_irq(irq, NULL, max310x_ist,
+ IRQF_ONESHOT | IRQF_SHARED, dev_name(dev), s);
if (!ret)
return 0;

@@ -2093,16 +2115,19 @@ static void max310x_remove(struct device *dev)
struct max310x_port *s = dev_get_drvdata(dev);
int i;

- for (i = 0; i < s->devtype->nr; i++) {
- hrtimer_cancel(&s->p[i].tx_delay_tmr);
- cancel_work_sync(&s->p[i].rts_work);
- cancel_work_sync(&s->p[i].tx_work);
- cancel_work_sync(&s->p[i].md_work);
- cancel_work_sync(&s->p[i].rs_work);
+ /*
+ * The handler loops while a port has status or RX data pending and
+ * free_irq() waits for it: unregister first, so that the shutdown()
+ * this triggers masks the interrupts and stops the transmitter.
+ */
+ for (i = 0; i < s->devtype->nr; i++)
+ max310x_port_unregister(&s->p[i]);

- if (test_and_clear_bit(s->p[i].port.line, max310x_lines))
- uart_remove_one_port(&max310x_uart, &s->p[i].port);
+ /* The handler queues tx_work from latched status regardless of IRQEN */
+ free_irq(s->p[0].port.irq, s);

+ for (i = 0; i < s->devtype->nr; i++) {
+ max310x_port_cancel_work(&s->p[i]);
max310x_power(&s->p[i].port, 0);
}


--
2.47.3