diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 60565e77..a23e3828 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1,6 +1,7 @@ #include "MyMesh.h" #include #include +#include /* ------------------------------ Config -------------------------------- */ @@ -404,7 +405,6 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr } MESH_DEBUG_PRINTLN("Login success!"); client->last_timestamp = sender_timestamp; - client->last_activity = getRTCClock()->getCurrentTime(); client->permissions &= ~0x03; client->permissions |= perms; acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): admin only, so after the role @@ -414,6 +414,7 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); } } + client->last_activity = getRTCClock()->getCurrentTime(); if (is_flood) { client->out_path_len = OUT_PATH_UNKNOWN; // need to rediscover out_path @@ -1252,12 +1253,12 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, uint32_t timestamp; memcpy(×tamp, data, 4); - if (timestamp > client->last_timestamp) { // prevent replay attacks + if (timestamp > client->last_req_timestamp) { // prevent replay attacks acl.persistTimestamp(client, timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting int reply_len = handleRequest(client, timestamp, &data[4], len - 4); if (reply_len == 0) return; // invalid command - client->last_timestamp = timestamp; + client->last_req_timestamp = timestamp; client->last_activity = getRTCClock()->getCurrentTime(); if (packet->isRouteFlood()) { @@ -1286,6 +1287,11 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, if (!(flags == TXT_TYPE_PLAIN || flags == TXT_TYPE_CLI_DATA || flags == TXT_TYPE_CLI_COMMAND)) { MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported text type received: flags=%02x", (uint32_t)flags); + } else if (!mesh::isValidUtf8TextAndPadding(&data[5], len - 5)) { + MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid text payload! (id prefix: %02X:%02X)", client->id.pub_key[0], client->id.pub_key[1]); + _err_flags |= ERR_EVENT_DIAG1; // report in next Get Stats + } else if (_reply_echo.matches(data, 5 + strnlen((const char *)&data[5], len - 5))) { + MESH_DEBUG_PRINTLN("onPeerDataRecv: text is our own CLI reply"); } else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks bool is_retry = (sender_timestamp == client->last_timestamp); acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting @@ -1329,6 +1335,7 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, } memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique temp[4] = (TXT_TYPE_CLI_DATA << 2); // NOTE: legacy was: TXT_TYPE_PLAIN + _reply_echo.note(temp, 5 + text_len); auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, secret, temp, 5 + text_len); if (reply) { diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index a3306f9e..9dddc80a 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -28,6 +28,7 @@ #include #include #include // CUSTOM (TeTeHacko) +#include #include #include #include @@ -197,6 +198,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { NodePrefs _prefs; ClientACL acl; LoginGuard login_guard; // CUSTOM (TeTeHacko) + ReplyEchoGuard _reply_echo; CommonCLI _cli; uint8_t reply_data[MAX_PACKET_PAYLOAD]; uint8_t reply_path[MAX_PATH_SIZE]; diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index 1af3ec5d..0ad73d21 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -1,4 +1,5 @@ #include "MyMesh.h" +#include #include #define REPLY_DELAY_MILLIS 1500 @@ -390,7 +391,6 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m client->extra.room.pending_ack = 0; client->extra.room.push_failures = 0; - client->last_activity = getRTCClock()->getCurrentTime(); client->permissions &= ~0x03; client->permissions |= perm; acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): admin only, so after the role @@ -398,6 +398,7 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); } + client->last_activity = getRTCClock()->getCurrentTime(); if (packet->isRouteFlood()) { client->out_path_len = OUT_PATH_UNKNOWN; // need to rediscover out_path @@ -469,6 +470,11 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, if (!(flags == TXT_TYPE_PLAIN || flags == TXT_TYPE_CLI_DATA || flags == TXT_TYPE_CLI_COMMAND)) { MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported command flags received: flags=%02x", (uint32_t)flags); + } else if (!mesh::isValidUtf8TextAndPadding(&data[5], len - 5)) { + MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid text payload! (id prefix: %02X:%02X)", client->id.pub_key[0], client->id.pub_key[1]); + _err_flags |= ERR_EVENT_DIAG1; // report in next Get Stats + } else if (_reply_echo.matches(data, 5 + strnlen((const char *)&data[5], len - 5))) { + MESH_DEBUG_PRINTLN("onPeerDataRecv: text is our own CLI reply"); } else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks, but send Acks for retries bool is_retry = (sender_timestamp == client->last_timestamp); acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting @@ -544,6 +550,7 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, now++; } memcpy(temp, &now, 4); // mostly an extra blob to help make packet_hash unique + _reply_echo.note(temp, 5 + text_len); // calc expected ACK reply // mesh::Utils::sha256((uint8_t *)&expected_ack_crc, 4, temp, 5 + text_len, self_id.pub_key, @@ -564,11 +571,11 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, } else if (type == PAYLOAD_TYPE_REQ && len >= 5) { uint32_t sender_timestamp; memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong) - if (sender_timestamp < client->last_timestamp) { // prevent replay attacks + if (sender_timestamp < client->last_req_timestamp) { // prevent replay attacks MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected"); } else { acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting - client->last_timestamp = sender_timestamp; + client->last_req_timestamp = sender_timestamp; uint32_t now = getRTCClock()->getCurrentTime(); client->last_activity = now; // <-- THIS will keep client connection alive diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index b1c01cec..1b3015fa 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -21,6 +21,7 @@ #include #include #include // CUSTOM (TeTeHacko) +#include #include #include #include @@ -100,6 +101,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { RegionMap region_map, temp_map; ClientACL acl; LoginGuard login_guard; // CUSTOM (TeTeHacko) + ReplyEchoGuard _reply_echo; CommonCLI _cli; unsigned long dirty_contacts_expiry; uint8_t reply_data[MAX_PACKET_PAYLOAD]; diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index 9b02bb44..7f6111d4 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -1,6 +1,7 @@ #include "SensorMesh.h" #include #include +#include /* ------------------------------ Config -------------------------------- */ @@ -438,13 +439,13 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* } MESH_DEBUG_PRINTLN("Login success!"); client->last_timestamp = sender_timestamp; - client->last_activity = getRTCClock()->getCurrentTime(); client->permissions |= PERM_ACL_ADMIN; acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): admin only, so after the role memcpy(client->shared_secret, secret, PUB_KEY_SIZE); dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); } + client->last_activity = getRTCClock()->getCurrentTime(); if (is_flood) { client->out_path_len = OUT_PATH_UNKNOWN; // need to rediscover out_path @@ -698,7 +699,12 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong) uint8_t flags = (data[4] >> 2); // message attempt number, and other flags - if (sender_timestamp > from->last_timestamp) { // prevent replay attacks + if (!mesh::isValidUtf8TextAndPadding(&data[5], len - 5)) { + MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid text payload! (id prefix: %02X:%02X)", from->id.pub_key[0], from->id.pub_key[1]); + _err_flags |= ERR_EVENT_DIAG1; // report in next Get Stats + } else if (_reply_echo.matches(data, 5 + strnlen((const char *)&data[5], len - 5))) { + MESH_DEBUG_PRINTLN("onPeerDataRecv: text is our own CLI reply"); + } else if (sender_timestamp > from->last_timestamp) { // prevent replay attacks acl.persistTimestamp(from, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting if (flags == TXT_TYPE_PLAIN) { bool handled = handleIncomingMsg(*from, sender_timestamp, &data[5], flags, len - 5); @@ -737,6 +743,7 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i } memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique temp[4] = (TXT_TYPE_CLI_DATA << 2); + _reply_echo.note(temp, 5 + text_len); auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, from->id, secret, temp, 5 + text_len); if (reply) { diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index b91633d2..91e97753 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -23,6 +23,7 @@ #include #include #include // CUSTOM (TeTeHacko) +#include #include #include #include @@ -149,6 +150,7 @@ private: NodePrefs _prefs; ClientACL acl; LoginGuard login_guard; // CUSTOM (TeTeHacko) + ReplyEchoGuard _reply_echo; CommonCLI _cli; uint8_t reply_data[MAX_PACKET_PAYLOAD]; unsigned long dirty_contacts_expiry; diff --git a/src/Dispatcher.h b/src/Dispatcher.h index c064923a..25625ec1 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -121,6 +121,7 @@ typedef uint32_t DispatcherAction; #ifndef MESH_TX_RETRIES #define MESH_TX_RETRIES 3 #endif +#define ERR_EVENT_DIAG1 (1 << 8) // role-specific error flag /** * \brief The low-level task that manages detecting incoming Packets, and the queueing diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index ce7cc3cb..6e89f474 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -1,5 +1,6 @@ #include #include +#include #ifndef SERVER_RESPONSE_DELAY #define SERVER_RESPONSE_DELAY 300 @@ -247,7 +248,10 @@ void BaseChatMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender // len can be > original length, but 'text' will be padded with zeroes data[len] = 0; // need to make a C string again, with null terminator - if (flags == TXT_TYPE_PLAIN) { + size_t text_ofs = (flags == TXT_TYPE_SIGNED_PLAIN) ? 9 : 5; // signed text starts after 4-byte sender prefix + if (len < text_ofs || !mesh::isValidUtf8TextAndPadding(&data[text_ofs], len - text_ofs)) { + MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid text payload!"); + } else if (flags == TXT_TYPE_PLAIN) { from.lastmod = getRTCClock()->getCurrentTime(); // update last heard time onMessageRecv(from, packet, sender_timestamp, (const char *) &data[5]); // let UI know diff --git a/src/helpers/ClientACL.cpp b/src/helpers/ClientACL.cpp index 7cdc007e..8a98af21 100644 --- a/src/helpers/ClientACL.cpp +++ b/src/helpers/ClientACL.cpp @@ -115,7 +115,7 @@ void ClientACL::loadTimestamps() { } } for (int i = 0; i < num_clients; i++) { - clients[i].last_timestamp = clients[i].saved_timestamp; + clients[i].last_timestamp = clients[i].last_req_timestamp = clients[i].saved_timestamp; } } @@ -242,7 +242,7 @@ ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms) { c->out_path_len = OUT_PATH_UNKNOWN; for (int i = 0; i < _num_ts_orphans; i++) { // known before: its replay floor comes back if (memcmp(_ts_orphans[i].prefix, id.pub_key, ACL_TS_PREFIX) == 0) { - c->saved_timestamp = c->last_timestamp = _ts_orphans[i].ts; + c->saved_timestamp = c->last_timestamp = c->last_req_timestamp = _ts_orphans[i].ts; _num_ts_orphans--; memmove(&_ts_orphans[i], &_ts_orphans[i + 1], (_num_ts_orphans - i) * sizeof(TsOrphan)); break; diff --git a/src/helpers/ClientACL.h b/src/helpers/ClientACL.h index fb9a66f6..5d7eaaad 100644 --- a/src/helpers/ClientACL.h +++ b/src/helpers/ClientACL.h @@ -20,6 +20,7 @@ struct ClientInfo { uint8_t shared_secret[PUB_KEY_SIZE]; uint32_t last_timestamp; // by THEIR clock (restored from /s_acl_ts* on boot) uint32_t saved_timestamp; // last_timestamp as last written to /s_acl_ts* + uint32_t last_req_timestamp; // REQ only (upstream split); restored from saved_timestamp too uint32_t last_activity; // by OUR clock (transient) union { struct { diff --git a/src/helpers/ReplyEchoGuard.h b/src/helpers/ReplyEchoGuard.h new file mode 100644 index 00000000..004b7361 --- /dev/null +++ b/src/helpers/ReplyEchoGuard.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +/** + * \brief Remembers the last CLI replies sent, as a hash of each reply's plaintext (timestamp, flags, text), + * so the same bytes coming back as a command (hash bytes swapped, same shared secret) are refused. + */ +class ReplyEchoGuard { + static const int SLOTS = 16; + uint32_t _hash[SLOTS] = {}; + int _next = 0; + + static uint32_t hash(const uint8_t* p, size_t len) { // FNV-1a, never zero (zero marks an empty slot) + uint32_t h = 2166136261u; + for (size_t i = 0; i < len; i++) { + h = (h ^ p[i]) * 16777619u; + } + return h ? h : 1; + } + +public: + void note(const uint8_t* plain, size_t len) { + _hash[_next] = hash(plain, len); + _next = (_next + 1) % SLOTS; + } + + bool matches(const uint8_t* plain, size_t len) const { + uint32_t h = hash(plain, len); + for (int i = 0; i < SLOTS; i++) { + if (_hash[i] == h) return true; + } + return false; + } +}; diff --git a/src/helpers/UTF8Helpers.h b/src/helpers/UTF8Helpers.h index e06cf4a6..c73b87eb 100644 --- a/src/helpers/UTF8Helpers.h +++ b/src/helpers/UTF8Helpers.h @@ -53,4 +53,50 @@ inline size_t validUtf8PrefixLength(const char* text, size_t max_bytes) { return offset; } +inline bool isValidUtf8TextAndPadding(const uint8_t* data, size_t len) { + size_t offset = 0; + bool seen_null = false; + size_t null_pos = 0; + while (offset < len) { + const uint8_t first = static_cast(data[offset]); + if (seen_null) { + if (first == 0) { // OK, is zero padding + offset++; + } else if (offset == null_pos + 1 && first >= 4 && first != 0xFF) { // OK, extended attempt byte (see composeMsgPacket) + offset++; + } else { + return false; // Error, non-zero padding encountered + } + } else { + if (first == 0) { + seen_null = true; + null_pos = offset; + offset++; + } else { + size_t sequence_length = 0; + + if (first < 0x20 && first != '\t' && first != '\n' && first != '\r') { + return false; // control character + } else if (first <= 0x7F) { + sequence_length = 1; + } else if (first >= 0xC2 && first <= 0xDF) { + sequence_length = 2; + } else if (first >= 0xE0 && first <= 0xEF) { + sequence_length = 3; + } else if (first >= 0xF0 && first <= 0xF4) { + sequence_length = 4; + } else { + return false; // unsupported UTF-8 encoding + } + if (offset + sequence_length > len) return false; // invalid UTF-8 encoding + for (size_t i = 1; i < sequence_length; i++) { + if (!isUtf8Continuation(data[offset + i])) return false; // invalid UTF-8 encoding + } + offset += sequence_length; + } + } + } + return true; // is OK +} + } // namespace mesh diff --git a/src/helpers/ui/companion-new/UITask.cpp b/src/helpers/ui/companion-new/UITask.cpp index dd792cbd..cdc540ca 100644 --- a/src/helpers/ui/companion-new/UITask.cpp +++ b/src/helpers/ui/companion-new/UITask.cpp @@ -392,7 +392,7 @@ public: } else if (_page == HomePage::RECENT) { display.setColor(UIColor::primary_txt); int y = 20; - for (int i = 0, k = recent_next_idx; i < UI_RECENT_LIST_SIZE; i++, y += 11, k = (k + 1) % UI_RECENT_LIST_SIZE) { + for (int i = 0, k = recent_next_idx; i < UI_RECENT_LIST_SIZE; i++, k = (k + 1) % UI_RECENT_LIST_SIZE) { auto a = &recent[k]; if (a->name[0] == 0) continue; // empty slot int secs = _rtc->getCurrentTime() - a->recv_timestamp; @@ -412,6 +412,10 @@ public: display.drawTextEllipsized(0, y, max_name_width, filtered_recent_name); display.setCursor(display.width() - timestamp_width - 1, y); display.print(tmp); + y += 11; + } + if (y == 20) { + display.drawTextCentered(display.width() / 2, 32, "No recent adverts"); } } else if (_page == HomePage::RADIO) { display.setColor(UIColor::primary_txt); @@ -513,6 +517,11 @@ public: } else if (_page == HomePage::SENSORS) { int y = 18; refresh_sensors(); + if (sensors_nb == 0) { + display.setColor(UIColor::primary_txt); + display.drawTextCentered(display.width() / 2, 32, "No sensor readings"); + return 5000; + } char buf[30]; char name[30]; LPPReader r(sensors_lpp.getBuffer(), sensors_lpp.getSize()); diff --git a/test/test_acl_replay_floor/test_acl_replay_floor.cpp b/test/test_acl_replay_floor/test_acl_replay_floor.cpp index 85bf0353..8141266f 100644 --- a/test/test_acl_replay_floor/test_acl_replay_floor.cpp +++ b/test/test_acl_replay_floor/test_acl_replay_floor.cpp @@ -183,6 +183,7 @@ TEST(AclReplayFloor, AlternatesSlotsAcrossReboots) { n.boot(); c = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE); ASSERT_EQ(c->last_timestamp, ts) << "boot " << boot; + ASSERT_EQ(c->last_req_timestamp, ts) << "boot " << boot; // REQ floor (upstream split) comes back too } } diff --git a/variants/wio-tracker-l1/variant.h b/variants/wio-tracker-l1/variant.h index 86ad62ba..433289d1 100644 --- a/variants/wio-tracker-l1/variant.h +++ b/variants/wio-tracker-l1/variant.h @@ -32,11 +32,25 @@ #define PIN_BUTTON5 (28) // Joystick Right #define PIN_BUTTON6 (29) // Joystick Press #define PIN_BACK_BTN PIN_BUTTON1 +// Joystick pins can be overridden from build_flags, e.g. for an enclosure +// that mounts the display rotated 90 degrees relative to the joystick: +// -D JOYSTICK_UP=PIN_BUTTON4 -D JOYSTICK_DOWN=PIN_BUTTON5 +// -D JOYSTICK_LEFT=PIN_BUTTON3 -D JOYSTICK_RIGHT=PIN_BUTTON2 +#ifndef JOYSTICK_UP #define JOYSTICK_UP PIN_BUTTON2 +#endif +#ifndef JOYSTICK_DOWN #define JOYSTICK_DOWN PIN_BUTTON3 +#endif +#ifndef JOYSTICK_LEFT #define JOYSTICK_LEFT PIN_BUTTON4 +#endif +#ifndef JOYSTICK_RIGHT #define JOYSTICK_RIGHT PIN_BUTTON5 +#endif +#ifndef JOYSTICK_PRESS #define JOYSTICK_PRESS PIN_BUTTON6 +#endif #define PIN_USER_BTN PIN_BUTTON6 // Buzzer