From 575c608be857b8733a896cd662a68539bb3a588b Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Thu, 9 Jan 2025 13:35:02 +0200 Subject: [PATCH] drivers/usbdev/cdcacm.c: Fix a crash in cdcacm if usbdev gets unregistered while client calls close for the tty Make sure that the cdcacm is disconnected before the usbdev gets unregistered. Also, check if the device is connected or not in cdcuart_txempty (uart_txempty). Otherwise there may be a crash during uart_tcdrain, called in tty close path, if the usbdev unregistration happens during the loop. This issue can be triggered by monitoring the cable connection status in one thread, sending BOARDIOC_USBDEV_DISCONNECT if the usb cable is detached. In another thread close the ttyACM. Signed-off-by: Jukka Laitinen --- drivers/usbdev/cdcacm.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/usbdev/cdcacm.c b/drivers/usbdev/cdcacm.c index 403ad6e230..d3cc8a5ad3 100644 --- a/drivers/usbdev/cdcacm.c +++ b/drivers/usbdev/cdcacm.c @@ -2618,6 +2618,13 @@ static bool cdcuart_txempty(FAR struct uart_dev_s *dev) #endif flags = enter_critical_section(); + + if (dev->disconnected) + { + leave_critical_section(flags); + return true; + } + priv->ispolling = true; EP_POLL(ep); priv->ispolling = false; @@ -3012,6 +3019,10 @@ void cdcacm_uninitialize(FAR struct usbdevclass_driver_s *classdev) char devname[CDCACM_DEVNAME_SIZE]; int ret; + /* Disconnect in case we are connected */ + + cdcacm_disconnect(classdev, priv->usbdev); + #ifndef CONFIG_CDCACM_COMPOSITE usbdev_unregister(&drvr->drvr); #endif