]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: Seed chunk_hash() only once, but before creating any hashtables
authorMartin Willi <martin@revosec.ch>
Thu, 3 Apr 2014 09:46:09 +0000 (11:46 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 4 Jun 2014 13:53:11 +0000 (15:53 +0200)
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.

src/libstrongswan/library.c
src/libstrongswan/tests/test_runner.c
src/libstrongswan/utils/chunk.c
src/libstrongswan/utils/chunk.h

index 93ff8400faf938d4160fe31facd11f111744c5b2..e3ad16411b6faa33d869baba8c687a635c9695da 100644 (file)
@@ -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 = {
index 4684eb18df6badd50a1981f92b50d0843c571aeb..63d79199f5645900571e2eb1d82353cce4357a46 100644 (file)
@@ -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);
        }
index ef79a74531c3f264480b8fea6d2b1f1f71d24cab..1a9674f4dbc46d1b332164f978a53703752840c7 100644 (file)
@@ -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;
 }
 
 /**
index 760f922e19aadf0e29a3d323fcf9fea887624797..9951ff31f761077d4107dd0adab324d1980e2282 100644 (file)
@@ -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();