Skip to content

Commit e077455

Browse files
committed
Stop raising ERR_R_MALLOC_FAILURE in most places
Since OPENSSL_malloc() and friends report ERR_R_MALLOC_FAILURE, and at least handle the file name and line number they are called from, there's no need to report ERR_R_MALLOC_FAILURE where they are called directly, or when SSLfatal() and RLAYERfatal() is used, the reason `ERR_R_MALLOC_FAILURE` is changed to `ERR_R_CRYPTO_LIB`. There were a number of places where `ERR_R_MALLOC_FAILURE` was reported even though it was a function from a different sub-system that was called. Those places are changed to report ERR_R_{lib}_LIB, where {lib} is the name of that sub-system. Some of them are tricky to get right, as we have a lot of functions that belong in the ASN1 sub-system, and all the `sk_` calls or from the CRYPTO sub-system. Some extra adaptation was necessary where there were custom OPENSSL_malloc() wrappers, and some bugs are fixed alongside these changes. Reviewed-by: Tomas Mraz <[email protected]> Reviewed-by: Hugo Landau <[email protected]> (Merged from openssl#19301)
1 parent 9167a47 commit e077455

File tree

380 files changed

+2220
-2778
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

380 files changed

+2220
-2778
lines changed

crypto/asn1/a_bitstr.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
8282
ASN1_BIT_STRING *ret = NULL;
8383
const unsigned char *p;
8484
unsigned char *s;
85-
int i;
85+
int i = 0;
8686

8787
if (len < 1) {
8888
i = ASN1_R_STRING_TOO_SHORT;
@@ -115,7 +115,6 @@ ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
115115
if (len-- > 1) { /* using one because of the bits left byte */
116116
s = OPENSSL_malloc((int)len);
117117
if (s == NULL) {
118-
i = ERR_R_MALLOC_FAILURE;
119118
goto err;
120119
}
121120
memcpy(s, p, (int)len);
@@ -131,7 +130,8 @@ ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
131130
*pp = p;
132131
return ret;
133132
err:
134-
ERR_raise(ERR_LIB_ASN1, i);
133+
if (i != 0)
134+
ERR_raise(ERR_LIB_ASN1, i);
135135
if ((a == NULL) || (*a != ret))
136136
ASN1_BIT_STRING_free(ret);
137137
return NULL;
@@ -160,10 +160,8 @@ int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
160160
if (!value)
161161
return 1; /* Don't need to set */
162162
c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
163-
if (c == NULL) {
164-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
163+
if (c == NULL)
165164
return 0;
166-
}
167165
if (w + 1 - a->length > 0)
168166
memset(c + a->length, 0, w + 1 - a->length);
169167
a->data = c;

crypto/asn1/a_d2i_fp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
123123

124124
b = BUF_MEM_new();
125125
if (b == NULL) {
126-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
126+
ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
127127
return -1;
128128
}
129129

@@ -134,7 +134,7 @@ int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
134134
want -= diff;
135135

136136
if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
137-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
137+
ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
138138
goto err;
139139
}
140140
i = BIO_read(in, &(b->data[len]), want);
@@ -206,7 +206,7 @@ int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
206206
size_t chunk = want > chunk_max ? chunk_max : want;
207207

208208
if (!BUF_MEM_grow_clean(b, len + chunk)) {
209-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
209+
ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
210210
goto err;
211211
}
212212
want -= chunk;

crypto/asn1/a_digest.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
3636
ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
3737
return 0;
3838
}
39-
if ((str = OPENSSL_malloc(inl)) == NULL) {
40-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
39+
if ((str = OPENSSL_malloc(inl)) == NULL)
4140
return 0;
42-
}
4341
p = str;
4442
i2d(data, &p);
4543

crypto/asn1/a_dup.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ void *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, const void *x)
2828
return NULL;
2929

3030
b = OPENSSL_malloc(i + 10);
31-
if (b == NULL) {
32-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
31+
if (b == NULL)
3332
return NULL;
34-
}
3533
p = b;
3634
i = i2d(x, &p);
3735
p2 = b;
@@ -78,7 +76,7 @@ void *ASN1_item_dup(const ASN1_ITEM *it, const void *x)
7876

7977
i = ASN1_item_i2d(x, &b, it);
8078
if (b == NULL) {
81-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
79+
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
8280
return NULL;
8381
}
8482
p = b;

crypto/asn1/a_i2d_fp.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, const void *x)
4242
return 0;
4343

4444
b = OPENSSL_malloc(n);
45-
if (b == NULL) {
46-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
45+
if (b == NULL)
4746
return 0;
48-
}
4947

