From 6bdcb2acadddd0d8dbec88d38b8a4d2f3ba01a4b Mon Sep 17 00:00:00 2001 From: Lars Nolden Date: Tue, 1 Sep 2026 11:28:14 +0200 Subject: [PATCH] wmbus: fix format-A length formula for L=10 (mod 16); host tests under ASan --- docs/FINDINGS.md | 17 +++ docs/OPEN_QUESTIONS.md | 15 +++ firmware/main/wmbus.c | 22 +++- firmware/test/run_tests.sh | 16 +++ firmware/test/test_wmbus.c | 222 +++++++++++++++++++++++++++++++++++++ hil/correlate.py | 13 +++ 6 files changed, 302 insertions(+), 3 deletions(-) create mode 100755 firmware/test/run_tests.sh create mode 100644 firmware/test/test_wmbus.c diff --git a/docs/FINDINGS.md b/docs/FINDINGS.md index aaa39f8..aa4a049 100644 --- a/docs/FINDINGS.md +++ b/docs/FINDINGS.md @@ -124,6 +124,23 @@ every heartbeat (`config_intact`), and recovery levels only report success when both the mode and the configuration verify. +## CONFIRMED (frame layer, host tests) + +- `firmware/test/run_tests.sh` validates the frame layer against real + captured meter frames under AddressSanitizer: CRCs of third-party frames + verify, every single-bit flip in them is detected, the builder's own + frames validate for all buildable L, and 200k adversarial length/buffer + combinations produce no out-of-bounds access. +- The tests found a real internal inconsistency: the format-A length formula + copied from rtl-wmbus disagrees with the greedy 16-byte block walk used + for CRC validation whenever L = 10 (mod 16). Fixed to + `1 + L + 2*(1 + ceil((L-9)/16))`, which matches wmbusmeters' trimming. + Both forms agree on every valid length observed here, and no valid + L = 10 (mod 16) frame was ever captured -- the 10 candidates in the logs + all fail their length-independent block-1 CRC, i.e. they are corrupted + receptions. So this is a latent-correctness fix, not a measured + improvement (docs/OPEN_QUESTIONS.md). + ## NEEDS TESTING - Narrower RX bandwidth (156 kHz) vs current 234.3 kHz on weak meters. diff --git a/docs/OPEN_QUESTIONS.md b/docs/OPEN_QUESTIONS.md index c057649..d9af0e1 100644 --- a/docs/OPEN_QUESTIONS.md +++ b/docs/OPEN_QUESTIONS.md @@ -24,3 +24,18 @@ (thermal suspected). The `rtl_test -t` theory was DISPROVEN. - Can the SX1262 RX be stopped early (poll GetRxBufferStatus after SyncWordValid) to remove the ~12 ms fixed-capture tail? + +## Standards detail needing an authoritative answer + +- Format A frame length for `L = 10 (mod 16)`. Two mature open-source + implementations disagree in this corner: + - rtl-wmbus computes `1 + L + 2*((L-10+15)/16 + 1)` CRC pairs, which for + L = 10, 26, 42, ... requires a 17-byte block; + - wmbusmeters does not predict the length at all; it walks greedy 16-byte + blocks and accepts a final block of 1..16 bytes. + This firmware follows the wmbusmeters decomposition + (`1 + L + 2*(1 + ceil((L-9)/16))`) because it is self-consistent with the + CRC walk actually used. No valid frame with such an L was ever captured + at this site, so the question is unresolved empirically -- it needs the + EN 13757-4 text, or a meter that emits one. Until then, a real meter using + those lengths is an interoperability risk worth a targeted test. diff --git a/firmware/main/wmbus.c b/firmware/main/wmbus.c index 5a684ed..1b3e8c3 100644 --- a/firmware/main/wmbus.c +++ b/firmware/main/wmbus.c @@ -26,10 +26,26 @@ int wmbus_frame_total_len(wmbus_frame_fmt_t fmt, uint8_t lfield) int total; switch (fmt) { case WMBUS_FRAME_A: - // L counts C,M,A,CI,data without CRCs. Block 1 holds 9 payload - // bytes; every further block holds up to 16, each with 2 CRC bytes. + // L counts C,M,A,CI,data and excludes every CRC byte. Block 1 + // carries the L-field plus C,M,A (10 bytes) with its own CRC, so 9 + // L-counted bytes. The remaining (L - 9) bytes are split into + // blocks of at most 16, each followed by 2 CRC bytes. + // + // rtl-wmbus uses `2 * ((L - 10 + 15)/16 + 1)`, i.e. floor((L+5)/16)+1 + // CRC pairs. That differs from the decomposition above by one pair + // whenever L = 10 (mod 16), where it would have to place 17 bytes in + // a 16-byte block. wmbusmeters' trimCRCsFrameFormatA walks greedy + // 16-byte blocks and accepts a final block of 1..16 bytes, which + // matches the form used here. + // + // Both forms agree on every VALID length observed at this site + // (L = 54, 57, 62, 67, 87, 95, 98, 102). No valid L = 10 (mod 16) + // frame was ever captured, so this is an internal-consistency fix + // found by firmware/test/test_wmbus.c, not a fix for an observed + // interoperability failure: with the old formula such a frame would + // have been computed 2 bytes short and always failed CRC. if (lfield < 9) return -1; - total = 1 + lfield + 2 * (((lfield - 10 + 15) / 16) + 1); + total = 1 + lfield + 2 * (1 + ((lfield - 9 + 15) / 16)); break; case WMBUS_FRAME_B: // L counts everything after itself including CRC bytes. diff --git a/firmware/test/run_tests.sh b/firmware/test/run_tests.sh new file mode 100755 index 0000000..ffdb91d --- /dev/null +++ b/firmware/test/run_tests.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# Build and run the host-side wM-Bus frame tests under AddressSanitizer. +# The sanitizer is the point: these tests feed adversarial lengths through +# the CRC walk, and only ASan makes an out-of-bounds read observable. +set -euo pipefail +cd "$(dirname "$0")" + +CC=${CC:-gcc} +OUT=$(mktemp -d) +trap 'rm -rf "$OUT"' EXIT + +"$CC" -std=c11 -O1 -g -Wall -Wextra -Werror \ + -fsanitize=address,undefined -fno-omit-frame-pointer \ + -o "$OUT/test_wmbus" test_wmbus.c ../main/wmbus.c + +"$OUT/test_wmbus" diff --git a/firmware/test/test_wmbus.c b/firmware/test/test_wmbus.c new file mode 100644 index 0000000..fd303ad --- /dev/null +++ b/firmware/test/test_wmbus.c @@ -0,0 +1,222 @@ +// Host-side tests for the Wireless M-Bus frame layer. +// +// RF packet lengths are hostile input: the L-field comes straight off the +// air and drives every index used when validating CRCs. These tests defend +// three contracts: +// 1. CRC validation agrees with reality on frames captured from real +// meters, and rejects them after a single bit flip. +// 2. The length formulas are exact for both C-mode frame formats and +// reject values that cannot fit the radio payload. +// 3. No input -- valid, malformed, or adversarial -- reads outside the +// caller's buffer. Run under AddressSanitizer to make that meaningful. +// +// Build/run: firmware/test/run_tests.sh +#include "../main/wmbus.h" + +#include +#include +#include +#include + +static int failures; + +#define CHECK(cond, ...) do { \ + if (!(cond)) { \ + printf("FAIL %s:%d: ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + failures++; \ + } \ + } while (0) + +static int hex2bin(const char *hex, uint8_t *out, size_t maxlen) +{ + size_t n = strlen(hex) / 2; + if (n > maxlen) return -1; + for (size_t i = 0; i < n; i++) { + unsigned v; + if (sscanf(hex + 2 * i, "%2x", &v) != 1) return -1; + out[i] = (uint8_t)v; + } + return (int)n; +} + +// ---------------------------------------------------------------- vectors + +struct vec { + wmbus_frame_fmt_t fmt; + int total_len; + const char *hex; +}; + +// Frames captured from real meters and reported CRC-valid by the firmware +// on hardware. Independent confirmation that the CRC implementation is not +// merely self-consistent: these bytes were produced by third-party meters. +static const struct vec valid_frames[] = { + { WMBUS_FRAME_A, 100, "5744685067677604940893CA8C00F3900F002C25C33B1600B630B6FD0A88CB9CF1297AC30030071032D3399DBA4849A49DFA4081C0FB18E97258FBC0494113BA043F4188906E502B841CA86A3797DE729BC0A4BA6C652096FFC471400EA70F96361BF9DF" }, + { WMBUS_FRAME_A, 63, "36446850855862035237B9757244096554A85CFE075800202537D250C8E3C9DE60928BA87627B047F07BF2933C959CDAC9E1838215A16755AB905C1569DAE3" }, + { WMBUS_FRAME_A, 66, "39449344601647611D0762287AAA0000200C13360000004C130000006A5B00426CFFFFCC081336000000C2086C5FE1F63702BB560000326CFFFF046D2E155F3841EF" }, +}; + +static void test_real_frames_validate(void) +{ + uint8_t buf[300]; + for (size_t i = 0; i < sizeof(valid_frames) / sizeof(valid_frames[0]); i++) { + const struct vec *v = &valid_frames[i]; + int n = hex2bin(v->hex, buf, sizeof(buf)); + CHECK(n == v->total_len, "vector %zu: hex decodes to %d, expected %d", + i, n, v->total_len); + if (n != v->total_len) continue; + + CHECK(wmbus_frame_total_len(v->fmt, buf[0]) == v->total_len, + "vector %zu: length formula gave %d for L=%u, expected %d", + i, wmbus_frame_total_len(v->fmt, buf[0]), buf[0], v->total_len); + CHECK(wmbus_check_crc(v->fmt, buf, n), + "vector %zu: real meter frame rejected by CRC check", i); + } +} + +static void test_bitflip_rejected(void) +{ + uint8_t buf[300]; + for (size_t i = 0; i < sizeof(valid_frames) / sizeof(valid_frames[0]); i++) { + const struct vec *v = &valid_frames[i]; + int n = hex2bin(v->hex, buf, sizeof(buf)); + if (n != v->total_len) continue; + + // Every single-bit flip in the payload must be caught. CRC-16 over + // blocks of <= 16 bytes detects all single-bit errors, so any miss + // is a real defect. + for (int byte = 0; byte < n; byte++) { + for (int bit = 0; bit < 8; bit++) { + buf[byte] ^= (uint8_t)(1u << bit); + bool ok = wmbus_check_crc(v->fmt, buf, n); + buf[byte] ^= (uint8_t)(1u << bit); + if (ok) { + CHECK(false, "vector %zu: bit flip at byte %d bit %d " + "not detected", i, byte, bit); + return; // one report is enough + } + } + } + } +} + +static void test_length_formulas(void) +{ + // Format A: L counts C,M,A,CI,data without CRCs; block 1 carries 9 of + // those bytes, further blocks up to 16, each with 2 CRC bytes. + CHECK(wmbus_frame_total_len(WMBUS_FRAME_A, 9) == 12, + "A: L=9 should be the 12-byte minimum, got %d", + wmbus_frame_total_len(WMBUS_FRAME_A, 9)); + CHECK(wmbus_frame_total_len(WMBUS_FRAME_A, 8) < 0, "A: L=8 must be rejected"); + // L=25 -> 1 + 25 + 2*(ceil(15/16)+1) = 30 + CHECK(wmbus_frame_total_len(WMBUS_FRAME_A, 25) == 30, + "A: L=25 expected 30, got %d", wmbus_frame_total_len(WMBUS_FRAME_A, 25)); + + // Format B: L counts everything after itself, CRCs included. + CHECK(wmbus_frame_total_len(WMBUS_FRAME_B, 11) == 12, + "B: L=11 expected 12, got %d", wmbus_frame_total_len(WMBUS_FRAME_B, 11)); + CHECK(wmbus_frame_total_len(WMBUS_FRAME_B, 10) < 0, "B: L=10 must be rejected"); + CHECK(wmbus_frame_total_len(WMBUS_FRAME_B, 200) == 201, + "B: L=200 expected 201, got %d", wmbus_frame_total_len(WMBUS_FRAME_B, 200)); + + CHECK(wmbus_frame_total_len(WMBUS_FRAME_UNKNOWN, 50) < 0, + "unknown format must be rejected"); + + // Nothing may exceed what fits the SX1262 payload alongside 2 mode bytes. + for (int L = 0; L <= 255; L++) { + int a = wmbus_frame_total_len(WMBUS_FRAME_A, (uint8_t)L); + int b = wmbus_frame_total_len(WMBUS_FRAME_B, (uint8_t)L); + CHECK(a <= 253, "A: L=%d produced oversized total %d", L, a); + CHECK(b <= 253, "B: L=%d produced oversized total %d", L, b); + } +} + +static void test_frame_format_detection(void) +{ + CHECK(wmbus_frame_format(0x54, 0xCD) == WMBUS_FRAME_A, "54CD must be format A"); + CHECK(wmbus_frame_format(0x54, 0x3D) == WMBUS_FRAME_B, "543D must be format B"); + CHECK(wmbus_frame_format(0x54, 0x00) == WMBUS_FRAME_UNKNOWN, "54 00 is not C-mode"); + CHECK(wmbus_frame_format(0x00, 0xCD) == WMBUS_FRAME_UNKNOWN, "00 CD is not C-mode"); + // T-mode traffic shares the 0x543D access code, so it lands here too and + // must be classified as not-C1 rather than mis-parsed. + CHECK(wmbus_frame_format(0x3D, 0x54) == WMBUS_FRAME_UNKNOWN, "3D 54 is not C-mode"); +} + +static void test_builder_roundtrip(void) +{ + uint8_t buf[300]; + for (int L = 19; L <= 200; L++) { + int n = wmbus_build_test_frame(buf, sizeof(buf), (uint8_t)(L & 0xFF), (uint8_t)L); + if (n < 0) continue; // does not fit: acceptable + CHECK(n == wmbus_frame_total_len(WMBUS_FRAME_A, buf[0]), + "builder L=%d: length %d disagrees with formula %d", + L, n, wmbus_frame_total_len(WMBUS_FRAME_A, buf[0])); + CHECK(wmbus_check_crc(WMBUS_FRAME_A, buf, n), + "builder L=%d: generated frame fails its own CRC check", L); + } + // Must refuse to overrun a short buffer rather than truncate. + uint8_t small[16]; + CHECK(wmbus_build_test_frame(small, sizeof(small), 0, 200) < 0, + "builder must refuse a buffer that cannot hold the frame"); +} + +// Adversarial input: random bytes with a length that disagrees with the +// L-field, exactly what a corrupted reception looks like. Under ASan this +// proves the CRC walk never indexes outside the buffer. +static void test_fuzz_bounds(void) +{ + srand(12345); + for (int iter = 0; iter < 200000; iter++) { + int n = 1 + rand() % 260; + uint8_t *buf = malloc((size_t)n); + if (!buf) continue; + for (int i = 0; i < n; i++) buf[i] = (uint8_t)(rand() & 0xFF); + + wmbus_frame_fmt_t fmt = (iter % 3 == 0) ? WMBUS_FRAME_A + : (iter % 3 == 1) ? WMBUS_FRAME_B + : WMBUS_FRAME_UNKNOWN; + // The firmware only calls check_crc with a total_len it derived from + // the L-field AND verified against the received byte count; mirror + // both that path and the raw path with mismatched lengths. + int total = wmbus_frame_total_len(fmt, buf[0]); + if (total > 0 && total <= n) { + (void)wmbus_check_crc(fmt, buf, total); + } + (void)wmbus_check_crc(fmt, buf, n); + (void)wmbus_hash(buf, (size_t)n); + free(buf); + } +} + +static void test_crc_known_answer(void) +{ + // EN 13757-4 CRC-16: poly 0x3D65, init 0x0000, MSB first, final + // complement. Anchored on the first block of a real captured frame, + // whose trailing CRC bytes were produced by the meter. + uint8_t block1[10] = {0x57, 0x44, 0x68, 0x50, 0x67, 0x67, 0x76, 0x04, 0x94, 0x08}; + uint16_t crc = wmbus_crc16(block1, sizeof(block1)); + CHECK(crc == 0x93CA, "CRC of real block 1 expected 0x93CA, got 0x%04X", crc); + + // Empty input: init complemented. + CHECK(wmbus_crc16(block1, 0) == 0xFFFF, "CRC of empty input must be 0xFFFF"); +} + +int main(void) +{ + test_crc_known_answer(); + test_real_frames_validate(); + test_bitflip_rejected(); + test_length_formulas(); + test_frame_format_detection(); + test_builder_roundtrip(); + test_fuzz_bounds(); + + if (failures) { + printf("\n%d check(s) FAILED\n", failures); + return 1; + } + printf("all wmbus tests passed\n"); + return 0; +} diff --git a/hil/correlate.py b/hil/correlate.py index 6db6553..c0e8a88 100755 --- a/hil/correlate.py +++ b/hil/correlate.py @@ -23,6 +23,19 @@ import sys from datetime import datetime +def total_len_a(lfield: int) -> int: + """Over-the-air length of a format A frame, incl. L and all CRC bytes. + + Block 1 = L + C,M,A (10 bytes) + CRC; the remaining (L - 9) L-counted + bytes go into blocks of at most 16, each followed by 2 CRC bytes. This + matches firmware/main/wmbus.c; see the note there about rtl-wmbus + differing for L = 10 (mod 16). + """ + if lfield < 9: + return -1 + return 1 + lfield + 2 * (1 + (lfield - 9 + 15) // 16) + + def cook_a(frame: bytes): """Strip block CRCs from a format A over-the-air frame.""" if len(frame) < 12: