Fix to randomize hash function, based on 28c3 congress, reported

by Peter van Dijk. Committed upstream in r2580.

Port diff from Brad, ok jakob@
This commit is contained in:
sthen 2012-01-10 22:15:54 +00:00
parent cb500b33c7
commit bcfb306eca
5 changed files with 163 additions and 3 deletions

View File

@ -1,9 +1,9 @@
# $OpenBSD: Makefile,v 1.36 2012/01/09 11:55:10 sthen Exp $
# $OpenBSD: Makefile,v 1.37 2012/01/10 22:15:54 sthen Exp $
COMMENT= validating DNS resolver
DISTNAME= unbound-1.4.14
REVISION= 1
REVISION= 2
CATEGORIES= net
MASTER_SITES= http://www.unbound.net/downloads/
@ -23,7 +23,7 @@ AUTOCONF_VERSION= 2.68
USE_LIBTOOL= Yes
NO_REGRESS= Yes
SHARED_LIBS+= unbound 3.0
SHARED_LIBS+= unbound 3.1
WANTLIB= c crypto event expat ldns>=4.9 ssl
LIB_DEPENDS+= net/ldns/libldns>=1.6.11

View File

@ -0,0 +1,23 @@
$OpenBSD: patch-daemon_daemon_c,v 1.1 2012/01/10 22:15:54 sthen Exp $
Fix to randomize hash function, based on 28c3 congress, reported
by Peter van Dijk.
--- daemon/daemon.c.orig Wed Jan 4 17:07:36 2012
+++ daemon/daemon.c Wed Jan 4 17:07:47 2012
@@ -63,6 +63,7 @@
#include "util/log.h"
#include "util/config_file.h"
#include "util/data/msgreply.h"
+#include "util/storage/lookup3.h"
#include "util/storage/slabhash.h"
#include "services/listen_dnsport.h"
#include "services/cache/rrset.h"
@@ -320,6 +321,7 @@ daemon_create_workers(struct daemon* daemon)
if(!daemon->rand)
fatal_exit("could not init random generator");
}
+ hash_set_raninit(ub_random(daemon->rand));
shufport = (int*)calloc(65536, sizeof(int));
if(!shufport)
fatal_exit("out of memory during daemon init");

View File

@ -0,0 +1,35 @@
$OpenBSD: patch-libunbound_libworker_c,v 1.1 2012/01/10 22:15:54 sthen Exp $
Fix to randomize hash function, based on 28c3 congress, reported
by Peter van Dijk.
--- libunbound/libworker.c.orig Wed Jan 4 17:09:21 2012
+++ libunbound/libworker.c Wed Jan 4 17:09:35 2012
@@ -58,6 +58,7 @@
#include "util/random.h"
#include "util/config_file.h"
#include "util/netevent.h"
+#include "util/storage/lookup3.h"
#include "util/storage/slabhash.h"
#include "util/net_help.h"
#include "util/data/dname.h"
@@ -158,6 +159,19 @@ libworker_setup(struct ub_ctx* ctx, int is_bg)
}
if(!w->is_bg || w->is_bg_thread) {
lock_basic_unlock(&ctx->cfglock);
+ }
+ if(1) {
+ /* primitive lockout for threading: if it overwrites another
+ * thread it is like wiping the cache (which is likely empty
+ * at the start) */
+ /* note we are holding the ctx lock in normal threaded
+ * cases so that is solved properly, it is only for many ctx
+ * in different threads that this may clash */
+ static int done_raninit = 0;
+ if(!done_raninit) {
+ done_raninit = 1;
+ hash_set_raninit(ub_random(w->env->rnd));
+ }
}
seed = 0;

View File

@ -0,0 +1,83 @@
$OpenBSD: patch-util_storage_lookup3_c,v 1.1 2012/01/10 22:15:54 sthen Exp $
Fix to randomize hash function, based on 28c3 congress, reported
by Peter van Dijk.
--- util/storage/lookup3.c.orig Wed Jan 4 17:16:34 2012
+++ util/storage/lookup3.c Wed Jan 4 17:16:54 2012
@@ -1,4 +1,5 @@
/*
+ January 2012(Wouter) added randomised initial value, fallout from 28c3.
March 2007(Wouter) adapted from lookup3.c original, add config.h include.
added #ifdef VALGRIND to remove 298,384,660 'unused variable k8' warnings.
added include of lookup3.h to check definitions match declarations.
@@ -52,6 +53,15 @@ on 1 byte), but shoehorning those bytes into integers
# include <endian.h> /* attempt to define endianness */
#endif
+/* random initial value */
+static uint32_t raninit = 0xdeadbeef;
+
+void
+hash_set_raninit(uint32_t v)
+{
+ raninit = v;
+}
+
/*
* My best guess at if you are big-endian or little-endian. This may
* need adjustment.
@@ -187,7 +197,7 @@ uint32_t initval) /* the previous hash,
uint32_t a,b,c;
/* Set up the internal state */
- a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
+ a = b = c = raninit + (((uint32_t)length)<<2) + initval;
/*------------------------------------------------- handle most of the key */
while (length > 3)
@@ -234,7 +244,7 @@ uint32_t *pb) /* IN: more seed OUT
uint32_t a,b,c;
/* Set up the internal state */
- a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
+ a = b = c = raninit + ((uint32_t)(length<<2)) + *pc;
c += *pb;
/*------------------------------------------------- handle most of the key */
@@ -297,7 +307,7 @@ uint32_t hashlittle( const void *key, size_t length, u
union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
/* Set up the internal state */
- a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
+ a = b = c = raninit + ((uint32_t)length) + initval;
u.ptr = key;
if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
@@ -484,7 +494,7 @@ void hashlittle2(
union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
/* Set up the internal state */
- a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
+ a = b = c = raninit + ((uint32_t)length) + *pc;
c += *pb;
u.ptr = key;
@@ -666,7 +676,7 @@ uint32_t hashbig( const void *key, size_t length, uint
union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
/* Set up the internal state */
- a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
+ a = b = c = raninit + ((uint32_t)length) + initval;
u.ptr = key;
if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
@@ -941,7 +951,7 @@ void driver3()
printf("hashlittle2 and hashlittle mismatch\n");
/* check that hashword2 and hashword produce the same results */
- len = 0xdeadbeef;
+ len = raninit;
i=47, j=0;
hashword2(&len, 1, &i, &j);
if (hashword(&len, 1, 47) != i)

View File

@ -0,0 +1,19 @@
$OpenBSD: patch-util_storage_lookup3_h,v 1.1 2012/01/10 22:15:54 sthen Exp $
Fix to randomize hash function, based on 28c3 congress, reported
by Peter van Dijk.
--- util/storage/lookup3.h.orig Wed Jan 4 17:16:41 2012
+++ util/storage/lookup3.h Wed Jan 4 17:16:56 2012
@@ -61,4 +61,11 @@ uint32_t hashword(const uint32_t *k, size_t length, ui
*/
uint32_t hashlittle(const void *k, size_t length, uint32_t initval);
+/**
+ * Set the randomisation initial value, set this before threads start,
+ * and before hashing stuff (because it changes subsequent results).
+ * @param v: value
+ */
+void hash_set_raninit(uint32_t v);
+
#endif /* UTIL_STORAGE_LOOKUP3_H */