diff options
author | Thomas Haller <thaller@redhat.com> | 2017-10-13 11:56:06 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-10-13 12:47:55 +0200 |
commit | c978b9dfe57e68d92c6dd3afac565fe05d325be0 (patch) | |
tree | 01c28258fd835aeda042088c46450f2125bea9d1 | |
parent | 4a2798434ef162b31a129cb6a857c950ec992f3e (diff) | |
download | NetworkManager-c978b9dfe57e68d92c6dd3afac565fe05d325be0.tar.gz |
core: randomize hash seed with a global seed
This makes hashing non-deterministic with the aim to
make it harder to exploit hash collisions.
Non-deterministic also means that for unit testing
we will get different values on each run. But since we
shall never assign any meaning to these hash values
nor rely on them being stable between restarts (or
upgrades), that doesn't hurt.
-rw-r--r-- | shared/nm-utils/nm-shared-utils.c | 23 | ||||
-rw-r--r-- | shared/nm-utils/nm-shared-utils.h | 6 |
2 files changed, 24 insertions, 5 deletions
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c index d2c057c48c..ba99ce2f87 100644 --- a/shared/nm-utils/nm-shared-utils.c +++ b/shared/nm-utils/nm-shared-utils.c @@ -863,6 +863,29 @@ nm_g_object_class_find_property_from_gtype (GType gtype, /*****************************************************************************/ +guint +NM_HASH_INIT (guint seed) +{ + static volatile guint global_seed = 0; + guint g, s; + + /* we xor @seed with a random @global_seed. This is to make the hashing behavior + * less predictable and harder to exploit collisions. */ + g = global_seed; + if (G_UNLIKELY (g == 0)) { + nm_utils_random_bytes (&s, sizeof (s)); + if (s == 0) + s = 42; + g_atomic_int_compare_and_exchange ((int *) &global_seed, 0, s); + g = global_seed; + nm_assert (g); + } + + return g ^ seed; +} + +/*****************************************************************************/ + static void _str_append_escape (GString *s, char ch) { diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index 57f1619245..0f9df73fb3 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -378,11 +378,7 @@ GParamSpec *nm_g_object_class_find_property_from_gtype (GType gtype, /*****************************************************************************/ -static inline guint -NM_HASH_INIT (guint seed) -{ - return seed; -} +guint NM_HASH_INIT (guint seed); static inline guint NM_HASH_COMBINE (guint h, guint val) |