Use /dev/urandom as the proper source for random numbers. Verify the
existence of /dev/urandom at compile time and program startup.
---
 Makefile.am   |  2 +-
 src/connman.h |  4 +++
 src/main.c    |  2 ++
 src/util.c    | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 95 insertions(+), 1 deletion(-)
 create mode 100644 src/util.c

diff --git a/Makefile.am b/Makefile.am
index a574170..09a0d6b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -106,7 +106,7 @@ src_connmand_SOURCES = $(gdhcp_sources) $(gweb_sources) \
                        src/stats.c src/iptables.c src/dnsproxy.c src/6to4.c \
                        src/ippool.c src/bridge.c src/nat.c src/ipaddress.c \
                        src/inotify.c src/firewall.c src/ipv6pd.c src/peer.c \
-                       src/peer_service.c src/machine.c
+                       src/peer_service.c src/machine.c src/util.c
 
 src_connmand_LDADD = gdbus/libgdbus-internal.la $(builtin_libadd) \
                        @GLIB_LIBS@ @DBUS_LIBS@ @XTABLES_LIBS@ @GNUTLS_LIBS@ \
diff --git a/src/connman.h b/src/connman.h
index da01215..2524f07 100644
--- a/src/connman.h
+++ b/src/connman.h
@@ -1034,3 +1034,7 @@ void __connman_nfacct_cleanup(void);
 
 int __connman_machine_init(void);
 void __connman_machine_cleanup(void);
+
+int __connman_util_get_random(uint64_t *val);
+int __connman_util_init(void);
+void __connman_util_cleanup(void);
diff --git a/src/main.c b/src/main.c
index 21d1e06..ba09eb6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -639,6 +639,7 @@ int main(int argc, char *argv[])
        else
                config_init(option_config);
 
+       __connman_util_init();
        __connman_inotify_init();
        __connman_technology_init();
        __connman_notifier_init();
@@ -729,6 +730,7 @@ int main(int argc, char *argv[])
        __connman_technology_cleanup();
        __connman_inotify_cleanup();
 
+       __connman_util_cleanup();
        __connman_dbus_cleanup();
 
        __connman_log_cleanup(option_backtrace);
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..2d3a7f7
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,88 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2014  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include "connman.h"
+
+#define URANDOM "/dev/urandom"
+
+int f = -1;
+
+int __connman_util_get_random(uint64_t *val)
+{
+       int r = 0;
+
+       if (!val)
+               return -EINVAL;
+
+       if (read(f, val, sizeof(*val)) < 0) {
+               r = -errno;
+               connman_warn_once("Could not read from "URANDOM);
+               *val = random();
+       }
+
+       return r;
+}
+
+int __connman_util_init(void)
+{
+       int r = 0;
+
+       if (f > 0)
+               return 0;
+
+       f = open(URANDOM, O_RDONLY);
+       if (f < 0) {
+               r = -errno;
+               connman_warn("Could not open "URANDOM);
+               srandom(time(NULL));
+       } else {
+               uint64_t val;
+
+               r = __connman_util_get_random(&val);
+               if (r < 0)
+                       srandom(time(NULL));
+               else
+                       srandom(val);
+       }
+
+       return r;
+}
+
+void __connman_util_cleanup(void)
+{
+       if (f > 0)
+               close(f);
+
+       f = -1;
+}
-- 
2.1.1

_______________________________________________
connman mailing list
[email protected]
https://lists.connman.net/mailman/listinfo/connman

Reply via email to