The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxc/pull/2486

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Hello,

fix thread safe issue : rand() => rand_r()

Thanks.

Signed-off-by: 2xsec <dh48.je...@samsung.com>
From 280cc35f082c581dabb86e8a83d93cf35f6dc79f Mon Sep 17 00:00:00 2001
From: 2xsec <dh48.je...@samsung.com>
Date: Sat, 21 Jul 2018 22:27:30 +0900
Subject: [PATCH] thread safe: rand() => rand_r()

Signed-off-by: 2xsec <dh48.je...@samsung.com>
---
 src/lxc/confile_utils.c | 20 +++++++++++++++-----
 src/lxc/network.c       | 25 +++++++++----------------
 2 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/src/lxc/confile_utils.c b/src/lxc/confile_utils.c
index 4a7d934a2..2bb46d17b 100644
--- a/src/lxc/confile_utils.c
+++ b/src/lxc/confile_utils.c
@@ -550,14 +550,15 @@ void rand_complete_hwaddr(char *hwaddr)
 {
        const char hex[] = "0123456789abcdef";
        char *curs = hwaddr;
-
-#ifndef HAVE_RAND_R
-       randseed(true);
-#else
+#ifdef HAVE_RAND_R
        unsigned int seed;
 
        seed = randseed(false);
+#else
+
+       (void)randseed(true);
 #endif
+
        while (*curs != '\0' && *curs != '\n') {
                if (*curs == 'x' || *curs == 'X') {
                        if (curs - hwaddr == 1) {
@@ -635,13 +636,22 @@ void update_hwaddr(const char *line)
 bool new_hwaddr(char *hwaddr)
 {
        int ret;
+#ifdef HAVE_RAND_R
+       unsigned int seed;
+
+       seed = randseed(false);
+
+       ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand_r(&seed) % 
255,
+                      rand_r(&seed) % 255, rand_r(&seed) % 255);
+#else
 
        (void)randseed(true);
 
        ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
                       rand() % 255, rand() % 255);
+#endif
        if (ret < 0 || ret >= 18) {
-               SYSERROR("Failed to call snprintf().");
+               SYSERROR("Failed to call snprintf()");
                return false;
        }
 
diff --git a/src/lxc/network.c b/src/lxc/network.c
index 63f321f46..50b5293c0 100644
--- a/src/lxc/network.c
+++ b/src/lxc/network.c
@@ -1965,12 +1965,18 @@ static const char padchar[] = 
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 char *lxc_mkifname(char *template)
 {
        int ret;
-       unsigned int seed;
-       FILE *urandom;
        struct ifaddrs *ifa, *ifaddr;
        char name[IFNAMSIZ];
        bool exists = false;
        size_t i = 0;
+#ifdef HAVE_RAND_R
+       unsigned int seed;
+
+       seed = randseed(false);
+#else
+
+       (void)randseed(true);
+#endif
 
        if (strlen(template) >= IFNAMSIZ)
                return NULL;
@@ -1982,26 +1988,13 @@ char *lxc_mkifname(char *template)
                return NULL;
        }
 
-       /* Initialize the random number generator. */
-       urandom = fopen("/dev/urandom", "r");
-       if (urandom != NULL) {
-               if (fread(&seed, sizeof(seed), 1, urandom) <= 0)
-                       seed = time(0);
-               fclose(urandom);
-       } else {
-               seed = time(0);
-       }
-
-#ifndef HAVE_RAND_R
-       srand(seed);
-#endif
-
        /* Generate random names until we find one that doesn't exist. */
        while (true) {
                name[0] = '\0';
                (void)strlcpy(name, template, IFNAMSIZ);
 
                exists = false;
+
                for (i = 0; i < strlen(name); i++) {
                        if (name[i] == 'X') {
 #ifdef HAVE_RAND_R
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to