Skip to content

Commit db108ac

Browse files
committed
Merge remote-tracking branch 'hanno/mpi_read_file_underflow' into development
* hanno/mpi_read_file_underflow: Fix potential stack underflow in mpi_read_file.
2 parents 1178ac5 + b2034b7 commit db108ac

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

ChangeLog

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Bugfix
2828
Found by blaufish. Fixes #641.
2929
* Fix incorrect sign computation in modular exponentiation
3030
when dealing with negative MPI. Found by Guido Vranken.
31+
* Fix potential stack underflow in mpi_read_file.
32+
Found by Guido Vranken.
3133

3234
Changes
3335
* Send fatal alerts in many more cases instead of dropping the connection.

include/mbedtls/bignum.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
340340

341341
#if defined(MBEDTLS_FS_IO)
342342
/**
343-
* \brief Read X from an opened file
343+
* \brief Read MPI from a line in an opened file
344344
*
345345
* \param X Destination MPI
346346
* \param radix Input numeric base
@@ -349,6 +349,15 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
349349
* \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
350350
* the file read buffer is too small or a
351351
* MBEDTLS_ERR_MPI_XXX error code
352+
*
353+
* \note On success, this function advances the file stream
354+
* to the end of the current line or to EOF.
355+
*
356+
* The function returns 0 on an empty line.
357+
*
358+
* Leading whitespaces are ignored, as is a
359+
* '0x' prefix for radix 16.
360+
*
352361
*/
353362
int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
354363

library/bignum.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -616,11 +616,11 @@ int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
616616
if( slen == sizeof( s ) - 2 )
617617
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
618618

619-
if( s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
620-
if( s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
619+
if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
620+
if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
621621

622622
p = s + slen;
623-
while( --p >= s )
623+
while( p-- > s )
624624
if( mpi_get_digit( &d, radix, *p ) != 0 )
625625
break;
626626

0 commit comments

Comments
 (0)