hwrng: core - Allow runtime disabling of the HW RNG
Subsystems (3)
PATCH 1/1 hwrng: core - Allow runtime disabling of the HW RNG
From: Jonathan McDowell <[email protected]>
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)
--- drivers/char/hw_random/core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/char/hw_random/core.c +5 -1
@@ -341,6 +341,10 @@ static ssize_t rng_current_store(struct device *dev,if (sysfs_streq (buf,"" )) { err =enable_best_rng (); + }else if (sysfs_streq (buf,"none" )) { +if (current_rng) + 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 +396,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);
Signature · 1 line
2.51.0
Herbert Xu
maintainerQuoted 53 lines · Jonathan McDowell
From: Jonathan McDowell <[email protected]>
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:0After:
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:0root@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 <[email protected]>--- drivers/char/hw_random/core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/char/hw_random/core.c +0
-0
drivers/char/hw_random/core.c static ssize_t rng_current_store(struct device *dev, · line 347
Quoted 7 lines
drivers/char/hw_random/core.c +4
-0@@ -341,6 +341,10 @@ static ssize_t rng_current_store(struct device *dev,if (sysfs_streq (buf,"" )) { err =enable_best_rng (); + }else if (sysfs_streq (buf,"none" )) { +if (current_rng) + cur_rng_set_by_user =1 ; +drop_current_rng ();
Is this setting supposed to be sticky?
Because as it stands, if another HWRNG is registered after setting it to none, it would still become the default RNG which seems to be surprising.
Cheers,
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
Quoted 64 lines · Herbert Xu
Quoted 53 lines · Jonathan McDowell
From: Jonathan McDowell <[email protected]>
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:0After:
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:0root@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 <[email protected]>--- drivers/char/hw_random/core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/char/hw_random/core.c +0
-0drivers/char/hw_random/core.c static ssize_t rng_current_store(struct device *dev, · line 347
Quoted 7 lines
drivers/char/hw_random/core.c +4
-0@@ -341,6 +341,10 @@ static ssize_t rng_current_store(struct device *dev,if (sysfs_streq (buf,"" )) { err =enable_best_rng (); + }else if (sysfs_streq (buf,"none" )) { +if (current_rng) + cur_rng_set_by_user =1 ; +drop_current_rng ();Is this setting supposed to be sticky?
Quoted 3 lines
Because as it stands, if another HWRNG is registered after setting it to none, it would still become the default RNG which seems to be surprising.
Good point, I'd missed that (it's not likely to be an issue in my situation), but I'll spin a v2 that fixes up hwrng_register() too.
J.
Signature · 1 line
I'm an atheist myself - there is no Greg Hill.
PATCH v2 1/1 hwrng: core - Allow runtime disabling of the HW RNG
In reply to Jonathan McDowellFrom: Jonathan McDowell <[email protected]>
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)
---
v2: If the user manually forces the HWRNG to none do not override this
when a new driver is loaded. Pointed out by Herbert Xu.
drivers/char/hw_random/core.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/char/hw_random/core.c +7 -3
@@ -341,6 +341,10 @@ static ssize_t rng_current_store(struct device *dev,if (sysfs_streq (buf,"" )) { err =enable_best_rng (); + }else if (sysfs_streq (buf,"none" )) { +if (current_rng) + 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 +396,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 +548,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
Signature · 1 line
2.51.0
Herbert Xu
maintainerdrivers/char/hw_random/core.c static ssize_t rng_current_store(struct device *dev, · line 345
Quoted 11 lines · Jonathan McDowell
drivers/char/hw_random/core.c +3
-0@@ -341,6 +341,10 @@ static ssize_t rng_current_store(struct device *dev,if (sysfs_streq (buf,"" )) { err =enable_best_rng (); + }else if (sysfs_streq (buf,"none" )) { +if (current_rng) + cur_rng_set_by_user =1 ;
Shouldn't this be set unconditionally?
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
Quoted 14 lines · Herbert Xu
drivers/char/hw_random/core.c static ssize_t rng_current_store(struct device *dev, · line 345
Quoted 11 lines · Jonathan McDowell
drivers/char/hw_random/core.c +3
-0@@ -341,6 +341,10 @@ static ssize_t rng_current_store(struct device *dev,if (sysfs_streq (buf,"" )) { err =enable_best_rng (); + }else if (sysfs_streq (buf,"none" )) { +if (current_rng) + cur_rng_set_by_user =1 ;Shouldn't this be set unconditionally?
Yes, I guess so. I'd been guided by enable_best_rng() which only clears it if it changes the current RNG, but re-reading I agree that's probably the wrong thing here.
J.
Signature · 1 line
101 things you can't have too much of : 16 - Time.
PATCH v3 1/1 hwrng: core - Allow runtime disabling of the HW RNG
In reply to Jonathan McDowellFrom: Jonathan McDowell <[email protected]>
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)
---
v3: Always set cur_rng_set_by_user if user forces no HW RNG.
v2: If the user manually forces the HWRNG to none do not override this
when a new driver is loaded. Pointed out by Herbert Xu.
drivers/char/hw_random/core.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/char/hw_random/core.c +6 -3
@@ -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
Signature · 1 line
2.51.0
Herbert Xu
maintainerQuoted 43 lines · Jonathan McDowell
From: Jonathan McDowell <[email protected]>
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:0After:
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:0root@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)
Quoted 7 lines
Signed-off-by Jonathan McDowell <[email protected]>---
v3: Always setcur_rng_set_by_userif user forces no HW RNG.
v2: If the user manually forces the HWRNG to none do not override this
when a new driver is loaded. Pointed out by Herbert Xu.drivers/char/hw_random/core.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
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