From 59c899b0c10c480d35e1ca4fbd9c29dc8fe58b8e Mon Sep 17 00:00:00 2001 From: Ray Morris Date: Sat, 5 Dec 2026 22:53:28 -0600 Subject: [PATCH] Reject servo mixer inputs from RC channels 19-34 on boards without them servoMixer() fills the RC channel 19-34 inputs only when USE_34CHANNELS is defined, but smix and both MSP setters accepted them on every board. On a 512 KB board a rule using one of them drove its servo from uninitialized stack memory. The MSP setters also accepted any input number at all, reading past the end of the input array. smix and both MSP setters now refuse an input the board cannot fill. A rule already saved with one is skipped, and blocks arming as an invalid setting; CLI status says which problem it is. --- src/main/fc/cli.c | 5 ++++- src/main/fc/config.c | 2 +- src/main/fc/fc_msp.c | 18 ++++++++++++++---- src/main/flight/servos.c | 28 ++++++++++++++++++++++++++++ src/main/flight/servos.h | 2 ++ 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/main/fc/cli.c b/src/main/fc/cli.c index b74fd8cfb13..758abb12731 100644 --- a/src/main/fc/cli.c +++ b/src/main/fc/cli.c @@ -2438,7 +2438,7 @@ static void cliServoMix(char *cmdline) if ( i >= 0 && i < MAX_SERVO_RULES && args[TARGET] >= 0 && args[TARGET] < MAX_SUPPORTED_SERVOS && - args[INPUT] >= 0 && args[INPUT] < INPUT_SOURCE_COUNT && + servoMixerInputSupported(args[INPUT]) && args[RATE] >= -1000 && args[RATE] <= 1000 && args[SPEED] >= 0 && args[SPEED] <= MAX_SERVO_SPEED && args[CONDITION] >= -1 && args[CONDITION] < MAX_LOGIC_CONDITIONS @@ -4303,6 +4303,9 @@ static void cliStatus(char *cmdline) settingGetName(settingGet(invalidIndex), buf); cliPrintErrorLinef("Invalid setting: %s", buf); } + if (!servoMixerRulesValid()) { + cliPrintErrorLinef("Servo mixer rule uses an input this board does not support"); + } } #if defined(USE_OSD) diff --git a/src/main/fc/config.c b/src/main/fc/config.c index 708ec5510c0..14b0e527258 100755 --- a/src/main/fc/config.c +++ b/src/main/fc/config.c @@ -309,7 +309,7 @@ void validateAndFixConfig(void) } #endif - if (settingsValidate(NULL)) { + if (settingsValidate(NULL) && servoMixerRulesValid()) { DISABLE_ARMING_FLAG(ARMING_DISABLED_INVALID_SETTING); } else { ENABLE_ARMING_FLAG(ARMING_DISABLED_INVALID_SETTING); diff --git a/src/main/fc/fc_msp.c b/src/main/fc/fc_msp.c index 3cd15c4a991..fbfe2679e82 100644 --- a/src/main/fc/fc_msp.c +++ b/src/main/fc/fc_msp.c @@ -2612,8 +2612,13 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src) case MSP_SET_SERVO_MIX_RULE: sbufReadU8Safe(&tmp_u8, src); if ((dataSize >= 9) && (tmp_u8 < MAX_SERVO_RULES)) { - customServoMixersMutable(tmp_u8)->targetChannel = sbufReadU8(src); - customServoMixersMutable(tmp_u8)->inputSource = sbufReadU8(src); + const uint8_t targetChannel = sbufReadU8(src); + const uint8_t inputSource = sbufReadU8(src); + if (!servoMixerInputSupported(inputSource)) { + return MSP_RESULT_ERROR; + } + customServoMixersMutable(tmp_u8)->targetChannel = targetChannel; + customServoMixersMutable(tmp_u8)->inputSource = inputSource; customServoMixersMutable(tmp_u8)->rate = sbufReadU16(src); customServoMixersMutable(tmp_u8)->speed = sbufReadU8(src); sbufReadU16(src); //Read 2bytes for min/max and ignore it @@ -2626,8 +2631,13 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src) case MSP2_INAV_SET_SERVO_MIXER: sbufReadU8Safe(&tmp_u8, src); if ((dataSize >= 7) && (tmp_u8 < MAX_SERVO_RULES)) { - customServoMixersMutable(tmp_u8)->targetChannel = sbufReadU8(src); - customServoMixersMutable(tmp_u8)->inputSource = sbufReadU8(src); + const uint8_t targetChannel = sbufReadU8(src); + const uint8_t inputSource = sbufReadU8(src); + if (!servoMixerInputSupported(inputSource)) { + return MSP_RESULT_ERROR; + } + customServoMixersMutable(tmp_u8)->targetChannel = targetChannel; + customServoMixersMutable(tmp_u8)->inputSource = inputSource; customServoMixersMutable(tmp_u8)->rate = sbufReadU16(src); customServoMixersMutable(tmp_u8)->speed = sbufReadU8(src); #ifdef USE_PROGRAMMING_FRAMEWORK diff --git a/src/main/flight/servos.c b/src/main/flight/servos.c index ef3a25693c1..7fbdea22172 100755 --- a/src/main/flight/servos.c +++ b/src/main/flight/servos.c @@ -220,6 +220,30 @@ int getServoCount(void) } } +bool servoMixerInputSupported(int inputSource) +{ +#ifndef USE_34CHANNELS + // servoMixer() fills these inputs only on 34-channel targets + if (inputSource >= INPUT_RC_CH19 && inputSource <= INPUT_RC_CH34) { + return false; + } +#endif + return inputSource >= 0 && inputSource < INPUT_SOURCE_COUNT; +} + +bool servoMixerRulesValid(void) +{ + for (int profile = 0; profile < MAX_MIXER_PROFILE_COUNT; profile++) { + const servoMixer_t *rules = mixerServoMixersByIndex(profile); + for (int i = 0; i < MAX_SERVO_RULES && rules[i].rate != 0; i++) { + if (!servoMixerInputSupported(rules[i].inputSource)) { + return false; + } + } + } + return true; +} + void loadCustomServoMixer(void) { @@ -256,6 +280,10 @@ void loadCustomServoMixer(void) if (customServoMixers(i)->rate == 0){ break; } + // Arming is blocked while such a rule exists; see servoMixerRulesValid() + if (!servoMixerInputSupported(customServoMixers(i)->inputSource)) { + continue; + } currentServoMixer[servoRuleCount] = *customServoMixers(i); servoSpeedLimitFilter[servoRuleCount].state = 0; servoRuleCount++; diff --git a/src/main/flight/servos.h b/src/main/flight/servos.h index 6462defc6bf..f9547104479 100644 --- a/src/main/flight/servos.h +++ b/src/main/flight/servos.h @@ -204,6 +204,8 @@ bool isServoOutputEnabled(void); void setServoOutputEnabled(bool flag); bool isMixerUsingServos(void); void writeServos(void); +bool servoMixerInputSupported(int inputSource); +bool servoMixerRulesValid(void); void loadCustomServoMixer(void); void servoMixerSetCarryoverOnNextLoad(bool enabled); #ifdef USE_AUTO_TRANSITION