From df29cc8f77bcf09cdd245feeaea452f5f91e4125 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Wed, 27 Nov 2002 12:24:05 +0000 Subject: Add OPENSSL_cleanse() to help cleanse memory and avoid certain compiler and linker optimizations. PR: 343 --- crypto/mem.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'crypto/mem.c') diff --git a/crypto/mem.c b/crypto/mem.c index 03d2569bce..46a00697ce 100644 --- a/crypto/mem.c +++ b/crypto/mem.c @@ -250,6 +250,7 @@ void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int), void *CRYPTO_malloc_locked(int num, const char *file, int line) { void *ret = NULL; + extern unsigned char cleanse_ctr; allow_customize = 0; if (malloc_debug_func != NULL) @@ -264,6 +265,12 @@ void *CRYPTO_malloc_locked(int num, const char *file, int line) if (malloc_debug_func != NULL) malloc_debug_func(ret, num, file, line, 1); + /* Create a dependency on the value of 'cleanse_ctr' so our memory + * sanitisation function can't be optimised out. NB: We only do + * this for >2Kb so the overhead doesn't bother us. */ + if(ret && (num > 2048)) + ((unsigned char *)ret)[0] = cleanse_ctr; + return ret; } @@ -282,6 +289,7 @@ void CRYPTO_free_locked(void *str) void *CRYPTO_malloc(int num, const char *file, int line) { void *ret = NULL; + extern unsigned char cleanse_ctr; allow_customize = 0; if (malloc_debug_func != NULL) @@ -296,6 +304,12 @@ void *CRYPTO_malloc(int num, const char *file, int line) if (malloc_debug_func != NULL) malloc_debug_func(ret, num, file, line, 1); + /* Create a dependency on the value of 'cleanse_ctr' so our memory + * sanitisation function can't be optimised out. NB: We only do + * this for >2Kb so the overhead doesn't bother us. */ + if(ret && (num > 2048)) + ((unsigned char *)ret)[0] = cleanse_ctr; + return ret; } -- cgit v1.2.3