fix: bound CLI replies to the real 160-byte reply buffer

All radd() reply writers were bounded to MAX_PACKET_PAYLOAD (184), but the
actual CLI reply buffers are 160 B (serial/ethernet reply[160]) and 161 B
(mesh path: &temp[5] of temp[166]). With 9+ channels, 'filter chan'/'filter
chan list' wrote ~23 bytes past temp[166] and rebooted the repeater; the
same class of bug existed in the token echoes 'Err - expected key=value'
and 'Err - unknown param', reachable via a long mesh admin command.

- CliUtil.h: add CLI_REPLY_MAX (160); bound all radd() callers to it
  (PacketFilter.cpp x5, BatteryGate.cpp x1)
- PacketFilter.cpp: snprintf-bound the two user-token error echoes
- chan list format trimmed to '<idx>:<name>:<hash>' so a full 16-slot
  store fits in one reply
- filter stats reordered (globals first, per-rule hits last) with
  shortened labels so tail truncation sacrifices hits detail, not the
  summary counters
- tests: pin replies to the real buffer for chan list and token-echo
  errors; update stats/chan-list format assertions
This commit is contained in:
Jason Huebert committed 2026-09-23 15:29:38 -05:00
1 parent 8b52d7e2f0
commit ad3872d3ef
4 files changed
+58 -27

No files matched your search

