Skip to content

Commit 9f4baec

Browse files
authoredOct 29, 2024
fix: close all /dev/urandom open fds (#4835)
1 parent b8c463b commit 9f4baec

4 files changed

+11
-3
lines changed
 

‎tests/unit/s2n_drbg_test.c

+2
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ int check_drgb_version(s2n_drbg_mode mode, int (*generator)(void *, uint32_t), i
296296
POSIX_GUARD(s2n_stuffer_read(&personalization, &personalization_string));
297297

298298
/* Over-ride the entropy sources */
299+
POSIX_GUARD_RESULT(s2n_rand_cleanup());
299300
POSIX_GUARD(s2n_rand_set_callbacks(nist_fake_entropy_init_cleanup, nist_fake_entropy_init_cleanup, generator, generator));
301+
POSIX_GUARD_RESULT(s2n_rand_init());
300302

301303
/* Instantiate the DRBG */
302304
POSIX_GUARD_RESULT(s2n_drbg_instantiate(&nist_drbg, &personalization_string, mode));

‎tests/unit/s2n_fork_generation_number_test.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ static int s2n_unit_test_clone_child_process(void *parent_process_fgn)
189189
/* Verify in threads */
190190
EXPECT_EQUAL(s2n_unit_test_thread(return_fork_generation_number), S2N_SUCCESS);
191191

192-
/* This translates to the exit code for this child process */
193-
return EXIT_SUCCESS;
192+
exit(EXIT_SUCCESS);
194193
}
195194

196195
#define PROCESS_CHILD_STACK_SIZE (1024 * 1024) /* Suggested by clone() man page... */

‎tests/unit/s2n_override_openssl_random_test.c

+3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ int main(int argc, char **argv)
100100
/* Set s2n_random to use a new fixed DRBG to test that other known answer tests with s2n_random and OpenSSL are deterministic */
101101
EXPECT_OK(s2n_stuffer_alloc_from_hex(&test_entropy, reference_entropy_hex));
102102
struct s2n_drbg drbg;
103+
104+
POSIX_GUARD_RESULT(s2n_rand_cleanup());
103105
EXPECT_SUCCESS(s2n_rand_set_callbacks(s2n_entropy_init_cleanup, s2n_entropy_init_cleanup, s2n_entropy_generator, s2n_entropy_generator));
106+
POSIX_GUARD_RESULT(s2n_rand_init());
104107

105108
s2n_stack_blob(personalization_string, 32, 32);
106109
EXPECT_OK(s2n_drbg_instantiate(&drbg, &personalization_string, S2N_AES_256_CTR_NO_DF_PR));

‎tests/unit/s2n_random_test.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,13 @@ static int s2n_random_invalid_urandom_fd_cb(struct random_test_case *test_case)
840840

841841
EXPECT_TRUE(dev_urandom->fd > STDERR_FILENO);
842842
if (test == 0) {
843-
/* Close the file descriptor. */
843+
/* Test 0 tests for closed dev_urandom fd */
844844
EXPECT_EQUAL(close(dev_urandom->fd), 0);
845845
} else {
846+
/* Test 1 tests for invalid value for dev_urandom.
847+
* Needs to close the previously set dev_urandom fd
848+
* before making it invalid. */
849+
EXPECT_EQUAL(close(dev_urandom->fd), 0);
846850
/* Make the file descriptor invalid by pointing it to STDERR. */
847851
dev_urandom->fd = STDERR_FILENO;
848852
}

0 commit comments

Comments
 (0)
Failed to load comments.