Brian Carlstrom | 243c891 | 2012-04-02 12:21:18 -0700 | [diff] [blame] | 1 | --- openssl/crypto/asn1/a_d2i_fp.c 2011-02-01 06:46:34.000000000 -0800 |
| 2 | +++ openssl/crypto/asn1/a_d2i_fp.c 2012-04-02 10:54:56.000000000 -0700 |
| 3 | @@ -57,6 +57,7 @@ |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | +#include <limits.h> |
| 8 | #include "cryptlib.h" |
| 9 | #include <openssl/buffer.h> |
| 10 | #include <openssl/asn1_mac.h> |
| 11 | @@ -143,17 +144,11 @@ |
| 12 | BUF_MEM *b; |
| 13 | unsigned char *p; |
| 14 | int i; |
| 15 | - int ret=-1; |
| 16 | ASN1_const_CTX c; |
| 17 | - int want=HEADER_SIZE; |
| 18 | + size_t want=HEADER_SIZE; |
| 19 | int eos=0; |
| 20 | -#if defined(__GNUC__) && defined(__ia64) |
| 21 | - /* pathetic compiler bug in all known versions as of Nov. 2002 */ |
| 22 | - long off=0; |
| 23 | -#else |
| 24 | - int off=0; |
| 25 | -#endif |
| 26 | - int len=0; |
| 27 | + size_t off=0; |
| 28 | + size_t len=0; |
| 29 | |
| 30 | b=BUF_MEM_new(); |
| 31 | if (b == NULL) |
| 32 | @@ -169,7 +164,7 @@ |
| 33 | { |
| 34 | want-=(len-off); |
| 35 | |
| 36 | - if (!BUF_MEM_grow_clean(b,len+want)) |
| 37 | + if (len+want < len || !BUF_MEM_grow_clean(b,len+want)) |
| 38 | { |
| 39 | ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE); |
| 40 | goto err; |
| 41 | @@ -181,7 +176,14 @@ |
| 42 | goto err; |
| 43 | } |
| 44 | if (i > 0) |
| 45 | + { |
| 46 | + if (len+i < len) |
| 47 | + { |
| 48 | + ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG); |
| 49 | + goto err; |
| 50 | + } |
| 51 | len+=i; |
| 52 | + } |
| 53 | } |
| 54 | /* else data already loaded */ |
| 55 | |
| 56 | @@ -206,6 +208,11 @@ |
| 57 | { |
| 58 | /* no data body so go round again */ |
| 59 | eos++; |
| 60 | + if (eos < 0) |
| 61 | + { |
| 62 | + ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_HEADER_TOO_LONG); |
| 63 | + goto err; |
| 64 | + } |
| 65 | want=HEADER_SIZE; |
| 66 | } |
| 67 | else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC)) |
| 68 | @@ -220,10 +227,16 @@ |
| 69 | else |
| 70 | { |
| 71 | /* suck in c.slen bytes of data */ |
| 72 | - want=(int)c.slen; |
| 73 | + want=c.slen; |
| 74 | if (want > (len-off)) |
| 75 | { |
| 76 | want-=(len-off); |
| 77 | + if (want > INT_MAX /* BIO_read takes an int length */ || |
| 78 | + len+want < len) |
| 79 | + { |
| 80 | + ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG); |
| 81 | + goto err; |
| 82 | + } |
| 83 | if (!BUF_MEM_grow_clean(b,len+want)) |
| 84 | { |
| 85 | ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE); |
| 86 | @@ -238,11 +251,18 @@ |
| 87 | ASN1_R_NOT_ENOUGH_DATA); |
| 88 | goto err; |
| 89 | } |
| 90 | + /* This can't overflow because |
| 91 | + * |len+want| didn't overflow. */ |
| 92 | len+=i; |
| 93 | - want -= i; |
| 94 | + want-=i; |
| 95 | } |
| 96 | } |
| 97 | - off+=(int)c.slen; |
| 98 | + if (off + c.slen < off) |
| 99 | + { |
| 100 | + ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG); |
| 101 | + goto err; |
| 102 | + } |
| 103 | + off+=c.slen; |
| 104 | if (eos <= 0) |
| 105 | { |
| 106 | break; |
| 107 | @@ -252,9 +272,14 @@ |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | + if (off > INT_MAX) |
| 112 | + { |
| 113 | + ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG); |
| 114 | + goto err; |
| 115 | + } |
| 116 | *pb = b; |
| 117 | return off; |
| 118 | err: |
| 119 | if (b != NULL) BUF_MEM_free(b); |
| 120 | - return(ret); |
| 121 | + return -1; |
| 122 | } |
| 123 | --- openssl/crypto/buffer/buffer.c 2011-02-01 06:46:34.000000000 -0800 |
| 124 | +++ openssl/crypto/buffer/buffer.c 2012-04-02 10:54:56.000000000 -0700 |
| 125 | @@ -60,6 +60,11 @@ |
| 126 | #include "cryptlib.h" |
| 127 | #include <openssl/buffer.h> |
| 128 | |
| 129 | +/* LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That |
| 130 | + * function is applied in several functions in this file and this limit ensures |
| 131 | + * that the result fits in an int. */ |
| 132 | +#define LIMIT_BEFORE_EXPANSION 0x5ffffffc |
| 133 | + |
| 134 | BUF_MEM *BUF_MEM_new(void) |
| 135 | { |
| 136 | BUF_MEM *ret; |
| 137 | @@ -105,6 +110,12 @@ |
| 138 | str->length=len; |
| 139 | return(len); |
| 140 | } |
| 141 | + /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */ |
| 142 | + if (len > LIMIT_BEFORE_EXPANSION) |
| 143 | + { |
| 144 | + BUFerr(BUF_F_BUF_MEM_GROW,ERR_R_MALLOC_FAILURE); |
| 145 | + return 0; |
| 146 | + } |
| 147 | n=(len+3)/3*4; |
| 148 | if (str->data == NULL) |
| 149 | ret=OPENSSL_malloc(n); |
| 150 | @@ -142,6 +153,12 @@ |
| 151 | str->length=len; |
| 152 | return(len); |
| 153 | } |
| 154 | + /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */ |
| 155 | + if (len > LIMIT_BEFORE_EXPANSION) |
| 156 | + { |
| 157 | + BUFerr(BUF_F_BUF_MEM_GROW,ERR_R_MALLOC_FAILURE); |
| 158 | + return 0; |
| 159 | + } |
| 160 | n=(len+3)/3*4; |
| 161 | if (str->data == NULL) |
| 162 | ret=OPENSSL_malloc(n); |
| 163 | --- openssl/crypto/mem.c 2011-02-02 16:53:02.000000000 -0800 |
| 164 | +++ openssl/crypto/mem.c 2012-04-02 10:54:56.000000000 -0700 |
| 165 | @@ -334,6 +334,10 @@ |
| 166 | |
| 167 | if (num <= 0) return NULL; |
| 168 | |
| 169 | + /* We don't support shrinking the buffer. Note the memcpy that copies |
| 170 | + * |old_len| bytes to the new buffer, below. */ |
| 171 | + if (num < old_len) return NULL; |
| 172 | + |
| 173 | if (realloc_debug_func != NULL) |
| 174 | realloc_debug_func(str, NULL, num, file, line, 0); |
| 175 | ret=malloc_ex_func(num,file,line); |