aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-11-14 14:33:09 +1100
committerStefano Brivio <sbrivio@redhat.com>2024-11-14 19:00:38 +0100
commitb39760cc7d89e69c7fb12eccc3df3bd15e2d5665 (patch)
tree108160846ba6cd58147468b642654d4d15c44a2f
parent71d5deed5eed3949ee09c5f0a53b4de0b09b4afc (diff)
downloadpasst-b39760cc7d89e69c7fb12eccc3df3bd15e2d5665.tar
passt-b39760cc7d89e69c7fb12eccc3df3bd15e2d5665.tar.gz
passt-b39760cc7d89e69c7fb12eccc3df3bd15e2d5665.tar.bz2
passt-b39760cc7d89e69c7fb12eccc3df3bd15e2d5665.tar.lz
passt-b39760cc7d89e69c7fb12eccc3df3bd15e2d5665.tar.xz
passt-b39760cc7d89e69c7fb12eccc3df3bd15e2d5665.tar.zst
passt-b39760cc7d89e69c7fb12eccc3df3bd15e2d5665.zip
passt: Seed libc's pseudo random number generator
We have an upcoming case where we need pseudo-random numbers to scatter timings, but we don't need cryptographically strong random numbers. libc's built in random() is fine for this purpose, but we should seed it. Extend secret_init() - the only current user of random numbers - to do this as well as generating the SipHash secret. Using /dev/random for a PRNG seed is probably overkill, but it's simple and we only do it once, so we might as well. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--passt.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/passt.c b/passt.c
index 73649de..83b26c5 100644
--- a/passt.c
+++ b/passt.c
@@ -110,12 +110,19 @@ static void post_handler(struct ctx *c, const struct timespec *now)
}
/**
- * secret_init() - Create secret value for SipHash calculations
+ * random_init() - Initialise things based on random data
* @c: Execution context
*/
-static void secret_init(struct ctx *c)
+static void random_init(struct ctx *c)
{
+ unsigned int seed;
+
+ /* Create secret value for SipHash calculations */
raw_random(&c->hash_secret, sizeof(c->hash_secret));
+
+ /* Seed pseudo-RNG for things that need non-cryptographic random */
+ raw_random(&seed, sizeof(seed));
+ srandom(seed);
}
/**
@@ -236,7 +243,7 @@ int main(int argc, char **argv)
tap_sock_init(&c);
- secret_init(&c);
+ random_init(&c);
if (clock_gettime(CLOCK_MONOTONIC, &now))
die_perror("Failed to get CLOCK_MONOTONIC time");