Fix packet, delivery, clock and radio stability regressions

Reject truncated anonymous reply paths and advert metadata before copying,
while preserving timestamp-only empty-password logins. Limit room posting to
read-write/admin ACL roles, retain inbox frames until the requesting transport
accepts them, and lock raw LittleFS traversal against concurrent writes.

Separate ISR event generations from main-loop radio state. Confirm RX/TX IRQs,
detach concrete wrapper callbacks safely, and discard inconsistent LR2021 FIFO
snapshots before rearming. Preserve bridge RX frames through packet-pool
exhaustion and bound ESP-NOW retries and missing-callback recovery. Screen RTC
register layouts without losing DS1307 startup, and ignore poisoned future
contact timestamps during clock bootstrap.

Keep the nRF52 loop stack at 8 KiB while reducing the OTA listing call chain
from 7,832 to 6,056 static bytes. Retain all target names through lossless Full
Companion compression and the canonical size optimizer. Preserve constrained
STM32 filesystems and bind corrected ESP32 UART/OLED budgets to source size
assertions. Document the separately measured preexisting W12 package limits.

Adapted for this branch from meshcore-dev/MeshCore PRs #3521, #3340, #3362,
#2062, #3503, #3038, #3534, #3544 and #3512.

Validation: 1,829 native tests; sanitized parser, inbox, LittleFS, ACL, clock,
RTC, IRQ/FIFO and bridge fault tests; 332 additional Companion cases; real
RAK4631 Full and both repeater profiles, G2 Full/standard, V3 Full and both
constrained STM32 release recipes. Radio-family compile coverage continues
separately; W12 full/portable package guards remain enforced.
This commit is contained in:
mikecarper committed 2026-10-09 19:56:08 -07:00
1 parent f79a316da8
commit ddf625e9d0
60 files changed
+3647 -369

No files matched your search

