What changed, and why it matters
This commit fixes a function that shuffles arrays (like recovery-seed word order or on-screen PIN layouts) in Trezor's cryptographic code. The original code could misbehave or even crash when given very short or very long inputs, and it used a signed integer type that could turn negative in unexpected ways. The patch adds a safety check, skips shuffling when there is nothing to shuffle, and uses the correct unsigned type. The practical security impact is moderate: it removes a source of non-random or buggy shuffling that could leak information or cause crashes, but the commit itself does not claim to fix an active exploit.
Treat as a hardening fix and include it in the next firmware release. Review all callers of `random_permute()` to confirm none pass `len == 0` or extremely large values. Consider whether a runtime error return is preferable to `assert()` for production builds where asserts may be disabled. No immediate user action is required unless the firmware is built from source without this patch.
Security signals we found
Signed/unsigned integer type mismatch in a security-critical shuffle routine
Potential underflow when `len == 0` leading to out-of-bounds memory access
Potential non-uniform or incorrect permutation for edge-case lengths
Use of `assert()` for input-length precondition enforcement
No changelog entry, suggesting routine hardening rather than announced vulnerability fix
Evidence from the diff
The change modifies random_permute() in crypto/rand.c. Previously the loop variable was int i = len - 1, which is unsafe when len is 0 (underflow to a large negative value) or when len exceeds INT_MAX. The patch: (1) asserts len <= UINT32_MAX, (2) early-returns for len < 2, and (3) changes the loop variable to size_t. This prevents out-of-bounds indexing and infinite loops caused by signed/unsigned mismatch. The fix is defensive and improves correctness of Fisher-Yates-style shuffling used for security-sensitive permutations.
Changed components
crypto/rand.crandom_permute()Any Trezor firmware feature relying on random_permute (e.g., recovery seed shuffle, PIN matrix layout)Inspect captured patch +7 / −1
### crypto/rand.c
@@ -21,6 +21,8 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/
+#include <assert.h>
+
#include "rand.h"
uint32_t random_uniform(uint32_t n) {
@@ -30,7 +32,11 @@ uint32_t random_uniform(uint32_t n) {
}
void random_permute(char *str, size_t len) {
- for (int i = len - 1; i >= 1; i--) {
+ assert(len <= UINT32_MAX);
+ if (len < 2) {
+ return;
+ }
+ for (size_t i = len - 1; i >= 1; i--) {
uint32_t j = random_uniform(i + 1);
char t = str[j];
str[j] = str[i];Why this scored 38/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.