diff options
author | Adam Vartanian <flooey@google.com> | 2017-05-11 09:47:09 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-05-11 09:47:09 +0000 |
commit | b1e20cbecfe2b557a9dcc14caf7f2b7399b53993 (patch) | |
tree | f7665dd7b34aa5368957a3874d165272f2075ff3 | |
parent | a3f5202285f6dd079fd7717a25d8e6f59e4d7e9c (diff) | |
parent | af3380318beca3d86274b3f5bb2ba98d1ecf618d (diff) | |
download | boringssl-b1e20cbecfe2b557a9dcc14caf7f2b7399b53993.tar.gz |
CVE 2016-2109 fix am: ea156ae109 am: e1ea8662fe
am: af3380318b
Change-Id: I9bbcfa8dd7abe7aa4b6669bbd406274134bbc9ad
-rw-r--r-- | src/crypto/asn1/a_d2i_fp.c | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/src/crypto/asn1/a_d2i_fp.c b/src/crypto/asn1/a_d2i_fp.c index 97ec75b5..af03bc0c 100644 --- a/src/crypto/asn1/a_d2i_fp.c +++ b/src/crypto/asn1/a_d2i_fp.c @@ -140,6 +140,7 @@ void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x) #endif #define HEADER_SIZE 8 +#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024) static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb) { BUF_MEM *b; @@ -231,6 +232,7 @@ static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb) want=c.slen; if (want > (len-off)) { + size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE; want-=(len-off); if (want > INT_MAX /* BIO_read takes an int length */ || len+want < len) @@ -238,23 +240,37 @@ static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb) OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); goto err; } - if (!BUF_MEM_grow_clean(b,len+want)) - { - OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); - goto err; - } while (want > 0) { - i=BIO_read(in,&(b->data[len]),want); - if (i <= 0) - { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA); - goto err; - } - /* This can't overflow because - * |len+want| didn't overflow. */ - len+=i; - want-=i; + + /* + * Read content in chunks of increasing size + * so we can return an error for EOF without + * having to allocate the entire content length + * in one go. + */ + size_t chunk = want > chunk_max ? chunk_max : want; + + if (!BUF_MEM_grow_clean(b, len + chunk)) { + OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); + goto err; + } + want -= chunk; + while (chunk > 0) { + i = BIO_read(in, &(b->data[len]), chunk); + if (i <= 0) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA); + goto err; + } + /* + * This can't overflow because |len+want| didn't + * overflow. + */ + len += i; + chunk -= i; + } + if (chunk_max < INT_MAX/2) + chunk_max *= 2; } } if (off + c.slen < off) |