+13 -2
View File
@@ -338,8 +338,19 @@ int _countLfsBlock(void *p, lfs_block_t block){
}
lfs_ssize_t _getLfsUsedBlockCount(FILESYSTEM* fs) {
LfsTraversalState state = {0, fs->_getFS()->cfg->block_count};
int err = lfs_traverse(fs->_getFS(), _countLfsBlock, &state);
if (fs == nullptr) return -1;
// Raw LittleFS traversal touches the same metadata/cache as file writes.
// Hold this filesystem's lock, separately from the physical flash lock.
fs->_lockFS();
lfs_t* raw_fs = fs->_getFS();
if (raw_fs == nullptr || raw_fs->cfg == nullptr
|| raw_fs->cfg->block_count == 0) {
fs->_unlockFS();
return -1;
}
LfsTraversalState state = {0, raw_fs->cfg->block_count};
int err = lfs_traverse(raw_fs, _countLfsBlock, &state);
fs->_unlockFS();
if (err) {
MESH_DEBUG_PRINTLN("ERROR: lfs_traverse() error: %d", err);
return -1;
+40 -14
View File
@@ -602,12 +602,15 @@ bool MyMesh::addToOfflineQueue(const uint8_t frame[], int len) {
return false;
}
int MyMesh::getFromOfflineQueue(uint8_t frame[]) {
if (offline_queue_len > 0) { // check offline queue
Frame& head = offlineQueueFrameAt(0);
size_t len = head.len; // take from top of queue
memcpy(frame, head.buf, len);
int MyMesh::peekOfflineQueue(uint8_t frame[]) {
if (!frame || offline_queue_len <= 0) return 0;
Frame& head = offlineQueueFrameAt(0);
memcpy(frame, head.buf, head.len);
return head.len;
}
void MyMesh::popOfflineQueue() {
if (offline_queue_len > 0) {
offline_queue_len--;
#if ONE_KEY_DM_SHARED_OFFLINE_QUEUE
for (int i = 0; i < offline_queue_len; ++i) {
@@ -623,9 +626,38 @@ int MyMesh::getFromOfflineQueue(uint8_t frame[]) {
#ifdef DISPLAY_CLASS
if (_ui) _ui->syncMessageQueue(offline_queue_len, 0);
#endif
return len;
}
return 0; // queue is empty
}
int MyMesh::getFromOfflineQueue(uint8_t frame[]) {
// Explicit OTA queue draining intentionally consumes frames without app
// delivery. Normal app downloads commit only after transport acceptance.
const int len = peekOfflineQueue(frame);
if (len > 0) popOfflineQueue();
return len;
}
void MyMesh::syncNextOfflineMessage() {
BaseSerialInterface* reply_route = _serial->captureReplyRoute();
const Frame* head = offline_queue_len > 0 ? &offlineQueueFrameAt(0) : nullptr;
size_t out_len = head ? head->len : 0;
const bool have_message = out_len > 0;
const uint8_t* reply_frame = out_frame;
if (have_message) {
// A full or disconnected requester must leave this message pending.
// Another connected transport cannot accept its reply on its behalf.
// Transports consume or copy the frame before returning, so the retained
// queue head can be sent directly and is only removed after acceptance.
if (!_serial->isReplyRouteAvailable(reply_route)) return;
reply_frame = head->buf;
} else {
out_frame[0] = RESP_CODE_NO_MORE_MESSAGES;
out_len = 1;
}
const size_t accepted = _serial->writeFrameToRoute(reply_route, reply_frame, out_len);
if (have_message && accepted == out_len) {
popOfflineQueue();
}
}
float MyMesh::getAirtimeBudgetFactor() const {
@@ -5908,13 +5940,7 @@ void MyMesh::handleCmdFrame(size_t len) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
}
} else if (cmd_frame[0] == CMD_SYNC_NEXT_MESSAGE) {
int out_len;
if ((out_len = getFromOfflineQueue(out_frame)) > 0) {
_serial->writeFrame(out_frame, out_len);
} else {
out_frame[0] = RESP_CODE_NO_MORE_MESSAGES;
_serial->writeFrame(out_frame, 1);
}
syncNextOfflineMessage();
} else if (cmd_frame[0] == CMD_SET_RADIO_PARAMS) {
if (len < 11) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
+8
View File
@@ -271,6 +271,11 @@ public:
void setNotificationOutputMute(mesh::notify::Output, bool) {}
#endif
bool handleNotificationCommand(const char* command, char* reply, size_t size);
#if defined(STM32_PLATFORM)
// Keep this large dispatcher separate from contact streaming on flash-
// constrained STM32 targets; merging their branches increases code size.
__attribute__((noinline))
#endif
void handleCmdFrame(size_t len);
bool advert();
void enterCLIRescue();
@@ -496,7 +501,10 @@ private:
void stopContactsIterator();
bool updateContactFromFrame(ContactInfo &contact, uint32_t& last_mod, const uint8_t *frame, int len);
bool addToOfflineQueue(const uint8_t frame[], int len);
int peekOfflineQueue(uint8_t frame[]);
void popOfflineQueue();
int getFromOfflineQueue(uint8_t frame[]);
void syncNextOfflineMessage();
int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) override {
return _store->getBlobByKey(key, key_len, dest_buf);
}
+22 -13
View File
@@ -676,12 +676,18 @@ static int cmp_neighbours_weakest_to_strongest(const void* a, const void* b) {
return 0;
}
uint8_t MyMesh::handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data) {
static bool hasCompleteAnonReplyPath(const uint8_t* data, size_t data_len) {
if (data == nullptr || data_len == 0 || !mesh::Packet::isValidPathLen(data[0])) return false;
const size_t path_bytes = (data[0] & 63U) * ((data[0] >> 6) + 1U);
return path_bytes <= data_len - 1;
}
uint8_t MyMesh::handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp,
const uint8_t* data, size_t data_len) {
if (!hasCompleteAnonReplyPath(data, data_len)) return 0;
if (anon_limiter.allow(rtc_clock.getCurrentTime())) {
// request data has: {reply-path-len}{reply-path}
reply_path_len = *data++;
if (!mesh::Packet::isValidPathLen(reply_path_len)) return 0; // reject - bad encoding
mesh::Packet::writePath(reply_path, data, reply_path_len);
// data += (uint8_t)reply_path_len * reply_path_hash_size;
@@ -696,12 +702,12 @@ uint8_t MyMesh::handleAnonRegionsReq(const mesh::Identity& sender, uint32_t send
return 0;
}
uint8_t MyMesh::handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data) {
uint8_t MyMesh::handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender_timestamp,
const uint8_t* data, size_t data_len) {
if (!hasCompleteAnonReplyPath(data, data_len)) return 0;
if (anon_limiter.allow(rtc_clock.getCurrentTime())) {
// request data has: {reply-path-len}{reply-path}
reply_path_len = *data++;
if (!mesh::Packet::isValidPathLen(reply_path_len)) return 0; // reject - bad encoding
mesh::Packet::writePath(reply_path, data, reply_path_len);
// data += (uint8_t)reply_path_len * reply_path_hash_size;
@@ -715,12 +721,12 @@ uint8_t MyMesh::handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender
return 0;
}
uint8_t MyMesh::handleAnonClockReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data) {
uint8_t MyMesh::handleAnonClockReq(const mesh::Identity& sender, uint32_t sender_timestamp,
const uint8_t* data, size_t data_len) {
if (!hasCompleteAnonReplyPath(data, data_len)) return 0;
if (anon_limiter.allow(rtc_clock.getCurrentTime())) {
// request data has: {reply-path-len}{reply-path}
reply_path_len = *data++;
if (!mesh::Packet::isValidPathLen(reply_path_len)) return 0; // reject - bad encoding
mesh::Packet::writePath(reply_path, data, reply_path_len);
// data += (uint8_t)reply_path_len * reply_path_hash_size;
@@ -2679,9 +2685,12 @@ mesh::DispatcherAction MyMesh::onRecvPacket(mesh::Packet* pkt) {
void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const mesh::Identity &sender,
uint8_t *data, size_t len) {
if (packet == nullptr) return;
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin
// client (unknown at this stage)
if (len < 4) {
// A timestamp-only request is a valid empty-password login. The bounded
// terminator below supplies data[4]; typed requests still need their path.
if (data == nullptr || len < 4 || len >= MAX_PACKET_PAYLOAD) {
MESH_DEBUG_PRINTLN("Rejected incomplete anonymous request");
return;
}
@@ -2695,11 +2704,11 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
if (data[4] == 0 || data[4] >= ' ') { // is password, ie. a login request
reply_len = handleLoginReq(sender, secret, timestamp, &data[4], packet->isRouteFlood());
} else if (data[4] == ANON_REQ_TYPE_REGIONS && packet->isRouteDirect()) {
reply_len = handleAnonRegionsReq(sender, timestamp, &data[5]);
reply_len = handleAnonRegionsReq(sender, timestamp, &data[5], len - 5);
} else if (data[4] == ANON_REQ_TYPE_OWNER && packet->isRouteDirect()) {
reply_len = handleAnonOwnerReq(sender, timestamp, &data[5]);
reply_len = handleAnonOwnerReq(sender, timestamp, &data[5], len - 5);
} else if (data[4] == ANON_REQ_TYPE_BASIC && packet->isRouteDirect()) {
reply_len = handleAnonClockReq(sender, timestamp, &data[5]);
reply_len = handleAnonClockReq(sender, timestamp, &data[5], len - 5);
} else {
reply_len = 0; // unknown/invalid request type
}
+3 -3
View File
@@ -781,9 +781,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr,
int16_t rssi);
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood);
uint8_t handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
uint8_t handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
uint8_t handleAnonClockReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
uint8_t handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data, size_t data_len);
uint8_t handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data, size_t data_len);
uint8_t handleAnonClockReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data, size_t data_len);
int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len,
size_t reply_capacity = MAX_PACKET_PAYLOAD - CIPHER_MAC_SIZE - (CIPHER_BLOCK_SIZE - 1));
mesh::Packet* createSelfAdvert();
+22 -28
View File
@@ -913,36 +913,30 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
bool send_ack = false;
if (flags == TXT_TYPE_PLAIN) {
const bool is_guest =
(client->permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST;
if (is_guest) {
if (sender_timestamp < client->extra.room.last_post_timestamp) {
MESH_DEBUG_PRINTLN("onPeerDataRecv: stale room post detected");
return;
}
client->extra.room.last_post_timestamp = sender_timestamp;
} else {
uint8_t message_fingerprint[MAX_HASH_SIZE];
mesh::Utils::sha256(message_fingerprint, sizeof(message_fingerprint),
client->id.pub_key, PUB_KEY_SIZE,
(const uint8_t*)text, text_len);
const auto replay_decision = recent_room_posts.classifyAndRemember(
message_fingerprint, sender_timestamp,
client->extra.room.last_post_timestamp);
const uint8_t role = client->permissions & PERM_ACL_ROLE_MASK;
// Management roles grant their specific CLI permissions, not room posting.
if (role != PERM_ACL_READ_WRITE && role != PERM_ACL_ADMIN) return;
using ReplayDecision =
mesh::LogicalMessageCache<ROOM_MESSAGE_CACHE_SIZE>::ReplayDecision;
if (replay_decision == ReplayDecision::StaleOrMismatched) {
MESH_DEBUG_PRINTLN("onPeerDataRecv: stale or mismatched room post detected");
return;
}
if (replay_decision == ReplayDecision::NewMessage) {
addPost(client, text);
}
// Exact retries are ACKed again regardless of whether newer posts have
// advanced this client's room-post replay timestamp.
send_ack = true;
uint8_t message_fingerprint[MAX_HASH_SIZE];
mesh::Utils::sha256(message_fingerprint, sizeof(message_fingerprint),
client->id.pub_key, PUB_KEY_SIZE,
(const uint8_t*)text, text_len);
const auto replay_decision = recent_room_posts.classifyAndRemember(
message_fingerprint, sender_timestamp,
client->extra.room.last_post_timestamp);
using ReplayDecision =
mesh::LogicalMessageCache<ROOM_MESSAGE_CACHE_SIZE>::ReplayDecision;
if (replay_decision == ReplayDecision::StaleOrMismatched) {
MESH_DEBUG_PRINTLN("onPeerDataRecv: stale or mismatched room post detected");
return;
}
if (replay_decision == ReplayDecision::NewMessage) {
addPost(client, text);
}
// Exact retries are ACKed again regardless of whether newer posts have
// advanced this client's room-post replay timestamp.
send_ack = true;
} else { // TXT_TYPE_CLI_DATA or TXT_TYPE_CLI_COMMAND
uint32_t request_id = sender_timestamp;
mesh::RemoteCliRequest::parse(data, len, 5, request_id);