Retry identity startup and harden replay state and permissions
Read identities up to three times before allowing replacement, publish only complete reads, and require new startup keys to be saved with bounded retries. Use authoritative replay-file metadata throughout persistence and clock recovery. Validate setperm input before narrowing or mutating roles, and add production-path regression coverage.
This commit is contained in:
1 parent
24b1b1167f
commit
b847b46ef7
21 files changed
+272
-73
No files matched your search
@@ -283,7 +283,9 @@ static bool readClientLoginReplayCeiling(
|
||||
bool* found) {
|
||||
*ceiling = 0;
|
||||
*found = false;
|
||||
if (!fs->exists(mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH)) return true;
|
||||
bool present = false;
|
||||
if (!mesh::filePresence(fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH, present)) return false;
|
||||
if (!present) return true;
|
||||
if (!validateLoginReplayFileIntegrity(
|
||||
fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH)) {
|
||||
return false;
|
||||
@@ -338,7 +340,9 @@ static bool writeClientLoginReplayCeiling(
|
||||
mesh::ClientLoginReplayReservationAction action) {
|
||||
if (action == mesh::ClientLoginReplayReservationAction::None) return true;
|
||||
#if defined(NRF52_PLATFORM)
|
||||
if (fs->exists(mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH)
|
||||
bool primary_exists = false;
|
||||
if (!mesh::filePresence(fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH, primary_exists)) return false;
|
||||
if (primary_exists
|
||||
&& !validateLoginReplayFileIntegrity(
|
||||
fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH)) {
|
||||
return false;
|
||||
@@ -352,7 +356,9 @@ static bool writeClientLoginReplayCeiling(
|
||||
|
||||
File source = mesh::emptyFile(fs);
|
||||
size_t record_count = 0;
|
||||
if (fs->exists(mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH)) {
|
||||
bool source_exists = false;
|
||||
if (!mesh::filePresence(fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH, source_exists)) return false;
|
||||
if (source_exists) {
|
||||
source = openRead(fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH);
|
||||
// A previously present image becoming unreadable is not a first login.
|
||||
// Check again after opening so disappearance/truncation cannot underflow
|
||||
@@ -487,7 +493,11 @@ bool ClientACL::clampLoginReplayTimestamps(
|
||||
if (_fs == NULL || !login_replay_store_available || now == 0) return false;
|
||||
|
||||
ClientLoginReplayClampResult pending = {};
|
||||
if (_fs->exists(mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH)) {
|
||||
bool primary = false, backup = false, temp = false;
|
||||
if (!mesh::filePresence(_fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH, primary)
|
||||
|| !mesh::filePresence(_fs, mesh::CLIENT_LOGIN_REPLAY_BACKUP_PATH, backup)
|
||||
|| !mesh::filePresence(_fs, mesh::CLIENT_LOGIN_REPLAY_TEMP_PATH, temp)) return false;
|
||||
if (primary) {
|
||||
File source = openRead(_fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH);
|
||||
uint32_t original_crc = 0;
|
||||
const bool valid = copyClampedLoginReplay(
|
||||
@@ -546,8 +556,7 @@ bool ClientACL::clampLoginReplayTimestamps(
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else if (_fs->exists(mesh::CLIENT_LOGIN_REPLAY_BACKUP_PATH)
|
||||
|| _fs->exists(mesh::CLIENT_LOGIN_REPLAY_TEMP_PATH)) {
|
||||
} else if (backup || temp) {
|
||||
// Recovery is performed by load(). Do not mistake an interrupted/corrupt
|
||||
// transaction for a never-created store during this explicit operation.
|
||||
login_replay_store_available = false;
|
||||
@@ -585,10 +594,11 @@ void ClientACL::load(FILESYSTEM* fs, const mesh::LocalIdentity& self_id) {
|
||||
// rename. The live image remains authoritative.
|
||||
mesh::removeClientLoginReplayArtifact(
|
||||
_fs, mesh::CLIENT_LOGIN_REPLAY_TEMP_PATH);
|
||||
bool replay_exists = false;
|
||||
login_replay_store_available =
|
||||
!_fs->exists(mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH)
|
||||
|| validateLoginReplayFileIntegrity(
|
||||
_fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH);
|
||||
mesh::filePresence(_fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH, replay_exists)
|
||||
&& (!replay_exists || validateLoginReplayFileIntegrity(
|
||||
_fs, mesh::CLIENT_LOGIN_REPLAY_PRIMARY_PATH));
|
||||
#else
|
||||
login_replay_store_available = mesh::recoverClientLoginReplayFiles(
|
||||
_fs, validateLoginReplayFileIntegrity);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "FilePresence.h"
|
||||
|
||||
namespace mesh {
|
||||
|
||||
@@ -162,14 +163,18 @@ inline bool validateClientLoginReplayImage(const uint8_t* image,
|
||||
|
||||
template <typename Filesystem>
|
||||
bool removeClientLoginReplayArtifact(Filesystem* fs, const char* path) {
|
||||
if (!fs->exists(path)) return true;
|
||||
fs->remove(path);
|
||||
return !fs->exists(path);
|
||||
bool present = false;
|
||||
if (!filePresence(fs, path, present)) return false;
|
||||
if (!present) return true;
|
||||
return fs->remove(path) && filePresence(fs, path, present) && !present;
|
||||
}
|
||||
|
||||
template <typename Filesystem, typename Validator>
|
||||
bool recoverClientLoginReplayFiles(Filesystem* fs, Validator is_valid) {
|
||||
if (fs->exists(CLIENT_LOGIN_REPLAY_PRIMARY_PATH)
|
||||
bool primary = false, backup = false;
|
||||
if (!filePresence(fs, CLIENT_LOGIN_REPLAY_PRIMARY_PATH, primary)
|
||||
|| !filePresence(fs, CLIENT_LOGIN_REPLAY_BACKUP_PATH, backup)) return false;
|
||||
if (primary
|
||||
&& is_valid(fs, CLIENT_LOGIN_REPLAY_PRIMARY_PATH)) {
|
||||
// The validated primary is already authoritative. Cleanup failure must
|
||||
// not disable replay protection or make committed state appear absent.
|
||||
@@ -177,7 +182,7 @@ bool recoverClientLoginReplayFiles(Filesystem* fs, Validator is_valid) {
|
||||
removeClientLoginReplayArtifact(fs, CLIENT_LOGIN_REPLAY_BACKUP_PATH);
|
||||
return true;
|
||||
}
|
||||
if (fs->exists(CLIENT_LOGIN_REPLAY_BACKUP_PATH)
|
||||
if (backup
|
||||
&& is_valid(fs, CLIENT_LOGIN_REPLAY_BACKUP_PATH)) {
|
||||
if (!removeClientLoginReplayArtifact(fs,
|
||||
CLIENT_LOGIN_REPLAY_PRIMARY_PATH)
|
||||
@@ -190,8 +195,7 @@ bool recoverClientLoginReplayFiles(Filesystem* fs, Validator is_valid) {
|
||||
}
|
||||
// A never-created store is the upgrade/first-boot case. An invalid live or
|
||||
// backup image is different: retain it for diagnosis and fail closed.
|
||||
return !fs->exists(CLIENT_LOGIN_REPLAY_PRIMARY_PATH)
|
||||
&& !fs->exists(CLIENT_LOGIN_REPLAY_BACKUP_PATH)
|
||||
return !primary && !backup
|
||||
&& removeClientLoginReplayArtifact(fs,
|
||||
CLIENT_LOGIN_REPLAY_TEMP_PATH);
|
||||
}
|
||||
@@ -204,9 +208,9 @@ bool publishClientLoginReplayTemp(Filesystem* fs,
|
||||
removeClientLoginReplayArtifact(fs, CLIENT_LOGIN_REPLAY_TEMP_PATH);
|
||||
return false;
|
||||
}
|
||||
if (fs->exists(CLIENT_LOGIN_REPLAY_BACKUP_PATH)) return false;
|
||||
|
||||
const bool had_primary = fs->exists(CLIENT_LOGIN_REPLAY_PRIMARY_PATH);
|
||||
bool backup = false, had_primary = false;
|
||||
if (!filePresence(fs, CLIENT_LOGIN_REPLAY_BACKUP_PATH, backup) || backup
|
||||
|| !filePresence(fs, CLIENT_LOGIN_REPLAY_PRIMARY_PATH, had_primary)) return false;
|
||||
if (had_primary
|
||||
&& !fs->rename(CLIENT_LOGIN_REPLAY_PRIMARY_PATH,
|
||||
CLIENT_LOGIN_REPLAY_BACKUP_PATH)) {
|
||||
|
||||
@@ -24,47 +24,55 @@ bool IdentityStore::recover(const char* name) {
|
||||
}
|
||||
|
||||
bool IdentityStore::load(const char *name, mesh::LocalIdentity& id) {
|
||||
if (!recover(name)) return false;
|
||||
bool loaded = false;
|
||||
char filename[40];
|
||||
sprintf(filename, "%s/%s.id", _dir, name);
|
||||
if (_fs->exists(filename)) {
|
||||
#if defined(RP2040_PLATFORM)
|
||||
File file = _fs->open(filename, "r");
|
||||
#else
|
||||
File file = _fs->open(filename);
|
||||
#endif
|
||||
if (file) {
|
||||
loaded = id.readFrom(file);
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
return loaded;
|
||||
return load(name, id, nullptr, 0);
|
||||
}
|
||||
|
||||
bool IdentityStore::load(const char *name, mesh::LocalIdentity& id, char display_name[], int max_name_sz) {
|
||||
if (!recover(name)) return false;
|
||||
bool loaded = false;
|
||||
char filename[40];
|
||||
sprintf(filename, "%s/%s.id", _dir, name);
|
||||
if (_fs->exists(filename)) {
|
||||
if (snprintf(filename, sizeof(filename), "%s/%s.id", _dir, name)
|
||||
>= (int)sizeof(filename)) return false;
|
||||
for (unsigned attempt = 0; attempt < IO_ATTEMPTS; ++attempt) {
|
||||
if (!recover(name)) continue;
|
||||
bool present = false;
|
||||
if (!mesh::filePresence(_fs, filename, present)) continue;
|
||||
if (!present) return false;
|
||||
#if defined(RP2040_PLATFORM)
|
||||
File file = _fs->open(filename, "r");
|
||||
#else
|
||||
File file = _fs->open(filename);
|
||||
#endif
|
||||
if (file) {
|
||||
loaded = id.readFrom(file);
|
||||
|
||||
int n = max_name_sz; // up to 32 bytes
|
||||
if (n > 32) n = 32;
|
||||
file.read((uint8_t *) display_name, n);
|
||||
display_name[n - 1] = 0; // ensure null terminator
|
||||
|
||||
if (!file || file.isDirectory()) {
|
||||
file.close();
|
||||
continue;
|
||||
}
|
||||
mesh::LocalIdentity loaded;
|
||||
if (!loaded.readFrom(file)) {
|
||||
file.close();
|
||||
continue;
|
||||
}
|
||||
// The legacy display name is optional. A key-only image remains valid;
|
||||
// neither a short name nor a partial key may leak into the caller's state.
|
||||
if (display_name != nullptr && max_name_sz > 0) {
|
||||
const int n = max_name_sz > 32 ? 32 : max_name_sz;
|
||||
char loaded_name[32];
|
||||
if (file.read(reinterpret_cast<uint8_t*>(loaded_name), n) == n) {
|
||||
loaded_name[n - 1] = 0;
|
||||
memcpy(display_name, loaded_name, n);
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
id = loaded;
|
||||
return true;
|
||||
}
|
||||
return loaded;
|
||||
// Callers may provision a replacement after bounded recovery is exhausted.
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IdentityStore::saveWithRetry(const char* name, const mesh::LocalIdentity& id) {
|
||||
for (unsigned attempt = 0; attempt < IO_ATTEMPTS; ++attempt) {
|
||||
if (save(name, id)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IdentityStore::save(const char *name, const mesh::LocalIdentity& id) {
|
||||
|
||||
@@ -15,6 +15,7 @@ class IdentityStore {
|
||||
FILESYSTEM* _fs;
|
||||
const char* _dir;
|
||||
public:
|
||||
static constexpr unsigned IO_ATTEMPTS = 3;
|
||||
IdentityStore(FILESYSTEM& fs, const char* dir): _fs(&fs), _dir(dir) { }
|
||||
|
||||
void begin() {
|
||||
@@ -25,5 +26,7 @@ public:
|
||||
bool load(const char *name, mesh::LocalIdentity& id);
|
||||
bool load(const char *name, mesh::LocalIdentity& id, char display_name[], int max_name_sz);
|
||||
bool save(const char *name, const mesh::LocalIdentity& id);
|
||||
// Startup must not run with a new identity that was never made durable.
|
||||
bool saveWithRetry(const char *name, const mesh::LocalIdentity& id);
|
||||
bool save(const char *name, const mesh::LocalIdentity& id, const char display_name[]);
|
||||
};
|
||||
Reference in new issue
Block a user