Skip to content

Commit 95c0f5c

Browse files
hfreudeherbertx
authored andcommitted
hwrng: core - Fix wrong quality calculation at hw rng registration
When there are rng sources registering at the hwrng core via hwrng_register() a struct hwrng is delivered. There is a quality field in there which is used to decide which of the registered hw rng sources will be used by the hwrng core. With commit 16bdbae ("hwrng: core - treat default_quality as a maximum and default to 1024") there came in a new default of 1024 in case this field is empty and all the known hw rng sources at that time had been reworked to not fill this field and thus use the default of 1024. The code choosing the 'better' hw rng source during registration of a new hw rng source has never been adapted to this and thus used 0 if the hw rng implementation does not fill the quality field. So when two rng sources register, one with 0 (meaning 1024) and the other one with 999, the 999 hw rng will be chosen. As the later invoked function hwrng_init() anyway adjusts the quality field of the hw rng source, this adjustment is now done during registration of this new hw rng source. Tested on s390 with two hardware rng sources: crypto cards and trng true random generator device driver. Fixes: 16bdbae ("hwrng: core - treat default_quality as a maximum and default to 1024") Reported-by: Christian Rund <[email protected]> Suggested-by: Herbert Xu <[email protected]> Signed-off-by: Harald Freudenberger <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent b0c2036 commit 95c0f5c

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

drivers/char/hw_random/core.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ static int hwrng_init(struct hwrng *rng)
174174
reinit_completion(&rng->cleanup_done);
175175

176176
skip_init:
177-
rng->quality = min_t(u16, min_t(u16, default_quality, 1024), rng->quality ?: 1024);
178177
current_quality = rng->quality; /* obsolete */
179178

180179
return 0;
@@ -563,6 +562,9 @@ int hwrng_register(struct hwrng *rng)
563562
complete(&rng->cleanup_done);
564563
init_completion(&rng->dying);
565564

565+
/* Adjust quality field to always have a proper value */
566+
rng->quality = min_t(u16, min_t(u16, default_quality, 1024), rng->quality ?: 1024);
567+
566568
if (!current_rng ||
567569
(!cur_rng_set_by_user && rng->quality > current_rng->quality)) {
568570
/*

0 commit comments

Comments
 (0)