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

Handle edge case in fr_rand_init() and, we suspect, oveflow (CID #16… #5434

Merged
merged 1 commit into from
Jan 12, 2025
Merged
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
17 changes: 8 additions & 9 deletions src/lib/util/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static _Thread_local bool fr_rand_initialized = false;
void fr_rand_init(void)
{
int fd;
uint8_t *p = (uint8_t *) &fr_rand_pool.randrsl[0];
uint8_t *end = p + sizeof(fr_rand_pool.randrsl);

if (fr_rand_initialized) return;

Expand All @@ -42,15 +44,12 @@ void fr_rand_init(void)

fd = open("/dev/urandom", O_RDONLY);
if (fd >= 0) {
size_t total;
ssize_t this;

total = 0;
while (total < sizeof(fr_rand_pool.randrsl)) {
this = read(fd, fr_rand_pool.randrsl,
sizeof(fr_rand_pool.randrsl) - total);
if ((this < 0) && (errno != EINTR)) break;
if (this > 0) total += this;
ssize_t rcode;

while (p < end) {
rcode = read(fd, p, (size_t) (end - p));
if ((rcode < 0) && (errno != EINTR)) break;
if (rcode > 0) p += rcode;
}
close(fd);
} else {
Expand Down
Loading