Skip to content

Guard GCM system-title length to prevent plaintext-DSMR reboot loop - #1210

Merged
gskjold merged 1 commit into
mainfrom
fix/gcm-systemtitle-overflow
Jun 18, 2026
Merged

gskjold merged 1 commit into
mainfrom
fix/gcm-systemtitle-overflow

Conversation

@gskjold

@gskjold gskjold commented Jun 18, 2026 •

Copy link
Copy Markdown
Member

Problem

Customer report (Kamstrup OMNIA / KAM5, Denmark, June 2026): POW-P1 reboots constantly, up max ~10 s. Telnet debug showed DSMR wants to decrypt … length: 684 → Failed! (-51), reboot reason Software 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, DSMRParser routes the telegram to GCMParser (DsmrParser.cpp:33). There, the first cleartext byte — '0' (0x30 == 48) — is read as the system-title length:

uint8_t systemTitleLength = *ptr;            // 48
...
memcpy(ctx.system_title, ptr, systemTitleLength);          // 48 bytes → uint8_t[8]
memcpy(initialization_vector, ctx.system_title, systemTitleLength); // 48 bytes → uint8_t[12]

ctx.system_title is uint8_t[8] and initialization_vector is uint8_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 overflowing memcpy runs before the len + headersize > ctx.length guard.

Fix

Reject system titles longer than 8 bytes (the DLMS maximum) before the memcpy. A plaintext frame is then cleanly rejected with GCM_DECRYPT_FAILED, which PassiveMeterCommunicator already handles gracefully — no crash.

if(systemTitleLength > 8) return GCM_DECRYPT_FAILED;

Tests

  • New native regression test test_plaintext_dsmr_with_key_does_not_overflow feeds the reported plaintext /KAM5 telegram to a key-configured decoder and asserts a clean rejection.
  • Enabled AddressSanitizer on the [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 no 0xDB tag (we call GCMParser::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-0xDB encrypted-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 begins 00 82 02 06 30 … → systemTitleLength = 0x00 (this meter sends no system title). The > 8 guard 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

  • The thread also mentions the meter's model/serial not being recognized — expected: the /KAM5 plaintext header carries no model/ID. No decoder change.
  • "Entering keys enabled power on the P1 port" is meter/hardware-side, not firmware.

🤖 Generated with Claude Code

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]>
@github-actions

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: b4076bb

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

@gskjold
gskjold marked this pull request as ready for review June 18, 2026 06:47
@gskjold
gskjold merged commit 536f8fa into main Jun 18, 2026
8 checks passed
@gskjold
gskjold deleted the fix/gcm-systemtitle-overflow branch June 18, 2026 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant