Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport 2.7: Check for zero length and NULL buffer pointer #2794

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Bugfix
* Update test certificates that were about to expire. Reported by
Bernhard M. Wiedemann in #2357.
* Make NV seed test support MBEDTLS_ENTROPY_FORCE_SHA256.
* Fix `mbedtls_zeroize()` to check for zero length.

Changes
* Make `make clean` clean all programs always. Fixes #1862.
Expand Down
11 changes: 9 additions & 2 deletions library/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@
#if !defined(MBEDTLS_AES_ALT)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the other implementations of zeroize that call memset, we probably don't need this if here as there is no undefined behavior. If n is 0, the loop is not executed. The local volatile pointer assignment won't do anything meaningful, since we don't use p at all when n is 0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no bug to fix in 2.7, is there?

{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/arc4.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@
#if !defined(MBEDTLS_ARC4_ALT)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
Expand Down
11 changes: 9 additions & 2 deletions library/asn1parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@
#endif

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n ) {
}

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Expand Down
11 changes: 9 additions & 2 deletions library/blowfish.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@
#if !defined(MBEDTLS_BLOWFISH_ALT)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/camellia.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@
#if !defined(MBEDTLS_CAMELLIA_ALT)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/ccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@
#if !defined(MBEDTLS_CCM_ALT)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

#define CCM_ENCRYPT 0
Expand Down
11 changes: 9 additions & 2 deletions library/cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@
#endif

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

static int supported_init = 0;
Expand Down
11 changes: 9 additions & 2 deletions library/cmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@
#if !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/ctr_drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@
#endif /* MBEDTLS_SELF_TEST */

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/des.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@
#if !defined(MBEDTLS_DES_ALT)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/dhm.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@

#if !defined(MBEDTLS_DHM_ALT)
/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/ecp.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@
#endif

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

#if defined(MBEDTLS_SELF_TEST)
Expand Down
11 changes: 9 additions & 2 deletions library/entropy.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@
#endif

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
Expand Down
11 changes: 9 additions & 2 deletions library/gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,15 @@
#endif

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/havege.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@
#endif

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/* ------------------------------------------------------------------------
Expand Down
11 changes: 9 additions & 2 deletions library/hmac_drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@
#endif /* MBEDTLS_PLATFORM_C */

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/md.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@
#endif

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/md2.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@
#if !defined(MBEDTLS_MD2_ALT)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

static const unsigned char PI_SUBST[256] =
Expand Down
11 changes: 9 additions & 2 deletions library/md4.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@
#if !defined(MBEDTLS_MD4_ALT)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions library/md5.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@
#if !defined(MBEDTLS_MD5_ALT)

/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
static void mbedtls_zeroize( void *v, size_t n )
{
if( n > 0 )
{
volatile unsigned char *p = (unsigned char*)v;

while( n-- )
*p++ = 0;
}
}

/*
Expand Down
Loading