From a28c83ae2046410e8d9216847ae294276ddcedca Mon Sep 17 00:00:00 2001 From: kb3cmt Date: Tue, 22 Sep 2026 08:17:38 -0400 Subject: [PATCH 1/2] Fix room, repeater, and sensor login so an empty guest password cannot grant access. --- examples/simple_repeater/MyMesh.cpp | 14 ++++++++-- examples/simple_room_server/MyMesh.cpp | 37 +++++++++++++++++--------- examples/simple_sensor/SensorMesh.cpp | 6 +++++ 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index ca6a3e607e4..96fd3ee59e4 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -101,7 +101,8 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr uint8_t perms; if (strcmp((char *)data, _prefs.password) == 0) { // check for valid admin password perms = PERM_ACL_ADMIN; - } else if (strcmp((char *)data, _prefs.guest_password) == 0) { // check guest password + } else if (_prefs.guest_password[0] != 0 && + strcmp((char *)data, _prefs.guest_password) == 0) { // empty guest password is not a match perms = PERM_ACL_GUEST; } else { #if MESH_DEBUG @@ -117,15 +118,24 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr } MESH_DEBUG_PRINTLN("Login success!"); + uint8_t existing = client->permissions & PERM_ACL_ROLE_MASK; + if (existing > perms) perms = existing; // never demote client->last_timestamp = sender_timestamp; client->last_activity = getRTCClock()->getCurrentTime(); - client->permissions &= ~0x03; + client->permissions &= ~PERM_ACL_ROLE_MASK; client->permissions |= perms; memcpy(client->shared_secret, secret, PUB_KEY_SIZE); if (perms != PERM_ACL_GUEST) { // keep number of FS writes to a minimum dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); } + } else { + if (sender_timestamp <= client->last_timestamp) { + MESH_DEBUG_PRINTLN("Possible login replay attack!"); + return 0; + } + client->last_timestamp = sender_timestamp; + client->last_activity = getRTCClock()->getCurrentTime(); } if (is_flood) { diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index 71b8d32a3a1..4fc5f168ba1 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -325,6 +325,7 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m uint8_t *data, size_t len) { if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin // client (unknown at this stage) + if (len < 9) return; uint32_t sender_timestamp, sender_sync_since; memcpy(&sender_timestamp, data, 4); memcpy(&sender_sync_since, &data[4], 4); // sender's "sync messags SINCE x" timestamp @@ -344,15 +345,14 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m uint8_t perm; if (strcmp((char *)&data[8], _prefs.password) == 0) { // check for valid admin password perm = PERM_ACL_ADMIN; + } else if (_prefs.guest_password[0] != 0 && + strcmp((char *)&data[8], _prefs.guest_password) == 0) { // empty guest password is not a match + perm = PERM_ACL_READ_WRITE; + } else if (_prefs.allow_read_only) { + perm = PERM_ACL_GUEST; } else { - if (strcmp((char *)&data[8], _prefs.guest_password) == 0) { // check the room/public password - perm = PERM_ACL_READ_WRITE; - } else if (_prefs.allow_read_only) { - perm = PERM_ACL_GUEST; - } else { - MESH_DEBUG_PRINTLN("Incorrect room password"); - return; // no response. Client will timeout - } + MESH_DEBUG_PRINTLN("Incorrect room password"); + return; // no response. Client will timeout } client = acl.putClient(sender, 0); // add to known clients (if not already known) @@ -362,17 +362,30 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m } MESH_DEBUG_PRINTLN("Login success!"); + uint8_t existing = client->permissions & PERM_ACL_ROLE_MASK; + if (existing > perm) perm = existing; // never demote (e.g. admin logging in with guest password) + client->last_timestamp = sender_timestamp; client->extra.room.sync_since = sender_sync_since; client->extra.room.pending_ack = 0; client->extra.room.push_failures = 0; client->last_activity = getRTCClock()->getCurrentTime(); - client->permissions &= ~0x03; + client->permissions &= ~PERM_ACL_ROLE_MASK; client->permissions |= perm; memcpy(client->shared_secret, secret, PUB_KEY_SIZE); dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); + } else { + if (sender_timestamp <= client->last_timestamp) { + MESH_DEBUG_PRINTLN("possible replay attack!"); + return; + } + client->last_timestamp = sender_timestamp; + client->extra.room.sync_since = sender_sync_since; + client->extra.room.pending_ack = 0; + client->extra.room.push_failures = 0; + client->last_activity = getRTCClock()->getCurrentTime(); } if (packet->isRouteFlood()) { @@ -477,9 +490,9 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, send_ack = false; // and no ACK... user shoudn't be sending these } } else { // TXT_TYPE_PLAIN - if ((client->permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) { + if ((client->permissions & PERM_ACL_ROLE_MASK) < PERM_ACL_READ_WRITE) { temp[5] = 0; // no reply - send_ack = false; // no ACK + send_ack = false; // no ACK; GUEST and READ_ONLY cannot post } else { if (!is_retry) { addPost(client, (const char *)&data[5]); @@ -538,7 +551,7 @@ 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_timestamp) { // prevent replay attacks (equal timestamp is not a retry) MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected"); } else { client->last_timestamp = sender_timestamp; diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index b186066810d..1e50faa89c0 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -374,6 +374,12 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* #endif return 0; } + if (sender_timestamp <= client->last_timestamp) { + MESH_DEBUG_PRINTLN("Possible login replay attack!"); + return 0; + } + client->last_timestamp = sender_timestamp; + client->last_activity = getRTCClock()->getCurrentTime(); } else { if (strcmp((char *) data, _prefs.password) != 0) { // check for valid admin password #if MESH_DEBUG From 52e6355c8547cc16d18aa9160b467d63f3bbeb7f Mon Sep 17 00:00:00 2001 From: kb3cmt Date: Thu, 24 Sep 2026 18:06:56 +0000 Subject: [PATCH 2/2] Put the repeater blank guest login back. With no guest password set, a blank login is a guest. That is what the repeater was built to do. Room and sensor login checks are unchanged. --- examples/simple_repeater/MyMesh.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 96fd3ee59e4..e4b451a0057 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -101,8 +101,7 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr uint8_t perms; if (strcmp((char *)data, _prefs.password) == 0) { // check for valid admin password perms = PERM_ACL_ADMIN; - } else if (_prefs.guest_password[0] != 0 && - strcmp((char *)data, _prefs.guest_password) == 0) { // empty guest password is not a match + } else if (strcmp((char *)data, _prefs.guest_password) == 0) { // check guest password perms = PERM_ACL_GUEST; } else { #if MESH_DEBUG