From 10b3cc8dd2bcb3dea2f9a5199cdeeac0dbac69c0 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 6 Jun 2025 10:31:06 -0400 Subject: [PATCH 1/3] Add fork test for RAND_poll() --- tests/api.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/api.c b/tests/api.c index adc826ed7..f12ab0bba 100644 --- a/tests/api.c +++ b/tests/api.c @@ -51,6 +51,11 @@ #endif #include + +#ifdef __linux__ +#include +#endif + #include /* compatibility layer */ #include @@ -33142,6 +33147,56 @@ static int test_wolfSSL_RAND(void) } +static int test_wolfSSL_RAND_poll(void) +{ + EXPECT_DECLS; + +#if defined(OPENSSL_EXTRA) && defined(__linux__) + byte seed[16] = {0}; + byte randbuf[8] = {0}; + int pipefds[2] = {0}; + pid_t pid = 0; + + XMEMSET(seed, 0, sizeof(seed)); + + /* No global methods set. */ + ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1); + + ExpectIntEQ(pipe(pipefds), 0); + pid = fork(); + if (pid == 0) + { + ssize_t n_written = 0; + + /* Child process. */ + close(pipefds[0]); + RAND_poll(); + RAND_bytes(randbuf, sizeof(randbuf)); + n_written = write(pipefds[1], randbuf, sizeof(randbuf)); + close(pipefds[1]); + exit(n_written == sizeof(randbuf) ? 0 : 1); + } + else + { + /* Parent process. */ + word64 childrand64 = 0; + + close(pipefds[1]); + ExpectIntEQ(RAND_poll(), 1); + ExpectIntEQ(RAND_bytes(randbuf, sizeof(randbuf)), 1); + ExpectIntEQ(read(pipefds[0], &childrand64, sizeof(childrand64)), sizeof(childrand64)); + ExpectBufNE(randbuf, &childrand64, sizeof(randbuf)); + close(pipefds[0]); + } + RAND_cleanup(); + + ExpectIntEQ(RAND_egd(NULL), -1); +#endif + + return EXPECT_RESULT(); +} + + static int test_wolfSSL_PKCS8_Compat(void) { EXPECT_DECLS; @@ -67671,6 +67726,7 @@ TEST_CASE testCases[] = { TEST_DECL(test_wolfSSL_RAND_set_rand_method), TEST_DECL(test_wolfSSL_RAND_bytes), TEST_DECL(test_wolfSSL_RAND), + TEST_DECL(test_wolfSSL_RAND_poll), /* BN compatibility API */ TEST_DECL(test_wolfSSL_BN_CTX), From 133e238359e16f1dc590a974423839cbde6cc126 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 9 Jun 2025 15:59:22 -0400 Subject: [PATCH 2/3] Wait on child process in RAND_poll fork test --- tests/api.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/api.c b/tests/api.c index f12ab0bba..e95d014b3 100644 --- a/tests/api.c +++ b/tests/api.c @@ -54,6 +54,7 @@ #ifdef __linux__ #include +#include #endif #include /* compatibility layer */ @@ -33180,6 +33181,7 @@ static int test_wolfSSL_RAND_poll(void) { /* Parent process. */ word64 childrand64 = 0; + int waitstatus = 0; close(pipefds[1]); ExpectIntEQ(RAND_poll(), 1); @@ -33187,6 +33189,7 @@ static int test_wolfSSL_RAND_poll(void) ExpectIntEQ(read(pipefds[0], &childrand64, sizeof(childrand64)), sizeof(childrand64)); ExpectBufNE(randbuf, &childrand64, sizeof(randbuf)); close(pipefds[0]); + waitpid(pid, &waitstatus, 0); } RAND_cleanup(); From 1c6e3d729a2bb602ee0516f6be691941eba4b185 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 10 Jun 2025 06:23:06 -0400 Subject: [PATCH 3/3] Check that fork() returns >= 0 in RAND_poll fork test --- tests/api.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/api.c b/tests/api.c index e95d014b3..e401e5cfa 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33165,6 +33165,7 @@ static int test_wolfSSL_RAND_poll(void) ExpectIntEQ(pipe(pipefds), 0); pid = fork(); + ExpectIntGE(pid, 0); if (pid == 0) { ssize_t n_written = 0;