From e86ed1373b2ce4f2dfb4b839c8a8238931361ecb Mon Sep 17 00:00:00 2001 From: Mark Rose Date: Thu, 26 Nov 2026 00:02:20 -0600 Subject: [PATCH] Terminate contact names received from the app updateContactFromFrame() copies 32 name bytes from the companion frame without terminating them, so an app (or a corrupt frame) can store a name with no NUL. The UI then passes it to sprintf() into fixed-size origin buffers. Terminate the name on receipt and use snprintf() in both UIs. Co-Authored-By: Claude Opus 5.5 --- examples/companion_radio/MyMesh.cpp | 1 + src/helpers/ui/companion-new/UITask.cpp | 4 ++-- src/helpers/ui/companion-orig/UITask.cpp | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 9dcb9d05..2ca0c97c 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -202,6 +202,7 @@ void MyMesh::updateContactFromFrame(ContactInfo &contact, uint32_t& last_mod, co memcpy(contact.out_path, &frame[i], MAX_PATH_SIZE); i += MAX_PATH_SIZE; memcpy(contact.name, &frame[i], 32); + contact.name[sizeof(contact.name) - 1] = 0; // the app is not trusted to NUL-terminate i += 32; memcpy(&contact.last_advert_timestamp, &frame[i], 4); i += 4; diff --git a/src/helpers/ui/companion-new/UITask.cpp b/src/helpers/ui/companion-new/UITask.cpp index 420e9b7b..2427347a 100644 --- a/src/helpers/ui/companion-new/UITask.cpp +++ b/src/helpers/ui/companion-new/UITask.cpp @@ -739,9 +739,9 @@ public: auto p = &unread[head]; p->timestamp = _rtc->getCurrentTime(); if (path_count == 0xFF) { - sprintf(p->origin, "(D) %s:", from_name); + snprintf(p->origin, sizeof(p->origin), "(D) %s:", from_name); } else { - sprintf(p->origin, "(%d) %s:", (uint32_t) path_count, from_name); + snprintf(p->origin, sizeof(p->origin), "(%d) %s:", (uint32_t) path_count, from_name); } StrHelper::strncpy(p->msg, msg, sizeof(p->msg)); } diff --git a/src/helpers/ui/companion-orig/UITask.cpp b/src/helpers/ui/companion-orig/UITask.cpp index 2c5abff7..1eae0018 100644 --- a/src/helpers/ui/companion-orig/UITask.cpp +++ b/src/helpers/ui/companion-orig/UITask.cpp @@ -155,9 +155,9 @@ void UITask::onMessageRecv(mesh::Packet *pkt, const ContactInfo &from, uint8_t t uint8_t path_len = pkt->isRouteFlood() ? pkt->path_len : 0xFF; if (path_len == 0xFF) { - sprintf(_origin, "(F) %s", from.name); + snprintf(_origin, sizeof(_origin), "(F) %s", from.name); } else { - sprintf(_origin, "(%d) %s", (uint32_t) path_len, from.name); + snprintf(_origin, sizeof(_origin), "(%d) %s", (uint32_t) path_len, from.name); } StrHelper::strncpy(_msg, text, sizeof(_msg)); @@ -182,9 +182,9 @@ void UITask::onChannelMessageRecv(mesh::Packet *pkt, ChannelDetails& channel_det uint8_t path_len = pkt->isRouteFlood() ? pkt->path_len : 0xFF; if (path_len == 0xFF) { - sprintf(_origin, "(F) %s", channel_details.name); + snprintf(_origin, sizeof(_origin), "(F) %s", channel_details.name); } else { - sprintf(_origin, "(%d) %s", (uint32_t) path_len, channel_details.name); + snprintf(_origin, sizeof(_origin), "(%d) %s", (uint32_t) path_len, channel_details.name); } StrHelper::strncpy(_msg, text, sizeof(_msg));