profiles/health: Replace random number generation function
authorTedd Ho-Jeong An <tedd.an@intel.com>
Wed, 8 Dec 2021 22:39:23 +0000 (14:39 -0800)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 11 Mar 2022 13:38:38 +0000 (19:08 +0530)
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

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
profiles/health/hdp.c
profiles/health/mcap.c

index 3dcdff8..52aecca 100755 (executable)
@@ -15,6 +15,7 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <unistd.h>
+#include <sys/random.h>
 
 #include <glib.h>
 
@@ -1483,13 +1484,15 @@ static void destroy_create_dc_data(gpointer data)
 static void *generate_echo_packet(void)
 {
        uint8_t *buf;
-       int i;
 
        buf = g_malloc(HDP_ECHO_LEN);
-       srand(time(NULL));
+       if (!buf)
+               return NULL;
 
-       for(i = 0; i < HDP_ECHO_LEN; i++)
-               buf[i] = rand() % UINT8_MAX;
+       if (getrandom(buf, HDP_ECHO_LEN, 0) < 0) {
+               g_free(buf);
+               return NULL;
+       }
 
        return buf;
 }
index 439d19f..9055ead 100755 (executable)
@@ -17,6 +17,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <unistd.h>
+#include <sys/random.h>
 
 #include <glib.h>
 
@@ -1886,6 +1887,7 @@ gboolean mcap_create_mcl(struct mcap_instance *mi,
 {
        struct mcap_mcl *mcl;
        struct connect_mcl *con;
+       uint16_t val;
 
        mcl = find_mcl(mi->mcls, addr);
        if (mcl) {
@@ -1901,7 +1903,12 @@ gboolean mcap_create_mcl(struct mcap_instance *mi,
                mcl->state = MCL_IDLE;
                bacpy(&mcl->addr, addr);
                set_default_cb(mcl);
-               mcl->next_mdl = (rand() % MCAP_MDLID_FINAL) + 1;
+               if (getrandom(&val, sizeof(val), 0) < 0) {
+                       mcap_instance_unref(mcl->mi);
+                       g_free(mcl);
+                       return FALSE;
+               }
+               mcl->next_mdl = (val % MCAP_MDLID_FINAL) + 1;
        }
 
        mcl->ctrl |= MCAP_CTRL_CONN;
@@ -2011,6 +2018,7 @@ static void connect_mcl_event_cb(GIOChannel *chan, GError *gerr,
        bdaddr_t dst;
        char address[18], srcstr[18];
        GError *err = NULL;
+       uint16_t val;
 
        if (gerr)
                return;
@@ -2039,7 +2047,12 @@ static void connect_mcl_event_cb(GIOChannel *chan, GError *gerr,
                mcl->mi = mcap_instance_ref(mi);
                bacpy(&mcl->addr, &dst);
                set_default_cb(mcl);
-               mcl->next_mdl = (rand() % MCAP_MDLID_FINAL) + 1;
+               if (getrandom(&val, sizeof(val), 0) < 0) {
+                       mcap_instance_unref(mcl->mi);
+                       g_free(mcl);
+                       goto drop;
+               }
+               mcl->next_mdl = (val % MCAP_MDLID_FINAL) + 1;
        }
 
        set_mcl_conf(chan, mcl);