Skip to content

Commit 3cb057f

Browse files
Fix possible integer overflow in DER parsing
If we’re in the last loop iteration, then `lenleft == 1` and it could be the case that `ret == MAX_SIZE`, and so `ret + lenleft` will overflow to 0 and the sanity check will not catch it. Then we will return `(int) MAX_SIZE`, which should be avoided because this value is implementation-defined. (However, this is harmless because `(int) MAX_SIZE == -1` on all supported platforms.)
1 parent 1e6f1f5 commit 3cb057f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/ecdsa_impl.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int secp256k1_der_read_len(const unsigned char **sigp, const unsigned cha
6666
return -1;
6767
}
6868
/* X.690-207 8.1.3.5 long form length octets */
69-
lenleft = b1 & 0x7F;
69+
lenleft = b1 & 0x7F; /* lenleft is at least 1 */
7070
if (lenleft > sigend - *sigp) {
7171
return -1;
7272
}
@@ -82,13 +82,13 @@ static int secp256k1_der_read_len(const unsigned char **sigp, const unsigned cha
8282
}
8383
while (lenleft > 0) {
8484
ret = (ret << 8) | **sigp;
85-
if (ret + lenleft > (size_t)(sigend - *sigp)) {
86-
/* Result exceeds the length of the passed array. */
87-
return -1;
88-
}
8985
(*sigp)++;
9086
lenleft--;
9187
}
88+
if (ret > (size_t)(sigend - *sigp)) {
89+
/* Result exceeds the length of the passed array. */
90+
return -1;
91+
}
9292
if (ret < 128) {
9393
/* Not the shortest possible length encoding. */
9494
return -1;

0 commit comments

Comments
 (0)