bugfix: rng_selftest fails with __fatal_error instead of mp_raise before VM init
What changed, and why it matters
This commit fixes a bug in the COLDCARD MK4 hardware wallet's random number generator (RNG) self-test. Before the fix, if the RNG hardware failed during the self-test, the code would try to raise a Python exception (mp_raise) even though the Python virtual machine had not started yet. That would cause an uncontrolled crash. Now it calls a controlled fatal-error shutdown instead. The change improves reliability and predictability during early boot, but does not by itself create a security vulnerability.
Treat as a hardening/bugfix patch. Apply it. No immediate incident response is required unless a device is observed failing RNG self-test with uncontrolled crashes. Continue to monitor for follow-up commits that strengthen RNG fault handling or add fault-injection tests.
Security signals we found
Controlled fatal error replaces uncontrolled exception in pre-VM context
RNG hardware-failure handling during early boot
Avoids potential undefined behavior / crash before nlr handler exists
Evidence from the diff
In stm32/COLDCARD_MK4/rng.c, rng_get_or_fault() now checks the global rng_count_active flag. When set (during rng_selftest()), a persistent RNG hardware fault invokes __fatal_error() rather than mp_raise_OSError(MP_EFAULT). The self-test runs before MicroPython’s VM, heap, and nlr handler are initialized, so raising an exception would be unsafe. The patch ensures a controlled, intentional shutdown on hardware RNG failure during boot.
Changed components
stm32/COLDCARD_MK4/rng.crng_get_or_fault()rng_selftest()COLDCARD MK4 boot-time RNG self-testInspect captured patch +9 / −6
### stm32/COLDCARD_MK4/rng.c
@@ -43,6 +43,7 @@
#endif
void random_buffer(uint8_t *p, size_t count);
+extern void __fatal_error(const char *msg); // NORETURN, ports/stm32/main.c
static void rng_init(void) {
if (!(RNG->CR & RNG_CR_RNGEN)) {
@@ -157,6 +158,10 @@ static uint32_t rng_get_or_fault(void)
}
// Persistent hardware failure: never return suspect randomness.
+ if (rng_count_active) {
+ // called from rng_selftest(): no VM, heap or nlr handler yet
+ __fatal_error("rng: hw fault");
+ }
mp_raise_OSError(MP_EFAULT);
return last_value;
}
@@ -175,16 +180,14 @@ uint32_t rng_get(void)
// enforced per-call by rng_get_or_fault(); here we only check linkage.
// Runs before the Python VM exists, so failure is fatal.
//
-extern void __fatal_error(const char *msg); // NORETURN, ports/stm32/main.c
-
void rng_selftest(void)
{
rng_init();
- // Wait at register level for the first word: proves entropy is flowing,
- // and guarantees the rng_get() calls below find DRDY set and can never
- // reach their mp_raise() timeout path (no VM/nlr handler installed
- // yet, so an exception here would be an uncontrolled crash).
+ // Wait at register level for the first word: proves entropy is flowing.
+ // While rng_count_active is set, a persistent fault in the rng_get()
+ // calls below ends in __fatal_error() rather than mp_raise(), since no
+ // VM/nlr handler is installed yet.
uint32_t start = HAL_GetTick();
while(!(RNG->SR & RNG_SR_DRDY)) {
if(HAL_GetTick() - start >= RNG_TIMEOUT_MS) {Why this scored 52/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.