fix: dedup the fleet decrypt candidate against the filled prefix of dest

appendChannelByHash() was called with 'channels + n' and scanned
max_matches entries from there, so its same-PSK dedup could only see the
uninitialised tail of core's candidate array — never the entries the filter
store had already appended. The exact case the comment describes (the same
PSK in both stores) appended a duplicate candidate and burned one of core's
four decrypt slots.

The manager now takes 'filled' (how many leading entries core has already
supplied) and scans/appends at the right offsets; unfilled entries are raw
stack and are never compared. Test updated to the new signature plus the
append-after-filled and store-full cases.
This commit is contained in:
Jason Huebert committed 2026-10-07 12:14:05 -05:00
1 parent 0b162e53e7
commit f195aa70f4
4 files changed
+32 -16

No files matched your search

+7 -5
View File
@@ -279,16 +279,18 @@ bool FleetManager::checkForward(const mesh::Packet* packet) {
// ---------------------------------------------------------------- channels
int FleetManager::appendChannelByHash(const uint8_t* hash, mesh::GroupChannel dest[],
int max_matches) {
int max_matches, int filled) {
if (!enabled || psk_len == 0) return 0;
if (filled >= max_matches) return 0;
if (chan_hash != hash[0]) return 0;
// the filter store may hold the same PSK: handing core the same secret twice
// would burn one of its few candidate slots
for (int i = 0; i < max_matches; i++) {
// would burn one of its few candidate slots. Only the filled prefix of dest
// is initialised (core's caller passes raw stack), so only it is compared.
for (int i = 0; i < filled; i++) {
if (memcmp(dest[i].secret, psk, sizeof(psk)) == 0) return 0;
}
dest[0].hash[0] = chan_hash;
memcpy(dest[0].secret, psk, sizeof(dest[0].secret));
dest[filled].hash[0] = chan_hash;
memcpy(dest[filled].secret, psk, sizeof(dest[filled].secret));
return 1;
}
+7 -4
View File
@@ -195,10 +195,13 @@ public:
void runScripts(CliExecFn fn, void* exec_ctx);
// Keyed-channel supply for the core's group decryption: offers the fleet
// channel when its hash matches, skipping a secret already in `dest` (the
// filter store may hold the same PSK; a duplicate would burn one of core's
// few candidate slots). Returns how many entries were appended.
int appendChannelByHash(const uint8_t* hash, mesh::GroupChannel dest[], int max_matches);
// channel when its hash matches, skipping a secret already in the `filled`
// leading entries of `dest` (the filter store may hold the same PSK; a
// duplicate would burn one of core's few candidate slots — and only that
// prefix is initialised, core hands over raw stack for the rest). Appends
// at dest[filled]; returns how many entries were appended.
int appendChannelByHash(const uint8_t* hash, mesh::GroupChannel dest[], int max_matches,
int filled);
void markDirty() { save_flag.markDirty(); }
+1 -1
View File
@@ -473,7 +473,7 @@ int MyMesh::searchChannelsByHash(const uint8_t* hash, mesh::GroupChannel channel
// merge: the filter's keyed-channel store plus the fleet channel (the same
// PSK in both is skipped by the fleet side; any matching key decrypts)
int n = filter.searchChannelsByHash(hash, channels, max_matches);
if (n < max_matches) n += fleet.appendChannelByHash(hash, channels + n, max_matches - n);
if (n < max_matches) n += fleet.appendChannelByHash(hash, channels, max_matches, n);
return n;
}
+17 -6
View File
@@ -1111,20 +1111,31 @@ TEST_F(FleetHooksTest, CheckForwardExemptsOnlyFleetChannelGroupPackets) {
TEST_F(FleetHooksTest, AppendChannelByHash) {
mesh::GroupChannel dest[4];
memset(dest, 0, sizeof(dest));
// hash match: the fleet channel is offered, deduped against what the filter
// store already handed over
// hash match: the fleet channel is offered, deduped against the entries the
// filter store already handed over (the filled prefix)
uint8_t hash[1] = { chan.hash[0] };
memcpy(dest[0].secret, chan.secret, PUB_KEY_SIZE);
EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4), 0); // same secret already present
EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4, 1), 0); // same secret already present
memset(dest, 0, sizeof(dest));
EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4), 1);
EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4, 0), 1);
EXPECT_EQ(dest[0].hash[0], chan.hash[0]);
EXPECT_EQ(memcmp(dest[0].secret, chan.secret, PUB_KEY_SIZE), 0);
// a different secret with the same hash is appended at dest[filled], after
// the filter's entries — never over them
mesh::GroupChannel mixed[4];
memset(mixed, 0, sizeof(mixed));
memset(mixed[0].secret, 0xEE, PUB_KEY_SIZE);
EXPECT_EQ(fleet.appendChannelByHash(hash, mixed, 4, 1), 1);
EXPECT_EQ(mixed[1].hash[0], chan.hash[0]);
EXPECT_EQ(memcmp(mixed[1].secret, chan.secret, PUB_KEY_SIZE), 0);
EXPECT_EQ(mixed[0].secret[0], 0xEE); // the earlier entry is untouched
// store full: nothing offered
EXPECT_EQ(fleet.appendChannelByHash(hash, mixed, 4, 4), 0);
// no hash match, or hooks idle: nothing offered
uint8_t other_hash[1] = { (uint8_t)(chan.hash[0] ^ 0xFF) };
EXPECT_EQ(fleet.appendChannelByHash(other_hash, dest, 4), 0);
EXPECT_EQ(fleet.appendChannelByHash(other_hash, dest, 4, 0), 0);
fleet.setEnabled(false);
EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4), 0);
EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4, 0), 0);
}
// ------------------------------------------------------------------ CLI