From b826ab586c9e0a9c0d438a75c28cf3a8ab485929 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Sun, 4 Oct 2015 00:22:41 +0200 Subject: hashmap: refactor hash_func All our hash functions are based on siphash24(), factor out siphash_init() and siphash24_finalize() and pass the siphash state to the hash functions rather than the hash key. This simplifies the hash functions, and in particular makes composition simpler as calling siphash24_compress() repeatedly on separate chunks of input has the same effect as first concatenating the input and then calling siphash23_compress() on the result. --- src/libsystemd/sd-bus/bus-objects.c | 19 ++++--------------- src/libsystemd/sd-bus/busctl.c | 11 ++++------- 2 files changed, 8 insertions(+), 22 deletions(-) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c index 1d061cb9cf..728f20447a 100644 --- a/src/libsystemd/sd-bus/bus-objects.c +++ b/src/libsystemd/sd-bus/bus-objects.c @@ -1578,25 +1578,14 @@ _public_ int sd_bus_add_fallback( return bus_add_object(bus, slot, true, prefix, callback, userdata); } -static unsigned long vtable_member_hash_func(const void *a, const uint8_t hash_key[HASH_KEY_SIZE]) { +static void vtable_member_hash_func(const void *a, struct siphash *state) { const struct vtable_member *m = a; - uint8_t hash_key2[HASH_KEY_SIZE]; - unsigned long ret; assert(m); - ret = string_hash_func(m->path, hash_key); - - /* Use a slightly different hash key for the interface */ - memcpy(hash_key2, hash_key, HASH_KEY_SIZE); - hash_key2[0]++; - ret ^= string_hash_func(m->interface, hash_key2); - - /* And an even different one for the member */ - hash_key2[0]++; - ret ^= string_hash_func(m->member, hash_key2); - - return ret; + string_hash_func(m->path, state); + string_hash_func(m->interface, state); + string_hash_func(m->member, state); } static int vtable_member_compare_func(const void *a, const void *b) { diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c index ab8816942c..496b9a7b4e 100644 --- a/src/libsystemd/sd-bus/busctl.c +++ b/src/libsystemd/sd-bus/busctl.c @@ -628,22 +628,19 @@ typedef struct Member { uint64_t flags; } Member; -static unsigned long member_hash_func(const void *p, const uint8_t hash_key[]) { +static void member_hash_func(const void *p, struct siphash *state) { const Member *m = p; - unsigned long ul; assert(m); assert(m->type); - ul = string_hash_func(m->type, hash_key); + string_hash_func(m->type, state); if (m->name) - ul ^= string_hash_func(m->name, hash_key); + string_hash_func(m->name, state); if (m->interface) - ul ^= string_hash_func(m->interface, hash_key); - - return ul; + string_hash_func(m->interface, state); } static int member_compare_func(const void *a, const void *b) { -- cgit v1.2.3-54-g00ecf From 1e2527a6fede996a429bd44b30a15e76ee293437 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Sun, 4 Oct 2015 01:59:06 +0200 Subject: hashmap: hash_funcs - make inputs unambiguous Make sure all variable-length inputs are properly terminated or that their length is encoded in some way. This avoids ambiguity of adjacent inputs. E.g., in case of a hash function taking two strings, compressing "ab" followed by "c" is now distinct from "a" followed by "bc". --- src/basic/hashmap.c | 2 +- src/libsystemd-network/sd-dhcp-server.c | 1 + src/libsystemd-network/sd-lldp.c | 1 + src/libsystemd/sd-bus/busctl.c | 5 +++++ src/shared/dns-domain.c | 6 ++++++ 5 files changed, 14 insertions(+), 1 deletion(-) (limited to 'src/libsystemd') diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index 3c05bee56d..3e17ed30df 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -277,7 +277,7 @@ static const struct hashmap_type_info hashmap_type_info[_HASHMAP_TYPE_MAX] = { }; void string_hash_func(const void *p, struct siphash *state) { - siphash24_compress(p, strlen(p), state); + siphash24_compress(p, strlen(p) + 1, state); } int string_compare_func(const void *a, const void *b) { diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index 4ea5a29e5c..d941e6c0de 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -117,6 +117,7 @@ void client_id_hash_func(const void *p, struct siphash *state) { assert(id->length); assert(id->data); + siphash24_compress(&id->length, sizeof(id->length), state); siphash24_compress(id->data, id->length, state); } diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c index b9bf4d3c00..0b7d35cdf1 100644 --- a/src/libsystemd-network/sd-lldp.c +++ b/src/libsystemd-network/sd-lldp.c @@ -74,6 +74,7 @@ static void chassis_id_hash_func(const void *p, struct siphash *state) { assert(id); assert(id->data); + siphash24_compress(&id->length, sizeof(id->length), state); siphash24_compress(id->data, id->length, state); } diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c index 496b9a7b4e..49c97af339 100644 --- a/src/libsystemd/sd-bus/busctl.c +++ b/src/libsystemd/sd-bus/busctl.c @@ -630,12 +630,17 @@ typedef struct Member { static void member_hash_func(const void *p, struct siphash *state) { const Member *m = p; + uint64_t arity = 1; assert(m); assert(m->type); string_hash_func(m->type, state); + arity += !!m->name + !!m->interface; + + uint64_hash_func(&arity, state); + if (m->name) string_hash_func(m->name, state); diff --git a/src/shared/dns-domain.c b/src/shared/dns-domain.c index 1517443736..5680f01bd9 100644 --- a/src/shared/dns-domain.c +++ b/src/shared/dns-domain.c @@ -399,11 +399,17 @@ void dns_name_hash_func(const void *s, struct siphash *state) { if (k > 0) r = k; + if (r == 0) + break; + label[r] = 0; ascii_strlower(label); string_hash_func(label, state); } + + /* enforce that all names are terminated by the empty label */ + string_hash_func("", state); } int dns_name_compare_func(const void *a, const void *b) { -- cgit v1.2.3-54-g00ecf