From: Martin Willi Date: Thu, 3 Apr 2014 09:46:09 +0000 (+0200) Subject: unit-tests: Seed chunk_hash() only once, but before creating any hashtables X-Git-Tag: 5.2.0dr6~24^2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=460adb5d0925f4af807b09434b771545d1f62b47;p=thirdparty%2Fstrongswan.git unit-tests: Seed chunk_hash() only once, but before creating any hashtables Due to the removal of pthread_once, we manually create the seed for chunk_hash(). With the new testable functions interface, this won't work for the hashtable initiated using __attribute__((constructor)). Enforce seeding before creating that hashtable. --- diff --git a/src/libstrongswan/library.c b/src/libstrongswan/library.c index 93ff8400fa..e3ad16411b 100644 --- a/src/libstrongswan/library.c +++ b/src/libstrongswan/library.c @@ -243,7 +243,6 @@ bool library_init(char *settings, const char *namespace) { private_library_t *this; printf_hook_t *pfh; - static bool seeded = FALSE; if (lib) { /* already initialized, increase refcount */ @@ -252,13 +251,7 @@ bool library_init(char *settings, const char *namespace) return !this->integrity_failed; } - if (!seeded) - { - /* we do this just once to allow hash table lifetimes longer than - * one init/deinit cycle. */ - seeded = TRUE; - chunk_hash_seed(); - } + chunk_hash_seed(); INIT(this, .public = { diff --git a/src/libstrongswan/tests/test_runner.c b/src/libstrongswan/tests/test_runner.c index 4684eb18df..63d79199f5 100644 --- a/src/libstrongswan/tests/test_runner.c +++ b/src/libstrongswan/tests/test_runner.c @@ -44,6 +44,9 @@ void testable_functions_create() { if (!testable_functions) { + /* as this is executed before chunk_hash() seed initialization used + * by hashtables, we enforce seeding it here. */ + chunk_hash_seed(); testable_functions = hashtable_create(hashtable_hash_str, hashtable_equals_str, 8); } diff --git a/src/libstrongswan/utils/chunk.c b/src/libstrongswan/utils/chunk.c index ef79a74531..1a9674f4db 100644 --- a/src/libstrongswan/utils/chunk.c +++ b/src/libstrongswan/utils/chunk.c @@ -917,10 +917,17 @@ static u_char static_key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, */ void chunk_hash_seed() { + static bool seeded = FALSE; ssize_t len; size_t done = 0; int fd; + if (seeded) + { + /* just once to have the same seed during the whole process lifetimes */ + return; + } + fd = open("/dev/urandom", O_RDONLY); if (fd >= 0) { @@ -944,6 +951,7 @@ void chunk_hash_seed() key[done] = (u_char)random(); } } + seeded = TRUE; } /** diff --git a/src/libstrongswan/utils/chunk.h b/src/libstrongswan/utils/chunk.h index 760f922e19..9951ff31f7 100644 --- a/src/libstrongswan/utils/chunk.h +++ b/src/libstrongswan/utils/chunk.h @@ -343,7 +343,8 @@ bool chunk_printable(chunk_t chunk, chunk_t *sane, char replace); * Seed initial key for chunk_hash(). * * This call should get invoked once during startup. This is usually done - * by calling library_init(). + * by calling library_init(). Calling it multiple times is safe, it gets + * executed just once. */ void chunk_hash_seed();