From 0d2a9b6282c9f2ddd792696a6575be3fe12935dd Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Tue, 23 Jul 2024 06:16:53 -0500 Subject: [PATCH 1/4] Fix de/compression buffer overflows in TAK packets (#4317) * Fix de/compression buffer overflows in TAK packets * Log message --- .semgrepignore | 2 +- .../compression/{unishox2.c => unishox2.cpp} | 19 +++- src/mesh/compression/unishox2.h | 76 ++++---------- src/modules/AtakPluginModule.cpp | 99 +++++++++++++------ src/modules/PositionModule.cpp | 8 +- 5 files changed, 108 insertions(+), 96 deletions(-) rename src/mesh/compression/{unishox2.c => unishox2.cpp} (98%) diff --git a/.semgrepignore b/.semgrepignore index 10fcb5f75..b4267ad23 100644 --- a/.semgrepignore +++ b/.semgrepignore @@ -1,2 +1,2 @@ .github/workflows/main_matrix.yml -src/mesh/compression/unishox2.c +src/mesh/compression/unishox2.cpp diff --git a/src/mesh/compression/unishox2.c b/src/mesh/compression/unishox2.cpp similarity index 98% rename from src/mesh/compression/unishox2.c rename to src/mesh/compression/unishox2.cpp index 99c62f659..fcb12a222 100644 --- a/src/mesh/compression/unishox2.c +++ b/src/mesh/compression/unishox2.cpp @@ -15,6 +15,7 @@ * * @author Arundale Ramanathan * + * Port for Particle (particle.io) / Aruino - Jonathan Greenblatt */ /** * @file unishox2.c @@ -36,6 +37,14 @@ /// uint8_t is unsigned char typedef unsigned char uint8_t; +const char *USX_FREQ_SEQ_DFLT[] = {"\": \"", "\": ", ""}; +const char *USX_FREQ_SEQ_XML[] = {"", "" \ - } -/// Frequently occurring sequences in XML content -#define USX_FREQ_SEQ_XML \ - (const char *[]) \ - { \ - "", "has_contact) { - auto length = unishox2_compress_simple(t->contact.callsign, strlen(t->contact.callsign), compressed.contact.callsign); + auto length = unishox2_compress_lines(t->contact.callsign, strlen(t->contact.callsign), compressed.contact.callsign, + sizeof(compressed.contact.callsign) - 1, USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Compression overflowed contact.callsign. Reverting to uncompressed packet\n"); + return; + } LOG_DEBUG("Compressed callsign: %d bytes\n", length); - - length = unishox2_compress_simple(t->contact.device_callsign, strlen(t->contact.device_callsign), - compressed.contact.device_callsign); + length = unishox2_compress_lines(t->contact.device_callsign, strlen(t->contact.device_callsign), + compressed.contact.device_callsign, sizeof(compressed.contact.device_callsign) - 1, + USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Compression overflowed contact.device_callsign. Reverting to uncompressed packet\n"); + return; + } LOG_DEBUG("Compressed device_callsign: %d bytes\n", length); } if (t->which_payload_variant == meshtastic_TAKPacket_chat_tag) { - auto length = unishox2_compress_simple(t->payload_variant.chat.message, strlen(t->payload_variant.chat.message), - compressed.payload_variant.chat.message); + auto length = unishox2_compress_lines(t->payload_variant.chat.message, strlen(t->payload_variant.chat.message), + compressed.payload_variant.chat.message, + sizeof(compressed.payload_variant.chat.message) - 1, USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Compression overflowed chat.message. Reverting to uncompressed packet\n"); + return; + } LOG_DEBUG("Compressed chat message: %d bytes\n", length); if (t->payload_variant.chat.has_to) { compressed.payload_variant.chat.has_to = true; - length = unishox2_compress_simple(t->payload_variant.chat.to, strlen(t->payload_variant.chat.to), - compressed.payload_variant.chat.to); + length = unishox2_compress_lines(t->payload_variant.chat.to, strlen(t->payload_variant.chat.to), + compressed.payload_variant.chat.to, + sizeof(compressed.payload_variant.chat.to) - 1, USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Compression overflowed chat.to. Reverting to uncompressed packet\n"); + return; + } LOG_DEBUG("Compressed chat to: %d bytes\n", length); } if (t->payload_variant.chat.has_to_callsign) { compressed.payload_variant.chat.has_to_callsign = true; - length = - unishox2_compress_simple(t->payload_variant.chat.to_callsign, strlen(t->payload_variant.chat.to_callsign), - compressed.payload_variant.chat.to_callsign); + length = unishox2_compress_lines(t->payload_variant.chat.to_callsign, strlen(t->payload_variant.chat.to_callsign), + compressed.payload_variant.chat.to_callsign, + sizeof(compressed.payload_variant.chat.to_callsign) - 1, USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Compression overflowed chat.to_callsign. Reverting to uncompressed packet\n"); + return; + } LOG_DEBUG("Compressed chat to_callsign: %d bytes\n", length); } } @@ -102,7 +122,7 @@ void AtakPluginModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtast } else { if (!t->is_compressed) { // Not compressed. Something is wrong - LOG_ERROR("Received uncompressed TAKPacket over radio!\n"); + LOG_WARN("Received uncompressed TAKPacket over radio! Skipping\n"); return; } @@ -112,32 +132,55 @@ void AtakPluginModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtast uncompressed.is_compressed = false; if (t->has_contact) { auto length = - unishox2_decompress_simple(t->contact.callsign, strlen(t->contact.callsign), uncompressed.contact.callsign); - + unishox2_decompress_lines(t->contact.callsign, strlen(t->contact.callsign), uncompressed.contact.callsign, + sizeof(uncompressed.contact.callsign) - 1, USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Decompression overflowed contact.callsign. Bailing out\n"); + return; + } LOG_DEBUG("Decompressed callsign: %d bytes\n", length); - length = unishox2_decompress_simple(t->contact.device_callsign, strlen(t->contact.device_callsign), - uncompressed.contact.device_callsign); - + length = unishox2_decompress_lines(t->contact.device_callsign, strlen(t->contact.device_callsign), + uncompressed.contact.device_callsign, + sizeof(uncompressed.contact.device_callsign) - 1, USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Decompression overflowed contact.device_callsign. Bailing out\n"); + return; + } LOG_DEBUG("Decompressed device_callsign: %d bytes\n", length); } if (uncompressed.which_payload_variant == meshtastic_TAKPacket_chat_tag) { - auto length = unishox2_decompress_simple(t->payload_variant.chat.message, strlen(t->payload_variant.chat.message), - uncompressed.payload_variant.chat.message); + auto length = unishox2_decompress_lines(t->payload_variant.chat.message, strlen(t->payload_variant.chat.message), + uncompressed.payload_variant.chat.message, + sizeof(uncompressed.payload_variant.chat.message) - 1, USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Decompression overflowed chat.message. Bailing out\n"); + return; + } LOG_DEBUG("Decompressed chat message: %d bytes\n", length); if (t->payload_variant.chat.has_to) { uncompressed.payload_variant.chat.has_to = true; - length = unishox2_decompress_simple(t->payload_variant.chat.to, strlen(t->payload_variant.chat.to), - uncompressed.payload_variant.chat.to); + length = unishox2_decompress_lines(t->payload_variant.chat.to, strlen(t->payload_variant.chat.to), + uncompressed.payload_variant.chat.to, + sizeof(uncompressed.payload_variant.chat.to) - 1, USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Decompression overflowed chat.to. Bailing out\n"); + return; + } LOG_DEBUG("Decompressed chat to: %d bytes\n", length); } if (t->payload_variant.chat.has_to_callsign) { uncompressed.payload_variant.chat.has_to_callsign = true; length = - unishox2_decompress_simple(t->payload_variant.chat.to_callsign, strlen(t->payload_variant.chat.to_callsign), - uncompressed.payload_variant.chat.to_callsign); + unishox2_decompress_lines(t->payload_variant.chat.to_callsign, strlen(t->payload_variant.chat.to_callsign), + uncompressed.payload_variant.chat.to_callsign, + sizeof(uncompressed.payload_variant.chat.to_callsign) - 1, USX_PSET_DFLT, NULL); + if (length < 0) { + LOG_WARN("Decompression overflowed chat.to_callsign. Bailing out\n"); + return; + } LOG_DEBUG("Decompressed chat to_callsign: %d bytes\n", length); } } @@ -148,4 +191,4 @@ void AtakPluginModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtast service.sendToPhone(decompressedCopy); } return; -} \ No newline at end of file +} diff --git a/src/modules/PositionModule.cpp b/src/modules/PositionModule.cpp index b3294a866..228929e96 100644 --- a/src/modules/PositionModule.cpp +++ b/src/modules/PositionModule.cpp @@ -11,12 +11,12 @@ #include "configuration.h" #include "gps/GeoCoord.h" #include "main.h" +#include "mesh/compression/unishox2.h" #include "meshtastic/atak.pb.h" #include "sleep.h" #include "target_specific.h" extern "C" { -#include "mesh/compression/unishox2.h" #include } @@ -255,10 +255,12 @@ meshtastic_MeshPacket *PositionModule::allocAtakPli() .course = static_cast(localPosition.ground_track), }}}; - auto length = unishox2_compress_simple(owner.long_name, strlen(owner.long_name), takPacket.contact.device_callsign); + auto length = unishox2_compress_lines(owner.long_name, strlen(owner.long_name), takPacket.contact.device_callsign, + sizeof(takPacket.contact.device_callsign) - 1, USX_PSET_DFLT, NULL); LOG_DEBUG("Uncompressed device_callsign '%s' - %d bytes\n", owner.long_name, strlen(owner.long_name)); LOG_DEBUG("Compressed device_callsign '%s' - %d bytes\n", takPacket.contact.device_callsign, length); - length = unishox2_compress_simple(owner.long_name, strlen(owner.long_name), takPacket.contact.callsign); + length = unishox2_compress_lines(owner.long_name, strlen(owner.long_name), takPacket.contact.callsign, + sizeof(takPacket.contact.callsign) - 1, USX_PSET_DFLT, NULL); mp->decoded.payload.size = pb_encode_to_bytes(mp->decoded.payload.bytes, sizeof(mp->decoded.payload.bytes), &meshtastic_TAKPacket_msg, &takPacket); return mp; From 316928deb079d41d0d5806a1cf8bbe80ec0f85cb Mon Sep 17 00:00:00 2001 From: Tom Fifield Date: Tue, 23 Jul 2024 19:18:27 +0800 Subject: [PATCH 2/4] Cleanup GPS, add UC6580 autodetect (#4319) * Cleanup GPS, add UC6580 autodetect Our GPS code autodetects devices by default. Previously UC6580 was statically assigned, and had its own baudrate configuration inside the GPS code. This change adds autodetect functionality for the UC6580 and moves any 'special' GPS baud rate requirements for a variant out into the variant configuration. Thereby cleaning up core GPS code a little, saving the whales, and curing global warming. New Functionality: * If GPS_BAUDRATE is defined in variant.h, GPS autodetection will try that baudrate first. * UC6580 GPS chips are now automatically detected * Only run speedSelect skip the first time * Cleanup GPS, add UC6580 autodetect Our GPS code autodetects devices by default. Previously UC6580 was statically assigned, and had its own baudrate configuration inside the GPS code. This change adds autodetect functionality for the UC6580 and moves any 'special' GPS baud rate requirements for a variant out into the variant configuration. Thereby cleaning up core GPS code a little, saving the whales, and curing global warming. New Functionality: * If GPS_BAUDRATE is defined in variant.h, GPS autodetection will try that baudrate first. * UC6580 GPS chips are now automatically detected * Cleanup GPS, add UC6580 autodetect Our GPS code autodetects devices by default. Previously UC6580 was statically assigned, and had its own baudrate configuration inside the GPS code. This change adds autodetect functionality for the UC6580 and moves any 'special' GPS baud rate requirements for a variant out into the variant configuration. Thereby cleaning up core GPS code a little, saving the whales, and curing global warming. New Functionality: * If GPS_BAUDRATE is defined in variant.h, GPS autodetection will try that baudrate first. * UC6580 GPS chips are now automatically detected * Remove Airoha baud rate code It's no longer needed. --- src/configuration.h | 10 +++--- src/gps/GPS.cpp | 34 +++++++++---------- variants/heltec_wireless_tracker/variant.h | 1 + .../heltec_wireless_tracker_V1_0/variant.h | 1 + variants/tracksenger/internal/variant.h | 3 +- variants/tracksenger/lcd/variant.h | 3 +- variants/tracksenger/oled/variant.h | 3 +- 7 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/configuration.h b/src/configuration.h index aad4ac457..6351c35b1 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -171,10 +171,6 @@ along with this program. If not, see . // ----------------------------------------------------------------------------- // GPS // ----------------------------------------------------------------------------- -#ifndef GPS_BAUDRATE -#define GPS_BAUDRATE 9600 -#endif - #ifndef GPS_THREAD_INTERVAL #define GPS_THREAD_INTERVAL 200 #endif @@ -185,6 +181,10 @@ along with this program. If not, see . /* Step #1: offer chance for variant-specific defines */ #include "variant.h" +#ifndef GPS_BAUDRATE +#define GPS_BAUDRATE 9600 +#endif + /* Step #2: follow with defines common to the architecture; also enable HAS_ option not specifically disabled by variant.h */ #include "architecture.h" @@ -313,4 +313,4 @@ along with this program. If not, see . #endif #include "DebugConfiguration.h" -#include "RF95Configuration.h" \ No newline at end of file +#include "RF95Configuration.h" diff --git a/src/gps/GPS.cpp b/src/gps/GPS.cpp index 9628784d6..f04b45622 100644 --- a/src/gps/GPS.cpp +++ b/src/gps/GPS.cpp @@ -400,14 +400,14 @@ bool GPS::setup() int msglen = 0; if (!didSerialInit) { -#ifdef GNSS_Airoha // change by WayenWeng - if (tx_gpio && gnssModel == GNSS_MODEL_UNKNOWN) { - probe(GPS_BAUDRATE); - LOG_INFO("GPS setting to %d.\n", GPS_BAUDRATE); - } -#else -#if !defined(GPS_UC6580) + if (tx_gpio && gnssModel == GNSS_MODEL_UNKNOWN) { + + // if GPS_BAUDRATE is specified in variant (i.e. not 9600), skip to the specified rate. + if (speedSelect == 0 && GPS_BAUDRATE != serialSpeeds[speedSelect]) { + speedSelect = std::find(serialSpeeds, std::end(serialSpeeds), GPS_BAUDRATE) - serialSpeeds; + } + LOG_DEBUG("Probing for GPS at %d \n", serialSpeeds[speedSelect]); gnssModel = probe(serialSpeeds[speedSelect]); if (gnssModel == GNSS_MODEL_UNKNOWN) { @@ -423,9 +423,6 @@ bool GPS::setup() } else { gnssModel = GNSS_MODEL_UNKNOWN; } -#else - gnssModel = GNSS_MODEL_UC6580; -#endif if (gnssModel == GNSS_MODEL_MTK) { /* @@ -777,7 +774,6 @@ bool GPS::setup() LOG_INFO("GNSS module configuration saved!\n"); } } -#endif // !GNSS_Airoha didSerialInit = true; } @@ -1191,6 +1187,15 @@ GnssModel_t GPS::probe(int serialSpeed) _serial_gps->write("$PCAS03,0,0,0,0,0,0,0,0,0,0,,,0,0*02\r\n"); delay(20); + // get version information from Unicore UFirebirdII Series + // Works for: UC6580, UM620, UM621, UM670A, UM680A, or UM681A + _serial_gps->write("$PDTINFO\r\n"); + delay(750); + if (getACK("UC6580", 500) == GNSS_RESPONSE_OK) { + LOG_INFO("UC6580 detected, using UC6580 Module\n"); + return GNSS_MODEL_UC6580; + } + // Get version information clearBuffer(); _serial_gps->write("$PCAS06,1*1A\r\n"); @@ -1398,13 +1403,6 @@ GPS *GPS::createGps() #else _serial_gps->begin(GPS_BAUDRATE); #endif - - /* - * T-Beam-S3-Core will be preset to use gps Probe here, and other boards will not be changed first - */ -#if defined(GPS_UC6580) - _serial_gps->updateBaudRate(115200); -#endif } return new_gps; } diff --git a/variants/heltec_wireless_tracker/variant.h b/variants/heltec_wireless_tracker/variant.h index f0ee0631d..685c9f079 100644 --- a/variants/heltec_wireless_tracker/variant.h +++ b/variants/heltec_wireless_tracker/variant.h @@ -52,6 +52,7 @@ #define GPS_RESET_MODE LOW #define GPS_UC6580 +#define GPS_BAUDRATE 115200 #define USE_SX1262 #define LORA_DIO0 -1 // a No connect on the SX1262 module diff --git a/variants/heltec_wireless_tracker_V1_0/variant.h b/variants/heltec_wireless_tracker_V1_0/variant.h index 1b4751a57..6b038dc28 100644 --- a/variants/heltec_wireless_tracker_V1_0/variant.h +++ b/variants/heltec_wireless_tracker_V1_0/variant.h @@ -49,6 +49,7 @@ #define GPS_RESET_MODE LOW #define GPS_UC6580 +#define GPS_BAUDRATE 11520 #define USE_SX1262 #define LORA_DIO0 -1 // a No connect on the SX1262 module diff --git a/variants/tracksenger/internal/variant.h b/variants/tracksenger/internal/variant.h index e63cecd7b..929c38793 100644 --- a/variants/tracksenger/internal/variant.h +++ b/variants/tracksenger/internal/variant.h @@ -48,6 +48,7 @@ #define GPS_RESET_MODE LOW #define GPS_UC6580 +#define GPS_BAUDRATE 115200 #define USE_SX1262 #define LORA_DIO0 -1 // a No connect on the SX1262 module @@ -87,4 +88,4 @@ { \ 26, 37, 17, 16, 15, 7 \ } -// #end keyboard \ No newline at end of file +// #end keyboard diff --git a/variants/tracksenger/lcd/variant.h b/variants/tracksenger/lcd/variant.h index 0f3423d52..3f952361b 100644 --- a/variants/tracksenger/lcd/variant.h +++ b/variants/tracksenger/lcd/variant.h @@ -72,6 +72,7 @@ #define GPS_RESET_MODE LOW #define GPS_UC6580 +#define GPS_BAUDRATE 115200 #define USE_SX1262 #define LORA_DIO0 -1 // a No connect on the SX1262 module @@ -111,4 +112,4 @@ { \ 26, 37, 17, 16, 15, 7 \ } -// #end keyboard \ No newline at end of file +// #end keyboard diff --git a/variants/tracksenger/oled/variant.h b/variants/tracksenger/oled/variant.h index d6bacf139..99f12bd23 100644 --- a/variants/tracksenger/oled/variant.h +++ b/variants/tracksenger/oled/variant.h @@ -50,6 +50,7 @@ #define GPS_RESET_MODE LOW #define GPS_UC6580 +#define GPS_BAUDRATE 115200 #define USE_SX1262 #define LORA_DIO0 -1 // a No connect on the SX1262 module @@ -89,4 +90,4 @@ { \ 26, 37, 17, 16, 15, 7 \ } -// #end keyboard \ No newline at end of file +// #end keyboard From 1d3ac57943b59d17c07468aed139ca84ac256240 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Tue, 23 Jul 2024 08:35:01 -0500 Subject: [PATCH 3/4] Fix type (#4323) --- variants/heltec_wireless_tracker_V1_0/variant.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/variants/heltec_wireless_tracker_V1_0/variant.h b/variants/heltec_wireless_tracker_V1_0/variant.h index 6b038dc28..23987adf0 100644 --- a/variants/heltec_wireless_tracker_V1_0/variant.h +++ b/variants/heltec_wireless_tracker_V1_0/variant.h @@ -49,7 +49,7 @@ #define GPS_RESET_MODE LOW #define GPS_UC6580 -#define GPS_BAUDRATE 11520 +#define GPS_BAUDRATE 115200 #define USE_SX1262 #define LORA_DIO0 -1 // a No connect on the SX1262 module From e27375d331346f63da2662f3e0f92472e641dd86 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 23 Jul 2024 17:13:58 +0300 Subject: [PATCH 4/4] Set PIN_3V3_EN to HIGH (nrf52_promicro_diy variants) (#4321) * Update variant.cpp PIN 3v3 to HIGH * Update variant.cpp * Trunk fmt --- variants/diy/nrf52_promicro_diy_tcxo/variant.cpp | 9 ++++++++- variants/diy/nrf52_promicro_diy_xtal/variant.cpp | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/variants/diy/nrf52_promicro_diy_tcxo/variant.cpp b/variants/diy/nrf52_promicro_diy_tcxo/variant.cpp index 4030122e5..5869ed1d4 100644 --- a/variants/diy/nrf52_promicro_diy_tcxo/variant.cpp +++ b/variants/diy/nrf52_promicro_diy_tcxo/variant.cpp @@ -28,4 +28,11 @@ const uint32_t g_ADigitalPinMap[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // P1 - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}; \ No newline at end of file + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}; + +void initVariant() +{ + // 3V3 Power Rail + pinMode(PIN_3V3_EN, OUTPUT); + digitalWrite(PIN_3V3_EN, HIGH); +} diff --git a/variants/diy/nrf52_promicro_diy_xtal/variant.cpp b/variants/diy/nrf52_promicro_diy_xtal/variant.cpp index 4030122e5..5869ed1d4 100644 --- a/variants/diy/nrf52_promicro_diy_xtal/variant.cpp +++ b/variants/diy/nrf52_promicro_diy_xtal/variant.cpp @@ -28,4 +28,11 @@ const uint32_t g_ADigitalPinMap[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // P1 - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}; \ No newline at end of file + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}; + +void initVariant() +{ + // 3V3 Power Rail + pinMode(PIN_3V3_EN, OUTPUT); + digitalWrite(PIN_3V3_EN, HIGH); +}