Contents of /trunk/kernel-alx-legacy/patches-4.9/0419-4.9.320-all-fixes.patch
Parent Directory | Revision Log
Revision 3721 -
(show annotations)
(download)
Mon Oct 24 14:08:32 2022 UTC (19 months, 3 weeks ago) by niro
File size: 264492 byte(s)
Mon Oct 24 14:08:32 2022 UTC (19 months, 3 weeks ago) by niro
File size: 264492 byte(s)
-linux-4.9.320
1 | diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt |
2 | index 97c0ff0787eaf..92a9a3282c5b6 100644 |
3 | --- a/Documentation/kernel-parameters.txt |
4 | +++ b/Documentation/kernel-parameters.txt |
5 | @@ -3577,6 +3577,18 @@ bytes respectively. Such letter suffixes can also be entirely omitted. |
6 | ramdisk_size= [RAM] Sizes of RAM disks in kilobytes |
7 | See Documentation/blockdev/ramdisk.txt. |
8 | |
9 | + random.trust_cpu={on,off} |
10 | + [KNL] Enable or disable trusting the use of the |
11 | + CPU's random number generator (if available) to |
12 | + fully seed the kernel's CRNG. Default is controlled |
13 | + by CONFIG_RANDOM_TRUST_CPU. |
14 | + |
15 | + random.trust_bootloader={on,off} |
16 | + [KNL] Enable or disable trusting the use of a |
17 | + seed passed by the bootloader (if available) to |
18 | + fully seed the kernel's CRNG. Default is controlled |
19 | + by CONFIG_RANDOM_TRUST_BOOTLOADER. |
20 | + |
21 | rcu_nocbs= [KNL] |
22 | The argument is a cpu list, as described above. |
23 | |
24 | diff --git a/Documentation/sysctl/kernel.txt b/Documentation/sysctl/kernel.txt |
25 | index 30ba179392d81..79608693ef0b0 100644 |
26 | --- a/Documentation/sysctl/kernel.txt |
27 | +++ b/Documentation/sysctl/kernel.txt |
28 | @@ -777,9 +777,40 @@ The kernel command line parameter printk.devkmsg= overrides this and is |
29 | a one-time setting until next reboot: once set, it cannot be changed by |
30 | this sysctl interface anymore. |
31 | |
32 | -============================================================== |
33 | +pty |
34 | +=== |
35 | + |
36 | +See Documentation/filesystems/devpts.rst. |
37 | + |
38 | + |
39 | +random |
40 | +====== |
41 | + |
42 | +This is a directory, with the following entries: |
43 | + |
44 | +* ``boot_id``: a UUID generated the first time this is retrieved, and |
45 | + unvarying after that; |
46 | + |
47 | +* ``uuid``: a UUID generated every time this is retrieved (this can |
48 | + thus be used to generate UUIDs at will); |
49 | + |
50 | +* ``entropy_avail``: the pool's entropy count, in bits; |
51 | + |
52 | +* ``poolsize``: the entropy pool size, in bits; |
53 | + |
54 | +* ``urandom_min_reseed_secs``: obsolete (used to determine the minimum |
55 | + number of seconds between urandom pool reseeding). This file is |
56 | + writable for compatibility purposes, but writing to it has no effect |
57 | + on any RNG behavior; |
58 | + |
59 | +* ``write_wakeup_threshold``: when the entropy count drops below this |
60 | + (as a number of bits), processes waiting to write to ``/dev/random`` |
61 | + are woken up. This file is writable for compatibility purposes, but |
62 | + writing to it has no effect on any RNG behavior. |
63 | + |
64 | |
65 | -randomize_va_space: |
66 | +randomize_va_space |
67 | +================== |
68 | |
69 | This option can be used to select the type of process address |
70 | space randomization that is used in the system, for architectures |
71 | diff --git a/MAINTAINERS b/MAINTAINERS |
72 | index fcaab221553e0..afafd0fa18a9f 100644 |
73 | --- a/MAINTAINERS |
74 | +++ b/MAINTAINERS |
75 | @@ -10068,6 +10068,7 @@ F: drivers/block/brd.c |
76 | |
77 | RANDOM NUMBER DRIVER |
78 | M: "Theodore Ts'o" <tytso@mit.edu> |
79 | +M: Jason A. Donenfeld <Jason@zx2c4.com> |
80 | S: Maintained |
81 | F: drivers/char/random.c |
82 | |
83 | diff --git a/Makefile b/Makefile |
84 | index bf4a7b0fe8e74..04cefc7d5b47f 100644 |
85 | --- a/Makefile |
86 | +++ b/Makefile |
87 | @@ -1,6 +1,6 @@ |
88 | VERSION = 4 |
89 | PATCHLEVEL = 9 |
90 | -SUBLEVEL = 319 |
91 | +SUBLEVEL = 320 |
92 | EXTRAVERSION = |
93 | NAME = Roaring Lionus |
94 | |
95 | diff --git a/arch/alpha/include/asm/timex.h b/arch/alpha/include/asm/timex.h |
96 | index afa0c45e3e987..c3ed97c790868 100644 |
97 | --- a/arch/alpha/include/asm/timex.h |
98 | +++ b/arch/alpha/include/asm/timex.h |
99 | @@ -27,5 +27,6 @@ static inline cycles_t get_cycles (void) |
100 | __asm__ __volatile__ ("rpcc %0" : "=r"(ret)); |
101 | return ret; |
102 | } |
103 | +#define get_cycles get_cycles |
104 | |
105 | #endif |
106 | diff --git a/arch/arm/include/asm/timex.h b/arch/arm/include/asm/timex.h |
107 | index f6fcc67ef06ef..c06d38f0df8ec 100644 |
108 | --- a/arch/arm/include/asm/timex.h |
109 | +++ b/arch/arm/include/asm/timex.h |
110 | @@ -14,5 +14,6 @@ |
111 | |
112 | typedef unsigned long cycles_t; |
113 | #define get_cycles() ({ cycles_t c; read_current_timer(&c) ? 0 : c; }) |
114 | +#define random_get_entropy() (((unsigned long)get_cycles()) ?: random_get_entropy_fallback()) |
115 | |
116 | #endif |
117 | diff --git a/arch/ia64/include/asm/timex.h b/arch/ia64/include/asm/timex.h |
118 | index 86c7db8611803..0117fa73ad490 100644 |
119 | --- a/arch/ia64/include/asm/timex.h |
120 | +++ b/arch/ia64/include/asm/timex.h |
121 | @@ -38,6 +38,7 @@ get_cycles (void) |
122 | ret = ia64_getreg(_IA64_REG_AR_ITC); |
123 | return ret; |
124 | } |
125 | +#define get_cycles get_cycles |
126 | |
127 | extern void ia64_cpu_local_tick (void); |
128 | extern unsigned long long ia64_native_sched_clock (void); |
129 | diff --git a/arch/m68k/include/asm/timex.h b/arch/m68k/include/asm/timex.h |
130 | index efc1f48923573..bbaf67f3a952d 100644 |
131 | --- a/arch/m68k/include/asm/timex.h |
132 | +++ b/arch/m68k/include/asm/timex.h |
133 | @@ -34,7 +34,7 @@ static inline unsigned long random_get_entropy(void) |
134 | { |
135 | if (mach_random_get_entropy) |
136 | return mach_random_get_entropy(); |
137 | - return 0; |
138 | + return random_get_entropy_fallback(); |
139 | } |
140 | #define random_get_entropy random_get_entropy |
141 | |
142 | diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h |
143 | index 8026baf46e729..2e107886f97ac 100644 |
144 | --- a/arch/mips/include/asm/timex.h |
145 | +++ b/arch/mips/include/asm/timex.h |
146 | @@ -76,25 +76,24 @@ static inline cycles_t get_cycles(void) |
147 | else |
148 | return 0; /* no usable counter */ |
149 | } |
150 | +#define get_cycles get_cycles |
151 | |
152 | /* |
153 | * Like get_cycles - but where c0_count is not available we desperately |
154 | * use c0_random in an attempt to get at least a little bit of entropy. |
155 | - * |
156 | - * R6000 and R6000A neither have a count register nor a random register. |
157 | - * That leaves no entropy source in the CPU itself. |
158 | */ |
159 | static inline unsigned long random_get_entropy(void) |
160 | { |
161 | - unsigned int prid = read_c0_prid(); |
162 | - unsigned int imp = prid & PRID_IMP_MASK; |
163 | + unsigned int c0_random; |
164 | |
165 | - if (can_use_mips_counter(prid)) |
166 | + if (can_use_mips_counter(read_c0_prid())) |
167 | return read_c0_count(); |
168 | - else if (likely(imp != PRID_IMP_R6000 && imp != PRID_IMP_R6000A)) |
169 | - return read_c0_random(); |
170 | + |
171 | + if (cpu_has_3kex) |
172 | + c0_random = (read_c0_random() >> 8) & 0x3f; |
173 | else |
174 | - return 0; /* no usable register */ |
175 | + c0_random = read_c0_random() & 0x3f; |
176 | + return (random_get_entropy_fallback() << 6) | (0x3f - c0_random); |
177 | } |
178 | #define random_get_entropy random_get_entropy |
179 | |
180 | diff --git a/arch/nios2/include/asm/timex.h b/arch/nios2/include/asm/timex.h |
181 | index 2f2abb28ec2fd..9c9b50599ea30 100644 |
182 | --- a/arch/nios2/include/asm/timex.h |
183 | +++ b/arch/nios2/include/asm/timex.h |
184 | @@ -20,5 +20,8 @@ |
185 | typedef unsigned long cycles_t; |
186 | |
187 | extern cycles_t get_cycles(void); |
188 | +#define get_cycles get_cycles |
189 | + |
190 | +#define random_get_entropy() (((unsigned long)get_cycles()) ?: random_get_entropy_fallback()) |
191 | |
192 | #endif |
193 | diff --git a/arch/parisc/include/asm/timex.h b/arch/parisc/include/asm/timex.h |
194 | index 2bd51f6d832bb..87aeeee5b700f 100644 |
195 | --- a/arch/parisc/include/asm/timex.h |
196 | +++ b/arch/parisc/include/asm/timex.h |
197 | @@ -11,9 +11,10 @@ |
198 | |
199 | typedef unsigned long cycles_t; |
200 | |
201 | -static inline cycles_t get_cycles (void) |
202 | +static inline cycles_t get_cycles(void) |
203 | { |
204 | return mfctl(16); |
205 | } |
206 | +#define get_cycles get_cycles |
207 | |
208 | #endif |
209 | diff --git a/arch/powerpc/include/asm/archrandom.h b/arch/powerpc/include/asm/archrandom.h |
210 | index 9ff848e3c4a62..d8da8c0fb928c 100644 |
211 | --- a/arch/powerpc/include/asm/archrandom.h |
212 | +++ b/arch/powerpc/include/asm/archrandom.h |
213 | @@ -5,27 +5,28 @@ |
214 | |
215 | #include <asm/machdep.h> |
216 | |
217 | -static inline int arch_get_random_long(unsigned long *v) |
218 | +static inline bool arch_get_random_long(unsigned long *v) |
219 | { |
220 | - return 0; |
221 | + return false; |
222 | } |
223 | |
224 | -static inline int arch_get_random_int(unsigned int *v) |
225 | +static inline bool arch_get_random_int(unsigned int *v) |
226 | { |
227 | - return 0; |
228 | + return false; |
229 | } |
230 | |
231 | -static inline int arch_get_random_seed_long(unsigned long *v) |
232 | +static inline bool arch_get_random_seed_long(unsigned long *v) |
233 | { |
234 | if (ppc_md.get_random_seed) |
235 | return ppc_md.get_random_seed(v); |
236 | |
237 | - return 0; |
238 | + return false; |
239 | } |
240 | -static inline int arch_get_random_seed_int(unsigned int *v) |
241 | + |
242 | +static inline bool arch_get_random_seed_int(unsigned int *v) |
243 | { |
244 | unsigned long val; |
245 | - int rc; |
246 | + bool rc; |
247 | |
248 | rc = arch_get_random_seed_long(&val); |
249 | if (rc) |
250 | @@ -33,16 +34,6 @@ static inline int arch_get_random_seed_int(unsigned int *v) |
251 | |
252 | return rc; |
253 | } |
254 | - |
255 | -static inline int arch_has_random(void) |
256 | -{ |
257 | - return 0; |
258 | -} |
259 | - |
260 | -static inline int arch_has_random_seed(void) |
261 | -{ |
262 | - return !!ppc_md.get_random_seed; |
263 | -} |
264 | #endif /* CONFIG_ARCH_RANDOM */ |
265 | |
266 | #ifdef CONFIG_PPC_POWERNV |
267 | diff --git a/arch/powerpc/include/asm/timex.h b/arch/powerpc/include/asm/timex.h |
268 | index 2cf846edb3fcc..28b8a63bc3661 100644 |
269 | --- a/arch/powerpc/include/asm/timex.h |
270 | +++ b/arch/powerpc/include/asm/timex.h |
271 | @@ -53,6 +53,7 @@ static inline cycles_t get_cycles(void) |
272 | return ret; |
273 | #endif |
274 | } |
275 | +#define get_cycles get_cycles |
276 | |
277 | #endif /* __KERNEL__ */ |
278 | #endif /* _ASM_POWERPC_TIMEX_H */ |
279 | diff --git a/arch/s390/include/asm/timex.h b/arch/s390/include/asm/timex.h |
280 | index f1330245b5840..f9c222cfc65c2 100644 |
281 | --- a/arch/s390/include/asm/timex.h |
282 | +++ b/arch/s390/include/asm/timex.h |
283 | @@ -168,6 +168,7 @@ static inline cycles_t get_cycles(void) |
284 | { |
285 | return (cycles_t) get_tod_clock() >> 2; |
286 | } |
287 | +#define get_cycles get_cycles |
288 | |
289 | int get_phys_clock(unsigned long long *clock); |
290 | void init_cpu_timer(void); |
291 | diff --git a/arch/s390/mm/pgtable.c b/arch/s390/mm/pgtable.c |
292 | index 7678f7956409b..1847bc3ff163b 100644 |
293 | --- a/arch/s390/mm/pgtable.c |
294 | +++ b/arch/s390/mm/pgtable.c |
295 | @@ -595,7 +595,7 @@ void ptep_zap_key(struct mm_struct *mm, unsigned long addr, pte_t *ptep) |
296 | PGSTE_GR_BIT | PGSTE_GC_BIT); |
297 | ptev = pte_val(*ptep); |
298 | if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE)) |
299 | - page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1); |
300 | + page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 0); |
301 | pgste_set_unlock(ptep, pgste); |
302 | preempt_enable(); |
303 | } |
304 | diff --git a/arch/sparc/include/asm/timex_32.h b/arch/sparc/include/asm/timex_32.h |
305 | index b6ccdb0d6f7de..b805c511755d6 100644 |
306 | --- a/arch/sparc/include/asm/timex_32.h |
307 | +++ b/arch/sparc/include/asm/timex_32.h |
308 | @@ -8,8 +8,6 @@ |
309 | |
310 | #define CLOCK_TICK_RATE 1193180 /* Underlying HZ */ |
311 | |
312 | -/* XXX Maybe do something better at some point... -DaveM */ |
313 | -typedef unsigned long cycles_t; |
314 | -#define get_cycles() (0) |
315 | +#include <asm-generic/timex.h> |
316 | |
317 | #endif |
318 | diff --git a/arch/um/include/asm/timex.h b/arch/um/include/asm/timex.h |
319 | index 0f4ada08f7488..62a0fbf79420f 100644 |
320 | --- a/arch/um/include/asm/timex.h |
321 | +++ b/arch/um/include/asm/timex.h |
322 | @@ -1,13 +1,8 @@ |
323 | #ifndef __UM_TIMEX_H |
324 | #define __UM_TIMEX_H |
325 | |
326 | -typedef unsigned long cycles_t; |
327 | - |
328 | -static inline cycles_t get_cycles (void) |
329 | -{ |
330 | - return 0; |
331 | -} |
332 | - |
333 | #define CLOCK_TICK_RATE (HZ) |
334 | |
335 | +#include <asm-generic/timex.h> |
336 | + |
337 | #endif |
338 | diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h |
339 | index 3ac991d81e74d..4d3cac3c9b250 100644 |
340 | --- a/arch/x86/include/asm/archrandom.h |
341 | +++ b/arch/x86/include/asm/archrandom.h |
342 | @@ -86,10 +86,6 @@ static inline bool rdseed_int(unsigned int *v) |
343 | return ok; |
344 | } |
345 | |
346 | -/* Conditional execution based on CPU type */ |
347 | -#define arch_has_random() static_cpu_has(X86_FEATURE_RDRAND) |
348 | -#define arch_has_random_seed() static_cpu_has(X86_FEATURE_RDSEED) |
349 | - |
350 | /* |
351 | * These are the generic interfaces; they must not be declared if the |
352 | * stubs in <linux/random.h> are to be invoked, |
353 | @@ -99,22 +95,22 @@ static inline bool rdseed_int(unsigned int *v) |
354 | |
355 | static inline bool arch_get_random_long(unsigned long *v) |
356 | { |
357 | - return arch_has_random() ? rdrand_long(v) : false; |
358 | + return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_long(v) : false; |
359 | } |
360 | |
361 | static inline bool arch_get_random_int(unsigned int *v) |
362 | { |
363 | - return arch_has_random() ? rdrand_int(v) : false; |
364 | + return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_int(v) : false; |
365 | } |
366 | |
367 | static inline bool arch_get_random_seed_long(unsigned long *v) |
368 | { |
369 | - return arch_has_random_seed() ? rdseed_long(v) : false; |
370 | + return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_long(v) : false; |
371 | } |
372 | |
373 | static inline bool arch_get_random_seed_int(unsigned int *v) |
374 | { |
375 | - return arch_has_random_seed() ? rdseed_int(v) : false; |
376 | + return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_int(v) : false; |
377 | } |
378 | |
379 | extern void x86_init_rdrand(struct cpuinfo_x86 *c); |
380 | diff --git a/arch/x86/include/asm/timex.h b/arch/x86/include/asm/timex.h |
381 | index 1375cfc93960e..627802b6204c2 100644 |
382 | --- a/arch/x86/include/asm/timex.h |
383 | +++ b/arch/x86/include/asm/timex.h |
384 | @@ -4,6 +4,15 @@ |
385 | #include <asm/processor.h> |
386 | #include <asm/tsc.h> |
387 | |
388 | +static inline unsigned long random_get_entropy(void) |
389 | +{ |
390 | + if (!IS_ENABLED(CONFIG_X86_TSC) && |
391 | + !cpu_feature_enabled(X86_FEATURE_TSC)) |
392 | + return random_get_entropy_fallback(); |
393 | + return rdtsc(); |
394 | +} |
395 | +#define random_get_entropy random_get_entropy |
396 | + |
397 | /* Assume we use the PIT time source for the clock tick */ |
398 | #define CLOCK_TICK_RATE PIT_TICK_RATE |
399 | |
400 | diff --git a/arch/x86/include/asm/tsc.h b/arch/x86/include/asm/tsc.h |
401 | index 33b6365c22fed..23235c5ef1a14 100644 |
402 | --- a/arch/x86/include/asm/tsc.h |
403 | +++ b/arch/x86/include/asm/tsc.h |
404 | @@ -21,13 +21,12 @@ extern void disable_TSC(void); |
405 | |
406 | static inline cycles_t get_cycles(void) |
407 | { |
408 | -#ifndef CONFIG_X86_TSC |
409 | - if (!boot_cpu_has(X86_FEATURE_TSC)) |
410 | + if (!IS_ENABLED(CONFIG_X86_TSC) && |
411 | + !cpu_feature_enabled(X86_FEATURE_TSC)) |
412 | return 0; |
413 | -#endif |
414 | - |
415 | return rdtsc(); |
416 | } |
417 | +#define get_cycles get_cycles |
418 | |
419 | extern struct system_counterval_t convert_art_to_tsc(cycle_t art); |
420 | |
421 | diff --git a/arch/xtensa/include/asm/timex.h b/arch/xtensa/include/asm/timex.h |
422 | index f9b389d4e9739..d866bc847d8dc 100644 |
423 | --- a/arch/xtensa/include/asm/timex.h |
424 | +++ b/arch/xtensa/include/asm/timex.h |
425 | @@ -30,10 +30,6 @@ |
426 | |
427 | extern unsigned long ccount_freq; |
428 | |
429 | -typedef unsigned long long cycles_t; |
430 | - |
431 | -#define get_cycles() (0) |
432 | - |
433 | void local_timer_setup(unsigned cpu); |
434 | |
435 | /* |
436 | @@ -69,4 +65,6 @@ static inline void set_linux_timer (unsigned long ccompare) |
437 | WSR_CCOMPARE(LINUX_TIMER, ccompare); |
438 | } |
439 | |
440 | +#include <asm-generic/timex.h> |
441 | + |
442 | #endif /* _XTENSA_TIMEX_H */ |
443 | diff --git a/crypto/chacha20_generic.c b/crypto/chacha20_generic.c |
444 | index 1cab83146e33b..56603afbf6bd0 100644 |
445 | --- a/crypto/chacha20_generic.c |
446 | +++ b/crypto/chacha20_generic.c |
447 | @@ -23,7 +23,8 @@ static inline u32 le32_to_cpuvp(const void *p) |
448 | static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src, |
449 | unsigned int bytes) |
450 | { |
451 | - u8 stream[CHACHA20_BLOCK_SIZE]; |
452 | + /* aligned to potentially speed up crypto_xor() */ |
453 | + u8 stream[CHACHA20_BLOCK_SIZE] __aligned(sizeof(long)); |
454 | |
455 | if (dst != src) |
456 | memcpy(dst, src, bytes); |
457 | diff --git a/crypto/drbg.c b/crypto/drbg.c |
458 | index 4bb5f93c94cdb..1b94a4306dfc4 100644 |
459 | --- a/crypto/drbg.c |
460 | +++ b/crypto/drbg.c |
461 | @@ -219,6 +219,57 @@ static inline unsigned short drbg_sec_strength(drbg_flag_t flags) |
462 | } |
463 | } |
464 | |
465 | +/* |
466 | + * FIPS 140-2 continuous self test for the noise source |
467 | + * The test is performed on the noise source input data. Thus, the function |
468 | + * implicitly knows the size of the buffer to be equal to the security |
469 | + * strength. |
470 | + * |
471 | + * Note, this function disregards the nonce trailing the entropy data during |
472 | + * initial seeding. |
473 | + * |
474 | + * drbg->drbg_mutex must have been taken. |
475 | + * |
476 | + * @drbg DRBG handle |
477 | + * @entropy buffer of seed data to be checked |
478 | + * |
479 | + * return: |
480 | + * 0 on success |
481 | + * -EAGAIN on when the CTRNG is not yet primed |
482 | + * < 0 on error |
483 | + */ |
484 | +static int drbg_fips_continuous_test(struct drbg_state *drbg, |
485 | + const unsigned char *entropy) |
486 | +{ |
487 | + unsigned short entropylen = drbg_sec_strength(drbg->core->flags); |
488 | + int ret = 0; |
489 | + |
490 | + if (!IS_ENABLED(CONFIG_CRYPTO_FIPS)) |
491 | + return 0; |
492 | + |
493 | + /* skip test if we test the overall system */ |
494 | + if (list_empty(&drbg->test_data.list)) |
495 | + return 0; |
496 | + /* only perform test in FIPS mode */ |
497 | + if (!fips_enabled) |
498 | + return 0; |
499 | + |
500 | + if (!drbg->fips_primed) { |
501 | + /* Priming of FIPS test */ |
502 | + memcpy(drbg->prev, entropy, entropylen); |
503 | + drbg->fips_primed = true; |
504 | + /* priming: another round is needed */ |
505 | + return -EAGAIN; |
506 | + } |
507 | + ret = memcmp(drbg->prev, entropy, entropylen); |
508 | + if (!ret) |
509 | + panic("DRBG continuous self test failed\n"); |
510 | + memcpy(drbg->prev, entropy, entropylen); |
511 | + |
512 | + /* the test shall pass when the two values are not equal */ |
513 | + return 0; |
514 | +} |
515 | + |
516 | /* |
517 | * Convert an integer into a byte representation of this integer. |
518 | * The byte representation is big-endian |
519 | @@ -986,55 +1037,79 @@ static const struct drbg_state_ops drbg_hash_ops = { |
520 | ******************************************************************/ |
521 | |
522 | static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed, |
523 | - int reseed) |
524 | + int reseed, enum drbg_seed_state new_seed_state) |
525 | { |
526 | int ret = drbg->d_ops->update(drbg, seed, reseed); |
527 | |
528 | if (ret) |
529 | return ret; |
530 | |
531 | - drbg->seeded = true; |
532 | + drbg->seeded = new_seed_state; |
533 | /* 10.1.1.2 / 10.1.1.3 step 5 */ |
534 | drbg->reseed_ctr = 1; |
535 | |
536 | + switch (drbg->seeded) { |
537 | + case DRBG_SEED_STATE_UNSEEDED: |
538 | + /* Impossible, but handle it to silence compiler warnings. */ |
539 | + case DRBG_SEED_STATE_PARTIAL: |
540 | + /* |
541 | + * Require frequent reseeds until the seed source is |
542 | + * fully initialized. |
543 | + */ |
544 | + drbg->reseed_threshold = 50; |
545 | + break; |
546 | + |
547 | + case DRBG_SEED_STATE_FULL: |
548 | + /* |
549 | + * Seed source has become fully initialized, frequent |
550 | + * reseeds no longer required. |
551 | + */ |
552 | + drbg->reseed_threshold = drbg_max_requests(drbg); |
553 | + break; |
554 | + } |
555 | + |
556 | return ret; |
557 | } |
558 | |
559 | -static void drbg_async_seed(struct work_struct *work) |
560 | +static inline int drbg_get_random_bytes(struct drbg_state *drbg, |
561 | + unsigned char *entropy, |
562 | + unsigned int entropylen) |
563 | +{ |
564 | + int ret; |
565 | + |
566 | + do { |
567 | + get_random_bytes(entropy, entropylen); |
568 | + ret = drbg_fips_continuous_test(drbg, entropy); |
569 | + if (ret && ret != -EAGAIN) |
570 | + return ret; |
571 | + } while (ret); |
572 | + |
573 | + return 0; |
574 | +} |
575 | + |
576 | +static int drbg_seed_from_random(struct drbg_state *drbg) |
577 | { |
578 | struct drbg_string data; |
579 | LIST_HEAD(seedlist); |
580 | - struct drbg_state *drbg = container_of(work, struct drbg_state, |
581 | - seed_work); |
582 | unsigned int entropylen = drbg_sec_strength(drbg->core->flags); |
583 | unsigned char entropy[32]; |
584 | + int ret; |
585 | |
586 | BUG_ON(!entropylen); |
587 | BUG_ON(entropylen > sizeof(entropy)); |
588 | - get_random_bytes(entropy, entropylen); |
589 | |
590 | drbg_string_fill(&data, entropy, entropylen); |
591 | list_add_tail(&data.list, &seedlist); |
592 | |
593 | - mutex_lock(&drbg->drbg_mutex); |
594 | - |
595 | - /* If nonblocking pool is initialized, deactivate Jitter RNG */ |
596 | - crypto_free_rng(drbg->jent); |
597 | - drbg->jent = NULL; |
598 | - |
599 | - /* Set seeded to false so that if __drbg_seed fails the |
600 | - * next generate call will trigger a reseed. |
601 | - */ |
602 | - drbg->seeded = false; |
603 | - |
604 | - __drbg_seed(drbg, &seedlist, true); |
605 | - |
606 | - if (drbg->seeded) |
607 | - drbg->reseed_threshold = drbg_max_requests(drbg); |
608 | + ret = drbg_get_random_bytes(drbg, entropy, entropylen); |
609 | + if (ret) |
610 | + goto out; |
611 | |
612 | - mutex_unlock(&drbg->drbg_mutex); |
613 | + ret = __drbg_seed(drbg, &seedlist, true, DRBG_SEED_STATE_FULL); |
614 | |
615 | +out: |
616 | memzero_explicit(entropy, entropylen); |
617 | + return ret; |
618 | } |
619 | |
620 | /* |
621 | @@ -1056,6 +1131,7 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, |
622 | unsigned int entropylen = drbg_sec_strength(drbg->core->flags); |
623 | struct drbg_string data1; |
624 | LIST_HEAD(seedlist); |
625 | + enum drbg_seed_state new_seed_state = DRBG_SEED_STATE_FULL; |
626 | |
627 | /* 9.1 / 9.2 / 9.3.1 step 3 */ |
628 | if (pers && pers->len > (drbg_max_addtl(drbg))) { |
629 | @@ -1083,7 +1159,12 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, |
630 | BUG_ON((entropylen * 2) > sizeof(entropy)); |
631 | |
632 | /* Get seed from in-kernel /dev/urandom */ |
633 | - get_random_bytes(entropy, entropylen); |
634 | + if (!rng_is_initialized()) |
635 | + new_seed_state = DRBG_SEED_STATE_PARTIAL; |
636 | + |
637 | + ret = drbg_get_random_bytes(drbg, entropy, entropylen); |
638 | + if (ret) |
639 | + goto out; |
640 | |
641 | if (!drbg->jent) { |
642 | drbg_string_fill(&data1, entropy, entropylen); |
643 | @@ -1096,7 +1177,23 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, |
644 | entropylen); |
645 | if (ret) { |
646 | pr_devel("DRBG: jent failed with %d\n", ret); |
647 | - return ret; |
648 | + |
649 | + /* |
650 | + * Do not treat the transient failure of the |
651 | + * Jitter RNG as an error that needs to be |
652 | + * reported. The combined number of the |
653 | + * maximum reseed threshold times the maximum |
654 | + * number of Jitter RNG transient errors is |
655 | + * less than the reseed threshold required by |
656 | + * SP800-90A allowing us to treat the |
657 | + * transient errors as such. |
658 | + * |
659 | + * However, we mandate that at least the first |
660 | + * seeding operation must succeed with the |
661 | + * Jitter RNG. |
662 | + */ |
663 | + if (!reseed || ret != -EAGAIN) |
664 | + goto out; |
665 | } |
666 | |
667 | drbg_string_fill(&data1, entropy, entropylen * 2); |
668 | @@ -1121,8 +1218,9 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, |
669 | memset(drbg->C, 0, drbg_statelen(drbg)); |
670 | } |
671 | |
672 | - ret = __drbg_seed(drbg, &seedlist, reseed); |
673 | + ret = __drbg_seed(drbg, &seedlist, reseed, new_seed_state); |
674 | |
675 | +out: |
676 | memzero_explicit(entropy, entropylen * 2); |
677 | |
678 | return ret; |
679 | @@ -1144,6 +1242,11 @@ static inline void drbg_dealloc_state(struct drbg_state *drbg) |
680 | drbg->reseed_ctr = 0; |
681 | drbg->d_ops = NULL; |
682 | drbg->core = NULL; |
683 | + if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) { |
684 | + kzfree(drbg->prev); |
685 | + drbg->prev = NULL; |
686 | + drbg->fips_primed = false; |
687 | + } |
688 | } |
689 | |
690 | /* |
691 | @@ -1213,6 +1316,14 @@ static inline int drbg_alloc_state(struct drbg_state *drbg) |
692 | drbg->scratchpad = PTR_ALIGN(drbg->scratchpadbuf, ret + 1); |
693 | } |
694 | |
695 | + if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) { |
696 | + drbg->prev = kzalloc(drbg_sec_strength(drbg->core->flags), |
697 | + GFP_KERNEL); |
698 | + if (!drbg->prev) |
699 | + goto fini; |
700 | + drbg->fips_primed = false; |
701 | + } |
702 | + |
703 | return 0; |
704 | |
705 | fini: |
706 | @@ -1285,19 +1396,25 @@ static int drbg_generate(struct drbg_state *drbg, |
707 | * here. The spec is a bit convoluted here, we make it simpler. |
708 | */ |
709 | if (drbg->reseed_threshold < drbg->reseed_ctr) |
710 | - drbg->seeded = false; |
711 | + drbg->seeded = DRBG_SEED_STATE_UNSEEDED; |
712 | |
713 | - if (drbg->pr || !drbg->seeded) { |
714 | + if (drbg->pr || drbg->seeded == DRBG_SEED_STATE_UNSEEDED) { |
715 | pr_devel("DRBG: reseeding before generation (prediction " |
716 | "resistance: %s, state %s)\n", |
717 | drbg->pr ? "true" : "false", |
718 | - drbg->seeded ? "seeded" : "unseeded"); |
719 | + (drbg->seeded == DRBG_SEED_STATE_FULL ? |
720 | + "seeded" : "unseeded")); |
721 | /* 9.3.1 steps 7.1 through 7.3 */ |
722 | len = drbg_seed(drbg, addtl, true); |
723 | if (len) |
724 | goto err; |
725 | /* 9.3.1 step 7.4 */ |
726 | addtl = NULL; |
727 | + } else if (rng_is_initialized() && |
728 | + drbg->seeded == DRBG_SEED_STATE_PARTIAL) { |
729 | + len = drbg_seed_from_random(drbg); |
730 | + if (len) |
731 | + goto err; |
732 | } |
733 | |
734 | if (addtl && 0 < addtl->len) |
735 | @@ -1390,51 +1507,15 @@ static int drbg_generate_long(struct drbg_state *drbg, |
736 | return 0; |
737 | } |
738 | |
739 | -static void drbg_schedule_async_seed(struct random_ready_callback *rdy) |
740 | -{ |
741 | - struct drbg_state *drbg = container_of(rdy, struct drbg_state, |
742 | - random_ready); |
743 | - |
744 | - schedule_work(&drbg->seed_work); |
745 | -} |
746 | - |
747 | static int drbg_prepare_hrng(struct drbg_state *drbg) |
748 | { |
749 | - int err; |
750 | - |
751 | /* We do not need an HRNG in test mode. */ |
752 | if (list_empty(&drbg->test_data.list)) |
753 | return 0; |
754 | |
755 | - INIT_WORK(&drbg->seed_work, drbg_async_seed); |
756 | - |
757 | - drbg->random_ready.owner = THIS_MODULE; |
758 | - drbg->random_ready.func = drbg_schedule_async_seed; |
759 | - |
760 | - err = add_random_ready_callback(&drbg->random_ready); |
761 | - |
762 | - switch (err) { |
763 | - case 0: |
764 | - break; |
765 | - |
766 | - case -EALREADY: |
767 | - err = 0; |
768 | - /* fall through */ |
769 | - |
770 | - default: |
771 | - drbg->random_ready.func = NULL; |
772 | - return err; |
773 | - } |
774 | - |
775 | drbg->jent = crypto_alloc_rng("jitterentropy_rng", 0, 0); |
776 | |
777 | - /* |
778 | - * Require frequent reseeds until the seed source is fully |
779 | - * initialized. |
780 | - */ |
781 | - drbg->reseed_threshold = 50; |
782 | - |
783 | - return err; |
784 | + return 0; |
785 | } |
786 | |
787 | /* |
788 | @@ -1477,7 +1558,7 @@ static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers, |
789 | if (!drbg->core) { |
790 | drbg->core = &drbg_cores[coreref]; |
791 | drbg->pr = pr; |
792 | - drbg->seeded = false; |
793 | + drbg->seeded = DRBG_SEED_STATE_UNSEEDED; |
794 | drbg->reseed_threshold = drbg_max_requests(drbg); |
795 | |
796 | ret = drbg_alloc_state(drbg); |
797 | @@ -1528,12 +1609,9 @@ free_everything: |
798 | */ |
799 | static int drbg_uninstantiate(struct drbg_state *drbg) |
800 | { |
801 | - if (drbg->random_ready.func) { |
802 | - del_random_ready_callback(&drbg->random_ready); |
803 | - cancel_work_sync(&drbg->seed_work); |
804 | + if (!IS_ERR_OR_NULL(drbg->jent)) |
805 | crypto_free_rng(drbg->jent); |
806 | - drbg->jent = NULL; |
807 | - } |
808 | + drbg->jent = NULL; |
809 | |
810 | if (drbg->d_ops) |
811 | drbg->d_ops->crypto_fini(drbg); |
812 | diff --git a/crypto/md4.c b/crypto/md4.c |
813 | index 3515af425cc91..810fefb0a007a 100644 |
814 | --- a/crypto/md4.c |
815 | +++ b/crypto/md4.c |
816 | @@ -64,23 +64,6 @@ static inline u32 H(u32 x, u32 y, u32 z) |
817 | #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s)) |
818 | #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s)) |
819 | |
820 | -/* XXX: this stuff can be optimized */ |
821 | -static inline void le32_to_cpu_array(u32 *buf, unsigned int words) |
822 | -{ |
823 | - while (words--) { |
824 | - __le32_to_cpus(buf); |
825 | - buf++; |
826 | - } |
827 | -} |
828 | - |
829 | -static inline void cpu_to_le32_array(u32 *buf, unsigned int words) |
830 | -{ |
831 | - while (words--) { |
832 | - __cpu_to_le32s(buf); |
833 | - buf++; |
834 | - } |
835 | -} |
836 | - |
837 | static void md4_transform(u32 *hash, u32 const *in) |
838 | { |
839 | u32 a, b, c, d; |
840 | diff --git a/crypto/md5.c b/crypto/md5.c |
841 | index 2355a7c25c458..419f2379b406b 100644 |
842 | --- a/crypto/md5.c |
843 | +++ b/crypto/md5.c |
844 | @@ -30,23 +30,6 @@ const u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = { |
845 | }; |
846 | EXPORT_SYMBOL_GPL(md5_zero_message_hash); |
847 | |
848 | -/* XXX: this stuff can be optimized */ |
849 | -static inline void le32_to_cpu_array(u32 *buf, unsigned int words) |
850 | -{ |
851 | - while (words--) { |
852 | - __le32_to_cpus(buf); |
853 | - buf++; |
854 | - } |
855 | -} |
856 | - |
857 | -static inline void cpu_to_le32_array(u32 *buf, unsigned int words) |
858 | -{ |
859 | - while (words--) { |
860 | - __cpu_to_le32s(buf); |
861 | - buf++; |
862 | - } |
863 | -} |
864 | - |
865 | static inline void md5_transform_helper(struct md5_state *ctx) |
866 | { |
867 | le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32)); |
868 | diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c |
869 | index 35db918a1de56..42f0a592b5ab0 100644 |
870 | --- a/drivers/ata/libata-core.c |
871 | +++ b/drivers/ata/libata-core.c |
872 | @@ -6051,7 +6051,7 @@ struct ata_host *ata_host_alloc_pinfo(struct device *dev, |
873 | const struct ata_port_info * const * ppi, |
874 | int n_ports) |
875 | { |
876 | - const struct ata_port_info *pi; |
877 | + const struct ata_port_info *pi = &ata_dummy_port_info; |
878 | struct ata_host *host; |
879 | int i, j; |
880 | |
881 | @@ -6059,7 +6059,7 @@ struct ata_host *ata_host_alloc_pinfo(struct device *dev, |
882 | if (!host) |
883 | return NULL; |
884 | |
885 | - for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) { |
886 | + for (i = 0, j = 0; i < host->n_ports; i++) { |
887 | struct ata_port *ap = host->ports[i]; |
888 | |
889 | if (ppi[j]) |
890 | diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig |
891 | index f4ae000eb2855..1d3813d4f5c81 100644 |
892 | --- a/drivers/char/Kconfig |
893 | +++ b/drivers/char/Kconfig |
894 | @@ -593,5 +593,41 @@ config TILE_SROM |
895 | |
896 | source "drivers/char/xillybus/Kconfig" |
897 | |
898 | -endmenu |
899 | +config RANDOM_TRUST_CPU |
900 | + bool "Initialize RNG using CPU RNG instructions" |
901 | + default y |
902 | + depends on ARCH_RANDOM |
903 | + help |
904 | + Initialize the RNG using random numbers supplied by the CPU's |
905 | + RNG instructions (e.g. RDRAND), if supported and available. These |
906 | + random numbers are never used directly, but are rather hashed into |
907 | + the main input pool, and this happens regardless of whether or not |
908 | + this option is enabled. Instead, this option controls whether the |
909 | + they are credited and hence can initialize the RNG. Additionally, |
910 | + other sources of randomness are always used, regardless of this |
911 | + setting. Enabling this implies trusting that the CPU can supply high |
912 | + quality and non-backdoored random numbers. |
913 | + |
914 | + Say Y here unless you have reason to mistrust your CPU or believe |
915 | + its RNG facilities may be faulty. This may also be configured at |
916 | + boot time with "random.trust_cpu=on/off". |
917 | + |
918 | +config RANDOM_TRUST_BOOTLOADER |
919 | + bool "Initialize RNG using bootloader-supplied seed" |
920 | + default y |
921 | + help |
922 | + Initialize the RNG using a seed supplied by the bootloader or boot |
923 | + environment (e.g. EFI or a bootloader-generated device tree). This |
924 | + seed is not used directly, but is rather hashed into the main input |
925 | + pool, and this happens regardless of whether or not this option is |
926 | + enabled. Instead, this option controls whether the seed is credited |
927 | + and hence can initialize the RNG. Additionally, other sources of |
928 | + randomness are always used, regardless of this setting. Enabling |
929 | + this implies trusting that the bootloader can supply high quality and |
930 | + non-backdoored seeds. |
931 | + |
932 | + Say Y here unless you have reason to mistrust your bootloader or |
933 | + believe its RNG facilities may be faulty. This may also be configured |
934 | + at boot time with "random.trust_bootloader=on/off". |
935 | |
936 | +endmenu |
937 | diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c |
938 | index 5e79b4bfe27a9..f1121a162ef02 100644 |
939 | --- a/drivers/char/hw_random/core.c |
940 | +++ b/drivers/char/hw_random/core.c |
941 | @@ -1,38 +1,18 @@ |
942 | /* |
943 | - Added support for the AMD Geode LX RNG |
944 | - (c) Copyright 2004-2005 Advanced Micro Devices, Inc. |
945 | - |
946 | - derived from |
947 | - |
948 | - Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG) |
949 | - (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com> |
950 | - |
951 | - derived from |
952 | - |
953 | - Hardware driver for the AMD 768 Random Number Generator (RNG) |
954 | - (c) Copyright 2001 Red Hat Inc <alan@redhat.com> |
955 | - |
956 | - derived from |
957 | - |
958 | - Hardware driver for Intel i810 Random Number Generator (RNG) |
959 | - Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com> |
960 | - Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com> |
961 | - |
962 | - Added generic RNG API |
963 | - Copyright 2006 Michael Buesch <m@bues.ch> |
964 | - Copyright 2005 (c) MontaVista Software, Inc. |
965 | - |
966 | - Please read Documentation/hw_random.txt for details on use. |
967 | - |
968 | - ---------------------------------------------------------- |
969 | - This software may be used and distributed according to the terms |
970 | - of the GNU General Public License, incorporated herein by reference. |
971 | - |
972 | + * hw_random/core.c: HWRNG core API |
973 | + * |
974 | + * Copyright 2006 Michael Buesch <m@bues.ch> |
975 | + * Copyright 2005 (c) MontaVista Software, Inc. |
976 | + * |
977 | + * Please read Documentation/hw_random.txt for details on use. |
978 | + * |
979 | + * This software may be used and distributed according to the terms |
980 | + * of the GNU General Public License, incorporated herein by reference. |
981 | */ |
982 | |
983 | - |
984 | #include <linux/device.h> |
985 | #include <linux/hw_random.h> |
986 | +#include <linux/random.h> |
987 | #include <linux/module.h> |
988 | #include <linux/kernel.h> |
989 | #include <linux/fs.h> |
990 | @@ -45,14 +25,13 @@ |
991 | #include <linux/err.h> |
992 | #include <asm/uaccess.h> |
993 | |
994 | - |
995 | #define RNG_MODULE_NAME "hw_random" |
996 | -#define PFX RNG_MODULE_NAME ": " |
997 | -#define RNG_MISCDEV_MINOR 183 /* official */ |
998 | - |
999 | |
1000 | static struct hwrng *current_rng; |
1001 | +/* the current rng has been explicitly chosen by user via sysfs */ |
1002 | +static int cur_rng_set_by_user; |
1003 | static struct task_struct *hwrng_fill; |
1004 | +/* list of registered rngs, sorted decending by quality */ |
1005 | static LIST_HEAD(rng_list); |
1006 | /* Protects rng_list and current_rng */ |
1007 | static DEFINE_MUTEX(rng_mutex); |
1008 | @@ -296,7 +275,6 @@ out_put: |
1009 | goto out; |
1010 | } |
1011 | |
1012 | - |
1013 | static const struct file_operations rng_chrdev_ops = { |
1014 | .owner = THIS_MODULE, |
1015 | .open = rng_dev_open, |
1016 | @@ -307,14 +285,13 @@ static const struct file_operations rng_chrdev_ops = { |
1017 | static const struct attribute_group *rng_dev_groups[]; |
1018 | |
1019 | static struct miscdevice rng_miscdev = { |
1020 | - .minor = RNG_MISCDEV_MINOR, |
1021 | + .minor = HWRNG_MINOR, |
1022 | .name = RNG_MODULE_NAME, |
1023 | .nodename = "hwrng", |
1024 | .fops = &rng_chrdev_ops, |
1025 | .groups = rng_dev_groups, |
1026 | }; |
1027 | |
1028 | - |
1029 | static ssize_t hwrng_attr_current_store(struct device *dev, |
1030 | struct device_attribute *attr, |
1031 | const char *buf, size_t len) |
1032 | @@ -329,6 +306,7 @@ static ssize_t hwrng_attr_current_store(struct device *dev, |
1033 | list_for_each_entry(rng, &rng_list, list) { |
1034 | if (sysfs_streq(rng->name, buf)) { |
1035 | err = 0; |
1036 | + cur_rng_set_by_user = 1; |
1037 | if (rng != current_rng) |
1038 | err = set_current_rng(rng); |
1039 | break; |
1040 | @@ -377,16 +355,27 @@ static ssize_t hwrng_attr_available_show(struct device *dev, |
1041 | return strlen(buf); |
1042 | } |
1043 | |
1044 | +static ssize_t hwrng_attr_selected_show(struct device *dev, |
1045 | + struct device_attribute *attr, |
1046 | + char *buf) |
1047 | +{ |
1048 | + return snprintf(buf, PAGE_SIZE, "%d\n", cur_rng_set_by_user); |
1049 | +} |
1050 | + |
1051 | static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR, |
1052 | hwrng_attr_current_show, |
1053 | hwrng_attr_current_store); |
1054 | static DEVICE_ATTR(rng_available, S_IRUGO, |
1055 | hwrng_attr_available_show, |
1056 | NULL); |
1057 | +static DEVICE_ATTR(rng_selected, S_IRUGO, |
1058 | + hwrng_attr_selected_show, |
1059 | + NULL); |
1060 | |
1061 | static struct attribute *rng_dev_attrs[] = { |
1062 | &dev_attr_rng_current.attr, |
1063 | &dev_attr_rng_available.attr, |
1064 | + &dev_attr_rng_selected.attr, |
1065 | NULL |
1066 | }; |
1067 | |
1068 | @@ -443,9 +432,9 @@ int hwrng_register(struct hwrng *rng) |
1069 | { |
1070 | int err = -EINVAL; |
1071 | struct hwrng *old_rng, *tmp; |
1072 | + struct list_head *rng_list_ptr; |
1073 | |
1074 | - if (rng->name == NULL || |
1075 | - (rng->data_read == NULL && rng->read == NULL)) |
1076 | + if (!rng->name || (!rng->data_read && !rng->read)) |
1077 | goto out; |
1078 | |
1079 | mutex_lock(&rng_mutex); |
1080 | @@ -459,14 +448,27 @@ int hwrng_register(struct hwrng *rng) |
1081 | init_completion(&rng->cleanup_done); |
1082 | complete(&rng->cleanup_done); |
1083 | |
1084 | + /* rng_list is sorted by decreasing quality */ |
1085 | + list_for_each(rng_list_ptr, &rng_list) { |
1086 | + tmp = list_entry(rng_list_ptr, struct hwrng, list); |
1087 | + if (tmp->quality < rng->quality) |
1088 | + break; |
1089 | + } |
1090 | + list_add_tail(&rng->list, rng_list_ptr); |
1091 | + |
1092 | old_rng = current_rng; |
1093 | err = 0; |
1094 | - if (!old_rng) { |
1095 | + if (!old_rng || |
1096 | + (!cur_rng_set_by_user && rng->quality > old_rng->quality)) { |
1097 | + /* |
1098 | + * Set new rng as current as the new rng source |
1099 | + * provides better entropy quality and was not |
1100 | + * chosen by userspace. |
1101 | + */ |
1102 | err = set_current_rng(rng); |
1103 | if (err) |
1104 | goto out_unlock; |
1105 | } |
1106 | - list_add_tail(&rng->list, &rng_list); |
1107 | |
1108 | if (old_rng && !rng->init) { |
1109 | /* |
1110 | @@ -493,12 +495,13 @@ void hwrng_unregister(struct hwrng *rng) |
1111 | list_del(&rng->list); |
1112 | if (current_rng == rng) { |
1113 | drop_current_rng(); |
1114 | + cur_rng_set_by_user = 0; |
1115 | + /* rng_list is sorted by quality, use the best (=first) one */ |
1116 | if (!list_empty(&rng_list)) { |
1117 | - struct hwrng *tail; |
1118 | - |
1119 | - tail = list_entry(rng_list.prev, struct hwrng, list); |
1120 | + struct hwrng *new_rng; |
1121 | |
1122 | - set_current_rng(tail); |
1123 | + new_rng = list_entry(rng_list.next, struct hwrng, list); |
1124 | + set_current_rng(new_rng); |
1125 | } |
1126 | } |
1127 | |
1128 | diff --git a/drivers/char/random.c b/drivers/char/random.c |
1129 | index 70ee86e034fcd..8e701ea78b0da 100644 |
1130 | --- a/drivers/char/random.c |
1131 | +++ b/drivers/char/random.c |
1132 | @@ -1,239 +1,29 @@ |
1133 | +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) |
1134 | /* |
1135 | - * random.c -- A strong random number generator |
1136 | - * |
1137 | + * Copyright (C) 2017-2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
1138 | * Copyright Matt Mackall <mpm@selenic.com>, 2003, 2004, 2005 |
1139 | - * |
1140 | - * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All |
1141 | - * rights reserved. |
1142 | - * |
1143 | - * Redistribution and use in source and binary forms, with or without |
1144 | - * modification, are permitted provided that the following conditions |
1145 | - * are met: |
1146 | - * 1. Redistributions of source code must retain the above copyright |
1147 | - * notice, and the entire permission notice in its entirety, |
1148 | - * including the disclaimer of warranties. |
1149 | - * 2. Redistributions in binary form must reproduce the above copyright |
1150 | - * notice, this list of conditions and the following disclaimer in the |
1151 | - * documentation and/or other materials provided with the distribution. |
1152 | - * 3. The name of the author may not be used to endorse or promote |
1153 | - * products derived from this software without specific prior |
1154 | - * written permission. |
1155 | - * |
1156 | - * ALTERNATIVELY, this product may be distributed under the terms of |
1157 | - * the GNU General Public License, in which case the provisions of the GPL are |
1158 | - * required INSTEAD OF the above restrictions. (This clause is |
1159 | - * necessary due to a potential bad interaction between the GPL and |
1160 | - * the restrictions contained in a BSD-style copyright.) |
1161 | - * |
1162 | - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
1163 | - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
1164 | - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
1165 | - * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
1166 | - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
1167 | - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
1168 | - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
1169 | - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
1170 | - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
1171 | - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
1172 | - * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
1173 | - * DAMAGE. |
1174 | + * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All rights reserved. |
1175 | + * |
1176 | + * This driver produces cryptographically secure pseudorandom data. It is divided |
1177 | + * into roughly six sections, each with a section header: |
1178 | + * |
1179 | + * - Initialization and readiness waiting. |
1180 | + * - Fast key erasure RNG, the "crng". |
1181 | + * - Entropy accumulation and extraction routines. |
1182 | + * - Entropy collection routines. |
1183 | + * - Userspace reader/writer interfaces. |
1184 | + * - Sysctl interface. |
1185 | + * |
1186 | + * The high level overview is that there is one input pool, into which |
1187 | + * various pieces of data are hashed. Prior to initialization, some of that |
1188 | + * data is then "credited" as having a certain number of bits of entropy. |
1189 | + * When enough bits of entropy are available, the hash is finalized and |
1190 | + * handed as a key to a stream cipher that expands it indefinitely for |
1191 | + * various consumers. This key is periodically refreshed as the various |
1192 | + * entropy collectors, described below, add data to the input pool. |
1193 | */ |
1194 | |
1195 | -/* |
1196 | - * (now, with legal B.S. out of the way.....) |
1197 | - * |
1198 | - * This routine gathers environmental noise from device drivers, etc., |
1199 | - * and returns good random numbers, suitable for cryptographic use. |
1200 | - * Besides the obvious cryptographic uses, these numbers are also good |
1201 | - * for seeding TCP sequence numbers, and other places where it is |
1202 | - * desirable to have numbers which are not only random, but hard to |
1203 | - * predict by an attacker. |
1204 | - * |
1205 | - * Theory of operation |
1206 | - * =================== |
1207 | - * |
1208 | - * Computers are very predictable devices. Hence it is extremely hard |
1209 | - * to produce truly random numbers on a computer --- as opposed to |
1210 | - * pseudo-random numbers, which can easily generated by using a |
1211 | - * algorithm. Unfortunately, it is very easy for attackers to guess |
1212 | - * the sequence of pseudo-random number generators, and for some |
1213 | - * applications this is not acceptable. So instead, we must try to |
1214 | - * gather "environmental noise" from the computer's environment, which |
1215 | - * must be hard for outside attackers to observe, and use that to |
1216 | - * generate random numbers. In a Unix environment, this is best done |
1217 | - * from inside the kernel. |
1218 | - * |
1219 | - * Sources of randomness from the environment include inter-keyboard |
1220 | - * timings, inter-interrupt timings from some interrupts, and other |
1221 | - * events which are both (a) non-deterministic and (b) hard for an |
1222 | - * outside observer to measure. Randomness from these sources are |
1223 | - * added to an "entropy pool", which is mixed using a CRC-like function. |
1224 | - * This is not cryptographically strong, but it is adequate assuming |
1225 | - * the randomness is not chosen maliciously, and it is fast enough that |
1226 | - * the overhead of doing it on every interrupt is very reasonable. |
1227 | - * As random bytes are mixed into the entropy pool, the routines keep |
1228 | - * an *estimate* of how many bits of randomness have been stored into |
1229 | - * the random number generator's internal state. |
1230 | - * |
1231 | - * When random bytes are desired, they are obtained by taking the SHA |
1232 | - * hash of the contents of the "entropy pool". The SHA hash avoids |
1233 | - * exposing the internal state of the entropy pool. It is believed to |
1234 | - * be computationally infeasible to derive any useful information |
1235 | - * about the input of SHA from its output. Even if it is possible to |
1236 | - * analyze SHA in some clever way, as long as the amount of data |
1237 | - * returned from the generator is less than the inherent entropy in |
1238 | - * the pool, the output data is totally unpredictable. For this |
1239 | - * reason, the routine decreases its internal estimate of how many |
1240 | - * bits of "true randomness" are contained in the entropy pool as it |
1241 | - * outputs random numbers. |
1242 | - * |
1243 | - * If this estimate goes to zero, the routine can still generate |
1244 | - * random numbers; however, an attacker may (at least in theory) be |
1245 | - * able to infer the future output of the generator from prior |
1246 | - * outputs. This requires successful cryptanalysis of SHA, which is |
1247 | - * not believed to be feasible, but there is a remote possibility. |
1248 | - * Nonetheless, these numbers should be useful for the vast majority |
1249 | - * of purposes. |
1250 | - * |
1251 | - * Exported interfaces ---- output |
1252 | - * =============================== |
1253 | - * |
1254 | - * There are three exported interfaces; the first is one designed to |
1255 | - * be used from within the kernel: |
1256 | - * |
1257 | - * void get_random_bytes(void *buf, int nbytes); |
1258 | - * |
1259 | - * This interface will return the requested number of random bytes, |
1260 | - * and place it in the requested buffer. |
1261 | - * |
1262 | - * The two other interfaces are two character devices /dev/random and |
1263 | - * /dev/urandom. /dev/random is suitable for use when very high |
1264 | - * quality randomness is desired (for example, for key generation or |
1265 | - * one-time pads), as it will only return a maximum of the number of |
1266 | - * bits of randomness (as estimated by the random number generator) |
1267 | - * contained in the entropy pool. |
1268 | - * |
1269 | - * The /dev/urandom device does not have this limit, and will return |
1270 | - * as many bytes as are requested. As more and more random bytes are |
1271 | - * requested without giving time for the entropy pool to recharge, |
1272 | - * this will result in random numbers that are merely cryptographically |
1273 | - * strong. For many applications, however, this is acceptable. |
1274 | - * |
1275 | - * Exported interfaces ---- input |
1276 | - * ============================== |
1277 | - * |
1278 | - * The current exported interfaces for gathering environmental noise |
1279 | - * from the devices are: |
1280 | - * |
1281 | - * void add_device_randomness(const void *buf, unsigned int size); |
1282 | - * void add_input_randomness(unsigned int type, unsigned int code, |
1283 | - * unsigned int value); |
1284 | - * void add_interrupt_randomness(int irq, int irq_flags); |
1285 | - * void add_disk_randomness(struct gendisk *disk); |
1286 | - * |
1287 | - * add_device_randomness() is for adding data to the random pool that |
1288 | - * is likely to differ between two devices (or possibly even per boot). |
1289 | - * This would be things like MAC addresses or serial numbers, or the |
1290 | - * read-out of the RTC. This does *not* add any actual entropy to the |
1291 | - * pool, but it initializes the pool to different values for devices |
1292 | - * that might otherwise be identical and have very little entropy |
1293 | - * available to them (particularly common in the embedded world). |
1294 | - * |
1295 | - * add_input_randomness() uses the input layer interrupt timing, as well as |
1296 | - * the event type information from the hardware. |
1297 | - * |
1298 | - * add_interrupt_randomness() uses the interrupt timing as random |
1299 | - * inputs to the entropy pool. Using the cycle counters and the irq source |
1300 | - * as inputs, it feeds the randomness roughly once a second. |
1301 | - * |
1302 | - * add_disk_randomness() uses what amounts to the seek time of block |
1303 | - * layer request events, on a per-disk_devt basis, as input to the |
1304 | - * entropy pool. Note that high-speed solid state drives with very low |
1305 | - * seek times do not make for good sources of entropy, as their seek |
1306 | - * times are usually fairly consistent. |
1307 | - * |
1308 | - * All of these routines try to estimate how many bits of randomness a |
1309 | - * particular randomness source. They do this by keeping track of the |
1310 | - * first and second order deltas of the event timings. |
1311 | - * |
1312 | - * Ensuring unpredictability at system startup |
1313 | - * ============================================ |
1314 | - * |
1315 | - * When any operating system starts up, it will go through a sequence |
1316 | - * of actions that are fairly predictable by an adversary, especially |
1317 | - * if the start-up does not involve interaction with a human operator. |
1318 | - * This reduces the actual number of bits of unpredictability in the |
1319 | - * entropy pool below the value in entropy_count. In order to |
1320 | - * counteract this effect, it helps to carry information in the |
1321 | - * entropy pool across shut-downs and start-ups. To do this, put the |
1322 | - * following lines an appropriate script which is run during the boot |
1323 | - * sequence: |
1324 | - * |
1325 | - * echo "Initializing random number generator..." |
1326 | - * random_seed=/var/run/random-seed |
1327 | - * # Carry a random seed from start-up to start-up |
1328 | - * # Load and then save the whole entropy pool |
1329 | - * if [ -f $random_seed ]; then |
1330 | - * cat $random_seed >/dev/urandom |
1331 | - * else |
1332 | - * touch $random_seed |
1333 | - * fi |
1334 | - * chmod 600 $random_seed |
1335 | - * dd if=/dev/urandom of=$random_seed count=1 bs=512 |
1336 | - * |
1337 | - * and the following lines in an appropriate script which is run as |
1338 | - * the system is shutdown: |
1339 | - * |
1340 | - * # Carry a random seed from shut-down to start-up |
1341 | - * # Save the whole entropy pool |
1342 | - * echo "Saving random seed..." |
1343 | - * random_seed=/var/run/random-seed |
1344 | - * touch $random_seed |
1345 | - * chmod 600 $random_seed |
1346 | - * dd if=/dev/urandom of=$random_seed count=1 bs=512 |
1347 | - * |
1348 | - * For example, on most modern systems using the System V init |
1349 | - * scripts, such code fragments would be found in |
1350 | - * /etc/rc.d/init.d/random. On older Linux systems, the correct script |
1351 | - * location might be in /etc/rcb.d/rc.local or /etc/rc.d/rc.0. |
1352 | - * |
1353 | - * Effectively, these commands cause the contents of the entropy pool |
1354 | - * to be saved at shut-down time and reloaded into the entropy pool at |
1355 | - * start-up. (The 'dd' in the addition to the bootup script is to |
1356 | - * make sure that /etc/random-seed is different for every start-up, |
1357 | - * even if the system crashes without executing rc.0.) Even with |
1358 | - * complete knowledge of the start-up activities, predicting the state |
1359 | - * of the entropy pool requires knowledge of the previous history of |
1360 | - * the system. |
1361 | - * |
1362 | - * Configuring the /dev/random driver under Linux |
1363 | - * ============================================== |
1364 | - * |
1365 | - * The /dev/random driver under Linux uses minor numbers 8 and 9 of |
1366 | - * the /dev/mem major number (#1). So if your system does not have |
1367 | - * /dev/random and /dev/urandom created already, they can be created |
1368 | - * by using the commands: |
1369 | - * |
1370 | - * mknod /dev/random c 1 8 |
1371 | - * mknod /dev/urandom c 1 9 |
1372 | - * |
1373 | - * Acknowledgements: |
1374 | - * ================= |
1375 | - * |
1376 | - * Ideas for constructing this random number generator were derived |
1377 | - * from Pretty Good Privacy's random number generator, and from private |
1378 | - * discussions with Phil Karn. Colin Plumb provided a faster random |
1379 | - * number generator, which speed up the mixing function of the entropy |
1380 | - * pool, taken from PGPfone. Dale Worley has also contributed many |
1381 | - * useful ideas and suggestions to improve this driver. |
1382 | - * |
1383 | - * Any flaws in the design are solely my responsibility, and should |
1384 | - * not be attributed to the Phil, Colin, or any of authors of PGP. |
1385 | - * |
1386 | - * Further background information on this topic may be obtained from |
1387 | - * RFC 1750, "Randomness Recommendations for Security", by Donald |
1388 | - * Eastlake, Steve Crocker, and Jeff Schiller. |
1389 | - */ |
1390 | +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1391 | |
1392 | #include <linux/utsname.h> |
1393 | #include <linux/module.h> |
1394 | @@ -253,8 +43,6 @@ |
1395 | #include <linux/spinlock.h> |
1396 | #include <linux/kthread.h> |
1397 | #include <linux/percpu.h> |
1398 | -#include <linux/cryptohash.h> |
1399 | -#include <linux/fips.h> |
1400 | #include <linux/ptrace.h> |
1401 | #include <linux/kmemcheck.h> |
1402 | #include <linux/workqueue.h> |
1403 | @@ -263,1480 +51,1067 @@ |
1404 | #include <linux/syscalls.h> |
1405 | #include <linux/completion.h> |
1406 | #include <linux/uuid.h> |
1407 | +#include <linux/siphash.h> |
1408 | +#include <linux/uio.h> |
1409 | #include <crypto/chacha20.h> |
1410 | - |
1411 | +#include <crypto/blake2s.h> |
1412 | #include <asm/processor.h> |
1413 | #include <asm/uaccess.h> |
1414 | #include <asm/irq.h> |
1415 | #include <asm/irq_regs.h> |
1416 | #include <asm/io.h> |
1417 | |
1418 | -#define CREATE_TRACE_POINTS |
1419 | -#include <trace/events/random.h> |
1420 | - |
1421 | -/* #define ADD_INTERRUPT_BENCH */ |
1422 | +/********************************************************************* |
1423 | + * |
1424 | + * Initialization and readiness waiting. |
1425 | + * |
1426 | + * Much of the RNG infrastructure is devoted to various dependencies |
1427 | + * being able to wait until the RNG has collected enough entropy and |
1428 | + * is ready for safe consumption. |
1429 | + * |
1430 | + *********************************************************************/ |
1431 | |
1432 | /* |
1433 | - * Configuration information |
1434 | + * crng_init is protected by base_crng->lock, and only increases |
1435 | + * its value (from empty->early->ready). |
1436 | */ |
1437 | -#define INPUT_POOL_SHIFT 12 |
1438 | -#define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5)) |
1439 | -#define OUTPUT_POOL_SHIFT 10 |
1440 | -#define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5)) |
1441 | -#define SEC_XFER_SIZE 512 |
1442 | -#define EXTRACT_SIZE 10 |
1443 | - |
1444 | -#define DEBUG_RANDOM_BOOT 0 |
1445 | +static enum { |
1446 | + CRNG_EMPTY = 0, /* Little to no entropy collected */ |
1447 | + CRNG_EARLY = 1, /* At least POOL_EARLY_BITS collected */ |
1448 | + CRNG_READY = 2 /* Fully initialized with POOL_READY_BITS collected */ |
1449 | +} crng_init __read_mostly = CRNG_EMPTY; |
1450 | +#define crng_ready() (likely(crng_init >= CRNG_READY)) |
1451 | +/* Various types of waiters for crng_init->CRNG_READY transition. */ |
1452 | +static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); |
1453 | +static struct fasync_struct *fasync; |
1454 | +static DEFINE_SPINLOCK(random_ready_chain_lock); |
1455 | +static RAW_NOTIFIER_HEAD(random_ready_chain); |
1456 | |
1457 | -#define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long)) |
1458 | +/* Control how we warn userspace. */ |
1459 | +static struct ratelimit_state urandom_warning = |
1460 | + RATELIMIT_STATE_INIT("warn_urandom_randomness", HZ, 3); |
1461 | +static int ratelimit_disable __read_mostly = |
1462 | + IS_ENABLED(CONFIG_WARN_ALL_UNSEEDED_RANDOM); |
1463 | +module_param_named(ratelimit_disable, ratelimit_disable, int, 0644); |
1464 | +MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression"); |
1465 | |
1466 | /* |
1467 | - * To allow fractional bits to be tracked, the entropy_count field is |
1468 | - * denominated in units of 1/8th bits. |
1469 | + * Returns whether or not the input pool has been seeded and thus guaranteed |
1470 | + * to supply cryptographically secure random numbers. This applies to: the |
1471 | + * /dev/urandom device, the get_random_bytes function, and the get_random_{u32, |
1472 | + * ,u64,int,long} family of functions. |
1473 | * |
1474 | - * 2*(ENTROPY_SHIFT + log2(poolbits)) must <= 31, or the multiply in |
1475 | - * credit_entropy_bits() needs to be 64 bits wide. |
1476 | + * Returns: true if the input pool has been seeded. |
1477 | + * false if the input pool has not been seeded. |
1478 | */ |
1479 | -#define ENTROPY_SHIFT 3 |
1480 | -#define ENTROPY_BITS(r) ((r)->entropy_count >> ENTROPY_SHIFT) |
1481 | +bool rng_is_initialized(void) |
1482 | +{ |
1483 | + return crng_ready(); |
1484 | +} |
1485 | +EXPORT_SYMBOL(rng_is_initialized); |
1486 | |
1487 | -/* |
1488 | - * The minimum number of bits of entropy before we wake up a read on |
1489 | - * /dev/random. Should be enough to do a significant reseed. |
1490 | - */ |
1491 | -static int random_read_wakeup_bits = 64; |
1492 | +/* Used by wait_for_random_bytes(), and considered an entropy collector, below. */ |
1493 | +static void try_to_generate_entropy(void); |
1494 | |
1495 | /* |
1496 | - * If the entropy count falls under this number of bits, then we |
1497 | - * should wake up processes which are selecting or polling on write |
1498 | - * access to /dev/random. |
1499 | + * Wait for the input pool to be seeded and thus guaranteed to supply |
1500 | + * cryptographically secure random numbers. This applies to: the /dev/urandom |
1501 | + * device, the get_random_bytes function, and the get_random_{u32,u64,int,long} |
1502 | + * family of functions. Using any of these functions without first calling |
1503 | + * this function forfeits the guarantee of security. |
1504 | + * |
1505 | + * Returns: 0 if the input pool has been seeded. |
1506 | + * -ERESTARTSYS if the function was interrupted by a signal. |
1507 | */ |
1508 | -static int random_write_wakeup_bits = 28 * OUTPUT_POOL_WORDS; |
1509 | +int wait_for_random_bytes(void) |
1510 | +{ |
1511 | + while (!crng_ready()) { |
1512 | + int ret; |
1513 | |
1514 | -/* |
1515 | - * The minimum number of seconds between urandom pool reseeding. We |
1516 | - * do this to limit the amount of entropy that can be drained from the |
1517 | - * input pool even if there are heavy demands on /dev/urandom. |
1518 | - */ |
1519 | -static int random_min_urandom_seed = 60; |
1520 | + try_to_generate_entropy(); |
1521 | + ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ); |
1522 | + if (ret) |
1523 | + return ret > 0 ? 0 : ret; |
1524 | + } |
1525 | + return 0; |
1526 | +} |
1527 | +EXPORT_SYMBOL(wait_for_random_bytes); |
1528 | |
1529 | /* |
1530 | - * Originally, we used a primitive polynomial of degree .poolwords |
1531 | - * over GF(2). The taps for various sizes are defined below. They |
1532 | - * were chosen to be evenly spaced except for the last tap, which is 1 |
1533 | - * to get the twisting happening as fast as possible. |
1534 | - * |
1535 | - * For the purposes of better mixing, we use the CRC-32 polynomial as |
1536 | - * well to make a (modified) twisted Generalized Feedback Shift |
1537 | - * Register. (See M. Matsumoto & Y. Kurita, 1992. Twisted GFSR |
1538 | - * generators. ACM Transactions on Modeling and Computer Simulation |
1539 | - * 2(3):179-194. Also see M. Matsumoto & Y. Kurita, 1994. Twisted |
1540 | - * GFSR generators II. ACM Transactions on Modeling and Computer |
1541 | - * Simulation 4:254-266) |
1542 | - * |
1543 | - * Thanks to Colin Plumb for suggesting this. |
1544 | - * |
1545 | - * The mixing operation is much less sensitive than the output hash, |
1546 | - * where we use SHA-1. All that we want of mixing operation is that |
1547 | - * it be a good non-cryptographic hash; i.e. it not produce collisions |
1548 | - * when fed "random" data of the sort we expect to see. As long as |
1549 | - * the pool state differs for different inputs, we have preserved the |
1550 | - * input entropy and done a good job. The fact that an intelligent |
1551 | - * attacker can construct inputs that will produce controlled |
1552 | - * alterations to the pool's state is not important because we don't |
1553 | - * consider such inputs to contribute any randomness. The only |
1554 | - * property we need with respect to them is that the attacker can't |
1555 | - * increase his/her knowledge of the pool's state. Since all |
1556 | - * additions are reversible (knowing the final state and the input, |
1557 | - * you can reconstruct the initial state), if an attacker has any |
1558 | - * uncertainty about the initial state, he/she can only shuffle that |
1559 | - * uncertainty about, but never cause any collisions (which would |
1560 | - * decrease the uncertainty). |
1561 | + * Add a callback function that will be invoked when the input |
1562 | + * pool is initialised. |
1563 | * |
1564 | - * Our mixing functions were analyzed by Lacharme, Roeck, Strubel, and |
1565 | - * Videau in their paper, "The Linux Pseudorandom Number Generator |
1566 | - * Revisited" (see: http://eprint.iacr.org/2012/251.pdf). In their |
1567 | - * paper, they point out that we are not using a true Twisted GFSR, |
1568 | - * since Matsumoto & Kurita used a trinomial feedback polynomial (that |
1569 | - * is, with only three taps, instead of the six that we are using). |
1570 | - * As a result, the resulting polynomial is neither primitive nor |
1571 | - * irreducible, and hence does not have a maximal period over |
1572 | - * GF(2**32). They suggest a slight change to the generator |
1573 | - * polynomial which improves the resulting TGFSR polynomial to be |
1574 | - * irreducible, which we have made here. |
1575 | + * returns: 0 if callback is successfully added |
1576 | + * -EALREADY if pool is already initialised (callback not called) |
1577 | */ |
1578 | -static struct poolinfo { |
1579 | - int poolbitshift, poolwords, poolbytes, poolbits, poolfracbits; |
1580 | -#define S(x) ilog2(x)+5, (x), (x)*4, (x)*32, (x) << (ENTROPY_SHIFT+5) |
1581 | - int tap1, tap2, tap3, tap4, tap5; |
1582 | -} poolinfo_table[] = { |
1583 | - /* was: x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 */ |
1584 | - /* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ |
1585 | - { S(128), 104, 76, 51, 25, 1 }, |
1586 | - /* was: x^32 + x^26 + x^20 + x^14 + x^7 + x + 1 */ |
1587 | - /* x^32 + x^26 + x^19 + x^14 + x^7 + x + 1 */ |
1588 | - { S(32), 26, 19, 14, 7, 1 }, |
1589 | -#if 0 |
1590 | - /* x^2048 + x^1638 + x^1231 + x^819 + x^411 + x + 1 -- 115 */ |
1591 | - { S(2048), 1638, 1231, 819, 411, 1 }, |
1592 | - |
1593 | - /* x^1024 + x^817 + x^615 + x^412 + x^204 + x + 1 -- 290 */ |
1594 | - { S(1024), 817, 615, 412, 204, 1 }, |
1595 | - |
1596 | - /* x^1024 + x^819 + x^616 + x^410 + x^207 + x^2 + 1 -- 115 */ |
1597 | - { S(1024), 819, 616, 410, 207, 2 }, |
1598 | - |
1599 | - /* x^512 + x^411 + x^308 + x^208 + x^104 + x + 1 -- 225 */ |
1600 | - { S(512), 411, 308, 208, 104, 1 }, |
1601 | - |
1602 | - /* x^512 + x^409 + x^307 + x^206 + x^102 + x^2 + 1 -- 95 */ |
1603 | - { S(512), 409, 307, 206, 102, 2 }, |
1604 | - /* x^512 + x^409 + x^309 + x^205 + x^103 + x^2 + 1 -- 95 */ |
1605 | - { S(512), 409, 309, 205, 103, 2 }, |
1606 | - |
1607 | - /* x^256 + x^205 + x^155 + x^101 + x^52 + x + 1 -- 125 */ |
1608 | - { S(256), 205, 155, 101, 52, 1 }, |
1609 | - |
1610 | - /* x^128 + x^103 + x^78 + x^51 + x^27 + x^2 + 1 -- 70 */ |
1611 | - { S(128), 103, 78, 51, 27, 2 }, |
1612 | - |
1613 | - /* x^64 + x^52 + x^39 + x^26 + x^14 + x + 1 -- 15 */ |
1614 | - { S(64), 52, 39, 26, 14, 1 }, |
1615 | -#endif |
1616 | -}; |
1617 | +int __cold register_random_ready_notifier(struct notifier_block *nb) |
1618 | +{ |
1619 | + unsigned long flags; |
1620 | + int ret = -EALREADY; |
1621 | + |
1622 | + if (crng_ready()) |
1623 | + return ret; |
1624 | + |
1625 | + spin_lock_irqsave(&random_ready_chain_lock, flags); |
1626 | + if (!crng_ready()) |
1627 | + ret = raw_notifier_chain_register(&random_ready_chain, nb); |
1628 | + spin_unlock_irqrestore(&random_ready_chain_lock, flags); |
1629 | + return ret; |
1630 | +} |
1631 | |
1632 | /* |
1633 | - * Static global variables |
1634 | + * Delete a previously registered readiness callback function. |
1635 | */ |
1636 | -static DECLARE_WAIT_QUEUE_HEAD(random_read_wait); |
1637 | -static DECLARE_WAIT_QUEUE_HEAD(random_write_wait); |
1638 | -static DECLARE_WAIT_QUEUE_HEAD(urandom_init_wait); |
1639 | -static struct fasync_struct *fasync; |
1640 | - |
1641 | -static DEFINE_SPINLOCK(random_ready_list_lock); |
1642 | -static LIST_HEAD(random_ready_list); |
1643 | +int __cold unregister_random_ready_notifier(struct notifier_block *nb) |
1644 | +{ |
1645 | + unsigned long flags; |
1646 | + int ret; |
1647 | |
1648 | -struct crng_state { |
1649 | - __u32 state[16]; |
1650 | - unsigned long init_time; |
1651 | - spinlock_t lock; |
1652 | -}; |
1653 | + spin_lock_irqsave(&random_ready_chain_lock, flags); |
1654 | + ret = raw_notifier_chain_unregister(&random_ready_chain, nb); |
1655 | + spin_unlock_irqrestore(&random_ready_chain_lock, flags); |
1656 | + return ret; |
1657 | +} |
1658 | |
1659 | -struct crng_state primary_crng = { |
1660 | - .lock = __SPIN_LOCK_UNLOCKED(primary_crng.lock), |
1661 | -}; |
1662 | +static void __cold process_random_ready_list(void) |
1663 | +{ |
1664 | + unsigned long flags; |
1665 | |
1666 | -/* |
1667 | - * crng_init = 0 --> Uninitialized |
1668 | - * 1 --> Initialized |
1669 | - * 2 --> Initialized from input_pool |
1670 | - * |
1671 | - * crng_init is protected by primary_crng->lock, and only increases |
1672 | - * its value (from 0->1->2). |
1673 | - */ |
1674 | -static int crng_init = 0; |
1675 | -#define crng_ready() (likely(crng_init > 1)) |
1676 | -static int crng_init_cnt = 0; |
1677 | -static unsigned long crng_global_init_time = 0; |
1678 | -#define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE) |
1679 | -static void _extract_crng(struct crng_state *crng, |
1680 | - __u8 out[CHACHA20_BLOCK_SIZE]); |
1681 | -static void _crng_backtrack_protect(struct crng_state *crng, |
1682 | - __u8 tmp[CHACHA20_BLOCK_SIZE], int used); |
1683 | -static void process_random_ready_list(void); |
1684 | - |
1685 | -static struct ratelimit_state unseeded_warning = |
1686 | - RATELIMIT_STATE_INIT("warn_unseeded_randomness", HZ, 3); |
1687 | -static struct ratelimit_state urandom_warning = |
1688 | - RATELIMIT_STATE_INIT("warn_urandom_randomness", HZ, 3); |
1689 | + spin_lock_irqsave(&random_ready_chain_lock, flags); |
1690 | + raw_notifier_call_chain(&random_ready_chain, 0, NULL); |
1691 | + spin_unlock_irqrestore(&random_ready_chain_lock, flags); |
1692 | +} |
1693 | |
1694 | -static int ratelimit_disable __read_mostly; |
1695 | +#define warn_unseeded_randomness() \ |
1696 | + if (IS_ENABLED(CONFIG_WARN_ALL_UNSEEDED_RANDOM) && !crng_ready()) \ |
1697 | + pr_notice("%s called from %pS with crng_init=%d\n", \ |
1698 | + __func__, (void *)_RET_IP_, crng_init) |
1699 | |
1700 | -module_param_named(ratelimit_disable, ratelimit_disable, int, 0644); |
1701 | -MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression"); |
1702 | |
1703 | -/********************************************************************** |
1704 | +/********************************************************************* |
1705 | * |
1706 | - * OS independent entropy store. Here are the functions which handle |
1707 | - * storing entropy in an entropy pool. |
1708 | + * Fast key erasure RNG, the "crng". |
1709 | * |
1710 | - **********************************************************************/ |
1711 | + * These functions expand entropy from the entropy extractor into |
1712 | + * long streams for external consumption using the "fast key erasure" |
1713 | + * RNG described at <https://blog.cr.yp.to/20170723-random.html>. |
1714 | + * |
1715 | + * There are a few exported interfaces for use by other drivers: |
1716 | + * |
1717 | + * void get_random_bytes(void *buf, size_t len) |
1718 | + * u32 get_random_u32() |
1719 | + * u64 get_random_u64() |
1720 | + * unsigned int get_random_int() |
1721 | + * unsigned long get_random_long() |
1722 | + * |
1723 | + * These interfaces will return the requested number of random bytes |
1724 | + * into the given buffer or as a return value. This is equivalent to |
1725 | + * a read from /dev/urandom. The u32, u64, int, and long family of |
1726 | + * functions may be higher performance for one-off random integers, |
1727 | + * because they do a bit of buffering and do not invoke reseeding |
1728 | + * until the buffer is emptied. |
1729 | + * |
1730 | + *********************************************************************/ |
1731 | |
1732 | -struct entropy_store; |
1733 | -struct entropy_store { |
1734 | - /* read-only data: */ |
1735 | - const struct poolinfo *poolinfo; |
1736 | - __u32 *pool; |
1737 | - const char *name; |
1738 | - struct entropy_store *pull; |
1739 | - struct work_struct push_work; |
1740 | - |
1741 | - /* read-write data: */ |
1742 | - unsigned long last_pulled; |
1743 | - spinlock_t lock; |
1744 | - unsigned short add_ptr; |
1745 | - unsigned short input_rotate; |
1746 | - int entropy_count; |
1747 | - int entropy_total; |
1748 | - unsigned int initialized:1; |
1749 | - unsigned int limit:1; |
1750 | - unsigned int last_data_init:1; |
1751 | - __u8 last_data[EXTRACT_SIZE]; |
1752 | +enum { |
1753 | + CRNG_RESEED_START_INTERVAL = HZ, |
1754 | + CRNG_RESEED_INTERVAL = 60 * HZ |
1755 | }; |
1756 | |
1757 | -static ssize_t extract_entropy(struct entropy_store *r, void *buf, |
1758 | - size_t nbytes, int min, int rsvd); |
1759 | -static ssize_t _extract_entropy(struct entropy_store *r, void *buf, |
1760 | - size_t nbytes, int fips); |
1761 | - |
1762 | -static void crng_reseed(struct crng_state *crng, struct entropy_store *r); |
1763 | -static void push_to_pool(struct work_struct *work); |
1764 | -static __u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy; |
1765 | -static __u32 blocking_pool_data[OUTPUT_POOL_WORDS] __latent_entropy; |
1766 | - |
1767 | -static struct entropy_store input_pool = { |
1768 | - .poolinfo = &poolinfo_table[0], |
1769 | - .name = "input", |
1770 | - .limit = 1, |
1771 | - .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), |
1772 | - .pool = input_pool_data |
1773 | +static struct { |
1774 | + u8 key[CHACHA20_KEY_SIZE] __aligned(__alignof__(long)); |
1775 | + unsigned long birth; |
1776 | + unsigned long generation; |
1777 | + spinlock_t lock; |
1778 | +} base_crng = { |
1779 | + .lock = __SPIN_LOCK_UNLOCKED(base_crng.lock) |
1780 | }; |
1781 | |
1782 | -static struct entropy_store blocking_pool = { |
1783 | - .poolinfo = &poolinfo_table[1], |
1784 | - .name = "blocking", |
1785 | - .limit = 1, |
1786 | - .pull = &input_pool, |
1787 | - .lock = __SPIN_LOCK_UNLOCKED(blocking_pool.lock), |
1788 | - .pool = blocking_pool_data, |
1789 | - .push_work = __WORK_INITIALIZER(blocking_pool.push_work, |
1790 | - push_to_pool), |
1791 | +struct crng { |
1792 | + u8 key[CHACHA20_KEY_SIZE]; |
1793 | + unsigned long generation; |
1794 | }; |
1795 | |
1796 | -static __u32 const twist_table[8] = { |
1797 | - 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158, |
1798 | - 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 }; |
1799 | - |
1800 | -/* |
1801 | - * This function adds bytes into the entropy "pool". It does not |
1802 | - * update the entropy estimate. The caller should call |
1803 | - * credit_entropy_bits if this is appropriate. |
1804 | - * |
1805 | - * The pool is stirred with a primitive polynomial of the appropriate |
1806 | - * degree, and then twisted. We twist by three bits at a time because |
1807 | - * it's cheap to do so and helps slightly in the expected case where |
1808 | - * the entropy is concentrated in the low-order bits. |
1809 | - */ |
1810 | -static void _mix_pool_bytes(struct entropy_store *r, const void *in, |
1811 | - int nbytes) |
1812 | -{ |
1813 | - unsigned long i, tap1, tap2, tap3, tap4, tap5; |
1814 | - int input_rotate; |
1815 | - int wordmask = r->poolinfo->poolwords - 1; |
1816 | - const char *bytes = in; |
1817 | - __u32 w; |
1818 | - |
1819 | - tap1 = r->poolinfo->tap1; |
1820 | - tap2 = r->poolinfo->tap2; |
1821 | - tap3 = r->poolinfo->tap3; |
1822 | - tap4 = r->poolinfo->tap4; |
1823 | - tap5 = r->poolinfo->tap5; |
1824 | - |
1825 | - input_rotate = r->input_rotate; |
1826 | - i = r->add_ptr; |
1827 | - |
1828 | - /* mix one byte at a time to simplify size handling and churn faster */ |
1829 | - while (nbytes--) { |
1830 | - w = rol32(*bytes++, input_rotate); |
1831 | - i = (i - 1) & wordmask; |
1832 | - |
1833 | - /* XOR in the various taps */ |
1834 | - w ^= r->pool[i]; |
1835 | - w ^= r->pool[(i + tap1) & wordmask]; |
1836 | - w ^= r->pool[(i + tap2) & wordmask]; |
1837 | - w ^= r->pool[(i + tap3) & wordmask]; |
1838 | - w ^= r->pool[(i + tap4) & wordmask]; |
1839 | - w ^= r->pool[(i + tap5) & wordmask]; |
1840 | - |
1841 | - /* Mix the result back in with a twist */ |
1842 | - r->pool[i] = (w >> 3) ^ twist_table[w & 7]; |
1843 | - |
1844 | - /* |
1845 | - * Normally, we add 7 bits of rotation to the pool. |
1846 | - * At the beginning of the pool, add an extra 7 bits |
1847 | - * rotation, so that successive passes spread the |
1848 | - * input bits across the pool evenly. |
1849 | - */ |
1850 | - input_rotate = (input_rotate + (i ? 7 : 14)) & 31; |
1851 | - } |
1852 | - |
1853 | - r->input_rotate = input_rotate; |
1854 | - r->add_ptr = i; |
1855 | -} |
1856 | +static DEFINE_PER_CPU(struct crng, crngs) = { |
1857 | + .generation = ULONG_MAX |
1858 | +}; |
1859 | |
1860 | -static void __mix_pool_bytes(struct entropy_store *r, const void *in, |
1861 | - int nbytes) |
1862 | -{ |
1863 | - trace_mix_pool_bytes_nolock(r->name, nbytes, _RET_IP_); |
1864 | - _mix_pool_bytes(r, in, nbytes); |
1865 | -} |
1866 | +/* Used by crng_reseed() and crng_make_state() to extract a new seed from the input pool. */ |
1867 | +static void extract_entropy(void *buf, size_t len); |
1868 | |
1869 | -static void mix_pool_bytes(struct entropy_store *r, const void *in, |
1870 | - int nbytes) |
1871 | +/* This extracts a new crng key from the input pool. */ |
1872 | +static void crng_reseed(void) |
1873 | { |
1874 | unsigned long flags; |
1875 | + unsigned long next_gen; |
1876 | + u8 key[CHACHA20_KEY_SIZE]; |
1877 | |
1878 | - trace_mix_pool_bytes(r->name, nbytes, _RET_IP_); |
1879 | - spin_lock_irqsave(&r->lock, flags); |
1880 | - _mix_pool_bytes(r, in, nbytes); |
1881 | - spin_unlock_irqrestore(&r->lock, flags); |
1882 | -} |
1883 | + extract_entropy(key, sizeof(key)); |
1884 | |
1885 | -struct fast_pool { |
1886 | - __u32 pool[4]; |
1887 | - unsigned long last; |
1888 | - unsigned short reg_idx; |
1889 | - unsigned char count; |
1890 | -}; |
1891 | + /* |
1892 | + * We copy the new key into the base_crng, overwriting the old one, |
1893 | + * and update the generation counter. We avoid hitting ULONG_MAX, |
1894 | + * because the per-cpu crngs are initialized to ULONG_MAX, so this |
1895 | + * forces new CPUs that come online to always initialize. |
1896 | + */ |
1897 | + spin_lock_irqsave(&base_crng.lock, flags); |
1898 | + memcpy(base_crng.key, key, sizeof(base_crng.key)); |
1899 | + next_gen = base_crng.generation + 1; |
1900 | + if (next_gen == ULONG_MAX) |
1901 | + ++next_gen; |
1902 | + WRITE_ONCE(base_crng.generation, next_gen); |
1903 | + WRITE_ONCE(base_crng.birth, jiffies); |
1904 | + if (!crng_ready()) |
1905 | + crng_init = CRNG_READY; |
1906 | + spin_unlock_irqrestore(&base_crng.lock, flags); |
1907 | + memzero_explicit(key, sizeof(key)); |
1908 | +} |
1909 | |
1910 | /* |
1911 | - * This is a fast mixing routine used by the interrupt randomness |
1912 | - * collector. It's hardcoded for an 128 bit pool and assumes that any |
1913 | - * locks that might be needed are taken by the caller. |
1914 | + * This generates a ChaCha block using the provided key, and then |
1915 | + * immediately overwites that key with half the block. It returns |
1916 | + * the resultant ChaCha state to the user, along with the second |
1917 | + * half of the block containing 32 bytes of random data that may |
1918 | + * be used; random_data_len may not be greater than 32. |
1919 | + * |
1920 | + * The returned ChaCha state contains within it a copy of the old |
1921 | + * key value, at index 4, so the state should always be zeroed out |
1922 | + * immediately after using in order to maintain forward secrecy. |
1923 | + * If the state cannot be erased in a timely manner, then it is |
1924 | + * safer to set the random_data parameter to &chacha_state[4] so |
1925 | + * that this function overwrites it before returning. |
1926 | */ |
1927 | -static void fast_mix(struct fast_pool *f) |
1928 | +static void crng_fast_key_erasure(u8 key[CHACHA20_KEY_SIZE], |
1929 | + u32 chacha_state[CHACHA20_BLOCK_SIZE / sizeof(u32)], |
1930 | + u8 *random_data, size_t random_data_len) |
1931 | { |
1932 | - __u32 a = f->pool[0], b = f->pool[1]; |
1933 | - __u32 c = f->pool[2], d = f->pool[3]; |
1934 | + u8 first_block[CHACHA20_BLOCK_SIZE]; |
1935 | |
1936 | - a += b; c += d; |
1937 | - b = rol32(b, 6); d = rol32(d, 27); |
1938 | - d ^= a; b ^= c; |
1939 | + BUG_ON(random_data_len > 32); |
1940 | |
1941 | - a += b; c += d; |
1942 | - b = rol32(b, 16); d = rol32(d, 14); |
1943 | - d ^= a; b ^= c; |
1944 | + chacha_init_consts(chacha_state); |
1945 | + memcpy(&chacha_state[4], key, CHACHA20_KEY_SIZE); |
1946 | + memset(&chacha_state[12], 0, sizeof(u32) * 4); |
1947 | + chacha20_block(chacha_state, first_block); |
1948 | |
1949 | - a += b; c += d; |
1950 | - b = rol32(b, 6); d = rol32(d, 27); |
1951 | - d ^= a; b ^= c; |
1952 | - |
1953 | - a += b; c += d; |
1954 | - b = rol32(b, 16); d = rol32(d, 14); |
1955 | - d ^= a; b ^= c; |
1956 | - |
1957 | - f->pool[0] = a; f->pool[1] = b; |
1958 | - f->pool[2] = c; f->pool[3] = d; |
1959 | - f->count++; |
1960 | + memcpy(key, first_block, CHACHA20_KEY_SIZE); |
1961 | + memcpy(random_data, first_block + CHACHA20_KEY_SIZE, random_data_len); |
1962 | + memzero_explicit(first_block, sizeof(first_block)); |
1963 | } |
1964 | |
1965 | -static void process_random_ready_list(void) |
1966 | +/* |
1967 | + * Return whether the crng seed is considered to be sufficiently old |
1968 | + * that a reseeding is needed. This happens if the last reseeding |
1969 | + * was CRNG_RESEED_INTERVAL ago, or during early boot, at an interval |
1970 | + * proportional to the uptime. |
1971 | + */ |
1972 | +static bool crng_has_old_seed(void) |
1973 | { |
1974 | - unsigned long flags; |
1975 | - struct random_ready_callback *rdy, *tmp; |
1976 | - |
1977 | - spin_lock_irqsave(&random_ready_list_lock, flags); |
1978 | - list_for_each_entry_safe(rdy, tmp, &random_ready_list, list) { |
1979 | - struct module *owner = rdy->owner; |
1980 | - |
1981 | - list_del_init(&rdy->list); |
1982 | - rdy->func(rdy); |
1983 | - module_put(owner); |
1984 | + static bool early_boot = true; |
1985 | + unsigned long interval = CRNG_RESEED_INTERVAL; |
1986 | + |
1987 | + if (unlikely(READ_ONCE(early_boot))) { |
1988 | + time64_t uptime = ktime_get_seconds(); |
1989 | + if (uptime >= CRNG_RESEED_INTERVAL / HZ * 2) |
1990 | + WRITE_ONCE(early_boot, false); |
1991 | + else |
1992 | + interval = max_t(unsigned int, CRNG_RESEED_START_INTERVAL, |
1993 | + (unsigned int)uptime / 2 * HZ); |
1994 | } |
1995 | - spin_unlock_irqrestore(&random_ready_list_lock, flags); |
1996 | + return time_is_before_jiffies(READ_ONCE(base_crng.birth) + interval); |
1997 | } |
1998 | |
1999 | /* |
2000 | - * Credit (or debit) the entropy store with n bits of entropy. |
2001 | - * Use credit_entropy_bits_safe() if the value comes from userspace |
2002 | - * or otherwise should be checked for extreme values. |
2003 | + * This function returns a ChaCha state that you may use for generating |
2004 | + * random data. It also returns up to 32 bytes on its own of random data |
2005 | + * that may be used; random_data_len may not be greater than 32. |
2006 | */ |
2007 | -static void credit_entropy_bits(struct entropy_store *r, int nbits) |
2008 | +static void crng_make_state(u32 chacha_state[CHACHA20_BLOCK_SIZE / sizeof(u32)], |
2009 | + u8 *random_data, size_t random_data_len) |
2010 | { |
2011 | - int entropy_count, orig; |
2012 | - const int pool_size = r->poolinfo->poolfracbits; |
2013 | - int nfrac = nbits << ENTROPY_SHIFT; |
2014 | + unsigned long flags; |
2015 | + struct crng *crng; |
2016 | |
2017 | - if (!nbits) |
2018 | - return; |
2019 | + BUG_ON(random_data_len > 32); |
2020 | |
2021 | -retry: |
2022 | - entropy_count = orig = ACCESS_ONCE(r->entropy_count); |
2023 | - if (nfrac < 0) { |
2024 | - /* Debit */ |
2025 | - entropy_count += nfrac; |
2026 | - } else { |
2027 | - /* |
2028 | - * Credit: we have to account for the possibility of |
2029 | - * overwriting already present entropy. Even in the |
2030 | - * ideal case of pure Shannon entropy, new contributions |
2031 | - * approach the full value asymptotically: |
2032 | - * |
2033 | - * entropy <- entropy + (pool_size - entropy) * |
2034 | - * (1 - exp(-add_entropy/pool_size)) |
2035 | - * |
2036 | - * For add_entropy <= pool_size/2 then |
2037 | - * (1 - exp(-add_entropy/pool_size)) >= |
2038 | - * (add_entropy/pool_size)*0.7869... |
2039 | - * so we can approximate the exponential with |
2040 | - * 3/4*add_entropy/pool_size and still be on the |
2041 | - * safe side by adding at most pool_size/2 at a time. |
2042 | - * |
2043 | - * The use of pool_size-2 in the while statement is to |
2044 | - * prevent rounding artifacts from making the loop |
2045 | - * arbitrarily long; this limits the loop to log2(pool_size)*2 |
2046 | - * turns no matter how large nbits is. |
2047 | - */ |
2048 | - int pnfrac = nfrac; |
2049 | - const int s = r->poolinfo->poolbitshift + ENTROPY_SHIFT + 2; |
2050 | - /* The +2 corresponds to the /4 in the denominator */ |
2051 | - |
2052 | - do { |
2053 | - unsigned int anfrac = min(pnfrac, pool_size/2); |
2054 | - unsigned int add = |
2055 | - ((pool_size - entropy_count)*anfrac*3) >> s; |
2056 | - |
2057 | - entropy_count += add; |
2058 | - pnfrac -= anfrac; |
2059 | - } while (unlikely(entropy_count < pool_size-2 && pnfrac)); |
2060 | - } |
2061 | - |
2062 | - if (unlikely(entropy_count < 0)) { |
2063 | - pr_warn("random: negative entropy/overflow: pool %s count %d\n", |
2064 | - r->name, entropy_count); |
2065 | - WARN_ON(1); |
2066 | - entropy_count = 0; |
2067 | - } else if (entropy_count > pool_size) |
2068 | - entropy_count = pool_size; |
2069 | - if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig) |
2070 | - goto retry; |
2071 | - |
2072 | - r->entropy_total += nbits; |
2073 | - if (!r->initialized && r->entropy_total > 128) { |
2074 | - r->initialized = 1; |
2075 | - r->entropy_total = 0; |
2076 | + /* |
2077 | + * For the fast path, we check whether we're ready, unlocked first, and |
2078 | + * then re-check once locked later. In the case where we're really not |
2079 | + * ready, we do fast key erasure with the base_crng directly, extracting |
2080 | + * when crng_init is CRNG_EMPTY. |
2081 | + */ |
2082 | + if (!crng_ready()) { |
2083 | + bool ready; |
2084 | + |
2085 | + spin_lock_irqsave(&base_crng.lock, flags); |
2086 | + ready = crng_ready(); |
2087 | + if (!ready) { |
2088 | + if (crng_init == CRNG_EMPTY) |
2089 | + extract_entropy(base_crng.key, sizeof(base_crng.key)); |
2090 | + crng_fast_key_erasure(base_crng.key, chacha_state, |
2091 | + random_data, random_data_len); |
2092 | + } |
2093 | + spin_unlock_irqrestore(&base_crng.lock, flags); |
2094 | + if (!ready) |
2095 | + return; |
2096 | } |
2097 | |
2098 | - trace_credit_entropy_bits(r->name, nbits, |
2099 | - entropy_count >> ENTROPY_SHIFT, |
2100 | - r->entropy_total, _RET_IP_); |
2101 | - |
2102 | - if (r == &input_pool) { |
2103 | - int entropy_bits = entropy_count >> ENTROPY_SHIFT; |
2104 | + /* |
2105 | + * If the base_crng is old enough, we reseed, which in turn bumps the |
2106 | + * generation counter that we check below. |
2107 | + */ |
2108 | + if (unlikely(crng_has_old_seed())) |
2109 | + crng_reseed(); |
2110 | |
2111 | - if (crng_init < 2 && entropy_bits >= 128) { |
2112 | - crng_reseed(&primary_crng, r); |
2113 | - entropy_bits = r->entropy_count >> ENTROPY_SHIFT; |
2114 | - } |
2115 | + local_irq_save(flags); |
2116 | + crng = raw_cpu_ptr(&crngs); |
2117 | |
2118 | - /* should we wake readers? */ |
2119 | - if (entropy_bits >= random_read_wakeup_bits) { |
2120 | - wake_up_interruptible(&random_read_wait); |
2121 | - kill_fasync(&fasync, SIGIO, POLL_IN); |
2122 | - } |
2123 | - /* If the input pool is getting full, send some |
2124 | - * entropy to the blocking pool until it is 75% full. |
2125 | - */ |
2126 | - if (entropy_bits > random_write_wakeup_bits && |
2127 | - r->initialized && |
2128 | - r->entropy_total >= 2*random_read_wakeup_bits) { |
2129 | - struct entropy_store *other = &blocking_pool; |
2130 | - |
2131 | - if (other->entropy_count <= |
2132 | - 3 * other->poolinfo->poolfracbits / 4) { |
2133 | - schedule_work(&other->push_work); |
2134 | - r->entropy_total = 0; |
2135 | - } |
2136 | - } |
2137 | + /* |
2138 | + * If our per-cpu crng is older than the base_crng, then it means |
2139 | + * somebody reseeded the base_crng. In that case, we do fast key |
2140 | + * erasure on the base_crng, and use its output as the new key |
2141 | + * for our per-cpu crng. This brings us up to date with base_crng. |
2142 | + */ |
2143 | + if (unlikely(crng->generation != READ_ONCE(base_crng.generation))) { |
2144 | + spin_lock(&base_crng.lock); |
2145 | + crng_fast_key_erasure(base_crng.key, chacha_state, |
2146 | + crng->key, sizeof(crng->key)); |
2147 | + crng->generation = base_crng.generation; |
2148 | + spin_unlock(&base_crng.lock); |
2149 | } |
2150 | + |
2151 | + /* |
2152 | + * Finally, when we've made it this far, our per-cpu crng has an up |
2153 | + * to date key, and we can do fast key erasure with it to produce |
2154 | + * some random data and a ChaCha state for the caller. All other |
2155 | + * branches of this function are "unlikely", so most of the time we |
2156 | + * should wind up here immediately. |
2157 | + */ |
2158 | + crng_fast_key_erasure(crng->key, chacha_state, random_data, random_data_len); |
2159 | + local_irq_restore(flags); |
2160 | } |
2161 | |
2162 | -static int credit_entropy_bits_safe(struct entropy_store *r, int nbits) |
2163 | +static void _get_random_bytes(void *buf, size_t len) |
2164 | { |
2165 | - const int nbits_max = r->poolinfo->poolwords * 32; |
2166 | + u32 chacha_state[CHACHA20_BLOCK_SIZE / sizeof(u32)]; |
2167 | + u8 tmp[CHACHA20_BLOCK_SIZE]; |
2168 | + size_t first_block_len; |
2169 | |
2170 | - if (nbits < 0) |
2171 | - return -EINVAL; |
2172 | - |
2173 | - /* Cap the value to avoid overflows */ |
2174 | - nbits = min(nbits, nbits_max); |
2175 | + if (!len) |
2176 | + return; |
2177 | |
2178 | - credit_entropy_bits(r, nbits); |
2179 | - return 0; |
2180 | -} |
2181 | + first_block_len = min_t(size_t, 32, len); |
2182 | + crng_make_state(chacha_state, buf, first_block_len); |
2183 | + len -= first_block_len; |
2184 | + buf += first_block_len; |
2185 | |
2186 | -/********************************************************************* |
2187 | - * |
2188 | - * CRNG using CHACHA20 |
2189 | - * |
2190 | - *********************************************************************/ |
2191 | + while (len) { |
2192 | + if (len < CHACHA20_BLOCK_SIZE) { |
2193 | + chacha20_block(chacha_state, tmp); |
2194 | + memcpy(buf, tmp, len); |
2195 | + memzero_explicit(tmp, sizeof(tmp)); |
2196 | + break; |
2197 | + } |
2198 | |
2199 | -#define CRNG_RESEED_INTERVAL (300*HZ) |
2200 | + chacha20_block(chacha_state, buf); |
2201 | + if (unlikely(chacha_state[12] == 0)) |
2202 | + ++chacha_state[13]; |
2203 | + len -= CHACHA20_BLOCK_SIZE; |
2204 | + buf += CHACHA20_BLOCK_SIZE; |
2205 | + } |
2206 | |
2207 | -static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); |
2208 | + memzero_explicit(chacha_state, sizeof(chacha_state)); |
2209 | +} |
2210 | |
2211 | -#ifdef CONFIG_NUMA |
2212 | /* |
2213 | - * Hack to deal with crazy userspace progams when they are all trying |
2214 | - * to access /dev/urandom in parallel. The programs are almost |
2215 | - * certainly doing something terribly wrong, but we'll work around |
2216 | - * their brain damage. |
2217 | + * This function is the exported kernel interface. It returns some |
2218 | + * number of good random numbers, suitable for key generation, seeding |
2219 | + * TCP sequence numbers, etc. It does not rely on the hardware random |
2220 | + * number generator. For random bytes direct from the hardware RNG |
2221 | + * (when available), use get_random_bytes_arch(). In order to ensure |
2222 | + * that the randomness provided by this function is okay, the function |
2223 | + * wait_for_random_bytes() should be called and return 0 at least once |
2224 | + * at any point prior. |
2225 | */ |
2226 | -static struct crng_state **crng_node_pool __read_mostly; |
2227 | -#endif |
2228 | - |
2229 | -static void crng_initialize(struct crng_state *crng) |
2230 | +void get_random_bytes(void *buf, size_t len) |
2231 | { |
2232 | - int i; |
2233 | - unsigned long rv; |
2234 | - |
2235 | - memcpy(&crng->state[0], "expand 32-byte k", 16); |
2236 | - if (crng == &primary_crng) |
2237 | - _extract_entropy(&input_pool, &crng->state[4], |
2238 | - sizeof(__u32) * 12, 0); |
2239 | - else |
2240 | - get_random_bytes(&crng->state[4], sizeof(__u32) * 12); |
2241 | - for (i = 4; i < 16; i++) { |
2242 | - if (!arch_get_random_seed_long(&rv) && |
2243 | - !arch_get_random_long(&rv)) |
2244 | - rv = random_get_entropy(); |
2245 | - crng->state[i] ^= rv; |
2246 | - } |
2247 | - crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1; |
2248 | + warn_unseeded_randomness(); |
2249 | + _get_random_bytes(buf, len); |
2250 | } |
2251 | +EXPORT_SYMBOL(get_random_bytes); |
2252 | |
2253 | -static int crng_fast_load(const char *cp, size_t len) |
2254 | +static ssize_t get_random_bytes_user(struct iov_iter *iter) |
2255 | { |
2256 | - unsigned long flags; |
2257 | - char *p; |
2258 | + u32 chacha_state[CHACHA20_BLOCK_SIZE / sizeof(u32)]; |
2259 | + u8 block[CHACHA20_BLOCK_SIZE]; |
2260 | + size_t ret = 0, copied; |
2261 | |
2262 | - if (!spin_trylock_irqsave(&primary_crng.lock, flags)) |
2263 | + if (unlikely(!iov_iter_count(iter))) |
2264 | return 0; |
2265 | - if (crng_init != 0) { |
2266 | - spin_unlock_irqrestore(&primary_crng.lock, flags); |
2267 | - return 0; |
2268 | - } |
2269 | - p = (unsigned char *) &primary_crng.state[4]; |
2270 | - while (len > 0 && crng_init_cnt < CRNG_INIT_CNT_THRESH) { |
2271 | - p[crng_init_cnt % CHACHA20_KEY_SIZE] ^= *cp; |
2272 | - cp++; crng_init_cnt++; len--; |
2273 | - } |
2274 | - if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { |
2275 | - crng_init = 1; |
2276 | - wake_up_interruptible(&crng_init_wait); |
2277 | - pr_notice("random: fast init done\n"); |
2278 | - } |
2279 | - spin_unlock_irqrestore(&primary_crng.lock, flags); |
2280 | - return 1; |
2281 | -} |
2282 | |
2283 | -#ifdef CONFIG_NUMA |
2284 | -static void do_numa_crng_init(struct work_struct *work) |
2285 | -{ |
2286 | - int i; |
2287 | - struct crng_state *crng; |
2288 | - struct crng_state **pool; |
2289 | - |
2290 | - pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL|__GFP_NOFAIL); |
2291 | - for_each_online_node(i) { |
2292 | - crng = kmalloc_node(sizeof(struct crng_state), |
2293 | - GFP_KERNEL | __GFP_NOFAIL, i); |
2294 | - spin_lock_init(&crng->lock); |
2295 | - crng_initialize(crng); |
2296 | - pool[i] = crng; |
2297 | - } |
2298 | - /* pairs with READ_ONCE() in select_crng() */ |
2299 | - if (cmpxchg_release(&crng_node_pool, NULL, pool) != NULL) { |
2300 | - for_each_node(i) |
2301 | - kfree(pool[i]); |
2302 | - kfree(pool); |
2303 | + /* |
2304 | + * Immediately overwrite the ChaCha key at index 4 with random |
2305 | + * bytes, in case userspace causes copy_to_user() below to sleep |
2306 | + * forever, so that we still retain forward secrecy in that case. |
2307 | + */ |
2308 | + crng_make_state(chacha_state, (u8 *)&chacha_state[4], CHACHA20_KEY_SIZE); |
2309 | + /* |
2310 | + * However, if we're doing a read of len <= 32, we don't need to |
2311 | + * use chacha_state after, so we can simply return those bytes to |
2312 | + * the user directly. |
2313 | + */ |
2314 | + if (iov_iter_count(iter) <= CHACHA20_KEY_SIZE) { |
2315 | + ret = copy_to_iter(&chacha_state[4], CHACHA20_KEY_SIZE, iter); |
2316 | + goto out_zero_chacha; |
2317 | } |
2318 | -} |
2319 | - |
2320 | -static DECLARE_WORK(numa_crng_init_work, do_numa_crng_init); |
2321 | - |
2322 | -static void numa_crng_init(void) |
2323 | -{ |
2324 | - schedule_work(&numa_crng_init_work); |
2325 | -} |
2326 | |
2327 | -static struct crng_state *select_crng(void) |
2328 | -{ |
2329 | - struct crng_state **pool; |
2330 | - int nid = numa_node_id(); |
2331 | - |
2332 | - /* pairs with cmpxchg_release() in do_numa_crng_init() */ |
2333 | - pool = READ_ONCE(crng_node_pool); |
2334 | - if (pool && pool[nid]) |
2335 | - return pool[nid]; |
2336 | + for (;;) { |
2337 | + chacha20_block(chacha_state, block); |
2338 | + if (unlikely(chacha_state[12] == 0)) |
2339 | + ++chacha_state[13]; |
2340 | |
2341 | - return &primary_crng; |
2342 | -} |
2343 | -#else |
2344 | -static void numa_crng_init(void) {} |
2345 | - |
2346 | -static struct crng_state *select_crng(void) |
2347 | -{ |
2348 | - return &primary_crng; |
2349 | -} |
2350 | -#endif |
2351 | + copied = copy_to_iter(block, sizeof(block), iter); |
2352 | + ret += copied; |
2353 | + if (!iov_iter_count(iter) || copied != sizeof(block)) |
2354 | + break; |
2355 | |
2356 | -static void crng_reseed(struct crng_state *crng, struct entropy_store *r) |
2357 | -{ |
2358 | - unsigned long flags; |
2359 | - int i, num; |
2360 | - union { |
2361 | - __u8 block[CHACHA20_BLOCK_SIZE]; |
2362 | - __u32 key[8]; |
2363 | - } buf; |
2364 | - |
2365 | - if (r) { |
2366 | - num = extract_entropy(r, &buf, 32, 16, 0); |
2367 | - if (num == 0) |
2368 | - return; |
2369 | - } else { |
2370 | - _extract_crng(&primary_crng, buf.block); |
2371 | - _crng_backtrack_protect(&primary_crng, buf.block, |
2372 | - CHACHA20_KEY_SIZE); |
2373 | - } |
2374 | - spin_lock_irqsave(&crng->lock, flags); |
2375 | - for (i = 0; i < 8; i++) { |
2376 | - unsigned long rv; |
2377 | - if (!arch_get_random_seed_long(&rv) && |
2378 | - !arch_get_random_long(&rv)) |
2379 | - rv = random_get_entropy(); |
2380 | - crng->state[i+4] ^= buf.key[i] ^ rv; |
2381 | - } |
2382 | - memzero_explicit(&buf, sizeof(buf)); |
2383 | - WRITE_ONCE(crng->init_time, jiffies); |
2384 | - if (crng == &primary_crng && crng_init < 2) { |
2385 | - numa_crng_init(); |
2386 | - crng_init = 2; |
2387 | - process_random_ready_list(); |
2388 | - wake_up_interruptible(&crng_init_wait); |
2389 | - pr_notice("random: crng init done\n"); |
2390 | - if (unseeded_warning.missed) { |
2391 | - pr_notice("random: %d get_random_xx warning(s) missed " |
2392 | - "due to ratelimiting\n", |
2393 | - unseeded_warning.missed); |
2394 | - unseeded_warning.missed = 0; |
2395 | - } |
2396 | - if (urandom_warning.missed) { |
2397 | - pr_notice("random: %d urandom warning(s) missed " |
2398 | - "due to ratelimiting\n", |
2399 | - urandom_warning.missed); |
2400 | - urandom_warning.missed = 0; |
2401 | + BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0); |
2402 | + if (ret % PAGE_SIZE == 0) { |
2403 | + if (signal_pending(current)) |
2404 | + break; |
2405 | + cond_resched(); |
2406 | } |
2407 | } |
2408 | - spin_unlock_irqrestore(&crng->lock, flags); |
2409 | -} |
2410 | |
2411 | -static inline void maybe_reseed_primary_crng(void) |
2412 | -{ |
2413 | - if (crng_init > 2 && |
2414 | - time_after(jiffies, primary_crng.init_time + CRNG_RESEED_INTERVAL)) |
2415 | - crng_reseed(&primary_crng, &input_pool); |
2416 | + memzero_explicit(block, sizeof(block)); |
2417 | +out_zero_chacha: |
2418 | + memzero_explicit(chacha_state, sizeof(chacha_state)); |
2419 | + return ret ? ret : -EFAULT; |
2420 | } |
2421 | |
2422 | -static inline void crng_wait_ready(void) |
2423 | -{ |
2424 | - wait_event_interruptible(crng_init_wait, crng_ready()); |
2425 | -} |
2426 | - |
2427 | -static void _extract_crng(struct crng_state *crng, |
2428 | - __u8 out[CHACHA20_BLOCK_SIZE]) |
2429 | -{ |
2430 | - unsigned long v, flags, init_time; |
2431 | - |
2432 | - if (crng_ready()) { |
2433 | - init_time = READ_ONCE(crng->init_time); |
2434 | - if (time_after(READ_ONCE(crng_global_init_time), init_time) || |
2435 | - time_after(jiffies, init_time + CRNG_RESEED_INTERVAL)) |
2436 | - crng_reseed(crng, crng == &primary_crng ? |
2437 | - &input_pool : NULL); |
2438 | - } |
2439 | - spin_lock_irqsave(&crng->lock, flags); |
2440 | - if (arch_get_random_long(&v)) |
2441 | - crng->state[14] ^= v; |
2442 | - chacha20_block(&crng->state[0], out); |
2443 | - if (crng->state[12] == 0) |
2444 | - crng->state[13]++; |
2445 | - spin_unlock_irqrestore(&crng->lock, flags); |
2446 | -} |
2447 | - |
2448 | -static void extract_crng(__u8 out[CHACHA20_BLOCK_SIZE]) |
2449 | -{ |
2450 | - _extract_crng(select_crng(), out); |
2451 | -} |
2452 | +/* |
2453 | + * Batched entropy returns random integers. The quality of the random |
2454 | + * number is good as /dev/urandom. In order to ensure that the randomness |
2455 | + * provided by this function is okay, the function wait_for_random_bytes() |
2456 | + * should be called and return 0 at least once at any point prior. |
2457 | + */ |
2458 | |
2459 | +#define DEFINE_BATCHED_ENTROPY(type) \ |
2460 | +struct batch_ ##type { \ |
2461 | + /* \ |
2462 | + * We make this 1.5x a ChaCha block, so that we get the \ |
2463 | + * remaining 32 bytes from fast key erasure, plus one full \ |
2464 | + * block from the detached ChaCha state. We can increase \ |
2465 | + * the size of this later if needed so long as we keep the \ |
2466 | + * formula of (integer_blocks + 0.5) * CHACHA20_BLOCK_SIZE. \ |
2467 | + */ \ |
2468 | + type entropy[CHACHA20_BLOCK_SIZE * 3 / (2 * sizeof(type))]; \ |
2469 | + unsigned long generation; \ |
2470 | + unsigned int position; \ |
2471 | +}; \ |
2472 | + \ |
2473 | +static DEFINE_PER_CPU(struct batch_ ##type, batched_entropy_ ##type) = { \ |
2474 | + .position = UINT_MAX \ |
2475 | +}; \ |
2476 | + \ |
2477 | +type get_random_ ##type(void) \ |
2478 | +{ \ |
2479 | + type ret; \ |
2480 | + unsigned long flags; \ |
2481 | + struct batch_ ##type *batch; \ |
2482 | + unsigned long next_gen; \ |
2483 | + \ |
2484 | + warn_unseeded_randomness(); \ |
2485 | + \ |
2486 | + if (!crng_ready()) { \ |
2487 | + _get_random_bytes(&ret, sizeof(ret)); \ |
2488 | + return ret; \ |
2489 | + } \ |
2490 | + \ |
2491 | + local_irq_save(flags); \ |
2492 | + batch = raw_cpu_ptr(&batched_entropy_##type); \ |
2493 | + \ |
2494 | + next_gen = READ_ONCE(base_crng.generation); \ |
2495 | + if (batch->position >= ARRAY_SIZE(batch->entropy) || \ |
2496 | + next_gen != batch->generation) { \ |
2497 | + _get_random_bytes(batch->entropy, sizeof(batch->entropy)); \ |
2498 | + batch->position = 0; \ |
2499 | + batch->generation = next_gen; \ |
2500 | + } \ |
2501 | + \ |
2502 | + ret = batch->entropy[batch->position]; \ |
2503 | + batch->entropy[batch->position] = 0; \ |
2504 | + ++batch->position; \ |
2505 | + local_irq_restore(flags); \ |
2506 | + return ret; \ |
2507 | +} \ |
2508 | +EXPORT_SYMBOL(get_random_ ##type); |
2509 | + |
2510 | +DEFINE_BATCHED_ENTROPY(u64) |
2511 | +DEFINE_BATCHED_ENTROPY(u32) |
2512 | + |
2513 | +#ifdef CONFIG_SMP |
2514 | /* |
2515 | - * Use the leftover bytes from the CRNG block output (if there is |
2516 | - * enough) to mutate the CRNG key to provide backtracking protection. |
2517 | + * This function is called when the CPU is coming up, with entry |
2518 | + * CPUHP_RANDOM_PREPARE, which comes before CPUHP_WORKQUEUE_PREP. |
2519 | */ |
2520 | -static void _crng_backtrack_protect(struct crng_state *crng, |
2521 | - __u8 tmp[CHACHA20_BLOCK_SIZE], int used) |
2522 | +int __cold random_prepare_cpu(unsigned int cpu) |
2523 | { |
2524 | - unsigned long flags; |
2525 | - __u32 *s, *d; |
2526 | - int i; |
2527 | - |
2528 | - used = round_up(used, sizeof(__u32)); |
2529 | - if (used + CHACHA20_KEY_SIZE > CHACHA20_BLOCK_SIZE) { |
2530 | - extract_crng(tmp); |
2531 | - used = 0; |
2532 | - } |
2533 | - spin_lock_irqsave(&crng->lock, flags); |
2534 | - s = (__u32 *) &tmp[used]; |
2535 | - d = &crng->state[4]; |
2536 | - for (i=0; i < 8; i++) |
2537 | - *d++ ^= *s++; |
2538 | - spin_unlock_irqrestore(&crng->lock, flags); |
2539 | + /* |
2540 | + * When the cpu comes back online, immediately invalidate both |
2541 | + * the per-cpu crng and all batches, so that we serve fresh |
2542 | + * randomness. |
2543 | + */ |
2544 | + per_cpu_ptr(&crngs, cpu)->generation = ULONG_MAX; |
2545 | + per_cpu_ptr(&batched_entropy_u32, cpu)->position = UINT_MAX; |
2546 | + per_cpu_ptr(&batched_entropy_u64, cpu)->position = UINT_MAX; |
2547 | + return 0; |
2548 | } |
2549 | +#endif |
2550 | |
2551 | -static void crng_backtrack_protect(__u8 tmp[CHACHA20_BLOCK_SIZE], int used) |
2552 | +/* |
2553 | + * This function will use the architecture-specific hardware random |
2554 | + * number generator if it is available. It is not recommended for |
2555 | + * use. Use get_random_bytes() instead. It returns the number of |
2556 | + * bytes filled in. |
2557 | + */ |
2558 | +size_t __must_check get_random_bytes_arch(void *buf, size_t len) |
2559 | { |
2560 | - _crng_backtrack_protect(select_crng(), tmp, used); |
2561 | -} |
2562 | + size_t left = len; |
2563 | + u8 *p = buf; |
2564 | |
2565 | -static ssize_t extract_crng_user(void __user *buf, size_t nbytes) |
2566 | -{ |
2567 | - ssize_t ret = 0, i = CHACHA20_BLOCK_SIZE; |
2568 | - __u8 tmp[CHACHA20_BLOCK_SIZE]; |
2569 | - int large_request = (nbytes > 256); |
2570 | - |
2571 | - while (nbytes) { |
2572 | - if (large_request && need_resched()) { |
2573 | - if (signal_pending(current)) { |
2574 | - if (ret == 0) |
2575 | - ret = -ERESTARTSYS; |
2576 | - break; |
2577 | - } |
2578 | - schedule(); |
2579 | - } |
2580 | + while (left) { |
2581 | + unsigned long v; |
2582 | + size_t block_len = min_t(size_t, left, sizeof(unsigned long)); |
2583 | |
2584 | - extract_crng(tmp); |
2585 | - i = min_t(int, nbytes, CHACHA20_BLOCK_SIZE); |
2586 | - if (copy_to_user(buf, tmp, i)) { |
2587 | - ret = -EFAULT; |
2588 | + if (!arch_get_random_long(&v)) |
2589 | break; |
2590 | - } |
2591 | |
2592 | - nbytes -= i; |
2593 | - buf += i; |
2594 | - ret += i; |
2595 | + memcpy(p, &v, block_len); |
2596 | + p += block_len; |
2597 | + left -= block_len; |
2598 | } |
2599 | - crng_backtrack_protect(tmp, i); |
2600 | - |
2601 | - /* Wipe data just written to memory */ |
2602 | - memzero_explicit(tmp, sizeof(tmp)); |
2603 | |
2604 | - return ret; |
2605 | + return len - left; |
2606 | } |
2607 | +EXPORT_SYMBOL(get_random_bytes_arch); |
2608 | |
2609 | |
2610 | -/********************************************************************* |
2611 | +/********************************************************************** |
2612 | * |
2613 | - * Entropy input management |
2614 | + * Entropy accumulation and extraction routines. |
2615 | * |
2616 | - *********************************************************************/ |
2617 | + * Callers may add entropy via: |
2618 | + * |
2619 | + * static void mix_pool_bytes(const void *buf, size_t len) |
2620 | + * |
2621 | + * After which, if added entropy should be credited: |
2622 | + * |
2623 | + * static void credit_init_bits(size_t bits) |
2624 | + * |
2625 | + * Finally, extract entropy via: |
2626 | + * |
2627 | + * static void extract_entropy(void *buf, size_t len) |
2628 | + * |
2629 | + **********************************************************************/ |
2630 | |
2631 | -/* There is one of these per entropy source */ |
2632 | -struct timer_rand_state { |
2633 | - cycles_t last_time; |
2634 | - long last_delta, last_delta2; |
2635 | - unsigned dont_count_entropy:1; |
2636 | +enum { |
2637 | + POOL_BITS = BLAKE2S_HASH_SIZE * 8, |
2638 | + POOL_READY_BITS = POOL_BITS, /* When crng_init->CRNG_READY */ |
2639 | + POOL_EARLY_BITS = POOL_READY_BITS / 2 /* When crng_init->CRNG_EARLY */ |
2640 | +}; |
2641 | + |
2642 | +static struct { |
2643 | + struct blake2s_state hash; |
2644 | + spinlock_t lock; |
2645 | + unsigned int init_bits; |
2646 | +} input_pool = { |
2647 | + .hash.h = { BLAKE2S_IV0 ^ (0x01010000 | BLAKE2S_HASH_SIZE), |
2648 | + BLAKE2S_IV1, BLAKE2S_IV2, BLAKE2S_IV3, BLAKE2S_IV4, |
2649 | + BLAKE2S_IV5, BLAKE2S_IV6, BLAKE2S_IV7 }, |
2650 | + .hash.outlen = BLAKE2S_HASH_SIZE, |
2651 | + .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), |
2652 | }; |
2653 | |
2654 | -#define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, }; |
2655 | +static void _mix_pool_bytes(const void *buf, size_t len) |
2656 | +{ |
2657 | + blake2s_update(&input_pool.hash, buf, len); |
2658 | +} |
2659 | |
2660 | /* |
2661 | - * Add device- or boot-specific data to the input pool to help |
2662 | - * initialize it. |
2663 | - * |
2664 | - * None of this adds any entropy; it is meant to avoid the problem of |
2665 | - * the entropy pool having similar initial state across largely |
2666 | - * identical devices. |
2667 | + * This function adds bytes into the input pool. It does not |
2668 | + * update the initialization bit counter; the caller should call |
2669 | + * credit_init_bits if this is appropriate. |
2670 | */ |
2671 | -void add_device_randomness(const void *buf, unsigned int size) |
2672 | +static void mix_pool_bytes(const void *buf, size_t len) |
2673 | { |
2674 | - unsigned long time = random_get_entropy() ^ jiffies; |
2675 | unsigned long flags; |
2676 | |
2677 | - trace_add_device_randomness(size, _RET_IP_); |
2678 | spin_lock_irqsave(&input_pool.lock, flags); |
2679 | - _mix_pool_bytes(&input_pool, buf, size); |
2680 | - _mix_pool_bytes(&input_pool, &time, sizeof(time)); |
2681 | + _mix_pool_bytes(buf, len); |
2682 | spin_unlock_irqrestore(&input_pool.lock, flags); |
2683 | } |
2684 | -EXPORT_SYMBOL(add_device_randomness); |
2685 | - |
2686 | -static struct timer_rand_state input_timer_state = INIT_TIMER_RAND_STATE; |
2687 | |
2688 | /* |
2689 | - * This function adds entropy to the entropy "pool" by using timing |
2690 | - * delays. It uses the timer_rand_state structure to make an estimate |
2691 | - * of how many bits of entropy this call has added to the pool. |
2692 | - * |
2693 | - * The number "num" is also added to the pool - it should somehow describe |
2694 | - * the type of event which just happened. This is currently 0-255 for |
2695 | - * keyboard scan codes, and 256 upwards for interrupts. |
2696 | - * |
2697 | + * This is an HKDF-like construction for using the hashed collected entropy |
2698 | + * as a PRF key, that's then expanded block-by-block. |
2699 | */ |
2700 | -static void add_timer_randomness(struct timer_rand_state *state, unsigned num) |
2701 | +static void extract_entropy(void *buf, size_t len) |
2702 | { |
2703 | - struct entropy_store *r; |
2704 | + unsigned long flags; |
2705 | + u8 seed[BLAKE2S_HASH_SIZE], next_key[BLAKE2S_HASH_SIZE]; |
2706 | struct { |
2707 | - long jiffies; |
2708 | - unsigned cycles; |
2709 | - unsigned num; |
2710 | - } sample; |
2711 | - long delta, delta2, delta3; |
2712 | - |
2713 | - preempt_disable(); |
2714 | - |
2715 | - sample.jiffies = jiffies; |
2716 | - sample.cycles = random_get_entropy(); |
2717 | - sample.num = num; |
2718 | - r = &input_pool; |
2719 | - mix_pool_bytes(r, &sample, sizeof(sample)); |
2720 | - |
2721 | - /* |
2722 | - * Calculate number of bits of randomness we probably added. |
2723 | - * We take into account the first, second and third-order deltas |
2724 | - * in order to make our estimate. |
2725 | - */ |
2726 | - |
2727 | - if (!state->dont_count_entropy) { |
2728 | - delta = sample.jiffies - state->last_time; |
2729 | - state->last_time = sample.jiffies; |
2730 | - |
2731 | - delta2 = delta - state->last_delta; |
2732 | - state->last_delta = delta; |
2733 | - |
2734 | - delta3 = delta2 - state->last_delta2; |
2735 | - state->last_delta2 = delta2; |
2736 | - |
2737 | - if (delta < 0) |
2738 | - delta = -delta; |
2739 | - if (delta2 < 0) |
2740 | - delta2 = -delta2; |
2741 | - if (delta3 < 0) |
2742 | - delta3 = -delta3; |
2743 | - if (delta > delta2) |
2744 | - delta = delta2; |
2745 | - if (delta > delta3) |
2746 | - delta = delta3; |
2747 | - |
2748 | - /* |
2749 | - * delta is now minimum absolute delta. |
2750 | - * Round down by 1 bit on general principles, |
2751 | - * and limit entropy entimate to 12 bits. |
2752 | - */ |
2753 | - credit_entropy_bits(r, min_t(int, fls(delta>>1), 11)); |
2754 | + unsigned long rdseed[32 / sizeof(long)]; |
2755 | + size_t counter; |
2756 | + } block; |
2757 | + size_t i; |
2758 | + |
2759 | + for (i = 0; i < ARRAY_SIZE(block.rdseed); ++i) { |
2760 | + if (!arch_get_random_seed_long(&block.rdseed[i]) && |
2761 | + !arch_get_random_long(&block.rdseed[i])) |
2762 | + block.rdseed[i] = random_get_entropy(); |
2763 | } |
2764 | - preempt_enable(); |
2765 | -} |
2766 | - |
2767 | -void add_input_randomness(unsigned int type, unsigned int code, |
2768 | - unsigned int value) |
2769 | -{ |
2770 | - static unsigned char last_value; |
2771 | |
2772 | - /* ignore autorepeat and the like */ |
2773 | - if (value == last_value) |
2774 | - return; |
2775 | - |
2776 | - last_value = value; |
2777 | - add_timer_randomness(&input_timer_state, |
2778 | - (type << 4) ^ code ^ (code >> 4) ^ value); |
2779 | - trace_add_input_randomness(ENTROPY_BITS(&input_pool)); |
2780 | -} |
2781 | -EXPORT_SYMBOL_GPL(add_input_randomness); |
2782 | + spin_lock_irqsave(&input_pool.lock, flags); |
2783 | |
2784 | -static DEFINE_PER_CPU(struct fast_pool, irq_randomness); |
2785 | + /* seed = HASHPRF(last_key, entropy_input) */ |
2786 | + blake2s_final(&input_pool.hash, seed); |
2787 | |
2788 | -#ifdef ADD_INTERRUPT_BENCH |
2789 | -static unsigned long avg_cycles, avg_deviation; |
2790 | + /* next_key = HASHPRF(seed, RDSEED || 0) */ |
2791 | + block.counter = 0; |
2792 | + blake2s(next_key, (u8 *)&block, seed, sizeof(next_key), sizeof(block), sizeof(seed)); |
2793 | + blake2s_init_key(&input_pool.hash, BLAKE2S_HASH_SIZE, next_key, sizeof(next_key)); |
2794 | |
2795 | -#define AVG_SHIFT 8 /* Exponential average factor k=1/256 */ |
2796 | -#define FIXED_1_2 (1 << (AVG_SHIFT-1)) |
2797 | + spin_unlock_irqrestore(&input_pool.lock, flags); |
2798 | + memzero_explicit(next_key, sizeof(next_key)); |
2799 | + |
2800 | + while (len) { |
2801 | + i = min_t(size_t, len, BLAKE2S_HASH_SIZE); |
2802 | + /* output = HASHPRF(seed, RDSEED || ++counter) */ |
2803 | + ++block.counter; |
2804 | + blake2s(buf, (u8 *)&block, seed, i, sizeof(block), sizeof(seed)); |
2805 | + len -= i; |
2806 | + buf += i; |
2807 | + } |
2808 | |
2809 | -static void add_interrupt_bench(cycles_t start) |
2810 | -{ |
2811 | - long delta = random_get_entropy() - start; |
2812 | - |
2813 | - /* Use a weighted moving average */ |
2814 | - delta = delta - ((avg_cycles + FIXED_1_2) >> AVG_SHIFT); |
2815 | - avg_cycles += delta; |
2816 | - /* And average deviation */ |
2817 | - delta = abs(delta) - ((avg_deviation + FIXED_1_2) >> AVG_SHIFT); |
2818 | - avg_deviation += delta; |
2819 | + memzero_explicit(seed, sizeof(seed)); |
2820 | + memzero_explicit(&block, sizeof(block)); |
2821 | } |
2822 | -#else |
2823 | -#define add_interrupt_bench(x) |
2824 | -#endif |
2825 | |
2826 | -static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs) |
2827 | -{ |
2828 | - __u32 *ptr = (__u32 *) regs; |
2829 | - unsigned int idx; |
2830 | - |
2831 | - if (regs == NULL) |
2832 | - return 0; |
2833 | - idx = READ_ONCE(f->reg_idx); |
2834 | - if (idx >= sizeof(struct pt_regs) / sizeof(__u32)) |
2835 | - idx = 0; |
2836 | - ptr += idx++; |
2837 | - WRITE_ONCE(f->reg_idx, idx); |
2838 | - return *ptr; |
2839 | -} |
2840 | +#define credit_init_bits(bits) if (!crng_ready()) _credit_init_bits(bits) |
2841 | |
2842 | -void add_interrupt_randomness(int irq, int irq_flags) |
2843 | +static void __cold _credit_init_bits(size_t bits) |
2844 | { |
2845 | - struct entropy_store *r; |
2846 | - struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness); |
2847 | - struct pt_regs *regs = get_irq_regs(); |
2848 | - unsigned long now = jiffies; |
2849 | - cycles_t cycles = random_get_entropy(); |
2850 | - __u32 c_high, j_high; |
2851 | - __u64 ip; |
2852 | - unsigned long seed; |
2853 | - int credit = 0; |
2854 | - |
2855 | - if (cycles == 0) |
2856 | - cycles = get_reg(fast_pool, regs); |
2857 | - c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0; |
2858 | - j_high = (sizeof(now) > 4) ? now >> 32 : 0; |
2859 | - fast_pool->pool[0] ^= cycles ^ j_high ^ irq; |
2860 | - fast_pool->pool[1] ^= now ^ c_high; |
2861 | - ip = regs ? instruction_pointer(regs) : _RET_IP_; |
2862 | - fast_pool->pool[2] ^= ip; |
2863 | - fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 : |
2864 | - get_reg(fast_pool, regs); |
2865 | - |
2866 | - fast_mix(fast_pool); |
2867 | - add_interrupt_bench(cycles); |
2868 | - |
2869 | - if (unlikely(crng_init == 0)) { |
2870 | - if ((fast_pool->count >= 64) && |
2871 | - crng_fast_load((char *) fast_pool->pool, |
2872 | - sizeof(fast_pool->pool))) { |
2873 | - fast_pool->count = 0; |
2874 | - fast_pool->last = now; |
2875 | - } |
2876 | - return; |
2877 | - } |
2878 | + unsigned int new, orig, add; |
2879 | + unsigned long flags; |
2880 | |
2881 | - if ((fast_pool->count < 64) && |
2882 | - !time_after(now, fast_pool->last + HZ)) |
2883 | + if (!bits) |
2884 | return; |
2885 | |
2886 | - r = &input_pool; |
2887 | - if (!spin_trylock(&r->lock)) |
2888 | - return; |
2889 | + add = min_t(size_t, bits, POOL_BITS); |
2890 | |
2891 | - fast_pool->last = now; |
2892 | - __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool)); |
2893 | + do { |
2894 | + orig = READ_ONCE(input_pool.init_bits); |
2895 | + new = min_t(unsigned int, POOL_BITS, orig + add); |
2896 | + } while (cmpxchg(&input_pool.init_bits, orig, new) != orig); |
2897 | |
2898 | - /* |
2899 | - * If we have architectural seed generator, produce a seed and |
2900 | - * add it to the pool. For the sake of paranoia don't let the |
2901 | - * architectural seed generator dominate the input from the |
2902 | - * interrupt noise. |
2903 | - */ |
2904 | - if (arch_get_random_seed_long(&seed)) { |
2905 | - __mix_pool_bytes(r, &seed, sizeof(seed)); |
2906 | - credit = 1; |
2907 | + if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) { |
2908 | + crng_reseed(); /* Sets crng_init to CRNG_READY under base_crng.lock. */ |
2909 | + process_random_ready_list(); |
2910 | + wake_up_interruptible(&crng_init_wait); |
2911 | + kill_fasync(&fasync, SIGIO, POLL_IN); |
2912 | + pr_notice("crng init done\n"); |
2913 | + if (urandom_warning.missed) |
2914 | + pr_notice("%d urandom warning(s) missed due to ratelimiting\n", |
2915 | + urandom_warning.missed); |
2916 | + } else if (orig < POOL_EARLY_BITS && new >= POOL_EARLY_BITS) { |
2917 | + spin_lock_irqsave(&base_crng.lock, flags); |
2918 | + /* Check if crng_init is CRNG_EMPTY, to avoid race with crng_reseed(). */ |
2919 | + if (crng_init == CRNG_EMPTY) { |
2920 | + extract_entropy(base_crng.key, sizeof(base_crng.key)); |
2921 | + crng_init = CRNG_EARLY; |
2922 | + } |
2923 | + spin_unlock_irqrestore(&base_crng.lock, flags); |
2924 | } |
2925 | - spin_unlock(&r->lock); |
2926 | - |
2927 | - fast_pool->count = 0; |
2928 | - |
2929 | - /* award one bit for the contents of the fast pool */ |
2930 | - credit_entropy_bits(r, credit + 1); |
2931 | } |
2932 | -EXPORT_SYMBOL_GPL(add_interrupt_randomness); |
2933 | |
2934 | -#ifdef CONFIG_BLOCK |
2935 | -void add_disk_randomness(struct gendisk *disk) |
2936 | -{ |
2937 | - if (!disk || !disk->random) |
2938 | - return; |
2939 | - /* first major is 1, so we get >= 0x200 here */ |
2940 | - add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); |
2941 | - trace_add_disk_randomness(disk_devt(disk), ENTROPY_BITS(&input_pool)); |
2942 | -} |
2943 | -EXPORT_SYMBOL_GPL(add_disk_randomness); |
2944 | -#endif |
2945 | |
2946 | -/********************************************************************* |
2947 | +/********************************************************************** |
2948 | * |
2949 | - * Entropy extraction routines |
2950 | + * Entropy collection routines. |
2951 | * |
2952 | - *********************************************************************/ |
2953 | - |
2954 | -/* |
2955 | - * This utility inline function is responsible for transferring entropy |
2956 | - * from the primary pool to the secondary extraction pool. We make |
2957 | - * sure we pull enough for a 'catastrophic reseed'. |
2958 | - */ |
2959 | -static void _xfer_secondary_pool(struct entropy_store *r, size_t nbytes); |
2960 | -static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes) |
2961 | -{ |
2962 | - if (!r->pull || |
2963 | - r->entropy_count >= (nbytes << (ENTROPY_SHIFT + 3)) || |
2964 | - r->entropy_count > r->poolinfo->poolfracbits) |
2965 | - return; |
2966 | - |
2967 | - if (r->limit == 0 && random_min_urandom_seed) { |
2968 | - unsigned long now = jiffies; |
2969 | - |
2970 | - if (time_before(now, |
2971 | - r->last_pulled + random_min_urandom_seed * HZ)) |
2972 | - return; |
2973 | - r->last_pulled = now; |
2974 | - } |
2975 | - |
2976 | - _xfer_secondary_pool(r, nbytes); |
2977 | -} |
2978 | - |
2979 | -static void _xfer_secondary_pool(struct entropy_store *r, size_t nbytes) |
2980 | -{ |
2981 | - __u32 tmp[OUTPUT_POOL_WORDS]; |
2982 | - |
2983 | - /* For /dev/random's pool, always leave two wakeups' worth */ |
2984 | - int rsvd_bytes = r->limit ? 0 : random_read_wakeup_bits / 4; |
2985 | - int bytes = nbytes; |
2986 | - |
2987 | - /* pull at least as much as a wakeup */ |
2988 | - bytes = max_t(int, bytes, random_read_wakeup_bits / 8); |
2989 | - /* but never more than the buffer size */ |
2990 | - bytes = min_t(int, bytes, sizeof(tmp)); |
2991 | - |
2992 | - trace_xfer_secondary_pool(r->name, bytes * 8, nbytes * 8, |
2993 | - ENTROPY_BITS(r), ENTROPY_BITS(r->pull)); |
2994 | - bytes = extract_entropy(r->pull, tmp, bytes, |
2995 | - random_read_wakeup_bits / 8, rsvd_bytes); |
2996 | - mix_pool_bytes(r, tmp, bytes); |
2997 | - credit_entropy_bits(r, bytes*8); |
2998 | -} |
2999 | + * The following exported functions are used for pushing entropy into |
3000 | + * the above entropy accumulation routines: |
3001 | + * |
3002 | + * void add_device_randomness(const void *buf, size_t len); |
3003 | + * void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy); |
3004 | + * void add_bootloader_randomness(const void *buf, size_t len); |
3005 | + * void add_interrupt_randomness(int irq); |
3006 | + * void add_input_randomness(unsigned int type, unsigned int code, unsigned int value); |
3007 | + * void add_disk_randomness(struct gendisk *disk); |
3008 | + * |
3009 | + * add_device_randomness() adds data to the input pool that |
3010 | + * is likely to differ between two devices (or possibly even per boot). |
3011 | + * This would be things like MAC addresses or serial numbers, or the |
3012 | + * read-out of the RTC. This does *not* credit any actual entropy to |
3013 | + * the pool, but it initializes the pool to different values for devices |
3014 | + * that might otherwise be identical and have very little entropy |
3015 | + * available to them (particularly common in the embedded world). |
3016 | + * |
3017 | + * add_hwgenerator_randomness() is for true hardware RNGs, and will credit |
3018 | + * entropy as specified by the caller. If the entropy pool is full it will |
3019 | + * block until more entropy is needed. |
3020 | + * |
3021 | + * add_bootloader_randomness() is called by bootloader drivers, such as EFI |
3022 | + * and device tree, and credits its input depending on whether or not the |
3023 | + * configuration option CONFIG_RANDOM_TRUST_BOOTLOADER is set. |
3024 | + * |
3025 | + * add_interrupt_randomness() uses the interrupt timing as random |
3026 | + * inputs to the entropy pool. Using the cycle counters and the irq source |
3027 | + * as inputs, it feeds the input pool roughly once a second or after 64 |
3028 | + * interrupts, crediting 1 bit of entropy for whichever comes first. |
3029 | + * |
3030 | + * add_input_randomness() uses the input layer interrupt timing, as well |
3031 | + * as the event type information from the hardware. |
3032 | + * |
3033 | + * add_disk_randomness() uses what amounts to the seek time of block |
3034 | + * layer request events, on a per-disk_devt basis, as input to the |
3035 | + * entropy pool. Note that high-speed solid state drives with very low |
3036 | + * seek times do not make for good sources of entropy, as their seek |
3037 | + * times are usually fairly consistent. |
3038 | + * |
3039 | + * The last two routines try to estimate how many bits of entropy |
3040 | + * to credit. They do this by keeping track of the first and second |
3041 | + * order deltas of the event timings. |
3042 | + * |
3043 | + **********************************************************************/ |
3044 | |
3045 | -/* |
3046 | - * Used as a workqueue function so that when the input pool is getting |
3047 | - * full, we can "spill over" some entropy to the output pools. That |
3048 | - * way the output pools can store some of the excess entropy instead |
3049 | - * of letting it go to waste. |
3050 | - */ |
3051 | -static void push_to_pool(struct work_struct *work) |
3052 | +static bool trust_cpu __initdata = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU); |
3053 | +static bool trust_bootloader __initdata = IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER); |
3054 | +static int __init parse_trust_cpu(char *arg) |
3055 | { |
3056 | - struct entropy_store *r = container_of(work, struct entropy_store, |
3057 | - push_work); |
3058 | - BUG_ON(!r); |
3059 | - _xfer_secondary_pool(r, random_read_wakeup_bits/8); |
3060 | - trace_push_to_pool(r->name, r->entropy_count >> ENTROPY_SHIFT, |
3061 | - r->pull->entropy_count >> ENTROPY_SHIFT); |
3062 | + return kstrtobool(arg, &trust_cpu); |
3063 | } |
3064 | - |
3065 | -/* |
3066 | - * This function decides how many bytes to actually take from the |
3067 | - * given pool, and also debits the entropy count accordingly. |
3068 | - */ |
3069 | -static size_t account(struct entropy_store *r, size_t nbytes, int min, |
3070 | - int reserved) |
3071 | +static int __init parse_trust_bootloader(char *arg) |
3072 | { |
3073 | - int entropy_count, orig; |
3074 | - size_t ibytes, nfrac; |
3075 | - |
3076 | - BUG_ON(r->entropy_count > r->poolinfo->poolfracbits); |
3077 | - |
3078 | - /* Can we pull enough? */ |
3079 | -retry: |
3080 | - entropy_count = orig = ACCESS_ONCE(r->entropy_count); |
3081 | - ibytes = nbytes; |
3082 | - /* If limited, never pull more than available */ |
3083 | - if (r->limit) { |
3084 | - int have_bytes = entropy_count >> (ENTROPY_SHIFT + 3); |
3085 | - |
3086 | - if ((have_bytes -= reserved) < 0) |
3087 | - have_bytes = 0; |
3088 | - ibytes = min_t(size_t, ibytes, have_bytes); |
3089 | - } |
3090 | - if (ibytes < min) |
3091 | - ibytes = 0; |
3092 | - |
3093 | - if (unlikely(entropy_count < 0)) { |
3094 | - pr_warn("random: negative entropy count: pool %s count %d\n", |
3095 | - r->name, entropy_count); |
3096 | - WARN_ON(1); |
3097 | - entropy_count = 0; |
3098 | - } |
3099 | - nfrac = ibytes << (ENTROPY_SHIFT + 3); |
3100 | - if ((size_t) entropy_count > nfrac) |
3101 | - entropy_count -= nfrac; |
3102 | - else |
3103 | - entropy_count = 0; |
3104 | - |
3105 | - if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig) |
3106 | - goto retry; |
3107 | - |
3108 | - trace_debit_entropy(r->name, 8 * ibytes); |
3109 | - if (ibytes && |
3110 | - (r->entropy_count >> ENTROPY_SHIFT) < random_write_wakeup_bits) { |
3111 | - wake_up_interruptible(&random_write_wait); |
3112 | - kill_fasync(&fasync, SIGIO, POLL_OUT); |
3113 | - } |
3114 | - |
3115 | - return ibytes; |
3116 | + return kstrtobool(arg, &trust_bootloader); |
3117 | } |
3118 | +early_param("random.trust_cpu", parse_trust_cpu); |
3119 | +early_param("random.trust_bootloader", parse_trust_bootloader); |
3120 | |
3121 | /* |
3122 | - * This function does the actual extraction for extract_entropy and |
3123 | - * extract_entropy_user. |
3124 | - * |
3125 | - * Note: we assume that .poolwords is a multiple of 16 words. |
3126 | + * The first collection of entropy occurs at system boot while interrupts |
3127 | + * are still turned off. Here we push in latent entropy, RDSEED, a timestamp, |
3128 | + * utsname(), and the command line. Depending on the above configuration knob, |
3129 | + * RDSEED may be considered sufficient for initialization. Note that much |
3130 | + * earlier setup may already have pushed entropy into the input pool by the |
3131 | + * time we get here. |
3132 | */ |
3133 | -static void extract_buf(struct entropy_store *r, __u8 *out) |
3134 | +int __init random_init(const char *command_line) |
3135 | { |
3136 | - int i; |
3137 | - union { |
3138 | - __u32 w[5]; |
3139 | - unsigned long l[LONGS(20)]; |
3140 | - } hash; |
3141 | - __u32 workspace[SHA_WORKSPACE_WORDS]; |
3142 | - unsigned long flags; |
3143 | - |
3144 | - /* |
3145 | - * If we have an architectural hardware random number |
3146 | - * generator, use it for SHA's initial vector |
3147 | - */ |
3148 | - sha_init(hash.w); |
3149 | - for (i = 0; i < LONGS(20); i++) { |
3150 | - unsigned long v; |
3151 | - if (!arch_get_random_long(&v)) |
3152 | - break; |
3153 | - hash.l[i] = v; |
3154 | - } |
3155 | - |
3156 | - /* Generate a hash across the pool, 16 words (512 bits) at a time */ |
3157 | - spin_lock_irqsave(&r->lock, flags); |
3158 | - for (i = 0; i < r->poolinfo->poolwords; i += 16) |
3159 | - sha_transform(hash.w, (__u8 *)(r->pool + i), workspace); |
3160 | - |
3161 | - /* |
3162 | - * We mix the hash back into the pool to prevent backtracking |
3163 | - * attacks (where the attacker knows the state of the pool |
3164 | - * plus the current outputs, and attempts to find previous |
3165 | - * ouputs), unless the hash function can be inverted. By |
3166 | - * mixing at least a SHA1 worth of hash data back, we make |
3167 | - * brute-forcing the feedback as hard as brute-forcing the |
3168 | - * hash. |
3169 | - */ |
3170 | - __mix_pool_bytes(r, hash.w, sizeof(hash.w)); |
3171 | - spin_unlock_irqrestore(&r->lock, flags); |
3172 | - |
3173 | - memzero_explicit(workspace, sizeof(workspace)); |
3174 | - |
3175 | - /* |
3176 | - * In case the hash function has some recognizable output |
3177 | - * pattern, we fold it in half. Thus, we always feed back |
3178 | - * twice as much data as we output. |
3179 | - */ |
3180 | - hash.w[0] ^= hash.w[3]; |
3181 | - hash.w[1] ^= hash.w[4]; |
3182 | - hash.w[2] ^= rol32(hash.w[2], 16); |
3183 | - |
3184 | - memcpy(out, &hash, EXTRACT_SIZE); |
3185 | - memzero_explicit(&hash, sizeof(hash)); |
3186 | -} |
3187 | - |
3188 | -static ssize_t _extract_entropy(struct entropy_store *r, void *buf, |
3189 | - size_t nbytes, int fips) |
3190 | -{ |
3191 | - ssize_t ret = 0, i; |
3192 | - __u8 tmp[EXTRACT_SIZE]; |
3193 | - unsigned long flags; |
3194 | + ktime_t now = ktime_get_real(); |
3195 | + unsigned int i, arch_bits; |
3196 | + unsigned long entropy; |
3197 | |
3198 | - while (nbytes) { |
3199 | - extract_buf(r, tmp); |
3200 | +#if defined(LATENT_ENTROPY_PLUGIN) |
3201 | + static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy; |
3202 | + _mix_pool_bytes(compiletime_seed, sizeof(compiletime_seed)); |
3203 | +#endif |
3204 | |
3205 | - if (fips) { |
3206 | - spin_lock_irqsave(&r->lock, flags); |
3207 | - if (!memcmp(tmp, r->last_data, EXTRACT_SIZE)) |
3208 | - panic("Hardware RNG duplicated output!\n"); |
3209 | - memcpy(r->last_data, tmp, EXTRACT_SIZE); |
3210 | - spin_unlock_irqrestore(&r->lock, flags); |
3211 | + for (i = 0, arch_bits = BLAKE2S_BLOCK_SIZE * 8; |
3212 | + i < BLAKE2S_BLOCK_SIZE; i += sizeof(entropy)) { |
3213 | + if (!arch_get_random_seed_long_early(&entropy) && |
3214 | + !arch_get_random_long_early(&entropy)) { |
3215 | + entropy = random_get_entropy(); |
3216 | + arch_bits -= sizeof(entropy) * 8; |
3217 | } |
3218 | - i = min_t(int, nbytes, EXTRACT_SIZE); |
3219 | - memcpy(buf, tmp, i); |
3220 | - nbytes -= i; |
3221 | - buf += i; |
3222 | - ret += i; |
3223 | + _mix_pool_bytes(&entropy, sizeof(entropy)); |
3224 | } |
3225 | + _mix_pool_bytes(&now, sizeof(now)); |
3226 | + _mix_pool_bytes(utsname(), sizeof(*(utsname()))); |
3227 | + _mix_pool_bytes(command_line, strlen(command_line)); |
3228 | + add_latent_entropy(); |
3229 | |
3230 | - /* Wipe data just returned from memory */ |
3231 | - memzero_explicit(tmp, sizeof(tmp)); |
3232 | + if (crng_ready()) |
3233 | + crng_reseed(); |
3234 | + else if (trust_cpu) |
3235 | + _credit_init_bits(arch_bits); |
3236 | |
3237 | - return ret; |
3238 | + return 0; |
3239 | } |
3240 | |
3241 | /* |
3242 | - * This function extracts randomness from the "entropy pool", and |
3243 | - * returns it in a buffer. |
3244 | + * Add device- or boot-specific data to the input pool to help |
3245 | + * initialize it. |
3246 | * |
3247 | - * The min parameter specifies the minimum amount we can pull before |
3248 | - * failing to avoid races that defeat catastrophic reseeding while the |
3249 | - * reserved parameter indicates how much entropy we must leave in the |
3250 | - * pool after each pull to avoid starving other readers. |
3251 | + * None of this adds any entropy; it is meant to avoid the problem of |
3252 | + * the entropy pool having similar initial state across largely |
3253 | + * identical devices. |
3254 | */ |
3255 | -static ssize_t extract_entropy(struct entropy_store *r, void *buf, |
3256 | - size_t nbytes, int min, int reserved) |
3257 | +void add_device_randomness(const void *buf, size_t len) |
3258 | { |
3259 | - __u8 tmp[EXTRACT_SIZE]; |
3260 | + unsigned long entropy = random_get_entropy(); |
3261 | unsigned long flags; |
3262 | |
3263 | - /* if last_data isn't primed, we need EXTRACT_SIZE extra bytes */ |
3264 | - if (fips_enabled) { |
3265 | - spin_lock_irqsave(&r->lock, flags); |
3266 | - if (!r->last_data_init) { |
3267 | - r->last_data_init = 1; |
3268 | - spin_unlock_irqrestore(&r->lock, flags); |
3269 | - trace_extract_entropy(r->name, EXTRACT_SIZE, |
3270 | - ENTROPY_BITS(r), _RET_IP_); |
3271 | - xfer_secondary_pool(r, EXTRACT_SIZE); |
3272 | - extract_buf(r, tmp); |
3273 | - spin_lock_irqsave(&r->lock, flags); |
3274 | - memcpy(r->last_data, tmp, EXTRACT_SIZE); |
3275 | - } |
3276 | - spin_unlock_irqrestore(&r->lock, flags); |
3277 | - } |
3278 | - |
3279 | - trace_extract_entropy(r->name, nbytes, ENTROPY_BITS(r), _RET_IP_); |
3280 | - xfer_secondary_pool(r, nbytes); |
3281 | - nbytes = account(r, nbytes, min, reserved); |
3282 | - |
3283 | - return _extract_entropy(r, buf, nbytes, fips_enabled); |
3284 | + spin_lock_irqsave(&input_pool.lock, flags); |
3285 | + _mix_pool_bytes(&entropy, sizeof(entropy)); |
3286 | + _mix_pool_bytes(buf, len); |
3287 | + spin_unlock_irqrestore(&input_pool.lock, flags); |
3288 | } |
3289 | +EXPORT_SYMBOL(add_device_randomness); |
3290 | |
3291 | /* |
3292 | - * This function extracts randomness from the "entropy pool", and |
3293 | - * returns it in a userspace buffer. |
3294 | + * Interface for in-kernel drivers of true hardware RNGs. |
3295 | + * Those devices may produce endless random bits and will be throttled |
3296 | + * when our pool is full. |
3297 | */ |
3298 | -static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf, |
3299 | - size_t nbytes) |
3300 | +void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy) |
3301 | { |
3302 | - ssize_t ret = 0, i; |
3303 | - __u8 tmp[EXTRACT_SIZE]; |
3304 | - int large_request = (nbytes > 256); |
3305 | - |
3306 | - trace_extract_entropy_user(r->name, nbytes, ENTROPY_BITS(r), _RET_IP_); |
3307 | - xfer_secondary_pool(r, nbytes); |
3308 | - nbytes = account(r, nbytes, 0, 0); |
3309 | - |
3310 | - while (nbytes) { |
3311 | - if (large_request && need_resched()) { |
3312 | - if (signal_pending(current)) { |
3313 | - if (ret == 0) |
3314 | - ret = -ERESTARTSYS; |
3315 | - break; |
3316 | - } |
3317 | - schedule(); |
3318 | - } |
3319 | - |
3320 | - extract_buf(r, tmp); |
3321 | - i = min_t(int, nbytes, EXTRACT_SIZE); |
3322 | - if (copy_to_user(buf, tmp, i)) { |
3323 | - ret = -EFAULT; |
3324 | - break; |
3325 | - } |
3326 | + mix_pool_bytes(buf, len); |
3327 | + credit_init_bits(entropy); |
3328 | |
3329 | - nbytes -= i; |
3330 | - buf += i; |
3331 | - ret += i; |
3332 | - } |
3333 | - |
3334 | - /* Wipe data just returned from memory */ |
3335 | - memzero_explicit(tmp, sizeof(tmp)); |
3336 | - |
3337 | - return ret; |
3338 | + /* |
3339 | + * Throttle writing to once every CRNG_RESEED_INTERVAL, unless |
3340 | + * we're not yet initialized. |
3341 | + */ |
3342 | + if (!kthread_should_stop() && crng_ready()) |
3343 | + schedule_timeout_interruptible(CRNG_RESEED_INTERVAL); |
3344 | } |
3345 | +EXPORT_SYMBOL_GPL(add_hwgenerator_randomness); |
3346 | |
3347 | /* |
3348 | - * This function is the exported kernel interface. It returns some |
3349 | - * number of good random numbers, suitable for key generation, seeding |
3350 | - * TCP sequence numbers, etc. It does not rely on the hardware random |
3351 | - * number generator. For random bytes direct from the hardware RNG |
3352 | - * (when available), use get_random_bytes_arch(). |
3353 | + * Handle random seed passed by bootloader, and credit it if |
3354 | + * CONFIG_RANDOM_TRUST_BOOTLOADER is set. |
3355 | */ |
3356 | -void get_random_bytes(void *buf, int nbytes) |
3357 | +void __init add_bootloader_randomness(const void *buf, size_t len) |
3358 | { |
3359 | - __u8 tmp[CHACHA20_BLOCK_SIZE]; |
3360 | + mix_pool_bytes(buf, len); |
3361 | + if (trust_bootloader) |
3362 | + credit_init_bits(len * 8); |
3363 | +} |
3364 | |
3365 | -#if DEBUG_RANDOM_BOOT > 0 |
3366 | - if (!crng_ready()) |
3367 | - printk(KERN_NOTICE "random: %pF get_random_bytes called " |
3368 | - "with crng_init = %d\n", (void *) _RET_IP_, crng_init); |
3369 | -#endif |
3370 | - trace_get_random_bytes(nbytes, _RET_IP_); |
3371 | +struct fast_pool { |
3372 | + struct work_struct mix; |
3373 | + unsigned long pool[4]; |
3374 | + unsigned long last; |
3375 | + unsigned int count; |
3376 | +}; |
3377 | |
3378 | - while (nbytes >= CHACHA20_BLOCK_SIZE) { |
3379 | - extract_crng(buf); |
3380 | - buf += CHACHA20_BLOCK_SIZE; |
3381 | - nbytes -= CHACHA20_BLOCK_SIZE; |
3382 | - } |
3383 | +static DEFINE_PER_CPU(struct fast_pool, irq_randomness) = { |
3384 | +#ifdef CONFIG_64BIT |
3385 | +#define FASTMIX_PERM SIPHASH_PERMUTATION |
3386 | + .pool = { SIPHASH_CONST_0, SIPHASH_CONST_1, SIPHASH_CONST_2, SIPHASH_CONST_3 } |
3387 | +#else |
3388 | +#define FASTMIX_PERM HSIPHASH_PERMUTATION |
3389 | + .pool = { HSIPHASH_CONST_0, HSIPHASH_CONST_1, HSIPHASH_CONST_2, HSIPHASH_CONST_3 } |
3390 | +#endif |
3391 | +}; |
3392 | |
3393 | - if (nbytes > 0) { |
3394 | - extract_crng(tmp); |
3395 | - memcpy(buf, tmp, nbytes); |
3396 | - crng_backtrack_protect(tmp, nbytes); |
3397 | - } else |
3398 | - crng_backtrack_protect(tmp, CHACHA20_BLOCK_SIZE); |
3399 | - memzero_explicit(tmp, sizeof(tmp)); |
3400 | +/* |
3401 | + * This is [Half]SipHash-1-x, starting from an empty key. Because |
3402 | + * the key is fixed, it assumes that its inputs are non-malicious, |
3403 | + * and therefore this has no security on its own. s represents the |
3404 | + * four-word SipHash state, while v represents a two-word input. |
3405 | + */ |
3406 | +static void fast_mix(unsigned long s[4], unsigned long v1, unsigned long v2) |
3407 | +{ |
3408 | + s[3] ^= v1; |
3409 | + FASTMIX_PERM(s[0], s[1], s[2], s[3]); |
3410 | + s[0] ^= v1; |
3411 | + s[3] ^= v2; |
3412 | + FASTMIX_PERM(s[0], s[1], s[2], s[3]); |
3413 | + s[0] ^= v2; |
3414 | } |
3415 | -EXPORT_SYMBOL(get_random_bytes); |
3416 | |
3417 | +#ifdef CONFIG_SMP |
3418 | /* |
3419 | - * Add a callback function that will be invoked when the nonblocking |
3420 | - * pool is initialised. |
3421 | - * |
3422 | - * returns: 0 if callback is successfully added |
3423 | - * -EALREADY if pool is already initialised (callback not called) |
3424 | - * -ENOENT if module for callback is not alive |
3425 | + * This function is called when the CPU has just come online, with |
3426 | + * entry CPUHP_AP_RANDOM_ONLINE, just after CPUHP_AP_WORKQUEUE_ONLINE. |
3427 | */ |
3428 | -int add_random_ready_callback(struct random_ready_callback *rdy) |
3429 | +int __cold random_online_cpu(unsigned int cpu) |
3430 | { |
3431 | - struct module *owner; |
3432 | - unsigned long flags; |
3433 | - int err = -EALREADY; |
3434 | + /* |
3435 | + * During CPU shutdown and before CPU onlining, add_interrupt_ |
3436 | + * randomness() may schedule mix_interrupt_randomness(), and |
3437 | + * set the MIX_INFLIGHT flag. However, because the worker can |
3438 | + * be scheduled on a different CPU during this period, that |
3439 | + * flag will never be cleared. For that reason, we zero out |
3440 | + * the flag here, which runs just after workqueues are onlined |
3441 | + * for the CPU again. This also has the effect of setting the |
3442 | + * irq randomness count to zero so that new accumulated irqs |
3443 | + * are fresh. |
3444 | + */ |
3445 | + per_cpu_ptr(&irq_randomness, cpu)->count = 0; |
3446 | + return 0; |
3447 | +} |
3448 | +#endif |
3449 | |
3450 | - if (crng_ready()) |
3451 | - return err; |
3452 | +static void mix_interrupt_randomness(struct work_struct *work) |
3453 | +{ |
3454 | + struct fast_pool *fast_pool = container_of(work, struct fast_pool, mix); |
3455 | + /* |
3456 | + * The size of the copied stack pool is explicitly 2 longs so that we |
3457 | + * only ever ingest half of the siphash output each time, retaining |
3458 | + * the other half as the next "key" that carries over. The entropy is |
3459 | + * supposed to be sufficiently dispersed between bits so on average |
3460 | + * we don't wind up "losing" some. |
3461 | + */ |
3462 | + unsigned long pool[2]; |
3463 | + unsigned int count; |
3464 | |
3465 | - owner = rdy->owner; |
3466 | - if (!try_module_get(owner)) |
3467 | - return -ENOENT; |
3468 | + /* Check to see if we're running on the wrong CPU due to hotplug. */ |
3469 | + local_irq_disable(); |
3470 | + if (fast_pool != this_cpu_ptr(&irq_randomness)) { |
3471 | + local_irq_enable(); |
3472 | + return; |
3473 | + } |
3474 | |
3475 | - spin_lock_irqsave(&random_ready_list_lock, flags); |
3476 | - if (crng_ready()) |
3477 | - goto out; |
3478 | + /* |
3479 | + * Copy the pool to the stack so that the mixer always has a |
3480 | + * consistent view, before we reenable irqs again. |
3481 | + */ |
3482 | + memcpy(pool, fast_pool->pool, sizeof(pool)); |
3483 | + count = fast_pool->count; |
3484 | + fast_pool->count = 0; |
3485 | + fast_pool->last = jiffies; |
3486 | + local_irq_enable(); |
3487 | + |
3488 | + mix_pool_bytes(pool, sizeof(pool)); |
3489 | + credit_init_bits(max(1u, (count & U16_MAX) / 64)); |
3490 | |
3491 | - owner = NULL; |
3492 | + memzero_explicit(pool, sizeof(pool)); |
3493 | +} |
3494 | + |
3495 | +void add_interrupt_randomness(int irq) |
3496 | +{ |
3497 | + enum { MIX_INFLIGHT = 1U << 31 }; |
3498 | + unsigned long entropy = random_get_entropy(); |
3499 | + struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness); |
3500 | + struct pt_regs *regs = get_irq_regs(); |
3501 | + unsigned int new_count; |
3502 | |
3503 | - list_add(&rdy->list, &random_ready_list); |
3504 | - err = 0; |
3505 | + fast_mix(fast_pool->pool, entropy, |
3506 | + (regs ? instruction_pointer(regs) : _RET_IP_) ^ swab(irq)); |
3507 | + new_count = ++fast_pool->count; |
3508 | |
3509 | -out: |
3510 | - spin_unlock_irqrestore(&random_ready_list_lock, flags); |
3511 | + if (new_count & MIX_INFLIGHT) |
3512 | + return; |
3513 | |
3514 | - module_put(owner); |
3515 | + if (new_count < 64 && !time_is_before_jiffies(fast_pool->last + HZ)) |
3516 | + return; |
3517 | |
3518 | - return err; |
3519 | + if (unlikely(!fast_pool->mix.func)) |
3520 | + INIT_WORK(&fast_pool->mix, mix_interrupt_randomness); |
3521 | + fast_pool->count |= MIX_INFLIGHT; |
3522 | + queue_work_on(raw_smp_processor_id(), system_highpri_wq, &fast_pool->mix); |
3523 | } |
3524 | -EXPORT_SYMBOL(add_random_ready_callback); |
3525 | +EXPORT_SYMBOL_GPL(add_interrupt_randomness); |
3526 | + |
3527 | +/* There is one of these per entropy source */ |
3528 | +struct timer_rand_state { |
3529 | + unsigned long last_time; |
3530 | + long last_delta, last_delta2; |
3531 | +}; |
3532 | |
3533 | /* |
3534 | - * Delete a previously registered readiness callback function. |
3535 | + * This function adds entropy to the entropy "pool" by using timing |
3536 | + * delays. It uses the timer_rand_state structure to make an estimate |
3537 | + * of how many bits of entropy this call has added to the pool. The |
3538 | + * value "num" is also added to the pool; it should somehow describe |
3539 | + * the type of event that just happened. |
3540 | */ |
3541 | -void del_random_ready_callback(struct random_ready_callback *rdy) |
3542 | +static void add_timer_randomness(struct timer_rand_state *state, unsigned int num) |
3543 | { |
3544 | - unsigned long flags; |
3545 | - struct module *owner = NULL; |
3546 | + unsigned long entropy = random_get_entropy(), now = jiffies, flags; |
3547 | + long delta, delta2, delta3; |
3548 | + unsigned int bits; |
3549 | |
3550 | - spin_lock_irqsave(&random_ready_list_lock, flags); |
3551 | - if (!list_empty(&rdy->list)) { |
3552 | - list_del_init(&rdy->list); |
3553 | - owner = rdy->owner; |
3554 | + /* |
3555 | + * If we're in a hard IRQ, add_interrupt_randomness() will be called |
3556 | + * sometime after, so mix into the fast pool. |
3557 | + */ |
3558 | + if (in_irq()) { |
3559 | + fast_mix(this_cpu_ptr(&irq_randomness)->pool, entropy, num); |
3560 | + } else { |
3561 | + spin_lock_irqsave(&input_pool.lock, flags); |
3562 | + _mix_pool_bytes(&entropy, sizeof(entropy)); |
3563 | + _mix_pool_bytes(&num, sizeof(num)); |
3564 | + spin_unlock_irqrestore(&input_pool.lock, flags); |
3565 | } |
3566 | - spin_unlock_irqrestore(&random_ready_list_lock, flags); |
3567 | - |
3568 | - module_put(owner); |
3569 | -} |
3570 | -EXPORT_SYMBOL(del_random_ready_callback); |
3571 | |
3572 | -/* |
3573 | - * This function will use the architecture-specific hardware random |
3574 | - * number generator if it is available. The arch-specific hw RNG will |
3575 | - * almost certainly be faster than what we can do in software, but it |
3576 | - * is impossible to verify that it is implemented securely (as |
3577 | - * opposed, to, say, the AES encryption of a sequence number using a |
3578 | - * key known by the NSA). So it's useful if we need the speed, but |
3579 | - * only if we're willing to trust the hardware manufacturer not to |
3580 | - * have put in a back door. |
3581 | - */ |
3582 | -void get_random_bytes_arch(void *buf, int nbytes) |
3583 | -{ |
3584 | - char *p = buf; |
3585 | + if (crng_ready()) |
3586 | + return; |
3587 | |
3588 | - trace_get_random_bytes_arch(nbytes, _RET_IP_); |
3589 | - while (nbytes) { |
3590 | - unsigned long v; |
3591 | - int chunk = min(nbytes, (int)sizeof(unsigned long)); |
3592 | + /* |
3593 | + * Calculate number of bits of randomness we probably added. |
3594 | + * We take into account the first, second and third-order deltas |
3595 | + * in order to make our estimate. |
3596 | + */ |
3597 | + delta = now - READ_ONCE(state->last_time); |
3598 | + WRITE_ONCE(state->last_time, now); |
3599 | + |
3600 | + delta2 = delta - READ_ONCE(state->last_delta); |
3601 | + WRITE_ONCE(state->last_delta, delta); |
3602 | + |
3603 | + delta3 = delta2 - READ_ONCE(state->last_delta2); |
3604 | + WRITE_ONCE(state->last_delta2, delta2); |
3605 | + |
3606 | + if (delta < 0) |
3607 | + delta = -delta; |
3608 | + if (delta2 < 0) |
3609 | + delta2 = -delta2; |
3610 | + if (delta3 < 0) |
3611 | + delta3 = -delta3; |
3612 | + if (delta > delta2) |
3613 | + delta = delta2; |
3614 | + if (delta > delta3) |
3615 | + delta = delta3; |
3616 | |
3617 | - if (!arch_get_random_long(&v)) |
3618 | - break; |
3619 | - |
3620 | - memcpy(p, &v, chunk); |
3621 | - p += chunk; |
3622 | - nbytes -= chunk; |
3623 | - } |
3624 | + /* |
3625 | + * delta is now minimum absolute delta. Round down by 1 bit |
3626 | + * on general principles, and limit entropy estimate to 11 bits. |
3627 | + */ |
3628 | + bits = min(fls(delta >> 1), 11); |
3629 | |
3630 | - if (nbytes) |
3631 | - get_random_bytes(p, nbytes); |
3632 | + /* |
3633 | + * As mentioned above, if we're in a hard IRQ, add_interrupt_randomness() |
3634 | + * will run after this, which uses a different crediting scheme of 1 bit |
3635 | + * per every 64 interrupts. In order to let that function do accounting |
3636 | + * close to the one in this function, we credit a full 64/64 bit per bit, |
3637 | + * and then subtract one to account for the extra one added. |
3638 | + */ |
3639 | + if (in_irq()) |
3640 | + this_cpu_ptr(&irq_randomness)->count += max(1u, bits * 64) - 1; |
3641 | + else |
3642 | + _credit_init_bits(bits); |
3643 | } |
3644 | -EXPORT_SYMBOL(get_random_bytes_arch); |
3645 | - |
3646 | |
3647 | -/* |
3648 | - * init_std_data - initialize pool with system data |
3649 | - * |
3650 | - * @r: pool to initialize |
3651 | - * |
3652 | - * This function clears the pool's entropy count and mixes some system |
3653 | - * data into the pool to prepare it for use. The pool is not cleared |
3654 | - * as that can only decrease the entropy in the pool. |
3655 | - */ |
3656 | -static void init_std_data(struct entropy_store *r) |
3657 | +void add_input_randomness(unsigned int type, unsigned int code, unsigned int value) |
3658 | { |
3659 | - int i; |
3660 | - ktime_t now = ktime_get_real(); |
3661 | - unsigned long rv; |
3662 | - |
3663 | - r->last_pulled = jiffies; |
3664 | - mix_pool_bytes(r, &now, sizeof(now)); |
3665 | - for (i = r->poolinfo->poolbytes; i > 0; i -= sizeof(rv)) { |
3666 | - if (!arch_get_random_seed_long(&rv) && |
3667 | - !arch_get_random_long(&rv)) |
3668 | - rv = random_get_entropy(); |
3669 | - mix_pool_bytes(r, &rv, sizeof(rv)); |
3670 | - } |
3671 | - mix_pool_bytes(r, utsname(), sizeof(*(utsname()))); |
3672 | + static unsigned char last_value; |
3673 | + static struct timer_rand_state input_timer_state = { INITIAL_JIFFIES }; |
3674 | + |
3675 | + /* Ignore autorepeat and the like. */ |
3676 | + if (value == last_value) |
3677 | + return; |
3678 | + |
3679 | + last_value = value; |
3680 | + add_timer_randomness(&input_timer_state, |
3681 | + (type << 4) ^ code ^ (code >> 4) ^ value); |
3682 | } |
3683 | +EXPORT_SYMBOL_GPL(add_input_randomness); |
3684 | |
3685 | -/* |
3686 | - * Note that setup_arch() may call add_device_randomness() |
3687 | - * long before we get here. This allows seeding of the pools |
3688 | - * with some platform dependent data very early in the boot |
3689 | - * process. But it limits our options here. We must use |
3690 | - * statically allocated structures that already have all |
3691 | - * initializations complete at compile time. We should also |
3692 | - * take care not to overwrite the precious per platform data |
3693 | - * we were given. |
3694 | - */ |
3695 | -static int rand_initialize(void) |
3696 | +#ifdef CONFIG_BLOCK |
3697 | +void add_disk_randomness(struct gendisk *disk) |
3698 | { |
3699 | - init_std_data(&input_pool); |
3700 | - init_std_data(&blocking_pool); |
3701 | - crng_initialize(&primary_crng); |
3702 | - crng_global_init_time = jiffies; |
3703 | - if (ratelimit_disable) { |
3704 | - urandom_warning.interval = 0; |
3705 | - unseeded_warning.interval = 0; |
3706 | - } |
3707 | - return 0; |
3708 | + if (!disk || !disk->random) |
3709 | + return; |
3710 | + /* First major is 1, so we get >= 0x200 here. */ |
3711 | + add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); |
3712 | } |
3713 | -early_initcall(rand_initialize); |
3714 | +EXPORT_SYMBOL_GPL(add_disk_randomness); |
3715 | |
3716 | -#ifdef CONFIG_BLOCK |
3717 | -void rand_initialize_disk(struct gendisk *disk) |
3718 | +void __cold rand_initialize_disk(struct gendisk *disk) |
3719 | { |
3720 | struct timer_rand_state *state; |
3721 | |
3722 | @@ -1752,134 +1127,189 @@ void rand_initialize_disk(struct gendisk *disk) |
3723 | } |
3724 | #endif |
3725 | |
3726 | -static ssize_t |
3727 | -_random_read(int nonblock, char __user *buf, size_t nbytes) |
3728 | +/* |
3729 | + * Each time the timer fires, we expect that we got an unpredictable |
3730 | + * jump in the cycle counter. Even if the timer is running on another |
3731 | + * CPU, the timer activity will be touching the stack of the CPU that is |
3732 | + * generating entropy.. |
3733 | + * |
3734 | + * Note that we don't re-arm the timer in the timer itself - we are |
3735 | + * happy to be scheduled away, since that just makes the load more |
3736 | + * complex, but we do not want the timer to keep ticking unless the |
3737 | + * entropy loop is running. |
3738 | + * |
3739 | + * So the re-arming always happens in the entropy loop itself. |
3740 | + */ |
3741 | +static void __cold entropy_timer(unsigned long data) |
3742 | +{ |
3743 | + credit_init_bits(1); |
3744 | +} |
3745 | + |
3746 | +/* |
3747 | + * If we have an actual cycle counter, see if we can |
3748 | + * generate enough entropy with timing noise |
3749 | + */ |
3750 | +static void __cold try_to_generate_entropy(void) |
3751 | { |
3752 | - ssize_t n; |
3753 | + struct { |
3754 | + unsigned long entropy; |
3755 | + struct timer_list timer; |
3756 | + } stack; |
3757 | |
3758 | - if (nbytes == 0) |
3759 | - return 0; |
3760 | + stack.entropy = random_get_entropy(); |
3761 | |
3762 | - nbytes = min_t(size_t, nbytes, SEC_XFER_SIZE); |
3763 | - while (1) { |
3764 | - n = extract_entropy_user(&blocking_pool, buf, nbytes); |
3765 | - if (n < 0) |
3766 | - return n; |
3767 | - trace_random_read(n*8, (nbytes-n)*8, |
3768 | - ENTROPY_BITS(&blocking_pool), |
3769 | - ENTROPY_BITS(&input_pool)); |
3770 | - if (n > 0) |
3771 | - return n; |
3772 | - |
3773 | - /* Pool is (near) empty. Maybe wait and retry. */ |
3774 | - if (nonblock) |
3775 | - return -EAGAIN; |
3776 | + /* Slow counter - or none. Don't even bother */ |
3777 | + if (stack.entropy == random_get_entropy()) |
3778 | + return; |
3779 | |
3780 | - wait_event_interruptible(random_read_wait, |
3781 | - ENTROPY_BITS(&input_pool) >= |
3782 | - random_read_wakeup_bits); |
3783 | - if (signal_pending(current)) |
3784 | - return -ERESTARTSYS; |
3785 | + __setup_timer_on_stack(&stack.timer, entropy_timer, 0, 0); |
3786 | + while (!crng_ready() && !signal_pending(current)) { |
3787 | + if (!timer_pending(&stack.timer)) |
3788 | + mod_timer(&stack.timer, jiffies + 1); |
3789 | + mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); |
3790 | + schedule(); |
3791 | + stack.entropy = random_get_entropy(); |
3792 | } |
3793 | -} |
3794 | |
3795 | -static ssize_t |
3796 | -random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) |
3797 | -{ |
3798 | - return _random_read(file->f_flags & O_NONBLOCK, buf, nbytes); |
3799 | + del_timer_sync(&stack.timer); |
3800 | + destroy_timer_on_stack(&stack.timer); |
3801 | + mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); |
3802 | } |
3803 | |
3804 | -static ssize_t |
3805 | -urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) |
3806 | + |
3807 | +/********************************************************************** |
3808 | + * |
3809 | + * Userspace reader/writer interfaces. |
3810 | + * |
3811 | + * getrandom(2) is the primary modern interface into the RNG and should |
3812 | + * be used in preference to anything else. |
3813 | + * |
3814 | + * Reading from /dev/random has the same functionality as calling |
3815 | + * getrandom(2) with flags=0. In earlier versions, however, it had |
3816 | + * vastly different semantics and should therefore be avoided, to |
3817 | + * prevent backwards compatibility issues. |
3818 | + * |
3819 | + * Reading from /dev/urandom has the same functionality as calling |
3820 | + * getrandom(2) with flags=GRND_INSECURE. Because it does not block |
3821 | + * waiting for the RNG to be ready, it should not be used. |
3822 | + * |
3823 | + * Writing to either /dev/random or /dev/urandom adds entropy to |
3824 | + * the input pool but does not credit it. |
3825 | + * |
3826 | + * Polling on /dev/random indicates when the RNG is initialized, on |
3827 | + * the read side, and when it wants new entropy, on the write side. |
3828 | + * |
3829 | + * Both /dev/random and /dev/urandom have the same set of ioctls for |
3830 | + * adding entropy, getting the entropy count, zeroing the count, and |
3831 | + * reseeding the crng. |
3832 | + * |
3833 | + **********************************************************************/ |
3834 | + |
3835 | +SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags) |
3836 | { |
3837 | - unsigned long flags; |
3838 | - static int maxwarn = 10; |
3839 | + struct iov_iter iter; |
3840 | + struct iovec iov; |
3841 | int ret; |
3842 | |
3843 | - if (!crng_ready() && maxwarn > 0) { |
3844 | - maxwarn--; |
3845 | - if (__ratelimit(&urandom_warning)) |
3846 | - printk(KERN_NOTICE "random: %s: uninitialized " |
3847 | - "urandom read (%zd bytes read)\n", |
3848 | - current->comm, nbytes); |
3849 | - spin_lock_irqsave(&primary_crng.lock, flags); |
3850 | - crng_init_cnt = 0; |
3851 | - spin_unlock_irqrestore(&primary_crng.lock, flags); |
3852 | + if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) |
3853 | + return -EINVAL; |
3854 | + |
3855 | + /* |
3856 | + * Requesting insecure and blocking randomness at the same time makes |
3857 | + * no sense. |
3858 | + */ |
3859 | + if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM)) |
3860 | + return -EINVAL; |
3861 | + |
3862 | + if (!crng_ready() && !(flags & GRND_INSECURE)) { |
3863 | + if (flags & GRND_NONBLOCK) |
3864 | + return -EAGAIN; |
3865 | + ret = wait_for_random_bytes(); |
3866 | + if (unlikely(ret)) |
3867 | + return ret; |
3868 | } |
3869 | - nbytes = min_t(size_t, nbytes, INT_MAX >> (ENTROPY_SHIFT + 3)); |
3870 | - ret = extract_crng_user(buf, nbytes); |
3871 | - trace_urandom_read(8 * nbytes, 0, ENTROPY_BITS(&input_pool)); |
3872 | - return ret; |
3873 | + |
3874 | + ret = import_single_range(READ, ubuf, len, &iov, &iter); |
3875 | + if (unlikely(ret)) |
3876 | + return ret; |
3877 | + return get_random_bytes_user(&iter); |
3878 | } |
3879 | |
3880 | -static unsigned int |
3881 | -random_poll(struct file *file, poll_table * wait) |
3882 | +static unsigned int random_poll(struct file *file, poll_table *wait) |
3883 | { |
3884 | - unsigned int mask; |
3885 | - |
3886 | - poll_wait(file, &random_read_wait, wait); |
3887 | - poll_wait(file, &random_write_wait, wait); |
3888 | - mask = 0; |
3889 | - if (ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits) |
3890 | - mask |= POLLIN | POLLRDNORM; |
3891 | - if (ENTROPY_BITS(&input_pool) < random_write_wakeup_bits) |
3892 | - mask |= POLLOUT | POLLWRNORM; |
3893 | - return mask; |
3894 | + poll_wait(file, &crng_init_wait, wait); |
3895 | + return crng_ready() ? POLLIN | POLLRDNORM : POLLOUT | POLLWRNORM; |
3896 | } |
3897 | |
3898 | -static int |
3899 | -write_pool(struct entropy_store *r, const char __user *buffer, size_t count) |
3900 | +static ssize_t write_pool_user(struct iov_iter *iter) |
3901 | { |
3902 | - size_t bytes; |
3903 | - __u32 t, buf[16]; |
3904 | - const char __user *p = buffer; |
3905 | + u8 block[BLAKE2S_BLOCK_SIZE]; |
3906 | + ssize_t ret = 0; |
3907 | + size_t copied; |
3908 | |
3909 | - while (count > 0) { |
3910 | - int b, i = 0; |
3911 | + if (unlikely(!iov_iter_count(iter))) |
3912 | + return 0; |
3913 | |
3914 | - bytes = min(count, sizeof(buf)); |
3915 | - if (copy_from_user(&buf, p, bytes)) |
3916 | - return -EFAULT; |
3917 | + for (;;) { |
3918 | + copied = copy_from_iter(block, sizeof(block), iter); |
3919 | + ret += copied; |
3920 | + mix_pool_bytes(block, copied); |
3921 | + if (!iov_iter_count(iter) || copied != sizeof(block)) |
3922 | + break; |
3923 | |
3924 | - for (b = bytes ; b > 0 ; b -= sizeof(__u32), i++) { |
3925 | - if (!arch_get_random_int(&t)) |
3926 | + BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0); |
3927 | + if (ret % PAGE_SIZE == 0) { |
3928 | + if (signal_pending(current)) |
3929 | break; |
3930 | - buf[i] ^= t; |
3931 | + cond_resched(); |
3932 | } |
3933 | + } |
3934 | |
3935 | - count -= bytes; |
3936 | - p += bytes; |
3937 | + memzero_explicit(block, sizeof(block)); |
3938 | + return ret ? ret : -EFAULT; |
3939 | +} |
3940 | + |
3941 | +static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *iter) |
3942 | +{ |
3943 | + return write_pool_user(iter); |
3944 | +} |
3945 | |
3946 | - mix_pool_bytes(r, buf, bytes); |
3947 | - cond_resched(); |
3948 | +static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *iter) |
3949 | +{ |
3950 | + static int maxwarn = 10; |
3951 | + |
3952 | + if (!crng_ready()) { |
3953 | + if (!ratelimit_disable && maxwarn <= 0) |
3954 | + ++urandom_warning.missed; |
3955 | + else if (ratelimit_disable || __ratelimit(&urandom_warning)) { |
3956 | + --maxwarn; |
3957 | + pr_notice("%s: uninitialized urandom read (%zu bytes read)\n", |
3958 | + current->comm, iov_iter_count(iter)); |
3959 | + } |
3960 | } |
3961 | |
3962 | - return 0; |
3963 | + return get_random_bytes_user(iter); |
3964 | } |
3965 | |
3966 | -static ssize_t random_write(struct file *file, const char __user *buffer, |
3967 | - size_t count, loff_t *ppos) |
3968 | +static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *iter) |
3969 | { |
3970 | - size_t ret; |
3971 | + int ret; |
3972 | |
3973 | - ret = write_pool(&input_pool, buffer, count); |
3974 | - if (ret) |
3975 | + ret = wait_for_random_bytes(); |
3976 | + if (ret != 0) |
3977 | return ret; |
3978 | - |
3979 | - return (ssize_t)count; |
3980 | + return get_random_bytes_user(iter); |
3981 | } |
3982 | |
3983 | static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
3984 | { |
3985 | - int size, ent_count; |
3986 | int __user *p = (int __user *)arg; |
3987 | - int retval; |
3988 | + int ent_count; |
3989 | |
3990 | switch (cmd) { |
3991 | case RNDGETENTCNT: |
3992 | - /* inherently racy, no point locking */ |
3993 | - ent_count = ENTROPY_BITS(&input_pool); |
3994 | - if (put_user(ent_count, p)) |
3995 | + /* Inherently racy, no point locking. */ |
3996 | + if (put_user(input_pool.init_bits, p)) |
3997 | return -EFAULT; |
3998 | return 0; |
3999 | case RNDADDTOENTCNT: |
4000 | @@ -1887,39 +1317,48 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
4001 | return -EPERM; |
4002 | if (get_user(ent_count, p)) |
4003 | return -EFAULT; |
4004 | - return credit_entropy_bits_safe(&input_pool, ent_count); |
4005 | - case RNDADDENTROPY: |
4006 | + if (ent_count < 0) |
4007 | + return -EINVAL; |
4008 | + credit_init_bits(ent_count); |
4009 | + return 0; |
4010 | + case RNDADDENTROPY: { |
4011 | + struct iov_iter iter; |
4012 | + struct iovec iov; |
4013 | + ssize_t ret; |
4014 | + int len; |
4015 | + |
4016 | if (!capable(CAP_SYS_ADMIN)) |
4017 | return -EPERM; |
4018 | if (get_user(ent_count, p++)) |
4019 | return -EFAULT; |
4020 | if (ent_count < 0) |
4021 | return -EINVAL; |
4022 | - if (get_user(size, p++)) |
4023 | + if (get_user(len, p++)) |
4024 | return -EFAULT; |
4025 | - retval = write_pool(&input_pool, (const char __user *)p, |
4026 | - size); |
4027 | - if (retval < 0) |
4028 | - return retval; |
4029 | - return credit_entropy_bits_safe(&input_pool, ent_count); |
4030 | + ret = import_single_range(WRITE, p, len, &iov, &iter); |
4031 | + if (unlikely(ret)) |
4032 | + return ret; |
4033 | + ret = write_pool_user(&iter); |
4034 | + if (unlikely(ret < 0)) |
4035 | + return ret; |
4036 | + /* Since we're crediting, enforce that it was all written into the pool. */ |
4037 | + if (unlikely(ret != len)) |
4038 | + return -EFAULT; |
4039 | + credit_init_bits(ent_count); |
4040 | + return 0; |
4041 | + } |
4042 | case RNDZAPENTCNT: |
4043 | case RNDCLEARPOOL: |
4044 | - /* |
4045 | - * Clear the entropy pool counters. We no longer clear |
4046 | - * the entropy pool, as that's silly. |
4047 | - */ |
4048 | + /* No longer has any effect. */ |
4049 | if (!capable(CAP_SYS_ADMIN)) |
4050 | return -EPERM; |
4051 | - input_pool.entropy_count = 0; |
4052 | - blocking_pool.entropy_count = 0; |
4053 | return 0; |
4054 | case RNDRESEEDCRNG: |
4055 | if (!capable(CAP_SYS_ADMIN)) |
4056 | return -EPERM; |
4057 | - if (crng_init < 2) |
4058 | + if (!crng_ready()) |
4059 | return -ENODATA; |
4060 | - crng_reseed(&primary_crng, &input_pool); |
4061 | - WRITE_ONCE(crng_global_init_time, jiffies - 1); |
4062 | + crng_reseed(); |
4063 | return 0; |
4064 | default: |
4065 | return -EINVAL; |
4066 | @@ -1932,47 +1371,54 @@ static int random_fasync(int fd, struct file *filp, int on) |
4067 | } |
4068 | |
4069 | const struct file_operations random_fops = { |
4070 | - .read = random_read, |
4071 | - .write = random_write, |
4072 | - .poll = random_poll, |
4073 | + .read_iter = random_read_iter, |
4074 | + .write_iter = random_write_iter, |
4075 | + .poll = random_poll, |
4076 | .unlocked_ioctl = random_ioctl, |
4077 | .fasync = random_fasync, |
4078 | .llseek = noop_llseek, |
4079 | + .splice_read = generic_file_splice_read, |
4080 | + .splice_write = iter_file_splice_write, |
4081 | }; |
4082 | |
4083 | const struct file_operations urandom_fops = { |
4084 | - .read = urandom_read, |
4085 | - .write = random_write, |
4086 | + .read_iter = urandom_read_iter, |
4087 | + .write_iter = random_write_iter, |
4088 | .unlocked_ioctl = random_ioctl, |
4089 | .fasync = random_fasync, |
4090 | .llseek = noop_llseek, |
4091 | + .splice_read = generic_file_splice_read, |
4092 | + .splice_write = iter_file_splice_write, |
4093 | }; |
4094 | |
4095 | -SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, |
4096 | - unsigned int, flags) |
4097 | -{ |
4098 | - if (flags & ~(GRND_NONBLOCK|GRND_RANDOM)) |
4099 | - return -EINVAL; |
4100 | - |
4101 | - if (count > INT_MAX) |
4102 | - count = INT_MAX; |
4103 | - |
4104 | - if (flags & GRND_RANDOM) |
4105 | - return _random_read(flags & GRND_NONBLOCK, buf, count); |
4106 | - |
4107 | - if (!crng_ready()) { |
4108 | - if (flags & GRND_NONBLOCK) |
4109 | - return -EAGAIN; |
4110 | - crng_wait_ready(); |
4111 | - if (signal_pending(current)) |
4112 | - return -ERESTARTSYS; |
4113 | - } |
4114 | - return urandom_read(NULL, buf, count, NULL); |
4115 | -} |
4116 | |
4117 | /******************************************************************** |
4118 | * |
4119 | - * Sysctl interface |
4120 | + * Sysctl interface. |
4121 | + * |
4122 | + * These are partly unused legacy knobs with dummy values to not break |
4123 | + * userspace and partly still useful things. They are usually accessible |
4124 | + * in /proc/sys/kernel/random/ and are as follows: |
4125 | + * |
4126 | + * - boot_id - a UUID representing the current boot. |
4127 | + * |
4128 | + * - uuid - a random UUID, different each time the file is read. |
4129 | + * |
4130 | + * - poolsize - the number of bits of entropy that the input pool can |
4131 | + * hold, tied to the POOL_BITS constant. |
4132 | + * |
4133 | + * - entropy_avail - the number of bits of entropy currently in the |
4134 | + * input pool. Always <= poolsize. |
4135 | + * |
4136 | + * - write_wakeup_threshold - the amount of entropy in the input pool |
4137 | + * below which write polls to /dev/random will unblock, requesting |
4138 | + * more entropy, tied to the POOL_READY_BITS constant. It is writable |
4139 | + * to avoid breaking old userspaces, but writing to it does not |
4140 | + * change any behavior of the RNG. |
4141 | + * |
4142 | + * - urandom_min_reseed_secs - fixed to the value CRNG_RESEED_INTERVAL. |
4143 | + * It is writable to avoid breaking old userspaces, but writing |
4144 | + * to it does not change any behavior of the RNG. |
4145 | * |
4146 | ********************************************************************/ |
4147 | |
4148 | @@ -1980,25 +1426,28 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, |
4149 | |
4150 | #include <linux/sysctl.h> |
4151 | |
4152 | -static int min_read_thresh = 8, min_write_thresh; |
4153 | -static int max_read_thresh = OUTPUT_POOL_WORDS * 32; |
4154 | -static int max_write_thresh = INPUT_POOL_WORDS * 32; |
4155 | -static char sysctl_bootid[16]; |
4156 | +static int sysctl_random_min_urandom_seed = CRNG_RESEED_INTERVAL / HZ; |
4157 | +static int sysctl_random_write_wakeup_bits = POOL_READY_BITS; |
4158 | +static int sysctl_poolsize = POOL_BITS; |
4159 | +static u8 sysctl_bootid[UUID_SIZE]; |
4160 | |
4161 | /* |
4162 | * This function is used to return both the bootid UUID, and random |
4163 | - * UUID. The difference is in whether table->data is NULL; if it is, |
4164 | + * UUID. The difference is in whether table->data is NULL; if it is, |
4165 | * then a new UUID is generated and returned to the user. |
4166 | - * |
4167 | - * If the user accesses this via the proc interface, the UUID will be |
4168 | - * returned as an ASCII string in the standard UUID format; if via the |
4169 | - * sysctl system call, as 16 bytes of binary data. |
4170 | */ |
4171 | -static int proc_do_uuid(struct ctl_table *table, int write, |
4172 | - void __user *buffer, size_t *lenp, loff_t *ppos) |
4173 | +static int proc_do_uuid(struct ctl_table *table, int write, void __user *buf, |
4174 | + size_t *lenp, loff_t *ppos) |
4175 | { |
4176 | - struct ctl_table fake_table; |
4177 | - unsigned char buf[64], tmp_uuid[16], *uuid; |
4178 | + u8 tmp_uuid[UUID_SIZE], *uuid; |
4179 | + char uuid_string[UUID_STRING_LEN + 1]; |
4180 | + struct ctl_table fake_table = { |
4181 | + .data = uuid_string, |
4182 | + .maxlen = UUID_STRING_LEN |
4183 | + }; |
4184 | + |
4185 | + if (write) |
4186 | + return -EPERM; |
4187 | |
4188 | uuid = table->data; |
4189 | if (!uuid) { |
4190 | @@ -2013,32 +1462,17 @@ static int proc_do_uuid(struct ctl_table *table, int write, |
4191 | spin_unlock(&bootid_spinlock); |
4192 | } |
4193 | |
4194 | - sprintf(buf, "%pU", uuid); |
4195 | - |
4196 | - fake_table.data = buf; |
4197 | - fake_table.maxlen = sizeof(buf); |
4198 | - |
4199 | - return proc_dostring(&fake_table, write, buffer, lenp, ppos); |
4200 | + snprintf(uuid_string, sizeof(uuid_string), "%pU", uuid); |
4201 | + return proc_dostring(&fake_table, 0, buf, lenp, ppos); |
4202 | } |
4203 | |
4204 | -/* |
4205 | - * Return entropy available scaled to integral bits |
4206 | - */ |
4207 | -static int proc_do_entropy(struct ctl_table *table, int write, |
4208 | - void __user *buffer, size_t *lenp, loff_t *ppos) |
4209 | +/* The same as proc_dointvec, but writes don't change anything. */ |
4210 | +static int proc_do_rointvec(struct ctl_table *table, int write, void __user *buf, |
4211 | + size_t *lenp, loff_t *ppos) |
4212 | { |
4213 | - struct ctl_table fake_table; |
4214 | - int entropy_count; |
4215 | - |
4216 | - entropy_count = *(int *)table->data >> ENTROPY_SHIFT; |
4217 | - |
4218 | - fake_table.data = &entropy_count; |
4219 | - fake_table.maxlen = sizeof(entropy_count); |
4220 | - |
4221 | - return proc_dointvec(&fake_table, write, buffer, lenp, ppos); |
4222 | + return write ? 0 : proc_dointvec(table, 0, buf, lenp, ppos); |
4223 | } |
4224 | |
4225 | -static int sysctl_poolsize = INPUT_POOL_WORDS * 32; |
4226 | extern struct ctl_table random_table[]; |
4227 | struct ctl_table random_table[] = { |
4228 | { |
4229 | @@ -2050,177 +1484,36 @@ struct ctl_table random_table[] = { |
4230 | }, |
4231 | { |
4232 | .procname = "entropy_avail", |
4233 | + .data = &input_pool.init_bits, |
4234 | .maxlen = sizeof(int), |
4235 | .mode = 0444, |
4236 | - .proc_handler = proc_do_entropy, |
4237 | - .data = &input_pool.entropy_count, |
4238 | - }, |
4239 | - { |
4240 | - .procname = "read_wakeup_threshold", |
4241 | - .data = &random_read_wakeup_bits, |
4242 | - .maxlen = sizeof(int), |
4243 | - .mode = 0644, |
4244 | - .proc_handler = proc_dointvec_minmax, |
4245 | - .extra1 = &min_read_thresh, |
4246 | - .extra2 = &max_read_thresh, |
4247 | + .proc_handler = proc_dointvec, |
4248 | }, |
4249 | { |
4250 | .procname = "write_wakeup_threshold", |
4251 | - .data = &random_write_wakeup_bits, |
4252 | + .data = &sysctl_random_write_wakeup_bits, |
4253 | .maxlen = sizeof(int), |
4254 | .mode = 0644, |
4255 | - .proc_handler = proc_dointvec_minmax, |
4256 | - .extra1 = &min_write_thresh, |
4257 | - .extra2 = &max_write_thresh, |
4258 | + .proc_handler = proc_do_rointvec, |
4259 | }, |
4260 | { |
4261 | .procname = "urandom_min_reseed_secs", |
4262 | - .data = &random_min_urandom_seed, |
4263 | + .data = &sysctl_random_min_urandom_seed, |
4264 | .maxlen = sizeof(int), |
4265 | .mode = 0644, |
4266 | - .proc_handler = proc_dointvec, |
4267 | + .proc_handler = proc_do_rointvec, |
4268 | }, |
4269 | { |
4270 | .procname = "boot_id", |
4271 | .data = &sysctl_bootid, |
4272 | - .maxlen = 16, |
4273 | .mode = 0444, |
4274 | .proc_handler = proc_do_uuid, |
4275 | }, |
4276 | { |
4277 | .procname = "uuid", |
4278 | - .maxlen = 16, |
4279 | .mode = 0444, |
4280 | .proc_handler = proc_do_uuid, |
4281 | }, |
4282 | -#ifdef ADD_INTERRUPT_BENCH |
4283 | - { |
4284 | - .procname = "add_interrupt_avg_cycles", |
4285 | - .data = &avg_cycles, |
4286 | - .maxlen = sizeof(avg_cycles), |
4287 | - .mode = 0444, |
4288 | - .proc_handler = proc_doulongvec_minmax, |
4289 | - }, |
4290 | - { |
4291 | - .procname = "add_interrupt_avg_deviation", |
4292 | - .data = &avg_deviation, |
4293 | - .maxlen = sizeof(avg_deviation), |
4294 | - .mode = 0444, |
4295 | - .proc_handler = proc_doulongvec_minmax, |
4296 | - }, |
4297 | -#endif |
4298 | { } |
4299 | }; |
4300 | -#endif /* CONFIG_SYSCTL */ |
4301 | - |
4302 | -struct batched_entropy { |
4303 | - union { |
4304 | - unsigned long entropy_long[CHACHA20_BLOCK_SIZE / sizeof(unsigned long)]; |
4305 | - unsigned int entropy_int[CHACHA20_BLOCK_SIZE / sizeof(unsigned int)]; |
4306 | - }; |
4307 | - unsigned int position; |
4308 | -}; |
4309 | - |
4310 | -/* |
4311 | - * Get a random word for internal kernel use only. The quality of the random |
4312 | - * number is good as /dev/urandom, but there is no backtrack protection, with |
4313 | - * the goal of being quite fast and not depleting entropy. |
4314 | - */ |
4315 | -static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_long); |
4316 | -unsigned long get_random_long(void) |
4317 | -{ |
4318 | - unsigned long ret; |
4319 | - struct batched_entropy *batch; |
4320 | - |
4321 | - batch = &get_cpu_var(batched_entropy_long); |
4322 | - if (batch->position % ARRAY_SIZE(batch->entropy_long) == 0) { |
4323 | - extract_crng((u8 *)batch->entropy_long); |
4324 | - batch->position = 0; |
4325 | - } |
4326 | - ret = batch->entropy_long[batch->position++]; |
4327 | - put_cpu_var(batched_entropy_long); |
4328 | - return ret; |
4329 | -} |
4330 | -EXPORT_SYMBOL(get_random_long); |
4331 | - |
4332 | -#if BITS_PER_LONG == 32 |
4333 | -unsigned int get_random_int(void) |
4334 | -{ |
4335 | - return get_random_long(); |
4336 | -} |
4337 | -#else |
4338 | -static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_int); |
4339 | -unsigned int get_random_int(void) |
4340 | -{ |
4341 | - unsigned int ret; |
4342 | - struct batched_entropy *batch; |
4343 | - |
4344 | - batch = &get_cpu_var(batched_entropy_int); |
4345 | - if (batch->position % ARRAY_SIZE(batch->entropy_int) == 0) { |
4346 | - extract_crng((u8 *)batch->entropy_int); |
4347 | - batch->position = 0; |
4348 | - } |
4349 | - ret = batch->entropy_int[batch->position++]; |
4350 | - put_cpu_var(batched_entropy_int); |
4351 | - return ret; |
4352 | -} |
4353 | -#endif |
4354 | -EXPORT_SYMBOL(get_random_int); |
4355 | - |
4356 | -/** |
4357 | - * randomize_page - Generate a random, page aligned address |
4358 | - * @start: The smallest acceptable address the caller will take. |
4359 | - * @range: The size of the area, starting at @start, within which the |
4360 | - * random address must fall. |
4361 | - * |
4362 | - * If @start + @range would overflow, @range is capped. |
4363 | - * |
4364 | - * NOTE: Historical use of randomize_range, which this replaces, presumed that |
4365 | - * @start was already page aligned. We now align it regardless. |
4366 | - * |
4367 | - * Return: A page aligned address within [start, start + range). On error, |
4368 | - * @start is returned. |
4369 | - */ |
4370 | -unsigned long |
4371 | -randomize_page(unsigned long start, unsigned long range) |
4372 | -{ |
4373 | - if (!PAGE_ALIGNED(start)) { |
4374 | - range -= PAGE_ALIGN(start) - start; |
4375 | - start = PAGE_ALIGN(start); |
4376 | - } |
4377 | - |
4378 | - if (start > ULONG_MAX - range) |
4379 | - range = ULONG_MAX - start; |
4380 | - |
4381 | - range >>= PAGE_SHIFT; |
4382 | - |
4383 | - if (range == 0) |
4384 | - return start; |
4385 | - |
4386 | - return start + (get_random_long() % range << PAGE_SHIFT); |
4387 | -} |
4388 | - |
4389 | -/* Interface for in-kernel drivers of true hardware RNGs. |
4390 | - * Those devices may produce endless random bits and will be throttled |
4391 | - * when our pool is full. |
4392 | - */ |
4393 | -void add_hwgenerator_randomness(const char *buffer, size_t count, |
4394 | - size_t entropy) |
4395 | -{ |
4396 | - struct entropy_store *poolp = &input_pool; |
4397 | - |
4398 | - if (unlikely(crng_init == 0)) { |
4399 | - crng_fast_load(buffer, count); |
4400 | - return; |
4401 | - } |
4402 | - |
4403 | - /* Suspend writing if we're above the trickle threshold. |
4404 | - * We'll be woken up again once below random_write_wakeup_thresh, |
4405 | - * or when the calling thread is about to terminate. |
4406 | - */ |
4407 | - wait_event_interruptible(random_write_wait, kthread_should_stop() || |
4408 | - ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits); |
4409 | - mix_pool_bytes(poolp, buffer, count); |
4410 | - credit_entropy_bits(poolp, entropy); |
4411 | -} |
4412 | -EXPORT_SYMBOL_GPL(add_hwgenerator_randomness); |
4413 | +#endif /* CONFIG_SYSCTL */ |
4414 | diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c |
4415 | index 9a0d0d0ad6d0f..3248aa7a35b36 100644 |
4416 | --- a/drivers/hv/vmbus_drv.c |
4417 | +++ b/drivers/hv/vmbus_drv.c |
4418 | @@ -828,7 +828,7 @@ static void vmbus_isr(void) |
4419 | tasklet_schedule(hv_context.msg_dpc[cpu]); |
4420 | } |
4421 | |
4422 | - add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0); |
4423 | + add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR); |
4424 | } |
4425 | |
4426 | |
4427 | diff --git a/drivers/irqchip/irq-gic-realview.c b/drivers/irqchip/irq-gic-realview.c |
4428 | index 54c296401525c..61024882c6854 100644 |
4429 | --- a/drivers/irqchip/irq-gic-realview.c |
4430 | +++ b/drivers/irqchip/irq-gic-realview.c |
4431 | @@ -56,6 +56,7 @@ realview_gic_of_init(struct device_node *node, struct device_node *parent) |
4432 | |
4433 | /* The PB11MPCore GIC needs to be configured in the syscon */ |
4434 | map = syscon_node_to_regmap(np); |
4435 | + of_node_put(np); |
4436 | if (!IS_ERR(map)) { |
4437 | /* new irq mode with no DCC */ |
4438 | regmap_write(map, REALVIEW_SYS_LOCK_OFFSET, |
4439 | diff --git a/drivers/irqchip/irq-gic-v3.c b/drivers/irqchip/irq-gic-v3.c |
4440 | index 2ab6060031a43..9ae24ffb9b09c 100644 |
4441 | --- a/drivers/irqchip/irq-gic-v3.c |
4442 | +++ b/drivers/irqchip/irq-gic-v3.c |
4443 | @@ -982,7 +982,7 @@ static int get_cpu_number(struct device_node *dn) |
4444 | { |
4445 | const __be32 *cell; |
4446 | u64 hwid; |
4447 | - int i; |
4448 | + int cpu; |
4449 | |
4450 | cell = of_get_property(dn, "reg", NULL); |
4451 | if (!cell) |
4452 | @@ -996,9 +996,9 @@ static int get_cpu_number(struct device_node *dn) |
4453 | if (hwid & ~MPIDR_HWID_BITMASK) |
4454 | return -1; |
4455 | |
4456 | - for (i = 0; i < num_possible_cpus(); i++) |
4457 | - if (cpu_logical_map(i) == hwid) |
4458 | - return i; |
4459 | + for_each_possible_cpu(cpu) |
4460 | + if (cpu_logical_map(cpu) == hwid) |
4461 | + return cpu; |
4462 | |
4463 | return -1; |
4464 | } |
4465 | diff --git a/drivers/misc/atmel-ssc.c b/drivers/misc/atmel-ssc.c |
4466 | index 8c9a444d61d3c..65bc573d6ab44 100644 |
4467 | --- a/drivers/misc/atmel-ssc.c |
4468 | +++ b/drivers/misc/atmel-ssc.c |
4469 | @@ -190,9 +190,9 @@ static int ssc_probe(struct platform_device *pdev) |
4470 | clk_disable_unprepare(ssc->clk); |
4471 | |
4472 | ssc->irq = platform_get_irq(pdev, 0); |
4473 | - if (!ssc->irq) { |
4474 | + if (ssc->irq < 0) { |
4475 | dev_dbg(&pdev->dev, "could not get irq\n"); |
4476 | - return -ENXIO; |
4477 | + return ssc->irq; |
4478 | } |
4479 | |
4480 | mutex_lock(&user_lock); |
4481 | diff --git a/drivers/net/ethernet/mediatek/mtk_eth_soc.c b/drivers/net/ethernet/mediatek/mtk_eth_soc.c |
4482 | index 84d6679572210..61a9b60ab0225 100644 |
4483 | --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c |
4484 | +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c |
4485 | @@ -527,6 +527,17 @@ static inline void mtk_rx_get_desc(struct mtk_rx_dma *rxd, |
4486 | rxd->rxd4 = READ_ONCE(dma_rxd->rxd4); |
4487 | } |
4488 | |
4489 | +static void *mtk_max_lro_buf_alloc(gfp_t gfp_mask) |
4490 | +{ |
4491 | + unsigned int size = mtk_max_frag_size(MTK_MAX_LRO_RX_LENGTH); |
4492 | + unsigned long data; |
4493 | + |
4494 | + data = __get_free_pages(gfp_mask | __GFP_COMP | __GFP_NOWARN, |
4495 | + get_order(size)); |
4496 | + |
4497 | + return (void *)data; |
4498 | +} |
4499 | + |
4500 | /* the qdma core needs scratch memory to be setup */ |
4501 | static int mtk_init_fq_dma(struct mtk_eth *eth) |
4502 | { |
4503 | @@ -928,7 +939,10 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget, |
4504 | goto release_desc; |
4505 | |
4506 | /* alloc new buffer */ |
4507 | - new_data = napi_alloc_frag(ring->frag_size); |
4508 | + if (ring->frag_size <= PAGE_SIZE) |
4509 | + new_data = napi_alloc_frag(ring->frag_size); |
4510 | + else |
4511 | + new_data = mtk_max_lro_buf_alloc(GFP_ATOMIC); |
4512 | if (unlikely(!new_data)) { |
4513 | netdev->stats.rx_dropped++; |
4514 | goto release_desc; |
4515 | @@ -1231,7 +1245,10 @@ static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag) |
4516 | return -ENOMEM; |
4517 | |
4518 | for (i = 0; i < rx_dma_size; i++) { |
4519 | - ring->data[i] = netdev_alloc_frag(ring->frag_size); |
4520 | + if (ring->frag_size <= PAGE_SIZE) |
4521 | + ring->data[i] = netdev_alloc_frag(ring->frag_size); |
4522 | + else |
4523 | + ring->data[i] = mtk_max_lro_buf_alloc(GFP_KERNEL); |
4524 | if (!ring->data[i]) |
4525 | return -ENOMEM; |
4526 | } |
4527 | diff --git a/drivers/nfc/nfcmrvl/usb.c b/drivers/nfc/nfcmrvl/usb.c |
4528 | index 585a0f20835b1..3263e2a2bdfd3 100644 |
4529 | --- a/drivers/nfc/nfcmrvl/usb.c |
4530 | +++ b/drivers/nfc/nfcmrvl/usb.c |
4531 | @@ -401,13 +401,25 @@ static void nfcmrvl_play_deferred(struct nfcmrvl_usb_drv_data *drv_data) |
4532 | int err; |
4533 | |
4534 | while ((urb = usb_get_from_anchor(&drv_data->deferred))) { |
4535 | + usb_anchor_urb(urb, &drv_data->tx_anchor); |
4536 | + |
4537 | err = usb_submit_urb(urb, GFP_ATOMIC); |
4538 | - if (err) |
4539 | + if (err) { |
4540 | + kfree(urb->setup_packet); |
4541 | + usb_unanchor_urb(urb); |
4542 | + usb_free_urb(urb); |
4543 | break; |
4544 | + } |
4545 | |
4546 | drv_data->tx_in_flight++; |
4547 | + usb_free_urb(urb); |
4548 | + } |
4549 | + |
4550 | + /* Cleanup the rest deferred urbs. */ |
4551 | + while ((urb = usb_get_from_anchor(&drv_data->deferred))) { |
4552 | + kfree(urb->setup_packet); |
4553 | + usb_free_urb(urb); |
4554 | } |
4555 | - usb_scuttle_anchored_urbs(&drv_data->deferred); |
4556 | } |
4557 | |
4558 | static int nfcmrvl_resume(struct usb_interface *intf) |
4559 | diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c |
4560 | index 6a239c9bbf59b..e797fa92c90dd 100644 |
4561 | --- a/drivers/of/fdt.c |
4562 | +++ b/drivers/of/fdt.c |
4563 | @@ -27,6 +27,7 @@ |
4564 | #include <linux/debugfs.h> |
4565 | #include <linux/serial_core.h> |
4566 | #include <linux/sysfs.h> |
4567 | +#include <linux/random.h> |
4568 | |
4569 | #include <asm/setup.h> /* for COMMAND_LINE_SIZE */ |
4570 | #include <asm/page.h> |
4571 | @@ -1068,6 +1069,7 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, |
4572 | { |
4573 | int l; |
4574 | const char *p; |
4575 | + const void *rng_seed; |
4576 | |
4577 | pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname); |
4578 | |
4579 | @@ -1102,6 +1104,14 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, |
4580 | |
4581 | pr_debug("Command line is: %s\n", (char*)data); |
4582 | |
4583 | + rng_seed = of_get_flat_dt_prop(node, "rng-seed", &l); |
4584 | + if (rng_seed && l > 0) { |
4585 | + add_bootloader_randomness(rng_seed, l); |
4586 | + |
4587 | + /* try to clear seed so it won't be found. */ |
4588 | + fdt_nop_property(initial_boot_params, node, "rng-seed"); |
4589 | + } |
4590 | + |
4591 | /* break now */ |
4592 | return 1; |
4593 | } |
4594 | @@ -1203,8 +1213,6 @@ bool __init early_init_dt_verify(void *params) |
4595 | |
4596 | /* Setup flat device-tree pointer */ |
4597 | initial_boot_params = params; |
4598 | - of_fdt_crc32 = crc32_be(~0, initial_boot_params, |
4599 | - fdt_totalsize(initial_boot_params)); |
4600 | return true; |
4601 | } |
4602 | |
4603 | @@ -1230,6 +1238,8 @@ bool __init early_init_dt_scan(void *params) |
4604 | return false; |
4605 | |
4606 | early_init_dt_scan_nodes(); |
4607 | + of_fdt_crc32 = crc32_be(~0, initial_boot_params, |
4608 | + fdt_totalsize(initial_boot_params)); |
4609 | return true; |
4610 | } |
4611 | |
4612 | diff --git a/drivers/scsi/lpfc/lpfc_nportdisc.c b/drivers/scsi/lpfc/lpfc_nportdisc.c |
4613 | index 30b5f65b29d15..7f230d0b2fd62 100644 |
4614 | --- a/drivers/scsi/lpfc/lpfc_nportdisc.c |
4615 | +++ b/drivers/scsi/lpfc/lpfc_nportdisc.c |
4616 | @@ -633,7 +633,8 @@ lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp, |
4617 | else |
4618 | lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL); |
4619 | if (ndlp->nlp_DID == Fabric_DID) { |
4620 | - if (vport->port_state <= LPFC_FDISC) |
4621 | + if (vport->port_state <= LPFC_FDISC || |
4622 | + vport->fc_flag & FC_PT2PT) |
4623 | goto out; |
4624 | lpfc_linkdown_port(vport); |
4625 | spin_lock_irq(shost->host_lock); |
4626 | diff --git a/drivers/scsi/vmw_pvscsi.h b/drivers/scsi/vmw_pvscsi.h |
4627 | index d41292ef85f2f..98ad17cb6643d 100644 |
4628 | --- a/drivers/scsi/vmw_pvscsi.h |
4629 | +++ b/drivers/scsi/vmw_pvscsi.h |
4630 | @@ -333,8 +333,8 @@ struct PVSCSIRingReqDesc { |
4631 | u8 tag; |
4632 | u8 bus; |
4633 | u8 target; |
4634 | - u8 vcpuHint; |
4635 | - u8 unused[59]; |
4636 | + u16 vcpuHint; |
4637 | + u8 unused[58]; |
4638 | } __packed; |
4639 | |
4640 | /* |
4641 | diff --git a/drivers/staging/comedi/drivers/vmk80xx.c b/drivers/staging/comedi/drivers/vmk80xx.c |
4642 | index 36470ee065967..6010c047f4f86 100644 |
4643 | --- a/drivers/staging/comedi/drivers/vmk80xx.c |
4644 | +++ b/drivers/staging/comedi/drivers/vmk80xx.c |
4645 | @@ -694,7 +694,7 @@ static int vmk80xx_alloc_usb_buffers(struct comedi_device *dev) |
4646 | if (!devpriv->usb_rx_buf) |
4647 | return -ENOMEM; |
4648 | |
4649 | - size = max(usb_endpoint_maxp(devpriv->ep_rx), MIN_BUF_SIZE); |
4650 | + size = max(usb_endpoint_maxp(devpriv->ep_tx), MIN_BUF_SIZE); |
4651 | devpriv->usb_tx_buf = kzalloc(size, GFP_KERNEL); |
4652 | if (!devpriv->usb_tx_buf) |
4653 | return -ENOMEM; |
4654 | diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c |
4655 | index 2d83f1dfb4d69..d3f17b23ac61f 100644 |
4656 | --- a/drivers/tty/serial/8250/8250_port.c |
4657 | +++ b/drivers/tty/serial/8250/8250_port.c |
4658 | @@ -1473,6 +1473,8 @@ static inline void __stop_tx(struct uart_8250_port *p) |
4659 | |
4660 | if (em485) { |
4661 | unsigned char lsr = serial_in(p, UART_LSR); |
4662 | + p->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS; |
4663 | + |
4664 | /* |
4665 | * To provide required timeing and allow FIFO transfer, |
4666 | * __stop_tx_rs485() must be called only when both FIFO and |
4667 | diff --git a/drivers/usb/gadget/udc/lpc32xx_udc.c b/drivers/usb/gadget/udc/lpc32xx_udc.c |
4668 | index 7107931617953..498a44ae2b925 100644 |
4669 | --- a/drivers/usb/gadget/udc/lpc32xx_udc.c |
4670 | +++ b/drivers/usb/gadget/udc/lpc32xx_udc.c |
4671 | @@ -3034,6 +3034,7 @@ static int lpc32xx_udc_probe(struct platform_device *pdev) |
4672 | } |
4673 | |
4674 | udc->isp1301_i2c_client = isp1301_get_client(isp1301_node); |
4675 | + of_node_put(isp1301_node); |
4676 | if (!udc->isp1301_i2c_client) { |
4677 | retval = -EPROBE_DEFER; |
4678 | goto phy_fail; |
4679 | diff --git a/drivers/usb/serial/io_ti.c b/drivers/usb/serial/io_ti.c |
4680 | index e98532feb0cce..8e49eb68896ac 100644 |
4681 | --- a/drivers/usb/serial/io_ti.c |
4682 | +++ b/drivers/usb/serial/io_ti.c |
4683 | @@ -172,6 +172,7 @@ static const struct usb_device_id edgeport_2port_id_table[] = { |
4684 | { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) }, |
4685 | { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) }, |
4686 | { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) }, |
4687 | + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_E5805A) }, |
4688 | { } |
4689 | }; |
4690 | |
4691 | @@ -210,6 +211,7 @@ static const struct usb_device_id id_table_combined[] = { |
4692 | { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) }, |
4693 | { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) }, |
4694 | { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) }, |
4695 | + { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_E5805A) }, |
4696 | { } |
4697 | }; |
4698 | |
4699 | diff --git a/drivers/usb/serial/io_usbvend.h b/drivers/usb/serial/io_usbvend.h |
4700 | index 6f6a856bc37cd..a470262dcf48b 100644 |
4701 | --- a/drivers/usb/serial/io_usbvend.h |
4702 | +++ b/drivers/usb/serial/io_usbvend.h |
4703 | @@ -215,6 +215,7 @@ |
4704 | // |
4705 | // Definitions for other product IDs |
4706 | #define ION_DEVICE_ID_MT4X56USB 0x1403 // OEM device |
4707 | +#define ION_DEVICE_ID_E5805A 0x1A01 // OEM device (rebranded Edgeport/4) |
4708 | |
4709 | |
4710 | #define GENERATION_ID_FROM_USB_PRODUCT_ID(ProductId) \ |
4711 | diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.c |
4712 | index 5b198d881a40a..8add5a762f219 100644 |
4713 | --- a/drivers/usb/serial/option.c |
4714 | +++ b/drivers/usb/serial/option.c |
4715 | @@ -430,6 +430,8 @@ static void option_instat_callback(struct urb *urb); |
4716 | #define CINTERION_PRODUCT_CLS8 0x00b0 |
4717 | #define CINTERION_PRODUCT_MV31_MBIM 0x00b3 |
4718 | #define CINTERION_PRODUCT_MV31_RMNET 0x00b7 |
4719 | +#define CINTERION_PRODUCT_MV31_2_MBIM 0x00b8 |
4720 | +#define CINTERION_PRODUCT_MV31_2_RMNET 0x00b9 |
4721 | #define CINTERION_PRODUCT_MV32_WA 0x00f1 |
4722 | #define CINTERION_PRODUCT_MV32_WB 0x00f2 |
4723 | |
4724 | @@ -1953,6 +1955,10 @@ static const struct usb_device_id option_ids[] = { |
4725 | .driver_info = RSVD(3)}, |
4726 | { USB_DEVICE_INTERFACE_CLASS(CINTERION_VENDOR_ID, CINTERION_PRODUCT_MV31_RMNET, 0xff), |
4727 | .driver_info = RSVD(0)}, |
4728 | + { USB_DEVICE_INTERFACE_CLASS(CINTERION_VENDOR_ID, CINTERION_PRODUCT_MV31_2_MBIM, 0xff), |
4729 | + .driver_info = RSVD(3)}, |
4730 | + { USB_DEVICE_INTERFACE_CLASS(CINTERION_VENDOR_ID, CINTERION_PRODUCT_MV31_2_RMNET, 0xff), |
4731 | + .driver_info = RSVD(0)}, |
4732 | { USB_DEVICE_INTERFACE_CLASS(CINTERION_VENDOR_ID, CINTERION_PRODUCT_MV32_WA, 0xff), |
4733 | .driver_info = RSVD(3)}, |
4734 | { USB_DEVICE_INTERFACE_CLASS(CINTERION_VENDOR_ID, CINTERION_PRODUCT_MV32_WB, 0xff), |
4735 | diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c |
4736 | index 50840984fbfac..f62da3b7c27b4 100644 |
4737 | --- a/drivers/virtio/virtio_mmio.c |
4738 | +++ b/drivers/virtio/virtio_mmio.c |
4739 | @@ -630,6 +630,7 @@ static int vm_cmdline_set(const char *device, |
4740 | if (!vm_cmdline_parent_registered) { |
4741 | err = device_register(&vm_cmdline_parent); |
4742 | if (err) { |
4743 | + put_device(&vm_cmdline_parent); |
4744 | pr_err("Failed to register parent device!\n"); |
4745 | return err; |
4746 | } |
4747 | diff --git a/fs/9p/vfs_inode_dotl.c b/fs/9p/vfs_inode_dotl.c |
4748 | index 425bc1a2b8c14..77a97195ef355 100644 |
4749 | --- a/fs/9p/vfs_inode_dotl.c |
4750 | +++ b/fs/9p/vfs_inode_dotl.c |
4751 | @@ -656,14 +656,10 @@ v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode, |
4752 | if (stat->st_result_mask & P9_STATS_NLINK) |
4753 | set_nlink(inode, stat->st_nlink); |
4754 | if (stat->st_result_mask & P9_STATS_MODE) { |
4755 | - inode->i_mode = stat->st_mode; |
4756 | - if ((S_ISBLK(inode->i_mode)) || |
4757 | - (S_ISCHR(inode->i_mode))) |
4758 | - init_special_inode(inode, inode->i_mode, |
4759 | - inode->i_rdev); |
4760 | + mode = stat->st_mode & S_IALLUGO; |
4761 | + mode |= inode->i_mode & ~S_IALLUGO; |
4762 | + inode->i_mode = mode; |
4763 | } |
4764 | - if (stat->st_result_mask & P9_STATS_RDEV) |
4765 | - inode->i_rdev = new_decode_dev(stat->st_rdev); |
4766 | if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE) && |
4767 | stat->st_result_mask & P9_STATS_SIZE) |
4768 | v9fs_i_size_write(inode, stat->st_size); |
4769 | diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c |
4770 | index 2a7fb2cf19b81..f39b656012339 100644 |
4771 | --- a/fs/ext4/mballoc.c |
4772 | +++ b/fs/ext4/mballoc.c |
4773 | @@ -3142,6 +3142,15 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac, |
4774 | size = size >> bsbits; |
4775 | start = start_off >> bsbits; |
4776 | |
4777 | + /* |
4778 | + * For tiny groups (smaller than 8MB) the chosen allocation |
4779 | + * alignment may be larger than group size. Make sure the |
4780 | + * alignment does not move allocation to a different group which |
4781 | + * makes mballoc fail assertions later. |
4782 | + */ |
4783 | + start = max(start, rounddown(ac->ac_o_ex.fe_logical, |
4784 | + (ext4_lblk_t)EXT4_BLOCKS_PER_GROUP(ac->ac_sb))); |
4785 | + |
4786 | /* don't cover already allocated blocks in selected range */ |
4787 | if (ar->pleft && start <= ar->lleft) { |
4788 | size -= ar->lleft + 1 - start; |
4789 | diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c |
4790 | index 159d259e5e5e9..edf78728ed322 100644 |
4791 | --- a/fs/ext4/namei.c |
4792 | +++ b/fs/ext4/namei.c |
4793 | @@ -1726,7 +1726,8 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, |
4794 | struct dx_hash_info *hinfo) |
4795 | { |
4796 | unsigned blocksize = dir->i_sb->s_blocksize; |
4797 | - unsigned count, continued; |
4798 | + unsigned continued; |
4799 | + int count; |
4800 | struct buffer_head *bh2; |
4801 | ext4_lblk_t newblock; |
4802 | u32 hash2; |
4803 | diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c |
4804 | index 51fa706707a3b..c367129dcdc16 100644 |
4805 | --- a/fs/ext4/resize.c |
4806 | +++ b/fs/ext4/resize.c |
4807 | @@ -51,6 +51,16 @@ int ext4_resize_begin(struct super_block *sb) |
4808 | if (!capable(CAP_SYS_RESOURCE)) |
4809 | return -EPERM; |
4810 | |
4811 | + /* |
4812 | + * If the reserved GDT blocks is non-zero, the resize_inode feature |
4813 | + * should always be set. |
4814 | + */ |
4815 | + if (EXT4_SB(sb)->s_es->s_reserved_gdt_blocks && |
4816 | + !ext4_has_feature_resize_inode(sb)) { |
4817 | + ext4_error(sb, "resize_inode disabled but reserved GDT blocks non-zero"); |
4818 | + return -EFSCORRUPTED; |
4819 | + } |
4820 | + |
4821 | /* |
4822 | * If we are not using the primary superblock/GDT copy don't resize, |
4823 | * because the user tools have no way of handling this. Probably a |
4824 | diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c |
4825 | index 585c52dbb2e39..13cf7c260a831 100644 |
4826 | --- a/fs/fuse/dev.c |
4827 | +++ b/fs/fuse/dev.c |
4828 | @@ -992,7 +992,17 @@ static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep, |
4829 | |
4830 | while (count) { |
4831 | if (cs->write && cs->pipebufs && page) { |
4832 | - return fuse_ref_page(cs, page, offset, count); |
4833 | + /* |
4834 | + * Can't control lifetime of pipe buffers, so always |
4835 | + * copy user pages. |
4836 | + */ |
4837 | + if (cs->req->user_pages) { |
4838 | + err = fuse_copy_fill(cs); |
4839 | + if (err) |
4840 | + return err; |
4841 | + } else { |
4842 | + return fuse_ref_page(cs, page, offset, count); |
4843 | + } |
4844 | } else if (!cs->len) { |
4845 | if (cs->move_pages && page && |
4846 | offset == 0 && count == PAGE_SIZE) { |
4847 | diff --git a/fs/fuse/file.c b/fs/fuse/file.c |
4848 | index 8aef8e56eb1b6..aa4f0d632bed4 100644 |
4849 | --- a/fs/fuse/file.c |
4850 | +++ b/fs/fuse/file.c |
4851 | @@ -1319,6 +1319,7 @@ static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii, |
4852 | (PAGE_SIZE - ret) & (PAGE_SIZE - 1); |
4853 | } |
4854 | |
4855 | + req->user_pages = true; |
4856 | if (write) |
4857 | req->in.argpages = 1; |
4858 | else |
4859 | diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h |
4860 | index 7e4b0e298bc73..6104534ddb7e6 100644 |
4861 | --- a/fs/fuse/fuse_i.h |
4862 | +++ b/fs/fuse/fuse_i.h |
4863 | @@ -310,6 +310,8 @@ struct fuse_req { |
4864 | /** refcount */ |
4865 | atomic_t count; |
4866 | |
4867 | + bool user_pages; |
4868 | + |
4869 | /** Unique ID for the interrupt request */ |
4870 | u64 intr_unique; |
4871 | |
4872 | diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c |
4873 | index f19cded49b29d..317d22f844924 100644 |
4874 | --- a/fs/nfs/pnfs.c |
4875 | +++ b/fs/nfs/pnfs.c |
4876 | @@ -1753,6 +1753,12 @@ lookup_again: |
4877 | /* Fallthrough */ |
4878 | case -EAGAIN: |
4879 | break; |
4880 | + case -ENODATA: |
4881 | + /* The server returned NFS4ERR_LAYOUTUNAVAILABLE */ |
4882 | + pnfs_layout_set_fail_bit( |
4883 | + lo, pnfs_iomode_to_fail_bit(iomode)); |
4884 | + lseg = NULL; |
4885 | + goto out_put_layout_hdr; |
4886 | default: |
4887 | if (!nfs_error_is_fatal(PTR_ERR(lseg))) { |
4888 | pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); |
4889 | diff --git a/include/crypto/blake2s.h b/include/crypto/blake2s.h |
4890 | new file mode 100644 |
4891 | index 0000000000000..d439496fa6baa |
4892 | --- /dev/null |
4893 | +++ b/include/crypto/blake2s.h |
4894 | @@ -0,0 +1,102 @@ |
4895 | +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ |
4896 | +/* |
4897 | + * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
4898 | + */ |
4899 | + |
4900 | +#ifndef _CRYPTO_BLAKE2S_H |
4901 | +#define _CRYPTO_BLAKE2S_H |
4902 | + |
4903 | +#include <linux/bug.h> |
4904 | +#include <linux/types.h> |
4905 | +#include <linux/kernel.h> |
4906 | +#include <linux/string.h> |
4907 | + |
4908 | +enum blake2s_lengths { |
4909 | + BLAKE2S_BLOCK_SIZE = 64, |
4910 | + BLAKE2S_HASH_SIZE = 32, |
4911 | + BLAKE2S_KEY_SIZE = 32, |
4912 | + |
4913 | + BLAKE2S_128_HASH_SIZE = 16, |
4914 | + BLAKE2S_160_HASH_SIZE = 20, |
4915 | + BLAKE2S_224_HASH_SIZE = 28, |
4916 | + BLAKE2S_256_HASH_SIZE = 32, |
4917 | +}; |
4918 | + |
4919 | +struct blake2s_state { |
4920 | + u32 h[8]; |
4921 | + u32 t[2]; |
4922 | + u32 f[2]; |
4923 | + u8 buf[BLAKE2S_BLOCK_SIZE]; |
4924 | + unsigned int buflen; |
4925 | + unsigned int outlen; |
4926 | +}; |
4927 | + |
4928 | +enum blake2s_iv { |
4929 | + BLAKE2S_IV0 = 0x6A09E667UL, |
4930 | + BLAKE2S_IV1 = 0xBB67AE85UL, |
4931 | + BLAKE2S_IV2 = 0x3C6EF372UL, |
4932 | + BLAKE2S_IV3 = 0xA54FF53AUL, |
4933 | + BLAKE2S_IV4 = 0x510E527FUL, |
4934 | + BLAKE2S_IV5 = 0x9B05688CUL, |
4935 | + BLAKE2S_IV6 = 0x1F83D9ABUL, |
4936 | + BLAKE2S_IV7 = 0x5BE0CD19UL, |
4937 | +}; |
4938 | + |
4939 | +void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen); |
4940 | +void blake2s_final(struct blake2s_state *state, u8 *out); |
4941 | + |
4942 | +static inline void blake2s_init_param(struct blake2s_state *state, |
4943 | + const u32 param) |
4944 | +{ |
4945 | + *state = (struct blake2s_state){{ |
4946 | + BLAKE2S_IV0 ^ param, |
4947 | + BLAKE2S_IV1, |
4948 | + BLAKE2S_IV2, |
4949 | + BLAKE2S_IV3, |
4950 | + BLAKE2S_IV4, |
4951 | + BLAKE2S_IV5, |
4952 | + BLAKE2S_IV6, |
4953 | + BLAKE2S_IV7, |
4954 | + }}; |
4955 | +} |
4956 | + |
4957 | +static inline void blake2s_init(struct blake2s_state *state, |
4958 | + const size_t outlen) |
4959 | +{ |
4960 | + blake2s_init_param(state, 0x01010000 | outlen); |
4961 | + state->outlen = outlen; |
4962 | +} |
4963 | + |
4964 | +static inline void blake2s_init_key(struct blake2s_state *state, |
4965 | + const size_t outlen, const void *key, |
4966 | + const size_t keylen) |
4967 | +{ |
4968 | + WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE || |
4969 | + !key || !keylen || keylen > BLAKE2S_KEY_SIZE)); |
4970 | + |
4971 | + blake2s_init_param(state, 0x01010000 | keylen << 8 | outlen); |
4972 | + memcpy(state->buf, key, keylen); |
4973 | + state->buflen = BLAKE2S_BLOCK_SIZE; |
4974 | + state->outlen = outlen; |
4975 | +} |
4976 | + |
4977 | +static inline void blake2s(u8 *out, const u8 *in, const u8 *key, |
4978 | + const size_t outlen, const size_t inlen, |
4979 | + const size_t keylen) |
4980 | +{ |
4981 | + struct blake2s_state state; |
4982 | + |
4983 | + WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen || |
4984 | + outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE || |
4985 | + (!key && keylen))); |
4986 | + |
4987 | + if (keylen) |
4988 | + blake2s_init_key(&state, outlen, key, keylen); |
4989 | + else |
4990 | + blake2s_init(&state, outlen); |
4991 | + |
4992 | + blake2s_update(&state, in, inlen); |
4993 | + blake2s_final(&state, out); |
4994 | +} |
4995 | + |
4996 | +#endif /* _CRYPTO_BLAKE2S_H */ |
4997 | diff --git a/include/crypto/chacha20.h b/include/crypto/chacha20.h |
4998 | index 20d20f681a72c..0b8deb5e82915 100644 |
4999 | --- a/include/crypto/chacha20.h |
5000 | +++ b/include/crypto/chacha20.h |
5001 | @@ -16,11 +16,26 @@ struct chacha20_ctx { |
5002 | u32 key[8]; |
5003 | }; |
5004 | |
5005 | -void chacha20_block(u32 *state, void *stream); |
5006 | +void chacha20_block(u32 *state, u8 *stream); |
5007 | void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv); |
5008 | int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key, |
5009 | unsigned int keysize); |
5010 | int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, |
5011 | struct scatterlist *src, unsigned int nbytes); |
5012 | |
5013 | +enum chacha_constants { /* expand 32-byte k */ |
5014 | + CHACHA_CONSTANT_EXPA = 0x61707865U, |
5015 | + CHACHA_CONSTANT_ND_3 = 0x3320646eU, |
5016 | + CHACHA_CONSTANT_2_BY = 0x79622d32U, |
5017 | + CHACHA_CONSTANT_TE_K = 0x6b206574U |
5018 | +}; |
5019 | + |
5020 | +static inline void chacha_init_consts(u32 *state) |
5021 | +{ |
5022 | + state[0] = CHACHA_CONSTANT_EXPA; |
5023 | + state[1] = CHACHA_CONSTANT_ND_3; |
5024 | + state[2] = CHACHA_CONSTANT_2_BY; |
5025 | + state[3] = CHACHA_CONSTANT_TE_K; |
5026 | +} |
5027 | + |
5028 | #endif |
5029 | diff --git a/include/crypto/drbg.h b/include/crypto/drbg.h |
5030 | index 22f884c97387e..19b44179bb7c7 100644 |
5031 | --- a/include/crypto/drbg.h |
5032 | +++ b/include/crypto/drbg.h |
5033 | @@ -105,6 +105,12 @@ struct drbg_test_data { |
5034 | struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */ |
5035 | }; |
5036 | |
5037 | +enum drbg_seed_state { |
5038 | + DRBG_SEED_STATE_UNSEEDED, |
5039 | + DRBG_SEED_STATE_PARTIAL, /* Seeded with !rng_is_initialized() */ |
5040 | + DRBG_SEED_STATE_FULL, |
5041 | +}; |
5042 | + |
5043 | struct drbg_state { |
5044 | struct mutex drbg_mutex; /* lock around DRBG */ |
5045 | unsigned char *V; /* internal state 10.1.1.1 1a) */ |
5046 | @@ -129,14 +135,14 @@ struct drbg_state { |
5047 | struct completion ctr_completion; /* CTR mode async handler */ |
5048 | int ctr_async_err; /* CTR mode async error */ |
5049 | |
5050 | - bool seeded; /* DRBG fully seeded? */ |
5051 | + enum drbg_seed_state seeded; /* DRBG fully seeded? */ |
5052 | bool pr; /* Prediction resistance enabled? */ |
5053 | - struct work_struct seed_work; /* asynchronous seeding support */ |
5054 | + bool fips_primed; /* Continuous test primed? */ |
5055 | + unsigned char *prev; /* FIPS 140-2 continuous test value */ |
5056 | struct crypto_rng *jent; |
5057 | const struct drbg_state_ops *d_ops; |
5058 | const struct drbg_core *core; |
5059 | struct drbg_string test_data; |
5060 | - struct random_ready_callback random_ready; |
5061 | }; |
5062 | |
5063 | static inline __u8 drbg_statelen(struct drbg_state *drbg) |
5064 | @@ -184,11 +190,7 @@ static inline size_t drbg_max_addtl(struct drbg_state *drbg) |
5065 | static inline size_t drbg_max_requests(struct drbg_state *drbg) |
5066 | { |
5067 | /* SP800-90A requires 2**48 maximum requests before reseeding */ |
5068 | -#if (__BITS_PER_LONG == 32) |
5069 | - return SIZE_MAX; |
5070 | -#else |
5071 | - return (1UL<<48); |
5072 | -#endif |
5073 | + return (1<<20); |
5074 | } |
5075 | |
5076 | /* |
5077 | diff --git a/include/crypto/internal/blake2s.h b/include/crypto/internal/blake2s.h |
5078 | new file mode 100644 |
5079 | index 0000000000000..3ba066845b699 |
5080 | --- /dev/null |
5081 | +++ b/include/crypto/internal/blake2s.h |
5082 | @@ -0,0 +1,19 @@ |
5083 | +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ |
5084 | + |
5085 | +#ifndef _CRYPTO_INTERNAL_BLAKE2S_H |
5086 | +#define _CRYPTO_INTERNAL_BLAKE2S_H |
5087 | + |
5088 | +#include <crypto/blake2s.h> |
5089 | + |
5090 | +void blake2s_compress_generic(struct blake2s_state *state,const u8 *block, |
5091 | + size_t nblocks, const u32 inc); |
5092 | + |
5093 | +void blake2s_compress_arch(struct blake2s_state *state,const u8 *block, |
5094 | + size_t nblocks, const u32 inc); |
5095 | + |
5096 | +static inline void blake2s_set_lastblock(struct blake2s_state *state) |
5097 | +{ |
5098 | + state->f[0] = -1; |
5099 | +} |
5100 | + |
5101 | +#endif /* _CRYPTO_INTERNAL_BLAKE2S_H */ |
5102 | diff --git a/include/linux/byteorder/generic.h b/include/linux/byteorder/generic.h |
5103 | index 89f67c1c31606..9cf01d7bb7c0a 100644 |
5104 | --- a/include/linux/byteorder/generic.h |
5105 | +++ b/include/linux/byteorder/generic.h |
5106 | @@ -155,6 +155,23 @@ static inline void le64_add_cpu(__le64 *var, u64 val) |
5107 | *var = cpu_to_le64(le64_to_cpu(*var) + val); |
5108 | } |
5109 | |
5110 | +/* XXX: this stuff can be optimized */ |
5111 | +static inline void le32_to_cpu_array(u32 *buf, unsigned int words) |
5112 | +{ |
5113 | + while (words--) { |
5114 | + __le32_to_cpus(buf); |
5115 | + buf++; |
5116 | + } |
5117 | +} |
5118 | + |
5119 | +static inline void cpu_to_le32_array(u32 *buf, unsigned int words) |
5120 | +{ |
5121 | + while (words--) { |
5122 | + __cpu_to_le32s(buf); |
5123 | + buf++; |
5124 | + } |
5125 | +} |
5126 | + |
5127 | static inline void be16_add_cpu(__be16 *var, u16 val) |
5128 | { |
5129 | *var = cpu_to_be16(be16_to_cpu(*var) + val); |
5130 | diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h |
5131 | index 1ab0273560aeb..8160ad1885dc2 100644 |
5132 | --- a/include/linux/cpuhotplug.h |
5133 | +++ b/include/linux/cpuhotplug.h |
5134 | @@ -29,6 +29,7 @@ enum cpuhp_state { |
5135 | CPUHP_ACPI_CPUDRV_DEAD, |
5136 | CPUHP_S390_PFAULT_DEAD, |
5137 | CPUHP_BLK_MQ_DEAD, |
5138 | + CPUHP_RANDOM_PREPARE, |
5139 | CPUHP_WORKQUEUE_PREP, |
5140 | CPUHP_POWER_NUMA_PREPARE, |
5141 | CPUHP_HRTIMERS_PREPARE, |
5142 | @@ -119,6 +120,7 @@ enum cpuhp_state { |
5143 | CPUHP_AP_PERF_ARM_CCN_ONLINE, |
5144 | CPUHP_AP_PERF_ARM_L2X0_ONLINE, |
5145 | CPUHP_AP_WORKQUEUE_ONLINE, |
5146 | + CPUHP_AP_RANDOM_ONLINE, |
5147 | CPUHP_AP_RCUTREE_ONLINE, |
5148 | CPUHP_AP_NOTIFY_ONLINE, |
5149 | CPUHP_AP_ONLINE_DYN, |
5150 | diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h |
5151 | index 34a0dc18f3271..48e28f7844fe9 100644 |
5152 | --- a/include/linux/hw_random.h |
5153 | +++ b/include/linux/hw_random.h |
5154 | @@ -60,7 +60,5 @@ extern int devm_hwrng_register(struct device *dev, struct hwrng *rng); |
5155 | /** Unregister a Hardware Random Number Generator driver. */ |
5156 | extern void hwrng_unregister(struct hwrng *rng); |
5157 | extern void devm_hwrng_unregister(struct device *dve, struct hwrng *rng); |
5158 | -/** Feed random bits into the pool. */ |
5159 | -extern void add_hwgenerator_randomness(const char *buffer, size_t count, size_t entropy); |
5160 | |
5161 | #endif /* LINUX_HWRANDOM_H_ */ |
5162 | diff --git a/include/linux/miscdevice.h b/include/linux/miscdevice.h |
5163 | index 722698a43d79c..659f58617b9e3 100644 |
5164 | --- a/include/linux/miscdevice.h |
5165 | +++ b/include/linux/miscdevice.h |
5166 | @@ -31,6 +31,7 @@ |
5167 | #define SGI_MMTIMER 153 |
5168 | #define STORE_QUEUE_MINOR 155 /* unused */ |
5169 | #define I2O_MINOR 166 |
5170 | +#define HWRNG_MINOR 183 |
5171 | #define MICROCODE_MINOR 184 |
5172 | #define VFIO_MINOR 196 |
5173 | #define TUN_MINOR 200 |
5174 | diff --git a/include/linux/mm.h b/include/linux/mm.h |
5175 | index 81ee5d0b26424..ebfe5b7711531 100644 |
5176 | --- a/include/linux/mm.h |
5177 | +++ b/include/linux/mm.h |
5178 | @@ -2059,6 +2059,8 @@ extern int install_special_mapping(struct mm_struct *mm, |
5179 | unsigned long addr, unsigned long len, |
5180 | unsigned long flags, struct page **pages); |
5181 | |
5182 | +unsigned long randomize_page(unsigned long start, unsigned long range); |
5183 | + |
5184 | extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); |
5185 | |
5186 | extern unsigned long mmap_region(struct file *file, unsigned long addr, |
5187 | diff --git a/include/linux/net.h b/include/linux/net.h |
5188 | index 54270c4707cf2..455aa9c1247ca 100644 |
5189 | --- a/include/linux/net.h |
5190 | +++ b/include/linux/net.h |
5191 | @@ -274,6 +274,8 @@ do { \ |
5192 | |
5193 | #define net_get_random_once(buf, nbytes) \ |
5194 | get_random_once((buf), (nbytes)) |
5195 | +#define net_get_random_once_wait(buf, nbytes) \ |
5196 | + get_random_once_wait((buf), (nbytes)) |
5197 | |
5198 | int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec, |
5199 | size_t num, size_t len); |
5200 | diff --git a/include/linux/once.h b/include/linux/once.h |
5201 | index 285f12cb40e6a..9c98aaa87cbcd 100644 |
5202 | --- a/include/linux/once.h |
5203 | +++ b/include/linux/once.h |
5204 | @@ -53,5 +53,7 @@ void __do_once_done(bool *done, struct static_key *once_key, |
5205 | |
5206 | #define get_random_once(buf, nbytes) \ |
5207 | DO_ONCE(get_random_bytes, (buf), (nbytes)) |
5208 | +#define get_random_once_wait(buf, nbytes) \ |
5209 | + DO_ONCE(get_random_bytes_wait, (buf), (nbytes)) \ |
5210 | |
5211 | #endif /* _LINUX_ONCE_H */ |
5212 | diff --git a/include/linux/prandom.h b/include/linux/prandom.h |
5213 | index e20339c78a84c..709e8e69fb39b 100644 |
5214 | --- a/include/linux/prandom.h |
5215 | +++ b/include/linux/prandom.h |
5216 | @@ -10,6 +10,7 @@ |
5217 | |
5218 | #include <linux/types.h> |
5219 | #include <linux/percpu.h> |
5220 | +#include <linux/siphash.h> |
5221 | |
5222 | u32 prandom_u32(void); |
5223 | void prandom_bytes(void *buf, size_t nbytes); |
5224 | @@ -21,15 +22,10 @@ void prandom_reseed_late(void); |
5225 | * The core SipHash round function. Each line can be executed in |
5226 | * parallel given enough CPU resources. |
5227 | */ |
5228 | -#define PRND_SIPROUND(v0, v1, v2, v3) ( \ |
5229 | - v0 += v1, v1 = rol64(v1, 13), v2 += v3, v3 = rol64(v3, 16), \ |
5230 | - v1 ^= v0, v0 = rol64(v0, 32), v3 ^= v2, \ |
5231 | - v0 += v3, v3 = rol64(v3, 21), v2 += v1, v1 = rol64(v1, 17), \ |
5232 | - v3 ^= v0, v1 ^= v2, v2 = rol64(v2, 32) \ |
5233 | -) |
5234 | +#define PRND_SIPROUND(v0, v1, v2, v3) SIPHASH_PERMUTATION(v0, v1, v2, v3) |
5235 | |
5236 | -#define PRND_K0 (0x736f6d6570736575 ^ 0x6c7967656e657261) |
5237 | -#define PRND_K1 (0x646f72616e646f6d ^ 0x7465646279746573) |
5238 | +#define PRND_K0 (SIPHASH_CONST_0 ^ SIPHASH_CONST_2) |
5239 | +#define PRND_K1 (SIPHASH_CONST_1 ^ SIPHASH_CONST_3) |
5240 | |
5241 | #elif BITS_PER_LONG == 32 |
5242 | /* |
5243 | @@ -37,14 +33,9 @@ void prandom_reseed_late(void); |
5244 | * This is weaker, but 32-bit machines are not used for high-traffic |
5245 | * applications, so there is less output for an attacker to analyze. |
5246 | */ |
5247 | -#define PRND_SIPROUND(v0, v1, v2, v3) ( \ |
5248 | - v0 += v1, v1 = rol32(v1, 5), v2 += v3, v3 = rol32(v3, 8), \ |
5249 | - v1 ^= v0, v0 = rol32(v0, 16), v3 ^= v2, \ |
5250 | - v0 += v3, v3 = rol32(v3, 7), v2 += v1, v1 = rol32(v1, 13), \ |
5251 | - v3 ^= v0, v1 ^= v2, v2 = rol32(v2, 16) \ |
5252 | -) |
5253 | -#define PRND_K0 0x6c796765 |
5254 | -#define PRND_K1 0x74656462 |
5255 | +#define PRND_SIPROUND(v0, v1, v2, v3) HSIPHASH_PERMUTATION(v0, v1, v2, v3) |
5256 | +#define PRND_K0 (HSIPHASH_CONST_0 ^ HSIPHASH_CONST_2) |
5257 | +#define PRND_K1 (HSIPHASH_CONST_1 ^ HSIPHASH_CONST_3) |
5258 | |
5259 | #else |
5260 | #error Unsupported BITS_PER_LONG |
5261 | diff --git a/include/linux/random.h b/include/linux/random.h |
5262 | index 15cd754544686..291b493fd2e7d 100644 |
5263 | --- a/include/linux/random.h |
5264 | +++ b/include/linux/random.h |
5265 | @@ -1,50 +1,78 @@ |
5266 | -/* |
5267 | - * include/linux/random.h |
5268 | - * |
5269 | - * Include file for the random number generator. |
5270 | - */ |
5271 | +/* SPDX-License-Identifier: GPL-2.0 */ |
5272 | + |
5273 | #ifndef _LINUX_RANDOM_H |
5274 | #define _LINUX_RANDOM_H |
5275 | |
5276 | +#include <linux/bug.h> |
5277 | +#include <linux/kernel.h> |
5278 | #include <linux/list.h> |
5279 | #include <linux/once.h> |
5280 | |
5281 | #include <uapi/linux/random.h> |
5282 | |
5283 | -struct random_ready_callback { |
5284 | - struct list_head list; |
5285 | - void (*func)(struct random_ready_callback *rdy); |
5286 | - struct module *owner; |
5287 | -}; |
5288 | +struct notifier_block; |
5289 | |
5290 | -extern void add_device_randomness(const void *, unsigned int); |
5291 | +void add_device_randomness(const void *buf, size_t len); |
5292 | +void __init add_bootloader_randomness(const void *buf, size_t len); |
5293 | +void add_input_randomness(unsigned int type, unsigned int code, |
5294 | + unsigned int value) __latent_entropy; |
5295 | +void add_interrupt_randomness(int irq) __latent_entropy; |
5296 | +void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy); |
5297 | |
5298 | -#if defined(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) && !defined(__CHECKER__) |
5299 | +#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__) |
5300 | static inline void add_latent_entropy(void) |
5301 | { |
5302 | - add_device_randomness((const void *)&latent_entropy, |
5303 | - sizeof(latent_entropy)); |
5304 | + add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy)); |
5305 | } |
5306 | #else |
5307 | -static inline void add_latent_entropy(void) {} |
5308 | +static inline void add_latent_entropy(void) { } |
5309 | #endif |
5310 | |
5311 | -extern void add_input_randomness(unsigned int type, unsigned int code, |
5312 | - unsigned int value) __latent_entropy; |
5313 | -extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy; |
5314 | +void get_random_bytes(void *buf, size_t len); |
5315 | +size_t __must_check get_random_bytes_arch(void *buf, size_t len); |
5316 | +u32 get_random_u32(void); |
5317 | +u64 get_random_u64(void); |
5318 | +static inline unsigned int get_random_int(void) |
5319 | +{ |
5320 | + return get_random_u32(); |
5321 | +} |
5322 | +static inline unsigned long get_random_long(void) |
5323 | +{ |
5324 | +#if BITS_PER_LONG == 64 |
5325 | + return get_random_u64(); |
5326 | +#else |
5327 | + return get_random_u32(); |
5328 | +#endif |
5329 | +} |
5330 | |
5331 | -extern void get_random_bytes(void *buf, int nbytes); |
5332 | -extern int add_random_ready_callback(struct random_ready_callback *rdy); |
5333 | -extern void del_random_ready_callback(struct random_ready_callback *rdy); |
5334 | -extern void get_random_bytes_arch(void *buf, int nbytes); |
5335 | +int __init random_init(const char *command_line); |
5336 | +bool rng_is_initialized(void); |
5337 | +int wait_for_random_bytes(void); |
5338 | +int register_random_ready_notifier(struct notifier_block *nb); |
5339 | +int unregister_random_ready_notifier(struct notifier_block *nb); |
5340 | |
5341 | -#ifndef MODULE |
5342 | -extern const struct file_operations random_fops, urandom_fops; |
5343 | -#endif |
5344 | +/* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes). |
5345 | + * Returns the result of the call to wait_for_random_bytes. */ |
5346 | +static inline int get_random_bytes_wait(void *buf, size_t nbytes) |
5347 | +{ |
5348 | + int ret = wait_for_random_bytes(); |
5349 | + get_random_bytes(buf, nbytes); |
5350 | + return ret; |
5351 | +} |
5352 | |
5353 | -unsigned int get_random_int(void); |
5354 | -unsigned long get_random_long(void); |
5355 | -unsigned long randomize_page(unsigned long start, unsigned long range); |
5356 | +#define declare_get_random_var_wait(name, ret_type) \ |
5357 | + static inline int get_random_ ## name ## _wait(ret_type *out) { \ |
5358 | + int ret = wait_for_random_bytes(); \ |
5359 | + if (unlikely(ret)) \ |
5360 | + return ret; \ |
5361 | + *out = get_random_ ## name(); \ |
5362 | + return 0; \ |
5363 | + } |
5364 | +declare_get_random_var_wait(u32, u32) |
5365 | +declare_get_random_var_wait(u64, u32) |
5366 | +declare_get_random_var_wait(int, unsigned int) |
5367 | +declare_get_random_var_wait(long, unsigned long) |
5368 | +#undef declare_get_random_var |
5369 | |
5370 | /* |
5371 | * This is designed to be standalone for just prandom |
5372 | @@ -56,30 +84,39 @@ unsigned long randomize_page(unsigned long start, unsigned long range); |
5373 | #ifdef CONFIG_ARCH_RANDOM |
5374 | # include <asm/archrandom.h> |
5375 | #else |
5376 | -static inline bool arch_get_random_long(unsigned long *v) |
5377 | -{ |
5378 | - return 0; |
5379 | -} |
5380 | -static inline bool arch_get_random_int(unsigned int *v) |
5381 | -{ |
5382 | - return 0; |
5383 | -} |
5384 | -static inline bool arch_has_random(void) |
5385 | -{ |
5386 | - return 0; |
5387 | -} |
5388 | -static inline bool arch_get_random_seed_long(unsigned long *v) |
5389 | -{ |
5390 | - return 0; |
5391 | -} |
5392 | -static inline bool arch_get_random_seed_int(unsigned int *v) |
5393 | +static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; } |
5394 | +static inline bool __must_check arch_get_random_int(unsigned int *v) { return false; } |
5395 | +static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { return false; } |
5396 | +static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { return false; } |
5397 | +#endif |
5398 | + |
5399 | +/* |
5400 | + * Called from the boot CPU during startup; not valid to call once |
5401 | + * secondary CPUs are up and preemption is possible. |
5402 | + */ |
5403 | +#ifndef arch_get_random_seed_long_early |
5404 | +static inline bool __init arch_get_random_seed_long_early(unsigned long *v) |
5405 | { |
5406 | - return 0; |
5407 | + WARN_ON(system_state != SYSTEM_BOOTING); |
5408 | + return arch_get_random_seed_long(v); |
5409 | } |
5410 | -static inline bool arch_has_random_seed(void) |
5411 | +#endif |
5412 | + |
5413 | +#ifndef arch_get_random_long_early |
5414 | +static inline bool __init arch_get_random_long_early(unsigned long *v) |
5415 | { |
5416 | - return 0; |
5417 | + WARN_ON(system_state != SYSTEM_BOOTING); |
5418 | + return arch_get_random_long(v); |
5419 | } |
5420 | #endif |
5421 | |
5422 | +#ifdef CONFIG_SMP |
5423 | +int random_prepare_cpu(unsigned int cpu); |
5424 | +int random_online_cpu(unsigned int cpu); |
5425 | +#endif |
5426 | + |
5427 | +#ifndef MODULE |
5428 | +extern const struct file_operations random_fops, urandom_fops; |
5429 | +#endif |
5430 | + |
5431 | #endif /* _LINUX_RANDOM_H */ |
5432 | diff --git a/include/linux/siphash.h b/include/linux/siphash.h |
5433 | index 0cda61855d907..0bb5ecd507bef 100644 |
5434 | --- a/include/linux/siphash.h |
5435 | +++ b/include/linux/siphash.h |
5436 | @@ -136,4 +136,32 @@ static inline u32 hsiphash(const void *data, size_t len, |
5437 | return ___hsiphash_aligned(data, len, key); |
5438 | } |
5439 | |
5440 | +/* |
5441 | + * These macros expose the raw SipHash and HalfSipHash permutations. |
5442 | + * Do not use them directly! If you think you have a use for them, |
5443 | + * be sure to CC the maintainer of this file explaining why. |
5444 | + */ |
5445 | + |
5446 | +#define SIPHASH_PERMUTATION(a, b, c, d) ( \ |
5447 | + (a) += (b), (b) = rol64((b), 13), (b) ^= (a), (a) = rol64((a), 32), \ |
5448 | + (c) += (d), (d) = rol64((d), 16), (d) ^= (c), \ |
5449 | + (a) += (d), (d) = rol64((d), 21), (d) ^= (a), \ |
5450 | + (c) += (b), (b) = rol64((b), 17), (b) ^= (c), (c) = rol64((c), 32)) |
5451 | + |
5452 | +#define SIPHASH_CONST_0 0x736f6d6570736575ULL |
5453 | +#define SIPHASH_CONST_1 0x646f72616e646f6dULL |
5454 | +#define SIPHASH_CONST_2 0x6c7967656e657261ULL |
5455 | +#define SIPHASH_CONST_3 0x7465646279746573ULL |
5456 | + |
5457 | +#define HSIPHASH_PERMUTATION(a, b, c, d) ( \ |
5458 | + (a) += (b), (b) = rol32((b), 5), (b) ^= (a), (a) = rol32((a), 16), \ |
5459 | + (c) += (d), (d) = rol32((d), 8), (d) ^= (c), \ |
5460 | + (a) += (d), (d) = rol32((d), 7), (d) ^= (a), \ |
5461 | + (c) += (b), (b) = rol32((b), 13), (b) ^= (c), (c) = rol32((c), 16)) |
5462 | + |
5463 | +#define HSIPHASH_CONST_0 0U |
5464 | +#define HSIPHASH_CONST_1 0U |
5465 | +#define HSIPHASH_CONST_2 0x6c796765U |
5466 | +#define HSIPHASH_CONST_3 0x74656462U |
5467 | + |
5468 | #endif /* _LINUX_SIPHASH_H */ |
5469 | diff --git a/include/linux/swab.h b/include/linux/swab.h |
5470 | index 9ad3c60f6cae7..f66e6f8bf049d 100644 |
5471 | --- a/include/linux/swab.h |
5472 | +++ b/include/linux/swab.h |
5473 | @@ -6,6 +6,7 @@ |
5474 | # define swab16 __swab16 |
5475 | # define swab32 __swab32 |
5476 | # define swab64 __swab64 |
5477 | +# define swab __swab |
5478 | # define swahw32 __swahw32 |
5479 | # define swahb32 __swahb32 |
5480 | # define swab16p __swab16p |
5481 | diff --git a/include/linux/timex.h b/include/linux/timex.h |
5482 | index 39c25dbebfe80..c7616cfb48d20 100644 |
5483 | --- a/include/linux/timex.h |
5484 | +++ b/include/linux/timex.h |
5485 | @@ -62,6 +62,8 @@ |
5486 | #include <linux/types.h> |
5487 | #include <linux/param.h> |
5488 | |
5489 | +unsigned long random_get_entropy_fallback(void); |
5490 | + |
5491 | #include <asm/timex.h> |
5492 | |
5493 | #ifndef random_get_entropy |
5494 | @@ -74,8 +76,14 @@ |
5495 | * |
5496 | * By default we use get_cycles() for this purpose, but individual |
5497 | * architectures may override this in their asm/timex.h header file. |
5498 | + * If a given arch does not have get_cycles(), then we fallback to |
5499 | + * using random_get_entropy_fallback(). |
5500 | */ |
5501 | -#define random_get_entropy() get_cycles() |
5502 | +#ifdef get_cycles |
5503 | +#define random_get_entropy() ((unsigned long)get_cycles()) |
5504 | +#else |
5505 | +#define random_get_entropy() random_get_entropy_fallback() |
5506 | +#endif |
5507 | #endif |
5508 | |
5509 | /* |
5510 | diff --git a/include/linux/uuid.h b/include/linux/uuid.h |
5511 | index 2d095fc602045..c77fb14868b70 100644 |
5512 | --- a/include/linux/uuid.h |
5513 | +++ b/include/linux/uuid.h |
5514 | @@ -23,6 +23,7 @@ |
5515 | * not including trailing NUL. |
5516 | */ |
5517 | #define UUID_STRING_LEN 36 |
5518 | +#define UUID_SIZE 16 |
5519 | |
5520 | static inline int uuid_le_cmp(const uuid_le u1, const uuid_le u2) |
5521 | { |
5522 | diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h |
5523 | index 8e880f7f67b24..b95c511139b9d 100644 |
5524 | --- a/include/linux/workqueue.h |
5525 | +++ b/include/linux/workqueue.h |
5526 | @@ -359,6 +359,8 @@ extern struct workqueue_struct *system_freezable_wq; |
5527 | extern struct workqueue_struct *system_power_efficient_wq; |
5528 | extern struct workqueue_struct *system_freezable_power_efficient_wq; |
5529 | |
5530 | +extern bool wq_online; |
5531 | + |
5532 | extern struct workqueue_struct * |
5533 | __alloc_workqueue_key(const char *fmt, unsigned int flags, int max_active, |
5534 | struct lock_class_key *key, const char *lock_name, ...) __printf(1, 6); |
5535 | @@ -598,7 +600,7 @@ static inline bool schedule_delayed_work(struct delayed_work *dwork, |
5536 | */ |
5537 | static inline bool keventd_up(void) |
5538 | { |
5539 | - return system_wq != NULL; |
5540 | + return wq_online; |
5541 | } |
5542 | |
5543 | #ifndef CONFIG_SMP |
5544 | @@ -635,4 +637,7 @@ int workqueue_online_cpu(unsigned int cpu); |
5545 | int workqueue_offline_cpu(unsigned int cpu); |
5546 | #endif |
5547 | |
5548 | +int __init workqueue_init_early(void); |
5549 | +int __init workqueue_init(void); |
5550 | + |
5551 | #endif |
5552 | diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h |
5553 | index fc445e7ccadf4..c3cf2021bb3b3 100644 |
5554 | --- a/include/net/inet_hashtables.h |
5555 | +++ b/include/net/inet_hashtables.h |
5556 | @@ -382,7 +382,7 @@ static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr) |
5557 | } |
5558 | |
5559 | int __inet_hash_connect(struct inet_timewait_death_row *death_row, |
5560 | - struct sock *sk, u32 port_offset, |
5561 | + struct sock *sk, u64 port_offset, |
5562 | int (*check_established)(struct inet_timewait_death_row *, |
5563 | struct sock *, __u16, |
5564 | struct inet_timewait_sock **)); |
5565 | diff --git a/include/net/secure_seq.h b/include/net/secure_seq.h |
5566 | index 3f36d45b714a4..542a80c051074 100644 |
5567 | --- a/include/net/secure_seq.h |
5568 | +++ b/include/net/secure_seq.h |
5569 | @@ -3,8 +3,8 @@ |
5570 | |
5571 | #include <linux/types.h> |
5572 | |
5573 | -u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport); |
5574 | -u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr, |
5575 | +u64 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport); |
5576 | +u64 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr, |
5577 | __be16 dport); |
5578 | __u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr, |
5579 | __be16 sport, __be16 dport); |
5580 | diff --git a/include/trace/events/random.h b/include/trace/events/random.h |
5581 | deleted file mode 100644 |
5582 | index 4684de344c5d9..0000000000000 |
5583 | --- a/include/trace/events/random.h |
5584 | +++ /dev/null |
5585 | @@ -1,315 +0,0 @@ |
5586 | -#undef TRACE_SYSTEM |
5587 | -#define TRACE_SYSTEM random |
5588 | - |
5589 | -#if !defined(_TRACE_RANDOM_H) || defined(TRACE_HEADER_MULTI_READ) |
5590 | -#define _TRACE_RANDOM_H |
5591 | - |
5592 | -#include <linux/writeback.h> |
5593 | -#include <linux/tracepoint.h> |
5594 | - |
5595 | -TRACE_EVENT(add_device_randomness, |
5596 | - TP_PROTO(int bytes, unsigned long IP), |
5597 | - |
5598 | - TP_ARGS(bytes, IP), |
5599 | - |
5600 | - TP_STRUCT__entry( |
5601 | - __field( int, bytes ) |
5602 | - __field(unsigned long, IP ) |
5603 | - ), |
5604 | - |
5605 | - TP_fast_assign( |
5606 | - __entry->bytes = bytes; |
5607 | - __entry->IP = IP; |
5608 | - ), |
5609 | - |
5610 | - TP_printk("bytes %d caller %pS", |
5611 | - __entry->bytes, (void *)__entry->IP) |
5612 | -); |
5613 | - |
5614 | -DECLARE_EVENT_CLASS(random__mix_pool_bytes, |
5615 | - TP_PROTO(const char *pool_name, int bytes, unsigned long IP), |
5616 | - |
5617 | - TP_ARGS(pool_name, bytes, IP), |
5618 | - |
5619 | - TP_STRUCT__entry( |
5620 | - __field( const char *, pool_name ) |
5621 | - __field( int, bytes ) |
5622 | - __field(unsigned long, IP ) |
5623 | - ), |
5624 | - |
5625 | - TP_fast_assign( |
5626 | - __entry->pool_name = pool_name; |
5627 | - __entry->bytes = bytes; |
5628 | - __entry->IP = IP; |
5629 | - ), |
5630 | - |
5631 | - TP_printk("%s pool: bytes %d caller %pS", |
5632 | - __entry->pool_name, __entry->bytes, (void *)__entry->IP) |
5633 | -); |
5634 | - |
5635 | -DEFINE_EVENT(random__mix_pool_bytes, mix_pool_bytes, |
5636 | - TP_PROTO(const char *pool_name, int bytes, unsigned long IP), |
5637 | - |
5638 | - TP_ARGS(pool_name, bytes, IP) |
5639 | -); |
5640 | - |
5641 | -DEFINE_EVENT(random__mix_pool_bytes, mix_pool_bytes_nolock, |
5642 | - TP_PROTO(const char *pool_name, int bytes, unsigned long IP), |
5643 | - |
5644 | - TP_ARGS(pool_name, bytes, IP) |
5645 | -); |
5646 | - |
5647 | -TRACE_EVENT(credit_entropy_bits, |
5648 | - TP_PROTO(const char *pool_name, int bits, int entropy_count, |
5649 | - int entropy_total, unsigned long IP), |
5650 | - |
5651 | - TP_ARGS(pool_name, bits, entropy_count, entropy_total, IP), |
5652 | - |
5653 | - TP_STRUCT__entry( |
5654 | - __field( const char *, pool_name ) |
5655 | - __field( int, bits ) |
5656 | - __field( int, entropy_count ) |
5657 | - __field( int, entropy_total ) |
5658 | - __field(unsigned long, IP ) |
5659 | - ), |
5660 | - |
5661 | - TP_fast_assign( |
5662 | - __entry->pool_name = pool_name; |
5663 | - __entry->bits = bits; |
5664 | - __entry->entropy_count = entropy_count; |
5665 | - __entry->entropy_total = entropy_total; |
5666 | - __entry->IP = IP; |
5667 | - ), |
5668 | - |
5669 | - TP_printk("%s pool: bits %d entropy_count %d entropy_total %d " |
5670 | - "caller %pS", __entry->pool_name, __entry->bits, |
5671 | - __entry->entropy_count, __entry->entropy_total, |
5672 | - (void *)__entry->IP) |
5673 | -); |
5674 | - |
5675 | -TRACE_EVENT(push_to_pool, |
5676 | - TP_PROTO(const char *pool_name, int pool_bits, int input_bits), |
5677 | - |
5678 | - TP_ARGS(pool_name, pool_bits, input_bits), |
5679 | - |
5680 | - TP_STRUCT__entry( |
5681 | - __field( const char *, pool_name ) |
5682 | - __field( int, pool_bits ) |
5683 | - __field( int, input_bits ) |
5684 | - ), |
5685 | - |
5686 | - TP_fast_assign( |
5687 | - __entry->pool_name = pool_name; |
5688 | - __entry->pool_bits = pool_bits; |
5689 | - __entry->input_bits = input_bits; |
5690 | - ), |
5691 | - |
5692 | - TP_printk("%s: pool_bits %d input_pool_bits %d", |
5693 | - __entry->pool_name, __entry->pool_bits, |
5694 | - __entry->input_bits) |
5695 | -); |
5696 | - |
5697 | -TRACE_EVENT(debit_entropy, |
5698 | - TP_PROTO(const char *pool_name, int debit_bits), |
5699 | - |
5700 | - TP_ARGS(pool_name, debit_bits), |
5701 | - |
5702 | - TP_STRUCT__entry( |
5703 | - __field( const char *, pool_name ) |
5704 | - __field( int, debit_bits ) |
5705 | - ), |
5706 | - |
5707 | - TP_fast_assign( |
5708 | - __entry->pool_name = pool_name; |
5709 | - __entry->debit_bits = debit_bits; |
5710 | - ), |
5711 | - |
5712 | - TP_printk("%s: debit_bits %d", __entry->pool_name, |
5713 | - __entry->debit_bits) |
5714 | -); |
5715 | - |
5716 | -TRACE_EVENT(add_input_randomness, |
5717 | - TP_PROTO(int input_bits), |
5718 | - |
5719 | - TP_ARGS(input_bits), |
5720 | - |
5721 | - TP_STRUCT__entry( |
5722 | - __field( int, input_bits ) |
5723 | - ), |
5724 | - |
5725 | - TP_fast_assign( |
5726 | - __entry->input_bits = input_bits; |
5727 | - ), |
5728 | - |
5729 | - TP_printk("input_pool_bits %d", __entry->input_bits) |
5730 | -); |
5731 | - |
5732 | -TRACE_EVENT(add_disk_randomness, |
5733 | - TP_PROTO(dev_t dev, int input_bits), |
5734 | - |
5735 | - TP_ARGS(dev, input_bits), |
5736 | - |
5737 | - TP_STRUCT__entry( |
5738 | - __field( dev_t, dev ) |
5739 | - __field( int, input_bits ) |
5740 | - ), |
5741 | - |
5742 | - TP_fast_assign( |
5743 | - __entry->dev = dev; |
5744 | - __entry->input_bits = input_bits; |
5745 | - ), |
5746 | - |
5747 | - TP_printk("dev %d,%d input_pool_bits %d", MAJOR(__entry->dev), |
5748 | - MINOR(__entry->dev), __entry->input_bits) |
5749 | -); |
5750 | - |
5751 | -TRACE_EVENT(xfer_secondary_pool, |
5752 | - TP_PROTO(const char *pool_name, int xfer_bits, int request_bits, |
5753 | - int pool_entropy, int input_entropy), |
5754 | - |
5755 | - TP_ARGS(pool_name, xfer_bits, request_bits, pool_entropy, |
5756 | - input_entropy), |
5757 | - |
5758 | - TP_STRUCT__entry( |
5759 | - __field( const char *, pool_name ) |
5760 | - __field( int, xfer_bits ) |
5761 | - __field( int, request_bits ) |
5762 | - __field( int, pool_entropy ) |
5763 | - __field( int, input_entropy ) |
5764 | - ), |
5765 | - |
5766 | - TP_fast_assign( |
5767 | - __entry->pool_name = pool_name; |
5768 | - __entry->xfer_bits = xfer_bits; |
5769 | - __entry->request_bits = request_bits; |
5770 | - __entry->pool_entropy = pool_entropy; |
5771 | - __entry->input_entropy = input_entropy; |
5772 | - ), |
5773 | - |
5774 | - TP_printk("pool %s xfer_bits %d request_bits %d pool_entropy %d " |
5775 | - "input_entropy %d", __entry->pool_name, __entry->xfer_bits, |
5776 | - __entry->request_bits, __entry->pool_entropy, |
5777 | - __entry->input_entropy) |
5778 | -); |
5779 | - |
5780 | -DECLARE_EVENT_CLASS(random__get_random_bytes, |
5781 | - TP_PROTO(int nbytes, unsigned long IP), |
5782 | - |
5783 | - TP_ARGS(nbytes, IP), |
5784 | - |
5785 | - TP_STRUCT__entry( |
5786 | - __field( int, nbytes ) |
5787 | - __field(unsigned long, IP ) |
5788 | - ), |
5789 | - |
5790 | - TP_fast_assign( |
5791 | - __entry->nbytes = nbytes; |
5792 | - __entry->IP = IP; |
5793 | - ), |
5794 | - |
5795 | - TP_printk("nbytes %d caller %pS", __entry->nbytes, (void *)__entry->IP) |
5796 | -); |
5797 | - |
5798 | -DEFINE_EVENT(random__get_random_bytes, get_random_bytes, |
5799 | - TP_PROTO(int nbytes, unsigned long IP), |
5800 | - |
5801 | - TP_ARGS(nbytes, IP) |
5802 | -); |
5803 | - |
5804 | -DEFINE_EVENT(random__get_random_bytes, get_random_bytes_arch, |
5805 | - TP_PROTO(int nbytes, unsigned long IP), |
5806 | - |
5807 | - TP_ARGS(nbytes, IP) |
5808 | -); |
5809 | - |
5810 | -DECLARE_EVENT_CLASS(random__extract_entropy, |
5811 | - TP_PROTO(const char *pool_name, int nbytes, int entropy_count, |
5812 | - unsigned long IP), |
5813 | - |
5814 | - TP_ARGS(pool_name, nbytes, entropy_count, IP), |
5815 | - |
5816 | - TP_STRUCT__entry( |
5817 | - __field( const char *, pool_name ) |
5818 | - __field( int, nbytes ) |
5819 | - __field( int, entropy_count ) |
5820 | - __field(unsigned long, IP ) |
5821 | - ), |
5822 | - |
5823 | - TP_fast_assign( |
5824 | - __entry->pool_name = pool_name; |
5825 | - __entry->nbytes = nbytes; |
5826 | - __entry->entropy_count = entropy_count; |
5827 | - __entry->IP = IP; |
5828 | - ), |
5829 | - |
5830 | - TP_printk("%s pool: nbytes %d entropy_count %d caller %pS", |
5831 | - __entry->pool_name, __entry->nbytes, __entry->entropy_count, |
5832 | - (void *)__entry->IP) |
5833 | -); |
5834 | - |
5835 | - |
5836 | -DEFINE_EVENT(random__extract_entropy, extract_entropy, |
5837 | - TP_PROTO(const char *pool_name, int nbytes, int entropy_count, |
5838 | - unsigned long IP), |
5839 | - |
5840 | - TP_ARGS(pool_name, nbytes, entropy_count, IP) |
5841 | -); |
5842 | - |
5843 | -DEFINE_EVENT(random__extract_entropy, extract_entropy_user, |
5844 | - TP_PROTO(const char *pool_name, int nbytes, int entropy_count, |
5845 | - unsigned long IP), |
5846 | - |
5847 | - TP_ARGS(pool_name, nbytes, entropy_count, IP) |
5848 | -); |
5849 | - |
5850 | -TRACE_EVENT(random_read, |
5851 | - TP_PROTO(int got_bits, int need_bits, int pool_left, int input_left), |
5852 | - |
5853 | - TP_ARGS(got_bits, need_bits, pool_left, input_left), |
5854 | - |
5855 | - TP_STRUCT__entry( |
5856 | - __field( int, got_bits ) |
5857 | - __field( int, need_bits ) |
5858 | - __field( int, pool_left ) |
5859 | - __field( int, input_left ) |
5860 | - ), |
5861 | - |
5862 | - TP_fast_assign( |
5863 | - __entry->got_bits = got_bits; |
5864 | - __entry->need_bits = need_bits; |
5865 | - __entry->pool_left = pool_left; |
5866 | - __entry->input_left = input_left; |
5867 | - ), |
5868 | - |
5869 | - TP_printk("got_bits %d still_needed_bits %d " |
5870 | - "blocking_pool_entropy_left %d input_entropy_left %d", |
5871 | - __entry->got_bits, __entry->got_bits, __entry->pool_left, |
5872 | - __entry->input_left) |
5873 | -); |
5874 | - |
5875 | -TRACE_EVENT(urandom_read, |
5876 | - TP_PROTO(int got_bits, int pool_left, int input_left), |
5877 | - |
5878 | - TP_ARGS(got_bits, pool_left, input_left), |
5879 | - |
5880 | - TP_STRUCT__entry( |
5881 | - __field( int, got_bits ) |
5882 | - __field( int, pool_left ) |
5883 | - __field( int, input_left ) |
5884 | - ), |
5885 | - |
5886 | - TP_fast_assign( |
5887 | - __entry->got_bits = got_bits; |
5888 | - __entry->pool_left = pool_left; |
5889 | - __entry->input_left = input_left; |
5890 | - ), |
5891 | - |
5892 | - TP_printk("got_bits %d nonblocking_pool_entropy_left %d " |
5893 | - "input_entropy_left %d", __entry->got_bits, |
5894 | - __entry->pool_left, __entry->input_left) |
5895 | -); |
5896 | - |
5897 | -#endif /* _TRACE_RANDOM_H */ |
5898 | - |
5899 | -/* This part must be outside protection */ |
5900 | -#include <trace/define_trace.h> |
5901 | diff --git a/include/uapi/linux/random.h b/include/uapi/linux/random.h |
5902 | index b455b0d86f269..b01d118750ab2 100644 |
5903 | --- a/include/uapi/linux/random.h |
5904 | +++ b/include/uapi/linux/random.h |
5905 | @@ -47,9 +47,11 @@ struct rand_pool_info { |
5906 | * Flags for getrandom(2) |
5907 | * |
5908 | * GRND_NONBLOCK Don't block and return EAGAIN instead |
5909 | - * GRND_RANDOM Use the /dev/random pool instead of /dev/urandom |
5910 | + * GRND_RANDOM No effect |
5911 | + * GRND_INSECURE Return non-cryptographic random bytes |
5912 | */ |
5913 | #define GRND_NONBLOCK 0x0001 |
5914 | #define GRND_RANDOM 0x0002 |
5915 | +#define GRND_INSECURE 0x0004 |
5916 | |
5917 | #endif /* _UAPI_LINUX_RANDOM_H */ |
5918 | diff --git a/include/uapi/linux/swab.h b/include/uapi/linux/swab.h |
5919 | index 8f3a8f606fd95..1f42d110987a4 100644 |
5920 | --- a/include/uapi/linux/swab.h |
5921 | +++ b/include/uapi/linux/swab.h |
5922 | @@ -3,6 +3,7 @@ |
5923 | |
5924 | #include <linux/types.h> |
5925 | #include <linux/compiler.h> |
5926 | +#include <asm/bitsperlong.h> |
5927 | #include <asm/swab.h> |
5928 | |
5929 | /* |
5930 | @@ -131,6 +132,15 @@ static inline __attribute_const__ __u32 __fswahb32(__u32 val) |
5931 | __fswab64(x)) |
5932 | #endif |
5933 | |
5934 | +static __always_inline unsigned long __swab(const unsigned long y) |
5935 | +{ |
5936 | +#if BITS_PER_LONG == 64 |
5937 | + return __swab64(y); |
5938 | +#else /* BITS_PER_LONG == 32 */ |
5939 | + return __swab32(y); |
5940 | +#endif |
5941 | +} |
5942 | + |
5943 | /** |
5944 | * __swahw32 - return a word-swapped 32-bit value |
5945 | * @x: value to wordswap |
5946 | diff --git a/init/main.c b/init/main.c |
5947 | index 0c01d4e10d2f3..30226a836c8b5 100644 |
5948 | --- a/init/main.c |
5949 | +++ b/init/main.c |
5950 | @@ -490,6 +490,7 @@ asmlinkage __visible void __init start_kernel(void) |
5951 | /* |
5952 | * Set up the the initial canary ASAP: |
5953 | */ |
5954 | + add_latent_entropy(); |
5955 | boot_init_stack_canary(); |
5956 | |
5957 | cgroup_init_early(); |
5958 | @@ -553,6 +554,14 @@ asmlinkage __visible void __init start_kernel(void) |
5959 | "Interrupts were enabled *very* early, fixing it\n")) |
5960 | local_irq_disable(); |
5961 | idr_init_cache(); |
5962 | + |
5963 | + /* |
5964 | + * Allow workqueue creation and work item queueing/cancelling |
5965 | + * early. Work item execution depends on kthreads and starts after |
5966 | + * workqueue_init(). |
5967 | + */ |
5968 | + workqueue_init_early(); |
5969 | + |
5970 | rcu_init(); |
5971 | |
5972 | /* trace_printk() and trace points may be used after this */ |
5973 | @@ -570,6 +579,17 @@ asmlinkage __visible void __init start_kernel(void) |
5974 | softirq_init(); |
5975 | timekeeping_init(); |
5976 | time_init(); |
5977 | + |
5978 | + /* |
5979 | + * For best initial stack canary entropy, prepare it after: |
5980 | + * - setup_arch() for any UEFI RNG entropy and boot cmdline access |
5981 | + * - timekeeping_init() for ktime entropy used in random_init() |
5982 | + * - time_init() for making random_get_entropy() work on some platforms |
5983 | + * - random_init() to initialize the RNG from from early entropy sources |
5984 | + */ |
5985 | + random_init(command_line); |
5986 | + boot_init_stack_canary(); |
5987 | + |
5988 | sched_clock_postinit(); |
5989 | printk_nmi_init(); |
5990 | perf_event_init(); |
5991 | @@ -1011,6 +1031,8 @@ static noinline void __init kernel_init_freeable(void) |
5992 | |
5993 | smp_prepare_cpus(setup_max_cpus); |
5994 | |
5995 | + workqueue_init(); |
5996 | + |
5997 | do_pre_smp_initcalls(); |
5998 | lockup_detector_init(); |
5999 | |
6000 | diff --git a/kernel/cpu.c b/kernel/cpu.c |
6001 | index e005209f279e1..5ddac8914515a 100644 |
6002 | --- a/kernel/cpu.c |
6003 | +++ b/kernel/cpu.c |
6004 | @@ -26,6 +26,7 @@ |
6005 | #include <linux/smpboot.h> |
6006 | #include <linux/relay.h> |
6007 | #include <linux/slab.h> |
6008 | +#include <linux/random.h> |
6009 | |
6010 | #include <trace/events/power.h> |
6011 | #define CREATE_TRACE_POINTS |
6012 | @@ -1404,6 +1405,11 @@ static struct cpuhp_step cpuhp_bp_states[] = { |
6013 | .startup.single = perf_event_init_cpu, |
6014 | .teardown.single = perf_event_exit_cpu, |
6015 | }, |
6016 | + [CPUHP_RANDOM_PREPARE] = { |
6017 | + .name = "random:prepare", |
6018 | + .startup.single = random_prepare_cpu, |
6019 | + .teardown.single = NULL, |
6020 | + }, |
6021 | [CPUHP_WORKQUEUE_PREP] = { |
6022 | .name = "workqueue:prepare", |
6023 | .startup.single = workqueue_prepare_cpu, |
6024 | @@ -1529,6 +1535,11 @@ static struct cpuhp_step cpuhp_ap_states[] = { |
6025 | .startup.single = workqueue_online_cpu, |
6026 | .teardown.single = workqueue_offline_cpu, |
6027 | }, |
6028 | + [CPUHP_AP_RANDOM_ONLINE] = { |
6029 | + .name = "random:online", |
6030 | + .startup.single = random_online_cpu, |
6031 | + .teardown.single = NULL, |
6032 | + }, |
6033 | [CPUHP_AP_RCUTREE_ONLINE] = { |
6034 | .name = "RCU/tree:online", |
6035 | .startup.single = rcutree_online_cpu, |
6036 | diff --git a/kernel/irq/handle.c b/kernel/irq/handle.c |
6037 | index d3f24905852c9..374c45fbe3db6 100644 |
6038 | --- a/kernel/irq/handle.c |
6039 | +++ b/kernel/irq/handle.c |
6040 | @@ -184,7 +184,7 @@ irqreturn_t handle_irq_event_percpu(struct irq_desc *desc) |
6041 | |
6042 | retval = __handle_irq_event_percpu(desc, &flags); |
6043 | |
6044 | - add_interrupt_randomness(desc->irq_data.irq, flags); |
6045 | + add_interrupt_randomness(desc->irq_data.irq); |
6046 | |
6047 | if (!noirqdebug) |
6048 | note_interrupt(desc, retval); |
6049 | diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c |
6050 | index bcba817f7af20..828728639e0dd 100644 |
6051 | --- a/kernel/time/timekeeping.c |
6052 | +++ b/kernel/time/timekeeping.c |
6053 | @@ -19,6 +19,7 @@ |
6054 | #include <linux/clocksource.h> |
6055 | #include <linux/jiffies.h> |
6056 | #include <linux/time.h> |
6057 | +#include <linux/timex.h> |
6058 | #include <linux/tick.h> |
6059 | #include <linux/stop_machine.h> |
6060 | #include <linux/pvclock_gtod.h> |
6061 | @@ -2269,6 +2270,21 @@ ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real, |
6062 | return base; |
6063 | } |
6064 | |
6065 | +/** |
6066 | + * random_get_entropy_fallback - Returns the raw clock source value, |
6067 | + * used by random.c for platforms with no valid random_get_entropy(). |
6068 | + */ |
6069 | +unsigned long random_get_entropy_fallback(void) |
6070 | +{ |
6071 | + struct tk_read_base *tkr = &tk_core.timekeeper.tkr_mono; |
6072 | + struct clocksource *clock = READ_ONCE(tkr->clock); |
6073 | + |
6074 | + if (unlikely(timekeeping_suspended || !clock)) |
6075 | + return 0; |
6076 | + return clock->read(clock); |
6077 | +} |
6078 | +EXPORT_SYMBOL_GPL(random_get_entropy_fallback); |
6079 | + |
6080 | /** |
6081 | * do_adjtimex() - Accessor function to NTP __do_adjtimex function |
6082 | */ |
6083 | diff --git a/kernel/workqueue.c b/kernel/workqueue.c |
6084 | index b3476a21a7b31..df96f797939e4 100644 |
6085 | --- a/kernel/workqueue.c |
6086 | +++ b/kernel/workqueue.c |
6087 | @@ -292,6 +292,8 @@ module_param_named(disable_numa, wq_disable_numa, bool, 0444); |
6088 | static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); |
6089 | module_param_named(power_efficient, wq_power_efficient, bool, 0444); |
6090 | |
6091 | +bool wq_online; /* can kworkers be created yet? */ |
6092 | + |
6093 | static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ |
6094 | |
6095 | /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ |
6096 | @@ -2588,6 +2590,9 @@ void flush_workqueue(struct workqueue_struct *wq) |
6097 | }; |
6098 | int next_color; |
6099 | |
6100 | + if (WARN_ON(!wq_online)) |
6101 | + return; |
6102 | + |
6103 | lock_map_acquire(&wq->lockdep_map); |
6104 | lock_map_release(&wq->lockdep_map); |
6105 | |
6106 | @@ -2848,6 +2853,9 @@ bool flush_work(struct work_struct *work) |
6107 | { |
6108 | struct wq_barrier barr; |
6109 | |
6110 | + if (WARN_ON(!wq_online)) |
6111 | + return false; |
6112 | + |
6113 | lock_map_acquire(&work->lockdep_map); |
6114 | lock_map_release(&work->lockdep_map); |
6115 | |
6116 | @@ -2918,7 +2926,13 @@ static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) |
6117 | mark_work_canceling(work); |
6118 | local_irq_restore(flags); |
6119 | |
6120 | - flush_work(work); |
6121 | + /* |
6122 | + * This allows canceling during early boot. We know that @work |
6123 | + * isn't executing. |
6124 | + */ |
6125 | + if (wq_online) |
6126 | + flush_work(work); |
6127 | + |
6128 | clear_work_data(work); |
6129 | |
6130 | /* |
6131 | @@ -3368,7 +3382,7 @@ static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) |
6132 | goto fail; |
6133 | |
6134 | /* create and start the initial worker */ |
6135 | - if (!create_worker(pool)) |
6136 | + if (wq_online && !create_worker(pool)) |
6137 | goto fail; |
6138 | |
6139 | /* install */ |
6140 | @@ -3439,6 +3453,7 @@ static void pwq_adjust_max_active(struct pool_workqueue *pwq) |
6141 | { |
6142 | struct workqueue_struct *wq = pwq->wq; |
6143 | bool freezable = wq->flags & WQ_FREEZABLE; |
6144 | + unsigned long flags; |
6145 | |
6146 | /* for @wq->saved_max_active */ |
6147 | lockdep_assert_held(&wq->mutex); |
6148 | @@ -3447,7 +3462,8 @@ static void pwq_adjust_max_active(struct pool_workqueue *pwq) |
6149 | if (!freezable && pwq->max_active == wq->saved_max_active) |
6150 | return; |
6151 | |
6152 | - spin_lock_irq(&pwq->pool->lock); |
6153 | + /* this function can be called during early boot w/ irq disabled */ |
6154 | + spin_lock_irqsave(&pwq->pool->lock, flags); |
6155 | |
6156 | /* |
6157 | * During [un]freezing, the caller is responsible for ensuring that |
6158 | @@ -3477,7 +3493,7 @@ static void pwq_adjust_max_active(struct pool_workqueue *pwq) |
6159 | pwq->max_active = 0; |
6160 | } |
6161 | |
6162 | - spin_unlock_irq(&pwq->pool->lock); |
6163 | + spin_unlock_irqrestore(&pwq->pool->lock, flags); |
6164 | } |
6165 | |
6166 | /* initialize newly alloced @pwq which is associated with @wq and @pool */ |
6167 | @@ -5550,7 +5566,17 @@ static void __init wq_numa_init(void) |
6168 | wq_numa_enabled = true; |
6169 | } |
6170 | |
6171 | -static int __init init_workqueues(void) |
6172 | +/** |
6173 | + * workqueue_init_early - early init for workqueue subsystem |
6174 | + * |
6175 | + * This is the first half of two-staged workqueue subsystem initialization |
6176 | + * and invoked as soon as the bare basics - memory allocation, cpumasks and |
6177 | + * idr are up. It sets up all the data structures and system workqueues |
6178 | + * and allows early boot code to create workqueues and queue/cancel work |
6179 | + * items. Actual work item execution starts only after kthreads can be |
6180 | + * created and scheduled right before early initcalls. |
6181 | + */ |
6182 | +int __init workqueue_init_early(void) |
6183 | { |
6184 | int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; |
6185 | int i, cpu; |
6186 | @@ -5583,16 +5609,6 @@ static int __init init_workqueues(void) |
6187 | } |
6188 | } |
6189 | |
6190 | - /* create the initial worker */ |
6191 | - for_each_online_cpu(cpu) { |
6192 | - struct worker_pool *pool; |
6193 | - |
6194 | - for_each_cpu_worker_pool(pool, cpu) { |
6195 | - pool->flags &= ~POOL_DISASSOCIATED; |
6196 | - BUG_ON(!create_worker(pool)); |
6197 | - } |
6198 | - } |
6199 | - |
6200 | /* create default unbound and ordered wq attrs */ |
6201 | for (i = 0; i < NR_STD_WORKER_POOLS; i++) { |
6202 | struct workqueue_attrs *attrs; |
6203 | @@ -5629,8 +5645,36 @@ static int __init init_workqueues(void) |
6204 | !system_power_efficient_wq || |
6205 | !system_freezable_power_efficient_wq); |
6206 | |
6207 | + return 0; |
6208 | +} |
6209 | + |
6210 | +/** |
6211 | + * workqueue_init - bring workqueue subsystem fully online |
6212 | + * |
6213 | + * This is the latter half of two-staged workqueue subsystem initialization |
6214 | + * and invoked as soon as kthreads can be created and scheduled. |
6215 | + * Workqueues have been created and work items queued on them, but there |
6216 | + * are no kworkers executing the work items yet. Populate the worker pools |
6217 | + * with the initial workers and enable future kworker creations. |
6218 | + */ |
6219 | +int __init workqueue_init(void) |
6220 | +{ |
6221 | + struct worker_pool *pool; |
6222 | + int cpu, bkt; |
6223 | + |
6224 | + /* create the initial workers */ |
6225 | + for_each_online_cpu(cpu) { |
6226 | + for_each_cpu_worker_pool(pool, cpu) { |
6227 | + pool->flags &= ~POOL_DISASSOCIATED; |
6228 | + BUG_ON(!create_worker(pool)); |
6229 | + } |
6230 | + } |
6231 | + |
6232 | + hash_for_each(unbound_pool_hash, bkt, pool, hash_node) |
6233 | + BUG_ON(!create_worker(pool)); |
6234 | + |
6235 | + wq_online = true; |
6236 | wq_watchdog_init(); |
6237 | |
6238 | return 0; |
6239 | } |
6240 | -early_initcall(init_workqueues); |
6241 | diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug |
6242 | index e7addfcd302f4..95be8e1b12f60 100644 |
6243 | --- a/lib/Kconfig.debug |
6244 | +++ b/lib/Kconfig.debug |
6245 | @@ -1177,6 +1177,33 @@ config STACKTRACE |
6246 | It is also used by various kernel debugging features that require |
6247 | stack trace generation. |
6248 | |
6249 | +config WARN_ALL_UNSEEDED_RANDOM |
6250 | + bool "Warn for all uses of unseeded randomness" |
6251 | + default n |
6252 | + help |
6253 | + Some parts of the kernel contain bugs relating to their use of |
6254 | + cryptographically secure random numbers before it's actually possible |
6255 | + to generate those numbers securely. This setting ensures that these |
6256 | + flaws don't go unnoticed, by enabling a message, should this ever |
6257 | + occur. This will allow people with obscure setups to know when things |
6258 | + are going wrong, so that they might contact developers about fixing |
6259 | + it. |
6260 | + |
6261 | + Unfortunately, on some models of some architectures getting |
6262 | + a fully seeded CRNG is extremely difficult, and so this can |
6263 | + result in dmesg getting spammed for a surprisingly long |
6264 | + time. This is really bad from a security perspective, and |
6265 | + so architecture maintainers really need to do what they can |
6266 | + to get the CRNG seeded sooner after the system is booted. |
6267 | + However, since users cannot do anything actionable to |
6268 | + address this, by default this option is disabled. |
6269 | + |
6270 | + Say Y here if you want to receive warnings for all uses of |
6271 | + unseeded randomness. This will be of use primarily for |
6272 | + those developers interersted in improving the security of |
6273 | + Linux kernels running on their architecture (or |
6274 | + subarchitecture). |
6275 | + |
6276 | config DEBUG_KOBJECT |
6277 | bool "kobject debugging" |
6278 | depends on DEBUG_KERNEL |
6279 | diff --git a/lib/Makefile b/lib/Makefile |
6280 | index 7a55c52052810..775d6ef6d2eff 100644 |
6281 | --- a/lib/Makefile |
6282 | +++ b/lib/Makefile |
6283 | @@ -234,3 +234,5 @@ KASAN_SANITIZE_ubsan.o := n |
6284 | CFLAGS_ubsan.o := $(call cc-option, -fno-stack-protector) $(DISABLE_STACKLEAK_PLUGIN) |
6285 | |
6286 | obj-$(CONFIG_SBITMAP) += sbitmap.o |
6287 | + |
6288 | +obj-y += crypto/ |
6289 | diff --git a/lib/chacha20.c b/lib/chacha20.c |
6290 | index 250ceed9ec9a8..5f6c222e939a9 100644 |
6291 | --- a/lib/chacha20.c |
6292 | +++ b/lib/chacha20.c |
6293 | @@ -21,9 +21,9 @@ static inline u32 rotl32(u32 v, u8 n) |
6294 | return (v << n) | (v >> (sizeof(v) * 8 - n)); |
6295 | } |
6296 | |
6297 | -extern void chacha20_block(u32 *state, void *stream) |
6298 | +void chacha20_block(u32 *state, u8 *stream) |
6299 | { |
6300 | - u32 x[16], *out = stream; |
6301 | + u32 x[16]; |
6302 | int i; |
6303 | |
6304 | for (i = 0; i < ARRAY_SIZE(x); i++) |
6305 | @@ -72,7 +72,7 @@ extern void chacha20_block(u32 *state, void *stream) |
6306 | } |
6307 | |
6308 | for (i = 0; i < ARRAY_SIZE(x); i++) |
6309 | - out[i] = cpu_to_le32(x[i] + state[i]); |
6310 | + put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]); |
6311 | |
6312 | state[12]++; |
6313 | } |
6314 | diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile |
6315 | new file mode 100644 |
6316 | index 0000000000000..d0bca68618f03 |
6317 | --- /dev/null |
6318 | +++ b/lib/crypto/Makefile |
6319 | @@ -0,0 +1,7 @@ |
6320 | +# SPDX-License-Identifier: GPL-2.0 |
6321 | + |
6322 | +obj-y += libblake2s.o |
6323 | +libblake2s-y += blake2s.o blake2s-generic.o |
6324 | +ifneq ($(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS),y) |
6325 | +libblake2s-y += blake2s-selftest.o |
6326 | +endif |
6327 | diff --git a/lib/crypto/blake2s-generic.c b/lib/crypto/blake2s-generic.c |
6328 | new file mode 100644 |
6329 | index 0000000000000..04ff8df245136 |
6330 | --- /dev/null |
6331 | +++ b/lib/crypto/blake2s-generic.c |
6332 | @@ -0,0 +1,111 @@ |
6333 | +// SPDX-License-Identifier: GPL-2.0 OR MIT |
6334 | +/* |
6335 | + * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
6336 | + * |
6337 | + * This is an implementation of the BLAKE2s hash and PRF functions. |
6338 | + * |
6339 | + * Information: https://blake2.net/ |
6340 | + * |
6341 | + */ |
6342 | + |
6343 | +#include <crypto/internal/blake2s.h> |
6344 | +#include <linux/types.h> |
6345 | +#include <linux/string.h> |
6346 | +#include <linux/kernel.h> |
6347 | +#include <linux/module.h> |
6348 | +#include <linux/init.h> |
6349 | +#include <linux/bug.h> |
6350 | +#include <asm/unaligned.h> |
6351 | + |
6352 | +static const u8 blake2s_sigma[10][16] = { |
6353 | + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, |
6354 | + { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, |
6355 | + { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, |
6356 | + { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, |
6357 | + { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, |
6358 | + { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, |
6359 | + { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, |
6360 | + { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, |
6361 | + { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, |
6362 | + { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 }, |
6363 | +}; |
6364 | + |
6365 | +static inline void blake2s_increment_counter(struct blake2s_state *state, |
6366 | + const u32 inc) |
6367 | +{ |
6368 | + state->t[0] += inc; |
6369 | + state->t[1] += (state->t[0] < inc); |
6370 | +} |
6371 | + |
6372 | +void blake2s_compress_generic(struct blake2s_state *state,const u8 *block, |
6373 | + size_t nblocks, const u32 inc) |
6374 | +{ |
6375 | + u32 m[16]; |
6376 | + u32 v[16]; |
6377 | + int i; |
6378 | + |
6379 | + WARN_ON(IS_ENABLED(DEBUG) && |
6380 | + (nblocks > 1 && inc != BLAKE2S_BLOCK_SIZE)); |
6381 | + |
6382 | + while (nblocks > 0) { |
6383 | + blake2s_increment_counter(state, inc); |
6384 | + memcpy(m, block, BLAKE2S_BLOCK_SIZE); |
6385 | + le32_to_cpu_array(m, ARRAY_SIZE(m)); |
6386 | + memcpy(v, state->h, 32); |
6387 | + v[ 8] = BLAKE2S_IV0; |
6388 | + v[ 9] = BLAKE2S_IV1; |
6389 | + v[10] = BLAKE2S_IV2; |
6390 | + v[11] = BLAKE2S_IV3; |
6391 | + v[12] = BLAKE2S_IV4 ^ state->t[0]; |
6392 | + v[13] = BLAKE2S_IV5 ^ state->t[1]; |
6393 | + v[14] = BLAKE2S_IV6 ^ state->f[0]; |
6394 | + v[15] = BLAKE2S_IV7 ^ state->f[1]; |
6395 | + |
6396 | +#define G(r, i, a, b, c, d) do { \ |
6397 | + a += b + m[blake2s_sigma[r][2 * i + 0]]; \ |
6398 | + d = ror32(d ^ a, 16); \ |
6399 | + c += d; \ |
6400 | + b = ror32(b ^ c, 12); \ |
6401 | + a += b + m[blake2s_sigma[r][2 * i + 1]]; \ |
6402 | + d = ror32(d ^ a, 8); \ |
6403 | + c += d; \ |
6404 | + b = ror32(b ^ c, 7); \ |
6405 | +} while (0) |
6406 | + |
6407 | +#define ROUND(r) do { \ |
6408 | + G(r, 0, v[0], v[ 4], v[ 8], v[12]); \ |
6409 | + G(r, 1, v[1], v[ 5], v[ 9], v[13]); \ |
6410 | + G(r, 2, v[2], v[ 6], v[10], v[14]); \ |
6411 | + G(r, 3, v[3], v[ 7], v[11], v[15]); \ |
6412 | + G(r, 4, v[0], v[ 5], v[10], v[15]); \ |
6413 | + G(r, 5, v[1], v[ 6], v[11], v[12]); \ |
6414 | + G(r, 6, v[2], v[ 7], v[ 8], v[13]); \ |
6415 | + G(r, 7, v[3], v[ 4], v[ 9], v[14]); \ |
6416 | +} while (0) |
6417 | + ROUND(0); |
6418 | + ROUND(1); |
6419 | + ROUND(2); |
6420 | + ROUND(3); |
6421 | + ROUND(4); |
6422 | + ROUND(5); |
6423 | + ROUND(6); |
6424 | + ROUND(7); |
6425 | + ROUND(8); |
6426 | + ROUND(9); |
6427 | + |
6428 | +#undef G |
6429 | +#undef ROUND |
6430 | + |
6431 | + for (i = 0; i < 8; ++i) |
6432 | + state->h[i] ^= v[i] ^ v[i + 8]; |
6433 | + |
6434 | + block += BLAKE2S_BLOCK_SIZE; |
6435 | + --nblocks; |
6436 | + } |
6437 | +} |
6438 | + |
6439 | +EXPORT_SYMBOL(blake2s_compress_generic); |
6440 | + |
6441 | +MODULE_LICENSE("GPL v2"); |
6442 | +MODULE_DESCRIPTION("BLAKE2s hash function"); |
6443 | +MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>"); |
6444 | diff --git a/lib/crypto/blake2s-selftest.c b/lib/crypto/blake2s-selftest.c |
6445 | new file mode 100644 |
6446 | index 0000000000000..7a9edc96ddddf |
6447 | --- /dev/null |
6448 | +++ b/lib/crypto/blake2s-selftest.c |
6449 | @@ -0,0 +1,591 @@ |
6450 | +// SPDX-License-Identifier: GPL-2.0 OR MIT |
6451 | +/* |
6452 | + * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
6453 | + */ |
6454 | + |
6455 | +#include <crypto/blake2s.h> |
6456 | +#include <linux/string.h> |
6457 | + |
6458 | +/* |
6459 | + * blake2s_testvecs[] generated with the program below (using libb2-dev and |
6460 | + * libssl-dev [OpenSSL]) |
6461 | + * |
6462 | + * #include <blake2.h> |
6463 | + * #include <stdint.h> |
6464 | + * #include <stdio.h> |
6465 | + * |
6466 | + * #include <openssl/evp.h> |
6467 | + * |
6468 | + * #define BLAKE2S_TESTVEC_COUNT 256 |
6469 | + * |
6470 | + * static void print_vec(const uint8_t vec[], int len) |
6471 | + * { |
6472 | + * int i; |
6473 | + * |
6474 | + * printf(" { "); |
6475 | + * for (i = 0; i < len; i++) { |
6476 | + * if (i && (i % 12) == 0) |
6477 | + * printf("\n "); |
6478 | + * printf("0x%02x, ", vec[i]); |
6479 | + * } |
6480 | + * printf("},\n"); |
6481 | + * } |
6482 | + * |
6483 | + * int main(void) |
6484 | + * { |
6485 | + * uint8_t key[BLAKE2S_KEYBYTES]; |
6486 | + * uint8_t buf[BLAKE2S_TESTVEC_COUNT]; |
6487 | + * uint8_t hash[BLAKE2S_OUTBYTES]; |
6488 | + * int i, j; |
6489 | + * |
6490 | + * key[0] = key[1] = 1; |
6491 | + * for (i = 2; i < BLAKE2S_KEYBYTES; ++i) |
6492 | + * key[i] = key[i - 2] + key[i - 1]; |
6493 | + * |
6494 | + * for (i = 0; i < BLAKE2S_TESTVEC_COUNT; ++i) |
6495 | + * buf[i] = (uint8_t)i; |
6496 | + * |
6497 | + * printf("static const u8 blake2s_testvecs[][BLAKE2S_HASH_SIZE] __initconst = {\n"); |
6498 | + * |
6499 | + * for (i = 0; i < BLAKE2S_TESTVEC_COUNT; ++i) { |
6500 | + * int outlen = 1 + i % BLAKE2S_OUTBYTES; |
6501 | + * int keylen = (13 * i) % (BLAKE2S_KEYBYTES + 1); |
6502 | + * |
6503 | + * blake2s(hash, buf, key + BLAKE2S_KEYBYTES - keylen, outlen, i, |
6504 | + * keylen); |
6505 | + * print_vec(hash, outlen); |
6506 | + * } |
6507 | + * printf("};\n\n"); |
6508 | + * |
6509 | + * return 0; |
6510 | + *} |
6511 | + */ |
6512 | +static const u8 blake2s_testvecs[][BLAKE2S_HASH_SIZE] __initconst = { |
6513 | + { 0xa1, }, |
6514 | + { 0x7c, 0x89, }, |
6515 | + { 0x74, 0x0e, 0xd4, }, |
6516 | + { 0x47, 0x0c, 0x21, 0x15, }, |
6517 | + { 0x18, 0xd6, 0x9c, 0xa6, 0xc4, }, |
6518 | + { 0x13, 0x5d, 0x16, 0x63, 0x2e, 0xf9, }, |
6519 | + { 0x2c, 0xb5, 0x04, 0xb7, 0x99, 0xe2, 0x73, }, |
6520 | + { 0x9a, 0x0f, 0xd2, 0x39, 0xd6, 0x68, 0x1b, 0x92, }, |
6521 | + { 0xc8, 0xde, 0x7a, 0xea, 0x2f, 0xf4, 0xd2, 0xe3, 0x2b, }, |
6522 | + { 0x5b, 0xf9, 0x43, 0x52, 0x0c, 0x12, 0xba, 0xb5, 0x93, 0x9f, }, |
6523 | + { 0xc6, 0x2c, 0x4e, 0x80, 0xfc, 0x32, 0x5b, 0x33, 0xb8, 0xb8, 0x0a, }, |
6524 | + { 0xa7, 0x5c, 0xfd, 0x3a, 0xcc, 0xbf, 0x90, 0xca, 0xb7, 0x97, 0xde, 0xd8, }, |
6525 | + { 0x66, 0xca, 0x3c, 0xc4, 0x19, 0xef, 0x92, 0x66, 0x3f, 0x21, 0x8f, 0xda, |
6526 | + 0xb7, }, |
6527 | + { 0xba, 0xe5, 0xbb, 0x30, 0x25, 0x94, 0x6d, 0xc3, 0x89, 0x09, 0xc4, 0x25, |
6528 | + 0x52, 0x3e, }, |
6529 | + { 0xa2, 0xef, 0x0e, 0x52, 0x0b, 0x5f, 0xa2, 0x01, 0x6d, 0x0a, 0x25, 0xbc, |
6530 | + 0x57, 0xe2, 0x27, }, |
6531 | + { 0x4f, 0xe0, 0xf9, 0x52, 0x12, 0xda, 0x84, 0xb7, 0xab, 0xae, 0xb0, 0xa6, |
6532 | + 0x47, 0x2a, 0xc7, 0xf5, }, |
6533 | + { 0x56, 0xe7, 0xa8, 0x1c, 0x4c, 0xca, 0xed, 0x90, 0x31, 0xec, 0x87, 0x43, |
6534 | + 0xe7, 0x72, 0x08, 0xec, 0xbe, }, |
6535 | + { 0x7e, 0xdf, 0x80, 0x1c, 0x93, 0x33, 0xfd, 0x53, 0x44, 0xba, 0xfd, 0x96, |
6536 | + 0xe1, 0xbb, 0xb5, 0x65, 0xa5, 0x00, }, |
6537 | + { 0xec, 0x6b, 0xed, 0xf7, 0x7b, 0x62, 0x1d, 0x7d, 0xf4, 0x82, 0xf3, 0x1e, |
6538 | + 0x18, 0xff, 0x2b, 0xc4, 0x06, 0x20, 0x2a, }, |
6539 | + { 0x74, 0x98, 0xd7, 0x68, 0x63, 0xed, 0x87, 0xe4, 0x5d, 0x8d, 0x9e, 0x1d, |
6540 | + 0xfd, 0x2a, 0xbb, 0x86, 0xac, 0xe9, 0x2a, 0x89, }, |
6541 | + { 0x89, 0xc3, 0x88, 0xce, 0x2b, 0x33, 0x1e, 0x10, 0xd1, 0x37, 0x20, 0x86, |
6542 | + 0x28, 0x43, 0x70, 0xd9, 0xfb, 0x96, 0xd9, 0xb5, 0xd3, }, |
6543 | + { 0xcb, 0x56, 0x74, 0x41, 0x8d, 0x80, 0x01, 0x9a, 0x6b, 0x38, 0xe1, 0x41, |
6544 | + 0xad, 0x9c, 0x62, 0x74, 0xce, 0x35, 0xd5, 0x6c, 0x89, 0x6e, }, |
6545 | + { 0x79, 0xaf, 0x94, 0x59, 0x99, 0x26, 0xe1, 0xc9, 0x34, 0xfe, 0x7c, 0x22, |
6546 | + 0xf7, 0x43, 0xd7, 0x65, 0xd4, 0x48, 0x18, 0xac, 0x3d, 0xfd, 0x93, }, |
6547 | + { 0x85, 0x0d, 0xff, 0xb8, 0x3e, 0x87, 0x41, 0xb0, 0x95, 0xd3, 0x3d, 0x00, |
6548 | + 0x47, 0x55, 0x9e, 0xd2, 0x69, 0xea, 0xbf, 0xe9, 0x7a, 0x2d, 0x61, 0x45, }, |
6549 | + { 0x03, 0xe0, 0x85, 0xec, 0x54, 0xb5, 0x16, 0x53, 0xa8, 0xc4, 0x71, 0xe9, |
6550 | + 0x6a, 0xe7, 0xcb, 0xc4, 0x15, 0x02, 0xfc, 0x34, 0xa4, 0xa4, 0x28, 0x13, |
6551 | + 0xd1, }, |
6552 | + { 0xe3, 0x34, 0x4b, 0xe1, 0xd0, 0x4b, 0x55, 0x61, 0x8f, 0xc0, 0x24, 0x05, |
6553 | + 0xe6, 0xe0, 0x3d, 0x70, 0x24, 0x4d, 0xda, 0xb8, 0x91, 0x05, 0x29, 0x07, |
6554 | + 0x01, 0x3e, }, |
6555 | + { 0x61, 0xff, 0x01, 0x72, 0xb1, 0x4d, 0xf6, 0xfe, 0xd1, 0xd1, 0x08, 0x74, |
6556 | + 0xe6, 0x91, 0x44, 0xeb, 0x61, 0xda, 0x40, 0xaf, 0xfc, 0x8c, 0x91, 0x6b, |
6557 | + 0xec, 0x13, 0xed, }, |
6558 | + { 0xd4, 0x40, 0xd2, 0xa0, 0x7f, 0xc1, 0x58, 0x0c, 0x85, 0xa0, 0x86, 0xc7, |
6559 | + 0x86, 0xb9, 0x61, 0xc9, 0xea, 0x19, 0x86, 0x1f, 0xab, 0x07, 0xce, 0x37, |
6560 | + 0x72, 0x67, 0x09, 0xfc, }, |
6561 | + { 0x9e, 0xf8, 0x18, 0x67, 0x93, 0x10, 0x9b, 0x39, 0x75, 0xe8, 0x8b, 0x38, |
6562 | + 0x82, 0x7d, 0xb8, 0xb7, 0xa5, 0xaf, 0xe6, 0x6a, 0x22, 0x5e, 0x1f, 0x9c, |
6563 | + 0x95, 0x29, 0x19, 0xf2, 0x4b, }, |
6564 | + { 0xc8, 0x62, 0x25, 0xf5, 0x98, 0xc9, 0xea, 0xe5, 0x29, 0x3a, 0xd3, 0x22, |
6565 | + 0xeb, 0xeb, 0x07, 0x7c, 0x15, 0x07, 0xee, 0x15, 0x61, 0xbb, 0x05, 0x30, |
6566 | + 0x99, 0x7f, 0x11, 0xf6, 0x0a, 0x1d, }, |
6567 | + { 0x68, 0x70, 0xf7, 0x90, 0xa1, 0x8b, 0x1f, 0x0f, 0xbb, 0xce, 0xd2, 0x0e, |
6568 | + 0x33, 0x1f, 0x7f, 0xa9, 0x78, 0xa8, 0xa6, 0x81, 0x66, 0xab, 0x8d, 0xcd, |
6569 | + 0x58, 0x55, 0x3a, 0x0b, 0x7a, 0xdb, 0xb5, }, |
6570 | + { 0xdd, 0x35, 0xd2, 0xb4, 0xf6, 0xc7, 0xea, 0xab, 0x64, 0x24, 0x4e, 0xfe, |
6571 | + 0xe5, 0x3d, 0x4e, 0x95, 0x8b, 0x6d, 0x6c, 0xbc, 0xb0, 0xf8, 0x88, 0x61, |
6572 | + 0x09, 0xb7, 0x78, 0xa3, 0x31, 0xfe, 0xd9, 0x2f, }, |
6573 | + { 0x0a, }, |
6574 | + { 0x6e, 0xd4, }, |
6575 | + { 0x64, 0xe9, 0xd1, }, |
6576 | + { 0x30, 0xdd, 0x71, 0xef, }, |
6577 | + { 0x11, 0xb5, 0x0c, 0x87, 0xc9, }, |
6578 | + { 0x06, 0x1c, 0x6d, 0x04, 0x82, 0xd0, }, |
6579 | + { 0x5c, 0x42, 0x0b, 0xee, 0xc5, 0x9c, 0xb2, }, |
6580 | + { 0xe8, 0x29, 0xd6, 0xb4, 0x5d, 0xf7, 0x2b, 0x93, }, |
6581 | + { 0x18, 0xca, 0x27, 0x72, 0x43, 0x39, 0x16, 0xbc, 0x6a, }, |
6582 | + { 0x39, 0x8f, 0xfd, 0x64, 0xf5, 0x57, 0x23, 0xb0, 0x45, 0xf8, }, |
6583 | + { 0xbb, 0x3a, 0x78, 0x6b, 0x02, 0x1d, 0x0b, 0x16, 0xe3, 0xb2, 0x9a, }, |
6584 | + { 0xb8, 0xb4, 0x0b, 0xe5, 0xd4, 0x1d, 0x0d, 0x85, 0x49, 0x91, 0x35, 0xfa, }, |
6585 | + { 0x6d, 0x48, 0x2a, 0x0c, 0x42, 0x08, 0xbd, 0xa9, 0x78, 0x6f, 0x18, 0xaf, |
6586 | + 0xe2, }, |
6587 | + { 0x10, 0x45, 0xd4, 0x58, 0x88, 0xec, 0x4e, 0x1e, 0xf6, 0x14, 0x92, 0x64, |
6588 | + 0x7e, 0xb0, }, |
6589 | + { 0x8b, 0x0b, 0x95, 0xee, 0x92, 0xc6, 0x3b, 0x91, 0xf1, 0x1e, 0xeb, 0x51, |
6590 | + 0x98, 0x0a, 0x8d, }, |
6591 | + { 0xa3, 0x50, 0x4d, 0xa5, 0x1d, 0x03, 0x68, 0xe9, 0x57, 0x78, 0xd6, 0x04, |
6592 | + 0xf1, 0xc3, 0x94, 0xd8, }, |
6593 | + { 0xb8, 0x66, 0x6e, 0xdd, 0x46, 0x15, 0xae, 0x3d, 0x83, 0x7e, 0xcf, 0xe7, |
6594 | + 0x2c, 0xe8, 0x8f, 0xc7, 0x34, }, |
6595 | + { 0x2e, 0xc0, 0x1f, 0x29, 0xea, 0xf6, 0xb9, 0xe2, 0xc2, 0x93, 0xeb, 0x41, |
6596 | + 0x0d, 0xf0, 0x0a, 0x13, 0x0e, 0xa2, }, |
6597 | + { 0x71, 0xb8, 0x33, 0xa9, 0x1b, 0xac, 0xf1, 0xb5, 0x42, 0x8f, 0x5e, 0x81, |
6598 | + 0x34, 0x43, 0xb7, 0xa4, 0x18, 0x5c, 0x47, }, |
6599 | + { 0xda, 0x45, 0xb8, 0x2e, 0x82, 0x1e, 0xc0, 0x59, 0x77, 0x9d, 0xfa, 0xb4, |
6600 | + 0x1c, 0x5e, 0xa0, 0x2b, 0x33, 0x96, 0x5a, 0x58, }, |
6601 | + { 0xe3, 0x09, 0x05, 0xa9, 0xeb, 0x48, 0x13, 0xad, 0x71, 0x88, 0x81, 0x9a, |
6602 | + 0x3e, 0x2c, 0xe1, 0x23, 0x99, 0x13, 0x35, 0x9f, 0xb5, }, |
6603 | + { 0xb7, 0x86, 0x2d, 0x16, 0xe1, 0x04, 0x00, 0x47, 0x47, 0x61, 0x31, 0xfb, |
6604 | + 0x14, 0xac, 0xd8, 0xe9, 0xe3, 0x49, 0xbd, 0xf7, 0x9c, 0x3f, }, |
6605 | + { 0x7f, 0xd9, 0x95, 0xa8, 0xa7, 0xa0, 0xcc, 0xba, 0xef, 0xb1, 0x0a, 0xa9, |
6606 | + 0x21, 0x62, 0x08, 0x0f, 0x1b, 0xff, 0x7b, 0x9d, 0xae, 0xb2, 0x95, }, |
6607 | + { 0x85, 0x99, 0xea, 0x33, 0xe0, 0x56, 0xff, 0x13, 0xc6, 0x61, 0x8c, 0xf9, |
6608 | + 0x57, 0x05, 0x03, 0x11, 0xf9, 0xfb, 0x3a, 0xf7, 0xce, 0xbb, 0x52, 0x30, }, |
6609 | + { 0xb2, 0x72, 0x9c, 0xf8, 0x77, 0x4e, 0x8f, 0x6b, 0x01, 0x6c, 0xff, 0x4e, |
6610 | + 0x4f, 0x02, 0xd2, 0xbc, 0xeb, 0x51, 0x28, 0x99, 0x50, 0xab, 0xc4, 0x42, |
6611 | + 0xe3, }, |
6612 | + { 0x8b, 0x0a, 0xb5, 0x90, 0x8f, 0xf5, 0x7b, 0xdd, 0xba, 0x47, 0x37, 0xc9, |
6613 | + 0x2a, 0xd5, 0x4b, 0x25, 0x08, 0x8b, 0x02, 0x17, 0xa7, 0x9e, 0x6b, 0x6e, |
6614 | + 0xe3, 0x90, }, |
6615 | + { 0x90, 0xdd, 0xf7, 0x75, 0xa7, 0xa3, 0x99, 0x5e, 0x5b, 0x7d, 0x75, 0xc3, |
6616 | + 0x39, 0x6b, 0xa0, 0xe2, 0x44, 0x53, 0xb1, 0x9e, 0xc8, 0xf1, 0x77, 0x10, |
6617 | + 0x58, 0x06, 0x9a, }, |
6618 | + { 0x99, 0x52, 0xf0, 0x49, 0xa8, 0x8c, 0xec, 0xa6, 0x97, 0x32, 0x13, 0xb5, |
6619 | + 0xf7, 0xa3, 0x8e, 0xfb, 0x4b, 0x59, 0x31, 0x3d, 0x01, 0x59, 0x98, 0x5d, |
6620 | + 0x53, 0x03, 0x1a, 0x39, }, |
6621 | + { 0x9f, 0xe0, 0xc2, 0xe5, 0x5d, 0x93, 0xd6, 0x9b, 0x47, 0x8f, 0x9b, 0xe0, |
6622 | + 0x26, 0x35, 0x84, 0x20, 0x1d, 0xc5, 0x53, 0x10, 0x0f, 0x22, 0xb9, 0xb5, |
6623 | + 0xd4, 0x36, 0xb1, 0xac, 0x73, }, |
6624 | + { 0x30, 0x32, 0x20, 0x3b, 0x10, 0x28, 0xec, 0x1f, 0x4f, 0x9b, 0x47, 0x59, |
6625 | + 0xeb, 0x7b, 0xee, 0x45, 0xfb, 0x0c, 0x49, 0xd8, 0x3d, 0x69, 0xbd, 0x90, |
6626 | + 0x2c, 0xf0, 0x9e, 0x8d, 0xbf, 0xd5, }, |
6627 | + { 0x2a, 0x37, 0x73, 0x7f, 0xf9, 0x96, 0x19, 0xaa, 0x25, 0xd8, 0x13, 0x28, |
6628 | + 0x01, 0x29, 0x89, 0xdf, 0x6e, 0x0c, 0x9b, 0x43, 0x44, 0x51, 0xe9, 0x75, |
6629 | + 0x26, 0x0c, 0xb7, 0x87, 0x66, 0x0b, 0x5f, }, |
6630 | + { 0x23, 0xdf, 0x96, 0x68, 0x91, 0x86, 0xd0, 0x93, 0x55, 0x33, 0x24, 0xf6, |
6631 | + 0xba, 0x08, 0x75, 0x5b, 0x59, 0x11, 0x69, 0xb8, 0xb9, 0xe5, 0x2c, 0x77, |
6632 | + 0x02, 0xf6, 0x47, 0xee, 0x81, 0xdd, 0xb9, 0x06, }, |
6633 | + { 0x9d, }, |
6634 | + { 0x9d, 0x7d, }, |
6635 | + { 0xfd, 0xc3, 0xda, }, |
6636 | + { 0xe8, 0x82, 0xcd, 0x21, }, |
6637 | + { 0xc3, 0x1d, 0x42, 0x4c, 0x74, }, |
6638 | + { 0xe9, 0xda, 0xf1, 0xa2, 0xe5, 0x7c, }, |
6639 | + { 0x52, 0xb8, 0x6f, 0x81, 0x5c, 0x3a, 0x4c, }, |
6640 | + { 0x5b, 0x39, 0x26, 0xfc, 0x92, 0x5e, 0xe0, 0x49, }, |
6641 | + { 0x59, 0xe4, 0x7c, 0x93, 0x1c, 0xf9, 0x28, 0x93, 0xde, }, |
6642 | + { 0xde, 0xdf, 0xb2, 0x43, 0x61, 0x0b, 0x86, 0x16, 0x4c, 0x2e, }, |
6643 | + { 0x14, 0x8f, 0x75, 0x51, 0xaf, 0xb9, 0xee, 0x51, 0x5a, 0xae, 0x23, }, |
6644 | + { 0x43, 0x5f, 0x50, 0xd5, 0x70, 0xb0, 0x5b, 0x87, 0xf5, 0xd9, 0xb3, 0x6d, }, |
6645 | + { 0x66, 0x0a, 0x64, 0x93, 0x79, 0x71, 0x94, 0x40, 0xb7, 0x68, 0x2d, 0xd3, |
6646 | + 0x63, }, |
6647 | + { 0x15, 0x00, 0xc4, 0x0c, 0x7d, 0x1b, 0x10, 0xa9, 0x73, 0x1b, 0x90, 0x6f, |
6648 | + 0xe6, 0xa9, }, |
6649 | + { 0x34, 0x75, 0xf3, 0x86, 0x8f, 0x56, 0xcf, 0x2a, 0x0a, 0xf2, 0x62, 0x0a, |
6650 | + 0xf6, 0x0e, 0x20, }, |
6651 | + { 0xb1, 0xde, 0xc9, 0xf5, 0xdb, 0xf3, 0x2f, 0x4c, 0xd6, 0x41, 0x7d, 0x39, |
6652 | + 0x18, 0x3e, 0xc7, 0xc3, }, |
6653 | + { 0xc5, 0x89, 0xb2, 0xf8, 0xb8, 0xc0, 0xa3, 0xb9, 0x3b, 0x10, 0x6d, 0x7c, |
6654 | + 0x92, 0xfc, 0x7f, 0x34, 0x41, }, |
6655 | + { 0xc4, 0xd8, 0xef, 0xba, 0xef, 0xd2, 0xaa, 0xc5, 0x6c, 0x8e, 0x3e, 0xbb, |
6656 | + 0x12, 0xfc, 0x0f, 0x72, 0xbf, 0x0f, }, |
6657 | + { 0xdd, 0x91, 0xd1, 0x15, 0x9e, 0x7d, 0xf8, 0xc1, 0xb9, 0x14, 0x63, 0x96, |
6658 | + 0xb5, 0xcb, 0x83, 0x1d, 0x35, 0x1c, 0xec, }, |
6659 | + { 0xa9, 0xf8, 0x52, 0xc9, 0x67, 0x76, 0x2b, 0xad, 0xfb, 0xd8, 0x3a, 0xa6, |
6660 | + 0x74, 0x02, 0xae, 0xb8, 0x25, 0x2c, 0x63, 0x49, }, |
6661 | + { 0x77, 0x1f, 0x66, 0x70, 0xfd, 0x50, 0x29, 0xaa, 0xeb, 0xdc, 0xee, 0xba, |
6662 | + 0x75, 0x98, 0xdc, 0x93, 0x12, 0x3f, 0xdc, 0x7c, 0x38, }, |
6663 | + { 0xe2, 0xe1, 0x89, 0x5c, 0x37, 0x38, 0x6a, 0xa3, 0x40, 0xac, 0x3f, 0xb0, |
6664 | + 0xca, 0xfc, 0xa7, 0xf3, 0xea, 0xf9, 0x0f, 0x5d, 0x8e, 0x39, }, |
6665 | + { 0x0f, 0x67, 0xc8, 0x38, 0x01, 0xb1, 0xb7, 0xb8, 0xa2, 0xe7, 0x0a, 0x6d, |
6666 | + 0xd2, 0x63, 0x69, 0x9e, 0xcc, 0xf0, 0xf2, 0xbe, 0x9b, 0x98, 0xdd, }, |
6667 | + { 0x13, 0xe1, 0x36, 0x30, 0xfe, 0xc6, 0x01, 0x8a, 0xa1, 0x63, 0x96, 0x59, |
6668 | + 0xc2, 0xa9, 0x68, 0x3f, 0x58, 0xd4, 0x19, 0x0c, 0x40, 0xf3, 0xde, 0x02, }, |
6669 | + { 0xa3, 0x9e, 0xce, 0xda, 0x42, 0xee, 0x8c, 0x6c, 0x5a, 0x7d, 0xdc, 0x89, |
6670 | + 0x02, 0x77, 0xdd, 0xe7, 0x95, 0xbb, 0xff, 0x0d, 0xa4, 0xb5, 0x38, 0x1e, |
6671 | + 0xaf, }, |
6672 | + { 0x9a, 0xf6, 0xb5, 0x9a, 0x4f, 0xa9, 0x4f, 0x2c, 0x35, 0x3c, 0x24, 0xdc, |
6673 | + 0x97, 0x6f, 0xd9, 0xa1, 0x7d, 0x1a, 0x85, 0x0b, 0xf5, 0xda, 0x2e, 0xe7, |
6674 | + 0xb1, 0x1d, }, |
6675 | + { 0x84, 0x1e, 0x8e, 0x3d, 0x45, 0xa5, 0xf2, 0x27, 0xf3, 0x31, 0xfe, 0xb9, |
6676 | + 0xfb, 0xc5, 0x45, 0x99, 0x99, 0xdd, 0x93, 0x43, 0x02, 0xee, 0x58, 0xaf, |
6677 | + 0xee, 0x6a, 0xbe, }, |
6678 | + { 0x07, 0x2f, 0xc0, 0xa2, 0x04, 0xc4, 0xab, 0x7c, 0x26, 0xbb, 0xa8, 0xd8, |
6679 | + 0xe3, 0x1c, 0x75, 0x15, 0x64, 0x5d, 0x02, 0x6a, 0xf0, 0x86, 0xe9, 0xcd, |
6680 | + 0x5c, 0xef, 0xa3, 0x25, }, |
6681 | + { 0x2f, 0x3b, 0x1f, 0xb5, 0x91, 0x8f, 0x86, 0xe0, 0xdc, 0x31, 0x48, 0xb6, |
6682 | + 0xa1, 0x8c, 0xfd, 0x75, 0xbb, 0x7d, 0x3d, 0xc1, 0xf0, 0x10, 0x9a, 0xd8, |
6683 | + 0x4b, 0x0e, 0xe3, 0x94, 0x9f, }, |
6684 | + { 0x29, 0xbb, 0x8f, 0x6c, 0xd1, 0xf2, 0xb6, 0xaf, 0xe5, 0xe3, 0x2d, 0xdc, |
6685 | + 0x6f, 0xa4, 0x53, 0x88, 0xd8, 0xcf, 0x4d, 0x45, 0x42, 0x62, 0xdb, 0xdf, |
6686 | + 0xf8, 0x45, 0xc2, 0x13, 0xec, 0x35, }, |
6687 | + { 0x06, 0x3c, 0xe3, 0x2c, 0x15, 0xc6, 0x43, 0x03, 0x81, 0xfb, 0x08, 0x76, |
6688 | + 0x33, 0xcb, 0x02, 0xc1, 0xba, 0x33, 0xe5, 0xe0, 0xd1, 0x92, 0xa8, 0x46, |
6689 | + 0x28, 0x3f, 0x3e, 0x9d, 0x2c, 0x44, 0x54, }, |
6690 | + { 0xea, 0xbb, 0x96, 0xf8, 0xd1, 0x8b, 0x04, 0x11, 0x40, 0x78, 0x42, 0x02, |
6691 | + 0x19, 0xd1, 0xbc, 0x65, 0x92, 0xd3, 0xc3, 0xd6, 0xd9, 0x19, 0xe7, 0xc3, |
6692 | + 0x40, 0x97, 0xbd, 0xd4, 0xed, 0xfa, 0x5e, 0x28, }, |
6693 | + { 0x02, }, |
6694 | + { 0x52, 0xa8, }, |
6695 | + { 0x38, 0x25, 0x0d, }, |
6696 | + { 0xe3, 0x04, 0xd4, 0x92, }, |
6697 | + { 0x97, 0xdb, 0xf7, 0x81, 0xca, }, |
6698 | + { 0x8a, 0x56, 0x9d, 0x62, 0x56, 0xcc, }, |
6699 | + { 0xa1, 0x8e, 0x3c, 0x72, 0x8f, 0x63, 0x03, }, |
6700 | + { 0xf7, 0xf3, 0x39, 0x09, 0x0a, 0xa1, 0xbb, 0x23, }, |
6701 | + { 0x6b, 0x03, 0xc0, 0xe9, 0xd9, 0x83, 0x05, 0x22, 0x01, }, |
6702 | + { 0x1b, 0x4b, 0xf5, 0xd6, 0x4f, 0x05, 0x75, 0x91, 0x4c, 0x7f, }, |
6703 | + { 0x4c, 0x8c, 0x25, 0x20, 0x21, 0xcb, 0xc2, 0x4b, 0x3a, 0x5b, 0x8d, }, |
6704 | + { 0x56, 0xe2, 0x77, 0xa0, 0xb6, 0x9f, 0x81, 0xec, 0x83, 0x75, 0xc4, 0xf9, }, |
6705 | + { 0x71, 0x70, 0x0f, 0xad, 0x4d, 0x35, 0x81, 0x9d, 0x88, 0x69, 0xf9, 0xaa, |
6706 | + 0xd3, }, |
6707 | + { 0x50, 0x6e, 0x86, 0x6e, 0x43, 0xc0, 0xc2, 0x44, 0xc2, 0xe2, 0xa0, 0x1c, |
6708 | + 0xb7, 0x9a, }, |
6709 | + { 0xe4, 0x7e, 0x72, 0xc6, 0x12, 0x8e, 0x7c, 0xfc, 0xbd, 0xe2, 0x08, 0x31, |
6710 | + 0x3d, 0x47, 0x3d, }, |
6711 | + { 0x08, 0x97, 0x5b, 0x80, 0xae, 0xc4, 0x1d, 0x50, 0x77, 0xdf, 0x1f, 0xd0, |
6712 | + 0x24, 0xf0, 0x17, 0xc0, }, |
6713 | + { 0x01, 0xb6, 0x29, 0xf4, 0xaf, 0x78, 0x5f, 0xb6, 0x91, 0xdd, 0x76, 0x76, |
6714 | + 0xd2, 0xfd, 0x0c, 0x47, 0x40, }, |
6715 | + { 0xa1, 0xd8, 0x09, 0x97, 0x7a, 0xa6, 0xc8, 0x94, 0xf6, 0x91, 0x7b, 0xae, |
6716 | + 0x2b, 0x9f, 0x0d, 0x83, 0x48, 0xf7, }, |
6717 | + { 0x12, 0xd5, 0x53, 0x7d, 0x9a, 0xb0, 0xbe, 0xd9, 0xed, 0xe9, 0x9e, 0xee, |
6718 | + 0x61, 0x5b, 0x42, 0xf2, 0xc0, 0x73, 0xc0, }, |
6719 | + { 0xd5, 0x77, 0xd6, 0x5c, 0x6e, 0xa5, 0x69, 0x2b, 0x3b, 0x8c, 0xd6, 0x7d, |
6720 | + 0x1d, 0xbe, 0x2c, 0xa1, 0x02, 0x21, 0xcd, 0x29, }, |
6721 | + { 0xa4, 0x98, 0x80, 0xca, 0x22, 0xcf, 0x6a, 0xab, 0x5e, 0x40, 0x0d, 0x61, |
6722 | + 0x08, 0x21, 0xef, 0xc0, 0x6c, 0x52, 0xb4, 0xb0, 0x53, }, |
6723 | + { 0xbf, 0xaf, 0x8f, 0x3b, 0x7a, 0x97, 0x33, 0xe5, 0xca, 0x07, 0x37, 0xfd, |
6724 | + 0x15, 0xdf, 0xce, 0x26, 0x2a, 0xb1, 0xa7, 0x0b, 0xb3, 0xac, }, |
6725 | + { 0x16, 0x22, 0xe1, 0xbc, 0x99, 0x4e, 0x01, 0xf0, 0xfa, 0xff, 0x8f, 0xa5, |
6726 | + 0x0c, 0x61, 0xb0, 0xad, 0xcc, 0xb1, 0xe1, 0x21, 0x46, 0xfa, 0x2e, }, |
6727 | + { 0x11, 0x5b, 0x0b, 0x2b, 0xe6, 0x14, 0xc1, 0xd5, 0x4d, 0x71, 0x5e, 0x17, |
6728 | + 0xea, 0x23, 0xdd, 0x6c, 0xbd, 0x1d, 0xbe, 0x12, 0x1b, 0xee, 0x4c, 0x1a, }, |
6729 | + { 0x40, 0x88, 0x22, 0xf3, 0x20, 0x6c, 0xed, 0xe1, 0x36, 0x34, 0x62, 0x2c, |
6730 | + 0x98, 0x83, 0x52, 0xe2, 0x25, 0xee, 0xe9, 0xf5, 0xe1, 0x17, 0xf0, 0x5c, |
6731 | + 0xae, }, |
6732 | + { 0xc3, 0x76, 0x37, 0xde, 0x95, 0x8c, 0xca, 0x2b, 0x0c, 0x23, 0xe7, 0xb5, |
6733 | + 0x38, 0x70, 0x61, 0xcc, 0xff, 0xd3, 0x95, 0x7b, 0xf3, 0xff, 0x1f, 0x9d, |
6734 | + 0x59, 0x00, }, |
6735 | + { 0x0c, 0x19, 0x52, 0x05, 0x22, 0x53, 0xcb, 0x48, 0xd7, 0x10, 0x0e, 0x7e, |
6736 | + 0x14, 0x69, 0xb5, 0xa2, 0x92, 0x43, 0xa3, 0x9e, 0x4b, 0x8f, 0x51, 0x2c, |
6737 | + 0x5a, 0x2c, 0x3b, }, |
6738 | + { 0xe1, 0x9d, 0x70, 0x70, 0x28, 0xec, 0x86, 0x40, 0x55, 0x33, 0x56, 0xda, |
6739 | + 0x88, 0xca, 0xee, 0xc8, 0x6a, 0x20, 0xb1, 0xe5, 0x3d, 0x57, 0xf8, 0x3c, |
6740 | + 0x10, 0x07, 0x2a, 0xc4, }, |
6741 | + { 0x0b, 0xae, 0xf1, 0xc4, 0x79, 0xee, 0x1b, 0x3d, 0x27, 0x35, 0x8d, 0x14, |
6742 | + 0xd6, 0xae, 0x4e, 0x3c, 0xe9, 0x53, 0x50, 0xb5, 0xcc, 0x0c, 0xf7, 0xdf, |
6743 | + 0xee, 0xa1, 0x74, 0xd6, 0x71, }, |
6744 | + { 0xe6, 0xa4, 0xf4, 0x99, 0x98, 0xb9, 0x80, 0xea, 0x96, 0x7f, 0x4f, 0x33, |
6745 | + 0xcf, 0x74, 0x25, 0x6f, 0x17, 0x6c, 0xbf, 0xf5, 0x5c, 0x38, 0xd0, 0xff, |
6746 | + 0x96, 0xcb, 0x13, 0xf9, 0xdf, 0xfd, }, |
6747 | + { 0xbe, 0x92, 0xeb, 0xba, 0x44, 0x2c, 0x24, 0x74, 0xd4, 0x03, 0x27, 0x3c, |
6748 | + 0x5d, 0x5b, 0x03, 0x30, 0x87, 0x63, 0x69, 0xe0, 0xb8, 0x94, 0xf4, 0x44, |
6749 | + 0x7e, 0xad, 0xcd, 0x20, 0x12, 0x16, 0x79, }, |
6750 | + { 0x30, 0xf1, 0xc4, 0x8e, 0x05, 0x90, 0x2a, 0x97, 0x63, 0x94, 0x46, 0xff, |
6751 | + 0xce, 0xd8, 0x67, 0xa7, 0xac, 0x33, 0x8c, 0x95, 0xb7, 0xcd, 0xa3, 0x23, |
6752 | + 0x98, 0x9d, 0x76, 0x6c, 0x9d, 0xa8, 0xd6, 0x8a, }, |
6753 | + { 0xbe, }, |
6754 | + { 0x17, 0x6c, }, |
6755 | + { 0x1a, 0x42, 0x4f, }, |
6756 | + { 0xba, 0xaf, 0xb7, 0x65, }, |
6757 | + { 0xc2, 0x63, 0x43, 0x6a, 0xea, }, |
6758 | + { 0xe4, 0x4d, 0xad, 0xf2, 0x0b, 0x02, }, |
6759 | + { 0x04, 0xc7, 0xc4, 0x7f, 0xa9, 0x2b, 0xce, }, |
6760 | + { 0x66, 0xf6, 0x67, 0xcb, 0x03, 0x53, 0xc8, 0xf1, }, |
6761 | + { 0x56, 0xa3, 0x60, 0x78, 0xc9, 0x5f, 0x70, 0x1b, 0x5e, }, |
6762 | + { 0x99, 0xff, 0x81, 0x7c, 0x13, 0x3c, 0x29, 0x79, 0x4b, 0x65, }, |
6763 | + { 0x51, 0x10, 0x50, 0x93, 0x01, 0x93, 0xb7, 0x01, 0xc9, 0x18, 0xb7, }, |
6764 | + { 0x8e, 0x3c, 0x42, 0x1e, 0x5e, 0x7d, 0xc1, 0x50, 0x70, 0x1f, 0x00, 0x98, }, |
6765 | + { 0x5f, 0xd9, 0x9b, 0xc8, 0xd7, 0xb2, 0x72, 0x62, 0x1a, 0x1e, 0xba, 0x92, |
6766 | + 0xe9, }, |
6767 | + { 0x70, 0x2b, 0xba, 0xfe, 0xad, 0x5d, 0x96, 0x3f, 0x27, 0xc2, 0x41, 0x6d, |
6768 | + 0xc4, 0xb3, }, |
6769 | + { 0xae, 0xe0, 0xd5, 0xd4, 0xc7, 0xae, 0x15, 0x5e, 0xdc, 0xdd, 0x33, 0x60, |
6770 | + 0xd7, 0xd3, 0x5e, }, |
6771 | + { 0x79, 0x8e, 0xbc, 0x9e, 0x20, 0xb9, 0x19, 0x4b, 0x63, 0x80, 0xf3, 0x16, |
6772 | + 0xaf, 0x39, 0xbd, 0x92, }, |
6773 | + { 0xc2, 0x0e, 0x85, 0xa0, 0x0b, 0x9a, 0xb0, 0xec, 0xde, 0x38, 0xd3, 0x10, |
6774 | + 0xd9, 0xa7, 0x66, 0x27, 0xcf, }, |
6775 | + { 0x0e, 0x3b, 0x75, 0x80, 0x67, 0x14, 0x0c, 0x02, 0x90, 0xd6, 0xb3, 0x02, |
6776 | + 0x81, 0xf6, 0xa6, 0x87, 0xce, 0x58, }, |
6777 | + { 0x79, 0xb5, 0xe9, 0x5d, 0x52, 0x4d, 0xf7, 0x59, 0xf4, 0x2e, 0x27, 0xdd, |
6778 | + 0xb3, 0xed, 0x57, 0x5b, 0x82, 0xea, 0x6f, }, |
6779 | + { 0xa2, 0x97, 0xf5, 0x80, 0x02, 0x3d, 0xde, 0xa3, 0xf9, 0xf6, 0xab, 0xe3, |
6780 | + 0x57, 0x63, 0x7b, 0x9b, 0x10, 0x42, 0x6f, 0xf2, }, |
6781 | + { 0x12, 0x7a, 0xfc, 0xb7, 0x67, 0x06, 0x0c, 0x78, 0x1a, 0xfe, 0x88, 0x4f, |
6782 | + 0xc6, 0xac, 0x52, 0x96, 0x64, 0x28, 0x97, 0x84, 0x06, }, |
6783 | + { 0xc5, 0x04, 0x44, 0x6b, 0xb2, 0xa5, 0xa4, 0x66, 0xe1, 0x76, 0xa2, 0x51, |
6784 | + 0xf9, 0x59, 0x69, 0x97, 0x56, 0x0b, 0xbf, 0x50, 0xb3, 0x34, }, |
6785 | + { 0x21, 0x32, 0x6b, 0x42, 0xb5, 0xed, 0x71, 0x8d, 0xf7, 0x5a, 0x35, 0xe3, |
6786 | + 0x90, 0xe2, 0xee, 0xaa, 0x89, 0xf6, 0xc9, 0x9c, 0x4d, 0x73, 0xf4, }, |
6787 | + { 0x4c, 0xa6, 0x09, 0xf4, 0x48, 0xe7, 0x46, 0xbc, 0x49, 0xfc, 0xe5, 0xda, |
6788 | + 0xd1, 0x87, 0x13, 0x17, 0x4c, 0x59, 0x71, 0x26, 0x5b, 0x2c, 0x42, 0xb7, }, |
6789 | + { 0x13, 0x63, 0xf3, 0x40, 0x02, 0xe5, 0xa3, 0x3a, 0x5e, 0x8e, 0xf8, 0xb6, |
6790 | + 0x8a, 0x49, 0x60, 0x76, 0x34, 0x72, 0x94, 0x73, 0xf6, 0xd9, 0x21, 0x6a, |
6791 | + 0x26, }, |
6792 | + { 0xdf, 0x75, 0x16, 0x10, 0x1b, 0x5e, 0x81, 0xc3, 0xc8, 0xde, 0x34, 0x24, |
6793 | + 0xb0, 0x98, 0xeb, 0x1b, 0x8f, 0xa1, 0x9b, 0x05, 0xee, 0xa5, 0xe9, 0x35, |
6794 | + 0xf4, 0x1d, }, |
6795 | + { 0xcd, 0x21, 0x93, 0x6e, 0x5b, 0xa0, 0x26, 0x2b, 0x21, 0x0e, 0xa0, 0xb9, |
6796 | + 0x1c, 0xb5, 0xbb, 0xb8, 0xf8, 0x1e, 0xff, 0x5c, 0xa8, 0xf9, 0x39, 0x46, |
6797 | + 0x4e, 0x29, 0x26, }, |
6798 | + { 0x73, 0x7f, 0x0e, 0x3b, 0x0b, 0x5c, 0xf9, 0x60, 0xaa, 0x88, 0xa1, 0x09, |
6799 | + 0xb1, 0x5d, 0x38, 0x7b, 0x86, 0x8f, 0x13, 0x7a, 0x8d, 0x72, 0x7a, 0x98, |
6800 | + 0x1a, 0x5b, 0xff, 0xc9, }, |
6801 | + { 0xd3, 0x3c, 0x61, 0x71, 0x44, 0x7e, 0x31, 0x74, 0x98, 0x9d, 0x9a, 0xd2, |
6802 | + 0x27, 0xf3, 0x46, 0x43, 0x42, 0x51, 0xd0, 0x5f, 0xe9, 0x1c, 0x5c, 0x69, |
6803 | + 0xbf, 0xf6, 0xbe, 0x3c, 0x40, }, |
6804 | + { 0x31, 0x99, 0x31, 0x9f, 0xaa, 0x43, 0x2e, 0x77, 0x3e, 0x74, 0x26, 0x31, |
6805 | + 0x5e, 0x61, 0xf1, 0x87, 0xe2, 0xeb, 0x9b, 0xcd, 0xd0, 0x3a, 0xee, 0x20, |
6806 | + 0x7e, 0x10, 0x0a, 0x0b, 0x7e, 0xfa, }, |
6807 | + { 0xa4, 0x27, 0x80, 0x67, 0x81, 0x2a, 0xa7, 0x62, 0xf7, 0x6e, 0xda, 0xd4, |
6808 | + 0x5c, 0x39, 0x74, 0xad, 0x7e, 0xbe, 0xad, 0xa5, 0x84, 0x7f, 0xa9, 0x30, |
6809 | + 0x5d, 0xdb, 0xe2, 0x05, 0x43, 0xf7, 0x1b, }, |
6810 | + { 0x0b, 0x37, 0xd8, 0x02, 0xe1, 0x83, 0xd6, 0x80, 0xf2, 0x35, 0xc2, 0xb0, |
6811 | + 0x37, 0xef, 0xef, 0x5e, 0x43, 0x93, 0xf0, 0x49, 0x45, 0x0a, 0xef, 0xb5, |
6812 | + 0x76, 0x70, 0x12, 0x44, 0xc4, 0xdb, 0xf5, 0x7a, }, |
6813 | + { 0x1f, }, |
6814 | + { 0x82, 0x60, }, |
6815 | + { 0xcc, 0xe3, 0x08, }, |
6816 | + { 0x56, 0x17, 0xe4, 0x59, }, |
6817 | + { 0xe2, 0xd7, 0x9e, 0xc4, 0x4c, }, |
6818 | + { 0xb2, 0xad, 0xd3, 0x78, 0x58, 0x5a, }, |
6819 | + { 0xce, 0x43, 0xb4, 0x02, 0x96, 0xab, 0x3c, }, |
6820 | + { 0xe6, 0x05, 0x1a, 0x73, 0x22, 0x32, 0xbb, 0x77, }, |
6821 | + { 0x23, 0xe7, 0xda, 0xfe, 0x2c, 0xef, 0x8c, 0x22, 0xec, }, |
6822 | + { 0xe9, 0x8e, 0x55, 0x38, 0xd1, 0xd7, 0x35, 0x23, 0x98, 0xc7, }, |
6823 | + { 0xb5, 0x81, 0x1a, 0xe5, 0xb5, 0xa5, 0xd9, 0x4d, 0xca, 0x41, 0xe7, }, |
6824 | + { 0x41, 0x16, 0x16, 0x95, 0x8d, 0x9e, 0x0c, 0xea, 0x8c, 0x71, 0x9a, 0xc1, }, |
6825 | + { 0x7c, 0x33, 0xc0, 0xa4, 0x00, 0x62, 0xea, 0x60, 0x67, 0xe4, 0x20, 0xbc, |
6826 | + 0x5b, }, |
6827 | + { 0xdb, 0xb1, 0xdc, 0xfd, 0x08, 0xc0, 0xde, 0x82, 0xd1, 0xde, 0x38, 0xc0, |
6828 | + 0x90, 0x48, }, |
6829 | + { 0x37, 0x18, 0x2e, 0x0d, 0x61, 0xaa, 0x61, 0xd7, 0x86, 0x20, 0x16, 0x60, |
6830 | + 0x04, 0xd9, 0xd5, }, |
6831 | + { 0xb0, 0xcf, 0x2c, 0x4c, 0x5e, 0x5b, 0x4f, 0x2a, 0x23, 0x25, 0x58, 0x47, |
6832 | + 0xe5, 0x31, 0x06, 0x70, }, |
6833 | + { 0x91, 0xa0, 0xa3, 0x86, 0x4e, 0xe0, 0x72, 0x38, 0x06, 0x67, 0x59, 0x5c, |
6834 | + 0x70, 0x25, 0xdb, 0x33, 0x27, }, |
6835 | + { 0x44, 0x58, 0x66, 0xb8, 0x58, 0xc7, 0x13, 0xed, 0x4c, 0xc0, 0xf4, 0x9a, |
6836 | + 0x1e, 0x67, 0x75, 0x33, 0xb6, 0xb8, }, |
6837 | + { 0x7f, 0x98, 0x4a, 0x8e, 0x50, 0xa2, 0x5c, 0xcd, 0x59, 0xde, 0x72, 0xb3, |
6838 | + 0x9d, 0xc3, 0x09, 0x8a, 0xab, 0x56, 0xf1, }, |
6839 | + { 0x80, 0x96, 0x49, 0x1a, 0x59, 0xa2, 0xc5, 0xd5, 0xa7, 0x20, 0x8a, 0xb7, |
6840 | + 0x27, 0x62, 0x84, 0x43, 0xc6, 0xe1, 0x1b, 0x5d, }, |
6841 | + { 0x6b, 0xb7, 0x2b, 0x26, 0x62, 0x14, 0x70, 0x19, 0x3d, 0x4d, 0xac, 0xac, |
6842 | + 0x63, 0x58, 0x5e, 0x94, 0xb5, 0xb7, 0xe8, 0xe8, 0xa2, }, |
6843 | + { 0x20, 0xa8, 0xc0, 0xfd, 0x63, 0x3d, 0x6e, 0x98, 0xcf, 0x0c, 0x49, 0x98, |
6844 | + 0xe4, 0x5a, 0xfe, 0x8c, 0xaa, 0x70, 0x82, 0x1c, 0x7b, 0x74, }, |
6845 | + { 0xc8, 0xe8, 0xdd, 0xdf, 0x69, 0x30, 0x01, 0xc2, 0x0f, 0x7e, 0x2f, 0x11, |
6846 | + 0xcc, 0x3e, 0x17, 0xa5, 0x69, 0x40, 0x3f, 0x0e, 0x79, 0x7f, 0xcf, }, |
6847 | + { 0xdb, 0x61, 0xc0, 0xe2, 0x2e, 0x49, 0x07, 0x31, 0x1d, 0x91, 0x42, 0x8a, |
6848 | + 0xfc, 0x5e, 0xd3, 0xf8, 0x56, 0x1f, 0x2b, 0x73, 0xfd, 0x9f, 0xb2, 0x8e, }, |
6849 | + { 0x0c, 0x89, 0x55, 0x0c, 0x1f, 0x59, 0x2c, 0x9d, 0x1b, 0x29, 0x1d, 0x41, |
6850 | + 0x1d, 0xe6, 0x47, 0x8f, 0x8c, 0x2b, 0xea, 0x8f, 0xf0, 0xff, 0x21, 0x70, |
6851 | + 0x88, }, |
6852 | + { 0x12, 0x18, 0x95, 0xa6, 0x59, 0xb1, 0x31, 0x24, 0x45, 0x67, 0x55, 0xa4, |
6853 | + 0x1a, 0x2d, 0x48, 0x67, 0x1b, 0x43, 0x88, 0x2d, 0x8e, 0xa0, 0x70, 0xb3, |
6854 | + 0xc6, 0xbb, }, |
6855 | + { 0xe7, 0xb1, 0x1d, 0xb2, 0x76, 0x4d, 0x68, 0x68, 0x68, 0x23, 0x02, 0x55, |
6856 | + 0x3a, 0xe2, 0xe5, 0xd5, 0x4b, 0x43, 0xf9, 0x34, 0x77, 0x5c, 0xa1, 0xf5, |
6857 | + 0x55, 0xfd, 0x4f, }, |
6858 | + { 0x8c, 0x87, 0x5a, 0x08, 0x3a, 0x73, 0xad, 0x61, 0xe1, 0xe7, 0x99, 0x7e, |
6859 | + 0xf0, 0x5d, 0xe9, 0x5d, 0x16, 0x43, 0x80, 0x2f, 0xd0, 0x66, 0x34, 0xe2, |
6860 | + 0x42, 0x64, 0x3b, 0x1a, }, |
6861 | + { 0x39, 0xc1, 0x99, 0xcf, 0x22, 0xbf, 0x16, 0x8f, 0x9f, 0x80, 0x7f, 0x95, |
6862 | + 0x0a, 0x05, 0x67, 0x27, 0xe7, 0x15, 0xdf, 0x9d, 0xb2, 0xfe, 0x1c, 0xb5, |
6863 | + 0x1d, 0x60, 0x8f, 0x8a, 0x1d, }, |
6864 | + { 0x9b, 0x6e, 0x08, 0x09, 0x06, 0x73, 0xab, 0x68, 0x02, 0x62, 0x1a, 0xe4, |
6865 | + 0xd4, 0xdf, 0xc7, 0x02, 0x4c, 0x6a, 0x5f, 0xfd, 0x23, 0xac, 0xae, 0x6d, |
6866 | + 0x43, 0xa4, 0x7a, 0x50, 0x60, 0x3c, }, |
6867 | + { 0x1d, 0xb4, 0xc6, 0xe1, 0xb1, 0x4b, 0xe3, 0xf2, 0xe2, 0x1a, 0x73, 0x1b, |
6868 | + 0xa0, 0x92, 0xa7, 0xf5, 0xff, 0x8f, 0x8b, 0x5d, 0xdf, 0xa8, 0x04, 0xb3, |
6869 | + 0xb0, 0xf7, 0xcc, 0x12, 0xfa, 0x35, 0x46, }, |
6870 | + { 0x49, 0x45, 0x97, 0x11, 0x0f, 0x1c, 0x60, 0x8e, 0xe8, 0x47, 0x30, 0xcf, |
6871 | + 0x60, 0xa8, 0x71, 0xc5, 0x1b, 0xe9, 0x39, 0x4d, 0x49, 0xb6, 0x12, 0x1f, |
6872 | + 0x24, 0xab, 0x37, 0xff, 0x83, 0xc2, 0xe1, 0x3a, }, |
6873 | + { 0x60, }, |
6874 | + { 0x24, 0x26, }, |
6875 | + { 0x47, 0xeb, 0xc9, }, |
6876 | + { 0x4a, 0xd0, 0xbc, 0xf0, }, |
6877 | + { 0x8e, 0x2b, 0xc9, 0x85, 0x3c, }, |
6878 | + { 0xa2, 0x07, 0x15, 0xb8, 0x12, 0x74, }, |
6879 | + { 0x0f, 0xdb, 0x5b, 0x33, 0x69, 0xfe, 0x4b, }, |
6880 | + { 0xa2, 0x86, 0x54, 0xf4, 0xfd, 0xb2, 0xd4, 0xe6, }, |
6881 | + { 0xbb, 0x84, 0x78, 0x49, 0x27, 0x8e, 0x61, 0xda, 0x60, }, |
6882 | + { 0x04, 0xc3, 0xcd, 0xaa, 0x8f, 0xa7, 0x03, 0xc9, 0xf9, 0xb6, }, |
6883 | + { 0xf8, 0x27, 0x1d, 0x61, 0xdc, 0x21, 0x42, 0xdd, 0xad, 0x92, 0x40, }, |
6884 | + { 0x12, 0x87, 0xdf, 0xc2, 0x41, 0x45, 0x5a, 0x36, 0x48, 0x5b, 0x51, 0x2b, }, |
6885 | + { 0xbb, 0x37, 0x5d, 0x1f, 0xf1, 0x68, 0x7a, 0xc4, 0xa5, 0xd2, 0xa4, 0x91, |
6886 | + 0x8d, }, |
6887 | + { 0x5b, 0x27, 0xd1, 0x04, 0x54, 0x52, 0x9f, 0xa3, 0x47, 0x86, 0x33, 0x33, |
6888 | + 0xbf, 0xa0, }, |
6889 | + { 0xcf, 0x04, 0xea, 0xf8, 0x03, 0x2a, 0x43, 0xff, 0xa6, 0x68, 0x21, 0x4c, |
6890 | + 0xd5, 0x4b, 0xed, }, |
6891 | + { 0xaf, 0xb8, 0xbc, 0x63, 0x0f, 0x18, 0x4d, 0xe2, 0x7a, 0xdd, 0x46, 0x44, |
6892 | + 0xc8, 0x24, 0x0a, 0xb7, }, |
6893 | + { 0x3e, 0xdc, 0x36, 0xe4, 0x89, 0xb1, 0xfa, 0xc6, 0x40, 0x93, 0x2e, 0x75, |
6894 | + 0xb2, 0x15, 0xd1, 0xb1, 0x10, }, |
6895 | + { 0x6c, 0xd8, 0x20, 0x3b, 0x82, 0x79, 0xf9, 0xc8, 0xbc, 0x9d, 0xe0, 0x35, |
6896 | + 0xbe, 0x1b, 0x49, 0x1a, 0xbc, 0x3a, }, |
6897 | + { 0x78, 0x65, 0x2c, 0xbe, 0x35, 0x67, 0xdc, 0x78, 0xd4, 0x41, 0xf6, 0xc9, |
6898 | + 0xde, 0xde, 0x1f, 0x18, 0x13, 0x31, 0x11, }, |
6899 | + { 0x8a, 0x7f, 0xb1, 0x33, 0x8f, 0x0c, 0x3c, 0x0a, 0x06, 0x61, 0xf0, 0x47, |
6900 | + 0x29, 0x1b, 0x29, 0xbc, 0x1c, 0x47, 0xef, 0x7a, }, |
6901 | + { 0x65, 0x91, 0xf1, 0xe6, 0xb3, 0x96, 0xd3, 0x8c, 0xc2, 0x4a, 0x59, 0x35, |
6902 | + 0x72, 0x8e, 0x0b, 0x9a, 0x87, 0xca, 0x34, 0x7b, 0x63, }, |
6903 | + { 0x5f, 0x08, 0x87, 0x80, 0x56, 0x25, 0x89, 0x77, 0x61, 0x8c, 0x64, 0xa1, |
6904 | + 0x59, 0x6d, 0x59, 0x62, 0xe8, 0x4a, 0xc8, 0x58, 0x99, 0xd1, }, |
6905 | + { 0x23, 0x87, 0x1d, 0xed, 0x6f, 0xf2, 0x91, 0x90, 0xe2, 0xfe, 0x43, 0x21, |
6906 | + 0xaf, 0x97, 0xc6, 0xbc, 0xd7, 0x15, 0xc7, 0x2d, 0x08, 0x77, 0x91, }, |
6907 | + { 0x90, 0x47, 0x9a, 0x9e, 0x3a, 0xdf, 0xf3, 0xc9, 0x4c, 0x1e, 0xa7, 0xd4, |
6908 | + 0x6a, 0x32, 0x90, 0xfe, 0xb7, 0xb6, 0x7b, 0xfa, 0x96, 0x61, 0xfb, 0xa4, }, |
6909 | + { 0xb1, 0x67, 0x60, 0x45, 0xb0, 0x96, 0xc5, 0x15, 0x9f, 0x4d, 0x26, 0xd7, |
6910 | + 0x9d, 0xf1, 0xf5, 0x6d, 0x21, 0x00, 0x94, 0x31, 0x64, 0x94, 0xd3, 0xa7, |
6911 | + 0xd3, }, |
6912 | + { 0x02, 0x3e, 0xaf, 0xf3, 0x79, 0x73, 0xa5, 0xf5, 0xcc, 0x7a, 0x7f, 0xfb, |
6913 | + 0x79, 0x2b, 0x85, 0x8c, 0x88, 0x72, 0x06, 0xbe, 0xfe, 0xaf, 0xc1, 0x16, |
6914 | + 0xa6, 0xd6, }, |
6915 | + { 0x2a, 0xb0, 0x1a, 0xe5, 0xaa, 0x6e, 0xb3, 0xae, 0x53, 0x85, 0x33, 0x80, |
6916 | + 0x75, 0xae, 0x30, 0xe6, 0xb8, 0x72, 0x42, 0xf6, 0x25, 0x4f, 0x38, 0x88, |
6917 | + 0x55, 0xd1, 0xa9, }, |
6918 | + { 0x90, 0xd8, 0x0c, 0xc0, 0x93, 0x4b, 0x4f, 0x9e, 0x65, 0x6c, 0xa1, 0x54, |
6919 | + 0xa6, 0xf6, 0x6e, 0xca, 0xd2, 0xbb, 0x7e, 0x6a, 0x1c, 0xd3, 0xce, 0x46, |
6920 | + 0xef, 0xb0, 0x00, 0x8d, }, |
6921 | + { 0xed, 0x9c, 0x49, 0xcd, 0xc2, 0xde, 0x38, 0x0e, 0xe9, 0x98, 0x6c, 0xc8, |
6922 | + 0x90, 0x9e, 0x3c, 0xd4, 0xd3, 0xeb, 0x88, 0x32, 0xc7, 0x28, 0xe3, 0x94, |
6923 | + 0x1c, 0x9f, 0x8b, 0xf3, 0xcb, }, |
6924 | + { 0xac, 0xe7, 0x92, 0x16, 0xb4, 0x14, 0xa0, 0xe4, 0x04, 0x79, 0xa2, 0xf4, |
6925 | + 0x31, 0xe6, 0x0c, 0x26, 0xdc, 0xbf, 0x2f, 0x69, 0x1b, 0x55, 0x94, 0x67, |
6926 | + 0xda, 0x0c, 0xd7, 0x32, 0x1f, 0xef, }, |
6927 | + { 0x68, 0x63, 0x85, 0x57, 0x95, 0x9e, 0x42, 0x27, 0x41, 0x43, 0x42, 0x02, |
6928 | + 0xa5, 0x78, 0xa7, 0xc6, 0x43, 0xc1, 0x6a, 0xba, 0x70, 0x80, 0xcd, 0x04, |
6929 | + 0xb6, 0x78, 0x76, 0x29, 0xf3, 0xe8, 0xa0, }, |
6930 | + { 0xe6, 0xac, 0x8d, 0x9d, 0xf0, 0xc0, 0xf7, 0xf7, 0xe3, 0x3e, 0x4e, 0x28, |
6931 | + 0x0f, 0x59, 0xb2, 0x67, 0x9e, 0x84, 0x34, 0x42, 0x96, 0x30, 0x2b, 0xca, |
6932 | + 0x49, 0xb6, 0xc5, 0x9a, 0x84, 0x59, 0xa7, 0x81, }, |
6933 | + { 0x7e, }, |
6934 | + { 0x1e, 0x21, }, |
6935 | + { 0x26, 0xd3, 0xdd, }, |
6936 | + { 0x2c, 0xd4, 0xb3, 0x3d, }, |
6937 | + { 0x86, 0x7b, 0x76, 0x3c, 0xf0, }, |
6938 | + { 0x12, 0xc3, 0x70, 0x1d, 0x55, 0x18, }, |
6939 | + { 0x96, 0xc2, 0xbd, 0x61, 0x55, 0xf4, 0x24, }, |
6940 | + { 0x20, 0x51, 0xf7, 0x86, 0x58, 0x8f, 0x07, 0x2a, }, |
6941 | + { 0x93, 0x15, 0xa8, 0x1d, 0xda, 0x97, 0xee, 0x0e, 0x6c, }, |
6942 | + { 0x39, 0x93, 0xdf, 0xd5, 0x0e, 0xca, 0xdc, 0x7a, 0x92, 0xce, }, |
6943 | + { 0x60, 0xd5, 0xfd, 0xf5, 0x1b, 0x26, 0x82, 0x26, 0x73, 0x02, 0xbc, }, |
6944 | + { 0x98, 0xf2, 0x34, 0xe1, 0xf5, 0xfb, 0x00, 0xac, 0x10, 0x4a, 0x38, 0x9f, }, |
6945 | + { 0xda, 0x3a, 0x92, 0x8a, 0xd0, 0xcd, 0x12, 0xcd, 0x15, 0xbb, 0xab, 0x77, |
6946 | + 0x66, }, |
6947 | + { 0xa2, 0x92, 0x1a, 0xe5, 0xca, 0x0c, 0x30, 0x75, 0xeb, 0xaf, 0x00, 0x31, |
6948 | + 0x55, 0x66, }, |
6949 | + { 0x06, 0xea, 0xfd, 0x3e, 0x86, 0x38, 0x62, 0x4e, 0xa9, 0x12, 0xa4, 0x12, |
6950 | + 0x43, 0xbf, 0xa1, }, |
6951 | + { 0xe4, 0x71, 0x7b, 0x94, 0xdb, 0xa0, 0xd2, 0xff, 0x9b, 0xeb, 0xad, 0x8e, |
6952 | + 0x95, 0x8a, 0xc5, 0xed, }, |
6953 | + { 0x25, 0x5a, 0x77, 0x71, 0x41, 0x0e, 0x7a, 0xe9, 0xed, 0x0c, 0x10, 0xef, |
6954 | + 0xf6, 0x2b, 0x3a, 0xba, 0x60, }, |
6955 | + { 0xee, 0xe2, 0xa3, 0x67, 0x64, 0x1d, 0xc6, 0x04, 0xc4, 0xe1, 0x68, 0xd2, |
6956 | + 0x6e, 0xd2, 0x91, 0x75, 0x53, 0x07, }, |
6957 | + { 0xe0, 0xf6, 0x4d, 0x8f, 0x68, 0xfc, 0x06, 0x7e, 0x18, 0x79, 0x7f, 0x2b, |
6958 | + 0x6d, 0xef, 0x46, 0x7f, 0xab, 0xb2, 0xad, }, |
6959 | + { 0x3d, 0x35, 0x88, 0x9f, 0x2e, 0xcf, 0x96, 0x45, 0x07, 0x60, 0x71, 0x94, |
6960 | + 0x00, 0x8d, 0xbf, 0xf4, 0xef, 0x46, 0x2e, 0x3c, }, |
6961 | + { 0x43, 0xcf, 0x98, 0xf7, 0x2d, 0xf4, 0x17, 0xe7, 0x8c, 0x05, 0x2d, 0x9b, |
6962 | + 0x24, 0xfb, 0x4d, 0xea, 0x4a, 0xec, 0x01, 0x25, 0x29, }, |
6963 | + { 0x8e, 0x73, 0x9a, 0x78, 0x11, 0xfe, 0x48, 0xa0, 0x3b, 0x1a, 0x26, 0xdf, |
6964 | + 0x25, 0xe9, 0x59, 0x1c, 0x70, 0x07, 0x9f, 0xdc, 0xa0, 0xa6, }, |
6965 | + { 0xe8, 0x47, 0x71, 0xc7, 0x3e, 0xdf, 0xb5, 0x13, 0xb9, 0x85, 0x13, 0xa8, |
6966 | + 0x54, 0x47, 0x6e, 0x59, 0x96, 0x09, 0x13, 0x5f, 0x82, 0x16, 0x0b, }, |
6967 | + { 0xfb, 0xc0, 0x8c, 0x03, 0x21, 0xb3, 0xc4, 0xb5, 0x43, 0x32, 0x6c, 0xea, |
6968 | + 0x7f, 0xa8, 0x43, 0x91, 0xe8, 0x4e, 0x3f, 0xbf, 0x45, 0x58, 0x6a, 0xa3, }, |
6969 | + { 0x55, 0xf8, 0xf3, 0x00, 0x76, 0x09, 0xef, 0x69, 0x5d, 0xd2, 0x8a, 0xf2, |
6970 | + 0x65, 0xc3, 0xcb, 0x9b, 0x43, 0xfd, 0xb1, 0x7e, 0x7f, 0xa1, 0x94, 0xb0, |
6971 | + 0xd7, }, |
6972 | + { 0xaa, 0x13, 0xc1, 0x51, 0x40, 0x6d, 0x8d, 0x4c, 0x0a, 0x95, 0x64, 0x7b, |
6973 | + 0xd1, 0x96, 0xb6, 0x56, 0xb4, 0x5b, 0xcf, 0xd6, 0xd9, 0x15, 0x97, 0xdd, |
6974 | + 0xb6, 0xef, }, |
6975 | + { 0xaf, 0xb7, 0x36, 0xb0, 0x04, 0xdb, 0xd7, 0x9c, 0x9a, 0x44, 0xc4, 0xf6, |
6976 | + 0x1f, 0x12, 0x21, 0x2d, 0x59, 0x30, 0x54, 0xab, 0x27, 0x61, 0xa3, 0x57, |
6977 | + 0xef, 0xf8, 0x53, }, |
6978 | + { 0x97, 0x34, 0x45, 0x3e, 0xce, 0x7c, 0x35, 0xa2, 0xda, 0x9f, 0x4b, 0x46, |
6979 | + 0x6c, 0x11, 0x67, 0xff, 0x2f, 0x76, 0x58, 0x15, 0x71, 0xfa, 0x44, 0x89, |
6980 | + 0x89, 0xfd, 0xf7, 0x99, }, |
6981 | + { 0x1f, 0xb1, 0x62, 0xeb, 0x83, 0xc5, 0x9c, 0x89, 0xf9, 0x2c, 0xd2, 0x03, |
6982 | + 0x61, 0xbc, 0xbb, 0xa5, 0x74, 0x0e, 0x9b, 0x7e, 0x82, 0x3e, 0x70, 0x0a, |
6983 | + 0xa9, 0x8f, 0x2b, 0x59, 0xfb, }, |
6984 | + { 0xf8, 0xca, 0x5e, 0x3a, 0x4f, 0x9e, 0x10, 0x69, 0x10, 0xd5, 0x4c, 0xeb, |
6985 | + 0x1a, 0x0f, 0x3c, 0x6a, 0x98, 0xf5, 0xb0, 0x97, 0x5b, 0x37, 0x2f, 0x0d, |
6986 | + 0xbd, 0x42, 0x4b, 0x69, 0xa1, 0x82, }, |
6987 | + { 0x12, 0x8c, 0x6d, 0x52, 0x08, 0xef, 0x74, 0xb2, 0xe6, 0xaa, 0xd3, 0xb0, |
6988 | + 0x26, 0xb0, 0xd9, 0x94, 0xb6, 0x11, 0x45, 0x0e, 0x36, 0x71, 0x14, 0x2d, |
6989 | + 0x41, 0x8c, 0x21, 0x53, 0x31, 0xe9, 0x68, }, |
6990 | + { 0xee, 0xea, 0x0d, 0x89, 0x47, 0x7e, 0x72, 0xd1, 0xd8, 0xce, 0x58, 0x4c, |
6991 | + 0x94, 0x1f, 0x0d, 0x51, 0x08, 0xa3, 0xb6, 0x3d, 0xe7, 0x82, 0x46, 0x92, |
6992 | + 0xd6, 0x98, 0x6b, 0x07, 0x10, 0x65, 0x52, 0x65, }, |
6993 | +}; |
6994 | + |
6995 | +bool __init blake2s_selftest(void) |
6996 | +{ |
6997 | + u8 key[BLAKE2S_KEY_SIZE]; |
6998 | + u8 buf[ARRAY_SIZE(blake2s_testvecs)]; |
6999 | + u8 hash[BLAKE2S_HASH_SIZE]; |
7000 | + struct blake2s_state state; |
7001 | + bool success = true; |
7002 | + int i, l; |
7003 | + |
7004 | + key[0] = key[1] = 1; |
7005 | + for (i = 2; i < sizeof(key); ++i) |
7006 | + key[i] = key[i - 2] + key[i - 1]; |
7007 | + |
7008 | + for (i = 0; i < sizeof(buf); ++i) |
7009 | + buf[i] = (u8)i; |
7010 | + |
7011 | + for (i = l = 0; i < ARRAY_SIZE(blake2s_testvecs); l = (l + 37) % ++i) { |
7012 | + int outlen = 1 + i % BLAKE2S_HASH_SIZE; |
7013 | + int keylen = (13 * i) % (BLAKE2S_KEY_SIZE + 1); |
7014 | + |
7015 | + blake2s(hash, buf, key + BLAKE2S_KEY_SIZE - keylen, outlen, i, |
7016 | + keylen); |
7017 | + if (memcmp(hash, blake2s_testvecs[i], outlen)) { |
7018 | + pr_err("blake2s self-test %d: FAIL\n", i + 1); |
7019 | + success = false; |
7020 | + } |
7021 | + |
7022 | + if (!keylen) |
7023 | + blake2s_init(&state, outlen); |
7024 | + else |
7025 | + blake2s_init_key(&state, outlen, |
7026 | + key + BLAKE2S_KEY_SIZE - keylen, |
7027 | + keylen); |
7028 | + |
7029 | + blake2s_update(&state, buf, l); |
7030 | + blake2s_update(&state, buf + l, i - l); |
7031 | + blake2s_final(&state, hash); |
7032 | + if (memcmp(hash, blake2s_testvecs[i], outlen)) { |
7033 | + pr_err("blake2s init/update/final self-test %d: FAIL\n", |
7034 | + i + 1); |
7035 | + success = false; |
7036 | + } |
7037 | + } |
7038 | + |
7039 | + return success; |
7040 | +} |
7041 | diff --git a/lib/crypto/blake2s.c b/lib/crypto/blake2s.c |
7042 | new file mode 100644 |
7043 | index 0000000000000..536fce87555b3 |
7044 | --- /dev/null |
7045 | +++ b/lib/crypto/blake2s.c |
7046 | @@ -0,0 +1,78 @@ |
7047 | +// SPDX-License-Identifier: GPL-2.0 OR MIT |
7048 | +/* |
7049 | + * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
7050 | + * |
7051 | + * This is an implementation of the BLAKE2s hash and PRF functions. |
7052 | + * |
7053 | + * Information: https://blake2.net/ |
7054 | + * |
7055 | + */ |
7056 | + |
7057 | +#include <crypto/internal/blake2s.h> |
7058 | +#include <linux/types.h> |
7059 | +#include <linux/string.h> |
7060 | +#include <linux/kernel.h> |
7061 | +#include <linux/module.h> |
7062 | +#include <linux/init.h> |
7063 | +#include <linux/bug.h> |
7064 | +#include <asm/unaligned.h> |
7065 | + |
7066 | +bool blake2s_selftest(void); |
7067 | + |
7068 | +void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen) |
7069 | +{ |
7070 | + const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen; |
7071 | + |
7072 | + if (unlikely(!inlen)) |
7073 | + return; |
7074 | + if (inlen > fill) { |
7075 | + memcpy(state->buf + state->buflen, in, fill); |
7076 | + blake2s_compress_generic(state, state->buf, 1, |
7077 | + BLAKE2S_BLOCK_SIZE); |
7078 | + state->buflen = 0; |
7079 | + in += fill; |
7080 | + inlen -= fill; |
7081 | + } |
7082 | + if (inlen > BLAKE2S_BLOCK_SIZE) { |
7083 | + const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE); |
7084 | + /* Hash one less (full) block than strictly possible */ |
7085 | + blake2s_compress_generic(state, in, nblocks - 1, |
7086 | + BLAKE2S_BLOCK_SIZE); |
7087 | + in += BLAKE2S_BLOCK_SIZE * (nblocks - 1); |
7088 | + inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1); |
7089 | + } |
7090 | + memcpy(state->buf + state->buflen, in, inlen); |
7091 | + state->buflen += inlen; |
7092 | +} |
7093 | +EXPORT_SYMBOL(blake2s_update); |
7094 | + |
7095 | +void blake2s_final(struct blake2s_state *state, u8 *out) |
7096 | +{ |
7097 | + WARN_ON(IS_ENABLED(DEBUG) && !out); |
7098 | + blake2s_set_lastblock(state); |
7099 | + memset(state->buf + state->buflen, 0, |
7100 | + BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */ |
7101 | + blake2s_compress_generic(state, state->buf, 1, state->buflen); |
7102 | + cpu_to_le32_array(state->h, ARRAY_SIZE(state->h)); |
7103 | + memcpy(out, state->h, state->outlen); |
7104 | + memzero_explicit(state, sizeof(*state)); |
7105 | +} |
7106 | +EXPORT_SYMBOL(blake2s_final); |
7107 | + |
7108 | +static int __init mod_init(void) |
7109 | +{ |
7110 | + if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS) && |
7111 | + WARN_ON(!blake2s_selftest())) |
7112 | + return -ENODEV; |
7113 | + return 0; |
7114 | +} |
7115 | + |
7116 | +static void __exit mod_exit(void) |
7117 | +{ |
7118 | +} |
7119 | + |
7120 | +module_init(mod_init); |
7121 | +module_exit(mod_exit); |
7122 | +MODULE_LICENSE("GPL v2"); |
7123 | +MODULE_DESCRIPTION("BLAKE2s hash function"); |
7124 | +MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>"); |
7125 | diff --git a/lib/find_bit.c b/lib/find_bit.c |
7126 | index 18072ea9c20eb..479d0a9cf69fc 100644 |
7127 | --- a/lib/find_bit.c |
7128 | +++ b/lib/find_bit.c |
7129 | @@ -133,18 +133,6 @@ EXPORT_SYMBOL(find_last_bit); |
7130 | |
7131 | #ifdef __BIG_ENDIAN |
7132 | |
7133 | -/* include/linux/byteorder does not support "unsigned long" type */ |
7134 | -static inline unsigned long ext2_swab(const unsigned long y) |
7135 | -{ |
7136 | -#if BITS_PER_LONG == 64 |
7137 | - return (unsigned long) __swab64((u64) y); |
7138 | -#elif BITS_PER_LONG == 32 |
7139 | - return (unsigned long) __swab32((u32) y); |
7140 | -#else |
7141 | -#error BITS_PER_LONG not defined |
7142 | -#endif |
7143 | -} |
7144 | - |
7145 | #if !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) |
7146 | static unsigned long _find_next_bit_le(const unsigned long *addr, |
7147 | unsigned long nbits, unsigned long start, unsigned long invert) |
7148 | @@ -157,7 +145,7 @@ static unsigned long _find_next_bit_le(const unsigned long *addr, |
7149 | tmp = addr[start / BITS_PER_LONG] ^ invert; |
7150 | |
7151 | /* Handle 1st word. */ |
7152 | - tmp &= ext2_swab(BITMAP_FIRST_WORD_MASK(start)); |
7153 | + tmp &= swab(BITMAP_FIRST_WORD_MASK(start)); |
7154 | start = round_down(start, BITS_PER_LONG); |
7155 | |
7156 | while (!tmp) { |
7157 | @@ -168,7 +156,7 @@ static unsigned long _find_next_bit_le(const unsigned long *addr, |
7158 | tmp = addr[start / BITS_PER_LONG] ^ invert; |
7159 | } |
7160 | |
7161 | - return min(start + __ffs(ext2_swab(tmp)), nbits); |
7162 | + return min(start + __ffs(swab(tmp)), nbits); |
7163 | } |
7164 | #endif |
7165 | |
7166 | diff --git a/lib/random32.c b/lib/random32.c |
7167 | index 3c5b67b69cbaa..7e630025cbc3b 100644 |
7168 | --- a/lib/random32.c |
7169 | +++ b/lib/random32.c |
7170 | @@ -37,6 +37,9 @@ |
7171 | #include <linux/jiffies.h> |
7172 | #include <linux/random.h> |
7173 | #include <linux/sched.h> |
7174 | +#include <linux/bitops.h> |
7175 | +#include <linux/slab.h> |
7176 | +#include <linux/notifier.h> |
7177 | #include <asm/unaligned.h> |
7178 | |
7179 | /** |
7180 | @@ -543,9 +546,11 @@ static void prandom_reseed(unsigned long dontcare) |
7181 | * To avoid worrying about whether it's safe to delay that interrupt |
7182 | * long enough to seed all CPUs, just schedule an immediate timer event. |
7183 | */ |
7184 | -static void prandom_timer_start(struct random_ready_callback *unused) |
7185 | +static int prandom_timer_start(struct notifier_block *nb, |
7186 | + unsigned long action, void *data) |
7187 | { |
7188 | mod_timer(&seed_timer, jiffies); |
7189 | + return 0; |
7190 | } |
7191 | |
7192 | /* |
7193 | @@ -554,13 +559,13 @@ static void prandom_timer_start(struct random_ready_callback *unused) |
7194 | */ |
7195 | static int __init prandom_init_late(void) |
7196 | { |
7197 | - static struct random_ready_callback random_ready = { |
7198 | - .func = prandom_timer_start |
7199 | + static struct notifier_block random_ready = { |
7200 | + .notifier_call = prandom_timer_start |
7201 | }; |
7202 | - int ret = add_random_ready_callback(&random_ready); |
7203 | + int ret = register_random_ready_notifier(&random_ready); |
7204 | |
7205 | if (ret == -EALREADY) { |
7206 | - prandom_timer_start(&random_ready); |
7207 | + prandom_timer_start(&random_ready, 0, NULL); |
7208 | ret = 0; |
7209 | } |
7210 | return ret; |
7211 | diff --git a/lib/sha1.c b/lib/sha1.c |
7212 | index 5a56dfd7b99de..8aa4ee352ad58 100644 |
7213 | --- a/lib/sha1.c |
7214 | +++ b/lib/sha1.c |
7215 | @@ -9,6 +9,7 @@ |
7216 | #include <linux/export.h> |
7217 | #include <linux/bitops.h> |
7218 | #include <linux/cryptohash.h> |
7219 | +#include <linux/string.h> |
7220 | #include <asm/unaligned.h> |
7221 | |
7222 | /* |
7223 | @@ -54,7 +55,8 @@ |
7224 | #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \ |
7225 | __u32 TEMP = input(t); setW(t, TEMP); \ |
7226 | E += TEMP + rol32(A,5) + (fn) + (constant); \ |
7227 | - B = ror32(B, 2); } while (0) |
7228 | + B = ror32(B, 2); \ |
7229 | + TEMP = E; E = D; D = C; C = B; B = A; A = TEMP; } while (0) |
7230 | |
7231 | #define T_0_15(t, A, B, C, D, E) SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E ) |
7232 | #define T_16_19(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E ) |
7233 | @@ -81,6 +83,7 @@ |
7234 | void sha_transform(__u32 *digest, const char *data, __u32 *array) |
7235 | { |
7236 | __u32 A, B, C, D, E; |
7237 | + unsigned int i = 0; |
7238 | |
7239 | A = digest[0]; |
7240 | B = digest[1]; |
7241 | @@ -89,94 +92,24 @@ void sha_transform(__u32 *digest, const char *data, __u32 *array) |
7242 | E = digest[4]; |
7243 | |
7244 | /* Round 1 - iterations 0-16 take their input from 'data' */ |
7245 | - T_0_15( 0, A, B, C, D, E); |
7246 | - T_0_15( 1, E, A, B, C, D); |
7247 | - T_0_15( 2, D, E, A, B, C); |
7248 | - T_0_15( 3, C, D, E, A, B); |
7249 | - T_0_15( 4, B, C, D, E, A); |
7250 | - T_0_15( 5, A, B, C, D, E); |
7251 | - T_0_15( 6, E, A, B, C, D); |
7252 | - T_0_15( 7, D, E, A, B, C); |
7253 | - T_0_15( 8, C, D, E, A, B); |
7254 | - T_0_15( 9, B, C, D, E, A); |
7255 | - T_0_15(10, A, B, C, D, E); |
7256 | - T_0_15(11, E, A, B, C, D); |
7257 | - T_0_15(12, D, E, A, B, C); |
7258 | - T_0_15(13, C, D, E, A, B); |
7259 | - T_0_15(14, B, C, D, E, A); |
7260 | - T_0_15(15, A, B, C, D, E); |
7261 | + for (; i < 16; ++i) |
7262 | + T_0_15(i, A, B, C, D, E); |
7263 | |
7264 | /* Round 1 - tail. Input from 512-bit mixing array */ |
7265 | - T_16_19(16, E, A, B, C, D); |
7266 | - T_16_19(17, D, E, A, B, C); |
7267 | - T_16_19(18, C, D, E, A, B); |
7268 | - T_16_19(19, B, C, D, E, A); |
7269 | + for (; i < 20; ++i) |
7270 | + T_16_19(i, A, B, C, D, E); |
7271 | |
7272 | /* Round 2 */ |
7273 | - T_20_39(20, A, B, C, D, E); |
7274 | - T_20_39(21, E, A, B, C, D); |
7275 | - T_20_39(22, D, E, A, B, C); |
7276 | - T_20_39(23, C, D, E, A, B); |
7277 | - T_20_39(24, B, C, D, E, A); |
7278 | - T_20_39(25, A, B, C, D, E); |
7279 | - T_20_39(26, E, A, B, C, D); |
7280 | - T_20_39(27, D, E, A, B, C); |
7281 | - T_20_39(28, C, D, E, A, B); |
7282 | - T_20_39(29, B, C, D, E, A); |
7283 | - T_20_39(30, A, B, C, D, E); |
7284 | - T_20_39(31, E, A, B, C, D); |
7285 | - T_20_39(32, D, E, A, B, C); |
7286 | - T_20_39(33, C, D, E, A, B); |
7287 | - T_20_39(34, B, C, D, E, A); |
7288 | - T_20_39(35, A, B, C, D, E); |
7289 | - T_20_39(36, E, A, B, C, D); |
7290 | - T_20_39(37, D, E, A, B, C); |
7291 | - T_20_39(38, C, D, E, A, B); |
7292 | - T_20_39(39, B, C, D, E, A); |
7293 | + for (; i < 40; ++i) |
7294 | + T_20_39(i, A, B, C, D, E); |
7295 | |
7296 | /* Round 3 */ |
7297 | - T_40_59(40, A, B, C, D, E); |
7298 | - T_40_59(41, E, A, B, C, D); |
7299 | - T_40_59(42, D, E, A, B, C); |
7300 | - T_40_59(43, C, D, E, A, B); |
7301 | - T_40_59(44, B, C, D, E, A); |
7302 | - T_40_59(45, A, B, C, D, E); |
7303 | - T_40_59(46, E, A, B, C, D); |
7304 | - T_40_59(47, D, E, A, B, C); |
7305 | - T_40_59(48, C, D, E, A, B); |
7306 | - T_40_59(49, B, C, D, E, A); |
7307 | - T_40_59(50, A, B, C, D, E); |
7308 | - T_40_59(51, E, A, B, C, D); |
7309 | - T_40_59(52, D, E, A, B, C); |
7310 | - T_40_59(53, C, D, E, A, B); |
7311 | - T_40_59(54, B, C, D, E, A); |
7312 | - T_40_59(55, A, B, C, D, E); |
7313 | - T_40_59(56, E, A, B, C, D); |
7314 | - T_40_59(57, D, E, A, B, C); |
7315 | - T_40_59(58, C, D, E, A, B); |
7316 | - T_40_59(59, B, C, D, E, A); |
7317 | + for (; i < 60; ++i) |
7318 | + T_40_59(i, A, B, C, D, E); |
7319 | |
7320 | /* Round 4 */ |
7321 | - T_60_79(60, A, B, C, D, E); |
7322 | - T_60_79(61, E, A, B, C, D); |
7323 | - T_60_79(62, D, E, A, B, C); |
7324 | - T_60_79(63, C, D, E, A, B); |
7325 | - T_60_79(64, B, C, D, E, A); |
7326 | - T_60_79(65, A, B, C, D, E); |
7327 | - T_60_79(66, E, A, B, C, D); |
7328 | - T_60_79(67, D, E, A, B, C); |
7329 | - T_60_79(68, C, D, E, A, B); |
7330 | - T_60_79(69, B, C, D, E, A); |
7331 | - T_60_79(70, A, B, C, D, E); |
7332 | - T_60_79(71, E, A, B, C, D); |
7333 | - T_60_79(72, D, E, A, B, C); |
7334 | - T_60_79(73, C, D, E, A, B); |
7335 | - T_60_79(74, B, C, D, E, A); |
7336 | - T_60_79(75, A, B, C, D, E); |
7337 | - T_60_79(76, E, A, B, C, D); |
7338 | - T_60_79(77, D, E, A, B, C); |
7339 | - T_60_79(78, C, D, E, A, B); |
7340 | - T_60_79(79, B, C, D, E, A); |
7341 | + for (; i < 80; ++i) |
7342 | + T_60_79(i, A, B, C, D, E); |
7343 | |
7344 | digest[0] += A; |
7345 | digest[1] += B; |
7346 | diff --git a/lib/siphash.c b/lib/siphash.c |
7347 | index e632ee40aac1a..5b34b5c839887 100644 |
7348 | --- a/lib/siphash.c |
7349 | +++ b/lib/siphash.c |
7350 | @@ -18,19 +18,13 @@ |
7351 | #include <asm/word-at-a-time.h> |
7352 | #endif |
7353 | |
7354 | -#define SIPROUND \ |
7355 | - do { \ |
7356 | - v0 += v1; v1 = rol64(v1, 13); v1 ^= v0; v0 = rol64(v0, 32); \ |
7357 | - v2 += v3; v3 = rol64(v3, 16); v3 ^= v2; \ |
7358 | - v0 += v3; v3 = rol64(v3, 21); v3 ^= v0; \ |
7359 | - v2 += v1; v1 = rol64(v1, 17); v1 ^= v2; v2 = rol64(v2, 32); \ |
7360 | - } while (0) |
7361 | +#define SIPROUND SIPHASH_PERMUTATION(v0, v1, v2, v3) |
7362 | |
7363 | #define PREAMBLE(len) \ |
7364 | - u64 v0 = 0x736f6d6570736575ULL; \ |
7365 | - u64 v1 = 0x646f72616e646f6dULL; \ |
7366 | - u64 v2 = 0x6c7967656e657261ULL; \ |
7367 | - u64 v3 = 0x7465646279746573ULL; \ |
7368 | + u64 v0 = SIPHASH_CONST_0; \ |
7369 | + u64 v1 = SIPHASH_CONST_1; \ |
7370 | + u64 v2 = SIPHASH_CONST_2; \ |
7371 | + u64 v3 = SIPHASH_CONST_3; \ |
7372 | u64 b = ((u64)(len)) << 56; \ |
7373 | v3 ^= key->key[1]; \ |
7374 | v2 ^= key->key[0]; \ |
7375 | @@ -389,19 +383,13 @@ u32 hsiphash_4u32(const u32 first, const u32 second, const u32 third, |
7376 | } |
7377 | EXPORT_SYMBOL(hsiphash_4u32); |
7378 | #else |
7379 | -#define HSIPROUND \ |
7380 | - do { \ |
7381 | - v0 += v1; v1 = rol32(v1, 5); v1 ^= v0; v0 = rol32(v0, 16); \ |
7382 | - v2 += v3; v3 = rol32(v3, 8); v3 ^= v2; \ |
7383 | - v0 += v3; v3 = rol32(v3, 7); v3 ^= v0; \ |
7384 | - v2 += v1; v1 = rol32(v1, 13); v1 ^= v2; v2 = rol32(v2, 16); \ |
7385 | - } while (0) |
7386 | +#define HSIPROUND HSIPHASH_PERMUTATION(v0, v1, v2, v3) |
7387 | |
7388 | #define HPREAMBLE(len) \ |
7389 | - u32 v0 = 0; \ |
7390 | - u32 v1 = 0; \ |
7391 | - u32 v2 = 0x6c796765U; \ |
7392 | - u32 v3 = 0x74656462U; \ |
7393 | + u32 v0 = HSIPHASH_CONST_0; \ |
7394 | + u32 v1 = HSIPHASH_CONST_1; \ |
7395 | + u32 v2 = HSIPHASH_CONST_2; \ |
7396 | + u32 v3 = HSIPHASH_CONST_3; \ |
7397 | u32 b = ((u32)(len)) << 24; \ |
7398 | v3 ^= key->key[1]; \ |
7399 | v2 ^= key->key[0]; \ |
7400 | diff --git a/lib/swiotlb.c b/lib/swiotlb.c |
7401 | index 74b5b88621989..277a2f6fde6b1 100644 |
7402 | --- a/lib/swiotlb.c |
7403 | +++ b/lib/swiotlb.c |
7404 | @@ -532,9 +532,14 @@ found: |
7405 | */ |
7406 | for (i = 0; i < nslots; i++) |
7407 | io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT); |
7408 | - if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) |
7409 | - swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE); |
7410 | - |
7411 | + /* |
7412 | + * When dir == DMA_FROM_DEVICE we could omit the copy from the orig |
7413 | + * to the tlb buffer, if we knew for sure the device will |
7414 | + * overwirte the entire current content. But we don't. Thus |
7415 | + * unconditional bounce may prevent leaking swiotlb content (i.e. |
7416 | + * kernel memory) to user-space. |
7417 | + */ |
7418 | + swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE); |
7419 | return tlb_addr; |
7420 | } |
7421 | EXPORT_SYMBOL_GPL(swiotlb_tbl_map_single); |
7422 | diff --git a/mm/util.c b/mm/util.c |
7423 | index 07f4672061867..0559a1feec029 100644 |
7424 | --- a/mm/util.c |
7425 | +++ b/mm/util.c |
7426 | @@ -11,6 +11,7 @@ |
7427 | #include <linux/mman.h> |
7428 | #include <linux/hugetlb.h> |
7429 | #include <linux/vmalloc.h> |
7430 | +#include <linux/random.h> |
7431 | |
7432 | #include <asm/sections.h> |
7433 | #include <asm/uaccess.h> |
7434 | @@ -261,6 +262,38 @@ int vma_is_stack_for_current(struct vm_area_struct *vma) |
7435 | return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t)); |
7436 | } |
7437 | |
7438 | +/** |
7439 | + * randomize_page - Generate a random, page aligned address |
7440 | + * @start: The smallest acceptable address the caller will take. |
7441 | + * @range: The size of the area, starting at @start, within which the |
7442 | + * random address must fall. |
7443 | + * |
7444 | + * If @start + @range would overflow, @range is capped. |
7445 | + * |
7446 | + * NOTE: Historical use of randomize_range, which this replaces, presumed that |
7447 | + * @start was already page aligned. We now align it regardless. |
7448 | + * |
7449 | + * Return: A page aligned address within [start, start + range). On error, |
7450 | + * @start is returned. |
7451 | + */ |
7452 | +unsigned long randomize_page(unsigned long start, unsigned long range) |
7453 | +{ |
7454 | + if (!PAGE_ALIGNED(start)) { |
7455 | + range -= PAGE_ALIGN(start) - start; |
7456 | + start = PAGE_ALIGN(start); |
7457 | + } |
7458 | + |
7459 | + if (start > ULONG_MAX - range) |
7460 | + range = ULONG_MAX - start; |
7461 | + |
7462 | + range >>= PAGE_SHIFT; |
7463 | + |
7464 | + if (range == 0) |
7465 | + return start; |
7466 | + |
7467 | + return start + (get_random_long() % range << PAGE_SHIFT); |
7468 | +} |
7469 | + |
7470 | #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) |
7471 | void arch_pick_mmap_layout(struct mm_struct *mm) |
7472 | { |
7473 | diff --git a/net/core/secure_seq.c b/net/core/secure_seq.c |
7474 | index fd3ce461fbe62..ed05b8277aa40 100644 |
7475 | --- a/net/core/secure_seq.c |
7476 | +++ b/net/core/secure_seq.c |
7477 | @@ -62,7 +62,7 @@ __u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr, |
7478 | } |
7479 | EXPORT_SYMBOL(secure_tcpv6_sequence_number); |
7480 | |
7481 | -u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr, |
7482 | +u64 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr, |
7483 | __be16 dport) |
7484 | { |
7485 | u32 secret[MD5_MESSAGE_BYTES / 4]; |
7486 | @@ -102,7 +102,7 @@ __u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr, |
7487 | return seq_scale(hash[0]); |
7488 | } |
7489 | |
7490 | -u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport) |
7491 | +u64 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport) |
7492 | { |
7493 | u32 hash[MD5_DIGEST_WORDS]; |
7494 | |
7495 | diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c |
7496 | index 8876338707636..db47e1c407d9a 100644 |
7497 | --- a/net/ipv4/inet_hashtables.c |
7498 | +++ b/net/ipv4/inet_hashtables.c |
7499 | @@ -382,7 +382,7 @@ not_unique: |
7500 | return -EADDRNOTAVAIL; |
7501 | } |
7502 | |
7503 | -static u32 inet_sk_port_offset(const struct sock *sk) |
7504 | +static u64 inet_sk_port_offset(const struct sock *sk) |
7505 | { |
7506 | const struct inet_sock *inet = inet_sk(sk); |
7507 | |
7508 | @@ -537,8 +537,21 @@ void inet_unhash(struct sock *sk) |
7509 | } |
7510 | EXPORT_SYMBOL_GPL(inet_unhash); |
7511 | |
7512 | +/* RFC 6056 3.3.4. Algorithm 4: Double-Hash Port Selection Algorithm |
7513 | + * Note that we use 32bit integers (vs RFC 'short integers') |
7514 | + * because 2^16 is not a multiple of num_ephemeral and this |
7515 | + * property might be used by clever attacker. |
7516 | + * RFC claims using TABLE_LENGTH=10 buckets gives an improvement, though |
7517 | + * attacks were since demonstrated, thus we use 65536 instead to really |
7518 | + * give more isolation and privacy, at the expense of 256kB of kernel |
7519 | + * memory. |
7520 | + */ |
7521 | +#define INET_TABLE_PERTURB_SHIFT 16 |
7522 | +#define INET_TABLE_PERTURB_SIZE (1 << INET_TABLE_PERTURB_SHIFT) |
7523 | +static u32 *table_perturb; |
7524 | + |
7525 | int __inet_hash_connect(struct inet_timewait_death_row *death_row, |
7526 | - struct sock *sk, u32 port_offset, |
7527 | + struct sock *sk, u64 port_offset, |
7528 | int (*check_established)(struct inet_timewait_death_row *, |
7529 | struct sock *, __u16, struct inet_timewait_sock **)) |
7530 | { |
7531 | @@ -550,7 +563,7 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row, |
7532 | struct inet_bind_bucket *tb; |
7533 | u32 remaining, offset; |
7534 | int ret, i, low, high; |
7535 | - static u32 hint; |
7536 | + u32 index; |
7537 | |
7538 | if (port) { |
7539 | head = &hinfo->bhash[inet_bhashfn(net, port, |
7540 | @@ -575,7 +588,13 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row, |
7541 | if (likely(remaining > 1)) |
7542 | remaining &= ~1U; |
7543 | |
7544 | - offset = (hint + port_offset) % remaining; |
7545 | + net_get_random_once(table_perturb, |
7546 | + INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb)); |
7547 | + index = port_offset & (INET_TABLE_PERTURB_SIZE - 1); |
7548 | + |
7549 | + offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32); |
7550 | + offset %= remaining; |
7551 | + |
7552 | /* In first pass we try ports of @low parity. |
7553 | * inet_csk_get_port() does the opposite choice. |
7554 | */ |
7555 | @@ -628,7 +647,13 @@ next_port: |
7556 | return -EADDRNOTAVAIL; |
7557 | |
7558 | ok: |
7559 | - hint += i + 2; |
7560 | + /* Here we want to add a little bit of randomness to the next source |
7561 | + * port that will be chosen. We use a max() with a random here so that |
7562 | + * on low contention the randomness is maximal and on high contention |
7563 | + * it may be inexistent. |
7564 | + */ |
7565 | + i = max_t(int, i, (prandom_u32() & 7) * 2); |
7566 | + WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2); |
7567 | |
7568 | /* Head lock still held and bh's disabled */ |
7569 | inet_bind_hash(sk, tb, port); |
7570 | @@ -651,7 +676,7 @@ ok: |
7571 | int inet_hash_connect(struct inet_timewait_death_row *death_row, |
7572 | struct sock *sk) |
7573 | { |
7574 | - u32 port_offset = 0; |
7575 | + u64 port_offset = 0; |
7576 | |
7577 | if (!inet_sk(sk)->inet_num) |
7578 | port_offset = inet_sk_port_offset(sk); |
7579 | @@ -669,6 +694,15 @@ void inet_hashinfo_init(struct inet_hashinfo *h) |
7580 | INIT_HLIST_NULLS_HEAD(&h->listening_hash[i].nulls_head, |
7581 | i + LISTENING_NULLS_BASE); |
7582 | } |
7583 | + |
7584 | + if (h != &tcp_hashinfo) |
7585 | + return; |
7586 | + |
7587 | + /* this one is used for source ports of outgoing connections */ |
7588 | + table_perturb = kmalloc_array(INET_TABLE_PERTURB_SIZE, |
7589 | + sizeof(*table_perturb), GFP_KERNEL); |
7590 | + if (!table_perturb) |
7591 | + panic("TCP: failed to alloc table_perturb"); |
7592 | } |
7593 | EXPORT_SYMBOL_GPL(inet_hashinfo_init); |
7594 | |
7595 | diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c |
7596 | index d47cab6d7c6de..8223ac61a9fd3 100644 |
7597 | --- a/net/ipv6/inet6_hashtables.c |
7598 | +++ b/net/ipv6/inet6_hashtables.c |
7599 | @@ -242,7 +242,7 @@ not_unique: |
7600 | return -EADDRNOTAVAIL; |
7601 | } |
7602 | |
7603 | -static u32 inet6_sk_port_offset(const struct sock *sk) |
7604 | +static u64 inet6_sk_port_offset(const struct sock *sk) |
7605 | { |
7606 | const struct inet_sock *inet = inet_sk(sk); |
7607 | |
7608 | @@ -254,7 +254,7 @@ static u32 inet6_sk_port_offset(const struct sock *sk) |
7609 | int inet6_hash_connect(struct inet_timewait_death_row *death_row, |
7610 | struct sock *sk) |
7611 | { |
7612 | - u32 port_offset = 0; |
7613 | + u64 port_offset = 0; |
7614 | |
7615 | if (!inet_sk(sk)->inet_num) |
7616 | port_offset = inet6_sk_port_offset(sk); |
7617 | diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c |
7618 | index 76ef758db1124..e412020029dfb 100644 |
7619 | --- a/net/l2tp/l2tp_ip6.c |
7620 | +++ b/net/l2tp/l2tp_ip6.c |
7621 | @@ -518,14 +518,15 @@ static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) |
7622 | struct ipcm6_cookie ipc6; |
7623 | int addr_len = msg->msg_namelen; |
7624 | int transhdrlen = 4; /* zero session-id */ |
7625 | - int ulen = len + transhdrlen; |
7626 | + int ulen; |
7627 | int err; |
7628 | |
7629 | /* Rough check on arithmetic overflow, |
7630 | better check is made in ip6_append_data(). |
7631 | */ |
7632 | - if (len > INT_MAX) |
7633 | + if (len > INT_MAX - transhdrlen) |
7634 | return -EMSGSIZE; |
7635 | + ulen = len + transhdrlen; |
7636 | |
7637 | /* Mirror BSD error message compatibility */ |
7638 | if (msg->msg_flags & MSG_OOB) |
7639 | diff --git a/net/l2tp/l2tp_ppp.c b/net/l2tp/l2tp_ppp.c |
7640 | index 979fa868a4f1d..4673f6f489425 100644 |
7641 | --- a/net/l2tp/l2tp_ppp.c |
7642 | +++ b/net/l2tp/l2tp_ppp.c |
7643 | @@ -435,20 +435,28 @@ abort: |
7644 | * Session (and tunnel control) socket create/destroy. |
7645 | *****************************************************************************/ |
7646 | |
7647 | +static void pppol2tp_put_sk(struct rcu_head *head) |
7648 | +{ |
7649 | + struct pppol2tp_session *ps; |
7650 | + |
7651 | + ps = container_of(head, typeof(*ps), rcu); |
7652 | + sock_put(ps->__sk); |
7653 | +} |
7654 | + |
7655 | /* Called by l2tp_core when a session socket is being closed. |
7656 | */ |
7657 | static void pppol2tp_session_close(struct l2tp_session *session) |
7658 | { |
7659 | - struct sock *sk; |
7660 | - |
7661 | - BUG_ON(session->magic != L2TP_SESSION_MAGIC); |
7662 | + struct pppol2tp_session *ps; |
7663 | |
7664 | - sk = pppol2tp_session_get_sock(session); |
7665 | - if (sk) { |
7666 | - if (sk->sk_socket) |
7667 | - inet_shutdown(sk->sk_socket, SEND_SHUTDOWN); |
7668 | - sock_put(sk); |
7669 | - } |
7670 | + ps = l2tp_session_priv(session); |
7671 | + mutex_lock(&ps->sk_lock); |
7672 | + ps->__sk = rcu_dereference_protected(ps->sk, |
7673 | + lockdep_is_held(&ps->sk_lock)); |
7674 | + RCU_INIT_POINTER(ps->sk, NULL); |
7675 | + if (ps->__sk) |
7676 | + call_rcu(&ps->rcu, pppol2tp_put_sk); |
7677 | + mutex_unlock(&ps->sk_lock); |
7678 | } |
7679 | |
7680 | /* Really kill the session socket. (Called from sock_put() if |
7681 | @@ -468,14 +476,6 @@ static void pppol2tp_session_destruct(struct sock *sk) |
7682 | } |
7683 | } |
7684 | |
7685 | -static void pppol2tp_put_sk(struct rcu_head *head) |
7686 | -{ |
7687 | - struct pppol2tp_session *ps; |
7688 | - |
7689 | - ps = container_of(head, typeof(*ps), rcu); |
7690 | - sock_put(ps->__sk); |
7691 | -} |
7692 | - |
7693 | /* Called when the PPPoX socket (session) is closed. |
7694 | */ |
7695 | static int pppol2tp_release(struct socket *sock) |
7696 | @@ -499,26 +499,17 @@ static int pppol2tp_release(struct socket *sock) |
7697 | sock_orphan(sk); |
7698 | sock->sk = NULL; |
7699 | |
7700 | + /* If the socket is associated with a session, |
7701 | + * l2tp_session_delete will call pppol2tp_session_close which |
7702 | + * will drop the session's ref on the socket. |
7703 | + */ |
7704 | session = pppol2tp_sock_to_session(sk); |
7705 | - |
7706 | - if (session != NULL) { |
7707 | - struct pppol2tp_session *ps; |
7708 | - |
7709 | + if (session) { |
7710 | l2tp_session_delete(session); |
7711 | - |
7712 | - ps = l2tp_session_priv(session); |
7713 | - mutex_lock(&ps->sk_lock); |
7714 | - ps->__sk = rcu_dereference_protected(ps->sk, |
7715 | - lockdep_is_held(&ps->sk_lock)); |
7716 | - RCU_INIT_POINTER(ps->sk, NULL); |
7717 | - mutex_unlock(&ps->sk_lock); |
7718 | - call_rcu(&ps->rcu, pppol2tp_put_sk); |
7719 | - |
7720 | - /* Rely on the sock_put() call at the end of the function for |
7721 | - * dropping the reference held by pppol2tp_sock_to_session(). |
7722 | - * The last reference will be dropped by pppol2tp_put_sk(). |
7723 | - */ |
7724 | + /* drop the ref obtained by pppol2tp_sock_to_session */ |
7725 | + sock_put(sk); |
7726 | } |
7727 | + |
7728 | release_sock(sk); |
7729 | |
7730 | /* This will delete the session context via |
7731 | @@ -827,6 +818,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, |
7732 | |
7733 | out_no_ppp: |
7734 | /* This is how we get the session context from the socket. */ |
7735 | + sock_hold(sk); |
7736 | sk->sk_user_data = session; |
7737 | rcu_assign_pointer(ps->sk, sk); |
7738 | mutex_unlock(&ps->sk_lock); |
7739 | diff --git a/net/sunrpc/xprtrdma/rpc_rdma.c b/net/sunrpc/xprtrdma/rpc_rdma.c |
7740 | index 0287734f126f6..aec0113a88b08 100644 |
7741 | --- a/net/sunrpc/xprtrdma/rpc_rdma.c |
7742 | +++ b/net/sunrpc/xprtrdma/rpc_rdma.c |
7743 | @@ -75,7 +75,7 @@ static unsigned int rpcrdma_max_call_header_size(unsigned int maxsegs) |
7744 | |
7745 | /* Maximum Read list size */ |
7746 | maxsegs += 2; /* segment for head and tail buffers */ |
7747 | - size = maxsegs * sizeof(struct rpcrdma_read_chunk); |
7748 | + size += maxsegs * sizeof(struct rpcrdma_read_chunk); |
7749 | |
7750 | /* Minimal Read chunk size */ |
7751 | size += sizeof(__be32); /* segment count */ |
7752 | @@ -101,7 +101,7 @@ static unsigned int rpcrdma_max_reply_header_size(unsigned int maxsegs) |
7753 | |
7754 | /* Maximum Write list size */ |
7755 | maxsegs += 2; /* segment for head and tail buffers */ |
7756 | - size = sizeof(__be32); /* segment count */ |
7757 | + size += sizeof(__be32); /* segment count */ |
7758 | size += maxsegs * sizeof(struct rpcrdma_segment); |
7759 | size += sizeof(__be32); /* list discriminator */ |
7760 | |
7761 | diff --git a/sound/soc/codecs/cs42l52.c b/sound/soc/codecs/cs42l52.c |
7762 | index 0d9c4a57301bb..47f2439fd7b08 100644 |
7763 | --- a/sound/soc/codecs/cs42l52.c |
7764 | +++ b/sound/soc/codecs/cs42l52.c |
7765 | @@ -141,7 +141,9 @@ static DECLARE_TLV_DB_SCALE(mic_tlv, 1600, 100, 0); |
7766 | |
7767 | static DECLARE_TLV_DB_SCALE(pga_tlv, -600, 50, 0); |
7768 | |
7769 | -static DECLARE_TLV_DB_SCALE(mix_tlv, -50, 50, 0); |
7770 | +static DECLARE_TLV_DB_SCALE(pass_tlv, -6000, 50, 0); |
7771 | + |
7772 | +static DECLARE_TLV_DB_SCALE(mix_tlv, -5150, 50, 0); |
7773 | |
7774 | static DECLARE_TLV_DB_SCALE(beep_tlv, -56, 200, 0); |
7775 | |
7776 | @@ -355,7 +357,7 @@ static const struct snd_kcontrol_new cs42l52_snd_controls[] = { |
7777 | CS42L52_SPKB_VOL, 0, 0x40, 0xC0, hl_tlv), |
7778 | |
7779 | SOC_DOUBLE_R_SX_TLV("Bypass Volume", CS42L52_PASSTHRUA_VOL, |
7780 | - CS42L52_PASSTHRUB_VOL, 0, 0x88, 0x90, pga_tlv), |
7781 | + CS42L52_PASSTHRUB_VOL, 0, 0x88, 0x90, pass_tlv), |
7782 | |
7783 | SOC_DOUBLE("Bypass Mute", CS42L52_MISC_CTL, 4, 5, 1, 0), |
7784 | |
7785 | @@ -368,7 +370,7 @@ static const struct snd_kcontrol_new cs42l52_snd_controls[] = { |
7786 | CS42L52_ADCB_VOL, 0, 0xA0, 0x78, ipd_tlv), |
7787 | SOC_DOUBLE_R_SX_TLV("ADC Mixer Volume", |
7788 | CS42L52_ADCA_MIXER_VOL, CS42L52_ADCB_MIXER_VOL, |
7789 | - 0, 0x19, 0x7F, ipd_tlv), |
7790 | + 0, 0x19, 0x7F, mix_tlv), |
7791 | |
7792 | SOC_DOUBLE("ADC Switch", CS42L52_ADC_MISC_CTL, 0, 1, 1, 0), |
7793 | |
7794 | diff --git a/sound/soc/codecs/cs42l56.c b/sound/soc/codecs/cs42l56.c |
7795 | index a2535a7eb4bbd..f9f8a9112ff8d 100644 |
7796 | --- a/sound/soc/codecs/cs42l56.c |
7797 | +++ b/sound/soc/codecs/cs42l56.c |
7798 | @@ -405,9 +405,9 @@ static const struct snd_kcontrol_new cs42l56_snd_controls[] = { |
7799 | SOC_DOUBLE("ADC Boost Switch", CS42L56_GAIN_BIAS_CTL, 3, 2, 1, 1), |
7800 | |
7801 | SOC_DOUBLE_R_SX_TLV("Headphone Volume", CS42L56_HPA_VOLUME, |
7802 | - CS42L56_HPB_VOLUME, 0, 0x84, 0x48, hl_tlv), |
7803 | + CS42L56_HPB_VOLUME, 0, 0x44, 0x48, hl_tlv), |
7804 | SOC_DOUBLE_R_SX_TLV("LineOut Volume", CS42L56_LOA_VOLUME, |
7805 | - CS42L56_LOB_VOLUME, 0, 0x84, 0x48, hl_tlv), |
7806 | + CS42L56_LOB_VOLUME, 0, 0x44, 0x48, hl_tlv), |
7807 | |
7808 | SOC_SINGLE_TLV("Bass Shelving Volume", CS42L56_TONE_CTL, |
7809 | 0, 0x00, 1, tone_tlv), |
7810 | diff --git a/sound/soc/codecs/cs53l30.c b/sound/soc/codecs/cs53l30.c |
7811 | index cb47fb595ff41..5a16020423fe0 100644 |
7812 | --- a/sound/soc/codecs/cs53l30.c |
7813 | +++ b/sound/soc/codecs/cs53l30.c |
7814 | @@ -351,22 +351,22 @@ static const struct snd_kcontrol_new cs53l30_snd_controls[] = { |
7815 | SOC_ENUM("ADC2 NG Delay", adc2_ng_delay_enum), |
7816 | |
7817 | SOC_SINGLE_SX_TLV("ADC1A PGA Volume", |
7818 | - CS53L30_ADC1A_AFE_CTL, 0, 0x34, 0x18, pga_tlv), |
7819 | + CS53L30_ADC1A_AFE_CTL, 0, 0x34, 0x24, pga_tlv), |
7820 | SOC_SINGLE_SX_TLV("ADC1B PGA Volume", |
7821 | - CS53L30_ADC1B_AFE_CTL, 0, 0x34, 0x18, pga_tlv), |
7822 | + CS53L30_ADC1B_AFE_CTL, 0, 0x34, 0x24, pga_tlv), |
7823 | SOC_SINGLE_SX_TLV("ADC2A PGA Volume", |
7824 | - CS53L30_ADC2A_AFE_CTL, 0, 0x34, 0x18, pga_tlv), |
7825 | + CS53L30_ADC2A_AFE_CTL, 0, 0x34, 0x24, pga_tlv), |
7826 | SOC_SINGLE_SX_TLV("ADC2B PGA Volume", |
7827 | - CS53L30_ADC2B_AFE_CTL, 0, 0x34, 0x18, pga_tlv), |
7828 | + CS53L30_ADC2B_AFE_CTL, 0, 0x34, 0x24, pga_tlv), |
7829 | |
7830 | SOC_SINGLE_SX_TLV("ADC1A Digital Volume", |
7831 | - CS53L30_ADC1A_DIG_VOL, 0, 0xA0, 0x0C, dig_tlv), |
7832 | + CS53L30_ADC1A_DIG_VOL, 0, 0xA0, 0x6C, dig_tlv), |
7833 | SOC_SINGLE_SX_TLV("ADC1B Digital Volume", |
7834 | - CS53L30_ADC1B_DIG_VOL, 0, 0xA0, 0x0C, dig_tlv), |
7835 | + CS53L30_ADC1B_DIG_VOL, 0, 0xA0, 0x6C, dig_tlv), |
7836 | SOC_SINGLE_SX_TLV("ADC2A Digital Volume", |
7837 | - CS53L30_ADC2A_DIG_VOL, 0, 0xA0, 0x0C, dig_tlv), |
7838 | + CS53L30_ADC2A_DIG_VOL, 0, 0xA0, 0x6C, dig_tlv), |
7839 | SOC_SINGLE_SX_TLV("ADC2B Digital Volume", |
7840 | - CS53L30_ADC2B_DIG_VOL, 0, 0xA0, 0x0C, dig_tlv), |
7841 | + CS53L30_ADC2B_DIG_VOL, 0, 0xA0, 0x6C, dig_tlv), |
7842 | }; |
7843 | |
7844 | static const struct snd_soc_dapm_widget cs53l30_dapm_widgets[] = { |
7845 | diff --git a/sound/soc/codecs/wm8962.c b/sound/soc/codecs/wm8962.c |
7846 | index 0e8008d381619..d46881f96c164 100644 |
7847 | --- a/sound/soc/codecs/wm8962.c |
7848 | +++ b/sound/soc/codecs/wm8962.c |
7849 | @@ -3861,6 +3861,7 @@ static int wm8962_runtime_suspend(struct device *dev) |
7850 | #endif |
7851 | |
7852 | static const struct dev_pm_ops wm8962_pm = { |
7853 | + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) |
7854 | SET_RUNTIME_PM_OPS(wm8962_runtime_suspend, wm8962_runtime_resume, NULL) |
7855 | }; |
7856 |