fix(packet): readFrom četl pole dřív, než se zeptal, jestli tam vůbec jsou

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5 committed 2026-09-01 20:12:01 +02:00
1 parent 5230f5efb2
commit 0524c78459
3 files changed
+173 -1

No files matched your search

+24
View File
@@ -186,6 +186,30 @@ build_src_filter =
lib_deps =
google/googletest @ 1.17.0
; Stejné jako [env:native], ale pod AddressSanitizerem. Bounds fixy v parsování
; paketů nemění návratovou hodnotu -- odmítnou přesně to, co upstream odmítne až
; po přečtení -- takže rozdíl je vidět jenom tady. Testy krmí parser bufferem
; alokovaným na přesnou délku rámce, aby bylo čtení za jeho konec skutečná chyba.
[env:native_asan]
platform = native
test_framework = googletest
build_flags = -std=c++17
-I src
-I test/mocks
-fsanitize=address,undefined
-fno-omit-frame-pointer
-g -O1
build_unflags = -O2
test_build_src = yes
test_ignore = test_kiss_modem
build_src_filter =
-<*>
+<../src/Utils.cpp>
+<../src/Packet.cpp>
+<../src/helpers/ConfigSerializer.cpp>
lib_deps =
google/googletest @ 1.17.0
[env:native_kiss_modem]
platform = native
test_framework = googletest
+22 -1
View File
@@ -63,9 +63,30 @@ uint8_t Packet::writeTo(uint8_t dest[]) const {
}
bool Packet::readFrom(const uint8_t src[], uint8_t len) {
// CUSTOM (TeTeHacko): every field is bounds-checked against `len` BEFORE it is
// read. Upstream reads the header, the transport codes, path_len and the whole
// path first and only then asks whether any of it was actually there
// (`if (i >= len)`), so a short frame is read past its end and the result is
// thrown away afterwards.
//
// Measured with an ASAN harness that hands readFrom a heap buffer allocated to
// exactly `len` bytes: 5 242 880 generated frames hit FOUR distinct
// out-of-bounds reads (header, both transport codes, path_len, the path
// memcpy). With these guards the same sweep is clean and accepts exactly the
// same 1 351 424 frames -- the checks reject nothing that used to parse, they
// only stop reading memory that was never received.
//
// On the device the source is Dispatcher's `raw[MAX_TRANS_UNIT+1]`, a 256-byte
// stack buffer, and this reads at most ~70 bytes into it, so what it read was
// leftovers from an earlier packet rather than memory outside the array. That
// is why it never showed up as a crash -- and why this is hygiene, not a hole.
//
// Same fix as upstream PR #1666 (weebl2000), open and unmerged since Feb 2026.
if (len < 2) return false; // at minimum: header + path_len
uint8_t i = 0;
header = src[i++];
if (hasTransportCodes()) {
if (i + 4 >= len) return false; // 4 transport bytes plus the path_len byte
memcpy(&transport_codes[0], &src[i], 2); i += 2;
memcpy(&transport_codes[1], &src[i], 2); i += 2;
} else {
@@ -75,9 +96,9 @@ bool Packet::readFrom(const uint8_t src[], uint8_t len) {
if (!isValidPathLen(path_len)) return false; // bad encoding
uint8_t bl = getPathByteLen();
if (i + bl >= len) return false; // path plus at least one payload byte must fit
memcpy(path, &src[i], bl); i += bl;
if (i >= len) return false; // bad encoding
payload_len = len - i;
if (payload_len > sizeof(payload)) return false; // bad encoding
memcpy(payload, &src[i], payload_len); //i += payload_len;
+127
View File
@@ -0,0 +1,127 @@
#include <gtest/gtest.h>
#include "Packet.h"
#include <cstdlib>
using namespace mesh;
// Hand readFrom a buffer allocated to EXACTLY the frame length. Under -e native
// this only checks the return value -- and the guards in readFrom deliberately do
// not change it, so these pass with or without them. The point is the ASAN build
// (-e native_asan): there the byte after the buffer is a red zone, so a read past
// the end of the frame aborts the test instead of silently returning leftovers.
static bool feedTight(Packet& p, const uint8_t* frame, uint8_t len) {
uint8_t* tight = (uint8_t*)malloc(len ? len : 1);
memcpy(tight, frame, len);
bool ok = p.readFrom(tight, len);
free(tight);
return ok;
}
// Build a frame the parser should accept: header, path_len, then payload bytes.
static uint8_t makeFrame(uint8_t* out, uint8_t route, uint8_t type, uint8_t hash_size,
uint8_t hash_count, int payload_bytes) {
uint8_t i = 0;
out[i++] = route | (type << PH_TYPE_SHIFT);
out[i++] = ((hash_size - 1) << 6) | (hash_count & 63);
for (int h = 0; h < hash_count * hash_size; h++) out[i++] = 0xC0 + h;
for (int p = 0; p < payload_bytes; p++) out[i++] = 0x40 + p;
return i;
}
// ── the frame must be long enough for every field before it is read ──────────
//
// These all used to parse the field first and reject afterwards, so a short
// frame was read past its end. An ASAN sweep over 5 242 880 generated frames
// found four such reads; see the note in Packet::readFrom.
TEST(PacketReadFrom, RejectsFrameTooShortForHeaderAndPathLen) {
Packet p;
uint8_t frame[4] = {0};
EXPECT_FALSE(feedTight(p, frame, 0));
EXPECT_FALSE(feedTight(p, frame, 1));
}
TEST(PacketReadFrom, RejectsTruncatedTransportCodes) {
Packet p;
uint8_t frame[8];
frame[0] = ROUTE_TYPE_TRANSPORT_FLOOD | (PAYLOAD_TYPE_ACK << PH_TYPE_SHIFT);
for (int i = 1; i < 8; i++) frame[i] = 0x11;
// needs header + 4 transport bytes + path_len + at least 1 payload byte
for (uint8_t len = 2; len <= 6; len++) {
EXPECT_FALSE(feedTight(p, frame, len)) << "len=" << (int)len;
}
}
TEST(PacketReadFrom, RejectsPathOverrunningTheFrame) {
Packet p;
uint8_t frame[80];
// declares 20 one-byte hashes but the frame carries only a few bytes of them
uint8_t full = makeFrame(frame, ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ACK, 1, 20, 4);
for (uint8_t len = 2; len < full - 4; len++) {
EXPECT_FALSE(feedTight(p, frame, len)) << "len=" << (int)len;
}
}
TEST(PacketReadFrom, RejectsPathWithNoRoomLeftForPayload) {
Packet p;
uint8_t frame[80];
uint8_t full = makeFrame(frame, ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ACK, 1, 8, 0);
EXPECT_FALSE(feedTight(p, frame, full)); // path fits exactly, payload is empty
}
// ── control group: the guards must not reject what used to parse ─────────────
TEST(PacketReadFrom, AcceptsWellFormedFrame) {
Packet p;
uint8_t frame[80];
uint8_t len = makeFrame(frame, ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ACK, 1, 3, 5);
ASSERT_TRUE(feedTight(p, frame, len));
EXPECT_EQ(3, p.getPathHashCount());
EXPECT_EQ(1, p.getPathHashSize());
EXPECT_EQ(5, p.payload_len);
EXPECT_EQ(0x40, p.payload[0]);
EXPECT_EQ(0xC0, p.path[0]);
}
TEST(PacketReadFrom, AcceptsWellFormedFrameWithTransportCodes) {
Packet p;
uint8_t frame[80];
uint8_t i = 0;
frame[i++] = ROUTE_TYPE_TRANSPORT_DIRECT | (PAYLOAD_TYPE_ACK << PH_TYPE_SHIFT);
frame[i++] = 0x34; frame[i++] = 0x12; // transport_codes[0]
frame[i++] = 0x78; frame[i++] = 0x56; // transport_codes[1]
frame[i++] = 0; // path_len: no hops
frame[i++] = 0x99; // one payload byte
ASSERT_TRUE(feedTight(p, frame, i));
EXPECT_EQ(0x1234, p.transport_codes[0]);
EXPECT_EQ(0x5678, p.transport_codes[1]);
EXPECT_EQ(1, p.payload_len);
EXPECT_EQ(0x99, p.payload[0]);
}
TEST(PacketReadFrom, RoundTripsThroughWriteTo) {
Packet out;
out.header = ROUTE_TYPE_FLOOD | (PAYLOAD_TYPE_TXT_MSG << PH_TYPE_SHIFT);
out.setPathHashSizeAndCount(2, 3);
for (int i = 0; i < 6; i++) out.path[i] = 0xA0 + i;
out.payload_len = 10;
for (int i = 0; i < 10; i++) out.payload[i] = 0x50 + i;
uint8_t wire[MAX_TRANS_UNIT];
uint8_t len = out.writeTo(wire);
Packet in;
ASSERT_TRUE(feedTight(in, wire, len));
EXPECT_EQ(out.header, in.header);
EXPECT_EQ(out.path_len, in.path_len);
EXPECT_EQ(out.payload_len, in.payload_len);
EXPECT_EQ(0, memcmp(out.path, in.path, out.getPathByteLen()));
EXPECT_EQ(0, memcmp(out.payload, in.payload, out.payload_len));
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}