Fix possible reply buffer overflow in 'sensor list'

'sensor list' appended 'name=value' lines into the 160-byte CLI reply with
unbounded sprintf, only checking the position before each write (< 134).
A single long value (or several medium ones) could write past the end of
the buffer.

Write each entry with snprintf against the remaining space, keep room for
the '... next:N' paging hint, and move entries that do not fit to the next
page. Also guard against NULL names/values from a sensor manager.
This commit is contained in:
Olexandr Tyshchenko committed 2026-10-29 23:49:49 +02:00
1 parent 04d72b1049
commit 017db61cae
1 file changed
+14 -6
+14 -6
View File
@@ -326,18 +326,26 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
if (start >= end) {
strcpy(reply, "no custom var");
} else {
const int reply_max = 160; // callers pass a 160-byte reply buffer
const int next_reserve = 16; // room for "... next:NNN"
sprintf(dp, "%d vars\n", end);
dp = strchr(dp, 0);
int i;
for (i = start; i < end && (dp-reply < 134); i++) {
sprintf(dp, "%s=%s\n",
_sensors->getSettingName(i),
_sensors->getSettingValue(i));
for (i = start; i < end; i++) {
const char* name = _sensors->getSettingName(i);
const char* value = _sensors->getSettingValue(i);
if (name == NULL) name = "?";
if (value == NULL) value = "";
int room = reply_max - next_reserve - (int)(dp - reply);
int need = snprintf(NULL, 0, "%s=%s\n", name, value);
if (need >= room && i > start) break; // continue on the next page
snprintf(dp, room, "%s=%s\n", name, value); // a single oversized entry is truncated
dp = strchr(dp, 0);
if (need >= room) { i++; break; }
}
if (i < end) {
sprintf(dp, "... next:%d", i);
} else {
snprintf(dp, reply_max - (int)(dp - reply), "... next:%d", i);
} else if (dp > reply && *(dp-1) == '\n') {
*(dp-1) = 0; // remove last CR
}
}