Skip to content

Commit ea8960e

Browse files
hanno-beckermkannwischer
authored andcommitted
CBMC: Use instrumented malloc/free for MLD_ALLOC/MLD_FREE
The default implementation of MLD_ALLOC and MLD_FREE uses stack allocation, which the compiler can assume not to fail. This means that CBMC does not exercise the cleanup paths which handle fallible dynamic allocation -- a significant proof gap. This commit changes the configuration uses for the CBMC proofs to use the (instrumented) calls to malloc/free for MLD_ALLOC/MLD_FREE. Importantly, we set --malloc-may-fail to model allocation as fallible, and --malloc-fail-null to return NULL in case of allocation failure. Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
1 parent 19f0312 commit ea8960e

13 files changed

Lines changed: 65 additions & 15 deletions

File tree

proofs/cbmc/H/H_harness.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ void mld_H(uint8_t *out, size_t outlen, const uint8_t *in1, size_t in1len,
99

1010
void harness(void)
1111
{
12+
{
13+
/* Dummy use of `free` to work around CBMC issue #8814. */
14+
free(NULL);
15+
}
16+
1217
uint8_t *out;
1318
size_t outlen;
1419
const uint8_t *in1;

proofs/cbmc/Makefile.common

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ endif
247247
# * an entire project when added to Makefile-project-defines
248248
# * a specific proof when added to the harness Makefile
249249

250-
CBMC_FLAG_MALLOC_MAY_FAIL ?= --malloc-fail-assert
250+
CBMC_FLAG_MALLOC_MAY_FAIL ?= --malloc-may-fail --malloc-fail-null # alloc may fail with returning NULL
251251
CBMC_FLAG_BOUNDS_CHECK ?= # set to --no-bounds-check to disable
252252
CBMC_FLAG_CONVERSION_CHECK ?= --conversion-check
253253
CBMC_FLAG_DIV_BY_ZERO_CHECK ?= # set to --no-div-by-zero-check to disable

proofs/cbmc/check_pct/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ HARNESS_FILE = check_pct_harness
1010
# Litani dashboard. It can be human-readable and contain spaces if you wish.
1111
PROOF_UID = mld_check_pct
1212

13-
DEFINES += -DMLD_CONFIG_KEYGEN_PCT
13+
DEFINES +=
1414
INCLUDES +=
1515

1616
REMOVE_FUNCTION_BODY +=

proofs/cbmc/compute_pack_z/compute_pack_z_harness.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ int mld_compute_pack_z(uint8_t sig[MLDSA_CRYPTO_BYTES], const mld_poly *cp,
1010

1111
void harness(void)
1212
{
13+
{
14+
/* Dummy use of `free` to work around CBMC issue #8814. */
15+
free(NULL);
16+
}
17+
1318
uint8_t *sig;
1419
mld_poly *cp;
1520
mld_sk_s1hat *s1;

proofs/cbmc/mldsa_native_config_cbmc.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
*/
2626

2727
/*
28-
* Test configuration: mldsa-native configuration used for CBMC proofs
28+
* Test configuration: mldsa-native configuration used for CBMC proofs, using
29+
* instrumented alloc/free
2930
*
3031
* This configuration differs from the default mldsa/mldsa_native_config.h in
3132
* the following places:
3233
* - MLD_CONFIG_NAMESPACE_PREFIX
34+
* - MLD_CONFIG_KEYGEN_PCT
35+
* - MLD_CONFIG_CUSTOM_ALLOC_FREE
3336
*/
3437

3538

@@ -499,15 +502,13 @@
499502
* target pointer should simply be set to NULL. The calling
500503
* code will handle this case and invoke MLD_CUSTOM_FREE.
501504
*/
502-
/* #define MLD_CONFIG_CUSTOM_ALLOC_FREE
503-
#if !defined(__ASSEMBLER__)
504-
#include <stdlib.h>
505-
#define MLD_CUSTOM_ALLOC(v, T, N) \
506-
T* (v) = (T *)aligned_alloc(MLD_DEFAULT_ALIGN, \
507-
MLD_ALIGN_UP(sizeof(T) * (N)))
508-
#define MLD_CUSTOM_FREE(v, T, N) free(v)
509-
#endif
510-
*/
505+
#define MLD_CONFIG_CUSTOM_ALLOC_FREE
506+
#if !defined(__ASSEMBLER__)
507+
#include <stdlib.h>
508+
#define MLD_CUSTOM_ALLOC(v, T, N) T *v = (T *)malloc(sizeof(T) * (N))
509+
#define MLD_CUSTOM_FREE(v, T, N) free(v)
510+
#endif
511+
511512

512513
/**
513514
* MLD_CONFIG_CUSTOM_MEMCPY
@@ -629,7 +630,7 @@
629630
* and MLD_CONFIG_NO_VERIFY_API as the current PCT implementation
630631
* requires signature() and verify().
631632
*/
632-
/* #define MLD_CONFIG_KEYGEN_PCT */
633+
#define MLD_CONFIG_KEYGEN_PCT
633634

634635
/**
635636
* MLD_CONFIG_KEYGEN_PCT_BREAKAGE_TEST

proofs/cbmc/prepare_domain_separation_prefix/prepare_domain_separation_prefix_harness.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
void harness(void)
77
{
8+
{
9+
/* Dummy use of `free` to work around CBMC issue #8814. */
10+
free(NULL);
11+
}
12+
813
uint8_t *prefix;
914
const uint8_t *ph;
1015
size_t phlen;

proofs/cbmc/sample_s1_s2/sample_s1_s2_harness.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ static void mld_sample_s1_s2(mld_polyvecl *s1, mld_polyveck *s2,
88

99
void harness(void)
1010
{
11+
{
12+
/* Dummy use of `free` to work around CBMC issue #8814. */
13+
free(NULL);
14+
}
15+
1116
mld_polyvecl *s1;
1217
mld_polyveck *s2;
1318
uint8_t *seed;

proofs/cbmc/sample_s1_s2_serial/sample_s1_s2_harness.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ static void mld_sample_s1_s2(mld_polyvecl *s1, mld_polyveck *s2,
88

99
void harness(void)
1010
{
11+
{
12+
/* Dummy use of `free` to work around CBMC issue #8814. */
13+
free(NULL);
14+
}
15+
1116
mld_polyvecl *s1;
1217
mld_polyveck *s2;
1318
uint8_t *seed;

proofs/cbmc/sign_attempt/sign_attempt_harness.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
void harness(void)
77
{
8+
{
9+
/* Dummy use of `free` to work around CBMC issue #8814. */
10+
free(NULL);
11+
}
12+
813
uint16_t attempt;
914
int rc;
1015
/* `context` is consumed by the call macro (the CBMC config has no context

proofs/cbmc/sign_finish/sign_finish_harness.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
* parameter), exactly as at the call sites in sign.c. */
88
void harness(void)
99
{
10+
{
11+
/* Dummy use of `free` to work around CBMC issue #8814. */
12+
free(NULL);
13+
}
14+
1015
uint16_t attempt;
1116
mld_sign_finish(attempt, context);
1217
}

0 commit comments

Comments
 (0)