ClientACL: the replay floor survives a reboot -- a captured admin command no longer works after every boot

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-18 10:18:04 +01:00
1 parent 7949fb4274
commit 1171d8f972
8 files changed
+410 -1

No files matched your search

@@ -0,0 +1,181 @@
// The replay floor (ClientACL::persistTimestamp) must survive a reboot, a torn
// write, an ACL write the reboot beat, and an ACL wipe -- otherwise a captured
// admin command (a `reboot` among them) is accepted again after every boot.
#include <gtest/gtest.h>
#define NRF52_PLATFORM 1
#ifndef MAX_PATH_SIZE
#define MAX_PATH_SIZE 64
#endif
#include "../../src/helpers/ClientACL.cpp"
// src/Identity.cpp needs ed25519; ClientACL only stores keys and asks for a secret.
mesh::Identity::Identity() { memset(pub_key, 0, sizeof(pub_key)); }
mesh::LocalIdentity::LocalIdentity() { memset(prv_key, 0, sizeof(prv_key)); }
void mesh::LocalIdentity::calcSharedSecret(uint8_t* secret, const uint8_t*) const { memset(secret, 0x11, PUB_KEY_SIZE); }
namespace {
const uint32_t NOW = 1763400000; // 2025-11-17
mesh::Identity idFor(uint8_t n) {
uint8_t key[PUB_KEY_SIZE];
memset(key, n, sizeof(key));
return mesh::Identity(key);
}
struct Node {
Adafruit_LittleFS fs;
mesh::LocalIdentity self;
ClientACL* acl = nullptr;
Node() { boot(); }
~Node() { delete acl; }
void boot() { // a reboot: RAM gone, flash kept
delete acl;
acl = new ClientACL();
acl->load(&fs, self);
}
ClientInfo* admin(uint8_t n) {
ClientInfo* c = acl->putClient(idFor(n), 0);
c->permissions = PERM_ACL_ADMIN;
return c;
}
// What the repeater does on an authenticated CLI packet.
bool accept(ClientInfo* c, uint32_t ts) {
if (ts <= c->last_timestamp) return false;
acl->persistTimestamp(c, ts, NOW);
c->last_timestamp = ts;
return true;
}
};
} // namespace
TEST(AclReplayFloor, CapturedCommandIsRejectedAfterReboot) {
Node n;
ClientInfo* c = n.admin(1);
ASSERT_TRUE(n.accept(c, NOW - 10)); // the captured `reboot`
n.acl->save(&n.fs);
n.boot();
ClientInfo* again = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
ASSERT_NE(again, nullptr);
EXPECT_FALSE(n.accept(again, NOW - 10)); // the replay
EXPECT_TRUE(n.accept(again, NOW - 5)); // the admin's next command
}
TEST(AclReplayFloor, RetryWritesNothing) {
Node n;
ClientInfo* c = n.admin(1);
n.acl->persistTimestamp(c, NOW, NOW);
int writes = n.fs.writes;
n.acl->persistTimestamp(c, NOW, NOW);
n.acl->persistTimestamp(c, NOW - 1, NOW);
EXPECT_EQ(n.fs.writes, writes);
}
TEST(AclReplayFloor, RunawayClockIsNotPersisted) {
Node n;
ClientInfo* c = n.admin(1);
ASSERT_TRUE(n.accept(c, NOW + 30 * 86400)); // accepted in this session, as upstream
n.acl->save(&n.fs);
n.boot();
// ...but it does not lock the admin with a correct clock out after a reboot.
ClientInfo* again = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
EXPECT_TRUE(n.accept(again, NOW));
}
TEST(AclReplayFloor, LoginTheLazyAclWriteMissedKeepsItsFloor) {
Node n;
ClientInfo* c = n.admin(2);
ASSERT_TRUE(n.accept(c, NOW - 100)); // password login...
n.boot(); // ...and a reboot before acl.save()
EXPECT_EQ(n.acl->getClient(idFor(2).pub_key, PUB_KEY_SIZE), nullptr);
// The replayed login re-adds the client: its floor must come back with it.
ClientInfo* replay = n.acl->putClient(idFor(2), 0);
EXPECT_FALSE(n.accept(replay, NOW - 100));
}
TEST(AclReplayFloor, FloorsOutliveAclClear) {
Node n;
ASSERT_TRUE(n.accept(n.admin(3), NOW - 50));
n.acl->save(&n.fs);
n.acl->clear();
n.boot();
EXPECT_FALSE(n.accept(n.acl->putClient(idFor(3), 0), NOW - 50));
}
TEST(AclReplayFloor, EvictedClientKeepsItsFloor) {
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++) {
ClientInfo* c = n.acl->putClient(idFor(100 + i), 0);
c->last_activity = 1000 + i;
}
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));
}
TEST(AclReplayFloor, TornWriteFallsBackToTheOtherSlot) {
Node n;
ClientInfo* c = n.admin(1);
ASSERT_TRUE(n.accept(c, NOW - 30));
ASSERT_TRUE(n.accept(c, NOW - 20));
n.acl->save(&n.fs);
n.boot();
// The next write must go over the OLDER slot; tear it halfway.
std::map<std::string, std::string> before = n.fs.files;
c = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
n.acl->persistTimestamp(c, NOW - 10, NOW);
std::string written;
for (auto& f : n.fs.files) {
if (f.first.rfind("/s_acl_ts", 0) == 0 && before[f.first] != f.second) written = f.first;
}
ASSERT_FALSE(written.empty());
n.fs.files[written].resize(n.fs.files[written].size() / 2);
n.boot();
// The newest COMPLETE write (NOW - 20) holds: the torn one guarded a command
// that never ran.
c = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
EXPECT_EQ(c->last_timestamp, NOW - 20);
}
TEST(AclReplayFloor, CorruptedSlotIsIgnored) {
Node n;
ClientInfo* c = n.admin(1);
ASSERT_TRUE(n.accept(c, NOW - 30));
ASSERT_TRUE(n.accept(c, NOW - 20));
n.acl->save(&n.fs);
for (auto& f : n.fs.files) {
if (f.first.rfind("/s_acl_ts", 0) == 0) f.second[9] ^= 0x01; // flip one bit in the timestamp
}
n.boot();
c = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
EXPECT_EQ(c->last_timestamp, 0u); // both slots garbage -> no floor, but no garbage floor either
}
TEST(AclReplayFloor, AlternatesSlotsAcrossReboots) {
Node n;
ClientInfo* c = n.admin(1);
uint32_t ts = NOW - 1000;
for (int boot = 0; boot < 5; boot++) {
for (int k = 0; k < 3; k++) ASSERT_TRUE(n.accept(c, ++ts));
n.acl->save(&n.fs);
n.boot();
c = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
ASSERT_EQ(c->last_timestamp, ts) << "boot " << boot;
}
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}