bugfix: simulator crash from desynced LED pipe
What changed, and why it matters
This commit fixes a simulator-only bug in the COLDCARD firmware's development/test tooling. A special code path that simulates the secure element's 'greenlight' LED signal was writing one byte to an internal pipe instead of the expected two bytes. That single-byte write shifted all later messages out of alignment, eventually causing the simulator to crash with a Python unpacking error. The fix changes that one write to two bytes and adds a regression test to catch similar mistakes. It does not affect real COLDCARD hardware or user funds.
No user action required. This is a simulator-only bugfix in development tooling. Developers should ensure the new regression test passes in CI.
Security signals we found
Denial-of-service condition in simulator tooling caused by malformed inter-process pipe message
Violation of an internal two-byte wire protocol by one caller
Regression test added to enforce protocol framing across all writers
Evidence from the diff
In unix/variant/sim_secel.py, the SE1 simulator’s submethod 5 (‘greenlight firmware’) wrote led_pipe.write(b’\x01’), while simulator.py reads the LED pipe two bytes at a time as [mask, state]. The stray single byte desynchronized the byte stream, so every subsequent read(2) crossed message boundaries and decoded garbage, and the trailing lone byte caused mask, lset = c to raise ValueError. The patch changes the write to b’\x01\x01’ (mask=0x01, state=0x01) and adds testing/test_led_pipe.py, which statically inspects all led_pipe.write(b’…’) calls in unix/variant/*.py for exactly two-byte payloads and dynamically verifies that the concatenated payload stream can be read in aligned [mask, state] pairs with no trailing bytes.
Changed components
unix/variant/sim_secel.pytesting/test_led_pipe.pysimulator.py (consumer of the LED pipe, not modified)Inspect captured patch +70 / −1
### testing/test_led_pipe.py
@@ -0,0 +1,69 @@
+# Regression tests for the simulator's LED pipe framing.
+#
+# simulator.py reads the LED pipe two bytes at a time: [mask, state]. Any writer
+# that emits a different number of bytes desyncs the stream for good, and the
+# reader eventually dies on `mask, lset = c` with a short read.
+#
+import ast, os, pytest
+
+VARIANT_DIR = os.path.join(os.path.dirname(__file__), '..', 'unix', 'variant')
+
+def led_pipe_writes():
+ # yield (filename, lineno, bytes-literal) for each led_pipe.write(b'...') call
+ for fn in sorted(os.listdir(VARIANT_DIR)):
+ if not fn.endswith('.py'):
+ continue
+ path = os.path.join(VARIANT_DIR, fn)
+ with open(path, 'rt') as fd:
+ tree = ast.parse(fd.read(), filename=fn)
+
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.Call):
+ continue
+ f = node.func
+ if not (isinstance(f, ast.Attribute) and f.attr == 'write'):
+ continue
+ if not (isinstance(f.value, ast.Name) and f.value.id == 'led_pipe'):
+ continue
+ if not node.args:
+ continue
+ arg = node.args[0]
+ # only constant byte strings can be checked statically
+ if isinstance(arg, ast.Constant) and isinstance(arg.value, bytes):
+ yield (fn, node.lineno, arg.value)
+
+@pytest.mark.parametrize('fn, lineno, val', list(led_pipe_writes()))
+def test_led_pipe_writes_are_two_bytes(fn, lineno, val):
+ # every message on the LED pipe is exactly [mask, state]
+ assert len(val) == 2, \
+ "%s:%d writes %d byte(s) (%r) to led_pipe; must be 2: [mask, state]" \
+ % (fn, lineno, len(val), val)
+
+def test_led_pipe_writes_found():
+ # guard against the scraper silently finding nothing
+ assert len(list(led_pipe_writes())) >= 3
+
+def test_led_pipe_stream_stays_aligned():
+ # decode the real emitted sequence the way simulator.py does
+ msgs = [v for _, _, v in led_pipe_writes()]
+
+ r_fd, w_fd = os.pipe()
+ tx = open(w_fd, 'wb', buffering=0)
+ rx = open(r_fd, 'rb', closefd=0, buffering=0)
+ try:
+ for m in msgs:
+ tx.write(m)
+ tx.close()
+
+ state = 0
+ for _ in msgs:
+ c = rx.read(2)
+ assert len(c) == 2, "short read: %r - stream is desynced" % (c,)
+ mask, lset = c
+ state |= (mask & lset)
+ state &= ~(mask & ~lset)
+
+ assert rx.read(2) == b'', "trailing bytes left over - stream is desynced"
+ finally:
+ rx.close()
+ os.close(r_fd)
### unix/variant/sim_secel.py
@@ -202,7 +202,7 @@ def pin_stuff(self, submethod, buf_io):
# greenlight firmware
from ckcc import genuine_led, led_pipe
genuine_led = True
- led_pipe.write(b'\x01')
+ led_pipe.write(b'\x01\x01') # [mask, state] - two bytes always
elif submethod == 6:
if not version.has_608:Why this scored 23/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.