From 5b535de47447d56cf02668ccb2609b4f03c9378f Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Sun, 15 Feb 2015 17:34:22 -0500 Subject: [PATCH 01/10] Add a semantic patch to remove casts of malloc. --- scripts/rm-malloc-cast.cocci | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 scripts/rm-malloc-cast.cocci diff --git a/scripts/rm-malloc-cast.cocci b/scripts/rm-malloc-cast.cocci new file mode 100644 index 000000000..04893d97f --- /dev/null +++ b/scripts/rm-malloc-cast.cocci @@ -0,0 +1,7 @@ +@rm_malloc_cast@ +expression x, n; +type T; +@@ + x = +- (T *) + polarssl_malloc(n) From 369e6c20b34be59288cbbecdba47a17a35d927a4 Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Sun, 15 Feb 2015 17:35:38 -0500 Subject: [PATCH 02/10] Apply the semantic patch rm-malloc-cast.cocci. for dir in library programs; do spatch --sp-file scripts/rm-malloc-cast.cocci --dir $dir \ --in-place; done --- library/asn1parse.c | 2 +- library/bignum.c | 4 ++-- library/cipher_wrap.c | 12 ++++++------ library/dhm.c | 2 +- library/ecp.c | 4 ++-- library/md_wrap.c | 8 ++++---- library/pem.c | 2 +- library/pkparse.c | 2 +- library/ssl_cache.c | 6 +++--- library/ssl_tls.c | 30 +++++++++++++++--------------- library/x509.c | 4 ++-- library/x509_crl.c | 2 +- library/x509_crt.c | 6 +++--- library/x509_csr.c | 2 +- programs/test/ssl_test.c | 4 ++-- programs/util/pem2der.c | 2 +- 16 files changed, 46 insertions(+), 46 deletions(-) diff --git a/library/asn1parse.c b/library/asn1parse.c index 7e8fc32fa..9c47ba306 100644 --- a/library/asn1parse.c +++ b/library/asn1parse.c @@ -269,7 +269,7 @@ int asn1_get_sequence_of( unsigned char **p, /* Allocate and assign next pointer */ if( *p < end ) { - cur->next = (asn1_sequence *) polarssl_malloc( + cur->next = polarssl_malloc( sizeof( asn1_sequence ) ); if( cur->next == NULL ) diff --git a/library/bignum.c b/library/bignum.c index 0eb95ee4e..df519faa8 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -107,7 +107,7 @@ int mpi_grow( mpi *X, size_t nblimbs ) if( X->n < nblimbs ) { - if( ( p = (t_uint *) polarssl_malloc( nblimbs * ciL ) ) == NULL ) + if( ( p = polarssl_malloc( nblimbs * ciL ) ) == NULL ) return( POLARSSL_ERR_MPI_MALLOC_FAILED ); memset( p, 0, nblimbs * ciL ); @@ -147,7 +147,7 @@ int mpi_shrink( mpi *X, size_t nblimbs ) if( i < nblimbs ) i = nblimbs; - if( ( p = (t_uint *) polarssl_malloc( i * ciL ) ) == NULL ) + if( ( p = polarssl_malloc( i * ciL ) ) == NULL ) return( POLARSSL_ERR_MPI_MALLOC_FAILED ); memset( p, 0, i * ciL ); diff --git a/library/cipher_wrap.c b/library/cipher_wrap.c index e289aa2e9..5ba326137 100644 --- a/library/cipher_wrap.c +++ b/library/cipher_wrap.c @@ -179,7 +179,7 @@ static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, static void * aes_ctx_alloc( void ) { - aes_context *aes = (aes_context *) polarssl_malloc( sizeof( aes_context ) ); + aes_context *aes = polarssl_malloc( sizeof( aes_context ) ); if( aes == NULL ) return( NULL ); @@ -541,7 +541,7 @@ static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, static void * camellia_ctx_alloc( void ) { camellia_context *ctx; - ctx = (camellia_context *) polarssl_malloc( sizeof( camellia_context ) ); + ctx = polarssl_malloc( sizeof( camellia_context ) ); if( ctx == NULL ) return( NULL ); @@ -922,7 +922,7 @@ static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, static void * des_ctx_alloc( void ) { - des_context *des = (des_context *) polarssl_malloc( sizeof( des_context ) ); + des_context *des = polarssl_malloc( sizeof( des_context ) ); if( des == NULL ) return( NULL ); @@ -941,7 +941,7 @@ static void des_ctx_free( void *ctx ) static void * des3_ctx_alloc( void ) { des3_context *des3; - des3 = (des3_context *) polarssl_malloc( sizeof( des3_context ) ); + des3 = polarssl_malloc( sizeof( des3_context ) ); if( des3 == NULL ) return( NULL ); @@ -1145,7 +1145,7 @@ static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, static void * blowfish_ctx_alloc( void ) { blowfish_context *ctx; - ctx = (blowfish_context *) polarssl_malloc( sizeof( blowfish_context ) ); + ctx = polarssl_malloc( sizeof( blowfish_context ) ); if( ctx == NULL ) return( NULL ); @@ -1247,7 +1247,7 @@ static int arc4_setkey_wrap( void *ctx, const unsigned char *key, static void * arc4_ctx_alloc( void ) { arc4_context *ctx; - ctx = (arc4_context *) polarssl_malloc( sizeof( arc4_context ) ); + ctx = polarssl_malloc( sizeof( arc4_context ) ); if( ctx == NULL ) return( NULL ); diff --git a/library/dhm.c b/library/dhm.c index fb7826aaf..491b763db 100644 --- a/library/dhm.c +++ b/library/dhm.c @@ -505,7 +505,7 @@ static int load_file( const char *path, unsigned char **buf, size_t *n ) *n = (size_t) size; if( *n + 1 == 0 || - ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL ) + ( *buf = polarssl_malloc( *n + 1 ) ) == NULL ) { fclose( f ); return( POLARSSL_ERR_DHM_MALLOC_FAILED ); diff --git a/library/ecp.c b/library/ecp.c index aca3a2d2c..7c4d6e5ef 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -812,7 +812,7 @@ static int ecp_normalize_jac_many( const ecp_group *grp, if( t_len < 2 ) return( ecp_normalize_jac( grp, *T ) ); - if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL ) + if( ( c = polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL ) return( POLARSSL_ERR_ECP_MALLOC_FAILED ); mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi ); @@ -1415,7 +1415,7 @@ static int ecp_mul_comb( ecp_group *grp, ecp_point *R, if( T == NULL ) { - T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) ); + T = polarssl_malloc( pre_len * sizeof( ecp_point ) ); if( T == NULL ) { ret = POLARSSL_ERR_ECP_MALLOC_FAILED; diff --git a/library/md_wrap.c b/library/md_wrap.c index 62110ce76..ea0c1d207 100644 --- a/library/md_wrap.c +++ b/library/md_wrap.c @@ -396,7 +396,7 @@ static void ripemd160_hmac_reset_wrap( void *ctx ) static void * ripemd160_ctx_alloc( void ) { ripemd160_context *ctx; - ctx = (ripemd160_context *) polarssl_malloc( sizeof( ripemd160_context ) ); + ctx = polarssl_malloc( sizeof( ripemd160_context ) ); if( ctx == NULL ) return( NULL ); @@ -492,7 +492,7 @@ static void sha1_hmac_reset_wrap( void *ctx ) static void * sha1_ctx_alloc( void ) { sha1_context *ctx; - ctx = (sha1_context *) polarssl_malloc( sizeof( sha1_context ) ); + ctx = polarssl_malloc( sizeof( sha1_context ) ); if( ctx == NULL ) return( NULL ); @@ -701,7 +701,7 @@ static void sha256_hmac_wrap( const unsigned char *key, size_t keylen, static void * sha256_ctx_alloc( void ) { sha256_context *ctx; - ctx = (sha256_context *) polarssl_malloc( sizeof( sha256_context ) ); + ctx = polarssl_malloc( sizeof( sha256_context ) ); if( ctx == NULL ) return( NULL ); @@ -907,7 +907,7 @@ static void sha512_hmac_wrap( const unsigned char *key, size_t keylen, static void * sha512_ctx_alloc( void ) { sha512_context *ctx; - ctx = (sha512_context *) polarssl_malloc( sizeof( sha512_context ) ); + ctx = polarssl_malloc( sizeof( sha512_context ) ); if( ctx == NULL ) return( NULL ); diff --git a/library/pem.c b/library/pem.c index aeaa4b68e..b71e6660a 100644 --- a/library/pem.c +++ b/library/pem.c @@ -319,7 +319,7 @@ int pem_read_buffer( pem_context *ctx, const char *header, const char *footer, if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER ) return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); - if( ( buf = (unsigned char *) polarssl_malloc( len ) ) == NULL ) + if( ( buf = polarssl_malloc( len ) ) == NULL ) return( POLARSSL_ERR_PEM_MALLOC_FAILED ); if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 ) diff --git a/library/pkparse.c b/library/pkparse.c index bc4fc6e27..dddd46ad0 100644 --- a/library/pkparse.c +++ b/library/pkparse.c @@ -87,7 +87,7 @@ int pk_load_file( const char *path, unsigned char **buf, size_t *n ) *n = (size_t) size; if( *n + 1 == 0 || - ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL ) + ( *buf = polarssl_malloc( *n + 1 ) ) == NULL ) { fclose( f ); return( POLARSSL_ERR_PK_MALLOC_FAILED ); diff --git a/library/ssl_cache.c b/library/ssl_cache.c index c649129b8..130a75219 100644 --- a/library/ssl_cache.c +++ b/library/ssl_cache.c @@ -102,7 +102,7 @@ int ssl_cache_get( void *data, ssl_session *session ) */ if( entry->peer_cert.p != NULL ) { - if( ( session->peer_cert = (x509_crt *) polarssl_malloc( + if( ( session->peer_cert = polarssl_malloc( sizeof(x509_crt) ) ) == NULL ) { ret = 1; @@ -221,7 +221,7 @@ int ssl_cache_set( void *data, const ssl_session *session ) /* * max_entries not reached, create new entry */ - cur = (ssl_cache_entry *) polarssl_malloc( sizeof(ssl_cache_entry) ); + cur = polarssl_malloc( sizeof(ssl_cache_entry) ); if( cur == NULL ) { ret = 1; @@ -258,7 +258,7 @@ int ssl_cache_set( void *data, const ssl_session *session ) */ if( session->peer_cert != NULL ) { - cur->peer_cert.p = (unsigned char *) polarssl_malloc( + cur->peer_cert.p = polarssl_malloc( session->peer_cert->raw.len ); if( cur->peer_cert.p == NULL ) { diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 4b5418706..321e46933 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -92,7 +92,7 @@ static int ssl_session_copy( ssl_session *dst, const ssl_session *src ) { int ret; - dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) ); + dst->peer_cert = polarssl_malloc( sizeof(x509_crt) ); if( dst->peer_cert == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -111,7 +111,7 @@ static int ssl_session_copy( ssl_session *dst, const ssl_session *src ) #if defined(POLARSSL_SSL_SESSION_TICKETS) if( src->ticket != NULL ) { - dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len ); + dst->ticket = polarssl_malloc( src->ticket_len ); if( dst->ticket == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -2747,7 +2747,7 @@ int ssl_parse_certificate( ssl_context *ssl ) polarssl_free( ssl->session_negotiate->peer_cert ); } - if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc( + if( ( ssl->session_negotiate->peer_cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ) { SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", @@ -3544,19 +3544,19 @@ static int ssl_handshake_init( ssl_context *ssl ) */ if( ssl->transform_negotiate == NULL ) { - ssl->transform_negotiate = (ssl_transform *) polarssl_malloc( + ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) ); } if( ssl->session_negotiate == NULL ) { - ssl->session_negotiate = (ssl_session *) polarssl_malloc( + ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) ); } if( ssl->handshake == NULL ) { - ssl->handshake = (ssl_handshake_params *) + ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) ); } @@ -3630,7 +3630,7 @@ int ssl_init( ssl_context *ssl ) /* * Prepare base structures */ - ssl->in_ctr = (unsigned char *) polarssl_malloc( len ); + ssl->in_ctr = polarssl_malloc( len ); ssl->in_hdr = ssl->in_ctr + 8; ssl->in_iv = ssl->in_ctr + 13; ssl->in_msg = ssl->in_ctr + 13; @@ -3641,7 +3641,7 @@ int ssl_init( ssl_context *ssl ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); } - ssl->out_ctr = (unsigned char *) polarssl_malloc( len ); + ssl->out_ctr = polarssl_malloc( len ); ssl->out_hdr = ssl->out_ctr + 8; ssl->out_iv = ssl->out_ctr + 13; ssl->out_msg = ssl->out_ctr + 13; @@ -3782,7 +3782,7 @@ static int ssl_ticket_keys_init( ssl_context *ssl ) if( ssl->ticket_keys != NULL ) return( 0 ); - tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) ); + tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) ); if( tkeys == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -3939,7 +3939,7 @@ static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl ) { ssl_key_cert *key_cert, *last; - key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) ); + key_cert = polarssl_malloc( sizeof(ssl_key_cert) ); if( key_cert == NULL ) return( NULL ); @@ -3995,7 +3995,7 @@ int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert, if( key_cert == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); - key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) ); + key_cert->key = polarssl_malloc( sizeof(pk_context) ); if( key_cert->key == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -4027,7 +4027,7 @@ int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert, if( key_cert == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); - key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) ); + key_cert->key = polarssl_malloc( sizeof(pk_context) ); if( key_cert->key == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -4063,8 +4063,8 @@ int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len, ssl->psk_len = psk_len; ssl->psk_identity_len = psk_identity_len; - ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len ); - ssl->psk_identity = (unsigned char *) + ssl->psk = polarssl_malloc( ssl->psk_len ); + ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len ); if( ssl->psk == NULL || ssl->psk_identity == NULL ) @@ -4147,7 +4147,7 @@ int ssl_set_hostname( ssl_context *ssl, const char *hostname ) if( ssl->hostname_len + 1 == 0 ) return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); - ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 ); + ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 ); if( ssl->hostname == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); diff --git a/library/x509.c b/library/x509.c index a3cb66943..979878a4d 100644 --- a/library/x509.c +++ b/library/x509.c @@ -445,7 +445,7 @@ int x509_get_name( unsigned char **p, const unsigned char *end, /* Mark this item as being only one in a set */ cur->next_merged = 1; - cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) ); + cur->next = polarssl_malloc( sizeof( x509_name ) ); if( cur->next == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); @@ -461,7 +461,7 @@ int x509_get_name( unsigned char **p, const unsigned char *end, if( *p == end ) return( 0 ); - cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) ); + cur->next = polarssl_malloc( sizeof( x509_name ) ); if( cur->next == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); diff --git a/library/x509_crl.c b/library/x509_crl.c index 2c90582a1..9ca8f0870 100644 --- a/library/x509_crl.c +++ b/library/x509_crl.c @@ -277,7 +277,7 @@ int x509_crl_parse_der( x509_crl *chain, if( crl->version != 0 && crl->next == NULL ) { - crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) ); + crl->next = polarssl_malloc( sizeof( x509_crl ) ); if( crl->next == NULL ) { diff --git a/library/x509_crt.c b/library/x509_crt.c index d1d7d7396..251d82ad9 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -356,7 +356,7 @@ static int x509_get_subject_alt_name( unsigned char **p, if( cur->next != NULL ) return( POLARSSL_ERR_X509_INVALID_EXTENSIONS ); - cur->next = (asn1_sequence *) polarssl_malloc( + cur->next = polarssl_malloc( sizeof( asn1_sequence ) ); if( cur->next == NULL ) @@ -550,7 +550,7 @@ static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf, if( crt == NULL || buf == NULL ) return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); - p = (unsigned char *) polarssl_malloc( len = buflen ); + p = polarssl_malloc( len = buflen ); if( p == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); @@ -807,7 +807,7 @@ int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf, */ if( crt->version != 0 && crt->next == NULL ) { - crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) ); + crt->next = polarssl_malloc( sizeof( x509_crt ) ); if( crt->next == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); diff --git a/library/x509_csr.c b/library/x509_csr.c index a6fe58176..082fed133 100644 --- a/library/x509_csr.c +++ b/library/x509_csr.c @@ -110,7 +110,7 @@ int x509_csr_parse_der( x509_csr *csr, /* * first copy the raw DER data */ - p = (unsigned char *) polarssl_malloc( len = buflen ); + p = polarssl_malloc( len = buflen ); if( p == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); diff --git a/programs/test/ssl_test.c b/programs/test/ssl_test.c index 63c32206c..8ecd9c121 100644 --- a/programs/test/ssl_test.c +++ b/programs/test/ssl_test.c @@ -295,8 +295,8 @@ static int ssl_test( struct options *opt ) } } - read_buf = (unsigned char *) polarssl_malloc( opt->buffer_size ); - write_buf = (unsigned char *) polarssl_malloc( opt->buffer_size ); + read_buf = polarssl_malloc( opt->buffer_size ); + write_buf = polarssl_malloc( opt->buffer_size ); if( read_buf == NULL || write_buf == NULL ) { diff --git a/programs/util/pem2der.c b/programs/util/pem2der.c index 74f7a3e81..50ec94bf5 100644 --- a/programs/util/pem2der.c +++ b/programs/util/pem2der.c @@ -129,7 +129,7 @@ static int load_file( const char *path, unsigned char **buf, size_t *n ) *n = (size_t) size; if( *n + 1 == 0 || - ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL ) + ( *buf = polarssl_malloc( *n + 1 ) ) == NULL ) { fclose( f ); return( -1 ); From bd1d44e25190b2a9458946d1f6056b6965ed03fd Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Sun, 15 Feb 2015 17:46:32 -0500 Subject: [PATCH 03/10] Fix whitespace of 369e6c20. --- library/asn1parse.c | 3 +-- library/ssl_cache.c | 3 +-- library/ssl_tls.c | 12 ++++-------- library/x509_crt.c | 3 +-- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/library/asn1parse.c b/library/asn1parse.c index 9c47ba306..010c1c3e7 100644 --- a/library/asn1parse.c +++ b/library/asn1parse.c @@ -269,8 +269,7 @@ int asn1_get_sequence_of( unsigned char **p, /* Allocate and assign next pointer */ if( *p < end ) { - cur->next = polarssl_malloc( - sizeof( asn1_sequence ) ); + cur->next = polarssl_malloc( sizeof( asn1_sequence ) ); if( cur->next == NULL ) return( POLARSSL_ERR_ASN1_MALLOC_FAILED ); diff --git a/library/ssl_cache.c b/library/ssl_cache.c index 130a75219..40a7d13c0 100644 --- a/library/ssl_cache.c +++ b/library/ssl_cache.c @@ -258,8 +258,7 @@ int ssl_cache_set( void *data, const ssl_session *session ) */ if( session->peer_cert != NULL ) { - cur->peer_cert.p = polarssl_malloc( - session->peer_cert->raw.len ); + cur->peer_cert.p = polarssl_malloc( session->peer_cert->raw.len ); if( cur->peer_cert.p == NULL ) { ret = 1; diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 321e46933..ac35289ab 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -3544,20 +3544,17 @@ static int ssl_handshake_init( ssl_context *ssl ) */ if( ssl->transform_negotiate == NULL ) { - ssl->transform_negotiate = polarssl_malloc( - sizeof(ssl_transform) ); + ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) ); } if( ssl->session_negotiate == NULL ) { - ssl->session_negotiate = polarssl_malloc( - sizeof(ssl_session) ); + ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) ); } if( ssl->handshake == NULL ) { - ssl->handshake = - polarssl_malloc( sizeof(ssl_handshake_params) ); + ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) ); } /* All pointers should exist and can be directly freed without issue */ @@ -4064,8 +4061,7 @@ int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len, ssl->psk_identity_len = psk_identity_len; ssl->psk = polarssl_malloc( ssl->psk_len ); - ssl->psk_identity = - polarssl_malloc( ssl->psk_identity_len ); + ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len ); if( ssl->psk == NULL || ssl->psk_identity == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); diff --git a/library/x509_crt.c b/library/x509_crt.c index 251d82ad9..3327e6886 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -356,8 +356,7 @@ static int x509_get_subject_alt_name( unsigned char **p, if( cur->next != NULL ) return( POLARSSL_ERR_X509_INVALID_EXTENSIONS ); - cur->next = polarssl_malloc( - sizeof( asn1_sequence ) ); + cur->next = polarssl_malloc( sizeof( asn1_sequence ) ); if( cur->next == NULL ) return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + From 961c626d6d494cc520777498536239cd93623568 Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Tue, 17 Feb 2015 13:07:29 -0500 Subject: [PATCH 04/10] Add a semantic patch to find potential memory leaks. --- scripts/find-mem-leak.cocci | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 scripts/find-mem-leak.cocci diff --git a/scripts/find-mem-leak.cocci b/scripts/find-mem-leak.cocci new file mode 100644 index 000000000..a3f7ca99c --- /dev/null +++ b/scripts/find-mem-leak.cocci @@ -0,0 +1,9 @@ +@@ +expression x, y; +statement S; +@@ + x = polarssl_malloc(...); + y = polarssl_malloc(...); + ... +* if (x == NULL || y == NULL) + S From f81088bb80a35644f15bf19fccb71556cee10fe4 Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Tue, 17 Feb 2015 13:10:21 -0500 Subject: [PATCH 05/10] Fix a potential memory leak found by find-mem-leak.cocci. --- library/ssl_tls.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index ac35289ab..4eae92faa 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4063,8 +4063,13 @@ int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len, ssl->psk = polarssl_malloc( ssl->psk_len ); ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len ); - if( ssl->psk == NULL || ssl->psk_identity == NULL ) + if( ssl->psk == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); + if( ssl->psk_identity == NULL ) + { + polarssl_free( ssl->psk ); + return( POLARSSL_ERR_SSL_MALLOC_FAILED ); + } memcpy( ssl->psk, psk, ssl->psk_len ); memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len ); From 263438b071996ae426a5ed3a591d5aca0f81c64a Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Tue, 17 Feb 2015 13:40:48 -0500 Subject: [PATCH 06/10] Add a rule for another type of memory leak to find-mem-leak.cocci. --- scripts/find-mem-leak.cocci | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scripts/find-mem-leak.cocci b/scripts/find-mem-leak.cocci index a3f7ca99c..34cfd082d 100644 --- a/scripts/find-mem-leak.cocci +++ b/scripts/find-mem-leak.cocci @@ -7,3 +7,14 @@ statement S; ... * if (x == NULL || y == NULL) S + +@@ +expression x, y; +statement S; +@@ + if ( +* (x = polarssl_malloc(...)) == NULL + || +* (y = polarssl_malloc(...)) == NULL + ) + S From 5811d3e4ef98a0854a3796e8b50659545137ce0e Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Tue, 17 Feb 2015 13:51:16 -0500 Subject: [PATCH 07/10] Fix another potential memory leak found by find-mem-leak.cocci. --- programs/ssl/ssl_server2.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index a98eff8ab..39420d53a 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -419,13 +419,13 @@ sni_entry *sni_parse( char *sni_string ) while( p <= end ) { if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL ) - return( NULL ); + goto error; memset( new, 0, sizeof( sni_entry ) ); if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL || ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL ) - return( NULL ); + goto error; x509_crt_init( new->cert ); pk_init( new->key ); @@ -436,13 +436,17 @@ sni_entry *sni_parse( char *sni_string ) if( x509_crt_parse_file( new->cert, crt_file ) != 0 || pk_parse_keyfile( new->key, key_file, "" ) != 0 ) - return( NULL ); + goto error; new->next = cur; cur = new; } return( cur ); + +error: + sni_free( new ); + return( NULL ); } void sni_free( sni_entry *head ) From b199095ec9e3ec6792d75ebcfda488e381a5aad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 18 Feb 2015 09:32:06 +0000 Subject: [PATCH 08/10] Finish fixing memleak in ssl_server2 arg parsing Fixes omission in 5c078e1 --- programs/ssl/ssl_server2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index d1b76ac46..30814eb45 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -587,7 +587,7 @@ psk_entry *psk_parse( char *psk_string ) while( p <= end ) { if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL ) - return( NULL ); + goto error; memset( new, 0, sizeof( psk_entry ) ); From df4e44025dd43ab20a06bca36fa72b749dba9487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 18 Feb 2015 10:11:06 +0000 Subject: [PATCH 09/10] Update Changelog --- ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog b/ChangeLog index a834aa7cb..8e1434a08 100644 --- a/ChangeLog +++ b/ChangeLog @@ -24,6 +24,7 @@ Bugfix * Fix warnings from mingw64 in timing.c (found by kxjklele). * Fix potential unintended sign extension in asn1_get_len() on 64-bit platforms. + * Fix potential memory leak in ssl_set_psk() (found by Mansour Moufid). Changes * Move from SHA-1 to SHA-256 in example programs using signatures From f45850c493485a8fdc7b29f78108640430dfa66a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 18 Feb 2015 10:23:52 +0000 Subject: [PATCH 10/10] Fix the fix to ssl_set_psk() - possible for the first malloc to fail and the second to succeed - missing = NULL assignment --- library/ssl_tls.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index cb8a7d99f..6c27dac0f 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4052,26 +4052,23 @@ int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len, if( psk_len > POLARSSL_PSK_MAX_LEN ) return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); - if( ssl->psk != NULL ) + if( ssl->psk != NULL || ssl->psk_identity != NULL ) { polarssl_free( ssl->psk ); polarssl_free( ssl->psk_identity ); } - ssl->psk_len = psk_len; - ssl->psk_identity_len = psk_identity_len; - - ssl->psk = polarssl_malloc( ssl->psk_len ); - ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len ); - - if( ssl->psk == NULL ) - return( POLARSSL_ERR_SSL_MALLOC_FAILED ); - if( ssl->psk_identity == NULL ) + if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL || + ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL ) { polarssl_free( ssl->psk ); + ssl->psk = NULL; return( POLARSSL_ERR_SSL_MALLOC_FAILED ); } + ssl->psk_len = psk_len; + ssl->psk_identity_len = psk_identity_len; + memcpy( ssl->psk, psk, ssl->psk_len ); memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );