Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep RNG seed file descriptor open until the RNG is freed. #7586

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions wolfcrypt/src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,10 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
}
#endif

#ifndef USE_WINDOWS_API
rng->seed.fd = 0;
#endif

#ifdef CUSTOM_RAND_GENERATE_BLOCK
ret = 0; /* success */
#else
Expand Down Expand Up @@ -2026,6 +2030,13 @@ int wc_FreeRng(WC_RNG* rng)
ret = WC_HW_E;
#endif

#ifndef USE_WINDOWS_API
if(rng->seed.fd != 0 && rng->seed.fd != -1) {
close(rng->seed.fd);
rng->seed.fd = -1;
}
#endif

return ret;
}

Expand Down Expand Up @@ -3990,20 +4001,22 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
#endif /* HAVE_INTEL_RDSEED || HAVE_AMD_RDSEED */

#ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
os->fd = open("/dev/urandom", O_RDONLY);
if (os->fd == 0 || os->fd == -1) {
os->fd = open("/dev/urandom", O_RDONLY);
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("opened /dev/urandom.");
#endif
if (os->fd == -1)
#endif
{
/* may still have /dev/random */
os->fd = open("/dev/random", O_RDONLY);
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("opened /dev/urandom.");
WOLFSSL_MSG("opened /dev/random.");
#endif
if (os->fd == -1)
#endif
{
/* may still have /dev/random */
os->fd = open("/dev/random", O_RDONLY);
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("opened /dev/random.");
#endif
if (os->fd == -1)
return OPEN_RAN_E;
if (os->fd == -1)
return OPEN_RAN_E;
}
}
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("rnd read...");
Expand All @@ -4027,7 +4040,6 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
#endif
}
}
close(os->fd);

return ret;
}
Expand Down
Loading