Introduction
PX4 is an open-source autopilot for commercial and research Unmanned Aerial vehicles (UAVs). However, because many vehicles using PX4 perform in GPS-denied environments where real-time operations are critical, the system prioritizes performance. This creates a tradeoff between performance and security, where protections such as authentication and encryption are not implemented and deemed inefficient. Because of the architecture of PX4, gaps remain exploitable within the software.
Finding
Race Condition in MavlinkULog::stop() Function
ULog is the logging format that records flight controller data. UAVs can stream live data over a MAVLink connection to a host machine. Through rapidly toggling the logger on and off using start (MAV_CMD_LOGGING_START) and stop (MAV_CMD_LOGGING_STOP) commands, the system alternates between creating and destroying the MavlinkULog object.
An attacker that is network-adjacent, that is, on the same unauthenticated channel as the victim, can execute this exploit.
The global static pointer instance manages the logger subsystem. When MavLinkULog is destroyed, other threads like the receiver and work queue may access it. There is a race condition between two threads:
- The thread deleting the MavLinkULog object using
MavlinkUlog::stop() - A thread handling a polled update using the handle ack function
Classification
CWE-362: Concurrent Execution using Shared Resource with Improper Synchronization (’Race Condition’), CWE-667: Improper Locking
- Affected Versions: 1.17.0-rc2 and older
- Tested Version: v1.17.0-alpha1-695
Static Analysis
Cppcheck suggests a potential Use-After-Free (UAF):
A thread that is processing an acknowledgment can get a reference to instance right before the lock. If stop() is executed the instance object is destroyed. As the logger restarts, try_start creates a new _instance. When the thread unpauses, handle_ack() is executed on the freed pointer instance.
bool MavlinkULog::_init = false;
MavlinkULog *MavlinkULog::_instance = nullptr;
px4_sem_t MavlinkULog::_lock;void MavlinkULog::stop()
{
lock();
if (_instance) {
delete _instance;
_instance = nullptr;
}
unlock();
}void MavlinkULog::handle_ack(mavlink_logging_ack_t ack)
{
lock();
if (_instance) {
if (_wait_for_ack_sequence == ack.sequence) {
_ack_received = true;
publish_ack(ack.sequence);
}
}
unlock();
}And, in mavlink_receiver.cpp:
void
MavlinkReceiver::handle_message_logging_ack(mavlink_message_t *msg)
{
mavlink_logging_ack_t logging_ack;
mavlink_msg_logging_ack_decode(msg, &logging_ack);
MavlinkULog *ulog_streaming = _mavlink.get_ulog_streaming();
if (ulog_streaming) {
ulog_streaming->handle_ack(logging_ack);
}
}PoC
The Python script accomplishes two things:
- Opens a remote connection on UDP port 18570
- Floods the logger with STOP/START commands to trigger the race condition during the periodic acknowledgment
Impact
Flooding with LOGGING START and LOGGING STOP commands causes a 254.0% RX Loss value, a jump from 8.8K to ˜14.8K publication messages per second, and desynchronization within the MAVLink sequence tracking ability. The loss of communication between the drone and a transmitter causes severe issues such as modules (commander, navigator, and mavlink) skipping or losing important packets.
| Metric | Baseline | Attacked |
|---|---|---|
| RX Loss | 0.0% | 254.0% |
| Publication Rate (msgs/s) | ∼8.8K | ∼14.8K |
Additional commands sent from the operator are entered into the uORB work queue, which do not get executed until about a minute after the attacker has stopped the script. Stale acknowledgments additionally confuse an operator of the logger and connection status.
Throughout various runs, the RX loss fluctuated between 254.0% and undefined values such as NaN%. While spamming the logger caused state desynchronization, the UAF window itself was unattainable with ASan.