Repository navigation
Guard GCM system-title length to prevent plaintext-DSMR reboot loop - #1210
Merged
Merged
Conversation
When an encryption key is configured but the meter sends a plaintext
DSMR/P1 telegram, DSMRParser routes the telegram to GCMParser. The first
cleartext byte ('0' == 0x30 == 48) was read as the system-title length
and memcpy'd into the 8-byte ctx.system_title and 12-byte
initialization_vector, overflowing the stack and rebooting the device in
a loop (reboot reason "Software reset (3/0)").
Reported for a Kamstrup OMNIA (KAM5) in Denmark: POW-P1 up ~10s then
reboot; disabling encryption worked around it. The #1206 hardening
guarded the ciphertext length but not the system-title length.
Reject system titles longer than 8 bytes (the DLMS maximum) before the
memcpy, turning the crash into a clean GCM_DECRYPT_FAILED that the
existing error handling reports gracefully.
Adds a native regression test feeding the reported plaintext frame to a
key-configured decoder, and enables AddressSanitizer on the native test
env so this class of overflow fails the suite deterministically.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
🔧 PR Build ArtifactsVersion: All environments built successfully. Download the zip files:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Customer report (Kamstrup OMNIA /
KAM5, Denmark, June 2026): POW-P1 reboots constantly, up max ~10 s. Telnet debug showedDSMR wants to decrypt … length: 684→Failed! (-51), reboot reasonSoftware reset (3/0). Disabling encryption was the workaround.The meter sends a plaintext DSMR/P1 telegram, but the user had an encryption key configured. With a key set,
DSMRParserroutes the telegram toGCMParser(DsmrParser.cpp:33). There, the first cleartext byte —'0'(0x30== 48) — is read as the system-title length:ctx.system_titleisuint8_t[8]andinitialization_vectorisuint8_t[12]→ stack buffer overflow → reboot loop.Still present on
main: the #1206 hardening guarded the ciphertext length (len < 17,len < authkeylen+5) but not the system-title length, and the overflowingmemcpyruns before thelen + headersize > ctx.lengthguard.Fix
Reject system titles longer than 8 bytes (the DLMS maximum) before the
memcpy. A plaintext frame is then cleanly rejected withGCM_DECRYPT_FAILED, whichPassiveMeterCommunicatoralready handles gracefully — no crash.Tests
test_plaintext_dsmr_with_key_does_not_overflowfeeds the reported plaintext/KAM5telegram to a key-configured decoder and asserts a clean rejection.[env:native]test env so this class of overflow fails the suite deterministically. Verified the test is red pre-fix (stack-buffer-overflow … WRITE of size 48 at GcmParser.cpp:49) and green post-fix. All 15 decoder tests pass.Two ASAN default-options are baked into the test binary (
new_delete_type_mismatch=0,detect_leaks=0) to silence pre-existing, benign harness noise unrelated to memory-safety overflows.Why this is hard to detect, and why the guard is safe
On the DSMR/P1 path there is no in-band marker for encryption. Frames are dispatched purely by first byte (
PassiveMeterCommunicator.cpp:338):0xDB→ GCM frame (tag present),/(0x2F) → DSMR. An encrypted DSMR meter sends a plaintext/…identification line followed by a bare GCM APDU with no0xDBtag (we callGCMParser::parse(…, hastag=false)). So the body of an encrypted telegram and the body of a plaintext one are byte-for-byte indistinguishable without attempting decryption — the firmware's only signal is whether a key is configured. This bug is that assumption meeting a plaintext meter.The known real-world no-
0xDBencrypted-DSMR format is the Elgama GAMA (Poland, Stoen/E.ON): #1177 (GAMA 150 G15, decrypts successfully) and #1198 (GAMA 350 G35). #1177's encrypted payload begins00 82 02 06 30 …→systemTitleLength = 0x00(this meter sends no system title). The> 8guard leaves that untouched, while a plaintext telegram's first post-header byte is always an OBIS digit (0x30–0x39= 48–57, all> 8) and is rejected. So the boundary correctly accepts the real encrypted case and rejects plaintext. (We hold no Elgama key, so it isn't in the decoder test corpus.)Notes / out of scope
/KAM5plaintext header carries no model/ID. No decoder change.🤖 Generated with Claude Code