Skip to content

Commit 91ad940

Browse files
authored
fix: Unsets global libcrypto rand (#4424)
1 parent 7436ea0 commit 91ad940

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

tests/s2n_test.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,12 @@ int test_count;
6161
* not initialise at the start of the test. Useful for tests that e.g spawn a
6262
* number of independent childs at the start of a unit test and where you want
6363
* each child to have its own independently initialised s2n.
64-
*
65-
* BEGIN_TEST() prints unit test information to stdout. But this often gets
66-
* buffered by the kernel and will then be flushed in each child spawned. The
67-
* result is a number of repeated messages being send to stdout and, in turn,
68-
* appear in the logs. At the moment, we think this is better than risking not
69-
* having any printing at all.
7064
*/
7165
#define BEGIN_TEST_NO_INIT() \
7266
do { \
7367
test_count = 0; \
7468
fprintf(stdout, "Running %-50s ... ", __FILE__); \
69+
fflush(stdout); \
7570
EXPECT_SUCCESS_WITHOUT_COUNT(s2n_in_unit_test_set(true)); \
7671
S2N_TEST_OPTIONALLY_ENABLE_FIPS_MODE(); \
7772
} while(0)

tests/unit/s2n_random_test.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,30 @@ static int s2n_random_rand_bytes_after_cleanup_cb(struct random_test_case *test_
792792
return S2N_SUCCESS;
793793
}
794794

795+
static int s2n_random_rand_bytes_before_init(struct random_test_case *test_case)
796+
{
797+
#if S2N_LIBCRYPTO_SUPPORTS_CUSTOM_RAND
798+
/* Calling RAND_bytes will set a global random method */
799+
unsigned char rndbytes[16] = { 0 };
800+
EXPECT_EQUAL(RAND_bytes(rndbytes, sizeof(rndbytes)), 1);
801+
const RAND_METHOD *rand_method = RAND_get_rand_method();
802+
EXPECT_NOT_NULL(rand_method);
803+
EXPECT_NOT_EQUAL(rand_method->bytes, s2n_openssl_compat_rand);
804+
805+
EXPECT_SUCCESS(s2n_init());
806+
807+
/* The global random method is overridden after calling s2n_init() */
808+
const RAND_METHOD *custom_rand_method = RAND_get_rand_method();
809+
EXPECT_NOT_NULL(custom_rand_method);
810+
EXPECT_EQUAL(custom_rand_method->bytes, s2n_openssl_compat_rand);
811+
812+
/* RAND_bytes is still successful */
813+
EXPECT_EQUAL(RAND_bytes(rndbytes, sizeof(rndbytes)), 1);
814+
815+
#endif
816+
return S2N_SUCCESS;
817+
}
818+
795819
static int s2n_random_invalid_urandom_fd_cb(struct random_test_case *test_case)
796820
{
797821
EXPECT_SUCCESS(s2n_disable_atexit());
@@ -862,6 +886,7 @@ struct random_test_case random_test_cases[] = {
862886
{ "Test failure.", s2n_random_test_case_failure_cb, CLONE_TEST_DETERMINE_AT_RUNTIME, 1 },
863887
{ "Test libcrypto's RAND engine is reset correctly after manual s2n_cleanup()", s2n_random_rand_bytes_after_cleanup_cb, CLONE_TEST_DETERMINE_AT_RUNTIME, EXIT_SUCCESS },
864888
{ "Test getting entropy with an invalid file descriptor", s2n_random_invalid_urandom_fd_cb, CLONE_TEST_DETERMINE_AT_RUNTIME, EXIT_SUCCESS },
889+
{ "Test libcrypto's global RAND is unset after calling s2n_init()", s2n_random_rand_bytes_before_init, CLONE_TEST_DETERMINE_AT_RUNTIME, EXIT_SUCCESS },
865890
};
866891

867892
int main(int argc, char **argv)

utils/s2n_random.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ S2N_RESULT s2n_rand_init(void)
553553
return S2N_RESULT_OK;
554554
}
555555

556+
/* Unset any existing random engine */
557+
RESULT_GUARD_OSSL(RAND_set_rand_engine(NULL), S2N_ERR_OPEN_RANDOM);
558+
556559
/* Create an engine */
557560
ENGINE *e = ENGINE_new();
558561

utils/s2n_random.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ S2N_RESULT s2n_get_public_random_bytes_used(uint64_t *bytes_used);
4040
S2N_RESULT s2n_get_private_random_data(struct s2n_blob *blob);
4141
S2N_RESULT s2n_get_private_random_bytes_used(uint64_t *bytes_used);
4242
S2N_RESULT s2n_public_random(int64_t max, uint64_t *output);
43+
int s2n_openssl_compat_rand(unsigned char *buf, int num);

0 commit comments

Comments
 (0)