SFT-7909: size the se_read copy from the caller's buffer
What changed, and why it matters
This commit fixes two related bugs in the firmware that talks to the secure element chip inside the Passport hardware wallet. First, when reading data back from the secure element, the code was copying a number of bytes based on how many bytes arrived on the wire (`actual`) instead of how many bytes the caller's buffer could hold (`len`). That could overflow the caller's buffer if more bytes arrived than expected. Second, a CRC-check function used an 8-bit length variable, so any length above 255 would wrap around to a small number and could falsely match the response's count byte, letting an invalid or truncated response pass verification. The patch copies only `len` bytes and widens the length variable to an int, with an explicit cast only after it has been validated against the count byte.
Treat this as a security fix and include it in the next firmware release. Review other secure-element read/write paths for similar mismatches between bus-received length and caller buffer size. Consider whether the `actual - 3` arithmetic elsewhere in `se_read_response()` and `se_read()` needs additional bounds checks, and ensure callers cannot pass a `len` larger than the actual response payload.
Security signals we found
Buffer overflow / over-read in secure-element read path
Integer width mismatch leading to bypassable length validation
CRC length check could be defeated by uint8_t wraparound
Fix verified by host harness comparing old and new signatures
Secure-element interface code in hardware wallet firmware
Evidence from the diff
In ports/stm32/boards/Passport/common/se.c, se_read() previously performed memcpy(data, tmp + 1, actual - 3), using actual (bytes received from the bus) as the copy length. Since tmp is sized from len + 3 by deserialize() and data/len come from the caller, a large actual could write past the caller’s buffer. The fix copies len bytes. Separately, check_crc() declared its length parameter as uint8_t while callers passed an int. A value >255 wrapped modulo 256 and could equal the response count byte, causing the early length check to pass incorrectly. The parameter is widened to int, the comparison data[0] != length now compares against the full int, and the subsequent se_crc16_chain() call explicitly casts to uint8_t after the comparison has bounded the value. The commit message notes host-harness verification showing a count of 263 is now rejected by the widened signature but accepted by the narrow one.
Changed components
ports/stm32/boards/Passport/common/se.cse_read()check_crc()secure element communication layerInspect captured patch +14 / −3
### ports/stm32/boards/Passport/common/se.c
@@ -292,15 +292,21 @@ static int se_read_response(uint8_t* buf, int max_len) {
return actual / 8;
}
-static bool check_crc(const uint8_t* data, uint8_t length) {
+/*
+ * `length` is checked against the count byte, so it has to represent every value
+ * a caller can pass. A narrower parameter wraps, and a wrapped value can compare
+ * equal to a count that does not describe the response.
+ */
+static bool check_crc(const uint8_t* data, int length) {
uint8_t obs[2] = {0, 0};
if (data[0] != length) {
// length is wrong
return false;
}
- se_crc16_chain(length - 2, data, obs);
+ /* The comparison above succeeded against a uint8_t, so length fits one. */
+ se_crc16_chain((uint8_t)(length - 2), data, obs);
return (obs[0] == data[length - 2] && obs[1] == data[length - 1]);
}
@@ -404,7 +410,12 @@ int se_read(uint8_t* data, uint8_t len) {
}
}
- memcpy(data, tmp + 1, actual - 3);
+ /*
+ * Copy the length the caller asked for. `actual` counts what arrived on
+ * the bus, and tmp only ever holds len + 3 bytes, so it is not a bound
+ * on the destination.
+ */
+ memcpy(data, tmp + 1, len);
/*
* Pause the watchdog in case there's more to doWhy this scored 72/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.