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
@@ -50,6 +50,120 @@ void ClientACL::load(FILESYSTEM* fs, const mesh::LocalIdentity& self_id) {
|
||||
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*)) {
|
||||
@@ -82,8 +196,12 @@ bool ClientACL::clear() {
|
||||
if (_fs->exists("/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));
|
||||
num_clients = 0;
|
||||
saveTimestamps();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -110,11 +228,20 @@ ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms) {
|
||||
c = &clients[num_clients++];
|
||||
} else {
|
||||
c = oldest; // evict least active contact
|
||||
orphanTimestamp(c);
|
||||
}
|
||||
memset(c, 0, sizeof(*c));
|
||||
c->permissions = init_perms;
|
||||
c->id = id;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -124,6 +251,7 @@ bool ClientACL::applyPermissions(const mesh::LocalIdentity& self_id, const uint8
|
||||
c = getClient(pubkey, key_len);
|
||||
if (c == NULL) return false; // partial pubkey not found
|
||||
|
||||
orphanTimestamp(c);
|
||||
num_clients--; // delete from contacts[]
|
||||
int i = c - clients;
|
||||
while (i < num_clients) {
|
||||
|
||||
Reference in new issue
Block a user