Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenYGE - fix: address possible buffer underflow condition, add 2nd paranoid frame length validation … #96

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/main/sensors/esc_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,7 @@ enum {
static timeMs_t oygeRampTimer = 0;
static uint32_t oygeFrameTimestamp = 0;
static uint16_t oygeFramePeriod = OPENYGE_FRAME_PERIOD_INITIAL;
static volatile uint8_t oygeFrameLength = OPENYGE_FRAME_MIN_LENGTH;
static volatile uint8_t oygeFrameLength = 0;


static uint16_t oygeCalculateCRC16_CCITT(const uint8_t *ptr, size_t len)
Expand Down Expand Up @@ -1622,19 +1622,21 @@ static FAST_CODE void oygeDataReceive(uint16_t c, void *data)
else if (readBytes == 4) {
// frame length
// protect against buffer overflow
if (c > TELEMETRY_BUFFER_SIZE)
if (c < OPENYGE_FRAME_MIN_LENGTH || c > TELEMETRY_BUFFER_SIZE) {
oygeFrameSyncError();
else
}
else {
oygeFrameLength = c;
syncCount++;
oygeFrameLength = c;
}
}
}
}

static void oygeStartTelemetryFrame(timeMs_t currentTimeMs)
{
readBytes = 0;

oygeFrameLength = OPENYGE_FRAME_MIN_LENGTH;
oygeFrameTimestamp = currentTimeMs;
}

Expand All @@ -1644,9 +1646,15 @@ static uint8_t oygeDecodeTelemetryFrame(void)
if (readBytes < oygeFrameLength)
return OPENYGE_FRAME_PENDING;

// paranoid length check
uint8_t len = buffer[3];
if (len < OPENYGE_FRAME_MIN_LENGTH || len > TELEMETRY_BUFFER_SIZE) {
totalCrcErrorCount++;
return OPENYGE_FRAME_FAILED;
}
// verify CRC16 checksum
uint16_t crc = buffer[oygeFrameLength - 1] << 8 | buffer[oygeFrameLength - 2];
if (oygeCalculateCRC16_CCITT(buffer, oygeFrameLength - 2) != crc) {
uint16_t crc = buffer[len - 1] << 8 | buffer[len - 2];
if (oygeCalculateCRC16_CCITT(buffer, len - 2) != crc) {
totalCrcErrorCount++;
return OPENYGE_FRAME_FAILED;
}
Expand Down