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