random32: make prandom_u32() less predictable
Subsystems (4)
v4 cover random32: make prandom_u32() less predictable
This is the cleanup of the latest series of prandom_u32 experimentations consisting in using SipHash instead of Tausworthe to produce the randoms used by the network stack. The changes to the files were kept minimal, and the controversial commit that used to take noise from the fast_pool (f227e3ec3b5c) was reverted. Instead, a dedicated "net_rand_noise" per_cpu variable is fed from various sources of activities (networking, scheduling) to perturb the SipHash state using fast, non-trivially predictable data, instead of keeping it fully deterministic. The goal is essentially to make any occasional memory leakage or brute-force attempt useless.
The resulting code was verified to be very slightly faster on x86_64 than what is was with the controversial commit above, though this remains barely above measurement noise. It was also tested on i386 and arm, and build- tested only on arm64.
The whole discussion around this is archived here:
https://lore.kernel.org/netdev/[email protected]/
The code is also available for you to pull at:
git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/prandom.git tags/20201024-v4-5.10
---
v4:
- access noise using raw_cpu_read() instead of this_cpu_read()
- fixed build with CONFIG_RANDOM32_SELFTEST
- added a selftest for the prandom32 code
v3:
This v3 is a rebase on top of 5.9-final, and switches __this_cpu_read() for this_cpu_read() to address a crash on i386+SMP+PREEMPT reported by LKP. Nothing else was changed.
George Spelvin (1):
random32: make prandom_u32() output unpredictable
Willy Tarreau (2):
random32: add noise from network and scheduling activity
random32: add a selftest for the prandom32 code
drivers/char/random.c | 1 - include/linux/prandom.h | 55 ++++- kernel/time/timer.c | 9 +- lib/random32.c | 525 ++++++++++++++++++++++++++-------------- net/core/dev.c | 4 + 5 files changed, 404 insertions(+), 190 deletions(-)
Signature · 1 line
2.28.0
PATCH v4 1/3 random32: make prandom_u32() output unpredictable
From: George Spelvin <[email protected]>
Non-cryptographic PRNGs may have great statistical properties, but are usually trivially predictable to someone who knows the algorithm, given a small sample of their output. An LFSR like prandom_u32() is particularly simple, even if the sample is widely scattered bits.
It turns out the network stack uses prandom_u32() for some things like random port numbers which it would prefer are *not* trivially predictable. Predictability led to a practical DNS spoofing attack. Oops.
This patch replaces the LFSR with a homebrew cryptographic PRNG based on the SipHash round function, which is in turn seeded with 128 bits of strong random key. (The authors of SipHash have *not* been consulted about this abuse of their algorithm.) Speed is prioritized over security; attacks are rare, while performance is always wanted.
Replacing all callers of prandom_u32() is the quick fix. Whether to reinstate a weaker PRNG for uses which can tolerate it is an open question.
Commit f227e3ec3b5c ("random32: update the net random state on interrupt and activity") was an earlier attempt at a solution. This patch replaces it.
[ willy: partial reversal of f227e3ec3b5c; moved SIPROUND definitions
to prandom.h for later use; merged George's prandom_seed() proposal; inlined siprand_u32(); replaced the net_rand_state[] array with 4 members to fix a build issue; cosmetic cleanups to make checkpatch happy; fixed RANDOM32_SELFTEST build ]
--- drivers/char/random.c | 1 - include/linux/prandom.h | 36 +++- kernel/time/timer.c | 7 - lib/random32.c | 464 ++++++++++++++++++++++++---------------- 4 files changed, 318 insertions(+), 190 deletions(-)
drivers/char/random.c +0 -1
@@ -1277,7 +1277,6 @@ void add_interrupt_randomness(int irq, int irq_flags)fast_mix (fast_pool);add_interrupt_bench (cycles); -this_cpu_add (net_rand_state.s1, fast_pool->pool[cycles &3 ]);if (unlikely (crng_init ==0 )) { if ((fast_pool->count >= 64) &&
include/linux/prandom.h +34 -2
@@ -16,12 +16,44 @@ void prandom_bytes(void *buf, size_t nbytes);void prandom_seed (u32 seed);void prandom_reseed_late (void ); +#if BITS_PER_LONG ==64 +/* +* The core SipHash round function. Each line can be executed in +* parallel given enough CPU resources. +*/ +#define PRND_SIPROUND (v0, v1, v2, v3) ( \ + v0 += v1, v1 = rol64(v1, 13), v2 += v3, v3 = rol64(v3, 16), \ + v1 ^= v0, v0 = rol64(v0, 32), v3 ^= v2, \ + v0 += v3, v3 = rol64(v3, 21), v2 += v1, v1 = rol64(v1, 17), \ + v3 ^= v0, v1 ^= v2, v2 = rol64(v2, 32) \ +) + +#define PRND_K0 (0x736f6d6570736575 ^ 0x6c7967656e657261) +#define PRND_K1 (0x646f72616e646f6d ^ 0x7465646279746573) + +#elif BITS_PER_LONG ==32 +/* +* On 32-bit machines, we use HSipHash, a reduced-width version of SipHash. +* This is weaker, but 32-bit machines are not used for high-traffic +* applications, so there is less output for an attacker to analyze. +*/ +#define PRND_SIPROUND (v0, v1, v2, v3) ( \ + v0 += v1, v1 = rol32(v1, 5), v2 += v3, v3 = rol32(v3, 8), \ + v1 ^= v0, v0 = rol32(v0, 16), v3 ^= v2, \ + v0 += v3, v3 = rol32(v3, 7), v2 += v1, v1 = rol32(v1, 13), \ + v3 ^= v0, v1 ^= v2, v2 = rol32(v2, 16) \ +) +#define PRND_K0 0x6c796765 +#define PRND_K1 0x74656462 + +#else +#error Unsupported BITS_PER_LONG +#endif +struct rnd_state {__u32 s1, s2, s3, s4; }; -DECLARE_PER_CPU(struct rnd_state, net_rand_state); -u32 prandom_u32_state (struct rnd_state *state);void prandom_bytes_state (struct rnd_state *state,void *buf,size_t nbytes);void prandom_seed_full_state (struct rnd_state __percpu *pcpu_state);
kernel/time/timer.c +0 -7
@@ -1717,13 +1717,6 @@ void update_process_times(int user_tick)scheduler_tick ();if (IS_ENABLED (CONFIG_POSIX_TIMERS ))run_posix_cpu_timers (); - -/* The current CPU might make use of net randoms without receiving IRQs -* to renew them often enough. Let's update the net_rand_state from a -* non-constant value that's not affine to the number of calls to make -* sure it's updated when there's some activity (we don't care in idle). -*/ -this_cpu_add (net_rand_state.s1,rol32 (jiffies,24 ) + user_tick); }/**
lib/random32.c +284 -180
505 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH v4 2/3 random32: add noise from network and scheduling activity
In reply to Willy TarreauWith the removal of the interrupt perturbations in previous random32 change (random32: make prandom_u32() output unpredictable), the PRNG has become 100% deterministic again. While SipHash is expected to be way more robust against brute force than the previous Tausworthe LFSR, there's still the risk that whoever has even one temporary access to the PRNG's internal state is able to predict all subsequent draws till the next reseed (roughly every minute). This may happen through a side channel attack or any data leak.
This patch restores the spirit of commit f227e3ec3b5c ("random32: update the net random state on interrupt and activity") in that it will perturb the internal PRNG's statee using externally collected noise, except that it will not pick that noise from the random pool's bits nor upon interrupt, but will rather combine a few elements along the Tx path that are collectively hard to predict, such as dev, skb and txq pointers, packet length and jiffies values. These ones are combined using a single round of SipHash into a single long variable that is mixed with the net_rand_state upon each invocation.
The operation was inlined because it produces very small and efficient code, typically 3 xor, 2 add and 2 rol. The performance was measured to be the same (even very slightly better) than before the switch to SipHash; on a 6-core 12-thread Core i7-8700k equipped with a 40G NIC (i40e), the connection rate dropped from 556k/s to 555k/s while the SYN cookie rate grew from 5.38 Mpps to 5.45 Mpps.
--- include/linux/prandom.h | 19 +++++++++++++++++++ kernel/time/timer.c | 2 ++ lib/random32.c | 5 +++++ net/core/dev.c | 4 ++++ 4 files changed, 30 insertions(+)
include/linux/prandom.h +19 -0
@@ -16,6 +16,12 @@ void prandom_bytes(void *buf, size_t nbytes);void prandom_seed (u32 seed);void prandom_reseed_late (void ); +DECLARE_PER_CPU(unsigned long, net_rand_noise); + +#define PRANDOM_ADD_NOISE (a, b, c, d) \ + prandom_u32_add_noise((unsigned long)(a), (unsigned long)(b), \ + (unsigned long)(c), (unsigned long)(d)) +#if BITS_PER_LONG ==64 /* * The core SipHash round function. Each line can be executed in
@@ -50,6 +56,18 @@ void prandom_reseed_late(void);#error Unsupported BITS_PER_LONG#endif +static inline void prandom_u32_add_noise (unsigned long a,unsigned long b, +unsigned long c,unsigned long d) +{ +/* +* This is not used cryptographically; it's just +* a convenient 4-word hash function. (3 xor, 2 add, 2 rol) +*/ + a ^=raw_cpu_read (net_rand_noise); +PRND_SIPROUND (a, b, c, d); +raw_cpu_write (net_rand_noise, d); +} +struct rnd_state {__u32 s1, s2, s3, s4; };
@@ -99,6 +117,7 @@ static inline void prandom_seed_state(struct rnd_state *state, u64 seed) state->s2 =__seed (i,8U ); state->s3 =__seed (i,16U ); state->s4 =__seed (i,128U ); +PRANDOM_ADD_NOISE (state, i,0 ,0 ); }/* Pseudo random number generator from numerical recipes. */
kernel/time/timer.c +2 -0
@@ -1706,6 +1706,8 @@ void update_process_times(int user_tick) {struct task_struct *p = current; +PRANDOM_ADD_NOISE (jiffies, user_tick, p,0 ); +/* Note: this timer irq context must be accounted for as well. */ account_process_tick (p, user_tick);run_local_timers ();
lib/random32.c +5 -0
@@ -337,6 +337,8 @@ struct siprand_state { }; static DEFINE_PER_CPU(struct siprand_state, net_rand_state) __latent_entropy; +DEFINE_PER_CPU(unsigned long, net_rand_noise); +EXPORT_PER_CPU_SYMBOL (net_rand_noise);/* * This is the core CPRNG function. As "pseudorandom", this is not used
@@ -360,9 +362,12 @@ static DEFINE_PER_CPU(struct siprand_state, net_rand_state) __latent_entropy;static inline u32 siprand_u32 (struct siprand_state *s) {unsigned long v0 = s->v0, v1 = s->v1, v2 = s->v2, v3 = s->v3; +unsigned long n =raw_cpu_read (net_rand_noise); + v3 ^= n;PRND_SIPROUND (v0, v1, v2, v3);PRND_SIPROUND (v0, v1, v2, v3); + v0 ^= n; s->v0 = v0; s->v1 = v1; s->v2 = v2; s->v3 = v3;return v1 + v3; }
net/core/dev.c +4 -0
@@ -145,6 +145,7 @@#include <linux/indirect_call_wrapper.h> #include <net/devlink.h> #include <linux/pm_runtime.h> +#include <linux/prandom.h> #include "net-sysfs.h"
@@ -3558,6 +3559,7 @@ static int xmit_one(struct sk_buff *skb, struct net_device *dev,dev_queue_xmit_nit (skb, dev); len = skb->len; +PRANDOM_ADD_NOISE (skb, dev, txq, len + jiffies);trace_net_dev_start_xmit (skb, dev); rc =netdev_start_xmit (skb, dev, txq, more);trace_net_dev_xmit (skb, rc, dev, len);
@@ -4130,6 +4132,7 @@ static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)if (!skb) goto out; +PRANDOM_ADD_NOISE (skb, dev, txq, jiffies);HARD_TX_LOCK (dev, txq, cpu);if (!netif_xmit_stopped (txq)) {
@@ -4195,6 +4198,7 @@ int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)skb_set_queue_mapping (skb, queue_id); txq =skb_get_tx_queue (dev, skb); +PRANDOM_ADD_NOISE (skb, dev, txq, jiffies);local_bh_disable ();
Signature · 1 line
2.28.0
PATCH v4 3/3 random32: add a selftest for the prandom32 code
In reply to Willy TarreauGiven that this code is new, let's add a selftest for it as well. It doesn't rely on fixed sets, instead it picks 1024 numbers and verifies that they're not more correlated than desired.
--- lib/random32.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
lib/random32.c +56 -0
@@ -38,6 +38,7 @@#include <linux/jiffies.h> #include <linux/random.h> #include <linux/sched.h> +#include <linux/bitops.h> #include <asm/unaligned.h> #include <trace/events/random.h>
@@ -556,6 +557,61 @@ static void prandom_timer_start(struct random_ready_callback *unused)mod_timer (&seed_timer, jiffies); } +#ifdef CONFIG_RANDOM32_SELFTEST +/* Principle: True 32-bit random numbers will all have 16 differing bits on +* average. For each 32-bit number, there are 601M numbers differing by 16 +* bits, and 89% of the numbers differ by at least 12 bits. Note that more +* than 16 differing bits also implies a correlation with inverted bits. Thus +* we take 1024 random numbers and compare each of them to the other ones, +* counting the deviation of correlated bits to 16. Constants report 32, +* counters 32-log2(TEST_SIZE), and pure randoms, around 6 or lower. With the +* u32 total, TEST_SIZE may be as large as 4096 samples. +*/ +#define TEST_SIZE 1024 +static int __init prandom32_state_selftest (void ) +{ +unsigned int x, y, bits, samples; +u32 xor, flip; +u32 total; +u32 *data; + + data =kmalloc (sizeof (*data) *TEST_SIZE ,GFP_KERNEL ); +if (!data) +return 0 ; + +for (samples =0 ; samples <TEST_SIZE ; samples++) + data[samples] =prandom_u32 (); + + flip = total =0 ; +for (x =0 ; x < samples; x++) { +for (y =0 ; y < samples; y++) { +if (x == y) +continue ; + xor = data[x] ^ data[y]; + flip |= xor; + bits =hweight32 (xor); + total += (bits -16 ) * (bits -16 ); + } + } + +/* We'll return the average deviation as 2*sqrt(corr/samples), which +* is also sqrt(4*corr/samples) which provides a better resolution. +*/ + bits =int_sqrt (total / (samples * (samples -1 )) *4 ); +if (bits >6 ) +pr_warn ("prandom32: self test failed (at least %u bits" +" correlated, fixed_mask=%#x fixed_value=%#x\n" , + bits, ~flip, data[0 ] & ~flip); +else +pr_info ("prandom32: self test passed (less than %u bits" +" correlated)\n" , + bits+1 ); +kfree (data); +return 0 ; +} +core_initcall (prandom32_state_selftest); +#endif /* CONFIG_RANDOM32_SELFTEST */ +/* * Start periodic full reseeding as soon as strong * random numbers are available.
Signature · 1 line
2.28.0