random32: use real rng for non-deterministic randomness
Subsystems (6)
PATCH 1/1 random32: use real rng for non-deterministic randomness
random32.c has two RNGs in it: one that is meant to be used deterministically, with some predefined seed, and one that does the same exact thing as random.c, except does it poorly. The first one has some use cases. The second one no longer does and can be replaced with calls to random.c's proper random number generator.
The relatively recent siphash-based bad random32.c code was added in response to concerns that the prior random32.c was too deterministic. Out of fears that random.c was (at the time) too slow, this code was anonymously contributed by somebody who was likely reusing the alias of long time anonymous contributor George Spelvin. Then out of that emerged a kind of shadow entropy gathering system, with its own tentacles throughout various net code, added willy nilly.
Stop👏making👏crappy👏bespoke👏random👏number👏generators👏.
Fortunately, recently advances in random.c mean that we can stop playing with this sketchiness, and just use get_random_u32(), which is now fast enough. In micro benchmarks using RDPMC, I'm seeing the same median cycle count between the two functions, with the mean being _slightly_ higher due to batches refilling (which we can optimize further need be). However, when doing *real* benchmarks of the net functions that actually use these random numbers, the mean cycles actually *decreased* slightly (with the median still staying the same), likely because the additional prandom code means icache misses and complexity, whereas random.c is generally already being used by something else nearby.
The biggest benefit of this is that there are many users of prandom who probably should be using cryptographically secure random numbers. This makes all of those accidental cases become secure by just flipping a switch. Later on, we can do a tree-wide cleanup to remove the static inline wrapper functions that this commit adds.
---
Jakub - If there are no objections to this plan, I intend on taking this through the random.git tree, which is what this commit is based on, with its recent siphash changes and such. -Jason
include/linux/prandom.h | 68 ++------- kernel/time/timer.c | 2 - lib/random32.c | 321 ---------------------------------------- net/core/dev.c | 3 - net/ipv4/devinet.c | 4 +- net/ipv6/addrconf.c | 2 - 6 files changed, 16 insertions(+), 384 deletions(-)
include/linux/prandom.h +15 -53
@@ -10,53 +10,16 @@#include <linux/types.h> #include <linux/percpu.h> -#include <linux/siphash.h> +#include <linux/random.h> -u32 prandom_u32 (void ); -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 -* parallel given enough CPU resources. -*/ -#define PRND_SIPROUND (v0, v1, v2, v3) SIPHASH_PERMUTATION(v0, v1, v2, v3) - -#define PRND_K0 (SIPHASH_CONST_0 ^ SIPHASH_CONST_2) -#define PRND_K1 (SIPHASH_CONST_1 ^ SIPHASH_CONST_3) - -#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) HSIPHASH_PERMUTATION(v0, v1, v2, v3) -#define PRND_K0 (HSIPHASH_CONST_0 ^ HSIPHASH_CONST_2) -#define PRND_K1 (HSIPHASH_CONST_1 ^ HSIPHASH_CONST_3) - -#else -#error Unsupported BITS_PER_LONG -#endif +static inline u32 prandom_u32 (void ) +{ +return get_random_u32 (); +} -static inline void prandom_u32_add_noise (unsigned long a,unsigned long b, -unsigned long c,unsigned long d) +static inline void prandom_bytes (void *buf,size_t nbytes) { -/* -* 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); +return get_random_bytes (buf, nbytes); }struct rnd_state {
@@ -70,6 +33,14 @@ void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state);#define prandom_init_once (pcpu_state) \ DO_ONCE(prandom_seed_full_state, (pcpu_state)) +/* +* Handle minimum values for seeds +*/ +static inline u32 __seed (u32 x,u32 m) +{ +return (x < m) ? x + m : x; +} +/** * prandom_u32_max - returns a pseudo-random number in interval [0, ep_ro) * @ep_ro: right open interval endpoint
@@ -87,14 +58,6 @@ static inline u32 prandom_u32_max(u32 ep_ro)return (u32)(((u64 )prandom_u32 () * ep_ro) >>32 ); } -/* -* Handle minimum values for seeds -*/ -static inline u32 __seed (u32 x,u32 m) -{ -return (x < m) ? x + m : x; -} -/** * prandom_seed_state - set seed for prandom_u32_state(). * @state: pointer to state structure to receive the seed.
@@ -108,7 +71,6 @@ 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 +0 -2
@@ -1780,8 +1780,6 @@ 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 +0 -321
324 lines not on this page · raw message
net/core/dev.c +0 -3
@@ -3527,7 +3527,6 @@ 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);
@@ -4168,7 +4167,6 @@ 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)) {
@@ -4234,7 +4232,6 @@ 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 ();
net/ipv4/devinet.c +1 -3
@@ -536,10 +536,8 @@ static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,return ret; } -if (!(ifa->ifa_flags &IFA_F_SECONDARY )) { -prandom_seed ((__force u32 ) ifa->ifa_local); +if (!(ifa->ifa_flags &IFA_F_SECONDARY )) ifap = last_primary; - }rcu_assign_pointer (ifa->ifa_next, *ifap);rcu_assign_pointer (*ifap, ifa);
net/ipv6/addrconf.c +0 -2
@@ -3972,8 +3972,6 @@ static void addrconf_dad_begin(struct inet6_ifaddr *ifp)addrconf_join_solict (dev, &ifp->addr); -prandom_seed ((__force u32 ) ifp->addr.s6_addr32[3 ]); -read_lock_bh (&idev->lock);spin_lock (&ifp->lock);if (ifp->state ==INET6_IFADDR_STATE_DEAD )
Signature · 1 line
2.35.1
Quoted 33 lines · On Wed, 11 May 2022 16:32:57 +0200 Jason A. Donenfeld wrote:
random32.chas two RNGs in it: one that is meant to be used deterministically, with some predefined seed, and one that does the same exact thing asrandom.c, except does it poorly. The first one has some use cases. The second one no longer does and can be replaced with calls to random.c's proper random number generator.The relatively recent siphash-based bad
random32.ccode was added in response to concerns that the priorrandom32.cwas too deterministic. Out of fears thatrandom.cwas (at the time) too slow, this code was anonymously contributed by somebody who was likely reusing the alias of long time anonymous contributor George Spelvin. Then out of that emerged a kind of shadow entropy gathering system, with its own tentacles throughout various net code, added willy nilly.Stop👏making👏crappy👏bespoke👏random👏number👏generators👏.
Fortunately, recently advances in
random.cmean that we can stop playing with this sketchiness, and just useget_random_u32(), which is now fast enough. In micro benchmarks using RDPMC, I'm seeing the same median cycle count between the two functions, with the mean being _slightly_ higher due to batches refilling (which we can optimize further need be). However, when doing *real* benchmarks of the net functions that actually use these random numbers, the mean cycles actually *decreased* slightly (with the median still staying the same), likely because the additional prandom code means icache misses and complexity, whereasrandom.cis generally already being used by something else nearby.The biggest benefit of this is that there are many users of prandom who probably should be using cryptographically secure random numbers. This makes all of those accidental cases become secure by just flipping a switch. Later on, we can do a tree-wide cleanup to remove the static inline wrapper functions that this commit adds.
Quoted 7 lines
Cc Jakub Kicinski <[email protected]>Cc Theodore Ts'o <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>---
Jakub - If there are no objections to this plan, I intend on taking this through the random.git tree, which is what this commit is based on, with its recent siphash changes and such. -Jason
I wish there was a good way to anycast to subsystem maintainers. With 4 netdev maintainers now even name smooshing won't work.
Quoted 33 lines · Jason A. Donenfeld
random32.chas two RNGs in it: one that is meant to be used deterministically, with some predefined seed, and one that does the same exact thing asrandom.c, except does it poorly. The first one has some use cases. The second one no longer does and can be replaced with calls to random.c's proper random number generator.The relatively recent siphash-based bad
random32.ccode was added in response to concerns that the priorrandom32.cwas too deterministic. Out of fears thatrandom.cwas (at the time) too slow, this code was anonymously contributed by somebody who was likely reusing the alias of long time anonymous contributor George Spelvin. Then out of that emerged a kind of shadow entropy gathering system, with its own tentacles throughout various net code, added willy nilly.Stop👏making👏crappy👏bespoke👏random👏number👏generators👏.
Fortunately, recently advances in
random.cmean that we can stop playing with this sketchiness, and just useget_random_u32(), which is now fast enough. In micro benchmarks using RDPMC, I'm seeing the same median cycle count between the two functions, with the mean being _slightly_ higher due to batches refilling (which we can optimize further need be). However, when doing *real* benchmarks of the net functions that actually use these random numbers, the mean cycles actually *decreased* slightly (with the median still staying the same), likely because the additional prandom code means icache misses and complexity, whereasrandom.cis generally already being used by something else nearby.The biggest benefit of this is that there are many users of prandom who probably should be using cryptographically secure random numbers. This makes all of those accidental cases become secure by just flipping a switch. Later on, we can do a tree-wide cleanup to remove the static inline wrapper functions that this commit adds.
Quoted 3 lines
Cc Jakub Kicinski <[email protected]>Cc Theodore Ts'o <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
Yay!
Am Wed, May 11, 2022 at 04:32:57PM +0200 schrieb Jason A. Donenfeld:
Quoted 33 lines
random32.chas two RNGs in it: one that is meant to be used deterministically, with some predefined seed, and one that does the same exact thing asrandom.c, except does it poorly. The first one has some use cases. The second one no longer does and can be replaced with calls to random.c's proper random number generator.The relatively recent siphash-based bad
random32.ccode was added in response to concerns that the priorrandom32.cwas too deterministic. Out of fears thatrandom.cwas (at the time) too slow, this code was anonymously contributed by somebody who was likely reusing the alias of long time anonymous contributor George Spelvin. Then out of that emerged a kind of shadow entropy gathering system, with its own tentacles throughout various net code, added willy nilly.Stop👏making👏crappy👏bespoke👏random👏number👏generators👏.
Fortunately, recently advances in
random.cmean that we can stop playing with this sketchiness, and just useget_random_u32(), which is now fast enough. In micro benchmarks using RDPMC, I'm seeing the same median cycle count between the two functions, with the mean being _slightly_ higher due to batches refilling (which we can optimize further need be). However, when doing *real* benchmarks of the net functions that actually use these random numbers, the mean cycles actually *decreased* slightly (with the median still staying the same), likely because the additional prandom code means icache misses and complexity, whereasrandom.cis generally already being used by something else nearby.The biggest benefit of this is that there are many users of prandom who probably should be using cryptographically secure random numbers. This makes all of those accidental cases become secure by just flipping a switch. Later on, we can do a tree-wide cleanup to remove the static inline wrapper functions that this commit adds.
Quoted 7 lines
Cc Jakub Kicinski <[email protected]>Cc Theodore Ts'o <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>---
Jakub - If there are no objections to this plan, I intend on taking this through the random.git tree, which is what this commit is based on, with its recent siphash changes and such. -Jason
Nice! However, wouldn't it be much better to clean up the indirection introduced here as well? prandom_u32() as wrapper for get_random_u32() and prandom_bytes() as wrapper for get_random_bytes() seems unnecessary...
Thanks,
Dominik
Hi Dominik,
Quoted 3 lines · Dominik Brodowski
Nice! However, wouldn't it be much better to clean up the indirection introduced here as well?
prandom_u32()as wrapper forget_random_u32()andprandom_bytes()as wrapper forget_random_bytes()seems unnecessary...
Yes; we can look at tree-wide changes for 5.20. The first step in making tree-wide changes is filling in the old function with an inline wrapper, which then gets removed as part of the last step after all the other patches have landed. That's a huge process, so this is just step one.
Jason