Harden management report persistence and CLI input handling

This commit is contained in:
mikecarper committed 2026-10-03 18:26:26 -07:00
1 parent ec0ebeb981
commit 30b18beecd
8 files changed
+620 -18

No files matched your search

+2
View File
@@ -850,6 +850,8 @@ public:
bool resolveDataTxScope(TransportKey& scope, char* resolved_name = nullptr,
size_t resolved_name_size = 0,
bool* ambiguous = nullptr) const;
// True if a saved shared route already takes precedence or the legacy path
// was committed. False means migration failed and reporting must stop.
bool adoptLegacyDataTxPath(const uint8_t* path, uint8_t path_len);
bool setDataTxPath(const char* spec, char* reply, size_t reply_size);
bool saveCommonPrefs();
+20 -5
View File
@@ -89,7 +89,10 @@ static bool parseDataPath(char* raw, uint8_t path[MAX_PATH_SIZE],
char* token = spec;
while (token && *token) {
char* comma = strchr(token, ',');
if (comma) *comma = 0;
if (comma) {
if (!comma[1]) { error = "Err - missing path hash after comma"; return false; }
*comma = 0;
}
token = trimDataRoute(token);
const size_t chars = strlen(token);
const uint8_t token_width = uint8_t(chars / 2);
@@ -275,8 +278,11 @@ bool CommonCLI::resolveDataTxScope(TransportKey& scope, char* name,
}
bool CommonCLI::adoptLegacyDataTxPath(const uint8_t* path, uint8_t path_len) {
if (!_data_route || !_data_route->healthy || _data_route->persisted
|| path_len == OUT_PATH_UNKNOWN || !mesh::Packet::isValidPathLen(path_len)
if (!_data_route || !_data_route->healthy) return false;
// A saved shared route is authoritative, including an explicit "none".
// This is successful migration, not a storage failure to fault on.
if (_data_route->persisted) return true;
if (path_len == OUT_PATH_UNKNOWN || !mesh::Packet::isValidPathLen(path_len)
|| ((path_len & 63) && !path)) return false;
const uint8_t previous_len = _data_route->path_len;
uint8_t previous[MAX_PATH_SIZE]; memcpy(previous, _data_route->path, sizeof(previous));
@@ -373,7 +379,10 @@ bool CommonCLI::handleManagementCommand(char* command, char* reply) {
}
if (!strncmp(command, "set mgmt.path ", 14)) {
char alias[160];
snprintf(alias, sizeof(alias), "set data.tx path %s", command + 14);
const int length = snprintf(alias, sizeof(alias), "set data.tx path %s", command + 14);
if (length < 0 || size_t(length) >= sizeof(alias)) {
strcpy(reply, "Err - data.tx path too long"); return true;
}
return handleDataTxCommand(alias, reply);
}
if (strcmp(command, "get mgmt") && strncmp(command, "get mgmt.", 9)
@@ -383,7 +392,13 @@ bool CommonCLI::handleManagementCommand(char* command, char* reply) {
*_management_mesh, *_board, *_sensors, *_acl, *_prefs, *_callbacks, *this,
_management_fs);
}
if (!_management) { strcpy(reply, "ERR: management unavailable"); return true; }
if (!_management) {
// No reporter can consume/wipe the value when initialization or its
// nothrow allocation failed. Apply the same recognized-password hygiene.
if (!strncmp(command, "set mgmt.password ", 18))
mesh::management::erase(command + 18, strlen(command + 18));
strcpy(reply, "ERR: management unavailable"); return true;
}
if (!_management->command(command, reply, 160)) strcpy(reply, "ERR: unknown management setting");
return true;
}
+32 -11
View File
@@ -3,7 +3,6 @@
#include "FileRead.h"
#include "PersistentStoreFormat.h"
#include <new>
#include <stdlib.h>
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#include "AtomicFileWriter.h"
#else
@@ -22,6 +21,19 @@ namespace mesh {
using namespace management;
static constexpr char STATE_FILE[] = "/management";
static constexpr size_t STATE_SIZE = 120;
// Return zero for malformed/out-of-range input before any integer can wrap.
// Signs, whitespace and suffixes are not decimal day counts; leading zeroes
// remain valid. All management intervals have the same upper bound.
static unsigned parseDays(const char* text) {
unsigned days = 0;
if (!*text) return 0;
do {
if (*text < '0' || *text > '9') return 0;
days = days * 10 + (*text++ - '0');
if (days > 90) return 0;
} while (*text);
return days;
}
struct ManagementReporter::Working {
History history;
Extrema during_report;
@@ -70,9 +82,13 @@ bool ManagementReporter::load() {
!direct_valid || !flood_valid || (b[4] && (!b[6] || (!b[5] && !b[7]))) ||
read32(b + 116) != storage::updateCRC32(0xffffffff, b, 116) ||
read32(b + 104) > 90 * DAY + 3600 || read32(b + 108) > 90 * DAY + 3600) continue;
// Do not discard the only durable legacy route in a later MGC2 checkpoint
// unless the shared route was successfully adopted (or already saved).
if (legacy && b[7] != OUT_PATH_UNKNOWN && !common_cli.adoptLegacyDataTxPath(b + 8, b[7])) {
erase(b, sizeof(b)); return false;
}
enabled = b[4]; direct_days = b[5]; keyed = b[6];
flood_days = legacy ? (direct_days < 21 ? 21 : direct_days) : b[7];
if (legacy && b[7] != OUT_PATH_UNKNOWN) common_cli.adoptLegacyDataTxPath(b + 8, b[7]);
memcpy(key, b + 72, 32);
schedule.direct = read32(b + 104); schedule.flood = read32(b + 108);
if (!direct_days) schedule.direct = 0;
@@ -320,9 +336,10 @@ bool ManagementReporter::command(char* command, char* reply, size_t size) {
return true;
}
++value;
if (!healthy) { snprintf(reply, size, "ERR: management state fault; repair storage/reboot first"); return true; }
// Failed persistence stops reporting; no uncommitted state is transmitted.
cancel();
if (!healthy) {
if (!strncmp(command, "set mgmt.password ", 18)) erase(value, strlen(value));
snprintf(reply, size, "ERR: management state fault; repair storage/reboot first"); return true;
}
if (!strncmp(command, "set mgmt.password ", 18)) {
const size_t len = strlen(value);
if (len < 12 || len > 96) { snprintf(reply, size, "ERR: password must be 12..96 bytes"); erase(value, len); return true; }
@@ -352,9 +369,9 @@ bool ManagementReporter::command(char* command, char* reply, size_t size) {
}
direct_days = 0; schedule.direct = 0;
} else {
char* end; const unsigned long n = strtoul(value, &end, 10);
const unsigned n = parseDays(value);
const uint8_t* route_path = nullptr; uint8_t route_path_len = OUT_PATH_UNKNOWN;
if (!*value || *end || !Schedule::validDirect(n)
if (!Schedule::validDirect(n)
|| !common_cli.getDataTxPath(route_path, route_path_len)) {
snprintf(reply, size, "ERR: direct needs data.tx path and 5..90 days"); return true;
}
@@ -367,19 +384,19 @@ bool ManagementReporter::command(char* command, char* reply, size_t size) {
}
flood_days = 0; schedule.flood = 0;
} else {
char* end; const unsigned long n = strtoul(value, &end, 10);
const unsigned n = parseDays(value);
TransportKey scope;
if (!*value || *end || !Schedule::validFlood(n)
if (!Schedule::validFlood(n)
|| !common_cli.resolveDataTxScope(scope)) {
snprintf(reply, size, "ERR: flood needs data.tx region and 21..90 days"); return true;
}
flood_days = n; schedule.flood = flood_days * DAY;
}
} else if (!strncmp(command, "set mgmt.interval ", 18)) {
char* end; const unsigned long n = strtoul(value, &end, 10);
const unsigned n = parseDays(value);
const uint8_t* route_path = nullptr; uint8_t route_path_len = OUT_PATH_UNKNOWN;
TransportKey scope;
if (!*value || *end || !Schedule::validDirect(n)
if (!Schedule::validDirect(n)
|| !common_cli.getDataTxPath(route_path, route_path_len)
|| !common_cli.resolveDataTxScope(scope)) {
snprintf(reply, size, "ERR: interval needs path, region and 5..90 days"); return true;
@@ -388,6 +405,10 @@ bool ManagementReporter::command(char* command, char* reply, size_t size) {
direct_days = n; flood_days = n < 21 ? 21 : n;
schedule.direct = direct_days * DAY; schedule.flood = flood_days * DAY;
} else { snprintf(reply, size, "ERR: unknown management setting"); return true; }
// Only accepted changes abandon a frozen report. Invalid commands must not
// discard its remaining pages after the multi-day budget was reserved.
// Failed persistence still stops reporting; no uncommitted state leaves.
cancel();
if (!save()) { healthy = false; snprintf(reply, size, "ERR: save failed; reporting stopped"); return true; }
if (!enabled) { delete work; work = nullptr; }
snprintf(reply, size, "OK"); return true;