From f63695609c88a3f76129499bb49fb82e8155fb32 Mon Sep 17 00:00:00 2001 From: ellie timoney Date: Wed, 19 May 2021 14:01:41 +1000 Subject: [PATCH] hash: use a seed when hashing Part of CVE-2021-33582 --- lib/hash.c | 9 ++++++--- lib/hash.h | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/hash.c b/lib/hash.c index cf85d9d2ca..1b025b21b5 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -48,6 +48,9 @@ hash_table *construct_hash_table(hash_table *table, size_t size, int use_mpool) fatal("construct_hash_table called without a size", EC_TEMPFAIL); table->size = size; + do { + table->seed = rand(); + } while (table->seed == 0); /* Allocate the table -- different for using memory pools and not */ if(use_mpool) { @@ -76,7 +79,7 @@ hash_table *construct_hash_table(hash_table *table, size_t size, int use_mpool) void *hash_insert(const char *key, void *data, hash_table *table) { - unsigned val = strhash(key) % table->size; + unsigned val = strhash_seeded(table->seed, key) % table->size; bucket *ptr, *newptr; bucket **prev; @@ -163,7 +166,7 @@ void *hash_lookup(const char *key, hash_table *table) if (!table->size) return NULL; - val = strhash(key) % table->size; + val = strhash_seeded(table->seed, key) % table->size; if (!(table->table)[val]) return NULL; @@ -187,7 +190,7 @@ void *hash_lookup(const char *key, hash_table *table) * since it will leak memory until you get rid of the entire hash table */ void *hash_del(char *key, hash_table *table) { - unsigned val = strhash(key) % table->size; + unsigned val = strhash_seeded(table->seed, key) % table->size; bucket *ptr, *last = NULL; if (!(table->table)[val]) diff --git a/lib/hash.h b/lib/hash.h index 77d0dc8a33..c3bc1dc2f5 100644 --- a/lib/hash.h +++ b/lib/hash.h @@ -5,9 +5,13 @@ #define HASH__H #include /* For size_t */ +#include + #include "strhash.h" #include "mpool.h" +#define HASH_TABLE_INITIALIZER {0, 0, NULL, NULL} + /* ** A hash table consists of an array of these buckets. Each bucket ** holds a copy of the key, a pointer to the data associated with the @@ -32,6 +36,7 @@ typedef struct bucket { typedef struct hash_table { size_t size; + uint32_t seed; bucket **table; struct mpool *pool; } hash_table;