You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ha...@apache.org on 2020/09/10 20:43:50 UTC

[mynewt-nimble] branch master updated: controller: properly seed srand() during init

This is an automated email from the ASF dual-hosted git repository.

hauke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git


The following commit(s) were added to refs/heads/master by this push:
     new e9a4eed  controller: properly seed srand() during init
e9a4eed is described below

commit e9a4eed6843d4f06b57fe76d53acda001117d579
Author: Hauke Petersen <ha...@fu-berlin.de>
AuthorDate: Mon Aug 31 22:02:04 2020 +0200

    controller: properly seed srand() during init
    
    As the XXX comment in the code stated, the intialization of rand()
    was not done properly. In cases where further no other part of the
    system was taking care of this it lead to massive misbehavior due
    to the same sequence of pseudo-random numbers created on different
    nodes.
    This commit fixes this by using the hardware random number generator
    as seed for the STDLIBs pseudo random number generator.
---
 nimble/controller/src/ble_ll.c | 35 +++++++++--------------------------
 1 file changed, 9 insertions(+), 26 deletions(-)

diff --git a/nimble/controller/src/ble_ll.c b/nimble/controller/src/ble_ll.c
index 6fc6ad9..55cb60c 100644
--- a/nimble/controller/src/ble_ll.c
+++ b/nimble/controller/src/ble_ll.c
@@ -1203,8 +1203,6 @@ ble_ll_task(void *arg)
     /* Tell the host that we are ready to receive packets */
     ble_ll_hci_send_noop();
 
-    ble_ll_rand_start();
-
     while (1) {
         ev = ble_npl_eventq_get(&g_ble_ll_data.ll_evq, BLE_NPL_TIME_FOREVER);
         assert(ev);
@@ -1440,23 +1438,6 @@ ble_ll_reset(void)
     return rc;
 }
 
-static void
-ble_ll_seed_prng(void)
-{
-    uint32_t seed;
-    int i;
-
-    /* Seed random number generator with least significant bytes of device
-     * address.
-     */
-    seed = 0;
-    for (i = 0; i < 4; ++i) {
-        seed |= g_dev_addr[i];
-        seed <<= 8;
-    }
-    srand(seed);
-}
-
 uint32_t
 ble_ll_pdu_tx_time_get(uint16_t payload_len, int phy_mode)
 {
@@ -1554,6 +1535,7 @@ ble_ll_init(void)
     uint64_t features;
     ble_addr_t addr;
     struct ble_ll_obj *lldata;
+    unsigned seed;
 
     /* Ensure this function only gets called by sysinit. */
     SYSINIT_ASSERT_ACTIVE();
@@ -1682,15 +1664,16 @@ ble_ll_init(void)
     features |= BLE_LL_FEAT_ISO_HOST_SUPPORT;
 #endif
 
+    lldata->ll_supp_features = features;
+
     /* Initialize random number generation */
     ble_ll_rand_init();
-
-    /* XXX: This really doesn't belong here, as the address probably has not
-     * been set yet.
-     */
-    ble_ll_seed_prng();
-
-    lldata->ll_supp_features = features;
+    /* Start the random number generator */
+    ble_ll_rand_start();
+    /* Use the random number generator to seed the STDLIBs pseudo-number
+     * generator */
+    ble_ll_rand_data_get((uint8_t *)&seed, sizeof(seed));
+    srand(seed);
 
     rc = stats_init_and_reg(STATS_HDR(ble_ll_stats),
                             STATS_SIZE_INIT_PARMS(ble_ll_stats, STATS_SIZE_32),