1a07fdae3SJason A. Donenfeld // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) 21da177e4SLinus Torvalds /* 39f9eff85SJason A. Donenfeld * Copyright (C) 2017-2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 49e95ce27SMatt Mackall * Copyright Matt Mackall <mpm@selenic.com>, 2003, 2004, 2005 55f75d9f3SJason A. Donenfeld * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All rights reserved. 61da177e4SLinus Torvalds * 75f75d9f3SJason A. Donenfeld * This driver produces cryptographically secure pseudorandom data. It is divided 85f75d9f3SJason A. Donenfeld * into roughly six sections, each with a section header: 91da177e4SLinus Torvalds * 105f75d9f3SJason A. Donenfeld * - Initialization and readiness waiting. 115f75d9f3SJason A. Donenfeld * - Fast key erasure RNG, the "crng". 125f75d9f3SJason A. Donenfeld * - Entropy accumulation and extraction routines. 135f75d9f3SJason A. Donenfeld * - Entropy collection routines. 145f75d9f3SJason A. Donenfeld * - Userspace reader/writer interfaces. 155f75d9f3SJason A. Donenfeld * - Sysctl interface. 161da177e4SLinus Torvalds * 175f75d9f3SJason A. Donenfeld * The high level overview is that there is one input pool, into which 18e85c0fc1SJason A. Donenfeld * various pieces of data are hashed. Prior to initialization, some of that 19e85c0fc1SJason A. Donenfeld * data is then "credited" as having a certain number of bits of entropy. 20e85c0fc1SJason A. Donenfeld * When enough bits of entropy are available, the hash is finalized and 21e85c0fc1SJason A. Donenfeld * handed as a key to a stream cipher that expands it indefinitely for 22e85c0fc1SJason A. Donenfeld * various consumers. This key is periodically refreshed as the various 23e85c0fc1SJason A. Donenfeld * entropy collectors, described below, add data to the input pool. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 2612cd53afSYangtao Li #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 2712cd53afSYangtao Li 281da177e4SLinus Torvalds #include <linux/utsname.h> 291da177e4SLinus Torvalds #include <linux/module.h> 301da177e4SLinus Torvalds #include <linux/kernel.h> 311da177e4SLinus Torvalds #include <linux/major.h> 321da177e4SLinus Torvalds #include <linux/string.h> 331da177e4SLinus Torvalds #include <linux/fcntl.h> 341da177e4SLinus Torvalds #include <linux/slab.h> 351da177e4SLinus Torvalds #include <linux/random.h> 361da177e4SLinus Torvalds #include <linux/poll.h> 371da177e4SLinus Torvalds #include <linux/init.h> 381da177e4SLinus Torvalds #include <linux/fs.h> 39322cbb50SChristoph Hellwig #include <linux/blkdev.h> 401da177e4SLinus Torvalds #include <linux/interrupt.h> 4127ac792cSAndrea Righi #include <linux/mm.h> 42dd0f0cf5SMichael Ellerman #include <linux/nodemask.h> 431da177e4SLinus Torvalds #include <linux/spinlock.h> 44c84dbf61STorsten Duwe #include <linux/kthread.h> 451da177e4SLinus Torvalds #include <linux/percpu.h> 46775f4b29STheodore Ts'o #include <linux/ptrace.h> 476265e169STheodore Ts'o #include <linux/workqueue.h> 48d178a1ebSYinghai Lu #include <linux/irq.h> 494e00b339STheodore Ts'o #include <linux/ratelimit.h> 50c6e9d6f3STheodore Ts'o #include <linux/syscalls.h> 51c6e9d6f3STheodore Ts'o #include <linux/completion.h> 528da4b8c4SAndy Shevchenko #include <linux/uuid.h> 5387e7d5abSJason A. Donenfeld #include <linux/uaccess.h> 54b7b67d13SJason A. Donenfeld #include <linux/suspend.h> 55e73aaae2SJason A. Donenfeld #include <linux/siphash.h> 561ca1b917SEric Biggers #include <crypto/chacha.h> 579f9eff85SJason A. Donenfeld #include <crypto/blake2s.h> 581da177e4SLinus Torvalds #include <asm/processor.h> 591da177e4SLinus Torvalds #include <asm/irq.h> 60775f4b29STheodore Ts'o #include <asm/irq_regs.h> 611da177e4SLinus Torvalds #include <asm/io.h> 621da177e4SLinus Torvalds 635f1bb112SJason A. Donenfeld /********************************************************************* 645f1bb112SJason A. Donenfeld * 655f1bb112SJason A. Donenfeld * Initialization and readiness waiting. 665f1bb112SJason A. Donenfeld * 675f1bb112SJason A. Donenfeld * Much of the RNG infrastructure is devoted to various dependencies 685f1bb112SJason A. Donenfeld * being able to wait until the RNG has collected enough entropy and 695f1bb112SJason A. Donenfeld * is ready for safe consumption. 705f1bb112SJason A. Donenfeld * 715f1bb112SJason A. Donenfeld *********************************************************************/ 725f1bb112SJason A. Donenfeld 735f1bb112SJason A. Donenfeld /* 745f1bb112SJason A. Donenfeld * crng_init is protected by base_crng->lock, and only increases 75e3d2c5e7SJason A. Donenfeld * its value (from empty->early->ready). 765f1bb112SJason A. Donenfeld */ 77e3d2c5e7SJason A. Donenfeld static enum { 78e3d2c5e7SJason A. Donenfeld CRNG_EMPTY = 0, /* Little to no entropy collected */ 79e3d2c5e7SJason A. Donenfeld CRNG_EARLY = 1, /* At least POOL_EARLY_BITS collected */ 80e3d2c5e7SJason A. Donenfeld CRNG_READY = 2 /* Fully initialized with POOL_READY_BITS collected */ 81f5bda35fSJason A. Donenfeld } crng_init __read_mostly = CRNG_EMPTY; 82f5bda35fSJason A. Donenfeld static DEFINE_STATIC_KEY_FALSE(crng_is_ready); 83f5bda35fSJason A. Donenfeld #define crng_ready() (static_branch_likely(&crng_is_ready) || crng_init >= CRNG_READY) 84e3d2c5e7SJason A. Donenfeld /* Various types of waiters for crng_init->CRNG_READY transition. */ 855f1bb112SJason A. Donenfeld static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); 865f1bb112SJason A. Donenfeld static struct fasync_struct *fasync; 875f1bb112SJason A. Donenfeld 885f1bb112SJason A. Donenfeld /* Control how we warn userspace. */ 890313bc27SLinus Torvalds static struct ratelimit_state urandom_warning = 90c01d4d0aSJason A. Donenfeld RATELIMIT_STATE_INIT_FLAGS("urandom_warning", HZ, 3, RATELIMIT_MSG_ON_RELEASE); 91cc1e127bSJason A. Donenfeld static int ratelimit_disable __read_mostly = 92cc1e127bSJason A. Donenfeld IS_ENABLED(CONFIG_WARN_ALL_UNSEEDED_RANDOM); 935f1bb112SJason A. Donenfeld module_param_named(ratelimit_disable, ratelimit_disable, int, 0644); 945f1bb112SJason A. Donenfeld MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression"); 955f1bb112SJason A. Donenfeld 965f1bb112SJason A. Donenfeld /* 975f1bb112SJason A. Donenfeld * Returns whether or not the input pool has been seeded and thus guaranteed 980313bc27SLinus Torvalds * to supply cryptographically secure random numbers. This applies to: the 990313bc27SLinus Torvalds * /dev/urandom device, the get_random_bytes function, and the get_random_{u32, 1000313bc27SLinus Torvalds * ,u64,int,long} family of functions. 1015f1bb112SJason A. Donenfeld * 1025f1bb112SJason A. Donenfeld * Returns: true if the input pool has been seeded. 1035f1bb112SJason A. Donenfeld * false if the input pool has not been seeded. 1045f1bb112SJason A. Donenfeld */ 1055f1bb112SJason A. Donenfeld bool rng_is_initialized(void) 1065f1bb112SJason A. Donenfeld { 1075f1bb112SJason A. Donenfeld return crng_ready(); 1085f1bb112SJason A. Donenfeld } 1095f1bb112SJason A. Donenfeld EXPORT_SYMBOL(rng_is_initialized); 1105f1bb112SJason A. Donenfeld 111560181c2SJason A. Donenfeld static void __cold crng_set_ready(struct work_struct *work) 112f5bda35fSJason A. Donenfeld { 113f5bda35fSJason A. Donenfeld static_branch_enable(&crng_is_ready); 114f5bda35fSJason A. Donenfeld } 115f5bda35fSJason A. Donenfeld 1165f1bb112SJason A. Donenfeld /* Used by wait_for_random_bytes(), and considered an entropy collector, below. */ 1175f1bb112SJason A. Donenfeld static void try_to_generate_entropy(void); 1185f1bb112SJason A. Donenfeld 1195f1bb112SJason A. Donenfeld /* 1205f1bb112SJason A. Donenfeld * Wait for the input pool to be seeded and thus guaranteed to supply 1210313bc27SLinus Torvalds * cryptographically secure random numbers. This applies to: the /dev/urandom 1220313bc27SLinus Torvalds * device, the get_random_bytes function, and the get_random_{u32,u64,int,long} 1230313bc27SLinus Torvalds * family of functions. Using any of these functions without first calling 1240313bc27SLinus Torvalds * this function forfeits the guarantee of security. 1255f1bb112SJason A. Donenfeld * 1265f1bb112SJason A. Donenfeld * Returns: 0 if the input pool has been seeded. 1275f1bb112SJason A. Donenfeld * -ERESTARTSYS if the function was interrupted by a signal. 1285f1bb112SJason A. Donenfeld */ 1295f1bb112SJason A. Donenfeld int wait_for_random_bytes(void) 1305f1bb112SJason A. Donenfeld { 131a96cfe2dSJason A. Donenfeld while (!crng_ready()) { 1325f1bb112SJason A. Donenfeld int ret; 1333e504d20SJason A. Donenfeld 1343e504d20SJason A. Donenfeld try_to_generate_entropy(); 1355f1bb112SJason A. Donenfeld ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ); 1365f1bb112SJason A. Donenfeld if (ret) 1375f1bb112SJason A. Donenfeld return ret > 0 ? 0 : ret; 138a96cfe2dSJason A. Donenfeld } 1395f1bb112SJason A. Donenfeld return 0; 1405f1bb112SJason A. Donenfeld } 1415f1bb112SJason A. Donenfeld EXPORT_SYMBOL(wait_for_random_bytes); 1425f1bb112SJason A. Donenfeld 143cc1e127bSJason A. Donenfeld #define warn_unseeded_randomness() \ 144560181c2SJason A. Donenfeld if (IS_ENABLED(CONFIG_WARN_ALL_UNSEEDED_RANDOM) && !crng_ready()) \ 145560181c2SJason A. Donenfeld printk_deferred(KERN_NOTICE "random: %s called from %pS with crng_init=%d\n", \ 146560181c2SJason A. Donenfeld __func__, (void *)_RET_IP_, crng_init) 1475f1bb112SJason A. Donenfeld 1485f1bb112SJason A. Donenfeld 1493655adc7SJason A. Donenfeld /********************************************************************* 1503655adc7SJason A. Donenfeld * 1513655adc7SJason A. Donenfeld * Fast key erasure RNG, the "crng". 1523655adc7SJason A. Donenfeld * 1533655adc7SJason A. Donenfeld * These functions expand entropy from the entropy extractor into 1543655adc7SJason A. Donenfeld * long streams for external consumption using the "fast key erasure" 1553655adc7SJason A. Donenfeld * RNG described at <https://blog.cr.yp.to/20170723-random.html>. 1563655adc7SJason A. Donenfeld * 1573655adc7SJason A. Donenfeld * There are a few exported interfaces for use by other drivers: 1583655adc7SJason A. Donenfeld * 159a1940263SJason A. Donenfeld * void get_random_bytes(void *buf, size_t len) 1603655adc7SJason A. Donenfeld * u32 get_random_u32() 1613655adc7SJason A. Donenfeld * u64 get_random_u64() 1623655adc7SJason A. Donenfeld * unsigned int get_random_int() 1633655adc7SJason A. Donenfeld * unsigned long get_random_long() 1643655adc7SJason A. Donenfeld * 1653655adc7SJason A. Donenfeld * These interfaces will return the requested number of random bytes 1660313bc27SLinus Torvalds * into the given buffer or as a return value. This is equivalent to 167dd7aa36eSJason A. Donenfeld * a read from /dev/urandom. The u32, u64, int, and long family of 168dd7aa36eSJason A. Donenfeld * functions may be higher performance for one-off random integers, 169dd7aa36eSJason A. Donenfeld * because they do a bit of buffering and do not invoke reseeding 170dd7aa36eSJason A. Donenfeld * until the buffer is emptied. 1713655adc7SJason A. Donenfeld * 1723655adc7SJason A. Donenfeld *********************************************************************/ 1733655adc7SJason A. Donenfeld 174e85c0fc1SJason A. Donenfeld enum { 175e85c0fc1SJason A. Donenfeld CRNG_RESEED_START_INTERVAL = HZ, 176e85c0fc1SJason A. Donenfeld CRNG_RESEED_INTERVAL = 60 * HZ 177e85c0fc1SJason A. Donenfeld }; 1783655adc7SJason A. Donenfeld 1793655adc7SJason A. Donenfeld static struct { 1803655adc7SJason A. Donenfeld u8 key[CHACHA_KEY_SIZE] __aligned(__alignof__(long)); 1813655adc7SJason A. Donenfeld unsigned long birth; 1823655adc7SJason A. Donenfeld unsigned long generation; 1833655adc7SJason A. Donenfeld spinlock_t lock; 1843655adc7SJason A. Donenfeld } base_crng = { 1853655adc7SJason A. Donenfeld .lock = __SPIN_LOCK_UNLOCKED(base_crng.lock) 1863655adc7SJason A. Donenfeld }; 1873655adc7SJason A. Donenfeld 1883655adc7SJason A. Donenfeld struct crng { 1893655adc7SJason A. Donenfeld u8 key[CHACHA_KEY_SIZE]; 1903655adc7SJason A. Donenfeld unsigned long generation; 1913655adc7SJason A. Donenfeld local_lock_t lock; 1923655adc7SJason A. Donenfeld }; 1933655adc7SJason A. Donenfeld 1943655adc7SJason A. Donenfeld static DEFINE_PER_CPU(struct crng, crngs) = { 1953655adc7SJason A. Donenfeld .generation = ULONG_MAX, 1963655adc7SJason A. Donenfeld .lock = INIT_LOCAL_LOCK(crngs.lock), 1973655adc7SJason A. Donenfeld }; 1983655adc7SJason A. Donenfeld 199e85c0fc1SJason A. Donenfeld /* Used by crng_reseed() and crng_make_state() to extract a new seed from the input pool. */ 200a1940263SJason A. Donenfeld static void extract_entropy(void *buf, size_t len); 2013655adc7SJason A. Donenfeld 202e85c0fc1SJason A. Donenfeld /* This extracts a new crng key from the input pool. */ 203e85c0fc1SJason A. Donenfeld static void crng_reseed(void) 2043655adc7SJason A. Donenfeld { 2053655adc7SJason A. Donenfeld unsigned long flags; 2063655adc7SJason A. Donenfeld unsigned long next_gen; 2073655adc7SJason A. Donenfeld u8 key[CHACHA_KEY_SIZE]; 2083655adc7SJason A. Donenfeld 209e85c0fc1SJason A. Donenfeld extract_entropy(key, sizeof(key)); 2103655adc7SJason A. Donenfeld 2113655adc7SJason A. Donenfeld /* 2123655adc7SJason A. Donenfeld * We copy the new key into the base_crng, overwriting the old one, 2133655adc7SJason A. Donenfeld * and update the generation counter. We avoid hitting ULONG_MAX, 2143655adc7SJason A. Donenfeld * because the per-cpu crngs are initialized to ULONG_MAX, so this 2153655adc7SJason A. Donenfeld * forces new CPUs that come online to always initialize. 2163655adc7SJason A. Donenfeld */ 2173655adc7SJason A. Donenfeld spin_lock_irqsave(&base_crng.lock, flags); 2183655adc7SJason A. Donenfeld memcpy(base_crng.key, key, sizeof(base_crng.key)); 2193655adc7SJason A. Donenfeld next_gen = base_crng.generation + 1; 2203655adc7SJason A. Donenfeld if (next_gen == ULONG_MAX) 2213655adc7SJason A. Donenfeld ++next_gen; 2223655adc7SJason A. Donenfeld WRITE_ONCE(base_crng.generation, next_gen); 2233655adc7SJason A. Donenfeld WRITE_ONCE(base_crng.birth, jiffies); 224f5bda35fSJason A. Donenfeld if (!static_branch_likely(&crng_is_ready)) 225e3d2c5e7SJason A. Donenfeld crng_init = CRNG_READY; 2263655adc7SJason A. Donenfeld spin_unlock_irqrestore(&base_crng.lock, flags); 2273655adc7SJason A. Donenfeld memzero_explicit(key, sizeof(key)); 2283655adc7SJason A. Donenfeld } 2293655adc7SJason A. Donenfeld 2303655adc7SJason A. Donenfeld /* 2313655adc7SJason A. Donenfeld * This generates a ChaCha block using the provided key, and then 2327f637be4SJason A. Donenfeld * immediately overwrites that key with half the block. It returns 2333655adc7SJason A. Donenfeld * the resultant ChaCha state to the user, along with the second 2343655adc7SJason A. Donenfeld * half of the block containing 32 bytes of random data that may 2353655adc7SJason A. Donenfeld * be used; random_data_len may not be greater than 32. 2368717627dSJason A. Donenfeld * 2378717627dSJason A. Donenfeld * The returned ChaCha state contains within it a copy of the old 2388717627dSJason A. Donenfeld * key value, at index 4, so the state should always be zeroed out 2398717627dSJason A. Donenfeld * immediately after using in order to maintain forward secrecy. 2408717627dSJason A. Donenfeld * If the state cannot be erased in a timely manner, then it is 2418717627dSJason A. Donenfeld * safer to set the random_data parameter to &chacha_state[4] so 2428717627dSJason A. Donenfeld * that this function overwrites it before returning. 2433655adc7SJason A. Donenfeld */ 2443655adc7SJason A. Donenfeld static void crng_fast_key_erasure(u8 key[CHACHA_KEY_SIZE], 2453655adc7SJason A. Donenfeld u32 chacha_state[CHACHA_STATE_WORDS], 2463655adc7SJason A. Donenfeld u8 *random_data, size_t random_data_len) 2473655adc7SJason A. Donenfeld { 2483655adc7SJason A. Donenfeld u8 first_block[CHACHA_BLOCK_SIZE]; 2493655adc7SJason A. Donenfeld 2503655adc7SJason A. Donenfeld BUG_ON(random_data_len > 32); 2513655adc7SJason A. Donenfeld 2523655adc7SJason A. Donenfeld chacha_init_consts(chacha_state); 2533655adc7SJason A. Donenfeld memcpy(&chacha_state[4], key, CHACHA_KEY_SIZE); 2543655adc7SJason A. Donenfeld memset(&chacha_state[12], 0, sizeof(u32) * 4); 2553655adc7SJason A. Donenfeld chacha20_block(chacha_state, first_block); 2563655adc7SJason A. Donenfeld 2573655adc7SJason A. Donenfeld memcpy(key, first_block, CHACHA_KEY_SIZE); 2588717627dSJason A. Donenfeld memcpy(random_data, first_block + CHACHA_KEY_SIZE, random_data_len); 2593655adc7SJason A. Donenfeld memzero_explicit(first_block, sizeof(first_block)); 2603655adc7SJason A. Donenfeld } 2613655adc7SJason A. Donenfeld 2623655adc7SJason A. Donenfeld /* 263745558f9SDominik Brodowski * Return the interval until the next reseeding, which is normally 264745558f9SDominik Brodowski * CRNG_RESEED_INTERVAL, but during early boot, it is at an interval 265e85c0fc1SJason A. Donenfeld * proportional to the uptime. 2667a7ff644SJason A. Donenfeld */ 267745558f9SDominik Brodowski static unsigned int crng_reseed_interval(void) 2687a7ff644SJason A. Donenfeld { 2697a7ff644SJason A. Donenfeld static bool early_boot = true; 2707a7ff644SJason A. Donenfeld 2717a7ff644SJason A. Donenfeld if (unlikely(READ_ONCE(early_boot))) { 2727a7ff644SJason A. Donenfeld time64_t uptime = ktime_get_seconds(); 2737a7ff644SJason A. Donenfeld if (uptime >= CRNG_RESEED_INTERVAL / HZ * 2) 2747a7ff644SJason A. Donenfeld WRITE_ONCE(early_boot, false); 2757a7ff644SJason A. Donenfeld else 276745558f9SDominik Brodowski return max_t(unsigned int, CRNG_RESEED_START_INTERVAL, 2777a7ff644SJason A. Donenfeld (unsigned int)uptime / 2 * HZ); 2787a7ff644SJason A. Donenfeld } 279745558f9SDominik Brodowski return CRNG_RESEED_INTERVAL; 2807a7ff644SJason A. Donenfeld } 2817a7ff644SJason A. Donenfeld 2827a7ff644SJason A. Donenfeld /* 2833655adc7SJason A. Donenfeld * This function returns a ChaCha state that you may use for generating 2843655adc7SJason A. Donenfeld * random data. It also returns up to 32 bytes on its own of random data 2853655adc7SJason A. Donenfeld * that may be used; random_data_len may not be greater than 32. 2863655adc7SJason A. Donenfeld */ 2873655adc7SJason A. Donenfeld static void crng_make_state(u32 chacha_state[CHACHA_STATE_WORDS], 2883655adc7SJason A. Donenfeld u8 *random_data, size_t random_data_len) 2893655adc7SJason A. Donenfeld { 2903655adc7SJason A. Donenfeld unsigned long flags; 2913655adc7SJason A. Donenfeld struct crng *crng; 2923655adc7SJason A. Donenfeld 2933655adc7SJason A. Donenfeld BUG_ON(random_data_len > 32); 2943655adc7SJason A. Donenfeld 2953655adc7SJason A. Donenfeld /* 2963655adc7SJason A. Donenfeld * For the fast path, we check whether we're ready, unlocked first, and 2973655adc7SJason A. Donenfeld * then re-check once locked later. In the case where we're really not 2985c3b747eSJason A. Donenfeld * ready, we do fast key erasure with the base_crng directly, extracting 299e3d2c5e7SJason A. Donenfeld * when crng_init is CRNG_EMPTY. 3003655adc7SJason A. Donenfeld */ 301a96cfe2dSJason A. Donenfeld if (!crng_ready()) { 3023655adc7SJason A. Donenfeld bool ready; 3033655adc7SJason A. Donenfeld 3043655adc7SJason A. Donenfeld spin_lock_irqsave(&base_crng.lock, flags); 3053655adc7SJason A. Donenfeld ready = crng_ready(); 3065c3b747eSJason A. Donenfeld if (!ready) { 307e3d2c5e7SJason A. Donenfeld if (crng_init == CRNG_EMPTY) 3085c3b747eSJason A. Donenfeld extract_entropy(base_crng.key, sizeof(base_crng.key)); 3093655adc7SJason A. Donenfeld crng_fast_key_erasure(base_crng.key, chacha_state, 3103655adc7SJason A. Donenfeld random_data, random_data_len); 3115c3b747eSJason A. Donenfeld } 3123655adc7SJason A. Donenfeld spin_unlock_irqrestore(&base_crng.lock, flags); 3133655adc7SJason A. Donenfeld if (!ready) 3143655adc7SJason A. Donenfeld return; 3153655adc7SJason A. Donenfeld } 3163655adc7SJason A. Donenfeld 3173655adc7SJason A. Donenfeld /* 318e85c0fc1SJason A. Donenfeld * If the base_crng is old enough, we reseed, which in turn bumps the 319e85c0fc1SJason A. Donenfeld * generation counter that we check below. 3203655adc7SJason A. Donenfeld */ 321745558f9SDominik Brodowski if (unlikely(time_is_before_jiffies(READ_ONCE(base_crng.birth) + crng_reseed_interval()))) 322e85c0fc1SJason A. Donenfeld crng_reseed(); 3233655adc7SJason A. Donenfeld 3243655adc7SJason A. Donenfeld local_lock_irqsave(&crngs.lock, flags); 3253655adc7SJason A. Donenfeld crng = raw_cpu_ptr(&crngs); 3263655adc7SJason A. Donenfeld 3273655adc7SJason A. Donenfeld /* 3283655adc7SJason A. Donenfeld * If our per-cpu crng is older than the base_crng, then it means 3293655adc7SJason A. Donenfeld * somebody reseeded the base_crng. In that case, we do fast key 3303655adc7SJason A. Donenfeld * erasure on the base_crng, and use its output as the new key 3313655adc7SJason A. Donenfeld * for our per-cpu crng. This brings us up to date with base_crng. 3323655adc7SJason A. Donenfeld */ 3333655adc7SJason A. Donenfeld if (unlikely(crng->generation != READ_ONCE(base_crng.generation))) { 3343655adc7SJason A. Donenfeld spin_lock(&base_crng.lock); 3353655adc7SJason A. Donenfeld crng_fast_key_erasure(base_crng.key, chacha_state, 3363655adc7SJason A. Donenfeld crng->key, sizeof(crng->key)); 3373655adc7SJason A. Donenfeld crng->generation = base_crng.generation; 3383655adc7SJason A. Donenfeld spin_unlock(&base_crng.lock); 3393655adc7SJason A. Donenfeld } 3403655adc7SJason A. Donenfeld 3413655adc7SJason A. Donenfeld /* 3423655adc7SJason A. Donenfeld * Finally, when we've made it this far, our per-cpu crng has an up 3433655adc7SJason A. Donenfeld * to date key, and we can do fast key erasure with it to produce 3443655adc7SJason A. Donenfeld * some random data and a ChaCha state for the caller. All other 3453655adc7SJason A. Donenfeld * branches of this function are "unlikely", so most of the time we 3463655adc7SJason A. Donenfeld * should wind up here immediately. 3473655adc7SJason A. Donenfeld */ 3483655adc7SJason A. Donenfeld crng_fast_key_erasure(crng->key, chacha_state, random_data, random_data_len); 3493655adc7SJason A. Donenfeld local_unlock_irqrestore(&crngs.lock, flags); 3503655adc7SJason A. Donenfeld } 3513655adc7SJason A. Donenfeld 352a1940263SJason A. Donenfeld static void _get_random_bytes(void *buf, size_t len) 3533655adc7SJason A. Donenfeld { 3543655adc7SJason A. Donenfeld u32 chacha_state[CHACHA_STATE_WORDS]; 3553655adc7SJason A. Donenfeld u8 tmp[CHACHA_BLOCK_SIZE]; 356a1940263SJason A. Donenfeld size_t first_block_len; 3573655adc7SJason A. Donenfeld 358a1940263SJason A. Donenfeld if (!len) 3593655adc7SJason A. Donenfeld return; 3603655adc7SJason A. Donenfeld 361a1940263SJason A. Donenfeld first_block_len = min_t(size_t, 32, len); 362a1940263SJason A. Donenfeld crng_make_state(chacha_state, buf, first_block_len); 363a1940263SJason A. Donenfeld len -= first_block_len; 364a1940263SJason A. Donenfeld buf += first_block_len; 3653655adc7SJason A. Donenfeld 366a1940263SJason A. Donenfeld while (len) { 367a1940263SJason A. Donenfeld if (len < CHACHA_BLOCK_SIZE) { 3683655adc7SJason A. Donenfeld chacha20_block(chacha_state, tmp); 369a1940263SJason A. Donenfeld memcpy(buf, tmp, len); 3703655adc7SJason A. Donenfeld memzero_explicit(tmp, sizeof(tmp)); 3713655adc7SJason A. Donenfeld break; 3723655adc7SJason A. Donenfeld } 3733655adc7SJason A. Donenfeld 3743655adc7SJason A. Donenfeld chacha20_block(chacha_state, buf); 3753655adc7SJason A. Donenfeld if (unlikely(chacha_state[12] == 0)) 3763655adc7SJason A. Donenfeld ++chacha_state[13]; 377a1940263SJason A. Donenfeld len -= CHACHA_BLOCK_SIZE; 3783655adc7SJason A. Donenfeld buf += CHACHA_BLOCK_SIZE; 3793655adc7SJason A. Donenfeld } 3803655adc7SJason A. Donenfeld 3813655adc7SJason A. Donenfeld memzero_explicit(chacha_state, sizeof(chacha_state)); 3823655adc7SJason A. Donenfeld } 3833655adc7SJason A. Donenfeld 3843655adc7SJason A. Donenfeld /* 3853655adc7SJason A. Donenfeld * This function is the exported kernel interface. It returns some 3863655adc7SJason A. Donenfeld * number of good random numbers, suitable for key generation, seeding 387248561adSJason A. Donenfeld * TCP sequence numbers, etc. In order to ensure that the randomness 388248561adSJason A. Donenfeld * by this function is okay, the function wait_for_random_bytes() 389248561adSJason A. Donenfeld * should be called and return 0 at least once at any point prior. 3903655adc7SJason A. Donenfeld */ 391a1940263SJason A. Donenfeld void get_random_bytes(void *buf, size_t len) 3923655adc7SJason A. Donenfeld { 393cc1e127bSJason A. Donenfeld warn_unseeded_randomness(); 394a1940263SJason A. Donenfeld _get_random_bytes(buf, len); 3953655adc7SJason A. Donenfeld } 3963655adc7SJason A. Donenfeld EXPORT_SYMBOL(get_random_bytes); 3973655adc7SJason A. Donenfeld 3981b388e77SJens Axboe static ssize_t get_random_bytes_user(struct iov_iter *iter) 3993655adc7SJason A. Donenfeld { 4003655adc7SJason A. Donenfeld u32 chacha_state[CHACHA_STATE_WORDS]; 4011b388e77SJens Axboe u8 block[CHACHA_BLOCK_SIZE]; 4021b388e77SJens Axboe size_t ret = 0, copied; 4033655adc7SJason A. Donenfeld 4041b388e77SJens Axboe if (unlikely(!iov_iter_count(iter))) 4053655adc7SJason A. Donenfeld return 0; 4063655adc7SJason A. Donenfeld 407aba120ccSJason A. Donenfeld /* 408aba120ccSJason A. Donenfeld * Immediately overwrite the ChaCha key at index 4 with random 40963b8ea5eSJason A. Donenfeld * bytes, in case userspace causes copy_to_iter() below to sleep 410aba120ccSJason A. Donenfeld * forever, so that we still retain forward secrecy in that case. 411aba120ccSJason A. Donenfeld */ 412aba120ccSJason A. Donenfeld crng_make_state(chacha_state, (u8 *)&chacha_state[4], CHACHA_KEY_SIZE); 413aba120ccSJason A. Donenfeld /* 414aba120ccSJason A. Donenfeld * However, if we're doing a read of len <= 32, we don't need to 415aba120ccSJason A. Donenfeld * use chacha_state after, so we can simply return those bytes to 416aba120ccSJason A. Donenfeld * the user directly. 417aba120ccSJason A. Donenfeld */ 4181b388e77SJens Axboe if (iov_iter_count(iter) <= CHACHA_KEY_SIZE) { 4191b388e77SJens Axboe ret = copy_to_iter(&chacha_state[4], CHACHA_KEY_SIZE, iter); 420aba120ccSJason A. Donenfeld goto out_zero_chacha; 421aba120ccSJason A. Donenfeld } 4223655adc7SJason A. Donenfeld 4235209aed5SJason A. Donenfeld for (;;) { 4241b388e77SJens Axboe chacha20_block(chacha_state, block); 4253655adc7SJason A. Donenfeld if (unlikely(chacha_state[12] == 0)) 4263655adc7SJason A. Donenfeld ++chacha_state[13]; 4273655adc7SJason A. Donenfeld 4281b388e77SJens Axboe copied = copy_to_iter(block, sizeof(block), iter); 4291b388e77SJens Axboe ret += copied; 4301b388e77SJens Axboe if (!iov_iter_count(iter) || copied != sizeof(block)) 4315209aed5SJason A. Donenfeld break; 432e3c1c4fdSJason A. Donenfeld 4331b388e77SJens Axboe BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0); 4345209aed5SJason A. Donenfeld if (ret % PAGE_SIZE == 0) { 435e3c1c4fdSJason A. Donenfeld if (signal_pending(current)) 436e3c1c4fdSJason A. Donenfeld break; 437e3c1c4fdSJason A. Donenfeld cond_resched(); 438e3c1c4fdSJason A. Donenfeld } 4395209aed5SJason A. Donenfeld } 4403655adc7SJason A. Donenfeld 4411b388e77SJens Axboe memzero_explicit(block, sizeof(block)); 442aba120ccSJason A. Donenfeld out_zero_chacha: 443aba120ccSJason A. Donenfeld memzero_explicit(chacha_state, sizeof(chacha_state)); 4445209aed5SJason A. Donenfeld return ret ? ret : -EFAULT; 4453655adc7SJason A. Donenfeld } 4463655adc7SJason A. Donenfeld 4473655adc7SJason A. Donenfeld /* 4483655adc7SJason A. Donenfeld * Batched entropy returns random integers. The quality of the random 4493655adc7SJason A. Donenfeld * number is good as /dev/urandom. In order to ensure that the randomness 4503655adc7SJason A. Donenfeld * provided by this function is okay, the function wait_for_random_bytes() 4513655adc7SJason A. Donenfeld * should be called and return 0 at least once at any point prior. 4523655adc7SJason A. Donenfeld */ 4533655adc7SJason A. Donenfeld 4543092adceSJason A. Donenfeld #define DEFINE_BATCHED_ENTROPY(type) \ 4553092adceSJason A. Donenfeld struct batch_ ##type { \ 4563092adceSJason A. Donenfeld /* \ 4573092adceSJason A. Donenfeld * We make this 1.5x a ChaCha block, so that we get the \ 4583092adceSJason A. Donenfeld * remaining 32 bytes from fast key erasure, plus one full \ 4593092adceSJason A. Donenfeld * block from the detached ChaCha state. We can increase \ 4603092adceSJason A. Donenfeld * the size of this later if needed so long as we keep the \ 4613092adceSJason A. Donenfeld * formula of (integer_blocks + 0.5) * CHACHA_BLOCK_SIZE. \ 4623092adceSJason A. Donenfeld */ \ 4633092adceSJason A. Donenfeld type entropy[CHACHA_BLOCK_SIZE * 3 / (2 * sizeof(type))]; \ 4643092adceSJason A. Donenfeld local_lock_t lock; \ 4653092adceSJason A. Donenfeld unsigned long generation; \ 4663092adceSJason A. Donenfeld unsigned int position; \ 4673092adceSJason A. Donenfeld }; \ 4683092adceSJason A. Donenfeld \ 4693092adceSJason A. Donenfeld static DEFINE_PER_CPU(struct batch_ ##type, batched_entropy_ ##type) = { \ 4703092adceSJason A. Donenfeld .lock = INIT_LOCAL_LOCK(batched_entropy_ ##type.lock), \ 4713092adceSJason A. Donenfeld .position = UINT_MAX \ 4723092adceSJason A. Donenfeld }; \ 4733092adceSJason A. Donenfeld \ 4743092adceSJason A. Donenfeld type get_random_ ##type(void) \ 4753092adceSJason A. Donenfeld { \ 4763092adceSJason A. Donenfeld type ret; \ 4773092adceSJason A. Donenfeld unsigned long flags; \ 4783092adceSJason A. Donenfeld struct batch_ ##type *batch; \ 4793092adceSJason A. Donenfeld unsigned long next_gen; \ 4803092adceSJason A. Donenfeld \ 4813092adceSJason A. Donenfeld warn_unseeded_randomness(); \ 4823092adceSJason A. Donenfeld \ 4833092adceSJason A. Donenfeld if (!crng_ready()) { \ 4843092adceSJason A. Donenfeld _get_random_bytes(&ret, sizeof(ret)); \ 4853092adceSJason A. Donenfeld return ret; \ 4863092adceSJason A. Donenfeld } \ 4873092adceSJason A. Donenfeld \ 4883092adceSJason A. Donenfeld local_lock_irqsave(&batched_entropy_ ##type.lock, flags); \ 4893092adceSJason A. Donenfeld batch = raw_cpu_ptr(&batched_entropy_##type); \ 4903092adceSJason A. Donenfeld \ 4913092adceSJason A. Donenfeld next_gen = READ_ONCE(base_crng.generation); \ 4923092adceSJason A. Donenfeld if (batch->position >= ARRAY_SIZE(batch->entropy) || \ 4933092adceSJason A. Donenfeld next_gen != batch->generation) { \ 4943092adceSJason A. Donenfeld _get_random_bytes(batch->entropy, sizeof(batch->entropy)); \ 4953092adceSJason A. Donenfeld batch->position = 0; \ 4963092adceSJason A. Donenfeld batch->generation = next_gen; \ 4973092adceSJason A. Donenfeld } \ 4983092adceSJason A. Donenfeld \ 4993092adceSJason A. Donenfeld ret = batch->entropy[batch->position]; \ 5003092adceSJason A. Donenfeld batch->entropy[batch->position] = 0; \ 5013092adceSJason A. Donenfeld ++batch->position; \ 5023092adceSJason A. Donenfeld local_unlock_irqrestore(&batched_entropy_ ##type.lock, flags); \ 5033092adceSJason A. Donenfeld return ret; \ 5043092adceSJason A. Donenfeld } \ 5053092adceSJason A. Donenfeld EXPORT_SYMBOL(get_random_ ##type); 5063655adc7SJason A. Donenfeld 5073092adceSJason A. Donenfeld DEFINE_BATCHED_ENTROPY(u64) 5083092adceSJason A. Donenfeld DEFINE_BATCHED_ENTROPY(u32) 5093655adc7SJason A. Donenfeld 5103191dd5aSJason A. Donenfeld #ifdef CONFIG_SMP 5113191dd5aSJason A. Donenfeld /* 5123191dd5aSJason A. Donenfeld * This function is called when the CPU is coming up, with entry 5133191dd5aSJason A. Donenfeld * CPUHP_RANDOM_PREPARE, which comes before CPUHP_WORKQUEUE_PREP. 5143191dd5aSJason A. Donenfeld */ 515560181c2SJason A. Donenfeld int __cold random_prepare_cpu(unsigned int cpu) 5163191dd5aSJason A. Donenfeld { 5173191dd5aSJason A. Donenfeld /* 5183191dd5aSJason A. Donenfeld * When the cpu comes back online, immediately invalidate both 5193191dd5aSJason A. Donenfeld * the per-cpu crng and all batches, so that we serve fresh 5203191dd5aSJason A. Donenfeld * randomness. 5213191dd5aSJason A. Donenfeld */ 5223191dd5aSJason A. Donenfeld per_cpu_ptr(&crngs, cpu)->generation = ULONG_MAX; 5233191dd5aSJason A. Donenfeld per_cpu_ptr(&batched_entropy_u32, cpu)->position = UINT_MAX; 5243191dd5aSJason A. Donenfeld per_cpu_ptr(&batched_entropy_u64, cpu)->position = UINT_MAX; 5253191dd5aSJason A. Donenfeld return 0; 5263191dd5aSJason A. Donenfeld } 5273191dd5aSJason A. Donenfeld #endif 5283191dd5aSJason A. Donenfeld 529a5ed7cb1SJason A. Donenfeld 530a5ed7cb1SJason A. Donenfeld /********************************************************************** 531a5ed7cb1SJason A. Donenfeld * 532a5ed7cb1SJason A. Donenfeld * Entropy accumulation and extraction routines. 533a5ed7cb1SJason A. Donenfeld * 534a5ed7cb1SJason A. Donenfeld * Callers may add entropy via: 535a5ed7cb1SJason A. Donenfeld * 536a1940263SJason A. Donenfeld * static void mix_pool_bytes(const void *buf, size_t len) 537a5ed7cb1SJason A. Donenfeld * 538a5ed7cb1SJason A. Donenfeld * After which, if added entropy should be credited: 539a5ed7cb1SJason A. Donenfeld * 540a1940263SJason A. Donenfeld * static void credit_init_bits(size_t bits) 541a5ed7cb1SJason A. Donenfeld * 542e85c0fc1SJason A. Donenfeld * Finally, extract entropy via: 543a5ed7cb1SJason A. Donenfeld * 544a1940263SJason A. Donenfeld * static void extract_entropy(void *buf, size_t len) 545a5ed7cb1SJason A. Donenfeld * 546a5ed7cb1SJason A. Donenfeld **********************************************************************/ 547a5ed7cb1SJason A. Donenfeld 548c5704490SJason A. Donenfeld enum { 5496e8ec255SJason A. Donenfeld POOL_BITS = BLAKE2S_HASH_SIZE * 8, 550e3d2c5e7SJason A. Donenfeld POOL_READY_BITS = POOL_BITS, /* When crng_init->CRNG_READY */ 551e3d2c5e7SJason A. Donenfeld POOL_EARLY_BITS = POOL_READY_BITS / 2 /* When crng_init->CRNG_EARLY */ 5521da177e4SLinus Torvalds }; 5531da177e4SLinus Torvalds 55490ed1e67SJason A. Donenfeld static struct { 5556e8ec255SJason A. Donenfeld struct blake2s_state hash; 55643358209SMatt Mackall spinlock_t lock; 557e85c0fc1SJason A. Donenfeld unsigned int init_bits; 55890ed1e67SJason A. Donenfeld } input_pool = { 5596e8ec255SJason A. Donenfeld .hash.h = { BLAKE2S_IV0 ^ (0x01010000 | BLAKE2S_HASH_SIZE), 5606e8ec255SJason A. Donenfeld BLAKE2S_IV1, BLAKE2S_IV2, BLAKE2S_IV3, BLAKE2S_IV4, 5616e8ec255SJason A. Donenfeld BLAKE2S_IV5, BLAKE2S_IV6, BLAKE2S_IV7 }, 5626e8ec255SJason A. Donenfeld .hash.outlen = BLAKE2S_HASH_SIZE, 563eece09ecSThomas Gleixner .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), 5641da177e4SLinus Torvalds }; 5651da177e4SLinus Torvalds 566a1940263SJason A. Donenfeld static void _mix_pool_bytes(const void *buf, size_t len) 567a5ed7cb1SJason A. Donenfeld { 568a1940263SJason A. Donenfeld blake2s_update(&input_pool.hash, buf, len); 569a5ed7cb1SJason A. Donenfeld } 57090ed1e67SJason A. Donenfeld 5711da177e4SLinus Torvalds /* 572e85c0fc1SJason A. Donenfeld * This function adds bytes into the input pool. It does not 573e85c0fc1SJason A. Donenfeld * update the initialization bit counter; the caller should call 574e85c0fc1SJason A. Donenfeld * credit_init_bits if this is appropriate. 5751da177e4SLinus Torvalds */ 576a1940263SJason A. Donenfeld static void mix_pool_bytes(const void *buf, size_t len) 5771da177e4SLinus Torvalds { 578902c098aSTheodore Ts'o unsigned long flags; 579902c098aSTheodore Ts'o 58090ed1e67SJason A. Donenfeld spin_lock_irqsave(&input_pool.lock, flags); 581a1940263SJason A. Donenfeld _mix_pool_bytes(buf, len); 58290ed1e67SJason A. Donenfeld spin_unlock_irqrestore(&input_pool.lock, flags); 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 585a5ed7cb1SJason A. Donenfeld /* 586a5ed7cb1SJason A. Donenfeld * This is an HKDF-like construction for using the hashed collected entropy 587a5ed7cb1SJason A. Donenfeld * as a PRF key, that's then expanded block-by-block. 588a5ed7cb1SJason A. Donenfeld */ 589a1940263SJason A. Donenfeld static void extract_entropy(void *buf, size_t len) 590a5ed7cb1SJason A. Donenfeld { 591a5ed7cb1SJason A. Donenfeld unsigned long flags; 592a5ed7cb1SJason A. Donenfeld u8 seed[BLAKE2S_HASH_SIZE], next_key[BLAKE2S_HASH_SIZE]; 593a5ed7cb1SJason A. Donenfeld struct { 594a5ed7cb1SJason A. Donenfeld unsigned long rdseed[32 / sizeof(long)]; 595a5ed7cb1SJason A. Donenfeld size_t counter; 596a5ed7cb1SJason A. Donenfeld } block; 597d349ab99SJason A. Donenfeld size_t i, longs; 598a5ed7cb1SJason A. Donenfeld 599d349ab99SJason A. Donenfeld for (i = 0; i < ARRAY_SIZE(block.rdseed);) { 600d349ab99SJason A. Donenfeld longs = arch_get_random_seed_longs(&block.rdseed[i], ARRAY_SIZE(block.rdseed) - i); 601d349ab99SJason A. Donenfeld if (longs) { 602d349ab99SJason A. Donenfeld i += longs; 603d349ab99SJason A. Donenfeld continue; 604d349ab99SJason A. Donenfeld } 605d349ab99SJason A. Donenfeld longs = arch_get_random_longs(&block.rdseed[i], ARRAY_SIZE(block.rdseed) - i); 606d349ab99SJason A. Donenfeld if (longs) { 607d349ab99SJason A. Donenfeld i += longs; 608d349ab99SJason A. Donenfeld continue; 609d349ab99SJason A. Donenfeld } 610d349ab99SJason A. Donenfeld block.rdseed[i++] = random_get_entropy(); 611a5ed7cb1SJason A. Donenfeld } 612a5ed7cb1SJason A. Donenfeld 613a5ed7cb1SJason A. Donenfeld spin_lock_irqsave(&input_pool.lock, flags); 614a5ed7cb1SJason A. Donenfeld 615a5ed7cb1SJason A. Donenfeld /* seed = HASHPRF(last_key, entropy_input) */ 616a5ed7cb1SJason A. Donenfeld blake2s_final(&input_pool.hash, seed); 617a5ed7cb1SJason A. Donenfeld 618a5ed7cb1SJason A. Donenfeld /* next_key = HASHPRF(seed, RDSEED || 0) */ 619a5ed7cb1SJason A. Donenfeld block.counter = 0; 620a5ed7cb1SJason A. Donenfeld blake2s(next_key, (u8 *)&block, seed, sizeof(next_key), sizeof(block), sizeof(seed)); 621a5ed7cb1SJason A. Donenfeld blake2s_init_key(&input_pool.hash, BLAKE2S_HASH_SIZE, next_key, sizeof(next_key)); 622a5ed7cb1SJason A. Donenfeld 623a5ed7cb1SJason A. Donenfeld spin_unlock_irqrestore(&input_pool.lock, flags); 624a5ed7cb1SJason A. Donenfeld memzero_explicit(next_key, sizeof(next_key)); 625a5ed7cb1SJason A. Donenfeld 626a1940263SJason A. Donenfeld while (len) { 627a1940263SJason A. Donenfeld i = min_t(size_t, len, BLAKE2S_HASH_SIZE); 628a5ed7cb1SJason A. Donenfeld /* output = HASHPRF(seed, RDSEED || ++counter) */ 629a5ed7cb1SJason A. Donenfeld ++block.counter; 630a5ed7cb1SJason A. Donenfeld blake2s(buf, (u8 *)&block, seed, i, sizeof(block), sizeof(seed)); 631a1940263SJason A. Donenfeld len -= i; 632a5ed7cb1SJason A. Donenfeld buf += i; 633a5ed7cb1SJason A. Donenfeld } 634a5ed7cb1SJason A. Donenfeld 635a5ed7cb1SJason A. Donenfeld memzero_explicit(seed, sizeof(seed)); 636a5ed7cb1SJason A. Donenfeld memzero_explicit(&block, sizeof(block)); 637a5ed7cb1SJason A. Donenfeld } 638a5ed7cb1SJason A. Donenfeld 639560181c2SJason A. Donenfeld #define credit_init_bits(bits) if (!crng_ready()) _credit_init_bits(bits) 640560181c2SJason A. Donenfeld 641560181c2SJason A. Donenfeld static void __cold _credit_init_bits(size_t bits) 642a5ed7cb1SJason A. Donenfeld { 643f5bda35fSJason A. Donenfeld static struct execute_work set_ready; 644fed7ef06SJason A. Donenfeld unsigned int new, orig, add; 6455c3b747eSJason A. Donenfeld unsigned long flags; 6465c3b747eSJason A. Donenfeld 647560181c2SJason A. Donenfeld if (!bits) 6485c3b747eSJason A. Donenfeld return; 6495c3b747eSJason A. Donenfeld 650a1940263SJason A. Donenfeld add = min_t(size_t, bits, POOL_BITS); 6515c3b747eSJason A. Donenfeld 652e85c0fc1SJason A. Donenfeld orig = READ_ONCE(input_pool.init_bits); 653b7a68f67SUros Bizjak do { 654fed7ef06SJason A. Donenfeld new = min_t(unsigned int, POOL_BITS, orig + add); 655b7a68f67SUros Bizjak } while (!try_cmpxchg(&input_pool.init_bits, &orig, new)); 6565c3b747eSJason A. Donenfeld 65768c9c8b1SJason A. Donenfeld if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) { 65868c9c8b1SJason A. Donenfeld crng_reseed(); /* Sets crng_init to CRNG_READY under base_crng.lock. */ 65960e5b288SJason A. Donenfeld if (static_key_initialized) 660f5bda35fSJason A. Donenfeld execute_in_process_context(crng_set_ready, &set_ready); 66168c9c8b1SJason A. Donenfeld wake_up_interruptible(&crng_init_wait); 66268c9c8b1SJason A. Donenfeld kill_fasync(&fasync, SIGIO, POLL_IN); 66368c9c8b1SJason A. Donenfeld pr_notice("crng init done\n"); 664cc1e127bSJason A. Donenfeld if (urandom_warning.missed) 66568c9c8b1SJason A. Donenfeld pr_notice("%d urandom warning(s) missed due to ratelimiting\n", 66668c9c8b1SJason A. Donenfeld urandom_warning.missed); 66768c9c8b1SJason A. Donenfeld } else if (orig < POOL_EARLY_BITS && new >= POOL_EARLY_BITS) { 6685c3b747eSJason A. Donenfeld spin_lock_irqsave(&base_crng.lock, flags); 66968c9c8b1SJason A. Donenfeld /* Check if crng_init is CRNG_EMPTY, to avoid race with crng_reseed(). */ 670e3d2c5e7SJason A. Donenfeld if (crng_init == CRNG_EMPTY) { 6715c3b747eSJason A. Donenfeld extract_entropy(base_crng.key, sizeof(base_crng.key)); 672e3d2c5e7SJason A. Donenfeld crng_init = CRNG_EARLY; 6735c3b747eSJason A. Donenfeld } 6745c3b747eSJason A. Donenfeld spin_unlock_irqrestore(&base_crng.lock, flags); 6755c3b747eSJason A. Donenfeld } 6765c3b747eSJason A. Donenfeld } 6775c3b747eSJason A. Donenfeld 67892c653cfSJason A. Donenfeld 67992c653cfSJason A. Donenfeld /********************************************************************** 68092c653cfSJason A. Donenfeld * 68192c653cfSJason A. Donenfeld * Entropy collection routines. 68292c653cfSJason A. Donenfeld * 68392c653cfSJason A. Donenfeld * The following exported functions are used for pushing entropy into 68492c653cfSJason A. Donenfeld * the above entropy accumulation routines: 68592c653cfSJason A. Donenfeld * 686a1940263SJason A. Donenfeld * void add_device_randomness(const void *buf, size_t len); 687a1940263SJason A. Donenfeld * void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy); 688a1940263SJason A. Donenfeld * void add_bootloader_randomness(const void *buf, size_t len); 689a1940263SJason A. Donenfeld * void add_vmfork_randomness(const void *unique_vm_id, size_t len); 69092c653cfSJason A. Donenfeld * void add_interrupt_randomness(int irq); 691a1940263SJason A. Donenfeld * void add_input_randomness(unsigned int type, unsigned int code, unsigned int value); 692a4b5c26bSJason A. Donenfeld * void add_disk_randomness(struct gendisk *disk); 69392c653cfSJason A. Donenfeld * 69492c653cfSJason A. Donenfeld * add_device_randomness() adds data to the input pool that 69592c653cfSJason A. Donenfeld * is likely to differ between two devices (or possibly even per boot). 69692c653cfSJason A. Donenfeld * This would be things like MAC addresses or serial numbers, or the 69792c653cfSJason A. Donenfeld * read-out of the RTC. This does *not* credit any actual entropy to 69892c653cfSJason A. Donenfeld * the pool, but it initializes the pool to different values for devices 69992c653cfSJason A. Donenfeld * that might otherwise be identical and have very little entropy 70092c653cfSJason A. Donenfeld * available to them (particularly common in the embedded world). 70192c653cfSJason A. Donenfeld * 70292c653cfSJason A. Donenfeld * add_hwgenerator_randomness() is for true hardware RNGs, and will credit 70392c653cfSJason A. Donenfeld * entropy as specified by the caller. If the entropy pool is full it will 70492c653cfSJason A. Donenfeld * block until more entropy is needed. 70592c653cfSJason A. Donenfeld * 7065c3b747eSJason A. Donenfeld * add_bootloader_randomness() is called by bootloader drivers, such as EFI 7075c3b747eSJason A. Donenfeld * and device tree, and credits its input depending on whether or not the 7085c3b747eSJason A. Donenfeld * configuration option CONFIG_RANDOM_TRUST_BOOTLOADER is set. 70992c653cfSJason A. Donenfeld * 710ae099e8eSJason A. Donenfeld * add_vmfork_randomness() adds a unique (but not necessarily secret) ID 711ae099e8eSJason A. Donenfeld * representing the current instance of a VM to the pool, without crediting, 712ae099e8eSJason A. Donenfeld * and then force-reseeds the crng so that it takes effect immediately. 713ae099e8eSJason A. Donenfeld * 71492c653cfSJason A. Donenfeld * add_interrupt_randomness() uses the interrupt timing as random 71592c653cfSJason A. Donenfeld * inputs to the entropy pool. Using the cycle counters and the irq source 71692c653cfSJason A. Donenfeld * as inputs, it feeds the input pool roughly once a second or after 64 71792c653cfSJason A. Donenfeld * interrupts, crediting 1 bit of entropy for whichever comes first. 71892c653cfSJason A. Donenfeld * 719a4b5c26bSJason A. Donenfeld * add_input_randomness() uses the input layer interrupt timing, as well 720a4b5c26bSJason A. Donenfeld * as the event type information from the hardware. 721a4b5c26bSJason A. Donenfeld * 722a4b5c26bSJason A. Donenfeld * add_disk_randomness() uses what amounts to the seek time of block 723a4b5c26bSJason A. Donenfeld * layer request events, on a per-disk_devt basis, as input to the 724a4b5c26bSJason A. Donenfeld * entropy pool. Note that high-speed solid state drives with very low 725a4b5c26bSJason A. Donenfeld * seek times do not make for good sources of entropy, as their seek 726a4b5c26bSJason A. Donenfeld * times are usually fairly consistent. 727a4b5c26bSJason A. Donenfeld * 728a4b5c26bSJason A. Donenfeld * The last two routines try to estimate how many bits of entropy 729a4b5c26bSJason A. Donenfeld * to credit. They do this by keeping track of the first and second 730a4b5c26bSJason A. Donenfeld * order deltas of the event timings. 731a4b5c26bSJason A. Donenfeld * 73292c653cfSJason A. Donenfeld **********************************************************************/ 73392c653cfSJason A. Donenfeld 73439e0f991SJason A. Donenfeld static bool trust_cpu __initdata = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU); 73539e0f991SJason A. Donenfeld static bool trust_bootloader __initdata = IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER); 73692c653cfSJason A. Donenfeld static int __init parse_trust_cpu(char *arg) 73792c653cfSJason A. Donenfeld { 73892c653cfSJason A. Donenfeld return kstrtobool(arg, &trust_cpu); 73992c653cfSJason A. Donenfeld } 740d97c68d1SJason A. Donenfeld static int __init parse_trust_bootloader(char *arg) 741d97c68d1SJason A. Donenfeld { 742d97c68d1SJason A. Donenfeld return kstrtobool(arg, &trust_bootloader); 743d97c68d1SJason A. Donenfeld } 74492c653cfSJason A. Donenfeld early_param("random.trust_cpu", parse_trust_cpu); 745d97c68d1SJason A. Donenfeld early_param("random.trust_bootloader", parse_trust_bootloader); 746775f4b29STheodore Ts'o 747b7b67d13SJason A. Donenfeld static int random_pm_notification(struct notifier_block *nb, unsigned long action, void *data) 748b7b67d13SJason A. Donenfeld { 749b7b67d13SJason A. Donenfeld unsigned long flags, entropy = random_get_entropy(); 750b7b67d13SJason A. Donenfeld 751b7b67d13SJason A. Donenfeld /* 752b7b67d13SJason A. Donenfeld * Encode a representation of how long the system has been suspended, 753b7b67d13SJason A. Donenfeld * in a way that is distinct from prior system suspends. 754b7b67d13SJason A. Donenfeld */ 755b7b67d13SJason A. Donenfeld ktime_t stamps[] = { ktime_get(), ktime_get_boottime(), ktime_get_real() }; 756b7b67d13SJason A. Donenfeld 757b7b67d13SJason A. Donenfeld spin_lock_irqsave(&input_pool.lock, flags); 758b7b67d13SJason A. Donenfeld _mix_pool_bytes(&action, sizeof(action)); 759b7b67d13SJason A. Donenfeld _mix_pool_bytes(stamps, sizeof(stamps)); 760b7b67d13SJason A. Donenfeld _mix_pool_bytes(&entropy, sizeof(entropy)); 761b7b67d13SJason A. Donenfeld spin_unlock_irqrestore(&input_pool.lock, flags); 762b7b67d13SJason A. Donenfeld 763b7b67d13SJason A. Donenfeld if (crng_ready() && (action == PM_RESTORE_PREPARE || 764261e224dSKalesh Singh (action == PM_POST_SUSPEND && !IS_ENABLED(CONFIG_PM_AUTOSLEEP) && 765261e224dSKalesh Singh !IS_ENABLED(CONFIG_PM_USERSPACE_AUTOSLEEP)))) { 766e85c0fc1SJason A. Donenfeld crng_reseed(); 767b7b67d13SJason A. Donenfeld pr_notice("crng reseeded on system resumption\n"); 768b7b67d13SJason A. Donenfeld } 769b7b67d13SJason A. Donenfeld return 0; 770b7b67d13SJason A. Donenfeld } 771b7b67d13SJason A. Donenfeld 772b7b67d13SJason A. Donenfeld static struct notifier_block pm_notifier = { .notifier_call = random_pm_notification }; 773b7b67d13SJason A. Donenfeld 774775f4b29STheodore Ts'o /* 775*f6238499SJason A. Donenfeld * This is called extremely early, before time keeping functionality is 776*f6238499SJason A. Donenfeld * available, but arch randomness is. Interrupts are not yet enabled. 777775f4b29STheodore Ts'o */ 778*f6238499SJason A. Donenfeld void __init random_init_early(const char *command_line) 779775f4b29STheodore Ts'o { 780d349ab99SJason A. Donenfeld unsigned long entropy[BLAKE2S_BLOCK_SIZE / sizeof(long)]; 781*f6238499SJason A. Donenfeld size_t i, longs, arch_bits; 782775f4b29STheodore Ts'o 7831754abb3SJason A. Donenfeld #if defined(LATENT_ENTROPY_PLUGIN) 7841754abb3SJason A. Donenfeld static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy; 7851754abb3SJason A. Donenfeld _mix_pool_bytes(compiletime_seed, sizeof(compiletime_seed)); 7861754abb3SJason A. Donenfeld #endif 7871754abb3SJason A. Donenfeld 788d349ab99SJason A. Donenfeld for (i = 0, arch_bits = sizeof(entropy) * 8; i < ARRAY_SIZE(entropy);) { 789d349ab99SJason A. Donenfeld longs = arch_get_random_seed_longs(entropy, ARRAY_SIZE(entropy) - i); 790d349ab99SJason A. Donenfeld if (longs) { 791d349ab99SJason A. Donenfeld _mix_pool_bytes(entropy, sizeof(*entropy) * longs); 792d349ab99SJason A. Donenfeld i += longs; 793d349ab99SJason A. Donenfeld continue; 79492c653cfSJason A. Donenfeld } 795d349ab99SJason A. Donenfeld longs = arch_get_random_longs(entropy, ARRAY_SIZE(entropy) - i); 796d349ab99SJason A. Donenfeld if (longs) { 797d349ab99SJason A. Donenfeld _mix_pool_bytes(entropy, sizeof(*entropy) * longs); 798d349ab99SJason A. Donenfeld i += longs; 799d349ab99SJason A. Donenfeld continue; 800d349ab99SJason A. Donenfeld } 801d349ab99SJason A. Donenfeld arch_bits -= sizeof(*entropy) * 8; 802d349ab99SJason A. Donenfeld ++i; 80392c653cfSJason A. Donenfeld } 804*f6238499SJason A. Donenfeld 8052f14062bSJason A. Donenfeld _mix_pool_bytes(command_line, strlen(command_line)); 806*f6238499SJason A. Donenfeld 807*f6238499SJason A. Donenfeld /* Reseed if already seeded by earlier phases. */ 808*f6238499SJason A. Donenfeld if (crng_ready()) 809*f6238499SJason A. Donenfeld crng_reseed(); 810*f6238499SJason A. Donenfeld else if (trust_cpu) 811*f6238499SJason A. Donenfeld _credit_init_bits(arch_bits); 812*f6238499SJason A. Donenfeld } 813*f6238499SJason A. Donenfeld 814*f6238499SJason A. Donenfeld /* 815*f6238499SJason A. Donenfeld * This is called a little bit after the prior function, and now there is 816*f6238499SJason A. Donenfeld * access to timestamps counters. Interrupts are not yet enabled. 817*f6238499SJason A. Donenfeld */ 818*f6238499SJason A. Donenfeld void __init random_init(void) 819*f6238499SJason A. Donenfeld { 820*f6238499SJason A. Donenfeld unsigned long entropy = random_get_entropy(); 821*f6238499SJason A. Donenfeld ktime_t now = ktime_get_real(); 822*f6238499SJason A. Donenfeld 823*f6238499SJason A. Donenfeld _mix_pool_bytes(utsname(), sizeof(*(utsname()))); 824*f6238499SJason A. Donenfeld _mix_pool_bytes(&now, sizeof(now)); 825*f6238499SJason A. Donenfeld _mix_pool_bytes(&entropy, sizeof(entropy)); 8262f14062bSJason A. Donenfeld add_latent_entropy(); 827655b2264STheodore Ts'o 82860e5b288SJason A. Donenfeld /* 829*f6238499SJason A. Donenfeld * If we were initialized by the cpu or bootloader before jump labels 830*f6238499SJason A. Donenfeld * are initialized, then we should enable the static branch here, where 83160e5b288SJason A. Donenfeld * it's guaranteed that jump labels have been initialized. 83260e5b288SJason A. Donenfeld */ 83360e5b288SJason A. Donenfeld if (!static_branch_likely(&crng_is_ready) && crng_init >= CRNG_READY) 83460e5b288SJason A. Donenfeld crng_set_ready(NULL); 83560e5b288SJason A. Donenfeld 836*f6238499SJason A. Donenfeld /* Reseed if already seeded by earlier phases. */ 837e85c0fc1SJason A. Donenfeld if (crng_ready()) 838e85c0fc1SJason A. Donenfeld crng_reseed(); 839775f4b29STheodore Ts'o 840b7b67d13SJason A. Donenfeld WARN_ON(register_pm_notifier(&pm_notifier)); 841b7b67d13SJason A. Donenfeld 842*f6238499SJason A. Donenfeld WARN(!entropy, "Missing cycle counter and fallback timer; RNG " 8434b758edaSJason A. Donenfeld "entropy collection will consequently suffer."); 84492c653cfSJason A. Donenfeld } 8451da177e4SLinus Torvalds 846a2080a67SLinus Torvalds /* 847e192be9dSTheodore Ts'o * Add device- or boot-specific data to the input pool to help 848e192be9dSTheodore Ts'o * initialize it. 849a2080a67SLinus Torvalds * 850e192be9dSTheodore Ts'o * None of this adds any entropy; it is meant to avoid the problem of 851e192be9dSTheodore Ts'o * the entropy pool having similar initial state across largely 852e192be9dSTheodore Ts'o * identical devices. 853a2080a67SLinus Torvalds */ 854a1940263SJason A. Donenfeld void add_device_randomness(const void *buf, size_t len) 855a2080a67SLinus Torvalds { 8564b758edaSJason A. Donenfeld unsigned long entropy = random_get_entropy(); 8574b758edaSJason A. Donenfeld unsigned long flags; 858a2080a67SLinus Torvalds 8593ef4cb2dSTheodore Ts'o spin_lock_irqsave(&input_pool.lock, flags); 8604b758edaSJason A. Donenfeld _mix_pool_bytes(&entropy, sizeof(entropy)); 861a1940263SJason A. Donenfeld _mix_pool_bytes(buf, len); 8623ef4cb2dSTheodore Ts'o spin_unlock_irqrestore(&input_pool.lock, flags); 863a2080a67SLinus Torvalds } 864a2080a67SLinus Torvalds EXPORT_SYMBOL(add_device_randomness); 865a2080a67SLinus Torvalds 86692c653cfSJason A. Donenfeld /* 86792c653cfSJason A. Donenfeld * Interface for in-kernel drivers of true hardware RNGs. 86892c653cfSJason A. Donenfeld * Those devices may produce endless random bits and will be throttled 86992c653cfSJason A. Donenfeld * when our pool is full. 87092c653cfSJason A. Donenfeld */ 871a1940263SJason A. Donenfeld void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy) 87292c653cfSJason A. Donenfeld { 873a1940263SJason A. Donenfeld mix_pool_bytes(buf, len); 874e85c0fc1SJason A. Donenfeld credit_init_bits(entropy); 875e85c0fc1SJason A. Donenfeld 876e85c0fc1SJason A. Donenfeld /* 877745558f9SDominik Brodowski * Throttle writing to once every reseed interval, unless we're not yet 878d775335eSJason A. Donenfeld * initialized or no entropy is credited. 879e85c0fc1SJason A. Donenfeld */ 880d775335eSJason A. Donenfeld if (!kthread_should_stop() && (crng_ready() || !entropy)) 881745558f9SDominik Brodowski schedule_timeout_interruptible(crng_reseed_interval()); 88292c653cfSJason A. Donenfeld } 88392c653cfSJason A. Donenfeld EXPORT_SYMBOL_GPL(add_hwgenerator_randomness); 88492c653cfSJason A. Donenfeld 88592c653cfSJason A. Donenfeld /* 8865c3b747eSJason A. Donenfeld * Handle random seed passed by bootloader, and credit it if 8875c3b747eSJason A. Donenfeld * CONFIG_RANDOM_TRUST_BOOTLOADER is set. 88892c653cfSJason A. Donenfeld */ 88939e0f991SJason A. Donenfeld void __init add_bootloader_randomness(const void *buf, size_t len) 89092c653cfSJason A. Donenfeld { 891a1940263SJason A. Donenfeld mix_pool_bytes(buf, len); 892d97c68d1SJason A. Donenfeld if (trust_bootloader) 893a1940263SJason A. Donenfeld credit_init_bits(len * 8); 89492c653cfSJason A. Donenfeld } 89592c653cfSJason A. Donenfeld 896a4107d34SJason A. Donenfeld #if IS_ENABLED(CONFIG_VMGENID) 897f3c2682bSJason A. Donenfeld static BLOCKING_NOTIFIER_HEAD(vmfork_chain); 898f3c2682bSJason A. Donenfeld 899ae099e8eSJason A. Donenfeld /* 900ae099e8eSJason A. Donenfeld * Handle a new unique VM ID, which is unique, not secret, so we 901ae099e8eSJason A. Donenfeld * don't credit it, but we do immediately force a reseed after so 902ae099e8eSJason A. Donenfeld * that it's used by the crng posthaste. 903ae099e8eSJason A. Donenfeld */ 904560181c2SJason A. Donenfeld void __cold add_vmfork_randomness(const void *unique_vm_id, size_t len) 905ae099e8eSJason A. Donenfeld { 906a1940263SJason A. Donenfeld add_device_randomness(unique_vm_id, len); 907ae099e8eSJason A. Donenfeld if (crng_ready()) { 908e85c0fc1SJason A. Donenfeld crng_reseed(); 909ae099e8eSJason A. Donenfeld pr_notice("crng reseeded due to virtual machine fork\n"); 910ae099e8eSJason A. Donenfeld } 911f3c2682bSJason A. Donenfeld blocking_notifier_call_chain(&vmfork_chain, 0, NULL); 912ae099e8eSJason A. Donenfeld } 913a4107d34SJason A. Donenfeld #if IS_MODULE(CONFIG_VMGENID) 914ae099e8eSJason A. Donenfeld EXPORT_SYMBOL_GPL(add_vmfork_randomness); 915a4107d34SJason A. Donenfeld #endif 916f3c2682bSJason A. Donenfeld 917560181c2SJason A. Donenfeld int __cold register_random_vmfork_notifier(struct notifier_block *nb) 918f3c2682bSJason A. Donenfeld { 919f3c2682bSJason A. Donenfeld return blocking_notifier_chain_register(&vmfork_chain, nb); 920f3c2682bSJason A. Donenfeld } 921f3c2682bSJason A. Donenfeld EXPORT_SYMBOL_GPL(register_random_vmfork_notifier); 922f3c2682bSJason A. Donenfeld 923560181c2SJason A. Donenfeld int __cold unregister_random_vmfork_notifier(struct notifier_block *nb) 924f3c2682bSJason A. Donenfeld { 925f3c2682bSJason A. Donenfeld return blocking_notifier_chain_unregister(&vmfork_chain, nb); 926f3c2682bSJason A. Donenfeld } 927f3c2682bSJason A. Donenfeld EXPORT_SYMBOL_GPL(unregister_random_vmfork_notifier); 928a4107d34SJason A. Donenfeld #endif 929ae099e8eSJason A. Donenfeld 93092c653cfSJason A. Donenfeld struct fast_pool { 931f5eab0e2SJason A. Donenfeld unsigned long pool[4]; 93292c653cfSJason A. Donenfeld unsigned long last; 9333191dd5aSJason A. Donenfeld unsigned int count; 934748bc4ddSJason A. Donenfeld struct timer_list mix; 93592c653cfSJason A. Donenfeld }; 93692c653cfSJason A. Donenfeld 937748bc4ddSJason A. Donenfeld static void mix_interrupt_randomness(struct timer_list *work); 938748bc4ddSJason A. Donenfeld 939f5eab0e2SJason A. Donenfeld static DEFINE_PER_CPU(struct fast_pool, irq_randomness) = { 940f5eab0e2SJason A. Donenfeld #ifdef CONFIG_64BIT 941e73aaae2SJason A. Donenfeld #define FASTMIX_PERM SIPHASH_PERMUTATION 942748bc4ddSJason A. Donenfeld .pool = { SIPHASH_CONST_0, SIPHASH_CONST_1, SIPHASH_CONST_2, SIPHASH_CONST_3 }, 943f5eab0e2SJason A. Donenfeld #else 944e73aaae2SJason A. Donenfeld #define FASTMIX_PERM HSIPHASH_PERMUTATION 945748bc4ddSJason A. Donenfeld .pool = { HSIPHASH_CONST_0, HSIPHASH_CONST_1, HSIPHASH_CONST_2, HSIPHASH_CONST_3 }, 946f5eab0e2SJason A. Donenfeld #endif 947748bc4ddSJason A. Donenfeld .mix = __TIMER_INITIALIZER(mix_interrupt_randomness, 0) 948f5eab0e2SJason A. Donenfeld }; 949f5eab0e2SJason A. Donenfeld 95092c653cfSJason A. Donenfeld /* 951f5eab0e2SJason A. Donenfeld * This is [Half]SipHash-1-x, starting from an empty key. Because 952f5eab0e2SJason A. Donenfeld * the key is fixed, it assumes that its inputs are non-malicious, 953f5eab0e2SJason A. Donenfeld * and therefore this has no security on its own. s represents the 9544b758edaSJason A. Donenfeld * four-word SipHash state, while v represents a two-word input. 95592c653cfSJason A. Donenfeld */ 956791332b3SJason A. Donenfeld static void fast_mix(unsigned long s[4], unsigned long v1, unsigned long v2) 95792c653cfSJason A. Donenfeld { 958791332b3SJason A. Donenfeld s[3] ^= v1; 959e73aaae2SJason A. Donenfeld FASTMIX_PERM(s[0], s[1], s[2], s[3]); 960791332b3SJason A. Donenfeld s[0] ^= v1; 961791332b3SJason A. Donenfeld s[3] ^= v2; 962e73aaae2SJason A. Donenfeld FASTMIX_PERM(s[0], s[1], s[2], s[3]); 963791332b3SJason A. Donenfeld s[0] ^= v2; 964f5eab0e2SJason A. Donenfeld } 965775f4b29STheodore Ts'o 9663191dd5aSJason A. Donenfeld #ifdef CONFIG_SMP 9673191dd5aSJason A. Donenfeld /* 9683191dd5aSJason A. Donenfeld * This function is called when the CPU has just come online, with 9693191dd5aSJason A. Donenfeld * entry CPUHP_AP_RANDOM_ONLINE, just after CPUHP_AP_WORKQUEUE_ONLINE. 9703191dd5aSJason A. Donenfeld */ 971560181c2SJason A. Donenfeld int __cold random_online_cpu(unsigned int cpu) 9723191dd5aSJason A. Donenfeld { 9733191dd5aSJason A. Donenfeld /* 9743191dd5aSJason A. Donenfeld * During CPU shutdown and before CPU onlining, add_interrupt_ 9753191dd5aSJason A. Donenfeld * randomness() may schedule mix_interrupt_randomness(), and 9763191dd5aSJason A. Donenfeld * set the MIX_INFLIGHT flag. However, because the worker can 9773191dd5aSJason A. Donenfeld * be scheduled on a different CPU during this period, that 9783191dd5aSJason A. Donenfeld * flag will never be cleared. For that reason, we zero out 9793191dd5aSJason A. Donenfeld * the flag here, which runs just after workqueues are onlined 9803191dd5aSJason A. Donenfeld * for the CPU again. This also has the effect of setting the 9813191dd5aSJason A. Donenfeld * irq randomness count to zero so that new accumulated irqs 9823191dd5aSJason A. Donenfeld * are fresh. 9833191dd5aSJason A. Donenfeld */ 9843191dd5aSJason A. Donenfeld per_cpu_ptr(&irq_randomness, cpu)->count = 0; 9853191dd5aSJason A. Donenfeld return 0; 9863191dd5aSJason A. Donenfeld } 9873191dd5aSJason A. Donenfeld #endif 9883191dd5aSJason A. Donenfeld 989748bc4ddSJason A. Donenfeld static void mix_interrupt_randomness(struct timer_list *work) 99058340f8eSJason A. Donenfeld { 99158340f8eSJason A. Donenfeld struct fast_pool *fast_pool = container_of(work, struct fast_pool, mix); 992f5eab0e2SJason A. Donenfeld /* 9934b758edaSJason A. Donenfeld * The size of the copied stack pool is explicitly 2 longs so that we 9944b758edaSJason A. Donenfeld * only ever ingest half of the siphash output each time, retaining 9954b758edaSJason A. Donenfeld * the other half as the next "key" that carries over. The entropy is 9964b758edaSJason A. Donenfeld * supposed to be sufficiently dispersed between bits so on average 9974b758edaSJason A. Donenfeld * we don't wind up "losing" some. 998f5eab0e2SJason A. Donenfeld */ 9994b758edaSJason A. Donenfeld unsigned long pool[2]; 1000e3e33fc2SJason A. Donenfeld unsigned int count; 100158340f8eSJason A. Donenfeld 100258340f8eSJason A. Donenfeld /* Check to see if we're running on the wrong CPU due to hotplug. */ 100358340f8eSJason A. Donenfeld local_irq_disable(); 100458340f8eSJason A. Donenfeld if (fast_pool != this_cpu_ptr(&irq_randomness)) { 100558340f8eSJason A. Donenfeld local_irq_enable(); 100658340f8eSJason A. Donenfeld return; 100758340f8eSJason A. Donenfeld } 100858340f8eSJason A. Donenfeld 100958340f8eSJason A. Donenfeld /* 101058340f8eSJason A. Donenfeld * Copy the pool to the stack so that the mixer always has a 101158340f8eSJason A. Donenfeld * consistent view, before we reenable irqs again. 101258340f8eSJason A. Donenfeld */ 1013f5eab0e2SJason A. Donenfeld memcpy(pool, fast_pool->pool, sizeof(pool)); 1014e3e33fc2SJason A. Donenfeld count = fast_pool->count; 10153191dd5aSJason A. Donenfeld fast_pool->count = 0; 101658340f8eSJason A. Donenfeld fast_pool->last = jiffies; 101758340f8eSJason A. Donenfeld local_irq_enable(); 101858340f8eSJason A. Donenfeld 101958340f8eSJason A. Donenfeld mix_pool_bytes(pool, sizeof(pool)); 1020e78a802aSJason A. Donenfeld credit_init_bits(clamp_t(unsigned int, (count & U16_MAX) / 64, 1, sizeof(pool) * 8)); 1021c2a7de4fSJason A. Donenfeld 102258340f8eSJason A. Donenfeld memzero_explicit(pool, sizeof(pool)); 102358340f8eSJason A. Donenfeld } 102458340f8eSJason A. Donenfeld 1025703f7066SSebastian Andrzej Siewior void add_interrupt_randomness(int irq) 10261da177e4SLinus Torvalds { 102758340f8eSJason A. Donenfeld enum { MIX_INFLIGHT = 1U << 31 }; 10284b758edaSJason A. Donenfeld unsigned long entropy = random_get_entropy(); 10291b2a1a7eSChristoph Lameter struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness); 1030775f4b29STheodore Ts'o struct pt_regs *regs = get_irq_regs(); 103158340f8eSJason A. Donenfeld unsigned int new_count; 10323060d6feSYinghai Lu 1033791332b3SJason A. Donenfeld fast_mix(fast_pool->pool, entropy, 1034791332b3SJason A. Donenfeld (regs ? instruction_pointer(regs) : _RET_IP_) ^ swab(irq)); 10353191dd5aSJason A. Donenfeld new_count = ++fast_pool->count; 1036775f4b29STheodore Ts'o 103758340f8eSJason A. Donenfeld if (new_count & MIX_INFLIGHT) 10381da177e4SLinus Torvalds return; 1039840f9507STheodore Ts'o 1040534d2eafSJason A. Donenfeld if (new_count < 1024 && !time_is_before_jiffies(fast_pool->last + HZ)) 10411da177e4SLinus Torvalds return; 10421da177e4SLinus Torvalds 10433191dd5aSJason A. Donenfeld fast_pool->count |= MIX_INFLIGHT; 1044748bc4ddSJason A. Donenfeld if (!timer_pending(&fast_pool->mix)) { 1045748bc4ddSJason A. Donenfeld fast_pool->mix.expires = jiffies; 1046748bc4ddSJason A. Donenfeld add_timer_on(&fast_pool->mix, raw_smp_processor_id()); 1047748bc4ddSJason A. Donenfeld } 10481da177e4SLinus Torvalds } 10494b44f2d1SStephan Mueller EXPORT_SYMBOL_GPL(add_interrupt_randomness); 10501da177e4SLinus Torvalds 1051a4b5c26bSJason A. Donenfeld /* There is one of these per entropy source */ 1052a4b5c26bSJason A. Donenfeld struct timer_rand_state { 1053a4b5c26bSJason A. Donenfeld unsigned long last_time; 1054a4b5c26bSJason A. Donenfeld long last_delta, last_delta2; 1055a4b5c26bSJason A. Donenfeld }; 1056a4b5c26bSJason A. Donenfeld 1057a4b5c26bSJason A. Donenfeld /* 1058a4b5c26bSJason A. Donenfeld * This function adds entropy to the entropy "pool" by using timing 1059a4b5c26bSJason A. Donenfeld * delays. It uses the timer_rand_state structure to make an estimate 1060e3e33fc2SJason A. Donenfeld * of how many bits of entropy this call has added to the pool. The 1061e3e33fc2SJason A. Donenfeld * value "num" is also added to the pool; it should somehow describe 1062e3e33fc2SJason A. Donenfeld * the type of event that just happened. 1063a4b5c26bSJason A. Donenfeld */ 1064a4b5c26bSJason A. Donenfeld static void add_timer_randomness(struct timer_rand_state *state, unsigned int num) 1065a4b5c26bSJason A. Donenfeld { 1066a4b5c26bSJason A. Donenfeld unsigned long entropy = random_get_entropy(), now = jiffies, flags; 1067a4b5c26bSJason A. Donenfeld long delta, delta2, delta3; 1068e3e33fc2SJason A. Donenfeld unsigned int bits; 1069a4b5c26bSJason A. Donenfeld 1070e3e33fc2SJason A. Donenfeld /* 1071e3e33fc2SJason A. Donenfeld * If we're in a hard IRQ, add_interrupt_randomness() will be called 1072e3e33fc2SJason A. Donenfeld * sometime after, so mix into the fast pool. 1073e3e33fc2SJason A. Donenfeld */ 1074e3e33fc2SJason A. Donenfeld if (in_hardirq()) { 1075791332b3SJason A. Donenfeld fast_mix(this_cpu_ptr(&irq_randomness)->pool, entropy, num); 1076e3e33fc2SJason A. Donenfeld } else { 1077a4b5c26bSJason A. Donenfeld spin_lock_irqsave(&input_pool.lock, flags); 1078a4b5c26bSJason A. Donenfeld _mix_pool_bytes(&entropy, sizeof(entropy)); 1079a4b5c26bSJason A. Donenfeld _mix_pool_bytes(&num, sizeof(num)); 1080a4b5c26bSJason A. Donenfeld spin_unlock_irqrestore(&input_pool.lock, flags); 1081e3e33fc2SJason A. Donenfeld } 1082a4b5c26bSJason A. Donenfeld 1083a4b5c26bSJason A. Donenfeld if (crng_ready()) 1084a4b5c26bSJason A. Donenfeld return; 1085a4b5c26bSJason A. Donenfeld 1086a4b5c26bSJason A. Donenfeld /* 1087a4b5c26bSJason A. Donenfeld * Calculate number of bits of randomness we probably added. 1088a4b5c26bSJason A. Donenfeld * We take into account the first, second and third-order deltas 1089a4b5c26bSJason A. Donenfeld * in order to make our estimate. 1090a4b5c26bSJason A. Donenfeld */ 1091a4b5c26bSJason A. Donenfeld delta = now - READ_ONCE(state->last_time); 1092a4b5c26bSJason A. Donenfeld WRITE_ONCE(state->last_time, now); 1093a4b5c26bSJason A. Donenfeld 1094a4b5c26bSJason A. Donenfeld delta2 = delta - READ_ONCE(state->last_delta); 1095a4b5c26bSJason A. Donenfeld WRITE_ONCE(state->last_delta, delta); 1096a4b5c26bSJason A. Donenfeld 1097a4b5c26bSJason A. Donenfeld delta3 = delta2 - READ_ONCE(state->last_delta2); 1098a4b5c26bSJason A. Donenfeld WRITE_ONCE(state->last_delta2, delta2); 1099a4b5c26bSJason A. Donenfeld 1100a4b5c26bSJason A. Donenfeld if (delta < 0) 1101a4b5c26bSJason A. Donenfeld delta = -delta; 1102a4b5c26bSJason A. Donenfeld if (delta2 < 0) 1103a4b5c26bSJason A. Donenfeld delta2 = -delta2; 1104a4b5c26bSJason A. Donenfeld if (delta3 < 0) 1105a4b5c26bSJason A. Donenfeld delta3 = -delta3; 1106a4b5c26bSJason A. Donenfeld if (delta > delta2) 1107a4b5c26bSJason A. Donenfeld delta = delta2; 1108a4b5c26bSJason A. Donenfeld if (delta > delta3) 1109a4b5c26bSJason A. Donenfeld delta = delta3; 1110a4b5c26bSJason A. Donenfeld 1111a4b5c26bSJason A. Donenfeld /* 1112e3e33fc2SJason A. Donenfeld * delta is now minimum absolute delta. Round down by 1 bit 1113e3e33fc2SJason A. Donenfeld * on general principles, and limit entropy estimate to 11 bits. 1114a4b5c26bSJason A. Donenfeld */ 1115e3e33fc2SJason A. Donenfeld bits = min(fls(delta >> 1), 11); 1116e3e33fc2SJason A. Donenfeld 1117e3e33fc2SJason A. Donenfeld /* 1118e3e33fc2SJason A. Donenfeld * As mentioned above, if we're in a hard IRQ, add_interrupt_randomness() 1119e3e33fc2SJason A. Donenfeld * will run after this, which uses a different crediting scheme of 1 bit 1120e3e33fc2SJason A. Donenfeld * per every 64 interrupts. In order to let that function do accounting 1121e3e33fc2SJason A. Donenfeld * close to the one in this function, we credit a full 64/64 bit per bit, 1122e3e33fc2SJason A. Donenfeld * and then subtract one to account for the extra one added. 1123e3e33fc2SJason A. Donenfeld */ 1124e3e33fc2SJason A. Donenfeld if (in_hardirq()) 1125e3e33fc2SJason A. Donenfeld this_cpu_ptr(&irq_randomness)->count += max(1u, bits * 64) - 1; 1126e3e33fc2SJason A. Donenfeld else 1127560181c2SJason A. Donenfeld _credit_init_bits(bits); 1128a4b5c26bSJason A. Donenfeld } 1129a4b5c26bSJason A. Donenfeld 1130a1940263SJason A. Donenfeld void add_input_randomness(unsigned int type, unsigned int code, unsigned int value) 1131a4b5c26bSJason A. Donenfeld { 1132a4b5c26bSJason A. Donenfeld static unsigned char last_value; 1133a4b5c26bSJason A. Donenfeld static struct timer_rand_state input_timer_state = { INITIAL_JIFFIES }; 1134a4b5c26bSJason A. Donenfeld 1135a4b5c26bSJason A. Donenfeld /* Ignore autorepeat and the like. */ 1136a4b5c26bSJason A. Donenfeld if (value == last_value) 1137a4b5c26bSJason A. Donenfeld return; 1138a4b5c26bSJason A. Donenfeld 1139a4b5c26bSJason A. Donenfeld last_value = value; 1140a4b5c26bSJason A. Donenfeld add_timer_randomness(&input_timer_state, 1141a4b5c26bSJason A. Donenfeld (type << 4) ^ code ^ (code >> 4) ^ value); 1142a4b5c26bSJason A. Donenfeld } 1143a4b5c26bSJason A. Donenfeld EXPORT_SYMBOL_GPL(add_input_randomness); 1144a4b5c26bSJason A. Donenfeld 1145a4b5c26bSJason A. Donenfeld #ifdef CONFIG_BLOCK 1146a4b5c26bSJason A. Donenfeld void add_disk_randomness(struct gendisk *disk) 1147a4b5c26bSJason A. Donenfeld { 1148a4b5c26bSJason A. Donenfeld if (!disk || !disk->random) 1149a4b5c26bSJason A. Donenfeld return; 1150a4b5c26bSJason A. Donenfeld /* First major is 1, so we get >= 0x200 here. */ 1151a4b5c26bSJason A. Donenfeld add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); 1152a4b5c26bSJason A. Donenfeld } 1153a4b5c26bSJason A. Donenfeld EXPORT_SYMBOL_GPL(add_disk_randomness); 1154a4b5c26bSJason A. Donenfeld 1155560181c2SJason A. Donenfeld void __cold rand_initialize_disk(struct gendisk *disk) 1156a4b5c26bSJason A. Donenfeld { 1157a4b5c26bSJason A. Donenfeld struct timer_rand_state *state; 1158a4b5c26bSJason A. Donenfeld 1159a4b5c26bSJason A. Donenfeld /* 1160a4b5c26bSJason A. Donenfeld * If kzalloc returns null, we just won't use that entropy 1161a4b5c26bSJason A. Donenfeld * source. 1162a4b5c26bSJason A. Donenfeld */ 1163a4b5c26bSJason A. Donenfeld state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL); 1164a4b5c26bSJason A. Donenfeld if (state) { 1165a4b5c26bSJason A. Donenfeld state->last_time = INITIAL_JIFFIES; 1166a4b5c26bSJason A. Donenfeld disk->random = state; 1167a4b5c26bSJason A. Donenfeld } 1168a4b5c26bSJason A. Donenfeld } 1169a4b5c26bSJason A. Donenfeld #endif 1170a4b5c26bSJason A. Donenfeld 117178c768e6SJason A. Donenfeld struct entropy_timer_state { 117278c768e6SJason A. Donenfeld unsigned long entropy; 117378c768e6SJason A. Donenfeld struct timer_list timer; 117478c768e6SJason A. Donenfeld unsigned int samples, samples_per_bit; 117578c768e6SJason A. Donenfeld }; 117678c768e6SJason A. Donenfeld 11771da177e4SLinus Torvalds /* 117850ee7529SLinus Torvalds * Each time the timer fires, we expect that we got an unpredictable 117950ee7529SLinus Torvalds * jump in the cycle counter. Even if the timer is running on another 118050ee7529SLinus Torvalds * CPU, the timer activity will be touching the stack of the CPU that is 118150ee7529SLinus Torvalds * generating entropy.. 118250ee7529SLinus Torvalds * 118350ee7529SLinus Torvalds * Note that we don't re-arm the timer in the timer itself - we are 118450ee7529SLinus Torvalds * happy to be scheduled away, since that just makes the load more 118550ee7529SLinus Torvalds * complex, but we do not want the timer to keep ticking unless the 118650ee7529SLinus Torvalds * entropy loop is running. 118750ee7529SLinus Torvalds * 118850ee7529SLinus Torvalds * So the re-arming always happens in the entropy loop itself. 118950ee7529SLinus Torvalds */ 1190560181c2SJason A. Donenfeld static void __cold entropy_timer(struct timer_list *timer) 119150ee7529SLinus Torvalds { 119278c768e6SJason A. Donenfeld struct entropy_timer_state *state = container_of(timer, struct entropy_timer_state, timer); 119378c768e6SJason A. Donenfeld 119478c768e6SJason A. Donenfeld if (++state->samples == state->samples_per_bit) { 1195e85c0fc1SJason A. Donenfeld credit_init_bits(1); 119678c768e6SJason A. Donenfeld state->samples = 0; 119778c768e6SJason A. Donenfeld } 119850ee7529SLinus Torvalds } 119950ee7529SLinus Torvalds 120050ee7529SLinus Torvalds /* 120150ee7529SLinus Torvalds * If we have an actual cycle counter, see if we can 120250ee7529SLinus Torvalds * generate enough entropy with timing noise 120350ee7529SLinus Torvalds */ 1204560181c2SJason A. Donenfeld static void __cold try_to_generate_entropy(void) 120550ee7529SLinus Torvalds { 1206829d680eSJason A. Donenfeld enum { NUM_TRIAL_SAMPLES = 8192, MAX_SAMPLES_PER_BIT = HZ / 30 }; 120778c768e6SJason A. Donenfeld struct entropy_timer_state stack; 120878c768e6SJason A. Donenfeld unsigned int i, num_different = 0; 120978c768e6SJason A. Donenfeld unsigned long last = random_get_entropy(); 121050ee7529SLinus Torvalds 121178c768e6SJason A. Donenfeld for (i = 0; i < NUM_TRIAL_SAMPLES - 1; ++i) { 12124b758edaSJason A. Donenfeld stack.entropy = random_get_entropy(); 121378c768e6SJason A. Donenfeld if (stack.entropy != last) 121478c768e6SJason A. Donenfeld ++num_different; 121578c768e6SJason A. Donenfeld last = stack.entropy; 121678c768e6SJason A. Donenfeld } 121778c768e6SJason A. Donenfeld stack.samples_per_bit = DIV_ROUND_UP(NUM_TRIAL_SAMPLES, num_different + 1); 121878c768e6SJason A. Donenfeld if (stack.samples_per_bit > MAX_SAMPLES_PER_BIT) 121950ee7529SLinus Torvalds return; 122050ee7529SLinus Torvalds 122178c768e6SJason A. Donenfeld stack.samples = 0; 122250ee7529SLinus Torvalds timer_setup_on_stack(&stack.timer, entropy_timer, 0); 12233e504d20SJason A. Donenfeld while (!crng_ready() && !signal_pending(current)) { 122450ee7529SLinus Torvalds if (!timer_pending(&stack.timer)) 122550ee7529SLinus Torvalds mod_timer(&stack.timer, jiffies + 1); 12264b758edaSJason A. Donenfeld mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); 122750ee7529SLinus Torvalds schedule(); 12284b758edaSJason A. Donenfeld stack.entropy = random_get_entropy(); 122950ee7529SLinus Torvalds } 123050ee7529SLinus Torvalds 123150ee7529SLinus Torvalds del_timer_sync(&stack.timer); 123250ee7529SLinus Torvalds destroy_timer_on_stack(&stack.timer); 12334b758edaSJason A. Donenfeld mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); 123450ee7529SLinus Torvalds } 123550ee7529SLinus Torvalds 1236a6adf8e7SJason A. Donenfeld 1237a6adf8e7SJason A. Donenfeld /********************************************************************** 1238a6adf8e7SJason A. Donenfeld * 1239a6adf8e7SJason A. Donenfeld * Userspace reader/writer interfaces. 1240a6adf8e7SJason A. Donenfeld * 1241a6adf8e7SJason A. Donenfeld * getrandom(2) is the primary modern interface into the RNG and should 1242a6adf8e7SJason A. Donenfeld * be used in preference to anything else. 1243a6adf8e7SJason A. Donenfeld * 12440313bc27SLinus Torvalds * Reading from /dev/random has the same functionality as calling 12450313bc27SLinus Torvalds * getrandom(2) with flags=0. In earlier versions, however, it had 12460313bc27SLinus Torvalds * vastly different semantics and should therefore be avoided, to 12470313bc27SLinus Torvalds * prevent backwards compatibility issues. 12480313bc27SLinus Torvalds * 12490313bc27SLinus Torvalds * Reading from /dev/urandom has the same functionality as calling 12500313bc27SLinus Torvalds * getrandom(2) with flags=GRND_INSECURE. Because it does not block 12510313bc27SLinus Torvalds * waiting for the RNG to be ready, it should not be used. 1252a6adf8e7SJason A. Donenfeld * 1253a6adf8e7SJason A. Donenfeld * Writing to either /dev/random or /dev/urandom adds entropy to 1254a6adf8e7SJason A. Donenfeld * the input pool but does not credit it. 1255a6adf8e7SJason A. Donenfeld * 12560313bc27SLinus Torvalds * Polling on /dev/random indicates when the RNG is initialized, on 12570313bc27SLinus Torvalds * the read side, and when it wants new entropy, on the write side. 1258a6adf8e7SJason A. Donenfeld * 1259a6adf8e7SJason A. Donenfeld * Both /dev/random and /dev/urandom have the same set of ioctls for 1260a6adf8e7SJason A. Donenfeld * adding entropy, getting the entropy count, zeroing the count, and 1261a6adf8e7SJason A. Donenfeld * reseeding the crng. 1262a6adf8e7SJason A. Donenfeld * 1263a6adf8e7SJason A. Donenfeld **********************************************************************/ 1264a6adf8e7SJason A. Donenfeld 1265a1940263SJason A. Donenfeld SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags) 12661da177e4SLinus Torvalds { 12671b388e77SJens Axboe struct iov_iter iter; 12681b388e77SJens Axboe struct iovec iov; 12691b388e77SJens Axboe int ret; 12701b388e77SJens Axboe 1271a6adf8e7SJason A. Donenfeld if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) 1272a6adf8e7SJason A. Donenfeld return -EINVAL; 1273301f0595STheodore Ts'o 1274a6adf8e7SJason A. Donenfeld /* 1275a6adf8e7SJason A. Donenfeld * Requesting insecure and blocking randomness at the same time makes 1276a6adf8e7SJason A. Donenfeld * no sense. 1277a6adf8e7SJason A. Donenfeld */ 1278a6adf8e7SJason A. Donenfeld if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM)) 1279a6adf8e7SJason A. Donenfeld return -EINVAL; 1280c6f1deb1SAndy Lutomirski 1281f5bda35fSJason A. Donenfeld if (!crng_ready() && !(flags & GRND_INSECURE)) { 1282a6adf8e7SJason A. Donenfeld if (flags & GRND_NONBLOCK) 1283a6adf8e7SJason A. Donenfeld return -EAGAIN; 128430c08efeSAndy Lutomirski ret = wait_for_random_bytes(); 1285a6adf8e7SJason A. Donenfeld if (unlikely(ret)) 128630c08efeSAndy Lutomirski return ret; 1287a6adf8e7SJason A. Donenfeld } 12881b388e77SJens Axboe 12891b388e77SJens Axboe ret = import_single_range(READ, ubuf, len, &iov, &iter); 12901b388e77SJens Axboe if (unlikely(ret)) 12911b388e77SJens Axboe return ret; 12921b388e77SJens Axboe return get_random_bytes_user(&iter); 129330c08efeSAndy Lutomirski } 129430c08efeSAndy Lutomirski 1295248045b8SJason A. Donenfeld static __poll_t random_poll(struct file *file, poll_table *wait) 129689b310a2SChristoph Hellwig { 129730c08efeSAndy Lutomirski poll_wait(file, &crng_init_wait, wait); 1298e85c0fc1SJason A. Donenfeld return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM; 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 13011ce6c8d6SJason A. Donenfeld static ssize_t write_pool_user(struct iov_iter *iter) 13027f397dcdSMatt Mackall { 130304ec96b7SJason A. Donenfeld u8 block[BLAKE2S_BLOCK_SIZE]; 130422b0a222SJens Axboe ssize_t ret = 0; 130522b0a222SJens Axboe size_t copied; 13067f397dcdSMatt Mackall 130722b0a222SJens Axboe if (unlikely(!iov_iter_count(iter))) 130822b0a222SJens Axboe return 0; 130922b0a222SJens Axboe 131022b0a222SJens Axboe for (;;) { 131122b0a222SJens Axboe copied = copy_from_iter(block, sizeof(block), iter); 131222b0a222SJens Axboe ret += copied; 131322b0a222SJens Axboe mix_pool_bytes(block, copied); 131422b0a222SJens Axboe if (!iov_iter_count(iter) || copied != sizeof(block)) 131522b0a222SJens Axboe break; 13161ce6c8d6SJason A. Donenfeld 13171ce6c8d6SJason A. Donenfeld BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0); 13181ce6c8d6SJason A. Donenfeld if (ret % PAGE_SIZE == 0) { 13191ce6c8d6SJason A. Donenfeld if (signal_pending(current)) 13201ce6c8d6SJason A. Donenfeld break; 132191f3f1e3SMatt Mackall cond_resched(); 13227f397dcdSMatt Mackall } 13231ce6c8d6SJason A. Donenfeld } 13247f397dcdSMatt Mackall 13257b5164fbSJason A. Donenfeld memzero_explicit(block, sizeof(block)); 132622b0a222SJens Axboe return ret ? ret : -EFAULT; 13277f397dcdSMatt Mackall } 13287f397dcdSMatt Mackall 132922b0a222SJens Axboe static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *iter) 13301da177e4SLinus Torvalds { 13311ce6c8d6SJason A. Donenfeld return write_pool_user(iter); 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13341b388e77SJens Axboe static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *iter) 13350313bc27SLinus Torvalds { 13360313bc27SLinus Torvalds static int maxwarn = 10; 13370313bc27SLinus Torvalds 133848bff105SJason A. Donenfeld /* 133948bff105SJason A. Donenfeld * Opportunistically attempt to initialize the RNG on platforms that 134048bff105SJason A. Donenfeld * have fast cycle counters, but don't (for now) require it to succeed. 134148bff105SJason A. Donenfeld */ 134248bff105SJason A. Donenfeld if (!crng_ready()) 134348bff105SJason A. Donenfeld try_to_generate_entropy(); 134448bff105SJason A. Donenfeld 1345cc1e127bSJason A. Donenfeld if (!crng_ready()) { 1346cc1e127bSJason A. Donenfeld if (!ratelimit_disable && maxwarn <= 0) 1347cc1e127bSJason A. Donenfeld ++urandom_warning.missed; 1348cc1e127bSJason A. Donenfeld else if (ratelimit_disable || __ratelimit(&urandom_warning)) { 1349cc1e127bSJason A. Donenfeld --maxwarn; 13501b388e77SJens Axboe pr_notice("%s: uninitialized urandom read (%zu bytes read)\n", 13511b388e77SJens Axboe current->comm, iov_iter_count(iter)); 13520313bc27SLinus Torvalds } 1353cc1e127bSJason A. Donenfeld } 13540313bc27SLinus Torvalds 13551b388e77SJens Axboe return get_random_bytes_user(iter); 13560313bc27SLinus Torvalds } 13570313bc27SLinus Torvalds 13581b388e77SJens Axboe static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *iter) 1359a6adf8e7SJason A. Donenfeld { 1360a6adf8e7SJason A. Donenfeld int ret; 1361a6adf8e7SJason A. Donenfeld 1362cd4f24aeSJason A. Donenfeld if (!crng_ready() && 1363cd4f24aeSJason A. Donenfeld ((kiocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO)) || 1364cd4f24aeSJason A. Donenfeld (kiocb->ki_filp->f_flags & O_NONBLOCK))) 1365cd4f24aeSJason A. Donenfeld return -EAGAIN; 1366cd4f24aeSJason A. Donenfeld 1367a6adf8e7SJason A. Donenfeld ret = wait_for_random_bytes(); 1368a6adf8e7SJason A. Donenfeld if (ret != 0) 1369a6adf8e7SJason A. Donenfeld return ret; 13701b388e77SJens Axboe return get_random_bytes_user(iter); 1371a6adf8e7SJason A. Donenfeld } 1372a6adf8e7SJason A. Donenfeld 137343ae4860SMatt Mackall static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) 13741da177e4SLinus Torvalds { 13751da177e4SLinus Torvalds int __user *p = (int __user *)arg; 137622b0a222SJens Axboe int ent_count; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds switch (cmd) { 13791da177e4SLinus Torvalds case RNDGETENTCNT: 1380a6adf8e7SJason A. Donenfeld /* Inherently racy, no point locking. */ 1381e85c0fc1SJason A. Donenfeld if (put_user(input_pool.init_bits, p)) 13821da177e4SLinus Torvalds return -EFAULT; 13831da177e4SLinus Torvalds return 0; 13841da177e4SLinus Torvalds case RNDADDTOENTCNT: 13851da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 13861da177e4SLinus Torvalds return -EPERM; 13871da177e4SLinus Torvalds if (get_user(ent_count, p)) 13881da177e4SLinus Torvalds return -EFAULT; 1389a49c010eSJason A. Donenfeld if (ent_count < 0) 1390a49c010eSJason A. Donenfeld return -EINVAL; 1391e85c0fc1SJason A. Donenfeld credit_init_bits(ent_count); 1392a49c010eSJason A. Donenfeld return 0; 139322b0a222SJens Axboe case RNDADDENTROPY: { 139422b0a222SJens Axboe struct iov_iter iter; 139522b0a222SJens Axboe struct iovec iov; 139622b0a222SJens Axboe ssize_t ret; 139722b0a222SJens Axboe int len; 139822b0a222SJens Axboe 13991da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 14001da177e4SLinus Torvalds return -EPERM; 14011da177e4SLinus Torvalds if (get_user(ent_count, p++)) 14021da177e4SLinus Torvalds return -EFAULT; 14031da177e4SLinus Torvalds if (ent_count < 0) 14041da177e4SLinus Torvalds return -EINVAL; 140522b0a222SJens Axboe if (get_user(len, p++)) 14061da177e4SLinus Torvalds return -EFAULT; 140722b0a222SJens Axboe ret = import_single_range(WRITE, p, len, &iov, &iter); 140822b0a222SJens Axboe if (unlikely(ret)) 140922b0a222SJens Axboe return ret; 14101ce6c8d6SJason A. Donenfeld ret = write_pool_user(&iter); 141122b0a222SJens Axboe if (unlikely(ret < 0)) 141222b0a222SJens Axboe return ret; 141322b0a222SJens Axboe /* Since we're crediting, enforce that it was all written into the pool. */ 141422b0a222SJens Axboe if (unlikely(ret != len)) 141522b0a222SJens Axboe return -EFAULT; 1416e85c0fc1SJason A. Donenfeld credit_init_bits(ent_count); 1417a49c010eSJason A. Donenfeld return 0; 141822b0a222SJens Axboe } 14191da177e4SLinus Torvalds case RNDZAPENTCNT: 14201da177e4SLinus Torvalds case RNDCLEARPOOL: 1421e85c0fc1SJason A. Donenfeld /* No longer has any effect. */ 14221da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 14231da177e4SLinus Torvalds return -EPERM; 14241da177e4SLinus Torvalds return 0; 1425d848e5f8STheodore Ts'o case RNDRESEEDCRNG: 1426d848e5f8STheodore Ts'o if (!capable(CAP_SYS_ADMIN)) 1427d848e5f8STheodore Ts'o return -EPERM; 1428a96cfe2dSJason A. Donenfeld if (!crng_ready()) 1429d848e5f8STheodore Ts'o return -ENODATA; 1430e85c0fc1SJason A. Donenfeld crng_reseed(); 1431d848e5f8STheodore Ts'o return 0; 14321da177e4SLinus Torvalds default: 14331da177e4SLinus Torvalds return -EINVAL; 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds 14379a6f70bbSJeff Dike static int random_fasync(int fd, struct file *filp, int on) 14389a6f70bbSJeff Dike { 14399a6f70bbSJeff Dike return fasync_helper(fd, filp, on, &fasync); 14409a6f70bbSJeff Dike } 14419a6f70bbSJeff Dike 14422b8693c0SArjan van de Ven const struct file_operations random_fops = { 14431b388e77SJens Axboe .read_iter = random_read_iter, 144422b0a222SJens Axboe .write_iter = random_write_iter, 1445a11e1d43SLinus Torvalds .poll = random_poll, 144643ae4860SMatt Mackall .unlocked_ioctl = random_ioctl, 1447507e4e2bSArnd Bergmann .compat_ioctl = compat_ptr_ioctl, 14489a6f70bbSJeff Dike .fasync = random_fasync, 14496038f373SArnd Bergmann .llseek = noop_llseek, 145079025e72SJens Axboe .splice_read = generic_file_splice_read, 145179025e72SJens Axboe .splice_write = iter_file_splice_write, 14521da177e4SLinus Torvalds }; 14531da177e4SLinus Torvalds 14540313bc27SLinus Torvalds const struct file_operations urandom_fops = { 14551b388e77SJens Axboe .read_iter = urandom_read_iter, 145622b0a222SJens Axboe .write_iter = random_write_iter, 14570313bc27SLinus Torvalds .unlocked_ioctl = random_ioctl, 14580313bc27SLinus Torvalds .compat_ioctl = compat_ptr_ioctl, 14590313bc27SLinus Torvalds .fasync = random_fasync, 14600313bc27SLinus Torvalds .llseek = noop_llseek, 146179025e72SJens Axboe .splice_read = generic_file_splice_read, 146279025e72SJens Axboe .splice_write = iter_file_splice_write, 14630313bc27SLinus Torvalds }; 14640313bc27SLinus Torvalds 14650deff3c4SJason A. Donenfeld 14661da177e4SLinus Torvalds /******************************************************************** 14671da177e4SLinus Torvalds * 14680deff3c4SJason A. Donenfeld * Sysctl interface. 14690deff3c4SJason A. Donenfeld * 14700deff3c4SJason A. Donenfeld * These are partly unused legacy knobs with dummy values to not break 14710deff3c4SJason A. Donenfeld * userspace and partly still useful things. They are usually accessible 14720deff3c4SJason A. Donenfeld * in /proc/sys/kernel/random/ and are as follows: 14730deff3c4SJason A. Donenfeld * 14740deff3c4SJason A. Donenfeld * - boot_id - a UUID representing the current boot. 14750deff3c4SJason A. Donenfeld * 14760deff3c4SJason A. Donenfeld * - uuid - a random UUID, different each time the file is read. 14770deff3c4SJason A. Donenfeld * 14780deff3c4SJason A. Donenfeld * - poolsize - the number of bits of entropy that the input pool can 14790deff3c4SJason A. Donenfeld * hold, tied to the POOL_BITS constant. 14800deff3c4SJason A. Donenfeld * 14810deff3c4SJason A. Donenfeld * - entropy_avail - the number of bits of entropy currently in the 14820deff3c4SJason A. Donenfeld * input pool. Always <= poolsize. 14830deff3c4SJason A. Donenfeld * 14840deff3c4SJason A. Donenfeld * - write_wakeup_threshold - the amount of entropy in the input pool 14850deff3c4SJason A. Donenfeld * below which write polls to /dev/random will unblock, requesting 1486e3d2c5e7SJason A. Donenfeld * more entropy, tied to the POOL_READY_BITS constant. It is writable 14870deff3c4SJason A. Donenfeld * to avoid breaking old userspaces, but writing to it does not 14880deff3c4SJason A. Donenfeld * change any behavior of the RNG. 14890deff3c4SJason A. Donenfeld * 1490d0efdf35SJason A. Donenfeld * - urandom_min_reseed_secs - fixed to the value CRNG_RESEED_INTERVAL. 14910deff3c4SJason A. Donenfeld * It is writable to avoid breaking old userspaces, but writing 14920deff3c4SJason A. Donenfeld * to it does not change any behavior of the RNG. 14931da177e4SLinus Torvalds * 14941da177e4SLinus Torvalds ********************************************************************/ 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds #include <linux/sysctl.h> 14991da177e4SLinus Torvalds 1500d0efdf35SJason A. Donenfeld static int sysctl_random_min_urandom_seed = CRNG_RESEED_INTERVAL / HZ; 1501e3d2c5e7SJason A. Donenfeld static int sysctl_random_write_wakeup_bits = POOL_READY_BITS; 1502489c7fc4SJason A. Donenfeld static int sysctl_poolsize = POOL_BITS; 150364276a99SJason A. Donenfeld static u8 sysctl_bootid[UUID_SIZE]; 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds /* 1506f22052b2SGreg Price * This function is used to return both the bootid UUID, and random 15071da177e4SLinus Torvalds * UUID. The difference is in whether table->data is NULL; if it is, 15081da177e4SLinus Torvalds * then a new UUID is generated and returned to the user. 15091da177e4SLinus Torvalds */ 1510a1940263SJason A. Donenfeld static int proc_do_uuid(struct ctl_table *table, int write, void *buf, 1511248045b8SJason A. Donenfeld size_t *lenp, loff_t *ppos) 15121da177e4SLinus Torvalds { 151364276a99SJason A. Donenfeld u8 tmp_uuid[UUID_SIZE], *uuid; 151464276a99SJason A. Donenfeld char uuid_string[UUID_STRING_LEN + 1]; 151564276a99SJason A. Donenfeld struct ctl_table fake_table = { 151664276a99SJason A. Donenfeld .data = uuid_string, 151764276a99SJason A. Donenfeld .maxlen = UUID_STRING_LEN 151864276a99SJason A. Donenfeld }; 151964276a99SJason A. Donenfeld 152064276a99SJason A. Donenfeld if (write) 152164276a99SJason A. Donenfeld return -EPERM; 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds uuid = table->data; 15241da177e4SLinus Torvalds if (!uuid) { 15251da177e4SLinus Torvalds uuid = tmp_uuid; 15261da177e4SLinus Torvalds generate_random_uuid(uuid); 152744e4360fSMathieu Desnoyers } else { 152844e4360fSMathieu Desnoyers static DEFINE_SPINLOCK(bootid_spinlock); 152944e4360fSMathieu Desnoyers 153044e4360fSMathieu Desnoyers spin_lock(&bootid_spinlock); 153144e4360fSMathieu Desnoyers if (!uuid[8]) 153244e4360fSMathieu Desnoyers generate_random_uuid(uuid); 153344e4360fSMathieu Desnoyers spin_unlock(&bootid_spinlock); 153444e4360fSMathieu Desnoyers } 15351da177e4SLinus Torvalds 153664276a99SJason A. Donenfeld snprintf(uuid_string, sizeof(uuid_string), "%pU", uuid); 1537a1940263SJason A. Donenfeld return proc_dostring(&fake_table, 0, buf, lenp, ppos); 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 154077553cf8SJason A. Donenfeld /* The same as proc_dointvec, but writes don't change anything. */ 1541a1940263SJason A. Donenfeld static int proc_do_rointvec(struct ctl_table *table, int write, void *buf, 154277553cf8SJason A. Donenfeld size_t *lenp, loff_t *ppos) 154377553cf8SJason A. Donenfeld { 1544a1940263SJason A. Donenfeld return write ? 0 : proc_dointvec(table, 0, buf, lenp, ppos); 154577553cf8SJason A. Donenfeld } 154677553cf8SJason A. Donenfeld 15475475e8f0SXiaoming Ni static struct ctl_table random_table[] = { 15481da177e4SLinus Torvalds { 15491da177e4SLinus Torvalds .procname = "poolsize", 15501da177e4SLinus Torvalds .data = &sysctl_poolsize, 15511da177e4SLinus Torvalds .maxlen = sizeof(int), 15521da177e4SLinus Torvalds .mode = 0444, 15536d456111SEric W. Biederman .proc_handler = proc_dointvec, 15541da177e4SLinus Torvalds }, 15551da177e4SLinus Torvalds { 15561da177e4SLinus Torvalds .procname = "entropy_avail", 1557e85c0fc1SJason A. Donenfeld .data = &input_pool.init_bits, 15581da177e4SLinus Torvalds .maxlen = sizeof(int), 15591da177e4SLinus Torvalds .mode = 0444, 1560c5704490SJason A. Donenfeld .proc_handler = proc_dointvec, 15611da177e4SLinus Torvalds }, 15621da177e4SLinus Torvalds { 15631da177e4SLinus Torvalds .procname = "write_wakeup_threshold", 15640deff3c4SJason A. Donenfeld .data = &sysctl_random_write_wakeup_bits, 15651da177e4SLinus Torvalds .maxlen = sizeof(int), 15661da177e4SLinus Torvalds .mode = 0644, 156777553cf8SJason A. Donenfeld .proc_handler = proc_do_rointvec, 15681da177e4SLinus Torvalds }, 15691da177e4SLinus Torvalds { 1570f5c2742cSTheodore Ts'o .procname = "urandom_min_reseed_secs", 15710deff3c4SJason A. Donenfeld .data = &sysctl_random_min_urandom_seed, 1572f5c2742cSTheodore Ts'o .maxlen = sizeof(int), 1573f5c2742cSTheodore Ts'o .mode = 0644, 157477553cf8SJason A. Donenfeld .proc_handler = proc_do_rointvec, 1575f5c2742cSTheodore Ts'o }, 1576f5c2742cSTheodore Ts'o { 15771da177e4SLinus Torvalds .procname = "boot_id", 15781da177e4SLinus Torvalds .data = &sysctl_bootid, 15791da177e4SLinus Torvalds .mode = 0444, 15806d456111SEric W. Biederman .proc_handler = proc_do_uuid, 15811da177e4SLinus Torvalds }, 15821da177e4SLinus Torvalds { 15831da177e4SLinus Torvalds .procname = "uuid", 15841da177e4SLinus Torvalds .mode = 0444, 15856d456111SEric W. Biederman .proc_handler = proc_do_uuid, 15861da177e4SLinus Torvalds }, 1587894d2491SEric W. Biederman { } 15881da177e4SLinus Torvalds }; 15895475e8f0SXiaoming Ni 15905475e8f0SXiaoming Ni /* 15912f14062bSJason A. Donenfeld * random_init() is called before sysctl_init(), 15922f14062bSJason A. Donenfeld * so we cannot call register_sysctl_init() in random_init() 15935475e8f0SXiaoming Ni */ 15945475e8f0SXiaoming Ni static int __init random_sysctls_init(void) 15955475e8f0SXiaoming Ni { 15965475e8f0SXiaoming Ni register_sysctl_init("kernel/random", random_table); 15975475e8f0SXiaoming Ni return 0; 15985475e8f0SXiaoming Ni } 15995475e8f0SXiaoming Ni device_initcall(random_sysctls_init); 16000deff3c4SJason A. Donenfeld #endif 1601