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:
1 parent
bd4be350c5
commit
54b90cbdfa
1 file changed
+14
-6
@@ -326,18 +326,26 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
|||||||
if (start >= end) {
|
if (start >= end) {
|
||||||
strcpy(reply, "no custom var");
|
strcpy(reply, "no custom var");
|
||||||
} else {
|
} 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);
|
sprintf(dp, "%d vars\n", end);
|
||||||
dp = strchr(dp, 0);
|
dp = strchr(dp, 0);
|
||||||
int i;
|
int i;
|
||||||
for (i = start; i < end && (dp-reply < 134); i++) {
|
for (i = start; i < end; i++) {
|
||||||
sprintf(dp, "%s=%s\n",
|
const char* name = _sensors->getSettingName(i);
|
||||||
_sensors->getSettingName(i),
|
const char* value = _sensors->getSettingValue(i);
|
||||||
_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);
|
dp = strchr(dp, 0);
|
||||||
|
if (need >= room) { i++; break; }
|
||||||
}
|
}
|
||||||
if (i < end) {
|
if (i < end) {
|
||||||
sprintf(dp, "... next:%d", i);
|
snprintf(dp, reply_max - (int)(dp - reply), "... next:%d", i);
|
||||||
} else {
|
} else if (dp > reply && *(dp-1) == '\n') {
|
||||||
*(dp-1) = 0; // remove last CR
|
*(dp-1) = 0; // remove last CR
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user