Skip to content

Commit 23c8a81

Browse files
committed
KEYS: Fix ASN.1 indefinite length object parsing
This fixes CVE-2016-0758. In the ASN.1 decoder, when the length field of an ASN.1 value is extracted, it isn't validated against the remaining amount of data before being added to the cursor. With a sufficiently large size indicated, the check: datalen - dp < 2 may then fail due to integer overflow. Fix this by checking the length indicated against the amount of remaining data in both places a definite length is determined. Whilst we're at it, make the following changes: (1) Check the maximum size of extended length does not exceed the capacity of the variable it's being stored in (len) rather than the type that variable is assumed to be (size_t). (2) Compare the EOC tag to the symbolic constant ASN1_EOC rather than the integer 0. (3) To reduce confusion, move the initialisation of len outside of: for (len = 0; n > 0; n--) { since it doesn't have anything to do with the loop counter n. Signed-off-by: David Howells <dhowells@redhat.com> Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com> Acked-by: David Woodhouse <David.Woodhouse@intel.com> Acked-by: Peter Jones <pjones@redhat.com>
1 parent 685764b commit 23c8a81

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎lib/asn1_decoder.c‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static int asn1_find_indefinite_length(const unsigned char *data, size_t datalen
7474

7575
/* Extract a tag from the data */
7676
tag = data[dp++];
77-
if (tag == 0) {
77+
if (tag == ASN1_EOC) {
7878
/* It appears to be an EOC. */
7979
if (data[dp++] != 0)
8080
goto invalid_eoc;
@@ -96,10 +96,8 @@ static int asn1_find_indefinite_length(const unsigned char *data, size_t datalen
9696

9797
/* Extract the length */
9898
len = data[dp++];
99-
if (len <= 0x7f) {
100-
dp += len;
101-
goto next_tag;
102-
}
99+
if (len <= 0x7f)
100+
goto check_length;
103101

104102
if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
105103
/* Indefinite length */
@@ -110,14 +108,18 @@ static int asn1_find_indefinite_length(const unsigned char *data, size_t datalen
110108
}
111109

112110
n = len - 0x80;
113-
if (unlikely(n > sizeof(size_t) - 1))
111+
if (unlikely(n > sizeof(len) - 1))
114112
goto length_too_long;
115113
if (unlikely(n > datalen - dp))
116114
goto data_overrun_error;
117-
for (len = 0; n > 0; n--) {
115+
len = 0;
116+
for (; n > 0; n--) {
118117
len <<= 8;
119118
len |= data[dp++];
120119
}
120+
check_length:
121+
if (len > datalen - dp)
122+
goto data_overrun_error;
121123
dp += len;
122124
goto next_tag;
123125

0 commit comments

Comments
 (0)