diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index f3d526c9..33037996 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -379,13 +379,12 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr MESH_DEBUG_PRINTLN("Possible login replay attack!"); return 0; // FATAL: client table is full -OR- replay attack } - acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting - 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 memcpy(client->shared_secret, secret, PUB_KEY_SIZE); if (perms != PERM_ACL_GUEST) { // keep number of FS writes to a minimum diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index e0f349a2..a20c780b 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -372,8 +372,6 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m MESH_DEBUG_PRINTLN("possible replay attack!"); return; } - acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting - MESH_DEBUG_PRINTLN("Login success!"); client->last_timestamp = sender_timestamp; client->extra.room.sync_since = sender_sync_since; @@ -383,6 +381,7 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m 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 memcpy(client->shared_secret, secret, PUB_KEY_SIZE); dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index 73107f1a..ecb91e3e 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -387,12 +387,11 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* MESH_DEBUG_PRINTLN("Possible login replay attack!"); return 0; // FATAL: client table is full -OR- replay attack } - acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting - 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); diff --git a/src/helpers/ClientACL.cpp b/src/helpers/ClientACL.cpp index 13b59677..f1836d5b 100644 --- a/src/helpers/ClientACL.cpp +++ b/src/helpers/ClientACL.cpp @@ -160,6 +160,11 @@ void ClientACL::orphanTimestamp(const ClientInfo* c) { } void ClientACL::persistTimestamp(ClientInfo* c, uint32_t sender_timestamp, uint32_t now) { + // Admins only. A guest can only read, and anybody gets in as one when the + // guest password is empty (the default): its every request would be a flash + // write, and its floors would push the admins' out of the orphan list. Its + // floor stays in RAM, as upstream. + if (!c->isAdmin()) return; if (sender_timestamp <= c->saved_timestamp) return; // retry, or nothing new if (sender_timestamp > now + ACL_TS_MAX_AHEAD) return; // runaway sender clock c->saved_timestamp = sender_timestamp; 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 dffd5c35..85bf0353 100644 --- a/test/test_acl_replay_floor/test_acl_replay_floor.cpp +++ b/test/test_acl_replay_floor/test_acl_replay_floor.cpp @@ -109,17 +109,28 @@ TEST(AclReplayFloor, FloorsOutliveAclClear) { EXPECT_FALSE(n.accept(n.acl->putClient(idFor(3), 0), NOW - 50)); } -TEST(AclReplayFloor, EvictedClientKeepsItsFloor) { +TEST(AclReplayFloor, GuestTrafficWritesNothing) { Node n; ClientInfo* g = n.acl->putClient(idFor(4), 0); - g->last_activity = 1; // least active -> evicted first - ASSERT_TRUE(n.accept(g, NOW - 70)); - for (int i = 0; i < MAX_CLIENTS; i++) { + g->permissions = PERM_ACL_GUEST; // anybody, with the default empty guest password + int writes = n.fs.writes; + for (uint32_t i = 0; i < 1000; i++) ASSERT_TRUE(n.accept(g, NOW - 1000 + i)); + EXPECT_EQ(n.fs.writes, writes); +} + +TEST(AclReplayFloor, GuestsDoNotPushOutAdminFloors) { + Node n; + ASSERT_TRUE(n.accept(n.admin(3), NOW - 50)); + n.acl->save(&n.fs); + n.acl->clear(); // the admin's floor is an orphan now + for (int i = 0; i < 4 * MAX_CLIENTS; i++) { // a flood of fresh guest identities ClientInfo* c = n.acl->putClient(idFor(100 + i), 0); - c->last_activity = 1000 + i; + c->permissions = PERM_ACL_GUEST; + c->last_activity = i; + n.accept(c, NOW - 10); } - ASSERT_EQ(n.acl->getClient(idFor(4).pub_key, PUB_KEY_SIZE), nullptr); - EXPECT_FALSE(n.accept(n.acl->putClient(idFor(4), 0), NOW - 70)); + n.boot(); + EXPECT_FALSE(n.accept(n.acl->putClient(idFor(3), 0), NOW - 50)); } TEST(AclReplayFloor, TornWriteFallsBackToTheOtherSlot) {