From e1a05a534aab01167774ac0c0cb1b94f3f395920 Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Wed, 1 Apr 2020 15:52:06 +0200 Subject: [PATCH 1/2] unit tests main: Fix potential buffer overflow Fix potential buffer overflow when tracking the unmet dependencies of a test case. The identifiers of unmet dependencies are stored in an array of fixed size. Ensure that we don't overrun the array. Signed-off-by: Ronald Cron --- tests/suites/host_test.function | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/suites/host_test.function b/tests/suites/host_test.function index 1069c2415..14925ebaf 100644 --- a/tests/suites/host_test.function +++ b/tests/suites/host_test.function @@ -647,8 +647,12 @@ int execute_tests( int argc , const char ** argv ) int dep_id = strtol( params[i], NULL, 10 ); if( dep_check( dep_id ) != DEPENDENCY_SUPPORTED ) { - unmet_dependencies[unmet_dep_count] = dep_id; - unmet_dep_count++; + if( unmet_dep_count < + ARRAY_LENGTH( unmet_dependencies ) ) + { + unmet_dependencies[unmet_dep_count] = dep_id; + unmet_dep_count++; + } } } From 67a8a37b9179617cc3fbb1354b92c77babe93e0a Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Wed, 1 Apr 2020 16:04:41 +0200 Subject: [PATCH 2/2] unit test: Indicate missing unmet dependencies The identifiers of the unmet dependencies of a test case are stored in a buffer of fixed size that can be potentially to small to store all the unmet dependencies. Indicate in test reports if some unmet dependencies are missing. Signed-off-by: Ronald Cron --- tests/suites/host_test.function | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/suites/host_test.function b/tests/suites/host_test.function index 14925ebaf..b6490fbbb 100644 --- a/tests/suites/host_test.function +++ b/tests/suites/host_test.function @@ -418,14 +418,17 @@ static void write_outcome_entry( FILE *outcome_file, * * \param outcome_file The file to write to. * If this is \c NULL, this function does nothing. - * \param unmet_dep_count The number of unmet dependencies. - * \param unmet_dependencies The array of unmet dependencies. + * \param unmet_dep_count The number of unmet dependencies. + * \param unmet_dependencies The array of unmet dependencies. + * \param missing_unmet_dependencies Non-zero if there was a problem tracking + * all unmet dependencies, 0 otherwise. * \param ret The test dispatch status (DISPATCH_xxx). * \param test_info A pointer to the test info structure. */ static void write_outcome_result( FILE *outcome_file, size_t unmet_dep_count, int unmet_dependencies[], + int missing_unmet_dependencies, int ret, const test_info_t *info ) { @@ -447,6 +450,8 @@ static void write_outcome_result( FILE *outcome_file, i == 0 ? ';' : ':', unmet_dependencies[i] ); } + if( missing_unmet_dependencies ) + mbedtls_fprintf( outcome_file, ":..." ); break; } switch( info->result ) @@ -599,6 +604,7 @@ int execute_tests( int argc , const char ** argv ) { size_t unmet_dep_count = 0; int unmet_dependencies[20]; + int missing_unmet_dependencies = 0; test_filename = test_files[ testfile_index ]; @@ -621,6 +627,7 @@ int execute_tests( int argc , const char ** argv ) mbedtls_exit( MBEDTLS_EXIT_FAILURE ); } unmet_dep_count = 0; + missing_unmet_dependencies = 0; if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) break; @@ -653,6 +660,10 @@ int execute_tests( int argc , const char ** argv ) unmet_dependencies[unmet_dep_count] = dep_id; unmet_dep_count++; } + else + { + missing_unmet_dependencies = 1; + } } } @@ -706,6 +717,7 @@ int execute_tests( int argc , const char ** argv ) write_outcome_result( outcome_file, unmet_dep_count, unmet_dependencies, + missing_unmet_dependencies, ret, &test_info ); if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE ) { @@ -725,11 +737,14 @@ int execute_tests( int argc , const char ** argv ) mbedtls_fprintf( stdout, "%d ", unmet_dependencies[i] ); } + if( missing_unmet_dependencies ) + mbedtls_fprintf( stdout, "..." ); } mbedtls_fprintf( stdout, "\n" ); fflush( stdout ); unmet_dep_count = 0; + missing_unmet_dependencies = 0; } else if( ret == DISPATCH_TEST_SUCCESS ) {