[nginx] Always seed PRNG with PID, seconds, and milliseconds.

Ruslan Ermilov ru at nginx.com
Mon Aug 8 08:18:30 UTC 2016


details:   http://hg.nginx.org/nginx/rev/7d4e33092e2a
branches:  
changeset: 6651:7d4e33092e2a
user:      Ruslan Ermilov <ru at nginx.com>
date:      Thu Aug 04 23:43:10 2016 +0300
description:
Always seed PRNG with PID, seconds, and milliseconds.

diffstat:

 src/os/unix/ngx_posix_init.c     |   6 ++++--
 src/os/unix/ngx_process_cycle.c  |   4 +++-
 src/os/win32/ngx_process_cycle.c |   4 +++-
 src/os/win32/ngx_win32_init.c    |  16 +++++++++-------
 4 files changed, 19 insertions(+), 11 deletions(-)

diffs (93 lines):

diff -r 1a1d55834b5c -r 7d4e33092e2a src/os/unix/ngx_posix_init.c
--- a/src/os/unix/ngx_posix_init.c	Thu Aug 04 23:42:00 2016 +0300
+++ b/src/os/unix/ngx_posix_init.c	Thu Aug 04 23:43:10 2016 +0300
@@ -33,7 +33,8 @@ ngx_os_io_t ngx_os_io = {
 ngx_int_t
 ngx_os_init(ngx_log_t *log)
 {
-    ngx_uint_t  n;
+    ngx_time_t  *tp;
+    ngx_uint_t   n;
 
 #if (NGX_HAVE_OS_SPECIFIC_INIT)
     if (ngx_os_specific_init(log) != NGX_OK) {
@@ -76,7 +77,8 @@ ngx_os_init(ngx_log_t *log)
     ngx_inherited_nonblocking = 0;
 #endif
 
-    srandom(ngx_time());
+    tp = ngx_timeofday();
+    srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);
 
     return NGX_OK;
 }
diff -r 1a1d55834b5c -r 7d4e33092e2a src/os/unix/ngx_process_cycle.c
--- a/src/os/unix/ngx_process_cycle.c	Thu Aug 04 23:42:00 2016 +0300
+++ b/src/os/unix/ngx_process_cycle.c	Thu Aug 04 23:43:10 2016 +0300
@@ -785,6 +785,7 @@ ngx_worker_process_init(ngx_cycle_t *cyc
 {
     sigset_t          set;
     ngx_int_t         n;
+    ngx_time_t       *tp;
     ngx_uint_t        i;
     ngx_cpuset_t     *cpu_affinity;
     struct rlimit     rlmt;
@@ -884,7 +885,8 @@ ngx_worker_process_init(ngx_cycle_t *cyc
                       "sigprocmask() failed");
     }
 
-    srandom(((unsigned) ngx_pid << 16) ^ ngx_time());
+    tp = ngx_timeofday();
+    srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);
 
     /*
      * disable deleting previous events for the listening sockets because
diff -r 1a1d55834b5c -r 7d4e33092e2a src/os/win32/ngx_process_cycle.c
--- a/src/os/win32/ngx_process_cycle.c	Thu Aug 04 23:42:00 2016 +0300
+++ b/src/os/win32/ngx_process_cycle.c	Thu Aug 04 23:43:10 2016 +0300
@@ -762,9 +762,11 @@ static ngx_thread_value_t __stdcall
 ngx_worker_thread(void *data)
 {
     ngx_int_t     n;
+    ngx_time_t   *tp;
     ngx_cycle_t  *cycle;
 
-    srand((ngx_pid << 16) ^ (unsigned) ngx_time());
+    tp = ngx_timeofday();
+    srand((ngx_pid << 16) ^ (unsigned) tp->sec ^ tp->msec);
 
     cycle = (ngx_cycle_t *) ngx_cycle;
 
diff -r 1a1d55834b5c -r 7d4e33092e2a src/os/win32/ngx_win32_init.c
--- a/src/os/win32/ngx_win32_init.c	Thu Aug 04 23:42:00 2016 +0300
+++ b/src/os/win32/ngx_win32_init.c	Thu Aug 04 23:43:10 2016 +0300
@@ -59,12 +59,13 @@ static GUID dx_guid = WSAID_DISCONNECTEX
 ngx_int_t
 ngx_os_init(ngx_log_t *log)
 {
-    DWORD        bytes;
-    SOCKET       s;
-    WSADATA      wsd;
-    ngx_err_t    err;
-    ngx_uint_t   n;
-    SYSTEM_INFO  si;
+    DWORD         bytes;
+    SOCKET        s;
+    WSADATA       wsd;
+    ngx_err_t     err;
+    ngx_time_t   *tp;
+    ngx_uint_t    n;
+    SYSTEM_INFO   si;
 
     /* get Windows version */
 
@@ -237,7 +238,8 @@ ngx_os_init(ngx_log_t *log)
         ngx_sprintf((u_char *) ngx_unique, "%P%Z", ngx_pid);
     }
 
-    srand((ngx_pid << 16) ^ (unsigned) ngx_time());
+    tp = ngx_timeofday();
+    srand((ngx_pid << 16) ^ (unsigned) tp->sec ^ tp->msec);
 
     return NGX_OK;
 }



More information about the nginx-devel mailing list