]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
util: randomize hashing of IP addresses
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 14 Mar 2016 17:06:56 +0000 (18:06 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 15 Mar 2016 13:29:42 +0000 (14:29 +0100)
Include a random (constant) value in the hash in UTI_IPToHash() to
randomize the order in which NTP sources are stored in the hash table
and polled on start. This change also randomizes the order of clientlog
records.

util.c

diff --git a/util.c b/util.c
index 6967a9ea0001e81323c1f5b52d0fc6c67e15d72a..b654e632ddafd851988931ea6aef6eccc4738bf8 100644 (file)
--- a/util.c
+++ b/util.c
@@ -362,6 +362,7 @@ UTI_IPToRefid(IPAddr *ip)
 uint32_t
 UTI_IPToHash(IPAddr *ip)
 {
+  static uint32_t seed = 0;
   unsigned char *addr;
   unsigned int i, len;
   uint32_t hash;
@@ -379,10 +380,15 @@ UTI_IPToHash(IPAddr *ip)
       return 0;
   }
 
-  for (i = 0, hash = 0; i < len; i++)
+  /* Include a random seed in the hash to randomize collisions
+     and order of addresses in hash tables */
+  while (!seed)
+    UTI_GetRandomBytes(&seed, sizeof (seed));
+
+  for (i = 0, hash = seed; i < len; i++)
     hash = 71 * hash + addr[i];
 
-  return hash;
+  return hash + seed;
 }
 
 /* ================================================== */