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