5048
p = (unsigned char *)b;
5149
i2d(x, &p);
@@ -91,7 +89,7 @@ int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, const void *x)
9189

9290
n = ASN1_item_i2d(x, &b, it);
9391
if (b == NULL) {
94-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
92+
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
9593
return 0;
9694
}
9795

crypto/asn1/a_int.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,10 @@ ASN1_INTEGER *ossl_c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
303303
} else
304304
ret = *a;
305305

306-
if (ASN1_STRING_set(ret, NULL, r) == 0)
306+
if (ASN1_STRING_set(ret, NULL, r) == 0) {
307+
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
307308
goto err;
309+
}
308310

309311
c2i_ibuf(ret->data, &neg, *pp, len);
310312

@@ -318,7 +320,6 @@ ASN1_INTEGER *ossl_c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
318320
(*a) = ret;
319321
return ret;
320322
err:
321-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
322323
if (a == NULL || *a != ret)
323324
ASN1_INTEGER_free(ret);
324325
return NULL;
@@ -400,7 +401,7 @@ ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
400401
unsigned char *s;
401402
long len = 0;
402403
int inf, tag, xclass;
403-
int i;
404+
int i = 0;
404405

405406
if ((a == NULL) || ((*a) == NULL)) {
406407
if ((ret = ASN1_INTEGER_new()) == NULL)
@@ -430,10 +431,8 @@ ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
430431
* a missing NULL parameter.
431432
*/
432433
s = OPENSSL_malloc((int)len + 1);
433-
if (s == NULL) {
434-
i = ERR_R_MALLOC_FAILURE;
434+
if (s == NULL)
435435
goto err;
436-
}
437436
ret->type = V_ASN1_INTEGER;
438437
if (len) {
439438
if ((*p == 0) && (len != 1)) {
@@ -450,7 +449,8 @@ ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
450449
*pp = p;
451450
return ret;
452451
err:
453-
ERR_raise(ERR_LIB_ASN1, i);
452+
if (i != 0)
453+
ERR_raise(ERR_LIB_ASN1, i);
454454
if ((a == NULL) || (*a != ret))
455455
ASN1_INTEGER_free(ret);
456456
return NULL;
@@ -483,7 +483,7 @@ static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
483483
len = 1;
484484

485485
if (ASN1_STRING_set(ret, NULL, len) == 0) {
486-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
486+
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
487487
goto err;
488488
}
489489

crypto/asn1/a_mbstr.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
145145
free_out = 1;
146146
dest = ASN1_STRING_type_new(str_type);
147147
if (dest == NULL) {
148-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
148+
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
149149
return -1;
150150
}
151151
*out = dest;
152152
}
153153
/* If both the same type just copy across */
154154
if (inform == outform) {
155155
if (!ASN1_STRING_set(dest, in, len)) {
156-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
156+
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
157157
return -1;
158158
}
159159
return str_type;
@@ -185,7 +185,6 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
185185
if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
186186
if (free_out)
187187
ASN1_STRING_free(dest);
188-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
189188
return -1;
190189
}
191190
dest->length = outlen;

crypto/asn1/a_object.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
3131
return objsize;
3232

3333
if (*pp == NULL) {
34-
if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
35-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
34+
if ((p = allocated = OPENSSL_malloc(objsize)) == NULL)
3635
return 0;
37-
}
3836
} else {
3937
p = *pp;
4038
}
@@ -135,10 +133,8 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
135133
OPENSSL_free(tmp);
136134
tmpsize = blsize + 32;
137135
tmp = OPENSSL_malloc(tmpsize);
138-
if (tmp == NULL) {
139-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
136+
if (tmp == NULL)
140137
goto err;
141-
}
142138
}
143139
while (blsize--) {
144140
BN_ULONG t = BN_div_word(bl, 0x80L);
@@ -196,10 +192,8 @@ int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
196192
ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG);
197193
return -1;
198194
}
199-
if ((p = OPENSSL_malloc(i + 1)) == NULL) {
200-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
195+
if ((p = OPENSSL_malloc(i + 1)) == NULL)
201196
return -1;
202-
}
203197
i2t_ASN1_OBJECT(p, i + 1, a);
204198
}
205199
if (i <= 0) {
@@ -308,10 +302,8 @@ ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
308302
ret->length = 0;
309303
OPENSSL_free(data);
310304
data = OPENSSL_malloc(length);
311-
if (data == NULL) {
312-
i = ERR_R_MALLOC_FAILURE;
305+
if (data == NULL)
313306
goto err;
314-
}
315307
ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
316308
}
317309
memcpy(data, p, length);
@@ -345,10 +337,8 @@ ASN1_OBJECT *ASN1_OBJECT_new(void)
345337
ASN1_OBJECT *ret;
346338

