[PATCH] bluetooth: ensure RNG is properly seeded before powerup

From: Jason A. Donenfeld
Date: Thu Jun 08 2017 - 21:17:08 EST


The SMP protocol uses lots of complex cryptography that relies on securely
generated random numbers. Thus, it's important that the RNG is actually
seeded before use. Ted and the bluetooth maintainers seem to think that
it's sufficient to wait_for_random_bytes before powering up, and this
will ensure that all subsequent get_random_bytes calls occur after this
single wait_for_random_bytes. (I'm not completely convinced, but time
will tell.) Therefore, we put this call to wait_for_random_bytes, which
is running in a workqueue, so it can sleep while waiting. We also are
required to pass the potential error code back through the failure
setter, like the rest of the function does.

Signed-off-by: Jason A. Donenfeld <Jason@xxxxxxxxx>
Cc: Marcel Holtmann <marcel@xxxxxxxxxxxx>
Cc: Gustavo Padovan <gustavo@xxxxxxxxxxx>
Cc: Johan Hedberg <johan.hedberg@xxxxxxxxx>
---
Ted -- this would be instead of my other bluetooth patch. I think my other
one might be a bit more robust, but if you prefer this strategy instead,
here's the code for it.

net/bluetooth/hci_core.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 05686776a5fb..d940e22f365d 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -2017,6 +2017,18 @@ static void hci_power_on(struct work_struct *work)

BT_DBG("%s", hdev->name);

+ /* Bluetooth is a big user of cryptography and thus needs to have a
+ * good random number generator, especially for the SMP protocol.
+ * Thus, we ensure we have good randomness before powering up.
+ */
+ err = wait_for_random_bytes();
+ if (err < 0) {
+ hci_dev_lock(hdev);
+ mgmt_set_powered_failed(hdev, err);
+ hci_dev_unlock(hdev);
+ return;
+ }
+
if (test_bit(HCI_UP, &hdev->flags) &&
hci_dev_test_flag(hdev, HCI_MGMT) &&
hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
--
2.13.1