Skip to content

Commit f227e3e

Browse files
wtarreautorvalds
authored andcommitted
random32: update the net random state on interrupt and activity
This modifies the first 32 bits out of the 128 bits of a random CPU's net_rand_state on interrupt or CPU activity to complicate remote observations that could lead to guessing the network RNG's internal state. Note that depending on some network devices' interrupt rate moderation or binding, this re-seeding might happen on every packet or even almost never. In addition, with NOHZ some CPUs might not even get timer interrupts, leaving their local state rarely updated, while they are running networked processes making use of the random state. For this reason, we also perform this update in update_process_times() in order to at least update the state when there is user or system activity, since it's the only case we care about. Reported-by: Amit Klein <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Cc: Eric Dumazet <[email protected]> Cc: "Jason A. Donenfeld" <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Kees Cook <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: <[email protected]> Signed-off-by: Willy Tarreau <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 6ba1b00 commit f227e3e

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

drivers/char/random.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ void add_interrupt_randomness(int irq, int irq_flags)
12771277

12781278
fast_mix(fast_pool);
12791279
add_interrupt_bench(cycles);
1280+
this_cpu_add(net_rand_state.s1, fast_pool->pool[cycles & 3]);
12801281

12811282
if (unlikely(crng_init == 0)) {
12821283
if ((fast_pool->count >= 64) &&

include/linux/random.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/kernel.h>
1212
#include <linux/list.h>
1313
#include <linux/once.h>
14+
#include <linux/percpu.h>
1415

1516
#include <uapi/linux/random.h>
1617

@@ -119,6 +120,8 @@ struct rnd_state {
119120
__u32 s1, s2, s3, s4;
120121
};
121122

123+
DECLARE_PER_CPU(struct rnd_state, net_rand_state) __latent_entropy;
124+
122125
u32 prandom_u32_state(struct rnd_state *state);
123126
void prandom_bytes_state(struct rnd_state *state, void *buf, size_t nbytes);
124127
void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state);

kernel/time/timer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <linux/sched/debug.h>
4444
#include <linux/slab.h>
4545
#include <linux/compat.h>
46+
#include <linux/random.h>
4647

4748
#include <linux/uaccess.h>
4849
#include <asm/unistd.h>
@@ -1742,6 +1743,13 @@ void update_process_times(int user_tick)
17421743
scheduler_tick();
17431744
if (IS_ENABLED(CONFIG_POSIX_TIMERS))
17441745
run_posix_cpu_timers();
1746+
1747+
/* The current CPU might make use of net randoms without receiving IRQs
1748+
* to renew them often enough. Let's update the net_rand_state from a
1749+
* non-constant value that's not affine to the number of calls to make
1750+
* sure it's updated when there's some activity (we don't care in idle).
1751+
*/
1752+
this_cpu_add(net_rand_state.s1, rol32(jiffies, 24) + user_tick);
17451753
}
17461754

17471755
/**

lib/random32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static inline void prandom_state_selftest(void)
4848
}
4949
#endif
5050

51-
static DEFINE_PER_CPU(struct rnd_state, net_rand_state) __latent_entropy;
51+
DEFINE_PER_CPU(struct rnd_state, net_rand_state) __latent_entropy;
5252

5353
/**
5454
* prandom_u32_state - seeded pseudo-random number generator.

0 commit comments

Comments
 (0)