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:
1 parent
7949fb4274
commit
1171d8f972
8 files changed
+410
-1
No files matched your search
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
// In-memory stand-in for the nRF52 LittleFS, enough for ClientACL: whole-file
|
||||
// write, read, exists, remove. Tests reach into `files` to tear or drop a write.
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#define FILE_O_READ 0
|
||||
#define FILE_O_WRITE 1
|
||||
|
||||
namespace Adafruit_LittleFS_Namespace {
|
||||
|
||||
class File {
|
||||
std::map<std::string, std::string>* _files = nullptr;
|
||||
std::string _name;
|
||||
size_t _pos = 0;
|
||||
bool _write = false;
|
||||
|
||||
public:
|
||||
File() = default;
|
||||
File(std::map<std::string, std::string>* files, const std::string& name, bool write)
|
||||
: _files(files), _name(name), _write(write) {
|
||||
if (write) (*_files)[name] = "";
|
||||
}
|
||||
explicit operator bool() const { return _files != nullptr; }
|
||||
int read(uint8_t* buf, size_t len) {
|
||||
const std::string& d = (*_files)[_name];
|
||||
size_t n = std::min(len, d.size() - std::min(_pos, d.size()));
|
||||
std::memcpy(buf, d.data() + _pos, n);
|
||||
_pos += n;
|
||||
return (int)n;
|
||||
}
|
||||
size_t write(const uint8_t* buf, size_t len) {
|
||||
(*_files)[_name].append((const char*)buf, len);
|
||||
return len;
|
||||
}
|
||||
void close() {}
|
||||
};
|
||||
|
||||
} // namespace Adafruit_LittleFS_Namespace
|
||||
|
||||
class Adafruit_LittleFS {
|
||||
public:
|
||||
std::map<std::string, std::string> files;
|
||||
int writes = 0;
|
||||
|
||||
bool exists(const char* name) { return files.count(name) > 0; }
|
||||
bool remove(const char* name) { return files.erase(name) > 0; }
|
||||
bool mkdir(const char*) { return true; }
|
||||
Adafruit_LittleFS_Namespace::File open(const char* name, uint8_t mode = FILE_O_READ) {
|
||||
if (mode == FILE_O_WRITE) {
|
||||
writes++;
|
||||
return Adafruit_LittleFS_Namespace::File(&files, name, true);
|
||||
}
|
||||
if (!exists(name)) return Adafruit_LittleFS_Namespace::File();
|
||||
return Adafruit_LittleFS_Namespace::File(&files, name, false);
|
||||
}
|
||||
};
|
||||
@@ -28,6 +28,10 @@ public:
|
||||
for (size_t i = 0; i < hashLen; i++) {
|
||||
hash[i] = _state[i % 32];
|
||||
}
|
||||
// A short digest (4-byte checksum tags) must still depend on every byte.
|
||||
for (size_t i = hashLen; i < 32 && hashLen > 0; i++) {
|
||||
hash[i % hashLen] ^= _state[i];
|
||||
}
|
||||
}
|
||||
|
||||
void resetHMAC(const uint8_t* key, size_t keyLen) {}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
Reference in new issue
Block a user