Fix out-of-bounds read in TRACE packet hash matching #1655
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Severity: High
Summary
The TRACE packet handler compares this node's identity hash against an entry in the payload to decide whether to forward the packet. The hash size is variable — 1, 2, 4, or 8 bytes depending on the
path_szfield in the packet flags. The existing bounds check (offset >= len) only verifies that the start of the hash is within the payload, but doesn't account for the full hash length. Whenoffsetis close to the end of the payload,isHashMatchreads past the buffer boundary.How this can be exploited
Any node on the mesh can send TRACE packets — they're unauthenticated and travel as direct-routed. An attacker crafts a TRACE packet with:
path_szflag of 3 (meaning 8-byte hashes)offsetfalls near the end, leaving fewer than 8 bytes remainingEvery node that receives this packet calls
isHashMatchwhich reads 8 bytes starting atpayload[i + offset], but only 1-7 bytes are actually part of the payload. The remaining bytes come from whatever sits after the payload buffer in memory.What this enables:
Users would see this as intermittent reboots, broken path traces, or nodes that become temporarily unreachable.
Fix
Add a bounds check ensuring
offset + hash_sz <= lenbefore callingisHashMatch. Packets that don't have enough payload bytes for a full hash comparison are now silently dropped.Test plan
offset >= lenbranch)Heltec_v3_companion_radio_ble