From 1614cb3e4851d6c9670d28124b551c364aaa67b0 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Wed, 19 Aug 2026 12:27:43 +0200 Subject: [PATCH] fix(contacts): zero ContactInfo before a partial fill ContactInfo is filled field by field in three places, and none of them writes every member: - CMD_ADD_UPDATE_CONTACT, for a contact the node does not know yet (companion_radio/MyMesh.cpp): the frame's lat/lon/lastmod are optional, and clients omit them when the contact has no location -- so gps_lat/gps_lon keep whatever was on the stack. - DataStore::loadContacts(), and the same loop in simple_secure_chat: the on-disk record carries no shared_secret/shared_secret_valid, so those two members are never written. For every contact, on every boot. shared_secret_valid is the dangerous one. getSharedSecret() computes the secret only while the flag is false, so a non-zero byte there makes it hand out an uninitialised buffer as the ECDH shared secret, and DMs to that contact go out encrypted with garbage -- no error, no log. populateContactFromAdvert() already memsets for exactly this reason; do the same at the other three sites. Found in the field: a room server whose advert carries no location showed up at 0.255433, 0.0 in two independent companion clients. 255433 is 0x0003E5C9, straight off the stack. Verified the advert itself is clean by decoding it in a third, unrelated tool. Co-Authored-By: Claude Opus 5 (1M context) --- examples/companion_radio/DataStore.cpp | 6 ++++++ examples/companion_radio/MyMesh.cpp | 7 +++++++ examples/simple_secure_chat/main.cpp | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/examples/companion_radio/DataStore.cpp b/examples/companion_radio/DataStore.cpp index 06c56a7a..29f99840 100644 --- a/examples/companion_radio/DataStore.cpp +++ b/examples/companion_radio/DataStore.cpp @@ -265,6 +265,12 @@ File file = openRead(_getContactsChannelsFS(), "/contacts3"); uint8_t pub_key[32]; uint8_t unused; + // The on-disk record has no shared_secret/shared_secret_valid, so those + // two members are never written by the reads below. Left indeterminate, + // a non-zero shared_secret_valid makes getSharedSecret() return an + // uninitialised secret for a contact loaded at boot. + memset(&c, 0, sizeof(c)); + bool success = (file.read(pub_key, 32) == 32); success = success && (file.read((uint8_t *)&c.name, 32) == 32); success = success && (file.read(&c.type, 1) == 1); diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 8739b08c..5498f412 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -1307,6 +1307,13 @@ void MyMesh::handleCmdFrame(size_t len) { writeOKFrame(); } else { ContactInfo contact; + // The frame's lat/lon/lastmod are optional, and clients omit them when the + // contact has no location -- so anything updateContactFromFrame() does not + // write stays as whatever was on the stack. That leaks garbage into + // gps_lat/gps_lon and, worse, into shared_secret_valid: a non-zero byte + // there makes getSharedSecret() hand out an uninitialised secret. + // populateContactFromAdvert() zeroes for the same reason. + memset(&contact, 0, sizeof(contact)); updateContactFromFrame(contact, last_mod, cmd_frame, len); contact.lastmod = last_mod; contact.sync_since = 0; diff --git a/examples/simple_secure_chat/main.cpp b/examples/simple_secure_chat/main.cpp index da42ddcb..990d7175 100644 --- a/examples/simple_secure_chat/main.cpp +++ b/examples/simple_secure_chat/main.cpp @@ -103,6 +103,10 @@ class MyMesh : public BaseChatMesh, ContactVisitor { uint8_t unused; uint32_t reserved; + // No shared_secret/shared_secret_valid in the on-disk record; without + // this the flag is indeterminate for every contact loaded at boot. + memset(&c, 0, sizeof(c)); + bool success = (file.read(pub_key, 32) == 32); success = success && (file.read((uint8_t *) &c.name, 32) == 32); success = success && (file.read(&c.type, 1) == 1);