]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-test: fuzzer - Make random number generator deterministic when fuzzer is active
authorStephan Bosch <stephan.bosch@open-xchange.com>
Sat, 5 Oct 2024 12:49:25 +0000 (14:49 +0200)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Thu, 9 Oct 2025 08:41:22 +0000 (08:41 +0000)
m4/dovecot.m4
src/lib/rand.c
src/lib/randgen.c
src/lib/randgen.h

index 6567e4646a3506f746e7bcad6f29ee9e079948e7..f828c93b48e241124dd26f598cfb151811b0f55f 100644 (file)
@@ -310,6 +310,7 @@ AC_DEFUN([DC_DOVECOT_FUZZER],[
                 with_fuzzer=no)
        AS_IF([test x$with_fuzzer = xclang], [
                AM_CFLAGS="$AM_CFLAGS -fsanitize=fuzzer-no-link"
+               AM_CFLAGS="$AM_CFLAGS -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION"
                # use $LIB_FUZZING_ENGINE for linking if it exists
                FUZZER_LDFLAGS=${LIB_FUZZING_ENGINE--fsanitize=fuzzer}
                # May need to use CXXLINK for linking, which wants sources to
index 12c9686b604952863a0a29c1b064bd20bf8728d2..ffbdd33e7d3948050c53b25b4167d2aea707568d 100644 (file)
@@ -3,7 +3,7 @@
 #include "lib.h"
 #include "randgen.h"
 
-#ifdef HAVE_ARC4RANDOM
+#if defined(HAVE_ARC4RANDOM) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
 #ifdef HAVE_LIBBSD
 #include <bsd/stdlib.h>
 #endif
index 59dc617568e9c1e6508ce96b70409240038f6a03..2a0fd7fddde24f38b68197d53cc5e362a3d5a17e 100644 (file)
@@ -5,7 +5,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 
-#ifdef DEBUG
+#if defined(DEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
 /* For reproducing tests, fall back onto using a simple deterministic PRNG */
 /* Marsaglia's 1999 KISS, de-macro-ified, and with the fixed KISS11 SHR3,
    which is clearly what was intended given the "cycle length 2^123" claim. */
@@ -121,7 +121,7 @@ void random_fill(void *buf, size_t size)
        i_assert(init_refcount > 0);
        i_assert(size < SSIZE_T_MAX);
 
-#ifdef DEBUG
+#if defined(DEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
        if (kiss_in_use) {
                for (size_t pos = 0; pos < size; pos++)
                        ((unsigned char*)buf)[pos] = kiss_rand();
@@ -167,13 +167,17 @@ void random_fill(void *buf, size_t size)
 
 void random_init(void)
 {
+       if (init_refcount++ > 0)
+               return;
+
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+       kiss_init(0);
+       srand(0);
+#else
        /* static analyzer seems to require this */
        unsigned int seed = 0;
        const char *env_seed;
 
-       if (init_refcount++ > 0)
-               return;
-
        env_seed = getenv("DOVECOT_SRAND");
 #ifdef DEBUG
        if (env_seed != NULL && str_to_uint(env_seed, &seed) >= 0) {
@@ -204,6 +208,7 @@ void random_init(void)
 normal_exit:
 #endif
        srand(seed);
+#endif
 }
 
 void random_deinit(void)
index cab234d9844f3c67cbe248ca6d045c5a8ceb0260..f5062c7325cc7a4a8bd912a57471d8c89b1230a7 100644 (file)
@@ -9,7 +9,7 @@ void random_fill(void *buf, size_t size);
 void random_init(void);
 void random_deinit(void);
 
-#ifdef DEBUG
+#if defined(DEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
 /* Debug helper to make random tests reproduceable. 0=got seed, -1=failure. */
 int rand_get_last_seed(unsigned int *seed_r);
 #endif