From c72d3f7d8552eb6302ece4823efe8ab68e6512a9 Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Tue, 14 May 2013 13:22:41 +0200
Subject: [PATCH] Possible resource leak on FILE* removed in CTR_DRBG
---
library/ctr_drbg.c | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c
index 8cf03712e..b6a774b90 100644
--- a/library/ctr_drbg.c
+++ b/library/ctr_drbg.c
@@ -348,7 +348,7 @@ int ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len )
#if defined(POLARSSL_FS_IO)
int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path )
{
- int ret;
+ int ret = POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR;
FILE *f;
unsigned char buf[ CTR_DRBG_MAX_INPUT ];
@@ -356,16 +356,19 @@ int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path )
return( POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR );
if( ( ret = ctr_drbg_random( ctx, buf, CTR_DRBG_MAX_INPUT ) ) != 0 )
- return( ret );
+ goto exit;
if( fwrite( buf, 1, CTR_DRBG_MAX_INPUT, f ) != CTR_DRBG_MAX_INPUT )
{
- fclose( f );
- return( POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR );
+ ret = POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR;
+ goto exit;
}
+ ret = 0;
+
+exit:
fclose( f );
- return( 0 );
+ return( ret );
}
int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path )
@@ -382,7 +385,10 @@ int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path )
fseek( f, 0, SEEK_SET );
if( n > CTR_DRBG_MAX_INPUT )
+ {
+ fclose( f );
return( POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG );
+ }
if( fread( buf, 1, n, f ) != n )
{
@@ -390,10 +396,10 @@ int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path )
return( POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR );
}
- ctr_drbg_update( ctx, buf, n );
-
fclose( f );
-
+
+ ctr_drbg_update( ctx, buf, n );
+
return( ctr_drbg_write_seed_file( ctx, path ) );
}
#endif /* POLARSSL_FS_IO */