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.)
This commit is contained in:
parent
1e6f1f5ad5
commit
3cb057f842
@ -66,7 +66,7 @@ static int secp256k1_der_read_len(const unsigned char **sigp, const unsigned cha
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/* X.690-207 8.1.3.5 long form length octets */
|
/* X.690-207 8.1.3.5 long form length octets */
|
||||||
lenleft = b1 & 0x7F;
|
lenleft = b1 & 0x7F; /* lenleft is at least 1 */
|
||||||
if (lenleft > sigend - *sigp) {
|
if (lenleft > sigend - *sigp) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -82,13 +82,13 @@ static int secp256k1_der_read_len(const unsigned char **sigp, const unsigned cha
|
|||||||
}
|
}
|
||||||
while (lenleft > 0) {
|
while (lenleft > 0) {
|
||||||
ret = (ret << 8) | **sigp;
|
ret = (ret << 8) | **sigp;
|
||||||
if (ret + lenleft > (size_t)(sigend - *sigp)) {
|
|
||||||
/* Result exceeds the length of the passed array. */
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
(*sigp)++;
|
(*sigp)++;
|
||||||
lenleft--;
|
lenleft--;
|
||||||
}
|
}
|
||||||
|
if (ret > (size_t)(sigend - *sigp)) {
|
||||||
|
/* Result exceeds the length of the passed array. */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
if (ret < 128) {
|
if (ret < 128) {
|
||||||
/* Not the shortest possible length encoding. */
|
/* Not the shortest possible length encoding. */
|
||||||
return -1;
|
return -1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user