+1 -1
View File
@@ -157,7 +157,7 @@ static bool parseMilliVolts(const char* tok, bool* numeric, uint16_t* out) {
static void cliStatus(BatteryGate& gate, mesh::MainBoard& board, char* reply) {
char* out = reply;
int remain = MAX_PACKET_PAYLOAD;
int remain = CLI_REPLY_MAX;
radd(&out, &remain, "batt %umV; gate %s", board.getBattMilliVolts(),
gate.isEnabled() ? "on" : "off");
if (gate.isEnabled()) {
+5
View File
@@ -35,6 +35,11 @@ inline char* nextToken(char** p) {
return t;
}
// all CLI reply buffers are 160 B: main.cpp uses char reply[160] (serial and
// ethernet), and the mesh remote-CLI hands out &temp[5] of a uint8_t temp[166].
// Reply writers must bound to this, never MAX_PACKET_PAYLOAD.
#define CLI_REPLY_MAX 160
// bounded reply append (CLI reply buffer is 160 bytes)
inline void radd(char** out, int* remain, const char* fmt, ...) {
if (*remain <= 0) return;
+13 -13
View File
@@ -943,7 +943,7 @@ static bool addRuleParam(FilterRules& filter, FilterRule* r, RegionMap* regions,
r->throttle = (uint16_t)v;
return true;
}
sprintf(reply, "Err - unknown param '%s'", key);
snprintf(reply, CLI_REPLY_MAX, "Err - unknown param '%s'", key); // key is a raw command token
return false;
}
@@ -955,7 +955,7 @@ static void cliStatus(FilterRules& filter, char* reply) {
if (filter.getChannel(i)->name[0] != 0) used++;
}
char* out = reply;
int remain = MAX_PACKET_PAYLOAD;
int remain = CLI_REPLY_MAX;
radd(&out, &remain, "%s; rules %d/%d; chans %d/%d; ratelimit advert %uh; cache %d/%d",
filter.isEnabled() ? "on" : "off", filter.getNumRules(), FILTER_MAX_RULES,
used, FILTER_MAX_CHANNELS, filter.getAdvertRatelimit(),
@@ -966,15 +966,14 @@ static void cliStatus(FilterRules& filter, char* reply) {
static void cliChanList(FilterRules& filter, char* reply) {
char* out = reply;
int remain = MAX_PACKET_PAYLOAD;
int remain = CLI_REPLY_MAX;
if (filter.getNumChannels() == 0) {
strcpy(reply, "no channels");
return;
}
for (int i = 0; i < filter.getNumChannels(); i++) {
auto ch = filter.getChannel(i);
radd(&out, &remain, "%s%d:%s k%u h%02X%s", i ? " " : "", i, ch->name,
ch->secret_len, ch->hash, ch->secret_len == 16 && ch->name[0] == '#' ? " d" : "");
radd(&out, &remain, "%s%d:%s:%02X", i ? " " : "", i, ch->name, ch->hash);
}
}
@@ -1021,7 +1020,7 @@ static void cliAdd(FilterRules& filter, RegionMap* regions, char* params, char*
while ((tok = nextToken(&p)) != NULL) {
char* eq = strchr(tok, '=');
if (eq == NULL) {
sprintf(reply, "Err - expected key=value, got '%s'", tok);
snprintf(reply, CLI_REPLY_MAX, "Err - expected key=value, got '%s'", tok);
filter.delRule(idx);
return;
}
@@ -1036,7 +1035,7 @@ static void cliAdd(FilterRules& filter, RegionMap* regions, char* params, char*
static void cliList(FilterRules& filter, char* reply) {
char* out = reply;
int remain = MAX_PACKET_PAYLOAD;
int remain = CLI_REPLY_MAX;
radd(&out, &remain, "%s %d/%d:", filter.isEnabled() ? "on" : "off",
filter.getNumRules(), FILTER_MAX_RULES);
for (int i = 0; i < filter.getNumRules(); i++) {
@@ -1050,7 +1049,7 @@ static void cliGet(FilterRules& filter, int idx, char* reply) {
if (idx < 0 || idx >= filter.getNumRules()) { strcpy(reply, "Err - no such rule"); return; }
auto r = filter.getRule(idx);
char* out = reply;
int remain = MAX_PACKET_PAYLOAD;
int remain = CLI_REPLY_MAX;
radd(&out, &remain, "r%d %s %s", idx, r->enabled ? "en" : "dis",
r->action == FILTER_ACT_DROP ? "drop" : "forward");
@@ -1107,14 +1106,15 @@ static void cliGet(FilterRules& filter, int idx, char* reply) {
static void cliStats(FilterRules& filter, char* reply) {
char* out = reply;
int remain = MAX_PACKET_PAYLOAD;
radd(&out, &remain, "hits:");
int remain = CLI_REPLY_MAX;
// globals first, per-rule hits last: if the reply truncates, hits detail
// (recoverable via `get N`) is sacrificed before the summary counters
radd(&out, &remain, "lim:%lu abort:%lu air:%lu; hits:",
(unsigned long)filter.getLimiterDrops(), (unsigned long)filter.getBudgetAborts(),
(unsigned long)filter.getAirSavedMs());
for (int i = 0; i < filter.getNumRules(); i++) {
radd(&out, &remain, " %d:%lu", i, (unsigned long)filter.getRule(i)->hits);
}
radd(&out, &remain, "; limiter:%lu aborted:%lu", (unsigned long)filter.getLimiterDrops(),
(unsigned long)filter.getBudgetAborts());
radd(&out, &remain, "; air:%lu", (unsigned long)filter.getAirSavedMs());
}
static bool cliRuleIdx(FilterRules& filter, char* arg, int& idx, char* reply) {
+39 -13
View File
@@ -1797,7 +1797,7 @@ TEST_F(FilterTest, AirSavedShownInCli) {
expectOk(filter, "add type=advert");
mesh::Packet adv = makeAdvert(AIR_KEY);
filter.checkPacket(&adv, 0, nullptr, EST_AIR);
EXPECT_NE(cli(filter, "stats").find("; air:337"), std::string::npos);
EXPECT_NE(cli(filter, "stats").find(" air:337;"), std::string::npos);
EXPECT_NE(cli(filter, "get 0").find(" air=337"), std::string::npos);
}
@@ -2042,10 +2042,10 @@ TEST_F(FilterTest, ThrottleGetFullyLoadedRuleTruncatesAtReplyBuffer) {
"text=\"^y\" prob=50 throttle=65535"),
"OK - rule 0 added");
std::string reply = cli(filter, "get 0");
EXPECT_EQ(reply.size(), (size_t)MAX_PACKET_PAYLOAD - 1); // pinned truncation
EXPECT_NE(reply.find(" throttle=65535 pass=0"), std::string::npos);
EXPECT_NE(reply.find(" hits=0"), std::string::npos);
EXPECT_EQ(reply.find("air="), std::string::npos); // the tail counter truncates away
EXPECT_EQ(reply.size(), (size_t)160 - 1); // pinned truncation (CLI_REPLY_MAX - 1)
EXPECT_NE(reply.find("prob=50"), std::string::npos);
EXPECT_EQ(reply.find("throttle=65535"), std::string::npos); // tail truncates away
EXPECT_EQ(reply.find("air="), std::string::npos);
}
TEST_F(FilterTest, AddRejectsOverlongRegexWithoutTruncating) {
@@ -2078,6 +2078,21 @@ TEST_F(FilterTest, AddRejectsWhenFull) {
EXPECT_EQ(cli(filter, "add type=advert"), "Err - rule list full");
}
TEST_F(FilterTest, LongTokenErrorsStayWithinReplyBuffer) {
// token echoes (unknown param / expected key=value) are user-supplied and
// unbounded; replies must never exceed the real 160 B CLI reply buffer
// (mesh path reply is &temp[5] of temp[166])
std::string long_tok(150, 'a');
std::string reply = cli(filter, ("add " + long_tok).c_str());
EXPECT_LE(reply.size(), (size_t)160 - 1);
EXPECT_EQ(reply.find("Err - expected key=value"), 0);
reply = cli(filter, ("add " + long_tok + "=x").c_str());
EXPECT_LE(reply.size(), (size_t)160 - 1);
EXPECT_EQ(reply.find("Err - unknown param"), 0);
EXPECT_EQ(filter.getNumRules(), 0);
}
TEST_F(FilterTest, RuleIdxRejectsNonNumeric) {
expectOk(filter, "add type=advert");
EXPECT_EQ(cli(filter, "get abc").substr(0, 5), "Err -"); // atoi() would silently pick rule 0
@@ -2131,8 +2146,7 @@ TEST_F(FilterTest, ListAndStatsOutput) {
EXPECT_EQ(list.find("on 1/16: 0eD"), 0); // enabled + Drop digest line
std::string stats = cli(filter, "stats");
EXPECT_EQ(stats.find("hits: 0:3"), 0);
EXPECT_NE(stats.find("; limiter:0 aborted:0"), std::string::npos);
EXPECT_EQ(stats.find("lim:0 abort:0 air:0; hits: 0:3"), 0);
}
// ---------------------------------------------------------------- channel store
@@ -2206,19 +2220,31 @@ TEST_F(FilterTest, ChanDelRemapsRuleMasks) {
FILTER_ACT_DROP);
}
TEST_F(FilterTest, ChanListShowsDerivedFlag) {
TEST_F(FilterTest, ChanListStaysWithinReplyBuffer) {
// real CLI reply buffers are 160 B (serial/ethernet reply[160]; mesh path is
// &temp[5] of temp[166] — CliUtil.h caps radd() at CLI_REPLY_MAX for this);
// a full chan list must never exceed that
for (int i = 0; i < FILTER_MAX_CHANNELS - 1; i++) {
ASSERT_EQ(cli(filter, ("chan add #c" + std::to_string(i)).c_str()).substr(0, 3), "OK ");
}
std::string reply = cli(filter, "chan list");
// "<idx>:<name> k<len> h<hash>" — Public's hash is sha256(its psk)[0]
EXPECT_LE(reply.size(), (size_t)160 - 1); // radd leaves room for the NUL
EXPECT_NE(reply.find("0:Public"), std::string::npos); // clean, not garbage
}
TEST_F(FilterTest, ChanListCompactFormat) {
std::string reply = cli(filter, "chan list");
// "<idx>:<name>:<hash>" — Public's hash is sha256(its psk)[0]
char expect_head[40];
auto pub = filter.getChannel(0);
snprintf(expect_head, sizeof(expect_head), "0:Public k%u h%02X", pub->secret_len, pub->hash);
snprintf(expect_head, sizeof(expect_head), "0:Public:%02X", pub->hash);
EXPECT_NE(reply.find(expect_head), std::string::npos);
EXPECT_EQ(reply.find(" d"), std::string::npos); // provisioned Public has an explicit psk
EXPECT_EQ(reply.find("k"), std::string::npos); // no keylen field in the compact format
// a '#' channel with a companion-derived psk gets the " d" marker
// a '#' channel with a companion-derived psk still lists like any other
ASSERT_EQ(cli(filter, "chan add #derived").substr(0, 3), "OK ");
reply = cli(filter, "chan list");
EXPECT_NE(reply.find(" d"), std::string::npos);
EXPECT_NE(reply.find("#derived:"), std::string::npos);
}
// ---------------------------------------------------------------- ratelimit commands