fix(safety): harden dual-core atomics, UDP peer filtering, TOCTOU, and CRC-8 UART frames

This commit is contained in:
bleack committed 2026-11-23 22:34:13 +01:00
1 parent 426ab7c7d6
commit c0bad51d3b
9 files changed
+232 -72

No files matched your search

+54 -18
View File
@@ -242,17 +242,31 @@ volatile uint8_t extEmbrayagePct = 0;
volatile int8_t extVolantPct = 0;
volatile uint32_t dernierPaquetRC_ms = 0;
static inline uint16_t crsfChannelLoad(uint8_t idx) {
return __atomic_load_n(&crsf_channels[idx], __ATOMIC_RELAXED);
}
static inline void crsfChannelStore(uint8_t idx, uint16_t val) {
__atomic_store_n(&crsf_channels[idx], val, __ATOMIC_RELAXED);
}
static inline bool estActuellementArme() {
return (sourceActive == SRC_EXT) ? extArme : estArme;
uint8_t src = __atomic_load_n(&sourceActive, __ATOMIC_ACQUIRE);
return (src == SRC_EXT)
? __atomic_load_n(&extArme, __ATOMIC_ACQUIRE)
: __atomic_load_n(&estArme, __ATOMIC_ACQUIRE);
}
static inline int8_t rapportActuel() {
return (sourceActive == SRC_EXT) ? extRapport : rapportEngage;
uint8_t src = __atomic_load_n(&sourceActive, __ATOMIC_ACQUIRE);
return (src == SRC_EXT)
? __atomic_load_n(&extRapport, __ATOMIC_ACQUIRE)
: __atomic_load_n(&rapportEngage, __ATOMIC_ACQUIRE);
}
static inline void assignerCanalCRSF(uint8_t numeroCh, uint16_t valeur11bits) {
if (numeroCh >= 1 && numeroCh <= 16 && numeroCh != CANAL_ARM) {
crsf_channels[numeroCh - 1] = valeur11bits;
crsfChannelStore(numeroCh - 1, valeur11bits);
}
}
@@ -266,9 +280,9 @@ void reinitialiserCanauxCRSF() {
if (htActif && (i == (CANAL_HT_PAN - 1) || i == (CANAL_HT_TILT - 1) || i == (CANAL_HT_ROLL - 1))) {
continue;
}
crsf_channels[i] = CRSF_VAL_MID;
crsfChannelStore((uint8_t)i, CRSF_VAL_MID);
}
crsf_channels[CANAL_ARM - 1] = CRSF_VAL_MIN; // Toujours désarmé par défaut
crsfChannelStore(CANAL_ARM - 1, CRSF_VAL_MIN); // Toujours désarmé par défaut
}
void reinitialiserConfigurationDefaut() {
@@ -293,7 +307,10 @@ void reinitialiserConfigurationDefaut() {
void sauvegarderConfiguration() {
ConfigG29 cfgActuelle;
// Geler brièvement le Cœur 1 pour garantir un instantané cohérent de configSysteme.
rp2040.idleOtherCore();
memcpy(&cfgActuelle, (const void*)&configSysteme, sizeof(ConfigG29));
rp2040.resumeOtherCore();
ConfigG29 cfgStockee;
EEPROM.get(0, cfgStockee);
@@ -553,13 +570,32 @@ void traiterLigneSerie(char* commande) {
return;
}
// Validation CRC-8 DVB-S2 (*XX) obligatoire sur toutes les trames $... (anti-corruption UART)
if (commande[0] == '$') {
char* star = strrchr(commande, '*');
if (!star || strlen(star + 1) != 2) {
return;
}
char* endPtr = nullptr;
unsigned long crcRecu = strtoul(star + 1, &endPtr, 16);
if (endPtr != star + 3) {
return;
}
size_t bodyLen = (size_t)(star - commande);
uint8_t crcCalc = calculerCrc8Crsf((const uint8_t*)commande, bodyLen);
if ((uint8_t)crcRecu != crcCalc) {
return;
}
*star = '\0';
}
// Injection Head-Tracking depuis SteamFrame FPV : $HT,<pan>,<tilt>,<roll> (172..1811)
if (strncmp(commande, "$HT,", 4) == 0) {
int pan = CRSF_VAL_MID, tilt = CRSF_VAL_MID, roll = CRSF_VAL_MID;
if (sscanf(commande + 4, "%d,%d,%d", &pan, &tilt, &roll) == 3) {
crsf_channels[CANAL_HT_PAN - 1] = (uint16_t)constrain(pan, CRSF_VAL_MIN, CRSF_VAL_MAX);
crsf_channels[CANAL_HT_TILT - 1] = (uint16_t)constrain(tilt, CRSF_VAL_MIN, CRSF_VAL_MAX);
crsf_channels[CANAL_HT_ROLL - 1] = (uint16_t)constrain(roll, CRSF_VAL_MIN, CRSF_VAL_MAX);
crsfChannelStore(CANAL_HT_PAN - 1, (uint16_t)constrain(pan, CRSF_VAL_MIN, CRSF_VAL_MAX));
crsfChannelStore(CANAL_HT_TILT - 1, (uint16_t)constrain(tilt, CRSF_VAL_MIN, CRSF_VAL_MAX));
crsfChannelStore(CANAL_HT_ROLL - 1, (uint16_t)constrain(roll, CRSF_VAL_MIN, CRSF_VAL_MAX));
dernierPaquetHT_ms = millis();
htActif = true;
}
@@ -715,7 +751,7 @@ void traiterLigneSerie(char* commande) {
assignerCanalCRSF(configSysteme.accel, vAccel);
assignerCanalCRSF(configSysteme.frein, vBrake);
assignerCanalCRSF(configSysteme.embrayage, vClutch);
crsf_channels[CANAL_ARM - 1] = extArme ? CRSF_VAL_MAX : CRSF_VAL_MIN;
crsfChannelStore(CANAL_ARM - 1, extArme ? CRSF_VAL_MAX : CRSF_VAL_MIN);
dernierPaquetRC_ms = millis();
if (!extRcActif) {
@@ -809,7 +845,7 @@ void __not_in_flash_func(envoyer_paquet_crsf)(bool neutre) {
uint16_t ch[16];
for (int i = 0; i < 16; i++) {
ch[i] = neutre ? CRSF_VAL_MID : crsf_channels[i];
ch[i] = neutre ? CRSF_VAL_MID : crsfChannelLoad((uint8_t)i);
}
if (neutre) ch[CANAL_ARM - 1] = CRSF_VAL_MIN; // Failsafe : toujours désarmé
@@ -1111,10 +1147,10 @@ void gererSortieCockpit() {
int steer = (sourceActive == SRC_EXT)
? (int)extVolantPct
: (int)constrain(((float)dernierVolantCentre / (float)dMax) * 100.0f, -100.0f, 100.0f);
uint16_t ch1 = failsafeActif ? CRSF_VAL_MID : crsf_channels[configSysteme.volant - 1];
uint16_t ch2 = failsafeActif ? CRSF_VAL_MID : crsf_channels[configSysteme.accel - 1];
uint16_t ch3 = failsafeActif ? CRSF_VAL_MID : crsf_channels[configSysteme.frein - 1];
uint16_t ch4 = failsafeActif ? CRSF_VAL_MID : crsf_channels[configSysteme.embrayage - 1];
uint16_t ch1 = failsafeActif ? CRSF_VAL_MID : crsfChannelLoad(configSysteme.volant - 1);
uint16_t ch2 = failsafeActif ? CRSF_VAL_MID : crsfChannelLoad(configSysteme.accel - 1);
uint16_t ch3 = failsafeActif ? CRSF_VAL_MID : crsfChannelLoad(configSysteme.frein - 1);
uint16_t ch4 = failsafeActif ? CRSF_VAL_MID : crsfChannelLoad(configSysteme.embrayage - 1);
const char* srcStr = (sourceActive == SRC_EXT) ? "EXT" : "G29";
char bufEtat[176];
snprintf(bufEtat, sizeof(bufEtat),
@@ -1140,9 +1176,9 @@ void gererSortieCockpit() {
void gererTimeoutHeadTracking() {
if (htActif && (millis() - dernierPaquetHT_ms > TIMEOUT_HT_MS)) {
htActif = false;
crsf_channels[CANAL_HT_PAN - 1] = CRSF_VAL_MID;
crsf_channels[CANAL_HT_TILT - 1] = CRSF_VAL_MID;
crsf_channels[CANAL_HT_ROLL - 1] = CRSF_VAL_MID;
crsfChannelStore(CANAL_HT_PAN - 1, CRSF_VAL_MID);
crsfChannelStore(CANAL_HT_TILT - 1, CRSF_VAL_MID);
crsfChannelStore(CANAL_HT_ROLL - 1, CRSF_VAL_MID);
}
}
@@ -1953,7 +1989,7 @@ void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t i
assignerCanalCRSF(configSysteme.accel, vAccel);
assignerCanalCRSF(configSysteme.frein, vFrein);
assignerCanalCRSF(configSysteme.embrayage, vEmbrayage);
crsf_channels[CANAL_ARM - 1] = estArme ? CRSF_VAL_MAX : CRSF_VAL_MIN;
crsfChannelStore(CANAL_ARM - 1, __atomic_load_n(&estArme, __ATOMIC_ACQUIRE) ? CRSF_VAL_MAX : CRSF_VAL_MIN);
// Horodatage APRÈS l'écriture des canaux : à la sortie d'un failsafe, le cœur 0
// ne peut pas renvoyer d'anciennes valeurs (d'avant la coupure) pendant un paquet.