]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
hwrng: core - Allow runtime disabling of the HW RNG
authorJonathan McDowell <noodles@meta.com>
Tue, 23 Sep 2025 13:33:05 +0000 (14:33 +0100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 17 Oct 2025 08:03:57 +0000 (16:03 +0800)
The HW RNG core allows for manual selection of which RNG device to use,
but does not allow for no device to be enabled. It may be desirable to
do this on systems with only a single suitable hardware RNG, where we
need exclusive access to other functionality on this device. In
particular when performing TPM firmware upgrades this lets us ensure the
kernel does not try to access the device.

Before:

root@debian-qemu-efi:~# grep "" /sys/devices/virtual/misc/hw_random/rng_*
/sys/devices/virtual/misc/hw_random/rng_available:tpm-rng-0
/sys/devices/virtual/misc/hw_random/rng_current:tpm-rng-0
/sys/devices/virtual/misc/hw_random/rng_quality:1024
/sys/devices/virtual/misc/hw_random/rng_selected:0

After:

root@debian-qemu-efi:~# grep "" /sys/devices/virtual/misc/hw_random/rng_*
/sys/devices/virtual/misc/hw_random/rng_available:tpm-rng-0 none
/sys/devices/virtual/misc/hw_random/rng_current:tpm-rng-0
/sys/devices/virtual/misc/hw_random/rng_quality:1024
/sys/devices/virtual/misc/hw_random/rng_selected:0

root@debian-qemu-efi:~# echo none > /sys/devices/virtual/misc/hw_random/rng_current
root@debian-qemu-efi:~# grep "" /sys/devices/virtual/misc/hw_random/rng_*
/sys/devices/virtual/misc/hw_random/rng_available:tpm-rng-0 none
/sys/devices/virtual/misc/hw_random/rng_current:none
grep: /sys/devices/virtual/misc/hw_random/rng_quality: No such device
/sys/devices/virtual/misc/hw_random/rng_selected:1

(Observe using bpftrace no calls to TPM being made)

root@debian-qemu-efi:~# echo "" > /sys/devices/virtual/misc/hw_random/rng_current
root@debian-qemu-efi:~# grep "" /sys/devices/virtual/misc/hw_random/rng_*
/sys/devices/virtual/misc/hw_random/rng_available:tpm-rng-0 none
/sys/devices/virtual/misc/hw_random/rng_current:tpm-rng-0
/sys/devices/virtual/misc/hw_random/rng_quality:1024
/sys/devices/virtual/misc/hw_random/rng_selected:0

(Observe using bpftrace that calls to the TPM resume)

Signed-off-by: Jonathan McDowell <noodles@meta.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
drivers/char/hw_random/core.c

index 018316f546215abd4e179764487fb58d1c822d5f..56d888bebe0c0889e1396ffdcb5b0749e2790673 100644 (file)
@@ -341,6 +341,9 @@ static ssize_t rng_current_store(struct device *dev,
 
        if (sysfs_streq(buf, "")) {
                err = enable_best_rng();
+       } else if (sysfs_streq(buf, "none")) {
+               cur_rng_set_by_user = 1;
+               drop_current_rng();
        } else {
                list_for_each_entry(rng, &rng_list, list) {
                        if (sysfs_streq(rng->name, buf)) {
@@ -392,7 +395,7 @@ static ssize_t rng_available_show(struct device *dev,
                strlcat(buf, rng->name, PAGE_SIZE);
                strlcat(buf, " ", PAGE_SIZE);
        }
-       strlcat(buf, "\n", PAGE_SIZE);
+       strlcat(buf, "none\n", PAGE_SIZE);
        mutex_unlock(&rng_mutex);
 
        return strlen(buf);
@@ -544,8 +547,8 @@ int hwrng_register(struct hwrng *rng)
        /* Adjust quality field to always have a proper value */
        rng->quality = min_t(u16, min_t(u16, default_quality, 1024), rng->quality ?: 1024);
 
-       if (!current_rng ||
-           (!cur_rng_set_by_user && rng->quality > current_rng->quality)) {
+       if (!cur_rng_set_by_user &&
+           (!current_rng || rng->quality > current_rng->quality)) {
                /*
                 * Set new rng as current as the new rng source
                 * provides better entropy quality and was not