Skip to content

Commit 5c17010

Browse files
committed
macOS: use heap for 300kBytes memory
On macOS, the stack seems to blow up sometimes when using 300kBytes. Therefore, the malicious test now always allocates the buffer on heap. Also, prevent double clearing of memory. Signed-off-by: Stephan Mueller <smueller@chronox.de>
1 parent 1472aad commit 5c17010

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

asn1/api/lc_x509_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ struct lc_x509_certificate {
303303
unsigned int
304304
unsupported_sig : 1; /* T if signature uses unsupported crypto */
305305
unsigned int blacklisted : 1;
306-
unsigned int preallocated : 1;
306+
unsigned int allocated : 1;
307307
};
308308

309309
/// \endcond

asn1/src/pkcs7_memory.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ int pkcs7_sinfo_get(struct lc_pkcs7_signed_info **sinfo,
8888

8989
void pkcs7_x509_free(struct lc_x509_certificate *x509)
9090
{
91-
if (x509->preallocated) {
91+
if (x509->allocated) {
9292
lc_x509_cert_clear(x509);
93+
lc_free(x509);
9394
} else {
9495
lc_x509_cert_clear(x509);
95-
lc_free(x509);
9696
}
9797
}
9898

@@ -109,10 +109,10 @@ int pkcs7_x509_get(struct lc_x509_certificate **x509,
109109
tmp_x509 = pkcs7->preallocated_x509;
110110
pkcs7->consumed_preallocated_x509++;
111111
pkcs7->preallocated_x509++;
112-
tmp_x509->preallocated = 1;
113112
} else {
114113
CKINT(lc_alloc_aligned((void **)&tmp_x509, 8,
115114
sizeof(struct lc_x509_certificate)));
115+
tmp_x509->allocated = 1;
116116
}
117117

118118
*x509 = tmp_x509;

asn1/src/x509_cert_parser.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ static int x509_fabricate_name(struct x509_parse_context *ctx, size_t hdrlen,
404404
data + ctx->o_offset, 7) == 0)
405405
goto single_component;
406406

407-
if (ctx->o_size + 2 + ctx->cn_size + 1 >
407+
if (ctx->o_size + 2 + ctx->cn_size + 1 >=
408408
LC_ASN1_MAX_ISSUER_NAME) {
409409
ret = -EOVERFLOW;
410410
goto out;
@@ -431,7 +431,7 @@ static int x509_fabricate_name(struct x509_parse_context *ctx, size_t hdrlen,
431431
}
432432

433433
single_component:
434-
if (namesize > LC_ASN1_MAX_ISSUER_NAME) {
434+
if (namesize >= LC_ASN1_MAX_ISSUER_NAME) {
435435
ret = -EOVERFLOW;
436436
goto out;
437437
}
@@ -1133,16 +1133,20 @@ int x509_version(void *context, size_t hdrlen, unsigned char tag,
11331133
LC_INTERFACE_FUNCTION(void, lc_x509_cert_clear,
11341134
struct lc_x509_certificate *cert)
11351135
{
1136-
unsigned int prealloc;
1136+
unsigned char alloc;
11371137

11381138
if (!cert)
11391139
return;
11401140

1141-
prealloc = cert->preallocated;
1141+
alloc = cert->allocated;
11421142
public_key_clear(&cert->pub);
11431143
public_key_signature_clear(&cert->sig);
11441144
lc_memset_secure(cert, 0, sizeof(struct lc_x509_certificate));
1145-
cert->preallocated = prealloc;
1145+
1146+
#pragma GCC diagnostic push
1147+
#pragma GCC diagnostic ignored "-Wconversion"
1148+
cert->allocated = alloc;
1149+
#pragma GCC diagnostic pop
11461150
}
11471151

11481152
LC_INTERFACE_FUNCTION(int, lc_x509_cert_decode,

asn1/tests/pkcs7_trust_malicious1_tester.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828
#include "../../apps/src/lc_x509_generator_helper.h"
2929

30+
/*
31+
* This is a large memory buffer - use heap to allocate it as stack may
32+
* explode on some platforms like macOS.
33+
*/
3034
struct workspace {
3135
struct lc_pkcs7_trust_store trust_store;
3236
struct lc_x509_certificate ca1, ca2, ca3, ca1_dec, ca2_dec, ca3_dec;
@@ -59,8 +63,6 @@ static int pkcs7_malicious_set_cert(struct lc_x509_certificate *cert)
5963

6064
CKINT(lc_x509_cert_set_ca(cert));
6165

62-
cert->preallocated = 1;
63-
6466
out:
6567
return ret;
6668
}
@@ -79,8 +81,6 @@ static int pkcs7_malicious_set_cert2(struct lc_x509_certificate *cert)
7981
CKINT(lc_x509_cert_set_keyusage(cert, "keyCertSign"));
8082
CKINT(lc_x509_cert_set_keyusage(cert, "critical"));
8183

82-
cert->preallocated = 1;
83-
8484
out:
8585
return ret;
8686
}
@@ -145,10 +145,6 @@ static int pkcs7_maclious_gen_certs(struct workspace *ws, int set_ca3_akid_null)
145145
"X.509 decode CA3\n");
146146
CKINT(lc_x509_cert_set_signer(&ws->ca3_dec, &ws->keys3, &ws->ca2_dec));
147147

148-
ws->ca1_dec.preallocated = 1;
149-
ws->ca2_dec.preallocated = 1;
150-
ws->ca3_dec.preallocated = 1;
151-
152148
out:
153149
return ret;
154150
}
@@ -199,7 +195,9 @@ static void pkcs7_malicious_clear(struct workspace *ws)
199195
lc_x509_cert_clear(&ws->ca3_dec);
200196
lc_pkcs7_message_clear(&ws->pkcs7);
201197
lc_pkcs7_message_clear(&ws->pkcs7_dec);
202-
lc_pkcs7_trust_store_clear(&ws->trust_store);
198+
199+
/* Do not clear trust store all all its certs are cleared before */
200+
//lc_pkcs7_trust_store_clear(&ws->trust_store);
203201
}
204202

205203
/*
@@ -208,7 +206,7 @@ static void pkcs7_malicious_clear(struct workspace *ws)
208206
static int pkcs7_maclious_certs8(void)
209207
{
210208
int ret = 0;
211-
LC_DECLARE_MEM(ws, struct workspace, sizeof(uint64_t));
209+
__LC_DECLARE_MEM_HEAP(ws, struct workspace, sizeof(uint64_t));
212210

213211
CKINT(pkcs7_maclious_gen_certs(ws, 1));
214212
CKINT(pkcs7_maclious_gen_msg(ws));
@@ -222,7 +220,7 @@ static int pkcs7_maclious_certs8(void)
222220

223221
out:
224222
pkcs7_malicious_clear(ws);
225-
LC_RELEASE_MEM(ws);
223+
__LC_RELEASE_MEM_HEAP(ws);
226224
return ret;
227225
}
228226

@@ -234,7 +232,7 @@ static int pkcs7_maclious_certs7(void)
234232
{
235233
uint8_t *pk;
236234
int ret = 0;
237-
LC_DECLARE_MEM(ws, struct workspace, sizeof(uint64_t));
235+
__LC_DECLARE_MEM_HEAP(ws, struct workspace, sizeof(uint64_t));
238236

239237
CKINT(pkcs7_maclious_gen_certs(ws, 0));
240238
CKINT(pkcs7_maclious_gen_msg(ws));
@@ -248,7 +246,7 @@ static int pkcs7_maclious_certs7(void)
248246

249247
out:
250248
pkcs7_malicious_clear(ws);
251-
LC_RELEASE_MEM(ws);
249+
__LC_RELEASE_MEM_HEAP(ws);
252250
return ret;
253251
}
254252

@@ -261,7 +259,7 @@ static int pkcs7_maclious_certs6(void)
261259
{
262260
uint8_t *pk;
263261
int ret = 0;
264-
LC_DECLARE_MEM(ws, struct workspace, sizeof(uint64_t));
262+
__LC_DECLARE_MEM_HEAP(ws, struct workspace, sizeof(uint64_t));
265263

266264
CKINT(pkcs7_maclious_gen_certs(ws, 0));
267265
CKINT(pkcs7_maclious_gen_msg(ws));
@@ -279,7 +277,7 @@ static int pkcs7_maclious_certs6(void)
279277

280278
out:
281279
pkcs7_malicious_clear(ws);
282-
LC_RELEASE_MEM(ws);
280+
__LC_RELEASE_MEM_HEAP(ws);
283281
return ret;
284282
}
285283

@@ -293,7 +291,7 @@ static int pkcs7_maclious_certs5(void)
293291
{
294292
uint8_t *pk;
295293
int ret = 0;
296-
LC_DECLARE_MEM(ws, struct workspace, sizeof(uint64_t));
294+
__LC_DECLARE_MEM_HEAP(ws, struct workspace, sizeof(uint64_t));
297295

298296
CKINT(pkcs7_maclious_gen_certs(ws, 0));
299297
CKINT(pkcs7_maclious_gen_msg(ws));
@@ -307,7 +305,7 @@ static int pkcs7_maclious_certs5(void)
307305

308306
out:
309307
pkcs7_malicious_clear(ws);
310-
LC_RELEASE_MEM(ws);
308+
__LC_RELEASE_MEM_HEAP(ws);
311309
return ret;
312310
}
313311

@@ -321,7 +319,7 @@ static int pkcs7_maclious_certs4(void)
321319
{
322320
uint8_t *sig;
323321
int ret = 0;
324-
LC_DECLARE_MEM(ws, struct workspace, sizeof(uint64_t));
322+
__LC_DECLARE_MEM_HEAP(ws, struct workspace, sizeof(uint64_t));
325323

326324
CKINT(pkcs7_maclious_gen_certs(ws, 0));
327325
CKINT(pkcs7_maclious_gen_msg(ws));
@@ -335,7 +333,7 @@ static int pkcs7_maclious_certs4(void)
335333

336334
out:
337335
pkcs7_malicious_clear(ws);
338-
LC_RELEASE_MEM(ws);
336+
__LC_RELEASE_MEM_HEAP(ws);
339337
return ret;
340338
}
341339

@@ -351,7 +349,7 @@ static int pkcs7_maclious_certs4(void)
351349
static int pkcs7_maclious_certs2(void)
352350
{
353351
int ret = 0;
354-
LC_DECLARE_MEM(ws, struct workspace, sizeof(uint64_t));
352+
__LC_DECLARE_MEM_HEAP(ws, struct workspace, sizeof(uint64_t));
355353

356354
CKINT(pkcs7_maclious_gen_certs(ws, 0));
357355
CKINT(pkcs7_maclious_gen_msg(ws));
@@ -368,7 +366,7 @@ static int pkcs7_maclious_certs2(void)
368366

369367
out:
370368
pkcs7_malicious_clear(ws);
371-
LC_RELEASE_MEM(ws);
369+
__LC_RELEASE_MEM_HEAP(ws);
372370
return ret;
373371
}
374372

@@ -378,7 +376,7 @@ static int pkcs7_maclious_certs2(void)
378376
static int pkcs7_maclious_certs1(void)
379377
{
380378
int ret = 0;
381-
LC_DECLARE_MEM(ws, struct workspace, sizeof(uint64_t));
379+
__LC_DECLARE_MEM_HEAP(ws, struct workspace, sizeof(uint64_t));
382380

383381
CKINT(pkcs7_maclious_gen_certs(ws, 0));
384382
CKINT(pkcs7_maclious_gen_msg(ws));
@@ -388,7 +386,7 @@ static int pkcs7_maclious_certs1(void)
388386

389387
out:
390388
pkcs7_malicious_clear(ws);
391-
LC_RELEASE_MEM(ws);
389+
__LC_RELEASE_MEM_HEAP(ws);
392390
return ret;
393391
}
394392

@@ -400,7 +398,7 @@ static int pkcs7_maclious_certs1(void)
400398
static int pkcs7_maclious_certs0(void)
401399
{
402400
int ret = 0;
403-
LC_DECLARE_MEM(ws, struct workspace, sizeof(uint64_t));
401+
__LC_DECLARE_MEM_HEAP(ws, struct workspace, sizeof(uint64_t));
404402

405403
CKINT(pkcs7_maclious_gen_certs(ws, 0));
406404
CKINT(pkcs7_maclious_gen_msg(ws));
@@ -414,7 +412,7 @@ static int pkcs7_maclious_certs0(void)
414412

415413
out:
416414
pkcs7_malicious_clear(ws);
417-
LC_RELEASE_MEM(ws);
415+
__LC_RELEASE_MEM_HEAP(ws);
418416
return ret;
419417
}
420418

0 commit comments

Comments
 (0)