From 9a7a36db08f6a0b2d81668146fdb37546d2b3d32 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Wed, 27 Nov 2002 12:24:54 +0000 Subject: [PATCH] Add OPENSSL_cleanse() to help cleanse memory and avoid certain compiler and linker optimizations. PR: 343 --- CHANGES | 8 ++++++++ crypto/Makefile.ssl | 7 +++++-- crypto/crypto-lib.com | 2 +- crypto/crypto.h | 2 ++ crypto/mem.c | 14 ++++++++++++++ util/libeay.num | 1 + 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index e29a9a5efd..3ce964fd80 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,14 @@ Changes between 0.9.6g and 0.9.6h [xx XXX xxxx] + *) New function OPENSSL_cleanse(), which is used to cleanse a section of + memory from it's contents. This is done with a counter that will + place alternating values in each byte. This can be used to solve + two issues: 1) the removal of calls to memset() by highly optimizing + compilers, and 2) cleansing with other values than 0, since those can + be read through on certain media, for example a swap space on disk. + [Geoff Thorpe] + *) Bugfix: client side session caching did not work with external caching, because the session->cipher setting was not restored when reloading from the external cache. This problem was masked, when diff --git a/crypto/Makefile.ssl b/crypto/Makefile.ssl index f1da3aacfc..727d949703 100644 --- a/crypto/Makefile.ssl +++ b/crypto/Makefile.ssl @@ -34,8 +34,8 @@ SDIRS= md2 md5 sha mdc2 hmac ripemd \ GENERAL=Makefile README crypto-lib.com install.com LIB= $(TOP)/libcrypto.a -LIBSRC= cryptlib.c mem.c mem_dbg.c cversion.c ex_data.c tmdiff.c cpt_err.c ebcdic.c uid.c -LIBOBJ= cryptlib.o mem.o mem_dbg.o cversion.o ex_data.o tmdiff.o cpt_err.o ebcdic.o uid.o +LIBSRC= cryptlib.c mem.c mem_clr.c mem_dbg.c cversion.c ex_data.c tmdiff.c cpt_err.c ebcdic.c uid.c +LIBOBJ= cryptlib.o mem.o mem_clr.o mem_dbg.o cversion.o ex_data.o tmdiff.o cpt_err.o ebcdic.o uid.o SRC= $(LIBSRC) @@ -185,6 +185,9 @@ mem.o: ../include/openssl/e_os2.h ../include/openssl/err.h mem.o: ../include/openssl/lhash.h ../include/openssl/opensslconf.h mem.o: ../include/openssl/opensslv.h ../include/openssl/safestack.h mem.o: ../include/openssl/stack.h ../include/openssl/symhacks.h cryptlib.h +mem_clr.o: ../include/openssl/crypto.h ../include/openssl/opensslv.h +mem_clr.o: ../include/openssl/safestack.h ../include/openssl/stack.h +mem_clr.o: ../include/openssl/symhacks.h mem_dbg.o: ../include/openssl/bio.h ../include/openssl/buffer.h mem_dbg.o: ../include/openssl/crypto.h ../include/openssl/e_os.h mem_dbg.o: ../include/openssl/e_os2.h ../include/openssl/err.h diff --git a/crypto/crypto-lib.com b/crypto/crypto-lib.com index 08dc4a6779..5742a159a0 100644 --- a/crypto/crypto-lib.com +++ b/crypto/crypto-lib.com @@ -174,7 +174,7 @@ $! $ APPS_DES = "DES/DES,CBC3_ENC" $ APPS_PKCS7 = "ENC/ENC;DEC/DEC;SIGN/SIGN;VERIFY/VERIFY,EXAMPLE" $ -$ LIB_ = "cryptlib,mem,mem_dbg,cversion,ex_data,tmdiff,cpt_err,ebcdic,uid" +$ LIB_ = "cryptlib,mem,mem_clr,mem_dbg,cversion,ex_data,tmdiff,cpt_err,ebcdic,uid" $ LIB_MD2 = "md2_dgst,md2_one" $ LIB_MD4 = "md4_dgst,md4_one" $ LIB_MD5 = "md5_dgst,md5_one" diff --git a/crypto/crypto.h b/crypto/crypto.h index 4fb17bb356..c539c435b5 100644 --- a/crypto/crypto.h +++ b/crypto/crypto.h @@ -345,6 +345,8 @@ void CRYPTO_free(void *); void *CRYPTO_realloc(void *addr,int num, const char *file, int line); void *CRYPTO_remalloc(void *addr,int num, const char *file, int line); +void OPENSSL_cleanse(void *ptr, size_t len); + void CRYPTO_set_mem_debug_options(long bits); long CRYPTO_get_mem_debug_options(void); diff --git a/crypto/mem.c b/crypto/mem.c index 9df2a367e7..b026fd38aa 100644 --- a/crypto/mem.c +++ b/crypto/mem.c @@ -173,6 +173,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) @@ -187,6 +188,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; } @@ -205,6 +212,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) @@ -219,6 +227,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; } diff --git a/util/libeay.num b/util/libeay.num index 369f14a09a..cfae8e59dd 100755 --- a/util/libeay.num +++ b/util/libeay.num @@ -1934,3 +1934,4 @@ ENGINE_set_ctrl_function 2522 NOEXIST::FUNCTION: BN_pseudo_rand_range 2523 EXIST::FUNCTION: X509_STORE_CTX_set_verify_cb 2524 EXIST::FUNCTION: ERR_load_COMP_strings 2525 EXIST::FUNCTION: +OPENSSL_cleanse 3245 EXIST::FUNCTION: -- 2.25.1