git: 921bb16f82d0 - stable/12 - random(4): deduplicate explicit_bzero() in harvest

From: David E. O'Brien <obrien_at_FreeBSD.org>
Date: Mon, 14 Feb 2022 02:04:13 UTC
The branch stable/12 has been updated by obrien:

URL: https://cgit.FreeBSD.org/src/commit/?id=921bb16f82d050c166d45117d1c4d6e3c03a710b

commit 921bb16f82d050c166d45117d1c4d6e3c03a710b
Author:     Conrad Meyer <cem@FreeBSD.org>
AuthorDate: 2019-05-23 21:02:27 +0000
Commit:     David E. O'Brien <obrien@FreeBSD.org>
CommitDate: 2022-02-14 02:01:48 +0000

    random(4): deduplicate explicit_bzero() in harvest
    
    Pull the responsibility for zeroing events, which is general to any
    conceivable implementation of a random device algorithm, out of the
    algorithm-specific Fortuna code and into the callers.  Most callers
    indirect through random_fortuna_process_event(), so add the logic there.
    Most callers already explicitly bzeroed the events they provided, so the
    logic in Fortuna was mostly redundant.
    
    Add one missing bzero in randomdev_accumulate().  Also, remove a redundant
    bzero in the same function -- randomdev_hash_finish() is obliged to bzero
    the hash state.
    
    Reviewed by:    delphij
    Approved by:    secteam(delphij)
    Sponsored by:   Dell EMC Isilon
    Differential Revision:  https://reviews.freebsd.org/D20318
    
    (cherry picked from commit 00e0e488a023967ad9a650f64cb4d77ec83a812d)
---
 sys/dev/random/fortuna.c         | 1 -
 sys/dev/random/random_harvestq.c | 3 +--
 sys/dev/random/randomdev.c       | 2 +-
 3 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/sys/dev/random/fortuna.c b/sys/dev/random/fortuna.c
index e0991d411df3..b0ab6e88c69e 100644
--- a/sys/dev/random/fortuna.c
+++ b/sys/dev/random/fortuna.c
@@ -254,7 +254,6 @@ random_fortuna_process_event(struct harvest_event *event)
 	fortuna_state.fs_pool[pl].fsp_length = MIN(RANDOM_FORTUNA_MAXPOOLSIZE,
 	    fortuna_state.fs_pool[pl].fsp_length +
 	    sizeof(event->he_somecounter) + event->he_size);
-	explicit_bzero(event, sizeof(*event));
 	RANDOM_RESEED_UNLOCK();
 }
 
diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c
index 3cd82571ebc0..d681204832a5 100644
--- a/sys/dev/random/random_harvestq.c
+++ b/sys/dev/random/random_harvestq.c
@@ -152,6 +152,7 @@ random_harvestq_fast_process_event(struct harvest_event *event)
 #if defined(RANDOM_LOADABLE)
 	RANDOM_CONFIG_S_UNLOCK();
 #endif
+	explicit_bzero(event, sizeof(*event));
 }
 
 static void
@@ -414,7 +415,6 @@ random_harvestq_prime(void *unused __unused)
 				    harvest_context.hc_destination[RANDOM_CACHED]++;
 				memcpy(event.he_entropy, data + i, sizeof(event.he_entropy));
 				random_harvestq_fast_process_event(&event);
-				explicit_bzero(&event, sizeof(event));
 			}
 			explicit_bzero(data, size);
 			if (bootverbose)
@@ -517,7 +517,6 @@ random_harvest_direct_(const void *entropy, u_int size, enum random_entropy_sour
 	event.he_destination = harvest_context.hc_destination[origin]++;
 	memcpy(event.he_entropy, entropy, size);
 	random_harvestq_fast_process_event(&event);
-	explicit_bzero(&event, sizeof(event));
 }
 
 void
diff --git a/sys/dev/random/randomdev.c b/sys/dev/random/randomdev.c
index 9e2ddb29ce0b..e20669b3c207 100644
--- a/sys/dev/random/randomdev.c
+++ b/sys/dev/random/randomdev.c
@@ -321,7 +321,6 @@ randomdev_accumulate(uint8_t *buf, u_int count)
 	timestamp = (uint32_t)get_cyclecount();
 	randomdev_hash_iterate(&hash, &timestamp, sizeof(timestamp));
 	randomdev_hash_finish(&hash, entropy_data);
-	explicit_bzero(&hash, sizeof(hash));
 	for (i = 0; i < RANDOM_KEYSIZE_WORDS; i += sizeof(event.he_entropy)/sizeof(event.he_entropy[0])) {
 		event.he_somecounter = (uint32_t)get_cyclecount();
 		event.he_size = sizeof(event.he_entropy);
@@ -330,6 +329,7 @@ randomdev_accumulate(uint8_t *buf, u_int count)
 		memcpy(event.he_entropy, entropy_data + i, sizeof(event.he_entropy));
 		p_random_alg_context->ra_event_processor(&event);
 	}
+	explicit_bzero(&event, sizeof(event));
 	explicit_bzero(entropy_data, sizeof(entropy_data));
 }