Skip to contentSkip to conversation

hw_random: Always drop the RNG in hwrng_unregister()

PATCH In review Michael Büsch 3 participants 3 messages Jun 14, 2018 – Jun 15, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw_random: 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 142a27f0a731ddcf467546960a5585970ca98e21

---

See this discussion for a crash in b43's hwrng caused by this problem:
https://www.spinics.net/lists/linux-wireless/msg173089.html

drivers/char/hw_random/core.c +9 -2
index 91bb98c42a1c..aaf9e5afaad4 100644
@@ -516,11 +516,18 @@ EXPORT_SYMBOL_GPL(hwrng_register);
 
 void hwrng_unregister(struct hwrng *rng)
 {
+	int err;
+
 	mutex_lock(&rng_mutex);
 
 	list_del(&rng->list);
-	if (current_rng == rng)
-		enable_best_rng();
+	if (current_rng == rng) {
+		err = enable_best_rng();
+		if (err) {
+			drop_current_rng();
+			cur_rng_set_by_user = 0;
+		}
+	}
 
 	if (list_empty(&rng_list)) {
 		mutex_unlock(&rng_mutex);
Signature · 1 line
Michael
#1 lore raw
Quoted 44 lines · Michael Büsch

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 142a27f0a731ddcf467546960a5585970ca98e21

---

See this discussion for a crash in b43's hwrng caused by this problem:
https://www.spinics.net/lists/linux-wireless/msg173089.html

drivers/char/hw_random/core.c +2 -2
index 91bb98c42a1c..aaf9e5afaad4 100644
@@ -516,11 +516,18 @@ EXPORT_SYMBOL_GPL(hwrng_register);
 
 void hwrng_unregister(struct hwrng *rng)
 {
+	int err;
+
 	mutex_lock(&rng_mutex);
 
 	list_del(&rng->list);
-	if (current_rng == rng)
-		enable_best_rng();

drivers/char/hw_random/core.c EXPORT_SYMBOL_GPL(hwrng_register); · line 533

Quoted 10 lines
drivers/char/hw_random/core.c +7 -0
+	if (current_rng == rng) {
+		err = enable_best_rng();
+		if (err) {
+			drop_current_rng();
+			cur_rng_set_by_user = 0;
+		}
+	}
 
 	if (list_empty(&rng_list)) {
 		mutex_unlock(&rng_mutex);

Yes, if the hw_init() of the newly chosen rng fails, enable_best_rng() comes back with an error and did not drop the current rng. The patch handles this case. I did not test it, but the code looks fine.

reviewed-by Harald Freudenberger <[email protected]>

#2 lore raw
In reply to Michael Büsch
Quoted 13 lines · Michael Büsch

[-- text/plain, encoding quoted-printable, charset: UTF-8, 57 lines --]

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.

Quoted 7 lines

This problem was introduced by 142a27f0a731ddcf467546960a5585970ca98e21

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt