random: group initialization wait functions
[linux-block.git] / drivers / char / random.c
CommitLineData
a07fdae3 1// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
1da177e4 2/*
9f9eff85 3 * Copyright (C) 2017-2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
9e95ce27 4 * Copyright Matt Mackall <mpm@selenic.com>, 2003, 2004, 2005
1da177e4
LT
5 * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All
6 * rights reserved.
1da177e4
LT
7 */
8
9/*
1da177e4
LT
10 * Exported interfaces ---- output
11 * ===============================
12 *
92e507d2 13 * There are four exported interfaces; two for use within the kernel,
c0a8a61e 14 * and two for use from userspace.
1da177e4 15 *
92e507d2
GS
16 * Exported interfaces ---- userspace output
17 * -----------------------------------------
1da177e4 18 *
92e507d2 19 * The userspace interfaces are two character devices /dev/random and
1da177e4
LT
20 * /dev/urandom. /dev/random is suitable for use when very high
21 * quality randomness is desired (for example, for key generation or
22 * one-time pads), as it will only return a maximum of the number of
23 * bits of randomness (as estimated by the random number generator)
24 * contained in the entropy pool.
25 *
26 * The /dev/urandom device does not have this limit, and will return
27 * as many bytes as are requested. As more and more random bytes are
28 * requested without giving time for the entropy pool to recharge,
29 * this will result in random numbers that are merely cryptographically
30 * strong. For many applications, however, this is acceptable.
31 *
92e507d2
GS
32 * Exported interfaces ---- kernel output
33 * --------------------------------------
34 *
186873c5 35 * The primary kernel interfaces are:
92e507d2 36 *
04ec96b7 37 * void get_random_bytes(void *buf, size_t nbytes);
248045b8
JD
38 * u32 get_random_u32()
39 * u64 get_random_u64()
40 * unsigned int get_random_int()
41 * unsigned long get_random_long()
92e507d2 42 *
186873c5
JD
43 * These interfaces will return the requested number of random bytes
44 * into the given buffer or as a return value. This is equivalent to a
45 * read from /dev/urandom. The get_random_{u32,u64,int,long}() family
46 * of functions may be higher performance for one-off random integers,
47 * because they do a bit of buffering.
92e507d2
GS
48 *
49 * prandom_u32()
50 * -------------
51 *
52 * For even weaker applications, see the pseudorandom generator
53 * prandom_u32(), prandom_max(), and prandom_bytes(). If the random
54 * numbers aren't security-critical at all, these are *far* cheaper.
55 * Useful for self-tests, random error simulation, randomized backoffs,
56 * and any other application where you trust that nobody is trying to
57 * maliciously mess with you by guessing the "random" numbers.
58 *
1da177e4
LT
59 * Exported interfaces ---- input
60 * ==============================
61 *
62 * The current exported interfaces for gathering environmental noise
63 * from the devices are:
64 *
04ec96b7 65 * void add_device_randomness(const void *buf, size_t size);
248045b8 66 * void add_input_randomness(unsigned int type, unsigned int code,
1da177e4 67 * unsigned int value);
703f7066 68 * void add_interrupt_randomness(int irq);
248045b8 69 * void add_disk_randomness(struct gendisk *disk);
04ec96b7 70 * void add_hwgenerator_randomness(const void *buffer, size_t count,
2b6c6e3d 71 * size_t entropy);
04ec96b7 72 * void add_bootloader_randomness(const void *buf, size_t size);
1da177e4 73 *
a2080a67
LT
74 * add_device_randomness() is for adding data to the random pool that
75 * is likely to differ between two devices (or possibly even per boot).
76 * This would be things like MAC addresses or serial numbers, or the
77 * read-out of the RTC. This does *not* add any actual entropy to the
78 * pool, but it initializes the pool to different values for devices
79 * that might otherwise be identical and have very little entropy
80 * available to them (particularly common in the embedded world).
81 *
1da177e4
LT
82 * add_input_randomness() uses the input layer interrupt timing, as well as
83 * the event type information from the hardware.
84 *
775f4b29
TT
85 * add_interrupt_randomness() uses the interrupt timing as random
86 * inputs to the entropy pool. Using the cycle counters and the irq source
87 * as inputs, it feeds the randomness roughly once a second.
442a4fff
JW
88 *
89 * add_disk_randomness() uses what amounts to the seek time of block
90 * layer request events, on a per-disk_devt basis, as input to the
91 * entropy pool. Note that high-speed solid state drives with very low
92 * seek times do not make for good sources of entropy, as their seek
93 * times are usually fairly consistent.
1da177e4
LT
94 *
95 * All of these routines try to estimate how many bits of randomness a
96 * particular randomness source. They do this by keeping track of the
97 * first and second order deltas of the event timings.
98 *
2b6c6e3d
MB
99 * add_hwgenerator_randomness() is for true hardware RNGs, and will credit
100 * entropy as specified by the caller. If the entropy pool is full it will
101 * block until more entropy is needed.
102 *
103 * add_bootloader_randomness() is the same as add_hwgenerator_randomness() or
104 * add_device_randomness(), depending on whether or not the configuration
105 * option CONFIG_RANDOM_TRUST_BOOTLOADER is set.
106 *
1da177e4
LT
107 * Ensuring unpredictability at system startup
108 * ============================================
109 *
110 * When any operating system starts up, it will go through a sequence
111 * of actions that are fairly predictable by an adversary, especially
112 * if the start-up does not involve interaction with a human operator.
113 * This reduces the actual number of bits of unpredictability in the
114 * entropy pool below the value in entropy_count. In order to
115 * counteract this effect, it helps to carry information in the
116 * entropy pool across shut-downs and start-ups. To do this, put the
117 * following lines an appropriate script which is run during the boot
118 * sequence:
119 *
120 * echo "Initializing random number generator..."
121 * random_seed=/var/run/random-seed
122 * # Carry a random seed from start-up to start-up
123 * # Load and then save the whole entropy pool
124 * if [ -f $random_seed ]; then
125 * cat $random_seed >/dev/urandom
126 * else
127 * touch $random_seed
128 * fi
129 * chmod 600 $random_seed
130 * dd if=/dev/urandom of=$random_seed count=1 bs=512
131 *
132 * and the following lines in an appropriate script which is run as
133 * the system is shutdown:
134 *
135 * # Carry a random seed from shut-down to start-up
136 * # Save the whole entropy pool
137 * echo "Saving random seed..."
138 * random_seed=/var/run/random-seed
139 * touch $random_seed
140 * chmod 600 $random_seed
141 * dd if=/dev/urandom of=$random_seed count=1 bs=512
142 *
143 * For example, on most modern systems using the System V init
144 * scripts, such code fragments would be found in
145 * /etc/rc.d/init.d/random. On older Linux systems, the correct script
146 * location might be in /etc/rcb.d/rc.local or /etc/rc.d/rc.0.
147 *
148 * Effectively, these commands cause the contents of the entropy pool
149 * to be saved at shut-down time and reloaded into the entropy pool at
150 * start-up. (The 'dd' in the addition to the bootup script is to
151 * make sure that /etc/random-seed is different for every start-up,
152 * even if the system crashes without executing rc.0.) Even with
153 * complete knowledge of the start-up activities, predicting the state
154 * of the entropy pool requires knowledge of the previous history of
155 * the system.
156 *
157 * Configuring the /dev/random driver under Linux
158 * ==============================================
159 *
160 * The /dev/random driver under Linux uses minor numbers 8 and 9 of
161 * the /dev/mem major number (#1). So if your system does not have
162 * /dev/random and /dev/urandom created already, they can be created
163 * by using the commands:
164 *
248045b8
JD
165 * mknod /dev/random c 1 8
166 * mknod /dev/urandom c 1 9
1da177e4
LT
167 */
168
12cd53af
YL
169#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
170
1da177e4 171#include <linux/utsname.h>
1da177e4
LT
172#include <linux/module.h>
173#include <linux/kernel.h>
174#include <linux/major.h>
175#include <linux/string.h>
176#include <linux/fcntl.h>
177#include <linux/slab.h>
178#include <linux/random.h>
179#include <linux/poll.h>
180#include <linux/init.h>
181#include <linux/fs.h>
182#include <linux/genhd.h>
183#include <linux/interrupt.h>
27ac792c 184#include <linux/mm.h>
dd0f0cf5 185#include <linux/nodemask.h>
1da177e4 186#include <linux/spinlock.h>
c84dbf61 187#include <linux/kthread.h>
1da177e4 188#include <linux/percpu.h>
775f4b29 189#include <linux/ptrace.h>
6265e169 190#include <linux/workqueue.h>
0244ad00 191#include <linux/irq.h>
4e00b339 192#include <linux/ratelimit.h>
c6e9d6f3
TT
193#include <linux/syscalls.h>
194#include <linux/completion.h>
8da4b8c4 195#include <linux/uuid.h>
87e7d5ab 196#include <linux/uaccess.h>
1ca1b917 197#include <crypto/chacha.h>
9f9eff85 198#include <crypto/blake2s.h>
1da177e4 199#include <asm/processor.h>
1da177e4 200#include <asm/irq.h>
775f4b29 201#include <asm/irq_regs.h>
1da177e4
LT
202#include <asm/io.h>
203
5f1bb112
JD
204/*********************************************************************
205 *
206 * Initialization and readiness waiting.
207 *
208 * Much of the RNG infrastructure is devoted to various dependencies
209 * being able to wait until the RNG has collected enough entropy and
210 * is ready for safe consumption.
211 *
212 *********************************************************************/
205a525c 213
e192be9d
TT
214/*
215 * crng_init = 0 --> Uninitialized
216 * 1 --> Initialized
217 * 2 --> Initialized from input_pool
218 *
5f1bb112 219 * crng_init is protected by base_crng->lock, and only increases
e192be9d
TT
220 * its value (from 0->1->2).
221 */
222static int crng_init = 0;
43838a23 223#define crng_ready() (likely(crng_init > 1))
5f1bb112
JD
224/* Various types of waiters for crng_init->2 transition. */
225static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait);
226static struct fasync_struct *fasync;
227static DEFINE_SPINLOCK(random_ready_list_lock);
228static LIST_HEAD(random_ready_list);
e192be9d 229
5f1bb112 230/* Control how we warn userspace. */
4e00b339
TT
231static struct ratelimit_state unseeded_warning =
232 RATELIMIT_STATE_INIT("warn_unseeded_randomness", HZ, 3);
233static struct ratelimit_state urandom_warning =
234 RATELIMIT_STATE_INIT("warn_urandom_randomness", HZ, 3);
4e00b339 235static int ratelimit_disable __read_mostly;
4e00b339
TT
236module_param_named(ratelimit_disable, ratelimit_disable, int, 0644);
237MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression");
238
5f1bb112
JD
239/*
240 * Returns whether or not the input pool has been seeded and thus guaranteed
241 * to supply cryptographically secure random numbers. This applies to: the
242 * /dev/urandom device, the get_random_bytes function, and the get_random_{u32,
243 * ,u64,int,long} family of functions.
244 *
245 * Returns: true if the input pool has been seeded.
246 * false if the input pool has not been seeded.
247 */
248bool rng_is_initialized(void)
249{
250 return crng_ready();
251}
252EXPORT_SYMBOL(rng_is_initialized);
253
254/* Used by wait_for_random_bytes(), and considered an entropy collector, below. */
255static void try_to_generate_entropy(void);
256
257/*
258 * Wait for the input pool to be seeded and thus guaranteed to supply
259 * cryptographically secure random numbers. This applies to: the /dev/urandom
260 * device, the get_random_bytes function, and the get_random_{u32,u64,int,long}
261 * family of functions. Using any of these functions without first calling
262 * this function forfeits the guarantee of security.
263 *
264 * Returns: 0 if the input pool has been seeded.
265 * -ERESTARTSYS if the function was interrupted by a signal.
266 */
267int wait_for_random_bytes(void)
268{
269 if (likely(crng_ready()))
270 return 0;
271
272 do {
273 int ret;
274 ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ);
275 if (ret)
276 return ret > 0 ? 0 : ret;
277
278 try_to_generate_entropy();
279 } while (!crng_ready());
280
281 return 0;
282}
283EXPORT_SYMBOL(wait_for_random_bytes);
284
285/*
286 * Add a callback function that will be invoked when the input
287 * pool is initialised.
288 *
289 * returns: 0 if callback is successfully added
290 * -EALREADY if pool is already initialised (callback not called)
291 * -ENOENT if module for callback is not alive
292 */
293int add_random_ready_callback(struct random_ready_callback *rdy)
294{
295 struct module *owner;
296 unsigned long flags;
297 int err = -EALREADY;
298
299 if (crng_ready())
300 return err;
301
302 owner = rdy->owner;
303 if (!try_module_get(owner))
304 return -ENOENT;
305
306 spin_lock_irqsave(&random_ready_list_lock, flags);
307 if (crng_ready())
308 goto out;
309
310 owner = NULL;
311
312 list_add(&rdy->list, &random_ready_list);
313 err = 0;
314
315out:
316 spin_unlock_irqrestore(&random_ready_list_lock, flags);
317
318 module_put(owner);
319
320 return err;
321}
322EXPORT_SYMBOL(add_random_ready_callback);
323
324/*
325 * Delete a previously registered readiness callback function.
326 */
327void del_random_ready_callback(struct random_ready_callback *rdy)
328{
329 unsigned long flags;
330 struct module *owner = NULL;
331
332 spin_lock_irqsave(&random_ready_list_lock, flags);
333 if (!list_empty(&rdy->list)) {
334 list_del_init(&rdy->list);
335 owner = rdy->owner;
336 }
337 spin_unlock_irqrestore(&random_ready_list_lock, flags);
338
339 module_put(owner);
340}
341EXPORT_SYMBOL(del_random_ready_callback);
342
343static void process_random_ready_list(void)
344{
345 unsigned long flags;
346 struct random_ready_callback *rdy, *tmp;
347
348 spin_lock_irqsave(&random_ready_list_lock, flags);
349 list_for_each_entry_safe(rdy, tmp, &random_ready_list, list) {
350 struct module *owner = rdy->owner;
351
352 list_del_init(&rdy->list);
353 rdy->func(rdy);
354 module_put(owner);
355 }
356 spin_unlock_irqrestore(&random_ready_list_lock, flags);
357}
358
359#define warn_unseeded_randomness(previous) \
360 _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous))
361
362static void _warn_unseeded_randomness(const char *func_name, void *caller, void **previous)
363{
364#ifdef CONFIG_WARN_ALL_UNSEEDED_RANDOM
365 const bool print_once = false;
366#else
367 static bool print_once __read_mostly;
368#endif
369
370 if (print_once || crng_ready() ||
371 (previous && (caller == READ_ONCE(*previous))))
372 return;
373 WRITE_ONCE(*previous, caller);
374#ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM
375 print_once = true;
376#endif
377 if (__ratelimit(&unseeded_warning))
378 printk_deferred(KERN_NOTICE "random: %s called from %pS with crng_init=%d\n",
379 func_name, caller, crng_init);
380}
381
382
383enum {
384 POOL_BITS = BLAKE2S_HASH_SIZE * 8,
385 POOL_MIN_BITS = POOL_BITS /* No point in settling for less. */
386};
387
388/*
389 * Static global variables
390 */
391static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
392
393static int crng_init_cnt = 0;
394
1da177e4
LT
395/**********************************************************************
396 *
397 * OS independent entropy store. Here are the functions which handle
398 * storing entropy in an entropy pool.
399 *
400 **********************************************************************/
401
90ed1e67 402static struct {
6e8ec255 403 struct blake2s_state hash;
43358209 404 spinlock_t lock;
04ec96b7 405 unsigned int entropy_count;
90ed1e67 406} input_pool = {
6e8ec255
JD
407 .hash.h = { BLAKE2S_IV0 ^ (0x01010000 | BLAKE2S_HASH_SIZE),
408 BLAKE2S_IV1, BLAKE2S_IV2, BLAKE2S_IV3, BLAKE2S_IV4,
409 BLAKE2S_IV5, BLAKE2S_IV6, BLAKE2S_IV7 },
410 .hash.outlen = BLAKE2S_HASH_SIZE,
eece09ec 411 .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock),
1da177e4
LT
412};
413
9c07f578 414static void extract_entropy(void *buf, size_t nbytes);
246c03dd 415static bool drain_entropy(void *buf, size_t nbytes);
90ed1e67 416
a9412d51 417static void crng_reseed(void);
90ed1e67 418
1da177e4 419/*
e68e5b66 420 * This function adds bytes into the entropy "pool". It does not
1da177e4 421 * update the entropy estimate. The caller should call
adc782da 422 * credit_entropy_bits if this is appropriate.
1da177e4 423 */
04ec96b7 424static void _mix_pool_bytes(const void *in, size_t nbytes)
1da177e4 425{
6e8ec255 426 blake2s_update(&input_pool.hash, in, nbytes);
1da177e4
LT
427}
428
04ec96b7 429static void mix_pool_bytes(const void *in, size_t nbytes)
1da177e4 430{
902c098a
TT
431 unsigned long flags;
432
90ed1e67
JD
433 spin_lock_irqsave(&input_pool.lock, flags);
434 _mix_pool_bytes(in, nbytes);
435 spin_unlock_irqrestore(&input_pool.lock, flags);
1da177e4
LT
436}
437
775f4b29 438struct fast_pool {
b2f408fe
JD
439 union {
440 u32 pool32[4];
441 u64 pool64[2];
442 };
248045b8
JD
443 unsigned long last;
444 u16 reg_idx;
445 u8 count;
775f4b29
TT
446};
447
448/*
449 * This is a fast mixing routine used by the interrupt randomness
450 * collector. It's hardcoded for an 128 bit pool and assumes that any
451 * locks that might be needed are taken by the caller.
452 */
b2f408fe 453static void fast_mix(u32 pool[4])
775f4b29 454{
b2f408fe
JD
455 u32 a = pool[0], b = pool[1];
456 u32 c = pool[2], d = pool[3];
43759d4f
TT
457
458 a += b; c += d;
19acc77a 459 b = rol32(b, 6); d = rol32(d, 27);
43759d4f
TT
460 d ^= a; b ^= c;
461
462 a += b; c += d;
19acc77a 463 b = rol32(b, 16); d = rol32(d, 14);
43759d4f
TT
464 d ^= a; b ^= c;
465
466 a += b; c += d;
19acc77a 467 b = rol32(b, 6); d = rol32(d, 27);
43759d4f
TT
468 d ^= a; b ^= c;
469
470 a += b; c += d;
19acc77a 471 b = rol32(b, 16); d = rol32(d, 14);
43759d4f
TT
472 d ^= a; b ^= c;
473
b2f408fe
JD
474 pool[0] = a; pool[1] = b;
475 pool[2] = c; pool[3] = d;
775f4b29
TT
476}
477
04ec96b7 478static void credit_entropy_bits(size_t nbits)
1da177e4 479{
04ec96b7 480 unsigned int entropy_count, orig, add;
18263c4e 481
04ec96b7 482 if (!nbits)
adc782da
MM
483 return;
484
04ec96b7 485 add = min_t(size_t, nbits, POOL_BITS);
a49c010e 486
c5704490
JD
487 do {
488 orig = READ_ONCE(input_pool.entropy_count);
04ec96b7 489 entropy_count = min_t(unsigned int, POOL_BITS, orig + add);
c5704490 490 } while (cmpxchg(&input_pool.entropy_count, orig, entropy_count) != orig);
1da177e4 491
c5704490 492 if (crng_init < 2 && entropy_count >= POOL_MIN_BITS)
a9412d51 493 crng_reseed();
1da177e4
LT
494}
495
e192be9d
TT
496/*********************************************************************
497 *
498 * CRNG using CHACHA20
499 *
500 *********************************************************************/
501
186873c5
JD
502enum {
503 CRNG_RESEED_INTERVAL = 300 * HZ,
504 CRNG_INIT_CNT_THRESH = 2 * CHACHA_KEY_SIZE
505};
506
507static struct {
508 u8 key[CHACHA_KEY_SIZE] __aligned(__alignof__(long));
509 unsigned long birth;
510 unsigned long generation;
511 spinlock_t lock;
512} base_crng = {
513 .lock = __SPIN_LOCK_UNLOCKED(base_crng.lock)
514};
515
516struct crng {
517 u8 key[CHACHA_KEY_SIZE];
518 unsigned long generation;
519 local_lock_t lock;
520};
521
522static DEFINE_PER_CPU(struct crng, crngs) = {
523 .generation = ULONG_MAX,
524 .lock = INIT_LOCAL_LOCK(crngs.lock),
525};
e192be9d 526
dc12baac
TT
527/*
528 * crng_fast_load() can be called by code in the interrupt service
73c7733f
JD
529 * path. So we can't afford to dilly-dally. Returns the number of
530 * bytes processed from cp.
dc12baac 531 */
04ec96b7 532static size_t crng_fast_load(const void *cp, size_t len)
e192be9d
TT
533{
534 unsigned long flags;
04ec96b7 535 const u8 *src = (const u8 *)cp;
73c7733f 536 size_t ret = 0;
e192be9d 537
186873c5 538 if (!spin_trylock_irqsave(&base_crng.lock, flags))
e192be9d 539 return 0;
43838a23 540 if (crng_init != 0) {
186873c5 541 spin_unlock_irqrestore(&base_crng.lock, flags);
e192be9d
TT
542 return 0;
543 }
e192be9d 544 while (len > 0 && crng_init_cnt < CRNG_INIT_CNT_THRESH) {
04ec96b7
JD
545 base_crng.key[crng_init_cnt % sizeof(base_crng.key)] ^= *src;
546 src++; crng_init_cnt++; len--; ret++;
e192be9d
TT
547 }
548 if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) {
0791e8b6 549 ++base_crng.generation;
e192be9d 550 crng_init = 1;
e192be9d 551 }
186873c5 552 spin_unlock_irqrestore(&base_crng.lock, flags);
7c2fe2b3
DB
553 if (crng_init == 1)
554 pr_notice("fast init done\n");
73c7733f 555 return ret;
e192be9d
TT
556}
557
dc12baac
TT
558/*
559 * crng_slow_load() is called by add_device_randomness, which has two
560 * attributes. (1) We can't trust the buffer passed to it is
561 * guaranteed to be unpredictable (so it might not have any entropy at
562 * all), and (2) it doesn't have the performance constraints of
563 * crng_fast_load().
564 *
66e4c2b9
JD
565 * So, we simply hash the contents in with the current key. Finally,
566 * we do *not* advance crng_init_cnt since buffer we may get may be
567 * something like a fixed DMI table (for example), which might very
568 * well be unique to the machine, but is otherwise unvarying.
dc12baac 569 */
04ec96b7 570static void crng_slow_load(const void *cp, size_t len)
dc12baac 571{
248045b8 572 unsigned long flags;
66e4c2b9
JD
573 struct blake2s_state hash;
574
575 blake2s_init(&hash, sizeof(base_crng.key));
dc12baac 576
186873c5 577 if (!spin_trylock_irqsave(&base_crng.lock, flags))
66e4c2b9 578 return;
dc12baac 579 if (crng_init != 0) {
186873c5 580 spin_unlock_irqrestore(&base_crng.lock, flags);
66e4c2b9 581 return;
dc12baac 582 }
66e4c2b9
JD
583
584 blake2s_update(&hash, base_crng.key, sizeof(base_crng.key));
585 blake2s_update(&hash, cp, len);
586 blake2s_final(&hash, base_crng.key);
587
186873c5 588 spin_unlock_irqrestore(&base_crng.lock, flags);
dc12baac
TT
589}
590
a9412d51 591static void crng_reseed(void)
e192be9d 592{
248045b8 593 unsigned long flags;
186873c5
JD
594 unsigned long next_gen;
595 u8 key[CHACHA_KEY_SIZE];
7191c628 596 bool finalize_init = false;
e192be9d 597
246c03dd
JD
598 /* Only reseed if we can, to prevent brute forcing a small amount of new bits. */
599 if (!drain_entropy(key, sizeof(key)))
600 return;
a9412d51 601
186873c5
JD
602 /*
603 * We copy the new key into the base_crng, overwriting the old one,
604 * and update the generation counter. We avoid hitting ULONG_MAX,
605 * because the per-cpu crngs are initialized to ULONG_MAX, so this
606 * forces new CPUs that come online to always initialize.
607 */
608 spin_lock_irqsave(&base_crng.lock, flags);
609 memcpy(base_crng.key, key, sizeof(base_crng.key));
610 next_gen = base_crng.generation + 1;
611 if (next_gen == ULONG_MAX)
612 ++next_gen;
613 WRITE_ONCE(base_crng.generation, next_gen);
614 WRITE_ONCE(base_crng.birth, jiffies);
a9412d51 615 if (crng_init < 2) {
a9412d51 616 crng_init = 2;
7191c628
DB
617 finalize_init = true;
618 }
619 spin_unlock_irqrestore(&base_crng.lock, flags);
620 memzero_explicit(key, sizeof(key));
621 if (finalize_init) {
a9412d51
JD
622 process_random_ready_list();
623 wake_up_interruptible(&crng_init_wait);
624 kill_fasync(&fasync, SIGIO, POLL_IN);
625 pr_notice("crng init done\n");
626 if (unseeded_warning.missed) {
627 pr_notice("%d get_random_xx warning(s) missed due to ratelimiting\n",
628 unseeded_warning.missed);
629 unseeded_warning.missed = 0;
630 }
631 if (urandom_warning.missed) {
632 pr_notice("%d urandom warning(s) missed due to ratelimiting\n",
633 urandom_warning.missed);
634 urandom_warning.missed = 0;
635 }
636 }
e192be9d
TT
637}
638
186873c5
JD
639/*
640 * The general form here is based on a "fast key erasure RNG" from
641 * <https://blog.cr.yp.to/20170723-random.html>. It generates a ChaCha
642 * block using the provided key, and then immediately overwites that
643 * key with half the block. It returns the resultant ChaCha state to the
644 * user, along with the second half of the block containing 32 bytes of
645 * random data that may be used; random_data_len may not be greater than
646 * 32.
647 */
648static void crng_fast_key_erasure(u8 key[CHACHA_KEY_SIZE],
649 u32 chacha_state[CHACHA_STATE_WORDS],
650 u8 *random_data, size_t random_data_len)
e192be9d 651{
186873c5 652 u8 first_block[CHACHA_BLOCK_SIZE];
009ba856 653
186873c5
JD
654 BUG_ON(random_data_len > 32);
655
656 chacha_init_consts(chacha_state);
657 memcpy(&chacha_state[4], key, CHACHA_KEY_SIZE);
658 memset(&chacha_state[12], 0, sizeof(u32) * 4);
659 chacha20_block(chacha_state, first_block);
660
661 memcpy(key, first_block, CHACHA_KEY_SIZE);
662 memcpy(random_data, first_block + CHACHA_KEY_SIZE, random_data_len);
663 memzero_explicit(first_block, sizeof(first_block));
1e7f583a
TT
664}
665
c92e040d 666/*
186873c5
JD
667 * This function returns a ChaCha state that you may use for generating
668 * random data. It also returns up to 32 bytes on its own of random data
669 * that may be used; random_data_len may not be greater than 32.
c92e040d 670 */
186873c5
JD
671static void crng_make_state(u32 chacha_state[CHACHA_STATE_WORDS],
672 u8 *random_data, size_t random_data_len)
c92e040d 673{
248045b8 674 unsigned long flags;
186873c5 675 struct crng *crng;
c92e040d 676
186873c5
JD
677 BUG_ON(random_data_len > 32);
678
679 /*
680 * For the fast path, we check whether we're ready, unlocked first, and
681 * then re-check once locked later. In the case where we're really not
682 * ready, we do fast key erasure with the base_crng directly, because
683 * this is what crng_{fast,slow}_load mutate during early init.
684 */
685 if (unlikely(!crng_ready())) {
686 bool ready;
687
688 spin_lock_irqsave(&base_crng.lock, flags);
689 ready = crng_ready();
690 if (!ready)
691 crng_fast_key_erasure(base_crng.key, chacha_state,
692 random_data, random_data_len);
693 spin_unlock_irqrestore(&base_crng.lock, flags);
694 if (!ready)
695 return;
c92e040d 696 }
186873c5
JD
697
698 /*
699 * If the base_crng is more than 5 minutes old, we reseed, which
700 * in turn bumps the generation counter that we check below.
701 */
702 if (unlikely(time_after(jiffies, READ_ONCE(base_crng.birth) + CRNG_RESEED_INTERVAL)))
703 crng_reseed();
704
705 local_lock_irqsave(&crngs.lock, flags);
706 crng = raw_cpu_ptr(&crngs);
707
708 /*
709 * If our per-cpu crng is older than the base_crng, then it means
710 * somebody reseeded the base_crng. In that case, we do fast key
711 * erasure on the base_crng, and use its output as the new key
712 * for our per-cpu crng. This brings us up to date with base_crng.
713 */
714 if (unlikely(crng->generation != READ_ONCE(base_crng.generation))) {
715 spin_lock(&base_crng.lock);
716 crng_fast_key_erasure(base_crng.key, chacha_state,
717 crng->key, sizeof(crng->key));
718 crng->generation = base_crng.generation;
719 spin_unlock(&base_crng.lock);
720 }
721
722 /*
723 * Finally, when we've made it this far, our per-cpu crng has an up
724 * to date key, and we can do fast key erasure with it to produce
725 * some random data and a ChaCha state for the caller. All other
726 * branches of this function are "unlikely", so most of the time we
727 * should wind up here immediately.
728 */
729 crng_fast_key_erasure(crng->key, chacha_state, random_data, random_data_len);
730 local_unlock_irqrestore(&crngs.lock, flags);
c92e040d
TT
731}
732
186873c5 733static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes)
e192be9d 734{
186873c5 735 bool large_request = nbytes > 256;
04ec96b7
JD
736 ssize_t ret = 0;
737 size_t len;
186873c5
JD
738 u32 chacha_state[CHACHA_STATE_WORDS];
739 u8 output[CHACHA_BLOCK_SIZE];
740
741 if (!nbytes)
742 return 0;
743
04ec96b7 744 len = min_t(size_t, 32, nbytes);
186873c5
JD
745 crng_make_state(chacha_state, output, len);
746
747 if (copy_to_user(buf, output, len))
748 return -EFAULT;
749 nbytes -= len;
750 buf += len;
751 ret += len;
e192be9d
TT
752
753 while (nbytes) {
754 if (large_request && need_resched()) {
186873c5 755 if (signal_pending(current))
e192be9d 756 break;
e192be9d
TT
757 schedule();
758 }
759
186873c5
JD
760 chacha20_block(chacha_state, output);
761 if (unlikely(chacha_state[12] == 0))
762 ++chacha_state[13];
763
04ec96b7 764 len = min_t(size_t, nbytes, CHACHA_BLOCK_SIZE);
186873c5 765 if (copy_to_user(buf, output, len)) {
e192be9d
TT
766 ret = -EFAULT;
767 break;
768 }
769
186873c5
JD
770 nbytes -= len;
771 buf += len;
772 ret += len;
e192be9d 773 }
e192be9d 774
186873c5
JD
775 memzero_explicit(chacha_state, sizeof(chacha_state));
776 memzero_explicit(output, sizeof(output));
e192be9d
TT
777 return ret;
778}
779
1da177e4
LT
780/*********************************************************************
781 *
782 * Entropy input management
783 *
784 *********************************************************************/
785
786/* There is one of these per entropy source */
787struct timer_rand_state {
788 cycles_t last_time;
90b75ee5 789 long last_delta, last_delta2;
1da177e4
LT
790};
791
644008df
TT
792#define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, };
793
a2080a67 794/*
e192be9d
TT
795 * Add device- or boot-specific data to the input pool to help
796 * initialize it.
a2080a67 797 *
e192be9d
TT
798 * None of this adds any entropy; it is meant to avoid the problem of
799 * the entropy pool having similar initial state across largely
800 * identical devices.
a2080a67 801 */
04ec96b7 802void add_device_randomness(const void *buf, size_t size)
a2080a67 803{
61875f30 804 unsigned long time = random_get_entropy() ^ jiffies;
3ef4cb2d 805 unsigned long flags;
a2080a67 806
dc12baac
TT
807 if (!crng_ready() && size)
808 crng_slow_load(buf, size);
ee7998c5 809
3ef4cb2d 810 spin_lock_irqsave(&input_pool.lock, flags);
90ed1e67
JD
811 _mix_pool_bytes(buf, size);
812 _mix_pool_bytes(&time, sizeof(time));
3ef4cb2d 813 spin_unlock_irqrestore(&input_pool.lock, flags);
a2080a67
LT
814}
815EXPORT_SYMBOL(add_device_randomness);
816
644008df 817static struct timer_rand_state input_timer_state = INIT_TIMER_RAND_STATE;
3060d6fe 818
1da177e4
LT
819/*
820 * This function adds entropy to the entropy "pool" by using timing
821 * delays. It uses the timer_rand_state structure to make an estimate
822 * of how many bits of entropy this call has added to the pool.
823 *
824 * The number "num" is also added to the pool - it should somehow describe
825 * the type of event which just happened. This is currently 0-255 for
826 * keyboard scan codes, and 256 upwards for interrupts.
827 *
828 */
04ec96b7 829static void add_timer_randomness(struct timer_rand_state *state, unsigned int num)
1da177e4
LT
830{
831 struct {
1da177e4 832 long jiffies;
d38bb085
JD
833 unsigned int cycles;
834 unsigned int num;
1da177e4
LT
835 } sample;
836 long delta, delta2, delta3;
837
1da177e4 838 sample.jiffies = jiffies;
61875f30 839 sample.cycles = random_get_entropy();
1da177e4 840 sample.num = num;
90ed1e67 841 mix_pool_bytes(&sample, sizeof(sample));
1da177e4
LT
842
843 /*
844 * Calculate number of bits of randomness we probably added.
845 * We take into account the first, second and third-order deltas
846 * in order to make our estimate.
847 */
e00d996a
QC
848 delta = sample.jiffies - READ_ONCE(state->last_time);
849 WRITE_ONCE(state->last_time, sample.jiffies);
5e747dd9 850
e00d996a
QC
851 delta2 = delta - READ_ONCE(state->last_delta);
852 WRITE_ONCE(state->last_delta, delta);
5e747dd9 853
e00d996a
QC
854 delta3 = delta2 - READ_ONCE(state->last_delta2);
855 WRITE_ONCE(state->last_delta2, delta2);
5e747dd9
RV
856
857 if (delta < 0)
858 delta = -delta;
859 if (delta2 < 0)
860 delta2 = -delta2;
861 if (delta3 < 0)
862 delta3 = -delta3;
863 if (delta > delta2)
864 delta = delta2;
865 if (delta > delta3)
866 delta = delta3;
1da177e4 867
5e747dd9
RV
868 /*
869 * delta is now minimum absolute delta.
870 * Round down by 1 bit on general principles,
727d499a 871 * and limit entropy estimate to 12 bits.
5e747dd9 872 */
04ec96b7 873 credit_entropy_bits(min_t(unsigned int, fls(delta >> 1), 11));
1da177e4
LT
874}
875
d251575a 876void add_input_randomness(unsigned int type, unsigned int code,
248045b8 877 unsigned int value)
1da177e4
LT
878{
879 static unsigned char last_value;
880
881 /* ignore autorepeat and the like */
882 if (value == last_value)
883 return;
884
1da177e4
LT
885 last_value = value;
886 add_timer_randomness(&input_timer_state,
887 (type << 4) ^ code ^ (code >> 4) ^ value);
888}
80fc9f53 889EXPORT_SYMBOL_GPL(add_input_randomness);
1da177e4 890
775f4b29
TT
891static DEFINE_PER_CPU(struct fast_pool, irq_randomness);
892
d38bb085 893static u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
ee3e00e9 894{
248045b8 895 u32 *ptr = (u32 *)regs;
92e75428 896 unsigned int idx;
ee3e00e9
TT
897
898 if (regs == NULL)
899 return 0;
92e75428 900 idx = READ_ONCE(f->reg_idx);
d38bb085 901 if (idx >= sizeof(struct pt_regs) / sizeof(u32))
92e75428
TT
902 idx = 0;
903 ptr += idx++;
904 WRITE_ONCE(f->reg_idx, idx);
9dfa7bba 905 return *ptr;
ee3e00e9
TT
906}
907
703f7066 908void add_interrupt_randomness(int irq)
1da177e4 909{
248045b8
JD
910 struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
911 struct pt_regs *regs = get_irq_regs();
912 unsigned long now = jiffies;
913 cycles_t cycles = random_get_entropy();
3060d6fe 914
ee3e00e9
TT
915 if (cycles == 0)
916 cycles = get_reg(fast_pool, regs);
3060d6fe 917
b2f408fe
JD
918 if (sizeof(cycles) == 8)
919 fast_pool->pool64[0] ^= cycles ^ rol64(now, 32) ^ irq;
920 else {
921 fast_pool->pool32[0] ^= cycles ^ irq;
922 fast_pool->pool32[1] ^= now;
923 }
924
925 if (sizeof(unsigned long) == 8)
926 fast_pool->pool64[1] ^= regs ? instruction_pointer(regs) : _RET_IP_;
927 else {
928 fast_pool->pool32[2] ^= regs ? instruction_pointer(regs) : _RET_IP_;
929 fast_pool->pool32[3] ^= get_reg(fast_pool, regs);
930 }
931
932 fast_mix(fast_pool->pool32);
933 ++fast_pool->count;
3060d6fe 934
43838a23 935 if (unlikely(crng_init == 0)) {
04ec96b7 936 if (fast_pool->count >= 64 &&
b2f408fe 937 crng_fast_load(fast_pool->pool32, sizeof(fast_pool->pool32)) > 0) {
e192be9d
TT
938 fast_pool->count = 0;
939 fast_pool->last = now;
c30c575d 940 if (spin_trylock(&input_pool.lock)) {
b2f408fe 941 _mix_pool_bytes(&fast_pool->pool32, sizeof(fast_pool->pool32));
c30c575d
JD
942 spin_unlock(&input_pool.lock);
943 }
e192be9d
TT
944 }
945 return;
946 }
947
248045b8 948 if ((fast_pool->count < 64) && !time_after(now, fast_pool->last + HZ))
1da177e4
LT
949 return;
950
90ed1e67 951 if (!spin_trylock(&input_pool.lock))
91fcb532 952 return;
83664a69 953
91fcb532 954 fast_pool->last = now;
b2f408fe 955 _mix_pool_bytes(&fast_pool->pool32, sizeof(fast_pool->pool32));
90ed1e67 956 spin_unlock(&input_pool.lock);
83664a69 957
ee3e00e9 958 fast_pool->count = 0;
83664a69 959
ee3e00e9 960 /* award one bit for the contents of the fast pool */
90ed1e67 961 credit_entropy_bits(1);
1da177e4 962}
4b44f2d1 963EXPORT_SYMBOL_GPL(add_interrupt_randomness);
1da177e4 964
9361401e 965#ifdef CONFIG_BLOCK
1da177e4
LT
966void add_disk_randomness(struct gendisk *disk)
967{
968 if (!disk || !disk->random)
969 return;
970 /* first major is 1, so we get >= 0x200 here */
f331c029 971 add_timer_randomness(disk->random, 0x100 + disk_devt(disk));
1da177e4 972}
bdcfa3e5 973EXPORT_SYMBOL_GPL(add_disk_randomness);
9361401e 974#endif
1da177e4 975
1da177e4
LT
976/*********************************************************************
977 *
978 * Entropy extraction routines
979 *
980 *********************************************************************/
981
19fa5be1 982/*
6e8ec255
JD
983 * This is an HKDF-like construction for using the hashed collected entropy
984 * as a PRF key, that's then expanded block-by-block.
19fa5be1 985 */
9c07f578 986static void extract_entropy(void *buf, size_t nbytes)
1da177e4 987{
902c098a 988 unsigned long flags;
6e8ec255
JD
989 u8 seed[BLAKE2S_HASH_SIZE], next_key[BLAKE2S_HASH_SIZE];
990 struct {
28f425e5 991 unsigned long rdseed[32 / sizeof(long)];
6e8ec255
JD
992 size_t counter;
993 } block;
994 size_t i;
995
28f425e5
JD
996 for (i = 0; i < ARRAY_SIZE(block.rdseed); ++i) {
997 if (!arch_get_random_seed_long(&block.rdseed[i]) &&
998 !arch_get_random_long(&block.rdseed[i]))
999 block.rdseed[i] = random_get_entropy();
85a1f777
TT
1000 }
1001
90ed1e67 1002 spin_lock_irqsave(&input_pool.lock, flags);
46884442 1003
6e8ec255
JD
1004 /* seed = HASHPRF(last_key, entropy_input) */
1005 blake2s_final(&input_pool.hash, seed);
1da177e4 1006
28f425e5 1007 /* next_key = HASHPRF(seed, RDSEED || 0) */
6e8ec255
JD
1008 block.counter = 0;
1009 blake2s(next_key, (u8 *)&block, seed, sizeof(next_key), sizeof(block), sizeof(seed));
1010 blake2s_init_key(&input_pool.hash, BLAKE2S_HASH_SIZE, next_key, sizeof(next_key));
1da177e4 1011
6e8ec255
JD
1012 spin_unlock_irqrestore(&input_pool.lock, flags);
1013 memzero_explicit(next_key, sizeof(next_key));
e192be9d
TT
1014
1015 while (nbytes) {
6e8ec255 1016 i = min_t(size_t, nbytes, BLAKE2S_HASH_SIZE);
28f425e5 1017 /* output = HASHPRF(seed, RDSEED || ++counter) */
6e8ec255
JD
1018 ++block.counter;
1019 blake2s(buf, (u8 *)&block, seed, i, sizeof(block), sizeof(seed));
e192be9d
TT
1020 nbytes -= i;
1021 buf += i;
e192be9d
TT
1022 }
1023
6e8ec255
JD
1024 memzero_explicit(seed, sizeof(seed));
1025 memzero_explicit(&block, sizeof(block));
e192be9d
TT
1026}
1027
246c03dd
JD
1028/*
1029 * First we make sure we have POOL_MIN_BITS of entropy in the pool, and then we
1030 * set the entropy count to zero (but don't actually touch any data). Only then
1031 * can we extract a new key with extract_entropy().
1032 */
1033static bool drain_entropy(void *buf, size_t nbytes)
1034{
1035 unsigned int entropy_count;
1036 do {
1037 entropy_count = READ_ONCE(input_pool.entropy_count);
1038 if (entropy_count < POOL_MIN_BITS)
1039 return false;
1040 } while (cmpxchg(&input_pool.entropy_count, entropy_count, 0) != entropy_count);
1041 extract_entropy(buf, nbytes);
1042 wake_up_interruptible(&random_write_wait);
1043 kill_fasync(&fasync, SIGIO, POLL_OUT);
1044 return true;
1045}
1046
1da177e4
LT
1047/*
1048 * This function is the exported kernel interface. It returns some
c2557a30 1049 * number of good random numbers, suitable for key generation, seeding
18e9cea7
GP
1050 * TCP sequence numbers, etc. It does not rely on the hardware random
1051 * number generator. For random bytes direct from the hardware RNG
e297a783
JD
1052 * (when available), use get_random_bytes_arch(). In order to ensure
1053 * that the randomness provided by this function is okay, the function
1054 * wait_for_random_bytes() should be called and return 0 at least once
1055 * at any point prior.
1da177e4 1056 */
04ec96b7 1057static void _get_random_bytes(void *buf, size_t nbytes)
c2557a30 1058{
186873c5
JD
1059 u32 chacha_state[CHACHA_STATE_WORDS];
1060 u8 tmp[CHACHA_BLOCK_SIZE];
04ec96b7 1061 size_t len;
e192be9d 1062
186873c5
JD
1063 if (!nbytes)
1064 return;
1065
04ec96b7 1066 len = min_t(size_t, 32, nbytes);
186873c5
JD
1067 crng_make_state(chacha_state, buf, len);
1068 nbytes -= len;
1069 buf += len;
1070
1071 while (nbytes) {
1072 if (nbytes < CHACHA_BLOCK_SIZE) {
1073 chacha20_block(chacha_state, tmp);
1074 memcpy(buf, tmp, nbytes);
1075 memzero_explicit(tmp, sizeof(tmp));
1076 break;
1077 }
1078
1079 chacha20_block(chacha_state, buf);
1080 if (unlikely(chacha_state[12] == 0))
1081 ++chacha_state[13];
1ca1b917 1082 nbytes -= CHACHA_BLOCK_SIZE;
186873c5 1083 buf += CHACHA_BLOCK_SIZE;
e192be9d
TT
1084 }
1085
186873c5 1086 memzero_explicit(chacha_state, sizeof(chacha_state));
c2557a30 1087}
eecabf56 1088
04ec96b7 1089void get_random_bytes(void *buf, size_t nbytes)
eecabf56
TT
1090{
1091 static void *previous;
1092
1093 warn_unseeded_randomness(&previous);
1094 _get_random_bytes(buf, nbytes);
1095}
c2557a30
TT
1096EXPORT_SYMBOL(get_random_bytes);
1097
50ee7529
LT
1098/*
1099 * Each time the timer fires, we expect that we got an unpredictable
1100 * jump in the cycle counter. Even if the timer is running on another
1101 * CPU, the timer activity will be touching the stack of the CPU that is
1102 * generating entropy..
1103 *
1104 * Note that we don't re-arm the timer in the timer itself - we are
1105 * happy to be scheduled away, since that just makes the load more
1106 * complex, but we do not want the timer to keep ticking unless the
1107 * entropy loop is running.
1108 *
1109 * So the re-arming always happens in the entropy loop itself.
1110 */
1111static void entropy_timer(struct timer_list *t)
1112{
90ed1e67 1113 credit_entropy_bits(1);
50ee7529
LT
1114}
1115
1116/*
1117 * If we have an actual cycle counter, see if we can
1118 * generate enough entropy with timing noise
1119 */
1120static void try_to_generate_entropy(void)
1121{
1122 struct {
1123 unsigned long now;
1124 struct timer_list timer;
1125 } stack;
1126
1127 stack.now = random_get_entropy();
1128
1129 /* Slow counter - or none. Don't even bother */
1130 if (stack.now == random_get_entropy())
1131 return;
1132
1133 timer_setup_on_stack(&stack.timer, entropy_timer, 0);
1134 while (!crng_ready()) {
1135 if (!timer_pending(&stack.timer))
248045b8 1136 mod_timer(&stack.timer, jiffies + 1);
90ed1e67 1137 mix_pool_bytes(&stack.now, sizeof(stack.now));
50ee7529
LT
1138 schedule();
1139 stack.now = random_get_entropy();
1140 }
1141
1142 del_timer_sync(&stack.timer);
1143 destroy_timer_on_stack(&stack.timer);
90ed1e67 1144 mix_pool_bytes(&stack.now, sizeof(stack.now));
50ee7529
LT
1145}
1146
c2557a30
TT
1147/*
1148 * This function will use the architecture-specific hardware random
04ec96b7
JD
1149 * number generator if it is available. It is not recommended for
1150 * use. Use get_random_bytes() instead. It returns the number of
1151 * bytes filled in.
c2557a30 1152 */
04ec96b7 1153size_t __must_check get_random_bytes_arch(void *buf, size_t nbytes)
1da177e4 1154{
04ec96b7 1155 size_t left = nbytes;
d38bb085 1156 u8 *p = buf;
63d77173 1157
753d433b 1158 while (left) {
63d77173 1159 unsigned long v;
04ec96b7 1160 size_t chunk = min_t(size_t, left, sizeof(unsigned long));
c2557a30 1161
63d77173
PA
1162 if (!arch_get_random_long(&v))
1163 break;
8ddd6efa 1164
bd29e568 1165 memcpy(p, &v, chunk);
63d77173 1166 p += chunk;
753d433b 1167 left -= chunk;
63d77173
PA
1168 }
1169
753d433b 1170 return nbytes - left;
1da177e4 1171}
c2557a30
TT
1172EXPORT_SYMBOL(get_random_bytes_arch);
1173
85664172
JD
1174static bool trust_cpu __ro_after_init = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU);
1175static int __init parse_trust_cpu(char *arg)
1176{
1177 return kstrtobool(arg, &trust_cpu);
1178}
1179early_param("random.trust_cpu", parse_trust_cpu);
1180
1da177e4 1181/*
85664172
JD
1182 * Note that setup_arch() may call add_device_randomness()
1183 * long before we get here. This allows seeding of the pools
1184 * with some platform dependent data very early in the boot
1185 * process. But it limits our options here. We must use
1186 * statically allocated structures that already have all
1187 * initializations complete at compile time. We should also
1188 * take care not to overwrite the precious per platform data
1189 * we were given.
1da177e4 1190 */
85664172 1191int __init rand_initialize(void)
1da177e4 1192{
04ec96b7 1193 size_t i;
902c098a 1194 ktime_t now = ktime_get_real();
85664172 1195 bool arch_init = true;
902c098a 1196 unsigned long rv;
1da177e4 1197
04ec96b7 1198 for (i = 0; i < BLAKE2S_BLOCK_SIZE; i += sizeof(rv)) {
85664172
JD
1199 if (!arch_get_random_seed_long_early(&rv) &&
1200 !arch_get_random_long_early(&rv)) {
1201 rv = random_get_entropy();
1202 arch_init = false;
1203 }
a02cf3d0 1204 mix_pool_bytes(&rv, sizeof(rv));
85664172 1205 }
a02cf3d0
JD
1206 mix_pool_bytes(&now, sizeof(now));
1207 mix_pool_bytes(utsname(), sizeof(*(utsname())));
1208
186873c5 1209 extract_entropy(base_crng.key, sizeof(base_crng.key));
0791e8b6
JD
1210 ++base_crng.generation;
1211
85664172 1212 if (arch_init && trust_cpu && crng_init < 2) {
85664172
JD
1213 crng_init = 2;
1214 pr_notice("crng init done (trusting CPU's manufacturer)\n");
1215 }
85664172 1216
4e00b339
TT
1217 if (ratelimit_disable) {
1218 urandom_warning.interval = 0;
1219 unseeded_warning.interval = 0;
1220 }
1da177e4
LT
1221 return 0;
1222}
1da177e4 1223
9361401e 1224#ifdef CONFIG_BLOCK
1da177e4
LT
1225void rand_initialize_disk(struct gendisk *disk)
1226{
1227 struct timer_rand_state *state;
1228
1229 /*
f8595815 1230 * If kzalloc returns null, we just won't use that entropy
1da177e4
LT
1231 * source.
1232 */
f8595815 1233 state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
644008df
TT
1234 if (state) {
1235 state->last_time = INITIAL_JIFFIES;
1da177e4 1236 disk->random = state;
644008df 1237 }
1da177e4 1238}
9361401e 1239#endif
1da177e4 1240
248045b8
JD
1241static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes,
1242 loff_t *ppos)
1da177e4 1243{
9b4d0087 1244 static int maxwarn = 10;
301f0595 1245
e192be9d 1246 if (!crng_ready() && maxwarn > 0) {
9b4d0087 1247 maxwarn--;
4e00b339 1248 if (__ratelimit(&urandom_warning))
12cd53af
YL
1249 pr_notice("%s: uninitialized urandom read (%zd bytes read)\n",
1250 current->comm, nbytes);
9b4d0087 1251 }
c6f1deb1 1252
14c17463 1253 return get_random_bytes_user(buf, nbytes);
1da177e4
LT
1254}
1255
248045b8
JD
1256static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes,
1257 loff_t *ppos)
30c08efe
AL
1258{
1259 int ret;
1260
1261 ret = wait_for_random_bytes();
1262 if (ret != 0)
1263 return ret;
14c17463 1264 return get_random_bytes_user(buf, nbytes);
30c08efe
AL
1265}
1266
248045b8 1267static __poll_t random_poll(struct file *file, poll_table *wait)
1da177e4 1268{
a11e1d43 1269 __poll_t mask;
1da177e4 1270
30c08efe 1271 poll_wait(file, &crng_init_wait, wait);
a11e1d43
LT
1272 poll_wait(file, &random_write_wait, wait);
1273 mask = 0;
30c08efe 1274 if (crng_ready())
a9a08845 1275 mask |= EPOLLIN | EPOLLRDNORM;
489c7fc4 1276 if (input_pool.entropy_count < POOL_MIN_BITS)
a9a08845 1277 mask |= EPOLLOUT | EPOLLWRNORM;
1da177e4
LT
1278 return mask;
1279}
1280
04ec96b7 1281static int write_pool(const char __user *ubuf, size_t count)
1da177e4 1282{
04ec96b7 1283 size_t len;
7b5164fb 1284 int ret = 0;
04ec96b7 1285 u8 block[BLAKE2S_BLOCK_SIZE];
1da177e4 1286
04ec96b7
JD
1287 while (count) {
1288 len = min(count, sizeof(block));
7b5164fb
JD
1289 if (copy_from_user(block, ubuf, len)) {
1290 ret = -EFAULT;
1291 goto out;
1292 }
04ec96b7
JD
1293 count -= len;
1294 ubuf += len;
1295 mix_pool_bytes(block, len);
91f3f1e3 1296 cond_resched();
1da177e4 1297 }
7f397dcd 1298
7b5164fb
JD
1299out:
1300 memzero_explicit(block, sizeof(block));
1301 return ret;
7f397dcd
MM
1302}
1303
90b75ee5
MM
1304static ssize_t random_write(struct file *file, const char __user *buffer,
1305 size_t count, loff_t *ppos)
7f397dcd 1306{
04ec96b7 1307 int ret;
7f397dcd 1308
90ed1e67 1309 ret = write_pool(buffer, count);
7f397dcd
MM
1310 if (ret)
1311 return ret;
1312
7f397dcd 1313 return (ssize_t)count;
1da177e4
LT
1314}
1315
43ae4860 1316static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1da177e4
LT
1317{
1318 int size, ent_count;
1319 int __user *p = (int __user *)arg;
1320 int retval;
1321
1322 switch (cmd) {
1323 case RNDGETENTCNT:
43ae4860 1324 /* inherently racy, no point locking */
c5704490 1325 if (put_user(input_pool.entropy_count, p))
1da177e4
LT
1326 return -EFAULT;
1327 return 0;
1328 case RNDADDTOENTCNT:
1329 if (!capable(CAP_SYS_ADMIN))
1330 return -EPERM;
1331 if (get_user(ent_count, p))
1332 return -EFAULT;
a49c010e
JD
1333 if (ent_count < 0)
1334 return -EINVAL;
1335 credit_entropy_bits(ent_count);
1336 return 0;
1da177e4
LT
1337 case RNDADDENTROPY:
1338 if (!capable(CAP_SYS_ADMIN))
1339 return -EPERM;
1340 if (get_user(ent_count, p++))
1341 return -EFAULT;
1342 if (ent_count < 0)
1343 return -EINVAL;
1344 if (get_user(size, p++))
1345 return -EFAULT;
90ed1e67 1346 retval = write_pool((const char __user *)p, size);
1da177e4
LT
1347 if (retval < 0)
1348 return retval;
a49c010e
JD
1349 credit_entropy_bits(ent_count);
1350 return 0;
1da177e4
LT
1351 case RNDZAPENTCNT:
1352 case RNDCLEARPOOL:
ae9ecd92
TT
1353 /*
1354 * Clear the entropy pool counters. We no longer clear
1355 * the entropy pool, as that's silly.
1356 */
1da177e4
LT
1357 if (!capable(CAP_SYS_ADMIN))
1358 return -EPERM;
489c7fc4 1359 if (xchg(&input_pool.entropy_count, 0)) {
042e293e
JD
1360 wake_up_interruptible(&random_write_wait);
1361 kill_fasync(&fasync, SIGIO, POLL_OUT);
1362 }
1da177e4 1363 return 0;
d848e5f8
TT
1364 case RNDRESEEDCRNG:
1365 if (!capable(CAP_SYS_ADMIN))
1366 return -EPERM;
1367 if (crng_init < 2)
1368 return -ENODATA;
a9412d51 1369 crng_reseed();
d848e5f8 1370 return 0;
1da177e4
LT
1371 default:
1372 return -EINVAL;
1373 }
1374}
1375
9a6f70bb
JD
1376static int random_fasync(int fd, struct file *filp, int on)
1377{
1378 return fasync_helper(fd, filp, on, &fasync);
1379}
1380
2b8693c0 1381const struct file_operations random_fops = {
248045b8 1382 .read = random_read,
1da177e4 1383 .write = random_write,
248045b8 1384 .poll = random_poll,
43ae4860 1385 .unlocked_ioctl = random_ioctl,
507e4e2b 1386 .compat_ioctl = compat_ptr_ioctl,
9a6f70bb 1387 .fasync = random_fasync,
6038f373 1388 .llseek = noop_llseek,
1da177e4
LT
1389};
1390
2b8693c0 1391const struct file_operations urandom_fops = {
248045b8 1392 .read = urandom_read,
1da177e4 1393 .write = random_write,
43ae4860 1394 .unlocked_ioctl = random_ioctl,
4aa37c46 1395 .compat_ioctl = compat_ptr_ioctl,
9a6f70bb 1396 .fasync = random_fasync,
6038f373 1397 .llseek = noop_llseek,
1da177e4
LT
1398};
1399
248045b8
JD
1400SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int,
1401 flags)
c6e9d6f3 1402{
248045b8 1403 if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE))
75551dbf
AL
1404 return -EINVAL;
1405
1406 /*
1407 * Requesting insecure and blocking randomness at the same time makes
1408 * no sense.
1409 */
248045b8 1410 if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM))
c6e9d6f3
TT
1411 return -EINVAL;
1412
1413 if (count > INT_MAX)
1414 count = INT_MAX;
1415
75551dbf 1416 if (!(flags & GRND_INSECURE) && !crng_ready()) {
04ec96b7
JD
1417 int ret;
1418
c6e9d6f3
TT
1419 if (flags & GRND_NONBLOCK)
1420 return -EAGAIN;
e297a783
JD
1421 ret = wait_for_random_bytes();
1422 if (unlikely(ret))
1423 return ret;
c6e9d6f3 1424 }
14c17463 1425 return get_random_bytes_user(buf, count);
c6e9d6f3
TT
1426}
1427
1da177e4
LT
1428/********************************************************************
1429 *
1430 * Sysctl interface
1431 *
1432 ********************************************************************/
1433
1434#ifdef CONFIG_SYSCTL
1435
1436#include <linux/sysctl.h>
1437
db61ffe3 1438static int random_min_urandom_seed = 60;
489c7fc4
JD
1439static int random_write_wakeup_bits = POOL_MIN_BITS;
1440static int sysctl_poolsize = POOL_BITS;
1da177e4
LT
1441static char sysctl_bootid[16];
1442
1443/*
f22052b2 1444 * This function is used to return both the bootid UUID, and random
1da177e4
LT
1445 * UUID. The difference is in whether table->data is NULL; if it is,
1446 * then a new UUID is generated and returned to the user.
1447 *
f22052b2
GP
1448 * If the user accesses this via the proc interface, the UUID will be
1449 * returned as an ASCII string in the standard UUID format; if via the
1450 * sysctl system call, as 16 bytes of binary data.
1da177e4 1451 */
248045b8
JD
1452static int proc_do_uuid(struct ctl_table *table, int write, void *buffer,
1453 size_t *lenp, loff_t *ppos)
1da177e4 1454{
a151427e 1455 struct ctl_table fake_table;
1da177e4
LT
1456 unsigned char buf[64], tmp_uuid[16], *uuid;
1457
1458 uuid = table->data;
1459 if (!uuid) {
1460 uuid = tmp_uuid;
1da177e4 1461 generate_random_uuid(uuid);
44e4360f
MD
1462 } else {
1463 static DEFINE_SPINLOCK(bootid_spinlock);
1464
1465 spin_lock(&bootid_spinlock);
1466 if (!uuid[8])
1467 generate_random_uuid(uuid);
1468 spin_unlock(&bootid_spinlock);
1469 }
1da177e4 1470
35900771
JP
1471 sprintf(buf, "%pU", uuid);
1472
1da177e4
LT
1473 fake_table.data = buf;
1474 fake_table.maxlen = sizeof(buf);
1475
8d65af78 1476 return proc_dostring(&fake_table, write, buffer, lenp, ppos);
1da177e4
LT
1477}
1478
5475e8f0 1479static struct ctl_table random_table[] = {
1da177e4 1480 {
1da177e4
LT
1481 .procname = "poolsize",
1482 .data = &sysctl_poolsize,
1483 .maxlen = sizeof(int),
1484 .mode = 0444,
6d456111 1485 .proc_handler = proc_dointvec,
1da177e4
LT
1486 },
1487 {
1da177e4 1488 .procname = "entropy_avail",
c5704490 1489 .data = &input_pool.entropy_count,
1da177e4
LT
1490 .maxlen = sizeof(int),
1491 .mode = 0444,
c5704490 1492 .proc_handler = proc_dointvec,
1da177e4 1493 },
1da177e4 1494 {
1da177e4 1495 .procname = "write_wakeup_threshold",
2132a96f 1496 .data = &random_write_wakeup_bits,
1da177e4
LT
1497 .maxlen = sizeof(int),
1498 .mode = 0644,
489c7fc4 1499 .proc_handler = proc_dointvec,
1da177e4 1500 },
f5c2742c
TT
1501 {
1502 .procname = "urandom_min_reseed_secs",
1503 .data = &random_min_urandom_seed,
1504 .maxlen = sizeof(int),
1505 .mode = 0644,
1506 .proc_handler = proc_dointvec,
1507 },
1da177e4 1508 {
1da177e4
LT
1509 .procname = "boot_id",
1510 .data = &sysctl_bootid,
1511 .maxlen = 16,
1512 .mode = 0444,
6d456111 1513 .proc_handler = proc_do_uuid,
1da177e4
LT
1514 },
1515 {
1da177e4
LT
1516 .procname = "uuid",
1517 .maxlen = 16,
1518 .mode = 0444,
6d456111 1519 .proc_handler = proc_do_uuid,
1da177e4 1520 },
894d2491 1521 { }
1da177e4 1522};
5475e8f0
XN
1523
1524/*
1525 * rand_initialize() is called before sysctl_init(),
1526 * so we cannot call register_sysctl_init() in rand_initialize()
1527 */
1528static int __init random_sysctls_init(void)
1529{
1530 register_sysctl_init("kernel/random", random_table);
1531 return 0;
1532}
1533device_initcall(random_sysctls_init);
248045b8 1534#endif /* CONFIG_SYSCTL */
1da177e4 1535
f5b98461
JD
1536struct batched_entropy {
1537 union {
186873c5
JD
1538 /*
1539 * We make this 1.5x a ChaCha block, so that we get the
1540 * remaining 32 bytes from fast key erasure, plus one full
1541 * block from the detached ChaCha state. We can increase
1542 * the size of this later if needed so long as we keep the
1543 * formula of (integer_blocks + 0.5) * CHACHA_BLOCK_SIZE.
1544 */
1545 u64 entropy_u64[CHACHA_BLOCK_SIZE * 3 / (2 * sizeof(u64))];
1546 u32 entropy_u32[CHACHA_BLOCK_SIZE * 3 / (2 * sizeof(u32))];
f5b98461 1547 };
77760fd7 1548 local_lock_t lock;
0791e8b6 1549 unsigned long generation;
f5b98461
JD
1550 unsigned int position;
1551};
b1132dea 1552
1da177e4 1553/*
f5b98461 1554 * Get a random word for internal kernel use only. The quality of the random
186873c5
JD
1555 * number is good as /dev/urandom. In order to ensure that the randomness
1556 * provided by this function is okay, the function wait_for_random_bytes()
1557 * should be called and return 0 at least once at any point prior.
1da177e4 1558 */
b7d5dc21 1559static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64) = {
186873c5
JD
1560 .lock = INIT_LOCAL_LOCK(batched_entropy_u64.lock),
1561 .position = UINT_MAX
b7d5dc21
SAS
1562};
1563
c440408c 1564u64 get_random_u64(void)
1da177e4 1565{
c440408c 1566 u64 ret;
b7d5dc21 1567 unsigned long flags;
f5b98461 1568 struct batched_entropy *batch;
eecabf56 1569 static void *previous;
0791e8b6 1570 unsigned long next_gen;
8a0a9bd4 1571
eecabf56 1572 warn_unseeded_randomness(&previous);
d06bfd19 1573
77760fd7 1574 local_lock_irqsave(&batched_entropy_u64.lock, flags);
b7d5dc21 1575 batch = raw_cpu_ptr(&batched_entropy_u64);
77760fd7 1576
0791e8b6 1577 next_gen = READ_ONCE(base_crng.generation);
186873c5 1578 if (batch->position >= ARRAY_SIZE(batch->entropy_u64) ||
77760fd7 1579 next_gen != batch->generation) {
186873c5 1580 _get_random_bytes(batch->entropy_u64, sizeof(batch->entropy_u64));
f5b98461 1581 batch->position = 0;
77760fd7 1582 batch->generation = next_gen;
f5b98461 1583 }
77760fd7 1584
186873c5
JD
1585 ret = batch->entropy_u64[batch->position];
1586 batch->entropy_u64[batch->position] = 0;
1587 ++batch->position;
77760fd7 1588 local_unlock_irqrestore(&batched_entropy_u64.lock, flags);
8a0a9bd4 1589 return ret;
1da177e4 1590}
c440408c 1591EXPORT_SYMBOL(get_random_u64);
1da177e4 1592
b7d5dc21 1593static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32) = {
186873c5
JD
1594 .lock = INIT_LOCAL_LOCK(batched_entropy_u32.lock),
1595 .position = UINT_MAX
b7d5dc21 1596};
77760fd7 1597
c440408c 1598u32 get_random_u32(void)
f5b98461 1599{
c440408c 1600 u32 ret;
b7d5dc21 1601 unsigned long flags;
f5b98461 1602 struct batched_entropy *batch;
eecabf56 1603 static void *previous;
0791e8b6 1604 unsigned long next_gen;
ec9ee4ac 1605
eecabf56 1606 warn_unseeded_randomness(&previous);
d06bfd19 1607
77760fd7 1608 local_lock_irqsave(&batched_entropy_u32.lock, flags);
b7d5dc21 1609 batch = raw_cpu_ptr(&batched_entropy_u32);
77760fd7 1610
0791e8b6 1611 next_gen = READ_ONCE(base_crng.generation);
186873c5 1612 if (batch->position >= ARRAY_SIZE(batch->entropy_u32) ||
77760fd7 1613 next_gen != batch->generation) {
186873c5 1614 _get_random_bytes(batch->entropy_u32, sizeof(batch->entropy_u32));
f5b98461 1615 batch->position = 0;
77760fd7 1616 batch->generation = next_gen;
f5b98461 1617 }
77760fd7 1618
186873c5
JD
1619 ret = batch->entropy_u32[batch->position];
1620 batch->entropy_u32[batch->position] = 0;
1621 ++batch->position;
77760fd7 1622 local_unlock_irqrestore(&batched_entropy_u32.lock, flags);
ec9ee4ac
DC
1623 return ret;
1624}
c440408c 1625EXPORT_SYMBOL(get_random_u32);
ec9ee4ac 1626
99fdafde
JC
1627/**
1628 * randomize_page - Generate a random, page aligned address
1629 * @start: The smallest acceptable address the caller will take.
1630 * @range: The size of the area, starting at @start, within which the
1631 * random address must fall.
1632 *
1633 * If @start + @range would overflow, @range is capped.
1634 *
1635 * NOTE: Historical use of randomize_range, which this replaces, presumed that
1636 * @start was already page aligned. We now align it regardless.
1637 *
1638 * Return: A page aligned address within [start, start + range). On error,
1639 * @start is returned.
1640 */
248045b8 1641unsigned long randomize_page(unsigned long start, unsigned long range)
99fdafde
JC
1642{
1643 if (!PAGE_ALIGNED(start)) {
1644 range -= PAGE_ALIGN(start) - start;
1645 start = PAGE_ALIGN(start);
1646 }
1647
1648 if (start > ULONG_MAX - range)
1649 range = ULONG_MAX - start;
1650
1651 range >>= PAGE_SHIFT;
1652
1653 if (range == 0)
1654 return start;
1655
1656 return start + (get_random_long() % range << PAGE_SHIFT);
1657}
1658
c84dbf61
TD
1659/* Interface for in-kernel drivers of true hardware RNGs.
1660 * Those devices may produce endless random bits and will be throttled
1661 * when our pool is full.
1662 */
04ec96b7 1663void add_hwgenerator_randomness(const void *buffer, size_t count,
c84dbf61
TD
1664 size_t entropy)
1665{
43838a23 1666 if (unlikely(crng_init == 0)) {
73c7733f 1667 size_t ret = crng_fast_load(buffer, count);
90ed1e67 1668 mix_pool_bytes(buffer, ret);
73c7733f
JD
1669 count -= ret;
1670 buffer += ret;
1671 if (!count || crng_init == 0)
1672 return;
3371f3da 1673 }
e192be9d 1674
c321e907 1675 /* Throttle writing if we're above the trickle threshold.
489c7fc4
JD
1676 * We'll be woken up again once below POOL_MIN_BITS, when
1677 * the calling thread is about to terminate, or once
1678 * CRNG_RESEED_INTERVAL has elapsed.
e192be9d 1679 */
c321e907 1680 wait_event_interruptible_timeout(random_write_wait,
f7e67b8e 1681 !system_wq || kthread_should_stop() ||
489c7fc4 1682 input_pool.entropy_count < POOL_MIN_BITS,
c321e907 1683 CRNG_RESEED_INTERVAL);
90ed1e67
JD
1684 mix_pool_bytes(buffer, count);
1685 credit_entropy_bits(entropy);
c84dbf61
TD
1686}
1687EXPORT_SYMBOL_GPL(add_hwgenerator_randomness);
428826f5
HYW
1688
1689/* Handle random seed passed by bootloader.
1690 * If the seed is trustworthy, it would be regarded as hardware RNGs. Otherwise
1691 * it would be regarded as device data.
1692 * The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER.
1693 */
04ec96b7 1694void add_bootloader_randomness(const void *buf, size_t size)
428826f5
HYW
1695{
1696 if (IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER))
1697 add_hwgenerator_randomness(buf, size, size * 8);
1698 else
1699 add_device_randomness(buf, size);
1700}
3fd57e7a 1701EXPORT_SYMBOL_GPL(add_bootloader_randomness);