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
This commit is contained in:
pkwr-air authored and GitHub committed 2026-11-17 10:38:54 +13:00
1 parent e95bcda092
commit 824dd4c75a
6 files changed
+53 -2

No files matched your search

@@ -47,6 +47,7 @@ __END_DECLS
#include <px4_platform_common/posix.h>
#include <errno.h>
#include <signal.h>
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;
}
@@ -39,6 +39,7 @@
#include <uORB/SubscriptionInterval.hpp>
#include <uORB/topics/parameter_update.h>
#include <uORB/topics/actuator_armed.h>
#include <drivers/drv_hrt.h>
#include <termios.h>
@@ -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};
@@ -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) {
@@ -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};
+3
View File
@@ -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)