Skip to content

Commit 837bf7c

Browse files
mbueschherbertx
authored andcommitted
hwrng: core - Always drop the RNG in hwrng_unregister()
enable_best_rng() is used in hwrng_unregister() to switch away from the currently active RNG, if that is the one currently being removed. However enable_best_rng() might fail, if the next RNG's init routine fails. In that case enable_best_rng() will return an error code and the currently active RNG will remain active. After unregistering this might lead to crashes due to use-after-free. Fix this by dropping the currently active RNG, if enable_best_rng() failed. This will result in no RNG to be active, if the next-best one failed to initialize. This problem was introduced by 142a27f Fixes: 142a27f ("hwrng: core - Reset user selected rng by...") Reported-by: Wirz <[email protected]> Tested-by: Wirz <[email protected]> Signed-off-by: Michael Büsch <[email protected]> Cc: [email protected] Signed-off-by: Herbert Xu <[email protected]>
1 parent a81ae80 commit 837bf7c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

drivers/char/hw_random/core.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,18 @@ EXPORT_SYMBOL_GPL(hwrng_register);
516516

517517
void hwrng_unregister(struct hwrng *rng)
518518
{
519+
int err;
520+
519521
mutex_lock(&rng_mutex);
520522

521523
list_del(&rng->list);
522-
if (current_rng == rng)
523-
enable_best_rng();
524+
if (current_rng == rng) {
525+
err = enable_best_rng();
526+
if (err) {
527+
drop_current_rng();
528+
cur_rng_set_by_user = 0;
529+
}
530+
}
524531

525532
if (list_empty(&rng_list)) {
526533
mutex_unlock(&rng_mutex);

0 commit comments

Comments
 (0)