Skip to content

Commit c82bb82

Browse files
l1kgregkh
authored andcommitted
crypto: ecdsa - Avoid signed integer overflow on signature decoding
[ Upstream commit 3b0565c ] When extracting a signature component r or s from an ASN.1-encoded integer, ecdsa_get_signature_rs() subtracts the expected length "bufsize" from the ASN.1 length "vlen" (both of unsigned type size_t) and stores the result in "diff" (of signed type ssize_t). This results in a signed integer overflow if vlen > SSIZE_MAX + bufsize. The kernel is compiled with -fno-strict-overflow, which implies -fwrapv, meaning signed integer overflow is not undefined behavior. And the function does check for overflow: if (-diff >= bufsize) return -EINVAL; So the code is fine in principle but not very obvious. In the future it might trigger a false-positive with CONFIG_UBSAN_SIGNED_WRAP=y. Avoid by comparing the two unsigned variables directly and erroring out if "vlen" is too large. Signed-off-by: Lukas Wunner <[email protected]> Reviewed-by: Stefan Berger <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent b6ce2db commit c82bb82

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

crypto/ecdsa.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,24 @@ static int ecdsa_get_signature_rs(u64 *dest, size_t hdrlen, unsigned char tag,
3636
const void *value, size_t vlen, unsigned int ndigits)
3737
{
3838
size_t bufsize = ndigits * sizeof(u64);
39-
ssize_t diff = vlen - bufsize;
4039
const char *d = value;
4140

42-
if (!value || !vlen)
41+
if (!value || !vlen || vlen > bufsize + 1)
4342
return -EINVAL;
4443

45-
/* diff = 0: 'value' has exacly the right size
46-
* diff > 0: 'value' has too many bytes; one leading zero is allowed that
47-
* makes the value a positive integer; error on more
48-
* diff < 0: 'value' is missing leading zeros
44+
/*
45+
* vlen may be 1 byte larger than bufsize due to a leading zero byte
46+
* (necessary if the most significant bit of the integer is set).
4947
*/
50-
if (diff > 0) {
48+
if (vlen > bufsize) {
5149
/* skip over leading zeros that make 'value' a positive int */
5250
if (*d == 0) {
5351
vlen -= 1;
54-
diff--;
5552
d++;
56-
}
57-
if (diff)
53+
} else {
5854
return -EINVAL;
55+
}
5956
}
60-
if (-diff >= bufsize)
61-
return -EINVAL;
6257

6358
ecc_digits_from_bytes(d, vlen, dest, ndigits);
6459

0 commit comments

Comments
 (0)