fix(core): prevent NFC driver crash on repeated deinitialization
What changed, and why it matters
This commit fixes a bug in the Trezor hardware wallet's NFC driver where calling the NFC shutdown function more than once could crash the device. The fix adds a simple check to skip re-deinitializing the SPI hardware if it has already been shut down. It is a defensive hardening fix rather than a clear exploitable vulnerability.
Treat as a low-severity hardening fix. Review whether nfc_deinit() can be triggered from untrusted or error paths, and ensure all HAL deinit calls are idempotent. No urgent security response is indicated by the diff alone.
Security signals we found
Double-deinitialization bug in low-level hardware driver
Potential crash/fault on repeated NFC deinit
Defensive null-pointer guard added
Evidence from the diff
In core/embed/io/nfc/st25/nfc.c, nfc_deinit() now checks drv->hspi.Instance != NULL before invoking HAL_SPI_DeInit(). Repeated deinitialization of an already-deinitialized SPI peripheral could lead to a null/invalid pointer dereference or HAL-level fault. The patch prevents double-deinit by guarding the call.
Changed components
core/embed/io/nfc/st25/nfc.cNFC ST25 driverHAL SPI deinitialization pathInspect captured patch +3 / −1
diff --git a/core/embed/io/nfc/st25/nfc.c b/core/embed/io/nfc/st25/nfc.c
index 480b21b0..cdb1767e 100644
--- a/core/embed/io/nfc/st25/nfc.c
+++ b/core/embed/io/nfc/st25/nfc.c
@@ -262,7 +262,9 @@ void nfc_deinit(void) {
drv->rfal_initialized = false;
}
- HAL_SPI_DeInit(&drv->hspi);
+ if (drv->hspi.Instance != NULL) {
+ HAL_SPI_DeInit(&drv->hspi);
+ }
HAL_GPIO_DeInit(NFC_SPI_MISO_PORT, NFC_SPI_MISO_PIN);
HAL_GPIO_DeInit(NFC_SPI_MOSI_PORT, NFC_SPI_MOSI_PIN);
Why this scored 32/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.