u2f: bound DER signature component reads
What changed, and why it matters
This commit fixes a small but real bug in how U2F signature components are converted to DER format. The old code could strip too many leading zeros and then read past the end of a 32-byte signature piece, potentially producing an invalid or malformed DER-encoded signature. The fix ensures at least one byte of each component is always kept. The vendor describes this as defensive hardening, because the device's own signer is not expected to produce the all-zero inputs that trigger the bug.
Treat as a low-severity hardening fix. Merge the patch and run the new unit test. Review whether any other code paths feed externally supplied raw signatures into these helpers, since the bug is only practically reachable if an all-zero R or S component is processed.
Security signals we found
Out-of-bounds read at component boundary during DER encoding
Potential malformed DER output for all-zero signature component
Defensive hardening in U2F signature handling
Duplicate identical fix in test helper ecc_sig_to_der
Evidence from the diff
The function _sig_to_der/ecc_sig_to_der converts a 64-byte raw P-256 ECDSA signature (R || S, 32 bytes each) into DER. The original loops trimmed leading zeros with the condition while (sig[i] == 0 && i < 32) for R and while (sig[i] == 0 && i < 64) for S. If an entire 32-byte component was zero, the loop advanced i to 32 (or 64), then the subsequent if (sig[i] >= 0x80) read one byte beyond the component boundary. The patch changes the bounds to i + 1 < 32 and i + 1 < 64, preserving the final octet so a zero component is encoded as DER INTEGER 0x02 0x01 0x00. A unit test verifies all-zero and mixed leading-zero inputs.
Changed components
src/u2f.c:_sig_to_dertest/unit-test/u2f/ecc.c:ecc_sig_to_derInspect captured patch +21 / −4
### src/u2f.c
@@ -348,7 +348,7 @@ static int _sig_to_der(const uint8_t* sig, uint8_t* der)
// process R
i = 0;
- while (sig[i] == 0 && i < 32) {
+ while (i + 1 < 32 && sig[i] == 0) {
i++; // skip leading zeroes
}
if (sig[i] >= 0x80) { // put zero in output if MSB set
@@ -371,7 +371,7 @@ static int _sig_to_der(const uint8_t* sig, uint8_t* der)
// process S
i = 32;
- while (sig[i] == 0 && i < 64) {
+ while (i + 1 < 64 && sig[i] == 0) {
i++; // skip leading zeroes
}
if (sig[i] >= 0x80) { // put zero in output if MSB set
### test/unit-test/test_u2f_state.c
@@ -462,6 +462,22 @@ static void test_refresh_after_unlock_while_idle(void** state)
_test_refresh_after_unlock(true, false);
}
+static void test_sig_to_der(void** state)
+{
+ (void)state;
+ uint8_t sig[64] = {0};
+ uint8_t der[U2F_MAX_EC_SIG_SIZE] = {0};
+ const uint8_t zero[] = {0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00};
+ assert_int_equal(_sig_to_der(sig, der), sizeof(zero));
+ assert_memory_equal(der, zero, sizeof(zero));
+
+ sig[31] = 0x80;
+ sig[63] = 0x01;
+ const uint8_t padded[] = {0x30, 0x07, 0x02, 0x02, 0x00, 0x80, 0x02, 0x01, 0x01};
+ assert_int_equal(_sig_to_der(sig, der), sizeof(padded));
+ assert_memory_equal(der, padded, sizeof(padded));
+}
+
int main(void)
{
const struct CMUnitTest tests[] = {
@@ -480,6 +496,7 @@ int main(void)
cmocka_unit_test(test_refresh_after_unlock_during_hww_workflow),
cmocka_unit_test(test_refresh_after_unlock_during_u2f_workflow),
cmocka_unit_test(test_refresh_after_unlock_while_idle),
+ cmocka_unit_test(test_sig_to_der),
};
return cmocka_run_group_tests(tests, _setup, NULL);
}
### test/unit-test/u2f/ecc.c
@@ -262,7 +262,7 @@ int ecc_sig_to_der(const uint8_t* sig, uint8_t* der)
// process R
i = 0;
- while (sig[i] == 0 && i < 32) {
+ while (i + 1 < 32 && sig[i] == 0) {
i++; // skip leading zeroes
}
if (sig[i] >= 0x80) { // put zero in output if MSB set
@@ -285,7 +285,7 @@ int ecc_sig_to_der(const uint8_t* sig, uint8_t* der)
// process S
i = 32;
- while (sig[i] == 0 && i < 64) {
+ while (i + 1 < 64 && sig[i] == 0) {
i++; // skip leading zeroes
}
if (sig[i] >= 0x80) { // put zero in output if MSB setWhy this scored 34/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.