diff mbox series

[BlueZ,V2,1/5] emulator: Replace random number generation function

Message ID 20211208223923.519664-2-hj.tedd.an@gmail.com
State New
Headers show
Series Replace random number generation function | expand

Commit Message

Tedd Ho-Jeong An Dec. 8, 2021, 10:39 p.m. UTC
From: Tedd Ho-Jeong An <tedd.an@intel.com>

This patch replaces the rand() function to the getrandom() syscall.

It was reported by the Coverity scan
  rand() should not be used for security-related applications, because
  linear congruential algorithms are too easy to break
---
 emulator/le.c  | 11 +++++++++--
 emulator/phy.c | 10 ++++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)

Comments

bluez.test.bot@gmail.com Dec. 8, 2021, 11:11 p.m. UTC | #1
This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=592617

---Test result---

Test Summary:
CheckPatch                    PASS      6.87 seconds
GitLint                       PASS      4.68 seconds
Prep - Setup ELL              PASS      42.33 seconds
Build - Prep                  PASS      0.59 seconds
Build - Configure             PASS      8.11 seconds
Build - Make                  PASS      179.48 seconds
Make Check                    PASS      8.97 seconds
Make Distcheck                PASS      204.63 seconds
Build w/ext ELL - Configure   PASS      7.72 seconds
Build w/ext ELL - Make        PASS      169.93 seconds
Incremental Build with patchesPASS      955.83 seconds



---
Regards,
Linux Bluetooth
diff mbox series

Patch

diff --git a/emulator/le.c b/emulator/le.c
index 07a44c5f1..f8f313f2c 100644
--- a/emulator/le.c
+++ b/emulator/le.c
@@ -20,6 +20,7 @@ 
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <sys/uio.h>
+#include <sys/random.h>
 #include <time.h>
 
 #include "lib/bluetooth.h"
@@ -503,11 +504,17 @@  static void send_adv_pkt(struct bt_le *hci, uint8_t channel)
 
 static unsigned int get_adv_delay(void)
 {
+	unsigned int val;
+
 	/* The advertising delay is a pseudo-random value with a range
 	 * of 0 ms to 10 ms generated for each advertising event.
 	 */
-	srand(time(NULL));
-	return (rand() % 11);
+	if (getrandom(&val, sizeof(val), 0) < 0) {
+		/* If it fails to get the random number, use a static value */
+		val = 5;
+	}
+
+	return (val % 11);
 }
 
 static void adv_timeout_callback(int id, void *user_data)
diff --git a/emulator/phy.c b/emulator/phy.c
index 2ae6ad3a2..44cace438 100644
--- a/emulator/phy.c
+++ b/emulator/phy.c
@@ -19,6 +19,7 @@ 
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
+#include <sys/random.h>
 #include <netinet/in.h>
 #include <netinet/ip.h>
 #include <time.h>
@@ -173,8 +174,13 @@  struct bt_phy *bt_phy_new(void)
 	mainloop_add_fd(phy->rx_fd, EPOLLIN, phy_rx_callback, phy, NULL);
 
 	if (!get_random_bytes(&phy->id, sizeof(phy->id))) {
-		srandom(time(NULL));
-		phy->id = random();
+		if (getrandom(&phy->id, sizeof(phy->id), 0) < 0) {
+			mainloop_remove_fd(phy->rx_fd);
+			close(phy->tx_fd);
+			close(phy->rx_fd);
+			free(phy);
+			return NULL;
+		}
 	}
 
 	bt_phy_send(phy, BT_PHY_PKT_NULL, NULL, 0);