a4b143
From 9bf25f9e532dad32d71e95a85c413996d4bd58ac Mon Sep 17 00:00:00 2001
a4b143
From: Lennart Poettering <lennart@poettering.net>
a4b143
Date: Tue, 1 Oct 2013 23:11:23 +0200
a4b143
Subject: [PATCH] hashmap: randomize hash functions a bit
a4b143
a4b143
---
a4b143
 configure.ac         |   2 +-
a4b143
 src/shared/hashmap.c | 104 +++++++++++++++++++++++++++++++--------------------
a4b143
 src/shared/util.c    |  19 ++++++++++
a4b143
 src/shared/util.h    |   1 +
a4b143
 4 files changed, 85 insertions(+), 41 deletions(-)
a4b143
a4b143
diff --git a/configure.ac b/configure.ac
a4b143
index c7f4f1b..2ae0411 100644
a4b143
--- a/configure.ac
a4b143
+++ b/configure.ac
a4b143
@@ -804,7 +804,7 @@ have_myhostname=no
a4b143
 AC_ARG_ENABLE(myhostname, AS_HELP_STRING([--disable-myhostname], [disable nss-myhostname support]))
a4b143
 if test "x$enable_myhostname" != "xno"; then
a4b143
         AC_HEADER_STDC
a4b143
-        AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h netdb.h netinet/in.h stdlib.h string.h sys/socket.h sys/time.h unistd.h nss.h sys/ioctl.h])
a4b143
+        AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h netdb.h netinet/in.h stdlib.h string.h sys/socket.h sys/time.h unistd.h nss.h sys/ioctl.h sys/auxv.h])
a4b143
 
a4b143
         AC_C_CONST
a4b143
         AC_TYPE_SIZE_T
a4b143
diff --git a/src/shared/hashmap.c b/src/shared/hashmap.c
a4b143
index 6330792..f06fce6 100644
a4b143
--- a/src/shared/hashmap.c
a4b143
+++ b/src/shared/hashmap.c
a4b143
@@ -24,6 +24,10 @@
a4b143
 #include <string.h>
a4b143
 #include <errno.h>
a4b143
 
a4b143
+#ifdef HAVE_SYS_AUXV_H
a4b143
+#include <sys/auxv.h>
a4b143
+#endif
a4b143
+
a4b143
 #include "util.h"
a4b143
 #include "hashmap.h"
a4b143
 #include "macro.h"
a4b143
@@ -46,6 +50,7 @@ struct Hashmap {
a4b143
         struct hashmap_entry ** buckets;
a4b143
         unsigned n_buckets, n_entries;
a4b143
 
a4b143
+        unsigned random_xor;
a4b143
         bool from_pool;
a4b143
 };
a4b143
 
a4b143
@@ -171,10 +176,15 @@ int uint64_compare_func(const void *_a, const void *_b) {
a4b143
         return a < b ? -1 : (a > b ? 1 : 0);
a4b143
 }
a4b143
 
a4b143
+static unsigned bucket_hash(Hashmap *h, const void *p) {
a4b143
+        return (h->hash_func(p) ^ h->random_xor) % h->n_buckets;
a4b143
+}
a4b143
+
a4b143
 Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) {
a4b143
         bool b;
a4b143
         Hashmap *h;
a4b143
         size_t size;
a4b143
+        void *auxv;
a4b143
 
a4b143
         b = is_main_thread();
a4b143
 
a4b143
@@ -204,6 +214,19 @@ Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) {
a4b143
 
a4b143
         h->from_pool = b;
a4b143
 
a4b143
+        /* Let's randomize our hash functions a bit so that they are
a4b143
+         * harder to guess for clients. For this, start out by cheaply
a4b143
+         * using some bits the kernel passed into the process using
a4b143
+         * the auxiliary vector. If the hashmap grows later on we will
a4b143
+         * rehash everything using a new random XOR mask from
a4b143
+         * /dev/random. */
a4b143
+#ifdef HAVE_SYS_AUXV_H
a4b143
+        auxv = (void*) getauxval(AT_RANDOM);
a4b143
+        h->random_xor = auxv ? *(unsigned*) auxv : random_u();
a4b143
+#else
a4b143
+        h->random_xor = random_u();
a4b143
+#endif
a4b143
+
a4b143
         return h;
a4b143
 }
