From 5a83c230cd44769e1a50e0951f89478fb69336c0 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Wed, 2 Dec 2026 00:12:33 +0100 Subject: [PATCH] Repeater, room server, sensor: fix CLI reply buffer overflow, OTA bounds checks, and ACL slot rotation --- examples/simple_repeater/MyMesh.cpp | 5 +++-- src/helpers/ClientACL.cpp | 11 ++++++----- src/helpers/FwPatch.cpp | 4 ++-- src/helpers/FwUpdate.cpp | 5 +++-- src/helpers/NodeMaintenance.cpp | 1 + 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index a23e3828..5efe7e94 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -220,7 +220,7 @@ void MyMesh::formatNodesReply(char* reply, int max_len, uint16_t offset) { (int)n->snr, (int)n->rssi, (unsigned long)(now - n->heard_timestamp), (unsigned long)n->pkt_hash, pathhex, n->name); int ll = strlen(line); - if (i > offset && (dp - reply) + ll + 24 > max_len) break; // room for trailer; always emit >=1 + if ((dp - reply) + ll + 24 > max_len) break; // room for trailer; never overflow reply memcpy(dp, line, ll); dp += ll; } dp += sprintf(dp, "E,%d,%d", i, total); @@ -277,7 +277,7 @@ void MyMesh::formatRxLogReply(char* reply, int max_len, uint32_t cursor) { (unsigned long)r->pkt_hash, (unsigned)r->header, (int)r->snr, (int)r->rssi, pathhex); int ll = strlen(line); - if (i > 0 && (dp - reply) + ll + 40 > max_len) break; // room for trailer; always emit >=1 + if ((dp - reply) + ll + 40 > max_len) break; // room for trailer; never overflow reply memcpy(dp, line, ll); dp += ll; last = r->seq; } @@ -1934,6 +1934,7 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI) memcpy(reply, command, 3); // reflect the prefix back reply += 3; + reply_max = reply_max > 3 ? reply_max - 3 : 0; command += 3; } diff --git a/src/helpers/ClientACL.cpp b/src/helpers/ClientACL.cpp index 8a98af21..f495f9ef 100644 --- a/src/helpers/ClientACL.cpp +++ b/src/helpers/ClientACL.cpp @@ -63,9 +63,10 @@ void ClientACL::load(FILESYSTEM* fs, const mesh::LocalIdentity& self_id) { #define ACL_TS_RECORD (ACL_TS_PREFIX + 4) static const char* tsFilename(uint8_t slot) { return slot ? "/s_acl_ts1" : "/s_acl_ts0"; } +static uint8_t s_ts_buf[2 * MAX_CLIENTS * ACL_TS_RECORD + 8]; void ClientACL::loadTimestamps() { - static uint8_t buf[2 * MAX_CLIENTS * ACL_TS_RECORD + 8]; + uint8_t* buf = s_ts_buf; _num_ts_orphans = 0; _ts_seq = 0; _ts_slot = 0; @@ -78,7 +79,7 @@ void ClientACL::loadTimestamps() { File file = _fs->open(tsFilename(slot)); #endif if (!file) continue; - int len = file.read(buf, sizeof(buf)); + int len = file.read(buf, sizeof(s_ts_buf)); file.close(); if (len < 8 || (len - 8) % ACL_TS_RECORD != 0) continue; @@ -121,7 +122,7 @@ void ClientACL::loadTimestamps() { void ClientACL::saveTimestamps() { if (!_fs) return; - static uint8_t buf[2 * MAX_CLIENTS * ACL_TS_RECORD + 8]; + uint8_t* buf = s_ts_buf; int len = 0; for (int i = 0; i < num_clients; i++) { if (clients[i].saved_timestamp == 0) continue; @@ -142,10 +143,10 @@ void ClientACL::saveTimestamps() { File file = openWrite(_fs, tsFilename(_ts_slot)); if (file) { - file.write(buf, len); + size_t written = file.write(buf, len); file.close(); + if (written == (size_t)len) _ts_slot ^= 1; } - _ts_slot ^= 1; } void ClientACL::orphanTimestamp(const ClientInfo* c) { diff --git a/src/helpers/FwPatch.cpp b/src/helpers/FwPatch.cpp index f859ea5e..058fc3b8 100644 --- a/src/helpers/FwPatch.cpp +++ b/src/helpers/FwPatch.cpp @@ -62,7 +62,7 @@ static int in_shift_phase(void) { static int cb_read(void *arg, void *dst, uintptr_t src, size_t size) { fwp_job *j = (fwp_job *)arg; uint8_t *d = (uint8_t *)dst; - if (src + size > j->mem_size) return j->err = FWP_ERR_ARGS; + if (src > j->mem_size || size > j->mem_size - src) return j->err = FWP_ERR_ARGS; if (!in_shift_phase() && src < j->written_end) return j->err = FWP_ERR_OVERREAD; while (size > 0) { uintptr_t a = src; @@ -106,7 +106,7 @@ static int cb_erase(void *arg, uintptr_t addr, size_t size) { static int cb_write(void *arg, uintptr_t dst, void *src, size_t size) { fwp_job *j = (fwp_job *)arg; - if (j->page_addr == NO_PAGE || dst < j->page_addr || dst + size > j->page_addr + FWP_PAGE) + if (j->page_addr == NO_PAGE || dst < j->page_addr || size > FWP_PAGE - (dst - j->page_addr)) return j->err = FWP_ERR_ORDER; fwp_copy(&g_page[dst - j->page_addr], (const uint8_t *)src, (uint32_t)size); if (j->dry && !in_shift_phase()) j->sink(j->sink_ctx, (const uint8_t *)src, (uint32_t)size); diff --git a/src/helpers/FwUpdate.cpp b/src/helpers/FwUpdate.cpp index efe0da49..95f96d7d 100644 --- a/src/helpers/FwUpdate.cpp +++ b/src/helpers/FwUpdate.cpp @@ -743,9 +743,9 @@ void FwUpdate::cmdBegin(uint32_t sender_timestamp, const char *arg, char *reply) strcpy(reply, "ERR: fw begin "); return; } - if (stage + total > FW_OTA_STAGE_END) { + if (stage >= FW_OTA_STAGE_END || total > FW_OTA_STAGE_END - stage) { sprintf(reply, "ERR: %lu B does not fit, %lu free", (unsigned long)total, - (unsigned long)(FW_OTA_STAGE_END - stage)); + (unsigned long)(stage < FW_OTA_STAGE_END ? FW_OTA_STAGE_END - stage : 0)); return; } _stage = stage; @@ -975,6 +975,7 @@ void FwUpdate::cmdApply(uint32_t sender_timestamp, const char *arg, char *reply) _apply_force = force; _apply_err[0] = 0; _apply_at = millis() + 2000; // let the reply go out first + if (_apply_at == 0) _apply_at = 1; sprintf(reply, "OK applying %s in 2 s (batt %umV%s), back in ~20 s", _version, mv, _board.isExternalPowered() ? ", USB" : ""); } diff --git a/src/helpers/NodeMaintenance.cpp b/src/helpers/NodeMaintenance.cpp index b96d07b2..e902fd9c 100644 --- a/src/helpers/NodeMaintenance.cpp +++ b/src/helpers/NodeMaintenance.cpp @@ -323,6 +323,7 @@ bool NodeMaintenance::dfuCommand(uint32_t sender_timestamp, const char* command, return true; } _dfu_at = millis() + 700; + if (_dfu_at == 0) _dfu_at = 1; _dfu_mode = (strcmp(arg, "serial") == 0) ? 1 : (strcmp(arg, "ota") == 0 ? 2 : 0); #else strcpy(reply, "ERR: not an nRF52 board");