serial: 8250_aspeed_vuart: Fix potential stale port on probe failure
PATCH 1/1 serial: 8250_aspeed_vuart: Fix potential stale port on probe failure
Invalid LPC configuration can fail probe after the UART port has been registered. Removing only the sysfs group leaves a port whose private data and mappings are released by device-managed cleanup.
Disable the VUART, shut down its timer and unregister the port before returning from these failures.
This issue was found by our static analysis tool under development.
--- drivers/tty/serial/8250/8250_aspeed_vuart.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/tty/serial/8250/8250_aspeed_vuart.c +7 -3
@@ -513,7 +513,7 @@ static int aspeed_vuart_probe(struct platform_device *pdev) rc =aspeed_vuart_set_lpc_address (vuart, prop);if (rc <0 ) {dev_err_probe (dev, rc,"invalid value in aspeed,lpc-io-reg property\n" ); - goto err_sysfs_remove; + goto err_unregister_port; } rc =of_property_read_u32_array (np,"aspeed,lpc-interrupts" , sirq,2 );
@@ -525,14 +525,14 @@ static int aspeed_vuart_probe(struct platform_device *pdev) rc =aspeed_vuart_set_sirq (vuart, sirq[0 ]);if (rc <0 ) {dev_err_probe (dev, rc,"invalid sirq number in aspeed,lpc-interrupts property\n" ); - goto err_sysfs_remove; + goto err_unregister_port; } sirq_polarity =aspeed_vuart_map_irq_polarity (sirq[1 ]);if (sirq_polarity <0 ) { rc =dev_err_probe (dev, sirq_polarity,"invalid sirq polarity in aspeed,lpc-interrupts property\n" ); - goto err_sysfs_remove; + goto err_unregister_port; }aspeed_vuart_set_sirq_polarity (vuart, sirq_polarity);
@@ -543,6 +543,10 @@ static int aspeed_vuart_probe(struct platform_device *pdev)return 0 ; +err_unregister_port: +aspeed_vuart_set_enabled (vuart, false); +timer_shutdown_sync (&vuart->unthrottle_timer); +serial8250_unregister_port (vuart->port); err_sysfs_remove:sysfs_remove_group (&vuart->dev->kobj, &aspeed_vuart_attr_group);return rc;
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] serial: 8250_aspeed_vuart…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] serial: 8250_aspeed_vuart: use-after-free in aspeed_vuart_remove() [1]
--
commit 4eac67f7d5031d7988614ed386938a68b8050e5b
Author: Guangshuo Li <[email protected]>
serial: 8250_aspeed_vuart: Fix potential stale port on probe failure
This commit disables the VUART, shuts down its unthrottle timer, and unregisters the 8250 port when LPC configuration fails during probe.
Quoted 4 lines
drivers/tty/serial/8250/8250_aspeed_vuart.c +0
-0
[ ... ]
drivers/tty/serial/8250/8250_aspeed_vuart.c static int aspeed_vuart_probe(struct platform_device *pdev) · line 549
Quoted 8 lines
drivers/tty/serial/8250/8250_aspeed_vuart.c +4
-0@@ -543,6 +543,10 @@ static int aspeed_vuart_probe(struct platform_device *pdev)return 0 ; +err_unregister_port: +aspeed_vuart_set_enabled (vuart, false); +timer_shutdown_sync (&vuart->unthrottle_timer); +serial8250_unregister_port (vuart->port);
[Severity: Critical]
This problem wasn't introduced by this patch, but can [1] aspeed_vuart_remove() cause a use-after-free on vuart when unthrottle_timer is re-armed during removal?
drivers/tty/serial/8250/8250_aspeed_vuart.c:aspeed_vuart_remove() {
struct aspeed_vuart *vuart = platform_get_drvdata(pdev);
timer_delete_sync(&vuart->unthrottle_timer);
aspeed_vuart_set_enabled(vuart, false);
serial8250_unregister_port(vuart->port);
sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
}Because aspeed_vuart_remove() calls timer_delete_sync() instead of timer_shutdown_sync() before serial8250_unregister_port(), a UART interrupt arriving while the TTY flip buffers are full can re-arm vuart->unthrottle_timer before the port is unregistered:
CPU0 CPU1aspeed_vuart_remove()
timer_delete_sync(&vuart->unthrottle_timer)
aspeed_vuart_handle_irq()
if (!space)
mod_timer(&vuart->unthrottle_timer, ...)
serial8250_unregister_port(...)
// vuart freed by devres on return
aspeed_vuart_unthrottle_exp()
<-- use-after-freeUnregistering the port before stopping the timer, or using timer_shutdown_sync() as this patch does on the probe error path, would prevent the timer from being re-armed.
[1] https://sashiko.dev/#/bug/linux-2b3eb48a-af6f-4ebd-a642-413d2498b067
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1