From 824dd4c75a1d79953c4edb1ece7d492af0c9fcce Mon Sep 17 00:00:00 2001 From: pkwr-air <107416820+pkwr-air@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:38:54 +0100 Subject: [PATCH] fix(USB): prevent hardfault on unplugging [v1.18] (#29187) * fix(cdcacm_autostart): wait for mavlink stop before serdis on USB unplug On VBUS loss, 'mavlink stop' was spawned and serdis called on the next cycle without waiting for the stop to finish. serdis could then tear down the CDC/ACM driver while mavlink still had /dev/ttyACM0 open, causing a use-after-free in the driver (hardfault in cdcacm_recvpacket). Keep the pid of the stop task and only run serdis once it has exited (polled with kill(pid, 0), up to 5 s). waitpid() is not used because CONFIG_SCHED_WAITPID is not enabled on all boards. Also log exec_builtin() failures. * fix(mavlink): close log files in the receiver thread The log handler opens the log file in the receiver thread, but on 'mavlink stop' its destructor ran in the stop task and fclose()d it there. On NuttX a FILE belongs to the task group that opened it: fclose() from another task frees it while it stays linked in the owner's stream list, and the owner frees it again on exit. This double free corrupted the heap when USB was unplugged during a log download (hardfault in mm_free, or load_mon hanging in mallinfo()). Close the files at the end of MavlinkReceiver::run(), in the owning thread. * fix(nuttx): update submodule with CDC/ACM use-after-free fix - drivers/serial,usbdev/cdcacm: free cdcacm device on last close (adapted from apache/nuttx#12199) - drivers/usbdev/cdcacm: flush rxpending on reset and harden paths after unbind --- platforms/nuttx/NuttX/nuttx | 2 +- .../cdcacm_autostart/cdcacm_autostart.cpp | 26 ++++++++++++++++++- .../cdcacm_autostart/cdcacm_autostart.h | 3 +++ src/modules/mavlink/mavlink_log_handler.cpp | 12 +++++++++ src/modules/mavlink/mavlink_log_handler.h | 9 +++++++ src/modules/mavlink/mavlink_receiver.cpp | 3 +++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/platforms/nuttx/NuttX/nuttx b/platforms/nuttx/NuttX/nuttx index 17040b9f87c..3cc0c496015 160000 --- a/platforms/nuttx/NuttX/nuttx +++ b/platforms/nuttx/NuttX/nuttx @@ -1 +1 @@ -Subproject commit 17040b9f87cb17913cab3f5a5ce009be5526be31 +Subproject commit 3cc0c49601597d4a0c4df43b134b0dbc633dd04c diff --git a/src/drivers/cdcacm_autostart/cdcacm_autostart.cpp b/src/drivers/cdcacm_autostart/cdcacm_autostart.cpp index 2ed3d7a8991..f05741b4885 100644 --- a/src/drivers/cdcacm_autostart/cdcacm_autostart.cpp +++ b/src/drivers/cdcacm_autostart/cdcacm_autostart.cpp @@ -47,6 +47,7 @@ __END_DECLS #include #include +#include ModuleBase::Descriptor CdcAcmAutostart::desc{task_spawn, custom_command, print_usage}; @@ -368,6 +369,23 @@ void CdcAcmAutostart::state_disconnecting() { PX4_DEBUG("state_disconnecting"); + // Wait for 'mavlink stop' to finish before serdis tears down the CDC/ACM + // driver: until then mavlink can still have /dev/ttyACM0 open. + if (_mavlink_stop_pid > 0) { + const bool stop_running = (kill(_mavlink_stop_pid, 0) == 0); + + if (stop_running && (hrt_elapsed_time(&_mavlink_stop_time) < 5_s)) { + _reschedule_time = 100_ms; + return; + } + + if (stop_running) { + PX4_WARN("mavlink stop on %s did not finish", USB_DEVICE_PATH); + } + + _mavlink_stop_pid = -1; + } + close_ttyacm(); // Disconnect serial @@ -533,7 +551,8 @@ bool CdcAcmAutostart::start_mavlink() void CdcAcmAutostart::stop_mavlink() { char *stop_argv[] { (char *)"mavlink", (char *)"stop", (char *)"-d", (char *)USB_DEVICE_PATH, nullptr }; - execute_process(stop_argv); + _mavlink_stop_pid = execute_process(stop_argv); + _mavlink_stop_time = hrt_absolute_time(); } bool CdcAcmAutostart::start_nsh() @@ -573,6 +592,11 @@ int CdcAcmAutostart::execute_process(char *const *argv) pid = exec_builtin(argv[0], argv, nullptr, 0); sched_unlock(); + + if (pid <= 0) { + PX4_ERR("exec_builtin(%s %s) failed: %d errno %d", argv[0], argv[1] ? argv[1] : "", pid, errno); + } + return pid; } diff --git a/src/drivers/cdcacm_autostart/cdcacm_autostart.h b/src/drivers/cdcacm_autostart/cdcacm_autostart.h index fe3f3879f5d..96873712610 100644 --- a/src/drivers/cdcacm_autostart/cdcacm_autostart.h +++ b/src/drivers/cdcacm_autostart/cdcacm_autostart.h @@ -39,6 +39,7 @@ #include #include #include +#include #include @@ -116,6 +117,8 @@ private: bool _vbus_present{false}; bool _vbus_present_prev{false}; int _ttyacm_fd{-1}; + int _mavlink_stop_pid{-1}; + hrt_abstime _mavlink_stop_time{0}; char _buffer[80] {}; int _bytes_read{0}; diff --git a/src/modules/mavlink/mavlink_log_handler.cpp b/src/modules/mavlink/mavlink_log_handler.cpp index d899b5205a6..6dbc7f10d2e 100644 --- a/src/modules/mavlink/mavlink_log_handler.cpp +++ b/src/modules/mavlink/mavlink_log_handler.cpp @@ -115,6 +115,18 @@ void MavlinkLogHandler::handle_message(const mavlink_message_t *msg) } } +void MavlinkLogHandler::close_files() +{ + if (_current_entry.fp) { + fclose(_current_entry.fp); + _current_entry.fp = nullptr; + } + + _current_entry = LogEntry{}; + _entry_request = LogEntryRequest{}; + _state = LogHandlerState::Idle; +} + void MavlinkLogHandler::state_idle() { if (_current_entry.fp && _file_send_finished) { diff --git a/src/modules/mavlink/mavlink_log_handler.h b/src/modules/mavlink/mavlink_log_handler.h index cff026fc5f0..466f6811ace 100644 --- a/src/modules/mavlink/mavlink_log_handler.h +++ b/src/modules/mavlink/mavlink_log_handler.h @@ -58,6 +58,15 @@ public: void send(); void handle_message(const mavlink_message_t *msg); + /** + * Close any open log file. Must be called from the thread that handles the + * log messages (the receiver thread): on NuttX a FILE belongs to the task + * group that opened it, and fclose() from another task (e.g. the destructor + * running in 'mavlink stop') frees it while it stays linked in the owner's + * stream list, which then frees it again on exit (double free). + */ + void close_files(); + private: struct LogEntry { uint16_t id{0xffff}; diff --git a/src/modules/mavlink/mavlink_receiver.cpp b/src/modules/mavlink/mavlink_receiver.cpp index b9b363387ce..580090c8d61 100644 --- a/src/modules/mavlink/mavlink_receiver.cpp +++ b/src/modules/mavlink/mavlink_receiver.cpp @@ -4012,6 +4012,9 @@ MavlinkReceiver::run() _tune_publisher->publish_next_tune(t); } } + + // Release files opened by this thread while still in its task group (see close_files()) + _mavlink_log_handler.close_files(); } bool MavlinkReceiver::component_was_seen(int system_id, int component_id)