From c3a7cb99f3f0977906589f613a2375e604ef45c2 Mon Sep 17 00:00:00 2001 From: Jason Huebert Date: Wed, 7 Oct 2026 07:43:14 -0500 Subject: [PATCH] fix(filter): undefined chan_mask shift when deleting the last channel of a full store MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit delChannel() remaps each rule's chan_mask with (chan_mask >> (idx + 1)) << idx. The v7 branch widened chan_mask to 32 bits and the store to 32 channels, so deleting the last channel of a full store shifts by exactly 32 — undefined behavior. On x86 (variable shift counts mask to 5 bits) a rule naming channel 0 came out of the delete with a spurious bit 31 set instead. Guard the shift: at idx == FILTER_MAX_CHANNELS - 1 nothing sits above the deleted channel, so the high contribution is simply zero. Regression test covers the full-store edge (mask integrity plus a behavioral re-check that the surviving named channel still matches). --- AGENTS.md | 4 +-- examples/simple_repeater/PacketFilter.cpp | 4 ++- .../test_packet_filter/test_packet_filter.cpp | 25 +++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index a326c127..dffb81cc 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -59,7 +59,7 @@ commit** — the guides are user-facing API documentation, not optional docs. dispatch, lazy-save loop). - `main.cpp` — serial CLI entry (upstream + fork's buffer-hardening lines). -**Tests:** `test/test_packet_filter/` (294 behavior-level cases: matching, +**Tests:** `test/test_packet_filter/` (295 behavior-level cases: matching, content rules, limiter, persistence upgrades, CLI surface, TinyRegex, PatternMatch) and `test/test_battery_gate/`. Test-only shims: `NativeShim.h`, `NativeTestStubs.cpp`, `RegionMapStub.cpp`, `FilterTestHelpers.h`. @@ -218,7 +218,7 @@ FILTER.md: paths, added in the same commit. The suites are behavior-level (native googletest); reach them via the same CLI/`checkPacket`/`checkContent` entry points a user or the firmware would. -- Pure refactors keep both suites green **unchanged** — the suites (294 filter, +- Pure refactors keep both suites green **unchanged** — the suites (295 filter, 44 battery) are the safety net that proves no behavior slipped. - Run both suites, then re-read the diff: diff --git a/examples/simple_repeater/PacketFilter.cpp b/examples/simple_repeater/PacketFilter.cpp index e7f5d08d..e761fb7b 100644 --- a/examples/simple_repeater/PacketFilter.cpp +++ b/examples/simple_repeater/PacketFilter.cpp @@ -251,7 +251,9 @@ void FilterRules::delChannel(int idx) { for (int i = 0; i < num_rules; i++) { FilterRule* r = &rules[i]; uint32_t lo = r->chan_mask & (uint32_t)((1u << idx) - 1); - uint32_t hi = (r->chan_mask >> (idx + 1)) << idx; + // a full store deletes at idx == FILTER_MAX_CHANNELS - 1: nothing sits + // above it, and chan_mask >> 32 would be an undefined shift + uint32_t hi = (idx + 1 < FILTER_MAX_CHANNELS) ? ((r->chan_mask >> (idx + 1)) << idx) : 0; r->chan_mask = lo | hi; } markDirty(); diff --git a/test/test_packet_filter/test_packet_filter.cpp b/test/test_packet_filter/test_packet_filter.cpp index 6705d040..1958e0fd 100644 --- a/test/test_packet_filter/test_packet_filter.cpp +++ b/test/test_packet_filter/test_packet_filter.cpp @@ -4350,6 +4350,31 @@ TEST_F(FilterTest, ChanDelRemapsRuleMasks) { FILTER_ACT_DROP); } +TEST_F(FilterTest, ChanDelRemapsMasksAtTheWidenedStoreEdge) { + // chan_mask is 32 bits wide and the store can hold 32 channels: deleting the + // LAST channel of a full store shifts by idx+1 == 32, which must yield no + // "bits above" contribution at all (a naive chan_mask >> 32 is undefined) + 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 "); + } + ASSERT_EQ(filter.getNumChannels(), FILTER_MAX_CHANNELS); + int last = filter.indexOfChannel("#c30"); + int first = filter.indexOfChannel("Public"); + ASSERT_EQ(last, FILTER_MAX_CHANNELS - 1); + ASSERT_EQ(first, 0); + ASSERT_EQ(cli(filter, "add chan=Public,#c30"), "OK - rule 0 added"); + EXPECT_EQ(filter.getRule(0)->chan_mask, (1u << first) | (1u << last)); + + ASSERT_EQ(cli(filter, "chan del #c30"), "OK - chan #c30 deleted"); + EXPECT_EQ(filter.getRule(0)->chan_mask, 1u << first); // exactly Public, nothing above + + // behaviourally: the rule still matches the surviving named channel + auto pkt = makePacket(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_TXT, 10); + EXPECT_EQ(filter.checkContent(&pkt, PAYLOAD_TYPE_GRP_TXT, channelFromStore(filter, first), + nullptr, 0, nullptr), + FILTER_ACT_DROP); +} + 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);