a4b143
 
a4b143
@@ -284,8 +307,7 @@ static void remove_entry(Hashmap *h, struct hashmap_entry *e) {
a4b143
         assert(h);
a4b143
         assert(e);
a4b143
 
a4b143
-        hash = h->hash_func(e->key) % h->n_buckets;
a4b143
-
a4b143
+        hash = bucket_hash(h, e->key);
a4b143
         unlink_entry(h, e, hash);
a4b143
 
a4b143
         if (h->from_pool)
a4b143
@@ -368,7 +390,6 @@ void hashmap_clear_free_free(Hashmap *h) {
a4b143
         }
a4b143
 }
a4b143
 
a4b143
-
a4b143
 static struct hashmap_entry *hash_scan(Hashmap *h, unsigned hash, const void *key) {
a4b143
         struct hashmap_entry *e;
a4b143
         assert(h);
a4b143
@@ -382,8 +403,8 @@ static struct hashmap_entry *hash_scan(Hashmap *h, unsigned hash, const void *ke
a4b143
 }
a4b143
 
a4b143
 static bool resize_buckets(Hashmap *h) {
a4b143
-        unsigned m;
a4b143
         struct hashmap_entry **n, *i;
a4b143
+        unsigned m, nxor;
a4b143
 
a4b143
         assert(h);
a4b143
 
a4b143
@@ -398,6 +419,11 @@ static bool resize_buckets(Hashmap *h) {
a4b143
         if (!n)
a4b143
                 return false;
a4b143
 
a4b143
+        /* Let's use a different randomized xor value for the
a4b143
+         * extension, so that people cannot guess what we are using
a4b143
+         * here forever */
a4b143
+        nxor = random_u();
a4b143
+
a4b143
         for (i = h->iterate_list_head; i; i = i->iterate_next) {
a4b143
                 unsigned hash, x;
a4b143
 
a4b143
@@ -410,10 +436,10 @@ static bool resize_buckets(Hashmap *h) {
a4b143
                 if (i->bucket_previous)
a4b143
                         i->bucket_previous->bucket_next = i->bucket_next;
a4b143
                 else
a4b143
-                        h->buckets[hash % h->n_buckets] = i->bucket_next;
a4b143
+                        h->buckets[(hash ^ h->random_xor) % h->n_buckets] = i->bucket_next;
a4b143
 
a4b143
                 /* Then, add to new backet table */
a4b143
-                x = hash % m;
a4b143
+                x = (hash ^ nxor) % m;
a4b143
 
a4b143
                 i->bucket_next = n[x];
a4b143
                 i->bucket_previous = NULL;
a4b143
@@ -427,6 +453,7 @@ static bool resize_buckets(Hashmap *h) {
a4b143
 
a4b143
         h->buckets = n;
a4b143
         h->n_buckets = m;
a4b143
+        h->random_xor = nxor;
a4b143
 
a4b143
         return true;
a4b143
 }
a4b143
@@ -437,7 +464,7 @@ int hashmap_put(Hashmap *h, const void *key, void *value) {
a4b143
 
a4b143
         assert(h);
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
+        hash = bucket_hash(h, key);
a4b143
         e = hash_scan(h, hash, key);
a4b143
         if (e) {
a4b143
                 if (e->value == value)
a4b143
@@ -446,7 +473,7 @@ int hashmap_put(Hashmap *h, const void *key, void *value) {
a4b143
         }
a4b143
 
a4b143
         if (resize_buckets(h))
a4b143
-                hash = h->hash_func(key) % h->n_buckets;
a4b143
+                hash = bucket_hash(h, key);
a4b143
 
a4b143
         if (h->from_pool)
a4b143
                 e = allocate_tile(&first_entry_pool, &first_entry_tile, sizeof(struct hashmap_entry));
a4b143
@@ -470,7 +497,7 @@ int hashmap_replace(Hashmap *h, const void *key, void *value) {
a4b143
 
a4b143
         assert(h);
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
+        hash = bucket_hash(h, key);
a4b143
         e = hash_scan(h, hash, key);
a4b143
         if (e) {
a4b143
                 e->key = key;
a4b143
@@ -487,7 +514,7 @@ int hashmap_update(Hashmap *h, const void *key, void *value) {
a4b143
 
a4b143
         assert(h);
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
+        hash = bucket_hash(h, key);
a4b143
         e = hash_scan(h, hash, key);
a4b143
         if (!e)
a4b143
                 return -ENOENT;
a4b143
@@ -503,7 +530,7 @@ void* hashmap_get(Hashmap *h, const void *key) {
a4b143
         if (!h)
a4b143
                 return NULL;
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
+        hash = bucket_hash(h, key);
a4b143
         e = hash_scan(h, hash, key);
a4b143
         if (!e)
a4b143
                 return NULL;
a4b143
@@ -518,7 +545,7 @@ void* hashmap_get2(Hashmap *h, const void *key, void **key2) {
a4b143
         if (!h)
a4b143
                 return NULL;
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
+        hash = bucket_hash(h, key);
a4b143
         e = hash_scan(h, hash, key);
a4b143
         if (!e)
a4b143
                 return NULL;
a4b143
@@ -535,12 +562,8 @@ bool hashmap_contains(Hashmap *h, const void *key) {
a4b143
         if (!h)
a4b143
                 return false;
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
-
a4b143
-        if (!hash_scan(h, hash, key))
a4b143
-                return false;
a4b143
-
a4b143
-        return true;
a4b143
+        hash = bucket_hash(h, key);
a4b143
+        return !!hash_scan(h, hash, key);
a4b143
 }
a4b143
 
a4b143
 void* hashmap_remove(Hashmap *h, const void *key) {
a4b143
@@ -551,9 +574,9 @@ void* hashmap_remove(Hashmap *h, const void *key) {
a4b143
         if (!h)
a4b143
                 return NULL;
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
-
a4b143
-        if (!(e = hash_scan(h, hash, key)))
a4b143
+        hash = bucket_hash(h, key);
a4b143
+        e = hash_scan(h, hash, key);
a4b143
+        if (!e)
a4b143
                 return NULL;
a4b143
 
a4b143
         data = e->value;
a4b143
@@ -569,11 +592,12 @@ int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key,
a4b143
         if (!h)
a4b143
                 return -ENOENT;
a4b143
 
a4b143
-        old_hash = h->hash_func(old_key) % h->n_buckets;
a4b143
-        if (!(e = hash_scan(h, old_hash, old_key)))
a4b143
+        old_hash = bucket_hash(h, old_key);
a4b143
+        e = hash_scan(h, old_hash, old_key);
a4b143
+        if (!e)
a4b143
                 return -ENOENT;
a4b143
 
a4b143
-        new_hash = h->hash_func(new_key) % h->n_buckets;
a4b143
+        new_hash = bucket_hash(h, new_key);
a4b143
         if (hash_scan(h, new_hash, new_key))
a4b143
                 return -EEXIST;
a4b143
 
a4b143
@@ -594,12 +618,14 @@ int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_
a4b143
         if (!h)
a4b143
                 return -ENOENT;
a4b143
 
a4b143
-        old_hash = h->hash_func(old_key) % h->n_buckets;
a4b143
-        if (!(e = hash_scan(h, old_hash, old_key)))
a4b143
+        old_hash = bucket_hash(h, old_key);
a4b143
+        e = hash_scan(h, old_hash, old_key);
a4b143
+        if (!e)
a4b143
                 return -ENOENT;
a4b143
 
a4b143
-        new_hash = h->hash_func(new_key) % h->n_buckets;
a4b143
-        if ((k = hash_scan(h, new_hash, new_key)))
a4b143
+        new_hash = bucket_hash(h, new_key);
a4b143
+        k = hash_scan(h, new_hash, new_key);
a4b143
+        if (k)
a4b143
                 if (e != k)
a4b143
                         remove_entry(h, k);
a4b143
 
a4b143
@@ -620,7 +646,7 @@ void* hashmap_remove_value(Hashmap *h, const void *key, void *value) {
a4b143
         if (!h)
a4b143
                 return NULL;
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
+        hash = bucket_hash(h, key);
a4b143
 
a4b143
         e = hash_scan(h, hash, key);
a4b143
         if (!e)
a4b143
@@ -711,7 +737,7 @@ void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i) {
a4b143
         if (!h)
a4b143
                 return NULL;
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
+        hash = bucket_hash(h, key);
a4b143
 
a4b143
         e = hash_scan(h, hash, key);
a4b143
         if (!e)
a4b143
@@ -820,9 +846,9 @@ int hashmap_merge(Hashmap *h, Hashmap *other) {
a4b143
         for (e = other->iterate_list_head; e; e = e->iterate_next) {
a4b143
                 int r;
a4b143
 
a4b143
-                if ((r = hashmap_put(h, e->key, e->value)) < 0)
a4b143
-                        if (r != -EEXIST)
a4b143
-                                return r;
a4b143
+                r = hashmap_put(h, e->key, e->value);
a4b143
+                if (r < 0 && r != -EEXIST)
a4b143
+                        return r;
a4b143
         }
a4b143
 
a4b143
         return 0;
a4b143
@@ -844,13 +870,11 @@ void hashmap_move(Hashmap *h, Hashmap *other) {
a4b143
 
a4b143
                 n = e->iterate_next;
a4b143
 
a4b143
-                h_hash = h->hash_func(e->key) % h->n_buckets;
a4b143
-
a4b143
+                h_hash = bucket_hash(h, e->key);
a4b143
                 if (hash_scan(h, h_hash, e->key))
a4b143
                         continue;
a4b143
 
a4b143
-                other_hash = other->hash_func(e->key) % other->n_buckets;
a4b143
-
a4b143
+                other_hash = bucket_hash(other, e->key);
a4b143
                 unlink_entry(other, e, other_hash);
a4b143
                 link_entry(h, e, h_hash);
a4b143
         }
a4b143
@@ -865,11 +889,11 @@ int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key) {
a4b143
 
a4b143
         assert(h);
a4b143
 
a4b143
-        h_hash = h->hash_func(key) % h->n_buckets;
a4b143
+        h_hash = bucket_hash(h, key);
a4b143
         if (hash_scan(h, h_hash, key))
a4b143
                 return -EEXIST;
a4b143
 
a4b143
-        other_hash = other->hash_func(key) % other->n_buckets;
a4b143
+        other_hash = bucket_hash(other, key);
a4b143
         e = hash_scan(other, other_hash, key);
a4b143
         if (!e)
a4b143
                 return -ENOENT;
a4b143
@@ -925,7 +949,7 @@ void *hashmap_next(Hashmap *h, const void *key) {
a4b143
         if (!h)
a4b143
                 return NULL;
a4b143
 
a4b143
-        hash = h->hash_func(key) % h->n_buckets;
a4b143
+        hash = bucket_hash(h, key);
a4b143
         e = hash_scan(h, hash, key);
a4b143
         if (!e)
a4b143
                 return NULL;
a4b143
diff --git a/src/shared/util.c b/src/shared/util.c
a4b143
index e69d1ff..95fe35e 100644
a4b143
--- a/src/shared/util.c
a4b143
+++ b/src/shared/util.c
a4b143
@@ -2420,6 +2420,25 @@ fallback:
a4b143
         return random() * RAND_MAX + random();
a4b143
 }
a4b143
 
a4b143
+unsigned random_u(void) {
a4b143
+        _cleanup_close_ int fd;
a4b143
+        unsigned u;
a4b143
+        ssize_t r;
a4b143
+
a4b143
+        fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
a4b143
+        if (fd < 0)
a4b143
+                goto fallback;
a4b143
+
a4b143
+        r = loop_read(fd, &u, sizeof(u), true);
a4b143
+        if (r != sizeof(u))
a4b143
+                goto fallback;
a4b143
+
a4b143
+        return u;
a4b143
+
a4b143
+fallback:
a4b143
+        return random() * RAND_MAX + random();
a4b143
+}
a4b143
+
a4b143
 void rename_process(const char name[8]) {
a4b143
         assert(name);
a4b143
 
a4b143
diff --git a/src/shared/util.h b/src/shared/util.h
a4b143
index 63f4e3d..1b845b3 100644
a4b143
--- a/src/shared/util.h
a4b143
+++ b/src/shared/util.h
a4b143
@@ -253,6 +253,7 @@ int make_null_stdio(void);
a4b143
 int make_console_stdio(void);
a4b143
 
a4b143
 unsigned long long random_ull(void);
a4b143
+unsigned random_u(void);
a4b143
 
a4b143
 /* For basic lookup tables with strictly enumerated entries */
a4b143
 #define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope)                   \