From b2950c96dec295542bb51616056ca6ff7565ec05 Mon Sep 17 00:00:00 2001 From: Tony Finch Date: Mon, 25 Apr 2022 15:18:58 +0100 Subject: [PATCH] Revert "Move random number re-seeding out of the hot path" This reverts commit b1bb41603e76a86ec73419418d1e2db52e864058. --- lib/isc/Makefile.am | 1 - lib/isc/lib.c | 2 -- lib/isc/random.c | 20 +++++++++++++++++--- lib/isc/random_p.h | 30 ------------------------------ lib/isc/tests/random_test.c | 5 ----- lib/isc/trampoline.c | 6 ------ 6 files changed, 17 insertions(+), 47 deletions(-) delete mode 100644 lib/isc/random_p.h diff --git a/lib/isc/Makefile.am b/lib/isc/Makefile.am index 94f0a74d3f..c0bb69fa4a 100644 --- a/lib/isc/Makefile.am +++ b/lib/isc/Makefile.am @@ -170,7 +170,6 @@ libisc_la_SOURCES = \ quota.c \ radix.c \ random.c \ - random_p.h \ ratelimiter.c \ regex.c \ region.c \ diff --git a/lib/isc/lib.c b/lib/isc/lib.c index 4d52c5bc2f..7f6b12c4aa 100644 --- a/lib/isc/lib.c +++ b/lib/isc/lib.c @@ -22,7 +22,6 @@ #include "config.h" #include "mem_p.h" #include "os_p.h" -#include "random_p.h" #include "tls_p.h" #include "trampoline_p.h" @@ -43,7 +42,6 @@ void isc__initialize(void) { isc__os_initialize(); isc__mem_initialize(); - isc__random_initialize(); isc__tls_initialize(); isc__trampoline_initialize(); (void)isc_os_ncpus(); diff --git a/lib/isc/random.c b/lib/isc/random.c index 8f804360db..842f4e4fe3 100644 --- a/lib/isc/random.c +++ b/lib/isc/random.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -42,7 +43,6 @@ #include #include "entropy_private.h" -#include "random_p.h" /* * Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) @@ -88,8 +88,11 @@ next(void) { return (result_starstar); } -void -isc__random_initialize(void) { + +static thread_local isc_once_t isc_random_once = ISC_ONCE_INIT; + +static void +isc_random_initialize(void) { int useed[4] = { 0, 0, 0, 1 }; #if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* @@ -105,16 +108,22 @@ isc__random_initialize(void) { uint8_t isc_random8(void) { + RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) == + ISC_R_SUCCESS); return ((uint8_t)next()); } uint16_t isc_random16(void) { + RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) == + ISC_R_SUCCESS); return ((uint16_t)next()); } uint32_t isc_random32(void) { + RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) == + ISC_R_SUCCESS); return (next()); } @@ -126,6 +135,9 @@ isc_random_buf(void *buf, size_t buflen) { REQUIRE(buf != NULL); REQUIRE(buflen > 0); + RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) == + ISC_R_SUCCESS); + for (i = 0; i + sizeof(r) <= buflen; i += sizeof(r)) { r = next(); memmove((uint8_t *)buf + i, &r, sizeof(r)); @@ -137,6 +149,8 @@ isc_random_buf(void *buf, size_t buflen) { uint32_t isc_random_uniform(uint32_t limit) { + RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) == + ISC_R_SUCCESS); /* * Daniel Lemire's nearly-divisionless unbiased bounded random numbers. * diff --git a/lib/isc/random_p.h b/lib/isc/random_p.h deleted file mode 100644 index 3e4917f8b7..0000000000 --- a/lib/isc/random_p.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) Internet Systems Consortium, Inc. ("ISC") - * - * SPDX-License-Identifier: MPL-2.0 - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, you can obtain one at https://mozilla.org/MPL/2.0/. - * - * See the COPYRIGHT file distributed with this work for additional - * information regarding copyright ownership. - */ - -#pragma once - -#include - -/*! \file isc/random_p.h - * \brief For automatically seeding and re-seeding when required. - */ - -ISC_LANG_BEGINDECLS - -void -isc__random_initialize(void); -/*!< - * \brief Seed the thread-local random number state with fresh entropy. - */ - -ISC_LANG_ENDDECLS diff --git a/lib/isc/tests/random_test.c b/lib/isc/tests/random_test.c index 3d8cfdb260..040e1a5f77 100644 --- a/lib/isc/tests/random_test.c +++ b/lib/isc/tests/random_test.c @@ -87,11 +87,6 @@ _setup(void **state) { result = isc_test_begin(NULL, true, 0); assert_int_equal(result, ISC_R_SUCCESS); - /* - * Ensure the RNG has been seeded. - */ - assert_int_not_equal(isc_random32(), 0); - return (0); } diff --git a/lib/isc/trampoline.c b/lib/isc/trampoline.c index a718e097e2..fe01394b9e 100644 --- a/lib/isc/trampoline.c +++ b/lib/isc/trampoline.c @@ -22,7 +22,6 @@ #include #include -#include "random_p.h" #include "trampoline_p.h" #define ISC__TRAMPOLINE_UNUSED 0 @@ -186,11 +185,6 @@ isc__trampoline_attach(isc__trampoline_t *trampoline) { * malloc() + free() calls altogether, as it would foil the fix. */ trampoline->jemalloc_enforce_init = malloc(8); - - /* - * Re-seed the random number generator in each thread. - */ - isc__random_initialize(); } isc_threadresult_t