347339
ret = OPENSSL_zalloc(sizeof(*ret));
348-
if (ret == NULL) {
349-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
340+
if (ret == NULL)
350341
return NULL;
351-
}
352342
ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
353343
return ret;
354344
}

crypto/asn1/a_sign.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
3535
X509_ALGOR *a;
3636

3737
if (ctx == NULL) {
38-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
38+
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
3939
goto err;
4040
}
4141
for (i = 0; i < 2; i++) {
@@ -82,7 +82,6 @@ int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
8282
buf_out = OPENSSL_malloc(outll);
8383
if (buf_in == NULL || buf_out == NULL) {
8484
outl = 0;
85-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
8685
goto err;
8786
}
8887
p = buf_in;
@@ -130,7 +129,7 @@ int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1,
130129
EVP_MD_CTX *ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq);
131130

132131
if (ctx == NULL) {
133-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
132+
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
134133
return 0;
135134
}
136135
/* We can use the non _ex variant here since the pkey is already setup */
@@ -270,7 +269,6 @@ int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
270269
buf_out = OPENSSL_malloc(outll);
271270
if (buf_in == NULL || buf_out == NULL) {
272271
outl = 0;
273-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
274272
goto err;
275273
}
276274

crypto/asn1/a_strex.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,8 @@ static int do_dump(unsigned long lflags, char_io *io_ch, void *arg,
282282
der_len = i2d_ASN1_TYPE(&t, NULL);
283283
if (der_len <= 0)
284284
return -1;
285-
if ((der_buf = OPENSSL_malloc(der_len)) == NULL) {
286-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
285+
if ((der_buf = OPENSSL_malloc(der_len)) == NULL)
287286
return -1;
288-
}
289287
p = der_buf;
290288
i2d_ASN1_TYPE(&t, &p);
291289
outlen = do_hex_dump(io_ch, arg, der_buf, der_len);

crypto/asn1/a_strnid.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ static ASN1_STRING_TABLE *stable_get(int nid)
159159
tmp = ASN1_STRING_TABLE_get(nid);
160160
if (tmp != NULL && tmp->flags & STABLE_FLAGS_MALLOC)
161161
return tmp;
162-
if ((rv = OPENSSL_zalloc(sizeof(*rv))) == NULL) {
163-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
162+
if ((rv = OPENSSL_zalloc(sizeof(*rv))) == NULL)
164163
return NULL;
165-
}
166164
if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
167165
OPENSSL_free(rv);
168166
return NULL;
@@ -190,7 +188,7 @@ int ASN1_STRING_TABLE_add(int nid,
190188

191189
tmp = stable_get(nid);
192190
if (tmp == NULL) {
193-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
191+
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
194192
return 0;
195193
}
196194
if (minsize >= 0)

crypto/asn1/a_time.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,8 @@ int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
420420
* new t.data would be freed after ASN1_STRING_copy is done.
421421
*/
422422
t.data = OPENSSL_zalloc(t.length + 1);
423-
if (t.data == NULL) {
424-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
423+
if (t.data == NULL)
425424
goto out;
426-
}
427425
memcpy(t.data, str + 2, t.length);
428426
t.type = V_ASN1_UTCTIME;
429427
}

crypto/asn1/a_verify.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
3333
int ret = -1, i, inl;
3434

3535
if (ctx == NULL) {
36-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
36+
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
3737
goto err;
3838
}
3939
i = OBJ_obj2nid(a->algorithm);
@@ -54,10 +54,8 @@ int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
5454
goto err;
5555
}
5656
buf_in = OPENSSL_malloc((unsigned int)inl);
57-
if (buf_in == NULL) {
58-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
57+
if (buf_in == NULL)
5958
goto err;
60-
}
6159
p = buf_in;
6260

6361
i2d(data, &p);
@@ -206,7 +204,7 @@ int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
206204
goto err;
207205
}
208206
if (buf_in == NULL) {
209-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
207+
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
210208
goto err;
211209
}
212210
inll = inl;

crypto/asn1/ameth_lib.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,8 @@ EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
222222
{
223223
EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
224224

225-
if (ameth == NULL) {
226-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
225+
if (ameth == NULL)
227226
return NULL;
228-
}
229227

230228
ameth->pkey_id = id;
231229
ameth->pkey_base_id = id;
@@ -247,7 +245,6 @@ EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
247245

248246
err:
249247
EVP_PKEY_asn1_free(ameth);
250-
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
251248
return NULL;
252249
}
253250

0 commit comments

Comments
 (0)