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
@@ -379,6 +379,7 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr
|
|||||||
MESH_DEBUG_PRINTLN("Possible login replay attack!");
|
MESH_DEBUG_PRINTLN("Possible login replay attack!");
|
||||||
return 0; // FATAL: client table is full -OR- 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!");
|
MESH_DEBUG_PRINTLN("Login success!");
|
||||||
client->last_timestamp = sender_timestamp;
|
client->last_timestamp = sender_timestamp;
|
||||||
@@ -1122,6 +1123,7 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
|||||||
memcpy(×tamp, data, 4);
|
memcpy(×tamp, data, 4);
|
||||||
|
|
||||||
if (timestamp > client->last_timestamp) { // prevent replay attacks
|
if (timestamp > client->last_timestamp) { // prevent replay attacks
|
||||||
|
acl.persistTimestamp(client, timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting
|
||||||
int reply_len = handleRequest(client, timestamp, &data[4], len - 4);
|
int reply_len = handleRequest(client, timestamp, &data[4], len - 4);
|
||||||
if (reply_len == 0) return; // invalid command
|
if (reply_len == 0) return; // invalid command
|
||||||
|
|
||||||
@@ -1156,6 +1158,7 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
|||||||
MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported text type received: flags=%02x", (uint32_t)flags);
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported text type received: flags=%02x", (uint32_t)flags);
|
||||||
} else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks
|
} else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks
|
||||||
bool is_retry = (sender_timestamp == client->last_timestamp);
|
bool is_retry = (sender_timestamp == client->last_timestamp);
|
||||||
|
acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting
|
||||||
client->last_timestamp = sender_timestamp;
|
client->last_timestamp = sender_timestamp;
|
||||||
client->last_activity = getRTCClock()->getCurrentTime();
|
client->last_activity = getRTCClock()->getCurrentTime();
|
||||||
|
|
||||||
|
|||||||
@@ -372,6 +372,7 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
|
|||||||
MESH_DEBUG_PRINTLN("possible replay attack!");
|
MESH_DEBUG_PRINTLN("possible replay attack!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting
|
||||||
|
|
||||||
MESH_DEBUG_PRINTLN("Login success!");
|
MESH_DEBUG_PRINTLN("Login success!");
|
||||||
client->last_timestamp = sender_timestamp;
|
client->last_timestamp = sender_timestamp;
|
||||||
@@ -459,6 +460,7 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
|||||||
MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported command flags received: flags=%02x", (uint32_t)flags);
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported command flags received: flags=%02x", (uint32_t)flags);
|
||||||
} else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks, but send Acks for retries
|
} else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks, but send Acks for retries
|
||||||
bool is_retry = (sender_timestamp == client->last_timestamp);
|
bool is_retry = (sender_timestamp == client->last_timestamp);
|
||||||
|
acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting
|
||||||
client->last_timestamp = sender_timestamp;
|
client->last_timestamp = sender_timestamp;
|
||||||
|
|
||||||
uint32_t now = getRTCClock()->getCurrentTimeUnique();
|
uint32_t now = getRTCClock()->getCurrentTimeUnique();
|
||||||
@@ -554,6 +556,7 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
|||||||
if (sender_timestamp < client->last_timestamp) { // prevent replay attacks
|
if (sender_timestamp < client->last_timestamp) { // prevent replay attacks
|
||||||
MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected");
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected");
|
||||||
} else {
|
} else {
|
||||||
|
acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting
|
||||||
client->last_timestamp = sender_timestamp;
|
client->last_timestamp = sender_timestamp;
|
||||||
|
|
||||||
uint32_t now = getRTCClock()->getCurrentTime();
|
uint32_t now = getRTCClock()->getCurrentTime();
|
||||||
|
|||||||
@@ -387,6 +387,7 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
|
|||||||
MESH_DEBUG_PRINTLN("Possible login replay attack!");
|
MESH_DEBUG_PRINTLN("Possible login replay attack!");
|
||||||
return 0; // FATAL: client table is full -OR- 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!");
|
MESH_DEBUG_PRINTLN("Login success!");
|
||||||
client->last_timestamp = sender_timestamp;
|
client->last_timestamp = sender_timestamp;
|
||||||
@@ -619,6 +620,7 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
|
|||||||
memcpy(×tamp, data, 4);
|
memcpy(×tamp, data, 4);
|
||||||
|
|
||||||
if (timestamp > from->last_timestamp) { // prevent replay attacks
|
if (timestamp > from->last_timestamp) { // prevent replay attacks
|
||||||
|
acl.persistTimestamp(from, timestamp, getRTCClock()->getCurrentTime()); // CUSTOM (TeTeHacko): before acting
|
||||||
uint8_t reply_len = handleRequest(from, timestamp, data[4], &data[5], len - 5);
|
uint8_t reply_len = handleRequest(from, timestamp, data[4], &data[5], len - 5);
|
||||||
if (reply_len == 0) return; // invalid command
|
if (reply_len == 0) return; // invalid command
|
||||||
|
|
||||||
@@ -649,6 +651,7 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
|
|||||||
uint8_t flags = (data[4] >> 2); // message attempt number, and other flags
|
uint8_t flags = (data[4] >> 2); // message attempt number, and other flags
|
||||||
|
|
||||||
if (sender_timestamp > from->last_timestamp) { // prevent replay attacks
|
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) {
|
if (flags == TXT_TYPE_PLAIN) {
|
||||||
bool handled = handleIncomingMsg(*from, sender_timestamp, &data[5], flags, len - 5);
|
bool handled = handleIncomingMsg(*from, sender_timestamp, &data[5], flags, len - 5);
|
||||||
if (handled) { // if msg was handled then send an ack
|
if (handled) { // if msg was handled then send an ack
|
||||||
|
|||||||
@@ -50,6 +50,120 @@ void ClientACL::load(FILESYSTEM* fs, const mesh::LocalIdentity& self_id) {
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
loadTimestamps();
|
||||||
|
}
|
||||||
|
|
||||||
|
// CUSTOM (TeTeHacko): /s_acl_ts0 and /s_acl_ts1 are written alternately, each
|
||||||
|
// {pubkey prefix 8, timestamp 4} * n + seq 4 + sha256 tag 4. A write torn by a
|
||||||
|
// power cut leaves the other slot intact, and the floor is the max over both
|
||||||
|
// valid slots, so it can only ever move forward. The next write always goes over
|
||||||
|
// the OLDER slot (lower seq) -- and a torn write means the command it guarded
|
||||||
|
// never ran, so nothing executed is lost.
|
||||||
|
#define ACL_TS_PREFIX 8
|
||||||
|
#define ACL_TS_RECORD (ACL_TS_PREFIX + 4)
|
||||||
|
|
||||||
|
static const char* tsFilename(uint8_t slot) { return slot ? "/s_acl_ts1" : "/s_acl_ts0"; }
|
||||||
|
|
||||||
|
void ClientACL::loadTimestamps() {
|
||||||
|
static uint8_t buf[2 * MAX_CLIENTS * ACL_TS_RECORD + 8];
|
||||||
|
_num_ts_orphans = 0;
|
||||||
|
_ts_seq = 0;
|
||||||
|
_ts_slot = 0;
|
||||||
|
bool have_newest = false;
|
||||||
|
for (uint8_t slot = 0; slot < 2; slot++) {
|
||||||
|
if (!_fs->exists(tsFilename(slot))) continue;
|
||||||
|
#if defined(RP2040_PLATFORM)
|
||||||
|
File file = _fs->open(tsFilename(slot), "r");
|
||||||
|
#else
|
||||||
|
File file = _fs->open(tsFilename(slot));
|
||||||
|
#endif
|
||||||
|
if (!file) continue;
|
||||||
|
int len = file.read(buf, sizeof(buf));
|
||||||
|
file.close();
|
||||||
|
if (len < 8 || (len - 8) % ACL_TS_RECORD != 0) continue;
|
||||||
|
|
||||||
|
uint8_t tag[4];
|
||||||
|
mesh::Utils::sha256(tag, 4, buf, len - 4);
|
||||||
|
if (memcmp(tag, &buf[len - 4], 4) != 0) continue; // torn write: ignore this slot
|
||||||
|
uint32_t seq;
|
||||||
|
memcpy(&seq, &buf[len - 8], 4);
|
||||||
|
if (!have_newest || seq > _ts_seq) {
|
||||||
|
_ts_seq = seq;
|
||||||
|
_ts_slot = slot ^ 1; // next write goes over the other, older slot
|
||||||
|
have_newest = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int ofs = 0; ofs < len - 8; ofs += ACL_TS_RECORD) {
|
||||||
|
uint32_t ts;
|
||||||
|
memcpy(&ts, &buf[ofs + ACL_TS_PREFIX], 4);
|
||||||
|
ClientInfo* c = getClient(&buf[ofs], ACL_TS_PREFIX);
|
||||||
|
if (c) {
|
||||||
|
if (ts > c->saved_timestamp) c->saved_timestamp = ts;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Not in /s_contacts: a fresh login whose lazy ACL write a reboot beat, or an
|
||||||
|
// evicted guest. Keep the floor, or replaying their login would re-admit them.
|
||||||
|
int j = 0;
|
||||||
|
while (j < _num_ts_orphans && memcmp(_ts_orphans[j].prefix, &buf[ofs], ACL_TS_PREFIX) != 0) j++;
|
||||||
|
if (j == _num_ts_orphans) {
|
||||||
|
if (j == MAX_CLIENTS) continue;
|
||||||
|
memcpy(_ts_orphans[j].prefix, &buf[ofs], ACL_TS_PREFIX);
|
||||||
|
_ts_orphans[j].ts = 0;
|
||||||
|
_num_ts_orphans++;
|
||||||
|
}
|
||||||
|
if (ts > _ts_orphans[j].ts) _ts_orphans[j].ts = ts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < num_clients; i++) {
|
||||||
|
clients[i].last_timestamp = clients[i].saved_timestamp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientACL::saveTimestamps() {
|
||||||
|
if (!_fs) return;
|
||||||
|
static uint8_t buf[2 * MAX_CLIENTS * ACL_TS_RECORD + 8];
|
||||||
|
int len = 0;
|
||||||
|
for (int i = 0; i < num_clients; i++) {
|
||||||
|
if (clients[i].saved_timestamp == 0) continue;
|
||||||
|
memcpy(&buf[len], clients[i].id.pub_key, ACL_TS_PREFIX);
|
||||||
|
memcpy(&buf[len + ACL_TS_PREFIX], &clients[i].saved_timestamp, 4);
|
||||||
|
len += ACL_TS_RECORD;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < _num_ts_orphans; i++) {
|
||||||
|
memcpy(&buf[len], _ts_orphans[i].prefix, ACL_TS_PREFIX);
|
||||||
|
memcpy(&buf[len + ACL_TS_PREFIX], &_ts_orphans[i].ts, 4);
|
||||||
|
len += ACL_TS_RECORD;
|
||||||
|
}
|
||||||
|
_ts_seq++;
|
||||||
|
memcpy(&buf[len], &_ts_seq, 4);
|
||||||
|
len += 4;
|
||||||
|
mesh::Utils::sha256(&buf[len], 4, buf, len);
|
||||||
|
len += 4;
|
||||||
|
|
||||||
|
File file = openWrite(_fs, tsFilename(_ts_slot));
|
||||||
|
if (file) {
|
||||||
|
file.write(buf, len);
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
_ts_slot ^= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientACL::orphanTimestamp(const ClientInfo* c) {
|
||||||
|
if (c->saved_timestamp == 0) return;
|
||||||
|
if (_num_ts_orphans == MAX_CLIENTS) { // full: drop the oldest
|
||||||
|
memmove(&_ts_orphans[0], &_ts_orphans[1], (MAX_CLIENTS - 1) * sizeof(TsOrphan));
|
||||||
|
_num_ts_orphans--;
|
||||||
|
}
|
||||||
|
memcpy(_ts_orphans[_num_ts_orphans].prefix, c->id.pub_key, ACL_TS_PREFIX);
|
||||||
|
_ts_orphans[_num_ts_orphans].ts = c->saved_timestamp;
|
||||||
|
_num_ts_orphans++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientACL::persistTimestamp(ClientInfo* c, uint32_t sender_timestamp, uint32_t now) {
|
||||||
|
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;
|
||||||
|
saveTimestamps();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientACL::save(FILESYSTEM* fs, bool (*filter)(ClientInfo*)) {
|
void ClientACL::save(FILESYSTEM* fs, bool (*filter)(ClientInfo*)) {
|
||||||
@@ -82,8 +196,12 @@ bool ClientACL::clear() {
|
|||||||
if (_fs->exists("/s_contacts")) {
|
if (_fs->exists("/s_contacts")) {
|
||||||
_fs->remove("/s_contacts");
|
_fs->remove("/s_contacts");
|
||||||
}
|
}
|
||||||
|
// The replay floors outlive the ACL: with them gone, a captured password login
|
||||||
|
// would be accepted again.
|
||||||
|
for (int i = 0; i < num_clients; i++) orphanTimestamp(&clients[i]);
|
||||||
memset(clients, 0, sizeof(clients));
|
memset(clients, 0, sizeof(clients));
|
||||||
num_clients = 0;
|
num_clients = 0;
|
||||||
|
saveTimestamps();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,11 +228,20 @@ ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms) {
|
|||||||
c = &clients[num_clients++];
|
c = &clients[num_clients++];
|
||||||
} else {
|
} else {
|
||||||
c = oldest; // evict least active contact
|
c = oldest; // evict least active contact
|
||||||
|
orphanTimestamp(c);
|
||||||
}
|
}
|
||||||
memset(c, 0, sizeof(*c));
|
memset(c, 0, sizeof(*c));
|
||||||
c->permissions = init_perms;
|
c->permissions = init_perms;
|
||||||
c->id = id;
|
c->id = id;
|
||||||
c->out_path_len = OUT_PATH_UNKNOWN;
|
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;
|
||||||
|
_num_ts_orphans--;
|
||||||
|
memmove(&_ts_orphans[i], &_ts_orphans[i + 1], (_num_ts_orphans - i) * sizeof(TsOrphan));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +251,7 @@ bool ClientACL::applyPermissions(const mesh::LocalIdentity& self_id, const uint8
|
|||||||
c = getClient(pubkey, key_len);
|
c = getClient(pubkey, key_len);
|
||||||
if (c == NULL) return false; // partial pubkey not found
|
if (c == NULL) return false; // partial pubkey not found
|
||||||
|
|
||||||
|
orphanTimestamp(c);
|
||||||
num_clients--; // delete from contacts[]
|
num_clients--; // delete from contacts[]
|
||||||
int i = c - clients;
|
int i = c - clients;
|
||||||
while (i < num_clients) {
|
while (i < num_clients) {
|
||||||
|
|||||||
+27
-1
@@ -18,7 +18,8 @@ struct ClientInfo {
|
|||||||
uint8_t out_path_len;
|
uint8_t out_path_len;
|
||||||
uint8_t out_path[MAX_PATH_SIZE];
|
uint8_t out_path[MAX_PATH_SIZE];
|
||||||
uint8_t shared_secret[PUB_KEY_SIZE];
|
uint8_t shared_secret[PUB_KEY_SIZE];
|
||||||
uint32_t last_timestamp; // by THEIR clock (transient)
|
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_activity; // by OUR clock (transient)
|
uint32_t last_activity; // by OUR clock (transient)
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
@@ -45,20 +46,45 @@ struct ClientInfo {
|
|||||||
#define MAX_CLIENTS 32
|
#define MAX_CLIENTS 32
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// CUSTOM (TeTeHacko): a timestamp further ahead of our clock than this is accepted
|
||||||
|
// but not persisted, so a companion with a runaway RTC cannot lock its admin out
|
||||||
|
// across reboots.
|
||||||
|
#ifndef ACL_TS_MAX_AHEAD
|
||||||
|
#define ACL_TS_MAX_AHEAD (24 * 3600)
|
||||||
|
#endif
|
||||||
|
|
||||||
class ClientACL {
|
class ClientACL {
|
||||||
FILESYSTEM* _fs;
|
FILESYSTEM* _fs;
|
||||||
ClientInfo clients[MAX_CLIENTS];
|
ClientInfo clients[MAX_CLIENTS];
|
||||||
int num_clients;
|
int num_clients;
|
||||||
|
uint8_t _ts_slot;
|
||||||
|
uint32_t _ts_seq;
|
||||||
|
struct TsOrphan { uint8_t prefix[8]; uint32_t ts; };
|
||||||
|
TsOrphan _ts_orphans[MAX_CLIENTS]; // floors of clients not (or no longer) in clients[]
|
||||||
|
int _num_ts_orphans;
|
||||||
|
|
||||||
|
void loadTimestamps();
|
||||||
|
void saveTimestamps();
|
||||||
|
void orphanTimestamp(const ClientInfo* c);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ClientACL() {
|
ClientACL() {
|
||||||
memset(clients, 0, sizeof(clients));
|
memset(clients, 0, sizeof(clients));
|
||||||
num_clients = 0;
|
num_clients = 0;
|
||||||
|
_ts_slot = 0;
|
||||||
|
_ts_seq = 0;
|
||||||
|
_num_ts_orphans = 0;
|
||||||
}
|
}
|
||||||
void load(FILESYSTEM* _fs, const mesh::LocalIdentity& self_id);
|
void load(FILESYSTEM* _fs, const mesh::LocalIdentity& self_id);
|
||||||
void save(FILESYSTEM* _fs, bool (*filter)(ClientInfo*)=NULL);
|
void save(FILESYSTEM* _fs, bool (*filter)(ClientInfo*)=NULL);
|
||||||
bool clear();
|
bool clear();
|
||||||
|
|
||||||
|
// CUSTOM (TeTeHacko): replay protection that survives a reboot. last_timestamp
|
||||||
|
// used to live only in RAM, so after any reboot a captured command (a `reboot`
|
||||||
|
// among them) was accepted again -- forever. Call this BEFORE acting on an
|
||||||
|
// authenticated packet; it writes the new high-water mark synchronously.
|
||||||
|
void persistTimestamp(ClientInfo* c, uint32_t sender_timestamp, uint32_t now);
|
||||||
|
|
||||||
ClientInfo* getClient(const uint8_t* pubkey, int key_len);
|
ClientInfo* getClient(const uint8_t* pubkey, int key_len);
|
||||||
ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms);
|
ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms);
|
||||||
bool applyPermissions(const mesh::LocalIdentity& self_id, const uint8_t* pubkey, int key_len, uint8_t perms);
|
bool applyPermissions(const mesh::LocalIdentity& self_id, const uint8_t* pubkey, int key_len, uint8_t perms);
|
||||||
|
|||||||
@@ -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++) {
|
for (size_t i = 0; i < hashLen; i++) {
|
||||||
hash[i] = _state[i % 32];
|
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) {}
|
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