1 |
commit: a8ee0e96ec8829dcec0a8fac8e0f9203189002d3 |
2 |
Author: Mike Pagano <mpagano <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon May 30 13:58:07 2022 +0000 |
4 |
Commit: Mike Pagano <mpagano <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon May 30 13:58:07 2022 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/linux-patches.git/commit/?id=a8ee0e96 |
7 |
|
8 |
Linux patch 5.17.12 |
9 |
|
10 |
Signed-off-by: Mike Pagano <mpagano <AT> gentoo.org> |
11 |
|
12 |
0000_README | 4 + |
13 |
1011_linux-5.17.12.patch | 4650 ++++++++++++++++++++++++++++++++++++++++++++++ |
14 |
2 files changed, 4654 insertions(+) |
15 |
|
16 |
diff --git a/0000_README b/0000_README |
17 |
index 8aed7c53..ecb45bb4 100644 |
18 |
--- a/0000_README |
19 |
+++ b/0000_README |
20 |
@@ -87,6 +87,10 @@ Patch: 1010_linux-5.17.11.patch |
21 |
From: http://www.kernel.org |
22 |
Desc: Linux 5.17.11 |
23 |
|
24 |
+Patch: 1011_linux-5.17.12.patch |
25 |
+From: http://www.kernel.org |
26 |
+Desc: Linux 5.17.12 |
27 |
+ |
28 |
Patch: 1500_XATTR_USER_PREFIX.patch |
29 |
From: https://bugs.gentoo.org/show_bug.cgi?id=470644 |
30 |
Desc: Support for namespace user.pax.* on tmpfs. |
31 |
|
32 |
diff --git a/1011_linux-5.17.12.patch b/1011_linux-5.17.12.patch |
33 |
new file mode 100644 |
34 |
index 00000000..185a60e2 |
35 |
--- /dev/null |
36 |
+++ b/1011_linux-5.17.12.patch |
37 |
@@ -0,0 +1,4650 @@ |
38 |
+diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt |
39 |
+index 59f881f367793..ad67b848d04ee 100644 |
40 |
+--- a/Documentation/admin-guide/kernel-parameters.txt |
41 |
++++ b/Documentation/admin-guide/kernel-parameters.txt |
42 |
+@@ -4355,6 +4355,12 @@ |
43 |
+ fully seed the kernel's CRNG. Default is controlled |
44 |
+ by CONFIG_RANDOM_TRUST_CPU. |
45 |
+ |
46 |
++ random.trust_bootloader={on,off} |
47 |
++ [KNL] Enable or disable trusting the use of a |
48 |
++ seed passed by the bootloader (if available) to |
49 |
++ fully seed the kernel's CRNG. Default is controlled |
50 |
++ by CONFIG_RANDOM_TRUST_BOOTLOADER. |
51 |
++ |
52 |
+ randomize_kstack_offset= |
53 |
+ [KNL] Enable or disable kernel stack offset |
54 |
+ randomization, which provides roughly 5 bits of |
55 |
+diff --git a/Documentation/admin-guide/sysctl/kernel.rst b/Documentation/admin-guide/sysctl/kernel.rst |
56 |
+index 0f86e9f931293..264735c5d0bda 100644 |
57 |
+--- a/Documentation/admin-guide/sysctl/kernel.rst |
58 |
++++ b/Documentation/admin-guide/sysctl/kernel.rst |
59 |
+@@ -1025,28 +1025,22 @@ This is a directory, with the following entries: |
60 |
+ * ``boot_id``: a UUID generated the first time this is retrieved, and |
61 |
+ unvarying after that; |
62 |
+ |
63 |
++* ``uuid``: a UUID generated every time this is retrieved (this can |
64 |
++ thus be used to generate UUIDs at will); |
65 |
++ |
66 |
+ * ``entropy_avail``: the pool's entropy count, in bits; |
67 |
+ |
68 |
+ * ``poolsize``: the entropy pool size, in bits; |
69 |
+ |
70 |
+ * ``urandom_min_reseed_secs``: obsolete (used to determine the minimum |
71 |
+- number of seconds between urandom pool reseeding). |
72 |
+- |
73 |
+-* ``uuid``: a UUID generated every time this is retrieved (this can |
74 |
+- thus be used to generate UUIDs at will); |
75 |
++ number of seconds between urandom pool reseeding). This file is |
76 |
++ writable for compatibility purposes, but writing to it has no effect |
77 |
++ on any RNG behavior; |
78 |
+ |
79 |
+ * ``write_wakeup_threshold``: when the entropy count drops below this |
80 |
+ (as a number of bits), processes waiting to write to ``/dev/random`` |
81 |
+- are woken up. |
82 |
+- |
83 |
+-If ``drivers/char/random.c`` is built with ``ADD_INTERRUPT_BENCH`` |
84 |
+-defined, these additional entries are present: |
85 |
+- |
86 |
+-* ``add_interrupt_avg_cycles``: the average number of cycles between |
87 |
+- interrupts used to feed the pool; |
88 |
+- |
89 |
+-* ``add_interrupt_avg_deviation``: the standard deviation seen on the |
90 |
+- number of cycles between interrupts used to feed the pool. |
91 |
++ are woken up. This file is writable for compatibility purposes, but |
92 |
++ writing to it has no effect on any RNG behavior. |
93 |
+ |
94 |
+ |
95 |
+ randomize_va_space |
96 |
+diff --git a/Makefile b/Makefile |
97 |
+index b821f270a4ca6..25c44dda0ef37 100644 |
98 |
+--- a/Makefile |
99 |
++++ b/Makefile |
100 |
+@@ -1,7 +1,7 @@ |
101 |
+ # SPDX-License-Identifier: GPL-2.0 |
102 |
+ VERSION = 5 |
103 |
+ PATCHLEVEL = 17 |
104 |
+-SUBLEVEL = 11 |
105 |
++SUBLEVEL = 12 |
106 |
+ EXTRAVERSION = |
107 |
+ NAME = Superb Owl |
108 |
+ |
109 |
+diff --git a/arch/alpha/include/asm/timex.h b/arch/alpha/include/asm/timex.h |
110 |
+index b565cc6f408e9..f89798da8a147 100644 |
111 |
+--- a/arch/alpha/include/asm/timex.h |
112 |
++++ b/arch/alpha/include/asm/timex.h |
113 |
+@@ -28,5 +28,6 @@ static inline cycles_t get_cycles (void) |
114 |
+ __asm__ __volatile__ ("rpcc %0" : "=r"(ret)); |
115 |
+ return ret; |
116 |
+ } |
117 |
++#define get_cycles get_cycles |
118 |
+ |
119 |
+ #endif |
120 |
+diff --git a/arch/arm/include/asm/timex.h b/arch/arm/include/asm/timex.h |
121 |
+index 7c3b3671d6c25..6d1337c169cd3 100644 |
122 |
+--- a/arch/arm/include/asm/timex.h |
123 |
++++ b/arch/arm/include/asm/timex.h |
124 |
+@@ -11,5 +11,6 @@ |
125 |
+ |
126 |
+ typedef unsigned long cycles_t; |
127 |
+ #define get_cycles() ({ cycles_t c; read_current_timer(&c) ? 0 : c; }) |
128 |
++#define random_get_entropy() (((unsigned long)get_cycles()) ?: random_get_entropy_fallback()) |
129 |
+ |
130 |
+ #endif |
131 |
+diff --git a/arch/ia64/include/asm/timex.h b/arch/ia64/include/asm/timex.h |
132 |
+index 869a3ac6bf23a..7ccc077a60bed 100644 |
133 |
+--- a/arch/ia64/include/asm/timex.h |
134 |
++++ b/arch/ia64/include/asm/timex.h |
135 |
+@@ -39,6 +39,7 @@ get_cycles (void) |
136 |
+ ret = ia64_getreg(_IA64_REG_AR_ITC); |
137 |
+ return ret; |
138 |
+ } |
139 |
++#define get_cycles get_cycles |
140 |
+ |
141 |
+ extern void ia64_cpu_local_tick (void); |
142 |
+ extern unsigned long long ia64_native_sched_clock (void); |
143 |
+diff --git a/arch/m68k/include/asm/timex.h b/arch/m68k/include/asm/timex.h |
144 |
+index 6a21d93582805..f4a7a340f4cae 100644 |
145 |
+--- a/arch/m68k/include/asm/timex.h |
146 |
++++ b/arch/m68k/include/asm/timex.h |
147 |
+@@ -35,7 +35,7 @@ static inline unsigned long random_get_entropy(void) |
148 |
+ { |
149 |
+ if (mach_random_get_entropy) |
150 |
+ return mach_random_get_entropy(); |
151 |
+- return 0; |
152 |
++ return random_get_entropy_fallback(); |
153 |
+ } |
154 |
+ #define random_get_entropy random_get_entropy |
155 |
+ |
156 |
+diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h |
157 |
+index 8026baf46e729..2e107886f97ac 100644 |
158 |
+--- a/arch/mips/include/asm/timex.h |
159 |
++++ b/arch/mips/include/asm/timex.h |
160 |
+@@ -76,25 +76,24 @@ static inline cycles_t get_cycles(void) |
161 |
+ else |
162 |
+ return 0; /* no usable counter */ |
163 |
+ } |
164 |
++#define get_cycles get_cycles |
165 |
+ |
166 |
+ /* |
167 |
+ * Like get_cycles - but where c0_count is not available we desperately |
168 |
+ * use c0_random in an attempt to get at least a little bit of entropy. |
169 |
+- * |
170 |
+- * R6000 and R6000A neither have a count register nor a random register. |
171 |
+- * That leaves no entropy source in the CPU itself. |
172 |
+ */ |
173 |
+ static inline unsigned long random_get_entropy(void) |
174 |
+ { |
175 |
+- unsigned int prid = read_c0_prid(); |
176 |
+- unsigned int imp = prid & PRID_IMP_MASK; |
177 |
++ unsigned int c0_random; |
178 |
+ |
179 |
+- if (can_use_mips_counter(prid)) |
180 |
++ if (can_use_mips_counter(read_c0_prid())) |
181 |
+ return read_c0_count(); |
182 |
+- else if (likely(imp != PRID_IMP_R6000 && imp != PRID_IMP_R6000A)) |
183 |
+- return read_c0_random(); |
184 |
++ |
185 |
++ if (cpu_has_3kex) |
186 |
++ c0_random = (read_c0_random() >> 8) & 0x3f; |
187 |
+ else |
188 |
+- return 0; /* no usable register */ |
189 |
++ c0_random = read_c0_random() & 0x3f; |
190 |
++ return (random_get_entropy_fallback() << 6) | (0x3f - c0_random); |
191 |
+ } |
192 |
+ #define random_get_entropy random_get_entropy |
193 |
+ |
194 |
+diff --git a/arch/nios2/include/asm/timex.h b/arch/nios2/include/asm/timex.h |
195 |
+index a769f871b28d9..40a1adc9bd03e 100644 |
196 |
+--- a/arch/nios2/include/asm/timex.h |
197 |
++++ b/arch/nios2/include/asm/timex.h |
198 |
+@@ -8,5 +8,8 @@ |
199 |
+ typedef unsigned long cycles_t; |
200 |
+ |
201 |
+ extern cycles_t get_cycles(void); |
202 |
++#define get_cycles get_cycles |
203 |
++ |
204 |
++#define random_get_entropy() (((unsigned long)get_cycles()) ?: random_get_entropy_fallback()) |
205 |
+ |
206 |
+ #endif |
207 |
+diff --git a/arch/parisc/include/asm/timex.h b/arch/parisc/include/asm/timex.h |
208 |
+index 06b510f8172e3..b4622cb06a75e 100644 |
209 |
+--- a/arch/parisc/include/asm/timex.h |
210 |
++++ b/arch/parisc/include/asm/timex.h |
211 |
+@@ -13,9 +13,10 @@ |
212 |
+ |
213 |
+ typedef unsigned long cycles_t; |
214 |
+ |
215 |
+-static inline cycles_t get_cycles (void) |
216 |
++static inline cycles_t get_cycles(void) |
217 |
+ { |
218 |
+ return mfctl(16); |
219 |
+ } |
220 |
++#define get_cycles get_cycles |
221 |
+ |
222 |
+ #endif |
223 |
+diff --git a/arch/powerpc/include/asm/timex.h b/arch/powerpc/include/asm/timex.h |
224 |
+index fa2e76e4093a3..14b4489de52c5 100644 |
225 |
+--- a/arch/powerpc/include/asm/timex.h |
226 |
++++ b/arch/powerpc/include/asm/timex.h |
227 |
+@@ -19,6 +19,7 @@ static inline cycles_t get_cycles(void) |
228 |
+ { |
229 |
+ return mftb(); |
230 |
+ } |
231 |
++#define get_cycles get_cycles |
232 |
+ |
233 |
+ #endif /* __KERNEL__ */ |
234 |
+ #endif /* _ASM_POWERPC_TIMEX_H */ |
235 |
+diff --git a/arch/riscv/include/asm/timex.h b/arch/riscv/include/asm/timex.h |
236 |
+index 507cae273bc62..d6a7428f6248d 100644 |
237 |
+--- a/arch/riscv/include/asm/timex.h |
238 |
++++ b/arch/riscv/include/asm/timex.h |
239 |
+@@ -41,7 +41,7 @@ static inline u32 get_cycles_hi(void) |
240 |
+ static inline unsigned long random_get_entropy(void) |
241 |
+ { |
242 |
+ if (unlikely(clint_time_val == NULL)) |
243 |
+- return 0; |
244 |
++ return random_get_entropy_fallback(); |
245 |
+ return get_cycles(); |
246 |
+ } |
247 |
+ #define random_get_entropy() random_get_entropy() |
248 |
+diff --git a/arch/s390/include/asm/timex.h b/arch/s390/include/asm/timex.h |
249 |
+index 50d9b04ecbd14..bc50ee0e91ff1 100644 |
250 |
+--- a/arch/s390/include/asm/timex.h |
251 |
++++ b/arch/s390/include/asm/timex.h |
252 |
+@@ -201,6 +201,7 @@ static inline cycles_t get_cycles(void) |
253 |
+ { |
254 |
+ return (cycles_t) get_tod_clock() >> 2; |
255 |
+ } |
256 |
++#define get_cycles get_cycles |
257 |
+ |
258 |
+ int get_phys_clock(unsigned long *clock); |
259 |
+ void init_cpu_timer(void); |
260 |
+diff --git a/arch/sparc/include/asm/timex_32.h b/arch/sparc/include/asm/timex_32.h |
261 |
+index 542915b462097..f86326a6f89e0 100644 |
262 |
+--- a/arch/sparc/include/asm/timex_32.h |
263 |
++++ b/arch/sparc/include/asm/timex_32.h |
264 |
+@@ -9,8 +9,6 @@ |
265 |
+ |
266 |
+ #define CLOCK_TICK_RATE 1193180 /* Underlying HZ */ |
267 |
+ |
268 |
+-/* XXX Maybe do something better at some point... -DaveM */ |
269 |
+-typedef unsigned long cycles_t; |
270 |
+-#define get_cycles() (0) |
271 |
++#include <asm-generic/timex.h> |
272 |
+ |
273 |
+ #endif |
274 |
+diff --git a/arch/um/include/asm/timex.h b/arch/um/include/asm/timex.h |
275 |
+index e392a9a5bc9bd..9f27176adb26d 100644 |
276 |
+--- a/arch/um/include/asm/timex.h |
277 |
++++ b/arch/um/include/asm/timex.h |
278 |
+@@ -2,13 +2,8 @@ |
279 |
+ #ifndef __UM_TIMEX_H |
280 |
+ #define __UM_TIMEX_H |
281 |
+ |
282 |
+-typedef unsigned long cycles_t; |
283 |
+- |
284 |
+-static inline cycles_t get_cycles (void) |
285 |
+-{ |
286 |
+- return 0; |
287 |
+-} |
288 |
+- |
289 |
+ #define CLOCK_TICK_RATE (HZ) |
290 |
+ |
291 |
++#include <asm-generic/timex.h> |
292 |
++ |
293 |
+ #endif |
294 |
+diff --git a/arch/x86/include/asm/timex.h b/arch/x86/include/asm/timex.h |
295 |
+index a4a8b1b16c0c1..956e4145311b1 100644 |
296 |
+--- a/arch/x86/include/asm/timex.h |
297 |
++++ b/arch/x86/include/asm/timex.h |
298 |
+@@ -5,6 +5,15 @@ |
299 |
+ #include <asm/processor.h> |
300 |
+ #include <asm/tsc.h> |
301 |
+ |
302 |
++static inline unsigned long random_get_entropy(void) |
303 |
++{ |
304 |
++ if (!IS_ENABLED(CONFIG_X86_TSC) && |
305 |
++ !cpu_feature_enabled(X86_FEATURE_TSC)) |
306 |
++ return random_get_entropy_fallback(); |
307 |
++ return rdtsc(); |
308 |
++} |
309 |
++#define random_get_entropy random_get_entropy |
310 |
++ |
311 |
+ /* Assume we use the PIT time source for the clock tick */ |
312 |
+ #define CLOCK_TICK_RATE PIT_TICK_RATE |
313 |
+ |
314 |
+diff --git a/arch/x86/include/asm/tsc.h b/arch/x86/include/asm/tsc.h |
315 |
+index 01a300a9700b9..fbdc3d9514943 100644 |
316 |
+--- a/arch/x86/include/asm/tsc.h |
317 |
++++ b/arch/x86/include/asm/tsc.h |
318 |
+@@ -20,13 +20,12 @@ extern void disable_TSC(void); |
319 |
+ |
320 |
+ static inline cycles_t get_cycles(void) |
321 |
+ { |
322 |
+-#ifndef CONFIG_X86_TSC |
323 |
+- if (!boot_cpu_has(X86_FEATURE_TSC)) |
324 |
++ if (!IS_ENABLED(CONFIG_X86_TSC) && |
325 |
++ !cpu_feature_enabled(X86_FEATURE_TSC)) |
326 |
+ return 0; |
327 |
+-#endif |
328 |
+- |
329 |
+ return rdtsc(); |
330 |
+ } |
331 |
++#define get_cycles get_cycles |
332 |
+ |
333 |
+ extern struct system_counterval_t convert_art_to_tsc(u64 art); |
334 |
+ extern struct system_counterval_t convert_art_ns_to_tsc(u64 art_ns); |
335 |
+diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c |
336 |
+index 32333dfc85b6a..495329ae6b1b2 100644 |
337 |
+--- a/arch/x86/kvm/mmu/mmu.c |
338 |
++++ b/arch/x86/kvm/mmu/mmu.c |
339 |
+@@ -5416,14 +5416,16 @@ void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid) |
340 |
+ uint i; |
341 |
+ |
342 |
+ if (pcid == kvm_get_active_pcid(vcpu)) { |
343 |
+- mmu->invlpg(vcpu, gva, mmu->root_hpa); |
344 |
++ if (mmu->invlpg) |
345 |
++ mmu->invlpg(vcpu, gva, mmu->root_hpa); |
346 |
+ tlb_flush = true; |
347 |
+ } |
348 |
+ |
349 |
+ for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
350 |
+ if (VALID_PAGE(mmu->prev_roots[i].hpa) && |
351 |
+ pcid == kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd)) { |
352 |
+- mmu->invlpg(vcpu, gva, mmu->prev_roots[i].hpa); |
353 |
++ if (mmu->invlpg) |
354 |
++ mmu->invlpg(vcpu, gva, mmu->prev_roots[i].hpa); |
355 |
+ tlb_flush = true; |
356 |
+ } |
357 |
+ } |
358 |
+diff --git a/arch/xtensa/include/asm/timex.h b/arch/xtensa/include/asm/timex.h |
359 |
+index 233ec75e60c69..3f2462f2d0270 100644 |
360 |
+--- a/arch/xtensa/include/asm/timex.h |
361 |
++++ b/arch/xtensa/include/asm/timex.h |
362 |
+@@ -29,10 +29,6 @@ |
363 |
+ |
364 |
+ extern unsigned long ccount_freq; |
365 |
+ |
366 |
+-typedef unsigned long long cycles_t; |
367 |
+- |
368 |
+-#define get_cycles() (0) |
369 |
+- |
370 |
+ void local_timer_setup(unsigned cpu); |
371 |
+ |
372 |
+ /* |
373 |
+@@ -59,4 +55,6 @@ static inline void set_linux_timer (unsigned long ccompare) |
374 |
+ xtensa_set_sr(ccompare, SREG_CCOMPARE + LINUX_TIMER); |
375 |
+ } |
376 |
+ |
377 |
++#include <asm-generic/timex.h> |
378 |
++ |
379 |
+ #endif /* _XTENSA_TIMEX_H */ |
380 |
+diff --git a/drivers/acpi/sysfs.c b/drivers/acpi/sysfs.c |
381 |
+index a4b638bea6f16..cc2fe0618178e 100644 |
382 |
+--- a/drivers/acpi/sysfs.c |
383 |
++++ b/drivers/acpi/sysfs.c |
384 |
+@@ -415,19 +415,30 @@ static ssize_t acpi_data_show(struct file *filp, struct kobject *kobj, |
385 |
+ loff_t offset, size_t count) |
386 |
+ { |
387 |
+ struct acpi_data_attr *data_attr; |
388 |
+- void *base; |
389 |
+- ssize_t rc; |
390 |
++ void __iomem *base; |
391 |
++ ssize_t size; |
392 |
+ |
393 |
+ data_attr = container_of(bin_attr, struct acpi_data_attr, attr); |
394 |
++ size = data_attr->attr.size; |
395 |
++ |
396 |
++ if (offset < 0) |
397 |
++ return -EINVAL; |
398 |
++ |
399 |
++ if (offset >= size) |
400 |
++ return 0; |
401 |
+ |
402 |
+- base = acpi_os_map_memory(data_attr->addr, data_attr->attr.size); |
403 |
++ if (count > size - offset) |
404 |
++ count = size - offset; |
405 |
++ |
406 |
++ base = acpi_os_map_iomem(data_attr->addr, size); |
407 |
+ if (!base) |
408 |
+ return -ENOMEM; |
409 |
+- rc = memory_read_from_buffer(buf, count, &offset, base, |
410 |
+- data_attr->attr.size); |
411 |
+- acpi_os_unmap_memory(base, data_attr->attr.size); |
412 |
+ |
413 |
+- return rc; |
414 |
++ memcpy_fromio(buf, base + offset, count); |
415 |
++ |
416 |
++ acpi_os_unmap_iomem(base, size); |
417 |
++ |
418 |
++ return count; |
419 |
+ } |
420 |
+ |
421 |
+ static int acpi_bert_data_init(void *th, struct acpi_data_attr *data_attr) |
422 |
+diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig |
423 |
+index 740811893c570..55f48375e3fe5 100644 |
424 |
+--- a/drivers/char/Kconfig |
425 |
++++ b/drivers/char/Kconfig |
426 |
+@@ -449,6 +449,7 @@ config RANDOM_TRUST_BOOTLOADER |
427 |
+ device randomness. Say Y here to assume the entropy provided by the |
428 |
+ booloader is trustworthy so it will be added to the kernel's entropy |
429 |
+ pool. Otherwise, say N here so it will be regarded as device input that |
430 |
+- only mixes the entropy pool. |
431 |
++ only mixes the entropy pool. This can also be configured at boot with |
432 |
++ "random.trust_bootloader=on/off". |
433 |
+ |
434 |
+ endmenu |
435 |
+diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c |
436 |
+index a3db27916256d..cfb085de876b7 100644 |
437 |
+--- a/drivers/char/hw_random/core.c |
438 |
++++ b/drivers/char/hw_random/core.c |
439 |
+@@ -15,6 +15,7 @@ |
440 |
+ #include <linux/err.h> |
441 |
+ #include <linux/fs.h> |
442 |
+ #include <linux/hw_random.h> |
443 |
++#include <linux/random.h> |
444 |
+ #include <linux/kernel.h> |
445 |
+ #include <linux/kthread.h> |
446 |
+ #include <linux/sched/signal.h> |
447 |
+diff --git a/drivers/char/random.c b/drivers/char/random.c |
448 |
+index 3404a91edf292..92428bfdc1431 100644 |
449 |
+--- a/drivers/char/random.c |
450 |
++++ b/drivers/char/random.c |
451 |
+@@ -1,320 +1,26 @@ |
452 |
++// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) |
453 |
+ /* |
454 |
+- * random.c -- A strong random number generator |
455 |
+- * |
456 |
+ * Copyright (C) 2017-2022 Jason A. Donenfeld <Jason@×××××.com>. All Rights Reserved. |
457 |
+- * |
458 |
+ * Copyright Matt Mackall <mpm@×××××××.com>, 2003, 2004, 2005 |
459 |
+- * |
460 |
+- * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All |
461 |
+- * rights reserved. |
462 |
+- * |
463 |
+- * Redistribution and use in source and binary forms, with or without |
464 |
+- * modification, are permitted provided that the following conditions |
465 |
+- * are met: |
466 |
+- * 1. Redistributions of source code must retain the above copyright |
467 |
+- * notice, and the entire permission notice in its entirety, |
468 |
+- * including the disclaimer of warranties. |
469 |
+- * 2. Redistributions in binary form must reproduce the above copyright |
470 |
+- * notice, this list of conditions and the following disclaimer in the |
471 |
+- * documentation and/or other materials provided with the distribution. |
472 |
+- * 3. The name of the author may not be used to endorse or promote |
473 |
+- * products derived from this software without specific prior |
474 |
+- * written permission. |
475 |
+- * |
476 |
+- * ALTERNATIVELY, this product may be distributed under the terms of |
477 |
+- * the GNU General Public License, in which case the provisions of the GPL are |
478 |
+- * required INSTEAD OF the above restrictions. (This clause is |
479 |
+- * necessary due to a potential bad interaction between the GPL and |
480 |
+- * the restrictions contained in a BSD-style copyright.) |
481 |
+- * |
482 |
+- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
483 |
+- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
484 |
+- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
485 |
+- * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
486 |
+- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
487 |
+- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
488 |
+- * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
489 |
+- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
490 |
+- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
491 |
+- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
492 |
+- * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
493 |
+- * DAMAGE. |
494 |
+- */ |
495 |
+- |
496 |
+-/* |
497 |
+- * (now, with legal B.S. out of the way.....) |
498 |
+- * |
499 |
+- * This routine gathers environmental noise from device drivers, etc., |
500 |
+- * and returns good random numbers, suitable for cryptographic use. |
501 |
+- * Besides the obvious cryptographic uses, these numbers are also good |
502 |
+- * for seeding TCP sequence numbers, and other places where it is |
503 |
+- * desirable to have numbers which are not only random, but hard to |
504 |
+- * predict by an attacker. |
505 |
+- * |
506 |
+- * Theory of operation |
507 |
+- * =================== |
508 |
+- * |
509 |
+- * Computers are very predictable devices. Hence it is extremely hard |
510 |
+- * to produce truly random numbers on a computer --- as opposed to |
511 |
+- * pseudo-random numbers, which can easily generated by using a |
512 |
+- * algorithm. Unfortunately, it is very easy for attackers to guess |
513 |
+- * the sequence of pseudo-random number generators, and for some |
514 |
+- * applications this is not acceptable. So instead, we must try to |
515 |
+- * gather "environmental noise" from the computer's environment, which |
516 |
+- * must be hard for outside attackers to observe, and use that to |
517 |
+- * generate random numbers. In a Unix environment, this is best done |
518 |
+- * from inside the kernel. |
519 |
+- * |
520 |
+- * Sources of randomness from the environment include inter-keyboard |
521 |
+- * timings, inter-interrupt timings from some interrupts, and other |
522 |
+- * events which are both (a) non-deterministic and (b) hard for an |
523 |
+- * outside observer to measure. Randomness from these sources are |
524 |
+- * added to an "entropy pool", which is mixed using a CRC-like function. |
525 |
+- * This is not cryptographically strong, but it is adequate assuming |
526 |
+- * the randomness is not chosen maliciously, and it is fast enough that |
527 |
+- * the overhead of doing it on every interrupt is very reasonable. |
528 |
+- * As random bytes are mixed into the entropy pool, the routines keep |
529 |
+- * an *estimate* of how many bits of randomness have been stored into |
530 |
+- * the random number generator's internal state. |
531 |
+- * |
532 |
+- * When random bytes are desired, they are obtained by taking the BLAKE2s |
533 |
+- * hash of the contents of the "entropy pool". The BLAKE2s hash avoids |
534 |
+- * exposing the internal state of the entropy pool. It is believed to |
535 |
+- * be computationally infeasible to derive any useful information |
536 |
+- * about the input of BLAKE2s from its output. Even if it is possible to |
537 |
+- * analyze BLAKE2s in some clever way, as long as the amount of data |
538 |
+- * returned from the generator is less than the inherent entropy in |
539 |
+- * the pool, the output data is totally unpredictable. For this |
540 |
+- * reason, the routine decreases its internal estimate of how many |
541 |
+- * bits of "true randomness" are contained in the entropy pool as it |
542 |
+- * outputs random numbers. |
543 |
+- * |
544 |
+- * If this estimate goes to zero, the routine can still generate |
545 |
+- * random numbers; however, an attacker may (at least in theory) be |
546 |
+- * able to infer the future output of the generator from prior |
547 |
+- * outputs. This requires successful cryptanalysis of BLAKE2s, which is |
548 |
+- * not believed to be feasible, but there is a remote possibility. |
549 |
+- * Nonetheless, these numbers should be useful for the vast majority |
550 |
+- * of purposes. |
551 |
+- * |
552 |
+- * Exported interfaces ---- output |
553 |
+- * =============================== |
554 |
+- * |
555 |
+- * There are four exported interfaces; two for use within the kernel, |
556 |
+- * and two for use from userspace. |
557 |
+- * |
558 |
+- * Exported interfaces ---- userspace output |
559 |
+- * ----------------------------------------- |
560 |
+- * |
561 |
+- * The userspace interfaces are two character devices /dev/random and |
562 |
+- * /dev/urandom. /dev/random is suitable for use when very high |
563 |
+- * quality randomness is desired (for example, for key generation or |
564 |
+- * one-time pads), as it will only return a maximum of the number of |
565 |
+- * bits of randomness (as estimated by the random number generator) |
566 |
+- * contained in the entropy pool. |
567 |
+- * |
568 |
+- * The /dev/urandom device does not have this limit, and will return |
569 |
+- * as many bytes as are requested. As more and more random bytes are |
570 |
+- * requested without giving time for the entropy pool to recharge, |
571 |
+- * this will result in random numbers that are merely cryptographically |
572 |
+- * strong. For many applications, however, this is acceptable. |
573 |
+- * |
574 |
+- * Exported interfaces ---- kernel output |
575 |
+- * -------------------------------------- |
576 |
+- * |
577 |
+- * The primary kernel interface is |
578 |
+- * |
579 |
+- * void get_random_bytes(void *buf, int nbytes); |
580 |
+- * |
581 |
+- * This interface will return the requested number of random bytes, |
582 |
+- * and place it in the requested buffer. This is equivalent to a |
583 |
+- * read from /dev/urandom. |
584 |
+- * |
585 |
+- * For less critical applications, there are the functions: |
586 |
+- * |
587 |
+- * u32 get_random_u32() |
588 |
+- * u64 get_random_u64() |
589 |
+- * unsigned int get_random_int() |
590 |
+- * unsigned long get_random_long() |
591 |
+- * |
592 |
+- * These are produced by a cryptographic RNG seeded from get_random_bytes, |
593 |
+- * and so do not deplete the entropy pool as much. These are recommended |
594 |
+- * for most in-kernel operations *if the result is going to be stored in |
595 |
+- * the kernel*. |
596 |
+- * |
597 |
+- * Specifically, the get_random_int() family do not attempt to do |
598 |
+- * "anti-backtracking". If you capture the state of the kernel (e.g. |
599 |
+- * by snapshotting the VM), you can figure out previous get_random_int() |
600 |
+- * return values. But if the value is stored in the kernel anyway, |
601 |
+- * this is not a problem. |
602 |
+- * |
603 |
+- * It *is* safe to expose get_random_int() output to attackers (e.g. as |
604 |
+- * network cookies); given outputs 1..n, it's not feasible to predict |
605 |
+- * outputs 0 or n+1. The only concern is an attacker who breaks into |
606 |
+- * the kernel later; the get_random_int() engine is not reseeded as |
607 |
+- * often as the get_random_bytes() one. |
608 |
+- * |
609 |
+- * get_random_bytes() is needed for keys that need to stay secret after |
610 |
+- * they are erased from the kernel. For example, any key that will |
611 |
+- * be wrapped and stored encrypted. And session encryption keys: we'd |
612 |
+- * like to know that after the session is closed and the keys erased, |
613 |
+- * the plaintext is unrecoverable to someone who recorded the ciphertext. |
614 |
+- * |
615 |
+- * But for network ports/cookies, stack canaries, PRNG seeds, address |
616 |
+- * space layout randomization, session *authentication* keys, or other |
617 |
+- * applications where the sensitive data is stored in the kernel in |
618 |
+- * plaintext for as long as it's sensitive, the get_random_int() family |
619 |
+- * is just fine. |
620 |
+- * |
621 |
+- * Consider ASLR. We want to keep the address space secret from an |
622 |
+- * outside attacker while the process is running, but once the address |
623 |
+- * space is torn down, it's of no use to an attacker any more. And it's |
624 |
+- * stored in kernel data structures as long as it's alive, so worrying |
625 |
+- * about an attacker's ability to extrapolate it from the get_random_int() |
626 |
+- * CRNG is silly. |
627 |
+- * |
628 |
+- * Even some cryptographic keys are safe to generate with get_random_int(). |
629 |
+- * In particular, keys for SipHash are generally fine. Here, knowledge |
630 |
+- * of the key authorizes you to do something to a kernel object (inject |
631 |
+- * packets to a network connection, or flood a hash table), and the |
632 |
+- * key is stored with the object being protected. Once it goes away, |
633 |
+- * we no longer care if anyone knows the key. |
634 |
+- * |
635 |
+- * prandom_u32() |
636 |
+- * ------------- |
637 |
+- * |
638 |
+- * For even weaker applications, see the pseudorandom generator |
639 |
+- * prandom_u32(), prandom_max(), and prandom_bytes(). If the random |
640 |
+- * numbers aren't security-critical at all, these are *far* cheaper. |
641 |
+- * Useful for self-tests, random error simulation, randomized backoffs, |
642 |
+- * and any other application where you trust that nobody is trying to |
643 |
+- * maliciously mess with you by guessing the "random" numbers. |
644 |
+- * |
645 |
+- * Exported interfaces ---- input |
646 |
+- * ============================== |
647 |
+- * |
648 |
+- * The current exported interfaces for gathering environmental noise |
649 |
+- * from the devices are: |
650 |
+- * |
651 |
+- * void add_device_randomness(const void *buf, unsigned int size); |
652 |
+- * void add_input_randomness(unsigned int type, unsigned int code, |
653 |
+- * unsigned int value); |
654 |
+- * void add_interrupt_randomness(int irq); |
655 |
+- * void add_disk_randomness(struct gendisk *disk); |
656 |
+- * void add_hwgenerator_randomness(const char *buffer, size_t count, |
657 |
+- * size_t entropy); |
658 |
+- * void add_bootloader_randomness(const void *buf, unsigned int size); |
659 |
+- * |
660 |
+- * add_device_randomness() is for adding data to the random pool that |
661 |
+- * is likely to differ between two devices (or possibly even per boot). |
662 |
+- * This would be things like MAC addresses or serial numbers, or the |
663 |
+- * read-out of the RTC. This does *not* add any actual entropy to the |
664 |
+- * pool, but it initializes the pool to different values for devices |
665 |
+- * that might otherwise be identical and have very little entropy |
666 |
+- * available to them (particularly common in the embedded world). |
667 |
+- * |
668 |
+- * add_input_randomness() uses the input layer interrupt timing, as well as |
669 |
+- * the event type information from the hardware. |
670 |
+- * |
671 |
+- * add_interrupt_randomness() uses the interrupt timing as random |
672 |
+- * inputs to the entropy pool. Using the cycle counters and the irq source |
673 |
+- * as inputs, it feeds the randomness roughly once a second. |
674 |
+- * |
675 |
+- * add_disk_randomness() uses what amounts to the seek time of block |
676 |
+- * layer request events, on a per-disk_devt basis, as input to the |
677 |
+- * entropy pool. Note that high-speed solid state drives with very low |
678 |
+- * seek times do not make for good sources of entropy, as their seek |
679 |
+- * times are usually fairly consistent. |
680 |
+- * |
681 |
+- * All of these routines try to estimate how many bits of randomness a |
682 |
+- * particular randomness source. They do this by keeping track of the |
683 |
+- * first and second order deltas of the event timings. |
684 |
+- * |
685 |
+- * add_hwgenerator_randomness() is for true hardware RNGs, and will credit |
686 |
+- * entropy as specified by the caller. If the entropy pool is full it will |
687 |
+- * block until more entropy is needed. |
688 |
+- * |
689 |
+- * add_bootloader_randomness() is the same as add_hwgenerator_randomness() or |
690 |
+- * add_device_randomness(), depending on whether or not the configuration |
691 |
+- * option CONFIG_RANDOM_TRUST_BOOTLOADER is set. |
692 |
+- * |
693 |
+- * Ensuring unpredictability at system startup |
694 |
+- * ============================================ |
695 |
+- * |
696 |
+- * When any operating system starts up, it will go through a sequence |
697 |
+- * of actions that are fairly predictable by an adversary, especially |
698 |
+- * if the start-up does not involve interaction with a human operator. |
699 |
+- * This reduces the actual number of bits of unpredictability in the |
700 |
+- * entropy pool below the value in entropy_count. In order to |
701 |
+- * counteract this effect, it helps to carry information in the |
702 |
+- * entropy pool across shut-downs and start-ups. To do this, put the |
703 |
+- * following lines an appropriate script which is run during the boot |
704 |
+- * sequence: |
705 |
+- * |
706 |
+- * echo "Initializing random number generator..." |
707 |
+- * random_seed=/var/run/random-seed |
708 |
+- * # Carry a random seed from start-up to start-up |
709 |
+- * # Load and then save the whole entropy pool |
710 |
+- * if [ -f $random_seed ]; then |
711 |
+- * cat $random_seed >/dev/urandom |
712 |
+- * else |
713 |
+- * touch $random_seed |
714 |
+- * fi |
715 |
+- * chmod 600 $random_seed |
716 |
+- * dd if=/dev/urandom of=$random_seed count=1 bs=512 |
717 |
+- * |
718 |
+- * and the following lines in an appropriate script which is run as |
719 |
+- * the system is shutdown: |
720 |
+- * |
721 |
+- * # Carry a random seed from shut-down to start-up |
722 |
+- * # Save the whole entropy pool |
723 |
+- * echo "Saving random seed..." |
724 |
+- * random_seed=/var/run/random-seed |
725 |
+- * touch $random_seed |
726 |
+- * chmod 600 $random_seed |
727 |
+- * dd if=/dev/urandom of=$random_seed count=1 bs=512 |
728 |
+- * |
729 |
+- * For example, on most modern systems using the System V init |
730 |
+- * scripts, such code fragments would be found in |
731 |
+- * /etc/rc.d/init.d/random. On older Linux systems, the correct script |
732 |
+- * location might be in /etc/rcb.d/rc.local or /etc/rc.d/rc.0. |
733 |
+- * |
734 |
+- * Effectively, these commands cause the contents of the entropy pool |
735 |
+- * to be saved at shut-down time and reloaded into the entropy pool at |
736 |
+- * start-up. (The 'dd' in the addition to the bootup script is to |
737 |
+- * make sure that /etc/random-seed is different for every start-up, |
738 |
+- * even if the system crashes without executing rc.0.) Even with |
739 |
+- * complete knowledge of the start-up activities, predicting the state |
740 |
+- * of the entropy pool requires knowledge of the previous history of |
741 |
+- * the system. |
742 |
+- * |
743 |
+- * Configuring the /dev/random driver under Linux |
744 |
+- * ============================================== |
745 |
+- * |
746 |
+- * The /dev/random driver under Linux uses minor numbers 8 and 9 of |
747 |
+- * the /dev/mem major number (#1). So if your system does not have |
748 |
+- * /dev/random and /dev/urandom created already, they can be created |
749 |
+- * by using the commands: |
750 |
+- * |
751 |
+- * mknod /dev/random c 1 8 |
752 |
+- * mknod /dev/urandom c 1 9 |
753 |
+- * |
754 |
+- * Acknowledgements: |
755 |
+- * ================= |
756 |
+- * |
757 |
+- * Ideas for constructing this random number generator were derived |
758 |
+- * from Pretty Good Privacy's random number generator, and from private |
759 |
+- * discussions with Phil Karn. Colin Plumb provided a faster random |
760 |
+- * number generator, which speed up the mixing function of the entropy |
761 |
+- * pool, taken from PGPfone. Dale Worley has also contributed many |
762 |
+- * useful ideas and suggestions to improve this driver. |
763 |
+- * |
764 |
+- * Any flaws in the design are solely my responsibility, and should |
765 |
+- * not be attributed to the Phil, Colin, or any of authors of PGP. |
766 |
+- * |
767 |
+- * Further background information on this topic may be obtained from |
768 |
+- * RFC 1750, "Randomness Recommendations for Security", by Donald |
769 |
+- * Eastlake, Steve Crocker, and Jeff Schiller. |
770 |
++ * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All rights reserved. |
771 |
++ * |
772 |
++ * This driver produces cryptographically secure pseudorandom data. It is divided |
773 |
++ * into roughly six sections, each with a section header: |
774 |
++ * |
775 |
++ * - Initialization and readiness waiting. |
776 |
++ * - Fast key erasure RNG, the "crng". |
777 |
++ * - Entropy accumulation and extraction routines. |
778 |
++ * - Entropy collection routines. |
779 |
++ * - Userspace reader/writer interfaces. |
780 |
++ * - Sysctl interface. |
781 |
++ * |
782 |
++ * The high level overview is that there is one input pool, into which |
783 |
++ * various pieces of data are hashed. Prior to initialization, some of that |
784 |
++ * data is then "credited" as having a certain number of bits of entropy. |
785 |
++ * When enough bits of entropy are available, the hash is finalized and |
786 |
++ * handed as a key to a stream cipher that expands it indefinitely for |
787 |
++ * various consumers. This key is periodically refreshed as the various |
788 |
++ * entropy collectors, described below, add data to the input pool. |
789 |
+ */ |
790 |
+ |
791 |
+ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
792 |
+@@ -344,1371 +50,1080 @@ |
793 |
+ #include <linux/syscalls.h> |
794 |
+ #include <linux/completion.h> |
795 |
+ #include <linux/uuid.h> |
796 |
++#include <linux/uaccess.h> |
797 |
++#include <linux/siphash.h> |
798 |
++#include <linux/uio.h> |
799 |
+ #include <crypto/chacha.h> |
800 |
+ #include <crypto/blake2s.h> |
801 |
+- |
802 |
+ #include <asm/processor.h> |
803 |
+-#include <linux/uaccess.h> |
804 |
+ #include <asm/irq.h> |
805 |
+ #include <asm/irq_regs.h> |
806 |
+ #include <asm/io.h> |
807 |
+ |
808 |
+-#define CREATE_TRACE_POINTS |
809 |
+-#include <trace/events/random.h> |
810 |
+- |
811 |
+-/* #define ADD_INTERRUPT_BENCH */ |
812 |
+- |
813 |
+-/* |
814 |
+- * If the entropy count falls under this number of bits, then we |
815 |
+- * should wake up processes which are selecting or polling on write |
816 |
+- * access to /dev/random. |
817 |
+- */ |
818 |
+-static int random_write_wakeup_bits = 28 * (1 << 5); |
819 |
+- |
820 |
+-/* |
821 |
+- * Originally, we used a primitive polynomial of degree .poolwords |
822 |
+- * over GF(2). The taps for various sizes are defined below. They |
823 |
+- * were chosen to be evenly spaced except for the last tap, which is 1 |
824 |
+- * to get the twisting happening as fast as possible. |
825 |
+- * |
826 |
+- * For the purposes of better mixing, we use the CRC-32 polynomial as |
827 |
+- * well to make a (modified) twisted Generalized Feedback Shift |
828 |
+- * Register. (See M. Matsumoto & Y. Kurita, 1992. Twisted GFSR |
829 |
+- * generators. ACM Transactions on Modeling and Computer Simulation |
830 |
+- * 2(3):179-194. Also see M. Matsumoto & Y. Kurita, 1994. Twisted |
831 |
+- * GFSR generators II. ACM Transactions on Modeling and Computer |
832 |
+- * Simulation 4:254-266) |
833 |
++/********************************************************************* |
834 |
+ * |
835 |
+- * Thanks to Colin Plumb for suggesting this. |
836 |
++ * Initialization and readiness waiting. |
837 |
+ * |
838 |
+- * The mixing operation is much less sensitive than the output hash, |
839 |
+- * where we use BLAKE2s. All that we want of mixing operation is that |
840 |
+- * it be a good non-cryptographic hash; i.e. it not produce collisions |
841 |
+- * when fed "random" data of the sort we expect to see. As long as |
842 |
+- * the pool state differs for different inputs, we have preserved the |
843 |
+- * input entropy and done a good job. The fact that an intelligent |
844 |
+- * attacker can construct inputs that will produce controlled |
845 |
+- * alterations to the pool's state is not important because we don't |
846 |
+- * consider such inputs to contribute any randomness. The only |
847 |
+- * property we need with respect to them is that the attacker can't |
848 |
+- * increase his/her knowledge of the pool's state. Since all |
849 |
+- * additions are reversible (knowing the final state and the input, |
850 |
+- * you can reconstruct the initial state), if an attacker has any |
851 |
+- * uncertainty about the initial state, he/she can only shuffle that |
852 |
+- * uncertainty about, but never cause any collisions (which would |
853 |
+- * decrease the uncertainty). |
854 |
++ * Much of the RNG infrastructure is devoted to various dependencies |
855 |
++ * being able to wait until the RNG has collected enough entropy and |
856 |
++ * is ready for safe consumption. |
857 |
+ * |
858 |
+- * Our mixing functions were analyzed by Lacharme, Roeck, Strubel, and |
859 |
+- * Videau in their paper, "The Linux Pseudorandom Number Generator |
860 |
+- * Revisited" (see: http://eprint.iacr.org/2012/251.pdf). In their |
861 |
+- * paper, they point out that we are not using a true Twisted GFSR, |
862 |
+- * since Matsumoto & Kurita used a trinomial feedback polynomial (that |
863 |
+- * is, with only three taps, instead of the six that we are using). |
864 |
+- * As a result, the resulting polynomial is neither primitive nor |
865 |
+- * irreducible, and hence does not have a maximal period over |
866 |
+- * GF(2**32). They suggest a slight change to the generator |
867 |
+- * polynomial which improves the resulting TGFSR polynomial to be |
868 |
+- * irreducible, which we have made here. |
869 |
+- */ |
870 |
+-enum poolinfo { |
871 |
+- POOL_WORDS = 128, |
872 |
+- POOL_WORDMASK = POOL_WORDS - 1, |
873 |
+- POOL_BYTES = POOL_WORDS * sizeof(u32), |
874 |
+- POOL_BITS = POOL_BYTES * 8, |
875 |
+- POOL_BITSHIFT = ilog2(POOL_BITS), |
876 |
+- |
877 |
+- /* To allow fractional bits to be tracked, the entropy_count field is |
878 |
+- * denominated in units of 1/8th bits. */ |
879 |
+- POOL_ENTROPY_SHIFT = 3, |
880 |
+-#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT) |
881 |
+- POOL_FRACBITS = POOL_BITS << POOL_ENTROPY_SHIFT, |
882 |
+- |
883 |
+- /* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ |
884 |
+- POOL_TAP1 = 104, |
885 |
+- POOL_TAP2 = 76, |
886 |
+- POOL_TAP3 = 51, |
887 |
+- POOL_TAP4 = 25, |
888 |
+- POOL_TAP5 = 1, |
889 |
+- |
890 |
+- EXTRACT_SIZE = BLAKE2S_HASH_SIZE / 2 |
891 |
+-}; |
892 |
++ *********************************************************************/ |
893 |
+ |
894 |
+ /* |
895 |
+- * Static global variables |
896 |
++ * crng_init is protected by base_crng->lock, and only increases |
897 |
++ * its value (from empty->early->ready). |
898 |
+ */ |
899 |
+-static DECLARE_WAIT_QUEUE_HEAD(random_write_wait); |
900 |
++static enum { |
901 |
++ CRNG_EMPTY = 0, /* Little to no entropy collected */ |
902 |
++ CRNG_EARLY = 1, /* At least POOL_EARLY_BITS collected */ |
903 |
++ CRNG_READY = 2 /* Fully initialized with POOL_READY_BITS collected */ |
904 |
++} crng_init __read_mostly = CRNG_EMPTY; |
905 |
++static DEFINE_STATIC_KEY_FALSE(crng_is_ready); |
906 |
++#define crng_ready() (static_branch_likely(&crng_is_ready) || crng_init >= CRNG_READY) |
907 |
++/* Various types of waiters for crng_init->CRNG_READY transition. */ |
908 |
++static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); |
909 |
+ static struct fasync_struct *fasync; |
910 |
++static DEFINE_SPINLOCK(random_ready_chain_lock); |
911 |
++static RAW_NOTIFIER_HEAD(random_ready_chain); |
912 |
+ |
913 |
+-static DEFINE_SPINLOCK(random_ready_list_lock); |
914 |
+-static LIST_HEAD(random_ready_list); |
915 |
+- |
916 |
+-struct crng_state { |
917 |
+- u32 state[16]; |
918 |
+- unsigned long init_time; |
919 |
+- spinlock_t lock; |
920 |
+-}; |
921 |
+- |
922 |
+-static struct crng_state primary_crng = { |
923 |
+- .lock = __SPIN_LOCK_UNLOCKED(primary_crng.lock), |
924 |
+- .state[0] = CHACHA_CONSTANT_EXPA, |
925 |
+- .state[1] = CHACHA_CONSTANT_ND_3, |
926 |
+- .state[2] = CHACHA_CONSTANT_2_BY, |
927 |
+- .state[3] = CHACHA_CONSTANT_TE_K, |
928 |
+-}; |
929 |
+- |
930 |
+-/* |
931 |
+- * crng_init = 0 --> Uninitialized |
932 |
+- * 1 --> Initialized |
933 |
+- * 2 --> Initialized from input_pool |
934 |
+- * |
935 |
+- * crng_init is protected by primary_crng->lock, and only increases |
936 |
+- * its value (from 0->1->2). |
937 |
+- */ |
938 |
+-static int crng_init = 0; |
939 |
+-static bool crng_need_final_init = false; |
940 |
+-#define crng_ready() (likely(crng_init > 1)) |
941 |
+-static int crng_init_cnt = 0; |
942 |
+-static unsigned long crng_global_init_time = 0; |
943 |
+-#define CRNG_INIT_CNT_THRESH (2 * CHACHA_KEY_SIZE) |
944 |
+-static void _extract_crng(struct crng_state *crng, u8 out[CHACHA_BLOCK_SIZE]); |
945 |
+-static void _crng_backtrack_protect(struct crng_state *crng, |
946 |
+- u8 tmp[CHACHA_BLOCK_SIZE], int used); |
947 |
+-static void process_random_ready_list(void); |
948 |
+-static void _get_random_bytes(void *buf, int nbytes); |
949 |
+- |
950 |
+-static struct ratelimit_state unseeded_warning = |
951 |
+- RATELIMIT_STATE_INIT("warn_unseeded_randomness", HZ, 3); |
952 |
++/* Control how we warn userspace. */ |
953 |
+ static struct ratelimit_state urandom_warning = |
954 |
+ RATELIMIT_STATE_INIT("warn_urandom_randomness", HZ, 3); |
955 |
+- |
956 |
+-static int ratelimit_disable __read_mostly; |
957 |
+- |
958 |
++static int ratelimit_disable __read_mostly = |
959 |
++ IS_ENABLED(CONFIG_WARN_ALL_UNSEEDED_RANDOM); |
960 |
+ module_param_named(ratelimit_disable, ratelimit_disable, int, 0644); |
961 |
+ MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression"); |
962 |
+ |
963 |
+-/********************************************************************** |
964 |
+- * |
965 |
+- * OS independent entropy store. Here are the functions which handle |
966 |
+- * storing entropy in an entropy pool. |
967 |
+- * |
968 |
+- **********************************************************************/ |
969 |
+- |
970 |
+-static u32 input_pool_data[POOL_WORDS] __latent_entropy; |
971 |
+- |
972 |
+-static struct { |
973 |
+- spinlock_t lock; |
974 |
+- u16 add_ptr; |
975 |
+- u16 input_rotate; |
976 |
+- int entropy_count; |
977 |
+-} input_pool = { |
978 |
+- .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), |
979 |
+-}; |
980 |
+- |
981 |
+-static ssize_t extract_entropy(void *buf, size_t nbytes, int min); |
982 |
+-static ssize_t _extract_entropy(void *buf, size_t nbytes); |
983 |
+- |
984 |
+-static void crng_reseed(struct crng_state *crng, bool use_input_pool); |
985 |
+- |
986 |
+-static const u32 twist_table[8] = { |
987 |
+- 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158, |
988 |
+- 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 }; |
989 |
+- |
990 |
+ /* |
991 |
+- * This function adds bytes into the entropy "pool". It does not |
992 |
+- * update the entropy estimate. The caller should call |
993 |
+- * credit_entropy_bits if this is appropriate. |
994 |
++ * Returns whether or not the input pool has been seeded and thus guaranteed |
995 |
++ * to supply cryptographically secure random numbers. This applies to: the |
996 |
++ * /dev/urandom device, the get_random_bytes function, and the get_random_{u32, |
997 |
++ * ,u64,int,long} family of functions. |
998 |
+ * |
999 |
+- * The pool is stirred with a primitive polynomial of the appropriate |
1000 |
+- * degree, and then twisted. We twist by three bits at a time because |
1001 |
+- * it's cheap to do so and helps slightly in the expected case where |
1002 |
+- * the entropy is concentrated in the low-order bits. |
1003 |
++ * Returns: true if the input pool has been seeded. |
1004 |
++ * false if the input pool has not been seeded. |
1005 |
+ */ |
1006 |
+-static void _mix_pool_bytes(const void *in, int nbytes) |
1007 |
+-{ |
1008 |
+- unsigned long i; |
1009 |
+- int input_rotate; |
1010 |
+- const u8 *bytes = in; |
1011 |
+- u32 w; |
1012 |
+- |
1013 |
+- input_rotate = input_pool.input_rotate; |
1014 |
+- i = input_pool.add_ptr; |
1015 |
+- |
1016 |
+- /* mix one byte at a time to simplify size handling and churn faster */ |
1017 |
+- while (nbytes--) { |
1018 |
+- w = rol32(*bytes++, input_rotate); |
1019 |
+- i = (i - 1) & POOL_WORDMASK; |
1020 |
+- |
1021 |
+- /* XOR in the various taps */ |
1022 |
+- w ^= input_pool_data[i]; |
1023 |
+- w ^= input_pool_data[(i + POOL_TAP1) & POOL_WORDMASK]; |
1024 |
+- w ^= input_pool_data[(i + POOL_TAP2) & POOL_WORDMASK]; |
1025 |
+- w ^= input_pool_data[(i + POOL_TAP3) & POOL_WORDMASK]; |
1026 |
+- w ^= input_pool_data[(i + POOL_TAP4) & POOL_WORDMASK]; |
1027 |
+- w ^= input_pool_data[(i + POOL_TAP5) & POOL_WORDMASK]; |
1028 |
+- |
1029 |
+- /* Mix the result back in with a twist */ |
1030 |
+- input_pool_data[i] = (w >> 3) ^ twist_table[w & 7]; |
1031 |
+- |
1032 |
+- /* |
1033 |
+- * Normally, we add 7 bits of rotation to the pool. |
1034 |
+- * At the beginning of the pool, add an extra 7 bits |
1035 |
+- * rotation, so that successive passes spread the |
1036 |
+- * input bits across the pool evenly. |
1037 |
+- */ |
1038 |
+- input_rotate = (input_rotate + (i ? 7 : 14)) & 31; |
1039 |
+- } |
1040 |
+- |
1041 |
+- input_pool.input_rotate = input_rotate; |
1042 |
+- input_pool.add_ptr = i; |
1043 |
+-} |
1044 |
+- |
1045 |
+-static void __mix_pool_bytes(const void *in, int nbytes) |
1046 |
++bool rng_is_initialized(void) |
1047 |
+ { |
1048 |
+- trace_mix_pool_bytes_nolock(nbytes, _RET_IP_); |
1049 |
+- _mix_pool_bytes(in, nbytes); |
1050 |
++ return crng_ready(); |
1051 |
+ } |
1052 |
++EXPORT_SYMBOL(rng_is_initialized); |
1053 |
+ |
1054 |
+-static void mix_pool_bytes(const void *in, int nbytes) |
1055 |
++static void __cold crng_set_ready(struct work_struct *work) |
1056 |
+ { |
1057 |
+- unsigned long flags; |
1058 |
+- |
1059 |
+- trace_mix_pool_bytes(nbytes, _RET_IP_); |
1060 |
+- spin_lock_irqsave(&input_pool.lock, flags); |
1061 |
+- _mix_pool_bytes(in, nbytes); |
1062 |
+- spin_unlock_irqrestore(&input_pool.lock, flags); |
1063 |
++ static_branch_enable(&crng_is_ready); |
1064 |
+ } |
1065 |
+ |
1066 |
+-struct fast_pool { |
1067 |
+- u32 pool[4]; |
1068 |
+- unsigned long last; |
1069 |
+- u16 reg_idx; |
1070 |
+- u8 count; |
1071 |
+-}; |
1072 |
++/* Used by wait_for_random_bytes(), and considered an entropy collector, below. */ |
1073 |
++static void try_to_generate_entropy(void); |
1074 |
+ |
1075 |
+ /* |
1076 |
+- * This is a fast mixing routine used by the interrupt randomness |
1077 |
+- * collector. It's hardcoded for an 128 bit pool and assumes that any |
1078 |
+- * locks that might be needed are taken by the caller. |
1079 |
++ * Wait for the input pool to be seeded and thus guaranteed to supply |
1080 |
++ * cryptographically secure random numbers. This applies to: the /dev/urandom |
1081 |
++ * device, the get_random_bytes function, and the get_random_{u32,u64,int,long} |
1082 |
++ * family of functions. Using any of these functions without first calling |
1083 |
++ * this function forfeits the guarantee of security. |
1084 |
++ * |
1085 |
++ * Returns: 0 if the input pool has been seeded. |
1086 |
++ * -ERESTARTSYS if the function was interrupted by a signal. |
1087 |
+ */ |
1088 |
+-static void fast_mix(struct fast_pool *f) |
1089 |
++int wait_for_random_bytes(void) |
1090 |
+ { |
1091 |
+- u32 a = f->pool[0], b = f->pool[1]; |
1092 |
+- u32 c = f->pool[2], d = f->pool[3]; |
1093 |
+- |
1094 |
+- a += b; c += d; |
1095 |
+- b = rol32(b, 6); d = rol32(d, 27); |
1096 |
+- d ^= a; b ^= c; |
1097 |
+- |
1098 |
+- a += b; c += d; |
1099 |
+- b = rol32(b, 16); d = rol32(d, 14); |
1100 |
+- d ^= a; b ^= c; |
1101 |
+- |
1102 |
+- a += b; c += d; |
1103 |
+- b = rol32(b, 6); d = rol32(d, 27); |
1104 |
+- d ^= a; b ^= c; |
1105 |
+- |
1106 |
+- a += b; c += d; |
1107 |
+- b = rol32(b, 16); d = rol32(d, 14); |
1108 |
+- d ^= a; b ^= c; |
1109 |
++ while (!crng_ready()) { |
1110 |
++ int ret; |
1111 |
+ |
1112 |
+- f->pool[0] = a; f->pool[1] = b; |
1113 |
+- f->pool[2] = c; f->pool[3] = d; |
1114 |
+- f->count++; |
1115 |
++ try_to_generate_entropy(); |
1116 |
++ ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ); |
1117 |
++ if (ret) |
1118 |
++ return ret > 0 ? 0 : ret; |
1119 |
++ } |
1120 |
++ return 0; |
1121 |
+ } |
1122 |
++EXPORT_SYMBOL(wait_for_random_bytes); |
1123 |
+ |
1124 |
+-static void process_random_ready_list(void) |
1125 |
++/* |
1126 |
++ * Add a callback function that will be invoked when the input |
1127 |
++ * pool is initialised. |
1128 |
++ * |
1129 |
++ * returns: 0 if callback is successfully added |
1130 |
++ * -EALREADY if pool is already initialised (callback not called) |
1131 |
++ */ |
1132 |
++int __cold register_random_ready_notifier(struct notifier_block *nb) |
1133 |
+ { |
1134 |
+ unsigned long flags; |
1135 |
+- struct random_ready_callback *rdy, *tmp; |
1136 |
++ int ret = -EALREADY; |
1137 |
+ |
1138 |
+- spin_lock_irqsave(&random_ready_list_lock, flags); |
1139 |
+- list_for_each_entry_safe(rdy, tmp, &random_ready_list, list) { |
1140 |
+- struct module *owner = rdy->owner; |
1141 |
++ if (crng_ready()) |
1142 |
++ return ret; |
1143 |
+ |
1144 |
+- list_del_init(&rdy->list); |
1145 |
+- rdy->func(rdy); |
1146 |
+- module_put(owner); |
1147 |
+- } |
1148 |
+- spin_unlock_irqrestore(&random_ready_list_lock, flags); |
1149 |
++ spin_lock_irqsave(&random_ready_chain_lock, flags); |
1150 |
++ if (!crng_ready()) |
1151 |
++ ret = raw_notifier_chain_register(&random_ready_chain, nb); |
1152 |
++ spin_unlock_irqrestore(&random_ready_chain_lock, flags); |
1153 |
++ return ret; |
1154 |
+ } |
1155 |
+ |
1156 |
+ /* |
1157 |
+- * Credit (or debit) the entropy store with n bits of entropy. |
1158 |
+- * Use credit_entropy_bits_safe() if the value comes from userspace |
1159 |
+- * or otherwise should be checked for extreme values. |
1160 |
++ * Delete a previously registered readiness callback function. |
1161 |
+ */ |
1162 |
+-static void credit_entropy_bits(int nbits) |
1163 |
++int __cold unregister_random_ready_notifier(struct notifier_block *nb) |
1164 |
+ { |
1165 |
+- int entropy_count, entropy_bits, orig; |
1166 |
+- int nfrac = nbits << POOL_ENTROPY_SHIFT; |
1167 |
+- |
1168 |
+- /* Ensure that the multiplication can avoid being 64 bits wide. */ |
1169 |
+- BUILD_BUG_ON(2 * (POOL_ENTROPY_SHIFT + POOL_BITSHIFT) > 31); |
1170 |
+- |
1171 |
+- if (!nbits) |
1172 |
+- return; |
1173 |
+- |
1174 |
+-retry: |
1175 |
+- entropy_count = orig = READ_ONCE(input_pool.entropy_count); |
1176 |
+- if (nfrac < 0) { |
1177 |
+- /* Debit */ |
1178 |
+- entropy_count += nfrac; |
1179 |
+- } else { |
1180 |
+- /* |
1181 |
+- * Credit: we have to account for the possibility of |
1182 |
+- * overwriting already present entropy. Even in the |
1183 |
+- * ideal case of pure Shannon entropy, new contributions |
1184 |
+- * approach the full value asymptotically: |
1185 |
+- * |
1186 |
+- * entropy <- entropy + (pool_size - entropy) * |
1187 |
+- * (1 - exp(-add_entropy/pool_size)) |
1188 |
+- * |
1189 |
+- * For add_entropy <= pool_size/2 then |
1190 |
+- * (1 - exp(-add_entropy/pool_size)) >= |
1191 |
+- * (add_entropy/pool_size)*0.7869... |
1192 |
+- * so we can approximate the exponential with |
1193 |
+- * 3/4*add_entropy/pool_size and still be on the |
1194 |
+- * safe side by adding at most pool_size/2 at a time. |
1195 |
+- * |
1196 |
+- * The use of pool_size-2 in the while statement is to |
1197 |
+- * prevent rounding artifacts from making the loop |
1198 |
+- * arbitrarily long; this limits the loop to log2(pool_size)*2 |
1199 |
+- * turns no matter how large nbits is. |
1200 |
+- */ |
1201 |
+- int pnfrac = nfrac; |
1202 |
+- const int s = POOL_BITSHIFT + POOL_ENTROPY_SHIFT + 2; |
1203 |
+- /* The +2 corresponds to the /4 in the denominator */ |
1204 |
+- |
1205 |
+- do { |
1206 |
+- unsigned int anfrac = min(pnfrac, POOL_FRACBITS / 2); |
1207 |
+- unsigned int add = |
1208 |
+- ((POOL_FRACBITS - entropy_count) * anfrac * 3) >> s; |
1209 |
+- |
1210 |
+- entropy_count += add; |
1211 |
+- pnfrac -= anfrac; |
1212 |
+- } while (unlikely(entropy_count < POOL_FRACBITS - 2 && pnfrac)); |
1213 |
+- } |
1214 |
+- |
1215 |
+- if (WARN_ON(entropy_count < 0)) { |
1216 |
+- pr_warn("negative entropy/overflow: count %d\n", entropy_count); |
1217 |
+- entropy_count = 0; |
1218 |
+- } else if (entropy_count > POOL_FRACBITS) |
1219 |
+- entropy_count = POOL_FRACBITS; |
1220 |
+- if (cmpxchg(&input_pool.entropy_count, orig, entropy_count) != orig) |
1221 |
+- goto retry; |
1222 |
+- |
1223 |
+- trace_credit_entropy_bits(nbits, entropy_count >> POOL_ENTROPY_SHIFT, _RET_IP_); |
1224 |
++ unsigned long flags; |
1225 |
++ int ret; |
1226 |
+ |
1227 |
+- entropy_bits = entropy_count >> POOL_ENTROPY_SHIFT; |
1228 |
+- if (crng_init < 2 && entropy_bits >= 128) |
1229 |
+- crng_reseed(&primary_crng, true); |
1230 |
++ spin_lock_irqsave(&random_ready_chain_lock, flags); |
1231 |
++ ret = raw_notifier_chain_unregister(&random_ready_chain, nb); |
1232 |
++ spin_unlock_irqrestore(&random_ready_chain_lock, flags); |
1233 |
++ return ret; |
1234 |
+ } |
1235 |
+ |
1236 |
+-static int credit_entropy_bits_safe(int nbits) |
1237 |
++static void __cold process_random_ready_list(void) |
1238 |
+ { |
1239 |
+- if (nbits < 0) |
1240 |
+- return -EINVAL; |
1241 |
+- |
1242 |
+- /* Cap the value to avoid overflows */ |
1243 |
+- nbits = min(nbits, POOL_BITS); |
1244 |
++ unsigned long flags; |
1245 |
+ |
1246 |
+- credit_entropy_bits(nbits); |
1247 |
+- return 0; |
1248 |
++ spin_lock_irqsave(&random_ready_chain_lock, flags); |
1249 |
++ raw_notifier_call_chain(&random_ready_chain, 0, NULL); |
1250 |
++ spin_unlock_irqrestore(&random_ready_chain_lock, flags); |
1251 |
+ } |
1252 |
+ |
1253 |
++#define warn_unseeded_randomness() \ |
1254 |
++ if (IS_ENABLED(CONFIG_WARN_ALL_UNSEEDED_RANDOM) && !crng_ready()) \ |
1255 |
++ printk_deferred(KERN_NOTICE "random: %s called from %pS with crng_init=%d\n", \ |
1256 |
++ __func__, (void *)_RET_IP_, crng_init) |
1257 |
++ |
1258 |
++ |
1259 |
+ /********************************************************************* |
1260 |
+ * |
1261 |
+- * CRNG using CHACHA20 |
1262 |
++ * Fast key erasure RNG, the "crng". |
1263 |
++ * |
1264 |
++ * These functions expand entropy from the entropy extractor into |
1265 |
++ * long streams for external consumption using the "fast key erasure" |
1266 |
++ * RNG described at <https://blog.cr.yp.to/20170723-random.html>. |
1267 |
++ * |
1268 |
++ * There are a few exported interfaces for use by other drivers: |
1269 |
++ * |
1270 |
++ * void get_random_bytes(void *buf, size_t len) |
1271 |
++ * u32 get_random_u32() |
1272 |
++ * u64 get_random_u64() |
1273 |
++ * unsigned int get_random_int() |
1274 |
++ * unsigned long get_random_long() |
1275 |
++ * |
1276 |
++ * These interfaces will return the requested number of random bytes |
1277 |
++ * into the given buffer or as a return value. This is equivalent to |
1278 |
++ * a read from /dev/urandom. The u32, u64, int, and long family of |
1279 |
++ * functions may be higher performance for one-off random integers, |
1280 |
++ * because they do a bit of buffering and do not invoke reseeding |
1281 |
++ * until the buffer is emptied. |
1282 |
+ * |
1283 |
+ *********************************************************************/ |
1284 |
+ |
1285 |
+-#define CRNG_RESEED_INTERVAL (300 * HZ) |
1286 |
++enum { |
1287 |
++ CRNG_RESEED_START_INTERVAL = HZ, |
1288 |
++ CRNG_RESEED_INTERVAL = 60 * HZ |
1289 |
++}; |
1290 |
+ |
1291 |
+-static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); |
1292 |
++static struct { |
1293 |
++ u8 key[CHACHA_KEY_SIZE] __aligned(__alignof__(long)); |
1294 |
++ unsigned long birth; |
1295 |
++ unsigned long generation; |
1296 |
++ spinlock_t lock; |
1297 |
++} base_crng = { |
1298 |
++ .lock = __SPIN_LOCK_UNLOCKED(base_crng.lock) |
1299 |
++}; |
1300 |
+ |
1301 |
+-/* |
1302 |
+- * Hack to deal with crazy userspace progams when they are all trying |
1303 |
+- * to access /dev/urandom in parallel. The programs are almost |
1304 |
+- * certainly doing something terribly wrong, but we'll work around |
1305 |
+- * their brain damage. |
1306 |
+- */ |
1307 |
+-static struct crng_state **crng_node_pool __read_mostly; |
1308 |
++struct crng { |
1309 |
++ u8 key[CHACHA_KEY_SIZE]; |
1310 |
++ unsigned long generation; |
1311 |
++ local_lock_t lock; |
1312 |
++}; |
1313 |
+ |
1314 |
+-static void invalidate_batched_entropy(void); |
1315 |
+-static void numa_crng_init(void); |
1316 |
++static DEFINE_PER_CPU(struct crng, crngs) = { |
1317 |
++ .generation = ULONG_MAX, |
1318 |
++ .lock = INIT_LOCAL_LOCK(crngs.lock), |
1319 |
++}; |
1320 |
+ |
1321 |
+-static bool trust_cpu __ro_after_init = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU); |
1322 |
+-static int __init parse_trust_cpu(char *arg) |
1323 |
+-{ |
1324 |
+- return kstrtobool(arg, &trust_cpu); |
1325 |
+-} |
1326 |
+-early_param("random.trust_cpu", parse_trust_cpu); |
1327 |
++/* Used by crng_reseed() and crng_make_state() to extract a new seed from the input pool. */ |
1328 |
++static void extract_entropy(void *buf, size_t len); |
1329 |
+ |
1330 |
+-static bool crng_init_try_arch(struct crng_state *crng) |
1331 |
++/* This extracts a new crng key from the input pool. */ |
1332 |
++static void crng_reseed(void) |
1333 |
+ { |
1334 |
+- int i; |
1335 |
+- bool arch_init = true; |
1336 |
+- unsigned long rv; |
1337 |
+- |
1338 |
+- for (i = 4; i < 16; i++) { |
1339 |
+- if (!arch_get_random_seed_long(&rv) && |
1340 |
+- !arch_get_random_long(&rv)) { |
1341 |
+- rv = random_get_entropy(); |
1342 |
+- arch_init = false; |
1343 |
+- } |
1344 |
+- crng->state[i] ^= rv; |
1345 |
+- } |
1346 |
++ unsigned long flags; |
1347 |
++ unsigned long next_gen; |
1348 |
++ u8 key[CHACHA_KEY_SIZE]; |
1349 |
+ |
1350 |
+- return arch_init; |
1351 |
++ extract_entropy(key, sizeof(key)); |
1352 |
++ |
1353 |
++ /* |
1354 |
++ * We copy the new key into the base_crng, overwriting the old one, |
1355 |
++ * and update the generation counter. We avoid hitting ULONG_MAX, |
1356 |
++ * because the per-cpu crngs are initialized to ULONG_MAX, so this |
1357 |
++ * forces new CPUs that come online to always initialize. |
1358 |
++ */ |
1359 |
++ spin_lock_irqsave(&base_crng.lock, flags); |
1360 |
++ memcpy(base_crng.key, key, sizeof(base_crng.key)); |
1361 |
++ next_gen = base_crng.generation + 1; |
1362 |
++ if (next_gen == ULONG_MAX) |
1363 |
++ ++next_gen; |
1364 |
++ WRITE_ONCE(base_crng.generation, next_gen); |
1365 |
++ WRITE_ONCE(base_crng.birth, jiffies); |
1366 |
++ if (!static_branch_likely(&crng_is_ready)) |
1367 |
++ crng_init = CRNG_READY; |
1368 |
++ spin_unlock_irqrestore(&base_crng.lock, flags); |
1369 |
++ memzero_explicit(key, sizeof(key)); |
1370 |
+ } |
1371 |
+ |
1372 |
+-static bool __init crng_init_try_arch_early(void) |
1373 |
++/* |
1374 |
++ * This generates a ChaCha block using the provided key, and then |
1375 |
++ * immediately overwites that key with half the block. It returns |
1376 |
++ * the resultant ChaCha state to the user, along with the second |
1377 |
++ * half of the block containing 32 bytes of random data that may |
1378 |
++ * be used; random_data_len may not be greater than 32. |
1379 |
++ * |
1380 |
++ * The returned ChaCha state contains within it a copy of the old |
1381 |
++ * key value, at index 4, so the state should always be zeroed out |
1382 |
++ * immediately after using in order to maintain forward secrecy. |
1383 |
++ * If the state cannot be erased in a timely manner, then it is |
1384 |
++ * safer to set the random_data parameter to &chacha_state[4] so |
1385 |
++ * that this function overwrites it before returning. |
1386 |
++ */ |
1387 |
++static void crng_fast_key_erasure(u8 key[CHACHA_KEY_SIZE], |
1388 |
++ u32 chacha_state[CHACHA_STATE_WORDS], |
1389 |
++ u8 *random_data, size_t random_data_len) |
1390 |
+ { |
1391 |
+- int i; |
1392 |
+- bool arch_init = true; |
1393 |
+- unsigned long rv; |
1394 |
+- |
1395 |
+- for (i = 4; i < 16; i++) { |
1396 |
+- if (!arch_get_random_seed_long_early(&rv) && |
1397 |
+- !arch_get_random_long_early(&rv)) { |
1398 |
+- rv = random_get_entropy(); |
1399 |
+- arch_init = false; |
1400 |
+- } |
1401 |
+- primary_crng.state[i] ^= rv; |
1402 |
+- } |
1403 |
++ u8 first_block[CHACHA_BLOCK_SIZE]; |
1404 |
+ |
1405 |
+- return arch_init; |
1406 |
+-} |
1407 |
++ BUG_ON(random_data_len > 32); |
1408 |
+ |
1409 |
+-static void crng_initialize_secondary(struct crng_state *crng) |
1410 |
+-{ |
1411 |
+- chacha_init_consts(crng->state); |
1412 |
+- _get_random_bytes(&crng->state[4], sizeof(u32) * 12); |
1413 |
+- crng_init_try_arch(crng); |
1414 |
+- crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1; |
1415 |
++ chacha_init_consts(chacha_state); |
1416 |
++ memcpy(&chacha_state[4], key, CHACHA_KEY_SIZE); |
1417 |
++ memset(&chacha_state[12], 0, sizeof(u32) * 4); |
1418 |
++ chacha20_block(chacha_state, first_block); |
1419 |
++ |
1420 |
++ memcpy(key, first_block, CHACHA_KEY_SIZE); |
1421 |
++ memcpy(random_data, first_block + CHACHA_KEY_SIZE, random_data_len); |
1422 |
++ memzero_explicit(first_block, sizeof(first_block)); |
1423 |
+ } |
1424 |
+ |
1425 |
+-static void __init crng_initialize_primary(void) |
1426 |
+-{ |
1427 |
+- _extract_entropy(&primary_crng.state[4], sizeof(u32) * 12); |
1428 |
+- if (crng_init_try_arch_early() && trust_cpu && crng_init < 2) { |
1429 |
+- invalidate_batched_entropy(); |
1430 |
+- numa_crng_init(); |
1431 |
+- crng_init = 2; |
1432 |
+- pr_notice("crng init done (trusting CPU's manufacturer)\n"); |
1433 |
++/* |
1434 |
++ * Return whether the crng seed is considered to be sufficiently old |
1435 |
++ * that a reseeding is needed. This happens if the last reseeding |
1436 |
++ * was CRNG_RESEED_INTERVAL ago, or during early boot, at an interval |
1437 |
++ * proportional to the uptime. |
1438 |
++ */ |
1439 |
++static bool crng_has_old_seed(void) |
1440 |
++{ |
1441 |
++ static bool early_boot = true; |
1442 |
++ unsigned long interval = CRNG_RESEED_INTERVAL; |
1443 |
++ |
1444 |
++ if (unlikely(READ_ONCE(early_boot))) { |
1445 |
++ time64_t uptime = ktime_get_seconds(); |
1446 |
++ if (uptime >= CRNG_RESEED_INTERVAL / HZ * 2) |
1447 |
++ WRITE_ONCE(early_boot, false); |
1448 |
++ else |
1449 |
++ interval = max_t(unsigned int, CRNG_RESEED_START_INTERVAL, |
1450 |
++ (unsigned int)uptime / 2 * HZ); |
1451 |
+ } |
1452 |
+- primary_crng.init_time = jiffies - CRNG_RESEED_INTERVAL - 1; |
1453 |
++ return time_is_before_jiffies(READ_ONCE(base_crng.birth) + interval); |
1454 |
+ } |
1455 |
+ |
1456 |
+-static void crng_finalize_init(void) |
1457 |
++/* |
1458 |
++ * This function returns a ChaCha state that you may use for generating |
1459 |
++ * random data. It also returns up to 32 bytes on its own of random data |
1460 |
++ * that may be used; random_data_len may not be greater than 32. |
1461 |
++ */ |
1462 |
++static void crng_make_state(u32 chacha_state[CHACHA_STATE_WORDS], |
1463 |
++ u8 *random_data, size_t random_data_len) |
1464 |
+ { |
1465 |
+- if (!system_wq) { |
1466 |
+- /* We can't call numa_crng_init until we have workqueues, |
1467 |
+- * so mark this for processing later. */ |
1468 |
+- crng_need_final_init = true; |
1469 |
+- return; |
1470 |
+- } |
1471 |
++ unsigned long flags; |
1472 |
++ struct crng *crng; |
1473 |
+ |
1474 |
+- invalidate_batched_entropy(); |
1475 |
+- numa_crng_init(); |
1476 |
+- crng_init = 2; |
1477 |
+- crng_need_final_init = false; |
1478 |
+- process_random_ready_list(); |
1479 |
+- wake_up_interruptible(&crng_init_wait); |
1480 |
+- kill_fasync(&fasync, SIGIO, POLL_IN); |
1481 |
+- pr_notice("crng init done\n"); |
1482 |
+- if (unseeded_warning.missed) { |
1483 |
+- pr_notice("%d get_random_xx warning(s) missed due to ratelimiting\n", |
1484 |
+- unseeded_warning.missed); |
1485 |
+- unseeded_warning.missed = 0; |
1486 |
++ BUG_ON(random_data_len > 32); |
1487 |
++ |
1488 |
++ /* |
1489 |
++ * For the fast path, we check whether we're ready, unlocked first, and |
1490 |
++ * then re-check once locked later. In the case where we're really not |
1491 |
++ * ready, we do fast key erasure with the base_crng directly, extracting |
1492 |
++ * when crng_init is CRNG_EMPTY. |
1493 |
++ */ |
1494 |
++ if (!crng_ready()) { |
1495 |
++ bool ready; |
1496 |
++ |
1497 |
++ spin_lock_irqsave(&base_crng.lock, flags); |
1498 |
++ ready = crng_ready(); |
1499 |
++ if (!ready) { |
1500 |
++ if (crng_init == CRNG_EMPTY) |
1501 |
++ extract_entropy(base_crng.key, sizeof(base_crng.key)); |
1502 |
++ crng_fast_key_erasure(base_crng.key, chacha_state, |
1503 |
++ random_data, random_data_len); |
1504 |
++ } |
1505 |
++ spin_unlock_irqrestore(&base_crng.lock, flags); |
1506 |
++ if (!ready) |
1507 |
++ return; |
1508 |
+ } |
1509 |
+- if (urandom_warning.missed) { |
1510 |
+- pr_notice("%d urandom warning(s) missed due to ratelimiting\n", |
1511 |
+- urandom_warning.missed); |
1512 |
+- urandom_warning.missed = 0; |
1513 |
++ |
1514 |
++ /* |
1515 |
++ * If the base_crng is old enough, we reseed, which in turn bumps the |
1516 |
++ * generation counter that we check below. |
1517 |
++ */ |
1518 |
++ if (unlikely(crng_has_old_seed())) |
1519 |
++ crng_reseed(); |
1520 |
++ |
1521 |
++ local_lock_irqsave(&crngs.lock, flags); |
1522 |
++ crng = raw_cpu_ptr(&crngs); |
1523 |
++ |
1524 |
++ /* |
1525 |
++ * If our per-cpu crng is older than the base_crng, then it means |
1526 |
++ * somebody reseeded the base_crng. In that case, we do fast key |
1527 |
++ * erasure on the base_crng, and use its output as the new key |
1528 |
++ * for our per-cpu crng. This brings us up to date with base_crng. |
1529 |
++ */ |
1530 |
++ if (unlikely(crng->generation != READ_ONCE(base_crng.generation))) { |
1531 |
++ spin_lock(&base_crng.lock); |
1532 |
++ crng_fast_key_erasure(base_crng.key, chacha_state, |
1533 |
++ crng->key, sizeof(crng->key)); |
1534 |
++ crng->generation = base_crng.generation; |
1535 |
++ spin_unlock(&base_crng.lock); |
1536 |
+ } |
1537 |
++ |
1538 |
++ /* |
1539 |
++ * Finally, when we've made it this far, our per-cpu crng has an up |
1540 |
++ * to date key, and we can do fast key erasure with it to produce |
1541 |
++ * some random data and a ChaCha state for the caller. All other |
1542 |
++ * branches of this function are "unlikely", so most of the time we |
1543 |
++ * should wind up here immediately. |
1544 |
++ */ |
1545 |
++ crng_fast_key_erasure(crng->key, chacha_state, random_data, random_data_len); |
1546 |
++ local_unlock_irqrestore(&crngs.lock, flags); |
1547 |
+ } |
1548 |
+ |
1549 |
+-static void do_numa_crng_init(struct work_struct *work) |
1550 |
++static void _get_random_bytes(void *buf, size_t len) |
1551 |
+ { |
1552 |
+- int i; |
1553 |
+- struct crng_state *crng; |
1554 |
+- struct crng_state **pool; |
1555 |
+- |
1556 |
+- pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL | __GFP_NOFAIL); |
1557 |
+- for_each_online_node(i) { |
1558 |
+- crng = kmalloc_node(sizeof(struct crng_state), |
1559 |
+- GFP_KERNEL | __GFP_NOFAIL, i); |
1560 |
+- spin_lock_init(&crng->lock); |
1561 |
+- crng_initialize_secondary(crng); |
1562 |
+- pool[i] = crng; |
1563 |
+- } |
1564 |
+- /* pairs with READ_ONCE() in select_crng() */ |
1565 |
+- if (cmpxchg_release(&crng_node_pool, NULL, pool) != NULL) { |
1566 |
+- for_each_node(i) |
1567 |
+- kfree(pool[i]); |
1568 |
+- kfree(pool); |
1569 |
+- } |
1570 |
+-} |
1571 |
++ u32 chacha_state[CHACHA_STATE_WORDS]; |
1572 |
++ u8 tmp[CHACHA_BLOCK_SIZE]; |
1573 |
++ size_t first_block_len; |
1574 |
+ |
1575 |
+-static DECLARE_WORK(numa_crng_init_work, do_numa_crng_init); |
1576 |
++ if (!len) |
1577 |
++ return; |
1578 |
+ |
1579 |
+-static void numa_crng_init(void) |
1580 |
+-{ |
1581 |
+- if (IS_ENABLED(CONFIG_NUMA)) |
1582 |
+- schedule_work(&numa_crng_init_work); |
1583 |
+-} |
1584 |
++ first_block_len = min_t(size_t, 32, len); |
1585 |
++ crng_make_state(chacha_state, buf, first_block_len); |
1586 |
++ len -= first_block_len; |
1587 |
++ buf += first_block_len; |
1588 |
+ |
1589 |
+-static struct crng_state *select_crng(void) |
1590 |
+-{ |
1591 |
+- if (IS_ENABLED(CONFIG_NUMA)) { |
1592 |
+- struct crng_state **pool; |
1593 |
+- int nid = numa_node_id(); |
1594 |
+- |
1595 |
+- /* pairs with cmpxchg_release() in do_numa_crng_init() */ |
1596 |
+- pool = READ_ONCE(crng_node_pool); |
1597 |
+- if (pool && pool[nid]) |
1598 |
+- return pool[nid]; |
1599 |
++ while (len) { |
1600 |
++ if (len < CHACHA_BLOCK_SIZE) { |
1601 |
++ chacha20_block(chacha_state, tmp); |
1602 |
++ memcpy(buf, tmp, len); |
1603 |
++ memzero_explicit(tmp, sizeof(tmp)); |
1604 |
++ break; |
1605 |
++ } |
1606 |
++ |
1607 |
++ chacha20_block(chacha_state, buf); |
1608 |
++ if (unlikely(chacha_state[12] == 0)) |
1609 |
++ ++chacha_state[13]; |
1610 |
++ len -= CHACHA_BLOCK_SIZE; |
1611 |
++ buf += CHACHA_BLOCK_SIZE; |
1612 |
+ } |
1613 |
+ |
1614 |
+- return &primary_crng; |
1615 |
++ memzero_explicit(chacha_state, sizeof(chacha_state)); |
1616 |
+ } |
1617 |
+ |
1618 |
+ /* |
1619 |
+- * crng_fast_load() can be called by code in the interrupt service |
1620 |
+- * path. So we can't afford to dilly-dally. Returns the number of |
1621 |
+- * bytes processed from cp. |
1622 |
++ * This function is the exported kernel interface. It returns some |
1623 |
++ * number of good random numbers, suitable for key generation, seeding |
1624 |
++ * TCP sequence numbers, etc. It does not rely on the hardware random |
1625 |
++ * number generator. For random bytes direct from the hardware RNG |
1626 |
++ * (when available), use get_random_bytes_arch(). In order to ensure |
1627 |
++ * that the randomness provided by this function is okay, the function |
1628 |
++ * wait_for_random_bytes() should be called and return 0 at least once |
1629 |
++ * at any point prior. |
1630 |
+ */ |
1631 |
+-static size_t crng_fast_load(const u8 *cp, size_t len) |
1632 |
++void get_random_bytes(void *buf, size_t len) |
1633 |
+ { |
1634 |
+- unsigned long flags; |
1635 |
+- u8 *p; |
1636 |
+- size_t ret = 0; |
1637 |
+- |
1638 |
+- if (!spin_trylock_irqsave(&primary_crng.lock, flags)) |
1639 |
+- return 0; |
1640 |
+- if (crng_init != 0) { |
1641 |
+- spin_unlock_irqrestore(&primary_crng.lock, flags); |
1642 |
+- return 0; |
1643 |
+- } |
1644 |
+- p = (u8 *)&primary_crng.state[4]; |
1645 |
+- while (len > 0 && crng_init_cnt < CRNG_INIT_CNT_THRESH) { |
1646 |
+- p[crng_init_cnt % CHACHA_KEY_SIZE] ^= *cp; |
1647 |
+- cp++; crng_init_cnt++; len--; ret++; |
1648 |
+- } |
1649 |
+- spin_unlock_irqrestore(&primary_crng.lock, flags); |
1650 |
+- if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { |
1651 |
+- invalidate_batched_entropy(); |
1652 |
+- crng_init = 1; |
1653 |
+- pr_notice("fast init done\n"); |
1654 |
+- } |
1655 |
+- return ret; |
1656 |
++ warn_unseeded_randomness(); |
1657 |
++ _get_random_bytes(buf, len); |
1658 |
+ } |
1659 |
++EXPORT_SYMBOL(get_random_bytes); |
1660 |
+ |
1661 |
+-/* |
1662 |
+- * crng_slow_load() is called by add_device_randomness, which has two |
1663 |
+- * attributes. (1) We can't trust the buffer passed to it is |
1664 |
+- * guaranteed to be unpredictable (so it might not have any entropy at |
1665 |
+- * all), and (2) it doesn't have the performance constraints of |
1666 |
+- * crng_fast_load(). |
1667 |
+- * |
1668 |
+- * So we do something more comprehensive which is guaranteed to touch |
1669 |
+- * all of the primary_crng's state, and which uses a LFSR with a |
1670 |
+- * period of 255 as part of the mixing algorithm. Finally, we do |
1671 |
+- * *not* advance crng_init_cnt since buffer we may get may be something |
1672 |
+- * like a fixed DMI table (for example), which might very well be |
1673 |
+- * unique to the machine, but is otherwise unvarying. |
1674 |
+- */ |
1675 |
+-static int crng_slow_load(const u8 *cp, size_t len) |
1676 |
++static ssize_t get_random_bytes_user(struct iov_iter *iter) |
1677 |
+ { |
1678 |
+- unsigned long flags; |
1679 |
+- static u8 lfsr = 1; |
1680 |
+- u8 tmp; |
1681 |
+- unsigned int i, max = CHACHA_KEY_SIZE; |
1682 |
+- const u8 *src_buf = cp; |
1683 |
+- u8 *dest_buf = (u8 *)&primary_crng.state[4]; |
1684 |
++ u32 chacha_state[CHACHA_STATE_WORDS]; |
1685 |
++ u8 block[CHACHA_BLOCK_SIZE]; |
1686 |
++ size_t ret = 0, copied; |
1687 |
+ |
1688 |
+- if (!spin_trylock_irqsave(&primary_crng.lock, flags)) |
1689 |
+- return 0; |
1690 |
+- if (crng_init != 0) { |
1691 |
+- spin_unlock_irqrestore(&primary_crng.lock, flags); |
1692 |
++ if (unlikely(!iov_iter_count(iter))) |
1693 |
+ return 0; |
1694 |
+- } |
1695 |
+- if (len > max) |
1696 |
+- max = len; |
1697 |
+- |
1698 |
+- for (i = 0; i < max; i++) { |
1699 |
+- tmp = lfsr; |
1700 |
+- lfsr >>= 1; |
1701 |
+- if (tmp & 1) |
1702 |
+- lfsr ^= 0xE1; |
1703 |
+- tmp = dest_buf[i % CHACHA_KEY_SIZE]; |
1704 |
+- dest_buf[i % CHACHA_KEY_SIZE] ^= src_buf[i % len] ^ lfsr; |
1705 |
+- lfsr += (tmp << 3) | (tmp >> 5); |
1706 |
+- } |
1707 |
+- spin_unlock_irqrestore(&primary_crng.lock, flags); |
1708 |
+- return 1; |
1709 |
+-} |
1710 |
+ |
1711 |
+-static void crng_reseed(struct crng_state *crng, bool use_input_pool) |
1712 |
+-{ |
1713 |
+- unsigned long flags; |
1714 |
+- int i, num; |
1715 |
+- union { |
1716 |
+- u8 block[CHACHA_BLOCK_SIZE]; |
1717 |
+- u32 key[8]; |
1718 |
+- } buf; |
1719 |
+- |
1720 |
+- if (use_input_pool) { |
1721 |
+- num = extract_entropy(&buf, 32, 16); |
1722 |
+- if (num == 0) |
1723 |
+- return; |
1724 |
+- } else { |
1725 |
+- _extract_crng(&primary_crng, buf.block); |
1726 |
+- _crng_backtrack_protect(&primary_crng, buf.block, |
1727 |
+- CHACHA_KEY_SIZE); |
1728 |
+- } |
1729 |
+- spin_lock_irqsave(&crng->lock, flags); |
1730 |
+- for (i = 0; i < 8; i++) { |
1731 |
+- unsigned long rv; |
1732 |
+- if (!arch_get_random_seed_long(&rv) && |
1733 |
+- !arch_get_random_long(&rv)) |
1734 |
+- rv = random_get_entropy(); |
1735 |
+- crng->state[i + 4] ^= buf.key[i] ^ rv; |
1736 |
++ /* |
1737 |
++ * Immediately overwrite the ChaCha key at index 4 with random |
1738 |
++ * bytes, in case userspace causes copy_to_user() below to sleep |
1739 |
++ * forever, so that we still retain forward secrecy in that case. |
1740 |
++ */ |
1741 |
++ crng_make_state(chacha_state, (u8 *)&chacha_state[4], CHACHA_KEY_SIZE); |
1742 |
++ /* |
1743 |
++ * However, if we're doing a read of len <= 32, we don't need to |
1744 |
++ * use chacha_state after, so we can simply return those bytes to |
1745 |
++ * the user directly. |
1746 |
++ */ |
1747 |
++ if (iov_iter_count(iter) <= CHACHA_KEY_SIZE) { |
1748 |
++ ret = copy_to_iter(&chacha_state[4], CHACHA_KEY_SIZE, iter); |
1749 |
++ goto out_zero_chacha; |
1750 |
+ } |
1751 |
+- memzero_explicit(&buf, sizeof(buf)); |
1752 |
+- WRITE_ONCE(crng->init_time, jiffies); |
1753 |
+- spin_unlock_irqrestore(&crng->lock, flags); |
1754 |
+- if (crng == &primary_crng && crng_init < 2) |
1755 |
+- crng_finalize_init(); |
1756 |
+-} |
1757 |
+ |
1758 |
+-static void _extract_crng(struct crng_state *crng, u8 out[CHACHA_BLOCK_SIZE]) |
1759 |
+-{ |
1760 |
+- unsigned long flags, init_time; |
1761 |
++ for (;;) { |
1762 |
++ chacha20_block(chacha_state, block); |
1763 |
++ if (unlikely(chacha_state[12] == 0)) |
1764 |
++ ++chacha_state[13]; |
1765 |
++ |
1766 |
++ copied = copy_to_iter(block, sizeof(block), iter); |
1767 |
++ ret += copied; |
1768 |
++ if (!iov_iter_count(iter) || copied != sizeof(block)) |
1769 |
++ break; |
1770 |
+ |
1771 |
+- if (crng_ready()) { |
1772 |
+- init_time = READ_ONCE(crng->init_time); |
1773 |
+- if (time_after(READ_ONCE(crng_global_init_time), init_time) || |
1774 |
+- time_after(jiffies, init_time + CRNG_RESEED_INTERVAL)) |
1775 |
+- crng_reseed(crng, crng == &primary_crng); |
1776 |
++ BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0); |
1777 |
++ if (ret % PAGE_SIZE == 0) { |
1778 |
++ if (signal_pending(current)) |
1779 |
++ break; |
1780 |
++ cond_resched(); |
1781 |
++ } |
1782 |
+ } |
1783 |
+- spin_lock_irqsave(&crng->lock, flags); |
1784 |
+- chacha20_block(&crng->state[0], out); |
1785 |
+- if (crng->state[12] == 0) |
1786 |
+- crng->state[13]++; |
1787 |
+- spin_unlock_irqrestore(&crng->lock, flags); |
1788 |
+-} |
1789 |
+ |
1790 |
+-static void extract_crng(u8 out[CHACHA_BLOCK_SIZE]) |
1791 |
+-{ |
1792 |
+- _extract_crng(select_crng(), out); |
1793 |
++ memzero_explicit(block, sizeof(block)); |
1794 |
++out_zero_chacha: |
1795 |
++ memzero_explicit(chacha_state, sizeof(chacha_state)); |
1796 |
++ return ret ? ret : -EFAULT; |
1797 |
+ } |
1798 |
+ |
1799 |
+ /* |
1800 |
+- * Use the leftover bytes from the CRNG block output (if there is |
1801 |
+- * enough) to mutate the CRNG key to provide backtracking protection. |
1802 |
++ * Batched entropy returns random integers. The quality of the random |
1803 |
++ * number is good as /dev/urandom. In order to ensure that the randomness |
1804 |
++ * provided by this function is okay, the function wait_for_random_bytes() |
1805 |
++ * should be called and return 0 at least once at any point prior. |
1806 |
+ */ |
1807 |
+-static void _crng_backtrack_protect(struct crng_state *crng, |
1808 |
+- u8 tmp[CHACHA_BLOCK_SIZE], int used) |
1809 |
+-{ |
1810 |
+- unsigned long flags; |
1811 |
+- u32 *s, *d; |
1812 |
+- int i; |
1813 |
+ |
1814 |
+- used = round_up(used, sizeof(u32)); |
1815 |
+- if (used + CHACHA_KEY_SIZE > CHACHA_BLOCK_SIZE) { |
1816 |
+- extract_crng(tmp); |
1817 |
+- used = 0; |
1818 |
+- } |
1819 |
+- spin_lock_irqsave(&crng->lock, flags); |
1820 |
+- s = (u32 *)&tmp[used]; |
1821 |
+- d = &crng->state[4]; |
1822 |
+- for (i = 0; i < 8; i++) |
1823 |
+- *d++ ^= *s++; |
1824 |
+- spin_unlock_irqrestore(&crng->lock, flags); |
1825 |
+-} |
1826 |
+- |
1827 |
+-static void crng_backtrack_protect(u8 tmp[CHACHA_BLOCK_SIZE], int used) |
1828 |
++#define DEFINE_BATCHED_ENTROPY(type) \ |
1829 |
++struct batch_ ##type { \ |
1830 |
++ /* \ |
1831 |
++ * We make this 1.5x a ChaCha block, so that we get the \ |
1832 |
++ * remaining 32 bytes from fast key erasure, plus one full \ |
1833 |
++ * block from the detached ChaCha state. We can increase \ |
1834 |
++ * the size of this later if needed so long as we keep the \ |
1835 |
++ * formula of (integer_blocks + 0.5) * CHACHA_BLOCK_SIZE. \ |
1836 |
++ */ \ |
1837 |
++ type entropy[CHACHA_BLOCK_SIZE * 3 / (2 * sizeof(type))]; \ |
1838 |
++ local_lock_t lock; \ |
1839 |
++ unsigned long generation; \ |
1840 |
++ unsigned int position; \ |
1841 |
++}; \ |
1842 |
++ \ |
1843 |
++static DEFINE_PER_CPU(struct batch_ ##type, batched_entropy_ ##type) = { \ |
1844 |
++ .lock = INIT_LOCAL_LOCK(batched_entropy_ ##type.lock), \ |
1845 |
++ .position = UINT_MAX \ |
1846 |
++}; \ |
1847 |
++ \ |
1848 |
++type get_random_ ##type(void) \ |
1849 |
++{ \ |
1850 |
++ type ret; \ |
1851 |
++ unsigned long flags; \ |
1852 |
++ struct batch_ ##type *batch; \ |
1853 |
++ unsigned long next_gen; \ |
1854 |
++ \ |
1855 |
++ warn_unseeded_randomness(); \ |
1856 |
++ \ |
1857 |
++ if (!crng_ready()) { \ |
1858 |
++ _get_random_bytes(&ret, sizeof(ret)); \ |
1859 |
++ return ret; \ |
1860 |
++ } \ |
1861 |
++ \ |
1862 |
++ local_lock_irqsave(&batched_entropy_ ##type.lock, flags); \ |
1863 |
++ batch = raw_cpu_ptr(&batched_entropy_##type); \ |
1864 |
++ \ |
1865 |
++ next_gen = READ_ONCE(base_crng.generation); \ |
1866 |
++ if (batch->position >= ARRAY_SIZE(batch->entropy) || \ |
1867 |
++ next_gen != batch->generation) { \ |
1868 |
++ _get_random_bytes(batch->entropy, sizeof(batch->entropy)); \ |
1869 |
++ batch->position = 0; \ |
1870 |
++ batch->generation = next_gen; \ |
1871 |
++ } \ |
1872 |
++ \ |
1873 |
++ ret = batch->entropy[batch->position]; \ |
1874 |
++ batch->entropy[batch->position] = 0; \ |
1875 |
++ ++batch->position; \ |
1876 |
++ local_unlock_irqrestore(&batched_entropy_ ##type.lock, flags); \ |
1877 |
++ return ret; \ |
1878 |
++} \ |
1879 |
++EXPORT_SYMBOL(get_random_ ##type); |
1880 |
++ |
1881 |
++DEFINE_BATCHED_ENTROPY(u64) |
1882 |
++DEFINE_BATCHED_ENTROPY(u32) |
1883 |
++ |
1884 |
++#ifdef CONFIG_SMP |
1885 |
++/* |
1886 |
++ * This function is called when the CPU is coming up, with entry |
1887 |
++ * CPUHP_RANDOM_PREPARE, which comes before CPUHP_WORKQUEUE_PREP. |
1888 |
++ */ |
1889 |
++int __cold random_prepare_cpu(unsigned int cpu) |
1890 |
+ { |
1891 |
+- _crng_backtrack_protect(select_crng(), tmp, used); |
1892 |
++ /* |
1893 |
++ * When the cpu comes back online, immediately invalidate both |
1894 |
++ * the per-cpu crng and all batches, so that we serve fresh |
1895 |
++ * randomness. |
1896 |
++ */ |
1897 |
++ per_cpu_ptr(&crngs, cpu)->generation = ULONG_MAX; |
1898 |
++ per_cpu_ptr(&batched_entropy_u32, cpu)->position = UINT_MAX; |
1899 |
++ per_cpu_ptr(&batched_entropy_u64, cpu)->position = UINT_MAX; |
1900 |
++ return 0; |
1901 |
+ } |
1902 |
++#endif |
1903 |
+ |
1904 |
+-static ssize_t extract_crng_user(void __user *buf, size_t nbytes) |
1905 |
++/* |
1906 |
++ * This function will use the architecture-specific hardware random |
1907 |
++ * number generator if it is available. It is not recommended for |
1908 |
++ * use. Use get_random_bytes() instead. It returns the number of |
1909 |
++ * bytes filled in. |
1910 |
++ */ |
1911 |
++size_t __must_check get_random_bytes_arch(void *buf, size_t len) |
1912 |
+ { |
1913 |
+- ssize_t ret = 0, i = CHACHA_BLOCK_SIZE; |
1914 |
+- u8 tmp[CHACHA_BLOCK_SIZE] __aligned(4); |
1915 |
+- int large_request = (nbytes > 256); |
1916 |
+- |
1917 |
+- while (nbytes) { |
1918 |
+- if (large_request && need_resched()) { |
1919 |
+- if (signal_pending(current)) { |
1920 |
+- if (ret == 0) |
1921 |
+- ret = -ERESTARTSYS; |
1922 |
+- break; |
1923 |
+- } |
1924 |
+- schedule(); |
1925 |
+- } |
1926 |
++ size_t left = len; |
1927 |
++ u8 *p = buf; |
1928 |
++ |
1929 |
++ while (left) { |
1930 |
++ unsigned long v; |
1931 |
++ size_t block_len = min_t(size_t, left, sizeof(unsigned long)); |
1932 |
+ |
1933 |
+- extract_crng(tmp); |
1934 |
+- i = min_t(int, nbytes, CHACHA_BLOCK_SIZE); |
1935 |
+- if (copy_to_user(buf, tmp, i)) { |
1936 |
+- ret = -EFAULT; |
1937 |
++ if (!arch_get_random_long(&v)) |
1938 |
+ break; |
1939 |
+- } |
1940 |
+ |
1941 |
+- nbytes -= i; |
1942 |
+- buf += i; |
1943 |
+- ret += i; |
1944 |
++ memcpy(p, &v, block_len); |
1945 |
++ p += block_len; |
1946 |
++ left -= block_len; |
1947 |
+ } |
1948 |
+- crng_backtrack_protect(tmp, i); |
1949 |
+- |
1950 |
+- /* Wipe data just written to memory */ |
1951 |
+- memzero_explicit(tmp, sizeof(tmp)); |
1952 |
+ |
1953 |
+- return ret; |
1954 |
++ return len - left; |
1955 |
+ } |
1956 |
++EXPORT_SYMBOL(get_random_bytes_arch); |
1957 |
+ |
1958 |
+-/********************************************************************* |
1959 |
++ |
1960 |
++/********************************************************************** |
1961 |
+ * |
1962 |
+- * Entropy input management |
1963 |
++ * Entropy accumulation and extraction routines. |
1964 |
+ * |
1965 |
+- *********************************************************************/ |
1966 |
++ * Callers may add entropy via: |
1967 |
++ * |
1968 |
++ * static void mix_pool_bytes(const void *buf, size_t len) |
1969 |
++ * |
1970 |
++ * After which, if added entropy should be credited: |
1971 |
++ * |
1972 |
++ * static void credit_init_bits(size_t bits) |
1973 |
++ * |
1974 |
++ * Finally, extract entropy via: |
1975 |
++ * |
1976 |
++ * static void extract_entropy(void *buf, size_t len) |
1977 |
++ * |
1978 |
++ **********************************************************************/ |
1979 |
+ |
1980 |
+-/* There is one of these per entropy source */ |
1981 |
+-struct timer_rand_state { |
1982 |
+- cycles_t last_time; |
1983 |
+- long last_delta, last_delta2; |
1984 |
++enum { |
1985 |
++ POOL_BITS = BLAKE2S_HASH_SIZE * 8, |
1986 |
++ POOL_READY_BITS = POOL_BITS, /* When crng_init->CRNG_READY */ |
1987 |
++ POOL_EARLY_BITS = POOL_READY_BITS / 2 /* When crng_init->CRNG_EARLY */ |
1988 |
+ }; |
1989 |
+ |
1990 |
+-#define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, }; |
1991 |
++static struct { |
1992 |
++ struct blake2s_state hash; |
1993 |
++ spinlock_t lock; |
1994 |
++ unsigned int init_bits; |
1995 |
++} input_pool = { |
1996 |
++ .hash.h = { BLAKE2S_IV0 ^ (0x01010000 | BLAKE2S_HASH_SIZE), |
1997 |
++ BLAKE2S_IV1, BLAKE2S_IV2, BLAKE2S_IV3, BLAKE2S_IV4, |
1998 |
++ BLAKE2S_IV5, BLAKE2S_IV6, BLAKE2S_IV7 }, |
1999 |
++ .hash.outlen = BLAKE2S_HASH_SIZE, |
2000 |
++ .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), |
2001 |
++}; |
2002 |
++ |
2003 |
++static void _mix_pool_bytes(const void *buf, size_t len) |
2004 |
++{ |
2005 |
++ blake2s_update(&input_pool.hash, buf, len); |
2006 |
++} |
2007 |
+ |
2008 |
+ /* |
2009 |
+- * Add device- or boot-specific data to the input pool to help |
2010 |
+- * initialize it. |
2011 |
+- * |
2012 |
+- * None of this adds any entropy; it is meant to avoid the problem of |
2013 |
+- * the entropy pool having similar initial state across largely |
2014 |
+- * identical devices. |
2015 |
++ * This function adds bytes into the input pool. It does not |
2016 |
++ * update the initialization bit counter; the caller should call |
2017 |
++ * credit_init_bits if this is appropriate. |
2018 |
+ */ |
2019 |
+-void add_device_randomness(const void *buf, unsigned int size) |
2020 |
++static void mix_pool_bytes(const void *buf, size_t len) |
2021 |
+ { |
2022 |
+- unsigned long time = random_get_entropy() ^ jiffies; |
2023 |
+ unsigned long flags; |
2024 |
+ |
2025 |
+- if (!crng_ready() && size) |
2026 |
+- crng_slow_load(buf, size); |
2027 |
+- |
2028 |
+- trace_add_device_randomness(size, _RET_IP_); |
2029 |
+ spin_lock_irqsave(&input_pool.lock, flags); |
2030 |
+- _mix_pool_bytes(buf, size); |
2031 |
+- _mix_pool_bytes(&time, sizeof(time)); |
2032 |
++ _mix_pool_bytes(buf, len); |
2033 |
+ spin_unlock_irqrestore(&input_pool.lock, flags); |
2034 |
+ } |
2035 |
+-EXPORT_SYMBOL(add_device_randomness); |
2036 |
+- |
2037 |
+-static struct timer_rand_state input_timer_state = INIT_TIMER_RAND_STATE; |
2038 |
+ |
2039 |
+ /* |
2040 |
+- * This function adds entropy to the entropy "pool" by using timing |
2041 |
+- * delays. It uses the timer_rand_state structure to make an estimate |
2042 |
+- * of how many bits of entropy this call has added to the pool. |
2043 |
+- * |
2044 |
+- * The number "num" is also added to the pool - it should somehow describe |
2045 |
+- * the type of event which just happened. This is currently 0-255 for |
2046 |
+- * keyboard scan codes, and 256 upwards for interrupts. |
2047 |
+- * |
2048 |
++ * This is an HKDF-like construction for using the hashed collected entropy |
2049 |
++ * as a PRF key, that's then expanded block-by-block. |
2050 |
+ */ |
2051 |
+-static void add_timer_randomness(struct timer_rand_state *state, unsigned num) |
2052 |
++static void extract_entropy(void *buf, size_t len) |
2053 |
+ { |
2054 |
++ unsigned long flags; |
2055 |
++ u8 seed[BLAKE2S_HASH_SIZE], next_key[BLAKE2S_HASH_SIZE]; |
2056 |
+ struct { |
2057 |
+- long jiffies; |
2058 |
+- unsigned int cycles; |
2059 |
+- unsigned int num; |
2060 |
+- } sample; |
2061 |
+- long delta, delta2, delta3; |
2062 |
+- |
2063 |
+- sample.jiffies = jiffies; |
2064 |
+- sample.cycles = random_get_entropy(); |
2065 |
+- sample.num = num; |
2066 |
+- mix_pool_bytes(&sample, sizeof(sample)); |
2067 |
+- |
2068 |
+- /* |
2069 |
+- * Calculate number of bits of randomness we probably added. |
2070 |
+- * We take into account the first, second and third-order deltas |
2071 |
+- * in order to make our estimate. |
2072 |
+- */ |
2073 |
+- delta = sample.jiffies - READ_ONCE(state->last_time); |
2074 |
+- WRITE_ONCE(state->last_time, sample.jiffies); |
2075 |
+- |
2076 |
+- delta2 = delta - READ_ONCE(state->last_delta); |
2077 |
+- WRITE_ONCE(state->last_delta, delta); |
2078 |
+- |
2079 |
+- delta3 = delta2 - READ_ONCE(state->last_delta2); |
2080 |
+- WRITE_ONCE(state->last_delta2, delta2); |
2081 |
++ unsigned long rdseed[32 / sizeof(long)]; |
2082 |
++ size_t counter; |
2083 |
++ } block; |
2084 |
++ size_t i; |
2085 |
++ |
2086 |
++ for (i = 0; i < ARRAY_SIZE(block.rdseed); ++i) { |
2087 |
++ if (!arch_get_random_seed_long(&block.rdseed[i]) && |
2088 |
++ !arch_get_random_long(&block.rdseed[i])) |
2089 |
++ block.rdseed[i] = random_get_entropy(); |
2090 |
++ } |
2091 |
+ |
2092 |
+- if (delta < 0) |
2093 |
+- delta = -delta; |
2094 |
+- if (delta2 < 0) |
2095 |
+- delta2 = -delta2; |
2096 |
+- if (delta3 < 0) |
2097 |
+- delta3 = -delta3; |
2098 |
+- if (delta > delta2) |
2099 |
+- delta = delta2; |
2100 |
+- if (delta > delta3) |
2101 |
+- delta = delta3; |
2102 |
++ spin_lock_irqsave(&input_pool.lock, flags); |
2103 |
+ |
2104 |
+- /* |
2105 |
+- * delta is now minimum absolute delta. |
2106 |
+- * Round down by 1 bit on general principles, |
2107 |
+- * and limit entropy estimate to 12 bits. |
2108 |
+- */ |
2109 |
+- credit_entropy_bits(min_t(int, fls(delta >> 1), 11)); |
2110 |
+-} |
2111 |
++ /* seed = HASHPRF(last_key, entropy_input) */ |
2112 |
++ blake2s_final(&input_pool.hash, seed); |
2113 |
+ |
2114 |
+-void add_input_randomness(unsigned int type, unsigned int code, |
2115 |
+- unsigned int value) |
2116 |
+-{ |
2117 |
+- static unsigned char last_value; |
2118 |
++ /* next_key = HASHPRF(seed, RDSEED || 0) */ |
2119 |
++ block.counter = 0; |
2120 |
++ blake2s(next_key, (u8 *)&block, seed, sizeof(next_key), sizeof(block), sizeof(seed)); |
2121 |
++ blake2s_init_key(&input_pool.hash, BLAKE2S_HASH_SIZE, next_key, sizeof(next_key)); |
2122 |
+ |
2123 |
+- /* ignore autorepeat and the like */ |
2124 |
+- if (value == last_value) |
2125 |
+- return; |
2126 |
++ spin_unlock_irqrestore(&input_pool.lock, flags); |
2127 |
++ memzero_explicit(next_key, sizeof(next_key)); |
2128 |
++ |
2129 |
++ while (len) { |
2130 |
++ i = min_t(size_t, len, BLAKE2S_HASH_SIZE); |
2131 |
++ /* output = HASHPRF(seed, RDSEED || ++counter) */ |
2132 |
++ ++block.counter; |
2133 |
++ blake2s(buf, (u8 *)&block, seed, i, sizeof(block), sizeof(seed)); |
2134 |
++ len -= i; |
2135 |
++ buf += i; |
2136 |
++ } |
2137 |
+ |
2138 |
+- last_value = value; |
2139 |
+- add_timer_randomness(&input_timer_state, |
2140 |
+- (type << 4) ^ code ^ (code >> 4) ^ value); |
2141 |
+- trace_add_input_randomness(POOL_ENTROPY_BITS()); |
2142 |
++ memzero_explicit(seed, sizeof(seed)); |
2143 |
++ memzero_explicit(&block, sizeof(block)); |
2144 |
+ } |
2145 |
+-EXPORT_SYMBOL_GPL(add_input_randomness); |
2146 |
+- |
2147 |
+-static DEFINE_PER_CPU(struct fast_pool, irq_randomness); |
2148 |
+ |
2149 |
+-#ifdef ADD_INTERRUPT_BENCH |
2150 |
+-static unsigned long avg_cycles, avg_deviation; |
2151 |
++#define credit_init_bits(bits) if (!crng_ready()) _credit_init_bits(bits) |
2152 |
+ |
2153 |
+-#define AVG_SHIFT 8 /* Exponential average factor k=1/256 */ |
2154 |
+-#define FIXED_1_2 (1 << (AVG_SHIFT - 1)) |
2155 |
+- |
2156 |
+-static void add_interrupt_bench(cycles_t start) |
2157 |
++static void __cold _credit_init_bits(size_t bits) |
2158 |
+ { |
2159 |
+- long delta = random_get_entropy() - start; |
2160 |
+- |
2161 |
+- /* Use a weighted moving average */ |
2162 |
+- delta = delta - ((avg_cycles + FIXED_1_2) >> AVG_SHIFT); |
2163 |
+- avg_cycles += delta; |
2164 |
+- /* And average deviation */ |
2165 |
+- delta = abs(delta) - ((avg_deviation + FIXED_1_2) >> AVG_SHIFT); |
2166 |
+- avg_deviation += delta; |
2167 |
+-} |
2168 |
+-#else |
2169 |
+-#define add_interrupt_bench(x) |
2170 |
+-#endif |
2171 |
++ static struct execute_work set_ready; |
2172 |
++ unsigned int new, orig, add; |
2173 |
++ unsigned long flags; |
2174 |
+ |
2175 |
+-static u32 get_reg(struct fast_pool *f, struct pt_regs *regs) |
2176 |
+-{ |
2177 |
+- u32 *ptr = (u32 *)regs; |
2178 |
+- unsigned int idx; |
2179 |
++ if (!bits) |
2180 |
++ return; |
2181 |
+ |
2182 |
+- if (regs == NULL) |
2183 |
+- return 0; |
2184 |
+- idx = READ_ONCE(f->reg_idx); |
2185 |
+- if (idx >= sizeof(struct pt_regs) / sizeof(u32)) |
2186 |
+- idx = 0; |
2187 |
+- ptr += idx++; |
2188 |
+- WRITE_ONCE(f->reg_idx, idx); |
2189 |
+- return *ptr; |
2190 |
+-} |
2191 |
++ add = min_t(size_t, bits, POOL_BITS); |
2192 |
+ |
2193 |
+-void add_interrupt_randomness(int irq) |
2194 |
+-{ |
2195 |
+- struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness); |
2196 |
+- struct pt_regs *regs = get_irq_regs(); |
2197 |
+- unsigned long now = jiffies; |
2198 |
+- cycles_t cycles = random_get_entropy(); |
2199 |
+- u32 c_high, j_high; |
2200 |
+- u64 ip; |
2201 |
+- |
2202 |
+- if (cycles == 0) |
2203 |
+- cycles = get_reg(fast_pool, regs); |
2204 |
+- c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0; |
2205 |
+- j_high = (sizeof(now) > 4) ? now >> 32 : 0; |
2206 |
+- fast_pool->pool[0] ^= cycles ^ j_high ^ irq; |
2207 |
+- fast_pool->pool[1] ^= now ^ c_high; |
2208 |
+- ip = regs ? instruction_pointer(regs) : _RET_IP_; |
2209 |
+- fast_pool->pool[2] ^= ip; |
2210 |
+- fast_pool->pool[3] ^= |
2211 |
+- (sizeof(ip) > 4) ? ip >> 32 : get_reg(fast_pool, regs); |
2212 |
+- |
2213 |
+- fast_mix(fast_pool); |
2214 |
+- add_interrupt_bench(cycles); |
2215 |
+- |
2216 |
+- if (unlikely(crng_init == 0)) { |
2217 |
+- if ((fast_pool->count >= 64) && |
2218 |
+- crng_fast_load((u8 *)fast_pool->pool, sizeof(fast_pool->pool)) > 0) { |
2219 |
+- fast_pool->count = 0; |
2220 |
+- fast_pool->last = now; |
2221 |
++ do { |
2222 |
++ orig = READ_ONCE(input_pool.init_bits); |
2223 |
++ new = min_t(unsigned int, POOL_BITS, orig + add); |
2224 |
++ } while (cmpxchg(&input_pool.init_bits, orig, new) != orig); |
2225 |
++ |
2226 |
++ if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) { |
2227 |
++ crng_reseed(); /* Sets crng_init to CRNG_READY under base_crng.lock. */ |
2228 |
++ execute_in_process_context(crng_set_ready, &set_ready); |
2229 |
++ process_random_ready_list(); |
2230 |
++ wake_up_interruptible(&crng_init_wait); |
2231 |
++ kill_fasync(&fasync, SIGIO, POLL_IN); |
2232 |
++ pr_notice("crng init done\n"); |
2233 |
++ if (urandom_warning.missed) |
2234 |
++ pr_notice("%d urandom warning(s) missed due to ratelimiting\n", |
2235 |
++ urandom_warning.missed); |
2236 |
++ } else if (orig < POOL_EARLY_BITS && new >= POOL_EARLY_BITS) { |
2237 |
++ spin_lock_irqsave(&base_crng.lock, flags); |
2238 |
++ /* Check if crng_init is CRNG_EMPTY, to avoid race with crng_reseed(). */ |
2239 |
++ if (crng_init == CRNG_EMPTY) { |
2240 |
++ extract_entropy(base_crng.key, sizeof(base_crng.key)); |
2241 |
++ crng_init = CRNG_EARLY; |
2242 |
+ } |
2243 |
+- return; |
2244 |
++ spin_unlock_irqrestore(&base_crng.lock, flags); |
2245 |
+ } |
2246 |
++} |
2247 |
+ |
2248 |
+- if ((fast_pool->count < 64) && !time_after(now, fast_pool->last + HZ)) |
2249 |
+- return; |
2250 |
+- |
2251 |
+- if (!spin_trylock(&input_pool.lock)) |
2252 |
+- return; |
2253 |
+- |
2254 |
+- fast_pool->last = now; |
2255 |
+- __mix_pool_bytes(&fast_pool->pool, sizeof(fast_pool->pool)); |
2256 |
+- spin_unlock(&input_pool.lock); |
2257 |
+ |
2258 |
+- fast_pool->count = 0; |
2259 |
++/********************************************************************** |
2260 |
++ * |
2261 |
++ * Entropy collection routines. |
2262 |
++ * |
2263 |
++ * The following exported functions are used for pushing entropy into |
2264 |
++ * the above entropy accumulation routines: |
2265 |
++ * |
2266 |
++ * void add_device_randomness(const void *buf, size_t len); |
2267 |
++ * void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy); |
2268 |
++ * void add_bootloader_randomness(const void *buf, size_t len); |
2269 |
++ * void add_interrupt_randomness(int irq); |
2270 |
++ * void add_input_randomness(unsigned int type, unsigned int code, unsigned int value); |
2271 |
++ * void add_disk_randomness(struct gendisk *disk); |
2272 |
++ * |
2273 |
++ * add_device_randomness() adds data to the input pool that |
2274 |
++ * is likely to differ between two devices (or possibly even per boot). |
2275 |
++ * This would be things like MAC addresses or serial numbers, or the |
2276 |
++ * read-out of the RTC. This does *not* credit any actual entropy to |
2277 |
++ * the pool, but it initializes the pool to different values for devices |
2278 |
++ * that might otherwise be identical and have very little entropy |
2279 |
++ * available to them (particularly common in the embedded world). |
2280 |
++ * |
2281 |
++ * add_hwgenerator_randomness() is for true hardware RNGs, and will credit |
2282 |
++ * entropy as specified by the caller. If the entropy pool is full it will |
2283 |
++ * block until more entropy is needed. |
2284 |
++ * |
2285 |
++ * add_bootloader_randomness() is called by bootloader drivers, such as EFI |
2286 |
++ * and device tree, and credits its input depending on whether or not the |
2287 |
++ * configuration option CONFIG_RANDOM_TRUST_BOOTLOADER is set. |
2288 |
++ * |
2289 |
++ * add_interrupt_randomness() uses the interrupt timing as random |
2290 |
++ * inputs to the entropy pool. Using the cycle counters and the irq source |
2291 |
++ * as inputs, it feeds the input pool roughly once a second or after 64 |
2292 |
++ * interrupts, crediting 1 bit of entropy for whichever comes first. |
2293 |
++ * |
2294 |
++ * add_input_randomness() uses the input layer interrupt timing, as well |
2295 |
++ * as the event type information from the hardware. |
2296 |
++ * |
2297 |
++ * add_disk_randomness() uses what amounts to the seek time of block |
2298 |
++ * layer request events, on a per-disk_devt basis, as input to the |
2299 |
++ * entropy pool. Note that high-speed solid state drives with very low |
2300 |
++ * seek times do not make for good sources of entropy, as their seek |
2301 |
++ * times are usually fairly consistent. |
2302 |
++ * |
2303 |
++ * The last two routines try to estimate how many bits of entropy |
2304 |
++ * to credit. They do this by keeping track of the first and second |
2305 |
++ * order deltas of the event timings. |
2306 |
++ * |
2307 |
++ **********************************************************************/ |
2308 |
+ |
2309 |
+- /* award one bit for the contents of the fast pool */ |
2310 |
+- credit_entropy_bits(1); |
2311 |
++static bool trust_cpu __ro_after_init = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU); |
2312 |
++static bool trust_bootloader __ro_after_init = IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER); |
2313 |
++static int __init parse_trust_cpu(char *arg) |
2314 |
++{ |
2315 |
++ return kstrtobool(arg, &trust_cpu); |
2316 |
+ } |
2317 |
+-EXPORT_SYMBOL_GPL(add_interrupt_randomness); |
2318 |
+- |
2319 |
+-#ifdef CONFIG_BLOCK |
2320 |
+-void add_disk_randomness(struct gendisk *disk) |
2321 |
++static int __init parse_trust_bootloader(char *arg) |
2322 |
+ { |
2323 |
+- if (!disk || !disk->random) |
2324 |
+- return; |
2325 |
+- /* first major is 1, so we get >= 0x200 here */ |
2326 |
+- add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); |
2327 |
+- trace_add_disk_randomness(disk_devt(disk), POOL_ENTROPY_BITS()); |
2328 |
++ return kstrtobool(arg, &trust_bootloader); |
2329 |
+ } |
2330 |
+-EXPORT_SYMBOL_GPL(add_disk_randomness); |
2331 |
+-#endif |
2332 |
+- |
2333 |
+-/********************************************************************* |
2334 |
+- * |
2335 |
+- * Entropy extraction routines |
2336 |
+- * |
2337 |
+- *********************************************************************/ |
2338 |
++early_param("random.trust_cpu", parse_trust_cpu); |
2339 |
++early_param("random.trust_bootloader", parse_trust_bootloader); |
2340 |
+ |
2341 |
+ /* |
2342 |
+- * This function decides how many bytes to actually take from the |
2343 |
+- * given pool, and also debits the entropy count accordingly. |
2344 |
++ * The first collection of entropy occurs at system boot while interrupts |
2345 |
++ * are still turned off. Here we push in latent entropy, RDSEED, a timestamp, |
2346 |
++ * utsname(), and the command line. Depending on the above configuration knob, |
2347 |
++ * RDSEED may be considered sufficient for initialization. Note that much |
2348 |
++ * earlier setup may already have pushed entropy into the input pool by the |
2349 |
++ * time we get here. |
2350 |
+ */ |
2351 |
+-static size_t account(size_t nbytes, int min) |
2352 |
++int __init random_init(const char *command_line) |
2353 |
+ { |
2354 |
+- int entropy_count, orig; |
2355 |
+- size_t ibytes, nfrac; |
2356 |
++ ktime_t now = ktime_get_real(); |
2357 |
++ unsigned int i, arch_bytes; |
2358 |
++ unsigned long entropy; |
2359 |
+ |
2360 |
+- BUG_ON(input_pool.entropy_count > POOL_FRACBITS); |
2361 |
++#if defined(LATENT_ENTROPY_PLUGIN) |
2362 |
++ static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy; |
2363 |
++ _mix_pool_bytes(compiletime_seed, sizeof(compiletime_seed)); |
2364 |
++#endif |
2365 |
+ |
2366 |
+- /* Can we pull enough? */ |
2367 |
+-retry: |
2368 |
+- entropy_count = orig = READ_ONCE(input_pool.entropy_count); |
2369 |
+- if (WARN_ON(entropy_count < 0)) { |
2370 |
+- pr_warn("negative entropy count: count %d\n", entropy_count); |
2371 |
+- entropy_count = 0; |
2372 |
++ for (i = 0, arch_bytes = BLAKE2S_BLOCK_SIZE; |
2373 |
++ i < BLAKE2S_BLOCK_SIZE; i += sizeof(entropy)) { |
2374 |
++ if (!arch_get_random_seed_long_early(&entropy) && |
2375 |
++ !arch_get_random_long_early(&entropy)) { |
2376 |
++ entropy = random_get_entropy(); |
2377 |
++ arch_bytes -= sizeof(entropy); |
2378 |
++ } |
2379 |
++ _mix_pool_bytes(&entropy, sizeof(entropy)); |
2380 |
+ } |
2381 |
++ _mix_pool_bytes(&now, sizeof(now)); |
2382 |
++ _mix_pool_bytes(utsname(), sizeof(*(utsname()))); |
2383 |
++ _mix_pool_bytes(command_line, strlen(command_line)); |
2384 |
++ add_latent_entropy(); |
2385 |
+ |
2386 |
+- /* never pull more than available */ |
2387 |
+- ibytes = min_t(size_t, nbytes, entropy_count >> (POOL_ENTROPY_SHIFT + 3)); |
2388 |
+- if (ibytes < min) |
2389 |
+- ibytes = 0; |
2390 |
+- nfrac = ibytes << (POOL_ENTROPY_SHIFT + 3); |
2391 |
+- if ((size_t)entropy_count > nfrac) |
2392 |
+- entropy_count -= nfrac; |
2393 |
+- else |
2394 |
+- entropy_count = 0; |
2395 |
+- |
2396 |
+- if (cmpxchg(&input_pool.entropy_count, orig, entropy_count) != orig) |
2397 |
+- goto retry; |
2398 |
+- |
2399 |
+- trace_debit_entropy(8 * ibytes); |
2400 |
+- if (ibytes && POOL_ENTROPY_BITS() < random_write_wakeup_bits) { |
2401 |
+- wake_up_interruptible(&random_write_wait); |
2402 |
+- kill_fasync(&fasync, SIGIO, POLL_OUT); |
2403 |
+- } |
2404 |
++ if (crng_ready()) |
2405 |
++ crng_reseed(); |
2406 |
++ else if (trust_cpu) |
2407 |
++ credit_init_bits(arch_bytes * 8); |
2408 |
+ |
2409 |
+- return ibytes; |
2410 |
++ return 0; |
2411 |
+ } |
2412 |
+ |
2413 |
+ /* |
2414 |
+- * This function does the actual extraction for extract_entropy. |
2415 |
++ * Add device- or boot-specific data to the input pool to help |
2416 |
++ * initialize it. |
2417 |
+ * |
2418 |
+- * Note: we assume that .poolwords is a multiple of 16 words. |
2419 |
++ * None of this adds any entropy; it is meant to avoid the problem of |
2420 |
++ * the entropy pool having similar initial state across largely |
2421 |
++ * identical devices. |
2422 |
+ */ |
2423 |
+-static void extract_buf(u8 *out) |
2424 |
++void add_device_randomness(const void *buf, size_t len) |
2425 |
+ { |
2426 |
+- struct blake2s_state state __aligned(__alignof__(unsigned long)); |
2427 |
+- u8 hash[BLAKE2S_HASH_SIZE]; |
2428 |
+- unsigned long *salt; |
2429 |
++ unsigned long entropy = random_get_entropy(); |
2430 |
+ unsigned long flags; |
2431 |
+ |
2432 |
+- blake2s_init(&state, sizeof(hash)); |
2433 |
+- |
2434 |
+- /* |
2435 |
+- * If we have an architectural hardware random number |
2436 |
+- * generator, use it for BLAKE2's salt & personal fields. |
2437 |
+- */ |
2438 |
+- for (salt = (unsigned long *)&state.h[4]; |
2439 |
+- salt < (unsigned long *)&state.h[8]; ++salt) { |
2440 |
+- unsigned long v; |
2441 |
+- if (!arch_get_random_long(&v)) |
2442 |
+- break; |
2443 |
+- *salt ^= v; |
2444 |
+- } |
2445 |
+- |
2446 |
+- /* Generate a hash across the pool */ |
2447 |
+ spin_lock_irqsave(&input_pool.lock, flags); |
2448 |
+- blake2s_update(&state, (const u8 *)input_pool_data, POOL_BYTES); |
2449 |
+- blake2s_final(&state, hash); /* final zeros out state */ |
2450 |
+- |
2451 |
+- /* |
2452 |
+- * We mix the hash back into the pool to prevent backtracking |
2453 |
+- * attacks (where the attacker knows the state of the pool |
2454 |
+- * plus the current outputs, and attempts to find previous |
2455 |
+- * outputs), unless the hash function can be inverted. By |
2456 |
+- * mixing at least a hash worth of hash data back, we make |
2457 |
+- * brute-forcing the feedback as hard as brute-forcing the |
2458 |
+- * hash. |
2459 |
+- */ |
2460 |
+- __mix_pool_bytes(hash, sizeof(hash)); |
2461 |
++ _mix_pool_bytes(&entropy, sizeof(entropy)); |
2462 |
++ _mix_pool_bytes(buf, len); |
2463 |
+ spin_unlock_irqrestore(&input_pool.lock, flags); |
2464 |
+- |
2465 |
+- /* Note that EXTRACT_SIZE is half of hash size here, because above |
2466 |
+- * we've dumped the full length back into mixer. By reducing the |
2467 |
+- * amount that we emit, we retain a level of forward secrecy. |
2468 |
+- */ |
2469 |
+- memcpy(out, hash, EXTRACT_SIZE); |
2470 |
+- memzero_explicit(hash, sizeof(hash)); |
2471 |
+ } |
2472 |
++EXPORT_SYMBOL(add_device_randomness); |
2473 |
+ |
2474 |
+-static ssize_t _extract_entropy(void *buf, size_t nbytes) |
2475 |
++/* |
2476 |
++ * Interface for in-kernel drivers of true hardware RNGs. |
2477 |
++ * Those devices may produce endless random bits and will be throttled |
2478 |
++ * when our pool is full. |
2479 |
++ */ |
2480 |
++void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy) |
2481 |
+ { |
2482 |
+- ssize_t ret = 0, i; |
2483 |
+- u8 tmp[EXTRACT_SIZE]; |
2484 |
+- |
2485 |
+- while (nbytes) { |
2486 |
+- extract_buf(tmp); |
2487 |
+- i = min_t(int, nbytes, EXTRACT_SIZE); |
2488 |
+- memcpy(buf, tmp, i); |
2489 |
+- nbytes -= i; |
2490 |
+- buf += i; |
2491 |
+- ret += i; |
2492 |
+- } |
2493 |
++ mix_pool_bytes(buf, len); |
2494 |
++ credit_init_bits(entropy); |
2495 |
+ |
2496 |
+- /* Wipe data just returned from memory */ |
2497 |
+- memzero_explicit(tmp, sizeof(tmp)); |
2498 |
+- |
2499 |
+- return ret; |
2500 |
++ /* |
2501 |
++ * Throttle writing to once every CRNG_RESEED_INTERVAL, unless |
2502 |
++ * we're not yet initialized. |
2503 |
++ */ |
2504 |
++ if (!kthread_should_stop() && crng_ready()) |
2505 |
++ schedule_timeout_interruptible(CRNG_RESEED_INTERVAL); |
2506 |
+ } |
2507 |
++EXPORT_SYMBOL_GPL(add_hwgenerator_randomness); |
2508 |
+ |
2509 |
+ /* |
2510 |
+- * This function extracts randomness from the "entropy pool", and |
2511 |
+- * returns it in a buffer. |
2512 |
+- * |
2513 |
+- * The min parameter specifies the minimum amount we can pull before |
2514 |
+- * failing to avoid races that defeat catastrophic reseeding. |
2515 |
++ * Handle random seed passed by bootloader, and credit it if |
2516 |
++ * CONFIG_RANDOM_TRUST_BOOTLOADER is set. |
2517 |
+ */ |
2518 |
+-static ssize_t extract_entropy(void *buf, size_t nbytes, int min) |
2519 |
++void __cold add_bootloader_randomness(const void *buf, size_t len) |
2520 |
+ { |
2521 |
+- trace_extract_entropy(nbytes, POOL_ENTROPY_BITS(), _RET_IP_); |
2522 |
+- nbytes = account(nbytes, min); |
2523 |
+- return _extract_entropy(buf, nbytes); |
2524 |
++ mix_pool_bytes(buf, len); |
2525 |
++ if (trust_bootloader) |
2526 |
++ credit_init_bits(len * 8); |
2527 |
+ } |
2528 |
++EXPORT_SYMBOL_GPL(add_bootloader_randomness); |
2529 |
+ |
2530 |
+-#define warn_unseeded_randomness(previous) \ |
2531 |
+- _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous)) |
2532 |
++struct fast_pool { |
2533 |
++ struct work_struct mix; |
2534 |
++ unsigned long pool[4]; |
2535 |
++ unsigned long last; |
2536 |
++ unsigned int count; |
2537 |
++}; |
2538 |
+ |
2539 |
+-static void _warn_unseeded_randomness(const char *func_name, void *caller, void **previous) |
2540 |
+-{ |
2541 |
+-#ifdef CONFIG_WARN_ALL_UNSEEDED_RANDOM |
2542 |
+- const bool print_once = false; |
2543 |
++static DEFINE_PER_CPU(struct fast_pool, irq_randomness) = { |
2544 |
++#ifdef CONFIG_64BIT |
2545 |
++#define FASTMIX_PERM SIPHASH_PERMUTATION |
2546 |
++ .pool = { SIPHASH_CONST_0, SIPHASH_CONST_1, SIPHASH_CONST_2, SIPHASH_CONST_3 } |
2547 |
+ #else |
2548 |
+- static bool print_once __read_mostly; |
2549 |
+-#endif |
2550 |
+- |
2551 |
+- if (print_once || crng_ready() || |
2552 |
+- (previous && (caller == READ_ONCE(*previous)))) |
2553 |
+- return; |
2554 |
+- WRITE_ONCE(*previous, caller); |
2555 |
+-#ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM |
2556 |
+- print_once = true; |
2557 |
++#define FASTMIX_PERM HSIPHASH_PERMUTATION |
2558 |
++ .pool = { HSIPHASH_CONST_0, HSIPHASH_CONST_1, HSIPHASH_CONST_2, HSIPHASH_CONST_3 } |
2559 |
+ #endif |
2560 |
+- if (__ratelimit(&unseeded_warning)) |
2561 |
+- printk_deferred(KERN_NOTICE "random: %s called from %pS with crng_init=%d\n", |
2562 |
+- func_name, caller, crng_init); |
2563 |
+-} |
2564 |
++}; |
2565 |
+ |
2566 |
+ /* |
2567 |
+- * This function is the exported kernel interface. It returns some |
2568 |
+- * number of good random numbers, suitable for key generation, seeding |
2569 |
+- * TCP sequence numbers, etc. It does not rely on the hardware random |
2570 |
+- * number generator. For random bytes direct from the hardware RNG |
2571 |
+- * (when available), use get_random_bytes_arch(). In order to ensure |
2572 |
+- * that the randomness provided by this function is okay, the function |
2573 |
+- * wait_for_random_bytes() should be called and return 0 at least once |
2574 |
+- * at any point prior. |
2575 |
++ * This is [Half]SipHash-1-x, starting from an empty key. Because |
2576 |
++ * the key is fixed, it assumes that its inputs are non-malicious, |
2577 |
++ * and therefore this has no security on its own. s represents the |
2578 |
++ * four-word SipHash state, while v represents a two-word input. |
2579 |
+ */ |
2580 |
+-static void _get_random_bytes(void *buf, int nbytes) |
2581 |
+-{ |
2582 |
+- u8 tmp[CHACHA_BLOCK_SIZE] __aligned(4); |
2583 |
+- |
2584 |
+- trace_get_random_bytes(nbytes, _RET_IP_); |
2585 |
+- |
2586 |
+- while (nbytes >= CHACHA_BLOCK_SIZE) { |
2587 |
+- extract_crng(buf); |
2588 |
+- buf += CHACHA_BLOCK_SIZE; |
2589 |
+- nbytes -= CHACHA_BLOCK_SIZE; |
2590 |
+- } |
2591 |
+- |
2592 |
+- if (nbytes > 0) { |
2593 |
+- extract_crng(tmp); |
2594 |
+- memcpy(buf, tmp, nbytes); |
2595 |
+- crng_backtrack_protect(tmp, nbytes); |
2596 |
+- } else |
2597 |
+- crng_backtrack_protect(tmp, CHACHA_BLOCK_SIZE); |
2598 |
+- memzero_explicit(tmp, sizeof(tmp)); |
2599 |
+-} |
2600 |
+- |
2601 |
+-void get_random_bytes(void *buf, int nbytes) |
2602 |
++static void fast_mix(unsigned long s[4], unsigned long v1, unsigned long v2) |
2603 |
+ { |
2604 |
+- static void *previous; |
2605 |
+- |
2606 |
+- warn_unseeded_randomness(&previous); |
2607 |
+- _get_random_bytes(buf, nbytes); |
2608 |
++ s[3] ^= v1; |
2609 |
++ FASTMIX_PERM(s[0], s[1], s[2], s[3]); |
2610 |
++ s[0] ^= v1; |
2611 |
++ s[3] ^= v2; |
2612 |
++ FASTMIX_PERM(s[0], s[1], s[2], s[3]); |
2613 |
++ s[0] ^= v2; |
2614 |
+ } |
2615 |
+-EXPORT_SYMBOL(get_random_bytes); |
2616 |
+ |
2617 |
++#ifdef CONFIG_SMP |
2618 |
+ /* |
2619 |
+- * Each time the timer fires, we expect that we got an unpredictable |
2620 |
+- * jump in the cycle counter. Even if the timer is running on another |
2621 |
+- * CPU, the timer activity will be touching the stack of the CPU that is |
2622 |
+- * generating entropy.. |
2623 |
+- * |
2624 |
+- * Note that we don't re-arm the timer in the timer itself - we are |
2625 |
+- * happy to be scheduled away, since that just makes the load more |
2626 |
+- * complex, but we do not want the timer to keep ticking unless the |
2627 |
+- * entropy loop is running. |
2628 |
+- * |
2629 |
+- * So the re-arming always happens in the entropy loop itself. |
2630 |
++ * This function is called when the CPU has just come online, with |
2631 |
++ * entry CPUHP_AP_RANDOM_ONLINE, just after CPUHP_AP_WORKQUEUE_ONLINE. |
2632 |
+ */ |
2633 |
+-static void entropy_timer(struct timer_list *t) |
2634 |
++int __cold random_online_cpu(unsigned int cpu) |
2635 |
+ { |
2636 |
+- credit_entropy_bits(1); |
2637 |
++ /* |
2638 |
++ * During CPU shutdown and before CPU onlining, add_interrupt_ |
2639 |
++ * randomness() may schedule mix_interrupt_randomness(), and |
2640 |
++ * set the MIX_INFLIGHT flag. However, because the worker can |
2641 |
++ * be scheduled on a different CPU during this period, that |
2642 |
++ * flag will never be cleared. For that reason, we zero out |
2643 |
++ * the flag here, which runs just after workqueues are onlined |
2644 |
++ * for the CPU again. This also has the effect of setting the |
2645 |
++ * irq randomness count to zero so that new accumulated irqs |
2646 |
++ * are fresh. |
2647 |
++ */ |
2648 |
++ per_cpu_ptr(&irq_randomness, cpu)->count = 0; |
2649 |
++ return 0; |
2650 |
+ } |
2651 |
++#endif |
2652 |
+ |
2653 |
+-/* |
2654 |
+- * If we have an actual cycle counter, see if we can |
2655 |
+- * generate enough entropy with timing noise |
2656 |
+- */ |
2657 |
+-static void try_to_generate_entropy(void) |
2658 |
++static void mix_interrupt_randomness(struct work_struct *work) |
2659 |
+ { |
2660 |
+- struct { |
2661 |
+- unsigned long now; |
2662 |
+- struct timer_list timer; |
2663 |
+- } stack; |
2664 |
+- |
2665 |
+- stack.now = random_get_entropy(); |
2666 |
++ struct fast_pool *fast_pool = container_of(work, struct fast_pool, mix); |
2667 |
++ /* |
2668 |
++ * The size of the copied stack pool is explicitly 2 longs so that we |
2669 |
++ * only ever ingest half of the siphash output each time, retaining |
2670 |
++ * the other half as the next "key" that carries over. The entropy is |
2671 |
++ * supposed to be sufficiently dispersed between bits so on average |
2672 |
++ * we don't wind up "losing" some. |
2673 |
++ */ |
2674 |
++ unsigned long pool[2]; |
2675 |
++ unsigned int count; |
2676 |
+ |
2677 |
+- /* Slow counter - or none. Don't even bother */ |
2678 |
+- if (stack.now == random_get_entropy()) |
2679 |
++ /* Check to see if we're running on the wrong CPU due to hotplug. */ |
2680 |
++ local_irq_disable(); |
2681 |
++ if (fast_pool != this_cpu_ptr(&irq_randomness)) { |
2682 |
++ local_irq_enable(); |
2683 |
+ return; |
2684 |
+- |
2685 |
+- timer_setup_on_stack(&stack.timer, entropy_timer, 0); |
2686 |
+- while (!crng_ready()) { |
2687 |
+- if (!timer_pending(&stack.timer)) |
2688 |
+- mod_timer(&stack.timer, jiffies + 1); |
2689 |
+- mix_pool_bytes(&stack.now, sizeof(stack.now)); |
2690 |
+- schedule(); |
2691 |
+- stack.now = random_get_entropy(); |
2692 |
+ } |
2693 |
+ |
2694 |
+- del_timer_sync(&stack.timer); |
2695 |
+- destroy_timer_on_stack(&stack.timer); |
2696 |
+- mix_pool_bytes(&stack.now, sizeof(stack.now)); |
2697 |
+-} |
2698 |
+- |
2699 |
+-/* |
2700 |
+- * Wait for the urandom pool to be seeded and thus guaranteed to supply |
2701 |
+- * cryptographically secure random numbers. This applies to: the /dev/urandom |
2702 |
+- * device, the get_random_bytes function, and the get_random_{u32,u64,int,long} |
2703 |
+- * family of functions. Using any of these functions without first calling |
2704 |
+- * this function forfeits the guarantee of security. |
2705 |
+- * |
2706 |
+- * Returns: 0 if the urandom pool has been seeded. |
2707 |
+- * -ERESTARTSYS if the function was interrupted by a signal. |
2708 |
+- */ |
2709 |
+-int wait_for_random_bytes(void) |
2710 |
+-{ |
2711 |
+- if (likely(crng_ready())) |
2712 |
+- return 0; |
2713 |
+- |
2714 |
+- do { |
2715 |
+- int ret; |
2716 |
+- ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ); |
2717 |
+- if (ret) |
2718 |
+- return ret > 0 ? 0 : ret; |
2719 |
++ /* |
2720 |
++ * Copy the pool to the stack so that the mixer always has a |
2721 |
++ * consistent view, before we reenable irqs again. |
2722 |
++ */ |
2723 |
++ memcpy(pool, fast_pool->pool, sizeof(pool)); |
2724 |
++ count = fast_pool->count; |
2725 |
++ fast_pool->count = 0; |
2726 |
++ fast_pool->last = jiffies; |
2727 |
++ local_irq_enable(); |
2728 |
+ |
2729 |
+- try_to_generate_entropy(); |
2730 |
+- } while (!crng_ready()); |
2731 |
++ mix_pool_bytes(pool, sizeof(pool)); |
2732 |
++ credit_init_bits(max(1u, (count & U16_MAX) / 64)); |
2733 |
+ |
2734 |
+- return 0; |
2735 |
++ memzero_explicit(pool, sizeof(pool)); |
2736 |
+ } |
2737 |
+-EXPORT_SYMBOL(wait_for_random_bytes); |
2738 |
+ |
2739 |
+-/* |
2740 |
+- * Returns whether or not the urandom pool has been seeded and thus guaranteed |
2741 |
+- * to supply cryptographically secure random numbers. This applies to: the |
2742 |
+- * /dev/urandom device, the get_random_bytes function, and the get_random_{u32, |
2743 |
+- * ,u64,int,long} family of functions. |
2744 |
+- * |
2745 |
+- * Returns: true if the urandom pool has been seeded. |
2746 |
+- * false if the urandom pool has not been seeded. |
2747 |
+- */ |
2748 |
+-bool rng_is_initialized(void) |
2749 |
+-{ |
2750 |
+- return crng_ready(); |
2751 |
+-} |
2752 |
+-EXPORT_SYMBOL(rng_is_initialized); |
2753 |
+- |
2754 |
+-/* |
2755 |
+- * Add a callback function that will be invoked when the nonblocking |
2756 |
+- * pool is initialised. |
2757 |
+- * |
2758 |
+- * returns: 0 if callback is successfully added |
2759 |
+- * -EALREADY if pool is already initialised (callback not called) |
2760 |
+- * -ENOENT if module for callback is not alive |
2761 |
+- */ |
2762 |
+-int add_random_ready_callback(struct random_ready_callback *rdy) |
2763 |
++void add_interrupt_randomness(int irq) |
2764 |
+ { |
2765 |
+- struct module *owner; |
2766 |
+- unsigned long flags; |
2767 |
+- int err = -EALREADY; |
2768 |
+- |
2769 |
+- if (crng_ready()) |
2770 |
+- return err; |
2771 |
+- |
2772 |
+- owner = rdy->owner; |
2773 |
+- if (!try_module_get(owner)) |
2774 |
+- return -ENOENT; |
2775 |
+- |
2776 |
+- spin_lock_irqsave(&random_ready_list_lock, flags); |
2777 |
+- if (crng_ready()) |
2778 |
+- goto out; |
2779 |
+- |
2780 |
+- owner = NULL; |
2781 |
++ enum { MIX_INFLIGHT = 1U << 31 }; |
2782 |
++ unsigned long entropy = random_get_entropy(); |
2783 |
++ struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness); |
2784 |
++ struct pt_regs *regs = get_irq_regs(); |
2785 |
++ unsigned int new_count; |
2786 |
+ |
2787 |
+- list_add(&rdy->list, &random_ready_list); |
2788 |
+- err = 0; |
2789 |
++ fast_mix(fast_pool->pool, entropy, |
2790 |
++ (regs ? instruction_pointer(regs) : _RET_IP_) ^ swab(irq)); |
2791 |
++ new_count = ++fast_pool->count; |
2792 |
+ |
2793 |
+-out: |
2794 |
+- spin_unlock_irqrestore(&random_ready_list_lock, flags); |
2795 |
++ if (new_count & MIX_INFLIGHT) |
2796 |
++ return; |
2797 |
+ |
2798 |
+- module_put(owner); |
2799 |
++ if (new_count < 64 && !time_is_before_jiffies(fast_pool->last + HZ)) |
2800 |
++ return; |
2801 |
+ |
2802 |
+- return err; |
2803 |
++ if (unlikely(!fast_pool->mix.func)) |
2804 |
++ INIT_WORK(&fast_pool->mix, mix_interrupt_randomness); |
2805 |
++ fast_pool->count |= MIX_INFLIGHT; |
2806 |
++ queue_work_on(raw_smp_processor_id(), system_highpri_wq, &fast_pool->mix); |
2807 |
+ } |
2808 |
+-EXPORT_SYMBOL(add_random_ready_callback); |
2809 |
++EXPORT_SYMBOL_GPL(add_interrupt_randomness); |
2810 |
++ |
2811 |
++/* There is one of these per entropy source */ |
2812 |
++struct timer_rand_state { |
2813 |
++ unsigned long last_time; |
2814 |
++ long last_delta, last_delta2; |
2815 |
++}; |
2816 |
+ |
2817 |
+ /* |
2818 |
+- * Delete a previously registered readiness callback function. |
2819 |
++ * This function adds entropy to the entropy "pool" by using timing |
2820 |
++ * delays. It uses the timer_rand_state structure to make an estimate |
2821 |
++ * of how many bits of entropy this call has added to the pool. The |
2822 |
++ * value "num" is also added to the pool; it should somehow describe |
2823 |
++ * the type of event that just happened. |
2824 |
+ */ |
2825 |
+-void del_random_ready_callback(struct random_ready_callback *rdy) |
2826 |
++static void add_timer_randomness(struct timer_rand_state *state, unsigned int num) |
2827 |
+ { |
2828 |
+- unsigned long flags; |
2829 |
+- struct module *owner = NULL; |
2830 |
++ unsigned long entropy = random_get_entropy(), now = jiffies, flags; |
2831 |
++ long delta, delta2, delta3; |
2832 |
++ unsigned int bits; |
2833 |
+ |
2834 |
+- spin_lock_irqsave(&random_ready_list_lock, flags); |
2835 |
+- if (!list_empty(&rdy->list)) { |
2836 |
+- list_del_init(&rdy->list); |
2837 |
+- owner = rdy->owner; |
2838 |
++ /* |
2839 |
++ * If we're in a hard IRQ, add_interrupt_randomness() will be called |
2840 |
++ * sometime after, so mix into the fast pool. |
2841 |
++ */ |
2842 |
++ if (in_hardirq()) { |
2843 |
++ fast_mix(this_cpu_ptr(&irq_randomness)->pool, entropy, num); |
2844 |
++ } else { |
2845 |
++ spin_lock_irqsave(&input_pool.lock, flags); |
2846 |
++ _mix_pool_bytes(&entropy, sizeof(entropy)); |
2847 |
++ _mix_pool_bytes(&num, sizeof(num)); |
2848 |
++ spin_unlock_irqrestore(&input_pool.lock, flags); |
2849 |
+ } |
2850 |
+- spin_unlock_irqrestore(&random_ready_list_lock, flags); |
2851 |
+ |
2852 |
+- module_put(owner); |
2853 |
+-} |
2854 |
+-EXPORT_SYMBOL(del_random_ready_callback); |
2855 |
++ if (crng_ready()) |
2856 |
++ return; |
2857 |
+ |
2858 |
+-/* |
2859 |
+- * This function will use the architecture-specific hardware random |
2860 |
+- * number generator if it is available. The arch-specific hw RNG will |
2861 |
+- * almost certainly be faster than what we can do in software, but it |
2862 |
+- * is impossible to verify that it is implemented securely (as |
2863 |
+- * opposed, to, say, the AES encryption of a sequence number using a |
2864 |
+- * key known by the NSA). So it's useful if we need the speed, but |
2865 |
+- * only if we're willing to trust the hardware manufacturer not to |
2866 |
+- * have put in a back door. |
2867 |
+- * |
2868 |
+- * Return number of bytes filled in. |
2869 |
+- */ |
2870 |
+-int __must_check get_random_bytes_arch(void *buf, int nbytes) |
2871 |
+-{ |
2872 |
+- int left = nbytes; |
2873 |
+- u8 *p = buf; |
2874 |
++ /* |
2875 |
++ * Calculate number of bits of randomness we probably added. |
2876 |
++ * We take into account the first, second and third-order deltas |
2877 |
++ * in order to make our estimate. |
2878 |
++ */ |
2879 |
++ delta = now - READ_ONCE(state->last_time); |
2880 |
++ WRITE_ONCE(state->last_time, now); |
2881 |
++ |
2882 |
++ delta2 = delta - READ_ONCE(state->last_delta); |
2883 |
++ WRITE_ONCE(state->last_delta, delta); |
2884 |
+ |
2885 |
+- trace_get_random_bytes_arch(left, _RET_IP_); |
2886 |
+- while (left) { |
2887 |
+- unsigned long v; |
2888 |
+- int chunk = min_t(int, left, sizeof(unsigned long)); |
2889 |
++ delta3 = delta2 - READ_ONCE(state->last_delta2); |
2890 |
++ WRITE_ONCE(state->last_delta2, delta2); |
2891 |
+ |
2892 |
+- if (!arch_get_random_long(&v)) |
2893 |
+- break; |
2894 |
++ if (delta < 0) |
2895 |
++ delta = -delta; |
2896 |
++ if (delta2 < 0) |
2897 |
++ delta2 = -delta2; |
2898 |
++ if (delta3 < 0) |
2899 |
++ delta3 = -delta3; |
2900 |
++ if (delta > delta2) |
2901 |
++ delta = delta2; |
2902 |
++ if (delta > delta3) |
2903 |
++ delta = delta3; |
2904 |
+ |
2905 |
+- memcpy(p, &v, chunk); |
2906 |
+- p += chunk; |
2907 |
+- left -= chunk; |
2908 |
+- } |
2909 |
++ /* |
2910 |
++ * delta is now minimum absolute delta. Round down by 1 bit |
2911 |
++ * on general principles, and limit entropy estimate to 11 bits. |
2912 |
++ */ |
2913 |
++ bits = min(fls(delta >> 1), 11); |
2914 |
+ |
2915 |
+- return nbytes - left; |
2916 |
++ /* |
2917 |
++ * As mentioned above, if we're in a hard IRQ, add_interrupt_randomness() |
2918 |
++ * will run after this, which uses a different crediting scheme of 1 bit |
2919 |
++ * per every 64 interrupts. In order to let that function do accounting |
2920 |
++ * close to the one in this function, we credit a full 64/64 bit per bit, |
2921 |
++ * and then subtract one to account for the extra one added. |
2922 |
++ */ |
2923 |
++ if (in_hardirq()) |
2924 |
++ this_cpu_ptr(&irq_randomness)->count += max(1u, bits * 64) - 1; |
2925 |
++ else |
2926 |
++ _credit_init_bits(bits); |
2927 |
+ } |
2928 |
+-EXPORT_SYMBOL(get_random_bytes_arch); |
2929 |
+ |
2930 |
+-/* |
2931 |
+- * init_std_data - initialize pool with system data |
2932 |
+- * |
2933 |
+- * This function clears the pool's entropy count and mixes some system |
2934 |
+- * data into the pool to prepare it for use. The pool is not cleared |
2935 |
+- * as that can only decrease the entropy in the pool. |
2936 |
+- */ |
2937 |
+-static void __init init_std_data(void) |
2938 |
++void add_input_randomness(unsigned int type, unsigned int code, unsigned int value) |
2939 |
+ { |
2940 |
+- int i; |
2941 |
+- ktime_t now = ktime_get_real(); |
2942 |
+- unsigned long rv; |
2943 |
+- |
2944 |
+- mix_pool_bytes(&now, sizeof(now)); |
2945 |
+- for (i = POOL_BYTES; i > 0; i -= sizeof(rv)) { |
2946 |
+- if (!arch_get_random_seed_long(&rv) && |
2947 |
+- !arch_get_random_long(&rv)) |
2948 |
+- rv = random_get_entropy(); |
2949 |
+- mix_pool_bytes(&rv, sizeof(rv)); |
2950 |
+- } |
2951 |
+- mix_pool_bytes(utsname(), sizeof(*(utsname()))); |
2952 |
++ static unsigned char last_value; |
2953 |
++ static struct timer_rand_state input_timer_state = { INITIAL_JIFFIES }; |
2954 |
++ |
2955 |
++ /* Ignore autorepeat and the like. */ |
2956 |
++ if (value == last_value) |
2957 |
++ return; |
2958 |
++ |
2959 |
++ last_value = value; |
2960 |
++ add_timer_randomness(&input_timer_state, |
2961 |
++ (type << 4) ^ code ^ (code >> 4) ^ value); |
2962 |
+ } |
2963 |
++EXPORT_SYMBOL_GPL(add_input_randomness); |
2964 |
+ |
2965 |
+-/* |
2966 |
+- * Note that setup_arch() may call add_device_randomness() |
2967 |
+- * long before we get here. This allows seeding of the pools |
2968 |
+- * with some platform dependent data very early in the boot |
2969 |
+- * process. But it limits our options here. We must use |
2970 |
+- * statically allocated structures that already have all |
2971 |
+- * initializations complete at compile time. We should also |
2972 |
+- * take care not to overwrite the precious per platform data |
2973 |
+- * we were given. |
2974 |
+- */ |
2975 |
+-int __init rand_initialize(void) |
2976 |
++#ifdef CONFIG_BLOCK |
2977 |
++void add_disk_randomness(struct gendisk *disk) |
2978 |
+ { |
2979 |
+- init_std_data(); |
2980 |
+- if (crng_need_final_init) |
2981 |
+- crng_finalize_init(); |
2982 |
+- crng_initialize_primary(); |
2983 |
+- crng_global_init_time = jiffies; |
2984 |
+- if (ratelimit_disable) { |
2985 |
+- urandom_warning.interval = 0; |
2986 |
+- unseeded_warning.interval = 0; |
2987 |
+- } |
2988 |
+- return 0; |
2989 |
++ if (!disk || !disk->random) |
2990 |
++ return; |
2991 |
++ /* First major is 1, so we get >= 0x200 here. */ |
2992 |
++ add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); |
2993 |
+ } |
2994 |
++EXPORT_SYMBOL_GPL(add_disk_randomness); |
2995 |
+ |
2996 |
+-#ifdef CONFIG_BLOCK |
2997 |
+-void rand_initialize_disk(struct gendisk *disk) |
2998 |
++void __cold rand_initialize_disk(struct gendisk *disk) |
2999 |
+ { |
3000 |
+ struct timer_rand_state *state; |
3001 |
+ |
3002 |
+@@ -1724,109 +1139,189 @@ void rand_initialize_disk(struct gendisk *disk) |
3003 |
+ } |
3004 |
+ #endif |
3005 |
+ |
3006 |
+-static ssize_t urandom_read_nowarn(struct file *file, char __user *buf, |
3007 |
+- size_t nbytes, loff_t *ppos) |
3008 |
++/* |
3009 |
++ * Each time the timer fires, we expect that we got an unpredictable |
3010 |
++ * jump in the cycle counter. Even if the timer is running on another |
3011 |
++ * CPU, the timer activity will be touching the stack of the CPU that is |
3012 |
++ * generating entropy.. |
3013 |
++ * |
3014 |
++ * Note that we don't re-arm the timer in the timer itself - we are |
3015 |
++ * happy to be scheduled away, since that just makes the load more |
3016 |
++ * complex, but we do not want the timer to keep ticking unless the |
3017 |
++ * entropy loop is running. |
3018 |
++ * |
3019 |
++ * So the re-arming always happens in the entropy loop itself. |
3020 |
++ */ |
3021 |
++static void __cold entropy_timer(struct timer_list *t) |
3022 |
+ { |
3023 |
+- int ret; |
3024 |
+- |
3025 |
+- nbytes = min_t(size_t, nbytes, INT_MAX >> (POOL_ENTROPY_SHIFT + 3)); |
3026 |
+- ret = extract_crng_user(buf, nbytes); |
3027 |
+- trace_urandom_read(8 * nbytes, 0, POOL_ENTROPY_BITS()); |
3028 |
+- return ret; |
3029 |
++ credit_init_bits(1); |
3030 |
+ } |
3031 |
+ |
3032 |
+-static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes, |
3033 |
+- loff_t *ppos) |
3034 |
++/* |
3035 |
++ * If we have an actual cycle counter, see if we can |
3036 |
++ * generate enough entropy with timing noise |
3037 |
++ */ |
3038 |
++static void __cold try_to_generate_entropy(void) |
3039 |
+ { |
3040 |
+- static int maxwarn = 10; |
3041 |
++ struct { |
3042 |
++ unsigned long entropy; |
3043 |
++ struct timer_list timer; |
3044 |
++ } stack; |
3045 |
++ |
3046 |
++ stack.entropy = random_get_entropy(); |
3047 |
++ |
3048 |
++ /* Slow counter - or none. Don't even bother */ |
3049 |
++ if (stack.entropy == random_get_entropy()) |
3050 |
++ return; |
3051 |
+ |
3052 |
+- if (!crng_ready() && maxwarn > 0) { |
3053 |
+- maxwarn--; |
3054 |
+- if (__ratelimit(&urandom_warning)) |
3055 |
+- pr_notice("%s: uninitialized urandom read (%zd bytes read)\n", |
3056 |
+- current->comm, nbytes); |
3057 |
++ timer_setup_on_stack(&stack.timer, entropy_timer, 0); |
3058 |
++ while (!crng_ready() && !signal_pending(current)) { |
3059 |
++ if (!timer_pending(&stack.timer)) |
3060 |
++ mod_timer(&stack.timer, jiffies + 1); |
3061 |
++ mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); |
3062 |
++ schedule(); |
3063 |
++ stack.entropy = random_get_entropy(); |
3064 |
+ } |
3065 |
+ |
3066 |
+- return urandom_read_nowarn(file, buf, nbytes, ppos); |
3067 |
++ del_timer_sync(&stack.timer); |
3068 |
++ destroy_timer_on_stack(&stack.timer); |
3069 |
++ mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); |
3070 |
+ } |
3071 |
+ |
3072 |
+-static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes, |
3073 |
+- loff_t *ppos) |
3074 |
++ |
3075 |
++/********************************************************************** |
3076 |
++ * |
3077 |
++ * Userspace reader/writer interfaces. |
3078 |
++ * |
3079 |
++ * getrandom(2) is the primary modern interface into the RNG and should |
3080 |
++ * be used in preference to anything else. |
3081 |
++ * |
3082 |
++ * Reading from /dev/random has the same functionality as calling |
3083 |
++ * getrandom(2) with flags=0. In earlier versions, however, it had |
3084 |
++ * vastly different semantics and should therefore be avoided, to |
3085 |
++ * prevent backwards compatibility issues. |
3086 |
++ * |
3087 |
++ * Reading from /dev/urandom has the same functionality as calling |
3088 |
++ * getrandom(2) with flags=GRND_INSECURE. Because it does not block |
3089 |
++ * waiting for the RNG to be ready, it should not be used. |
3090 |
++ * |
3091 |
++ * Writing to either /dev/random or /dev/urandom adds entropy to |
3092 |
++ * the input pool but does not credit it. |
3093 |
++ * |
3094 |
++ * Polling on /dev/random indicates when the RNG is initialized, on |
3095 |
++ * the read side, and when it wants new entropy, on the write side. |
3096 |
++ * |
3097 |
++ * Both /dev/random and /dev/urandom have the same set of ioctls for |
3098 |
++ * adding entropy, getting the entropy count, zeroing the count, and |
3099 |
++ * reseeding the crng. |
3100 |
++ * |
3101 |
++ **********************************************************************/ |
3102 |
++ |
3103 |
++SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags) |
3104 |
+ { |
3105 |
++ struct iov_iter iter; |
3106 |
++ struct iovec iov; |
3107 |
+ int ret; |
3108 |
+ |
3109 |
+- ret = wait_for_random_bytes(); |
3110 |
+- if (ret != 0) |
3111 |
++ if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) |
3112 |
++ return -EINVAL; |
3113 |
++ |
3114 |
++ /* |
3115 |
++ * Requesting insecure and blocking randomness at the same time makes |
3116 |
++ * no sense. |
3117 |
++ */ |
3118 |
++ if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM)) |
3119 |
++ return -EINVAL; |
3120 |
++ |
3121 |
++ if (!crng_ready() && !(flags & GRND_INSECURE)) { |
3122 |
++ if (flags & GRND_NONBLOCK) |
3123 |
++ return -EAGAIN; |
3124 |
++ ret = wait_for_random_bytes(); |
3125 |
++ if (unlikely(ret)) |
3126 |
++ return ret; |
3127 |
++ } |
3128 |
++ |
3129 |
++ ret = import_single_range(READ, ubuf, len, &iov, &iter); |
3130 |
++ if (unlikely(ret)) |
3131 |
+ return ret; |
3132 |
+- return urandom_read_nowarn(file, buf, nbytes, ppos); |
3133 |
++ return get_random_bytes_user(&iter); |
3134 |
+ } |
3135 |
+ |
3136 |
+ static __poll_t random_poll(struct file *file, poll_table *wait) |
3137 |
+ { |
3138 |
+- __poll_t mask; |
3139 |
+- |
3140 |
+ poll_wait(file, &crng_init_wait, wait); |
3141 |
+- poll_wait(file, &random_write_wait, wait); |
3142 |
+- mask = 0; |
3143 |
+- if (crng_ready()) |
3144 |
+- mask |= EPOLLIN | EPOLLRDNORM; |
3145 |
+- if (POOL_ENTROPY_BITS() < random_write_wakeup_bits) |
3146 |
+- mask |= EPOLLOUT | EPOLLWRNORM; |
3147 |
+- return mask; |
3148 |
++ return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM; |
3149 |
+ } |
3150 |
+ |
3151 |
+-static int write_pool(const char __user *buffer, size_t count) |
3152 |
++static ssize_t write_pool_user(struct iov_iter *iter) |
3153 |
+ { |
3154 |
+- size_t bytes; |
3155 |
+- u32 t, buf[16]; |
3156 |
+- const char __user *p = buffer; |
3157 |
++ u8 block[BLAKE2S_BLOCK_SIZE]; |
3158 |
++ ssize_t ret = 0; |
3159 |
++ size_t copied; |
3160 |
+ |
3161 |
+- while (count > 0) { |
3162 |
+- int b, i = 0; |
3163 |
++ if (unlikely(!iov_iter_count(iter))) |
3164 |
++ return 0; |
3165 |
+ |
3166 |
+- bytes = min(count, sizeof(buf)); |
3167 |
+- if (copy_from_user(&buf, p, bytes)) |
3168 |
+- return -EFAULT; |
3169 |
++ for (;;) { |
3170 |
++ copied = copy_from_iter(block, sizeof(block), iter); |
3171 |
++ ret += copied; |
3172 |
++ mix_pool_bytes(block, copied); |
3173 |
++ if (!iov_iter_count(iter) || copied != sizeof(block)) |
3174 |
++ break; |
3175 |
+ |
3176 |
+- for (b = bytes; b > 0; b -= sizeof(u32), i++) { |
3177 |
+- if (!arch_get_random_int(&t)) |
3178 |
++ BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0); |
3179 |
++ if (ret % PAGE_SIZE == 0) { |
3180 |
++ if (signal_pending(current)) |
3181 |
+ break; |
3182 |
+- buf[i] ^= t; |
3183 |
++ cond_resched(); |
3184 |
+ } |
3185 |
++ } |
3186 |
+ |
3187 |
+- count -= bytes; |
3188 |
+- p += bytes; |
3189 |
++ memzero_explicit(block, sizeof(block)); |
3190 |
++ return ret ? ret : -EFAULT; |
3191 |
++} |
3192 |
++ |
3193 |
++static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *iter) |
3194 |
++{ |
3195 |
++ return write_pool_user(iter); |
3196 |
++} |
3197 |
+ |
3198 |
+- mix_pool_bytes(buf, bytes); |
3199 |
+- cond_resched(); |
3200 |
++static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *iter) |
3201 |
++{ |
3202 |
++ static int maxwarn = 10; |
3203 |
++ |
3204 |
++ if (!crng_ready()) { |
3205 |
++ if (!ratelimit_disable && maxwarn <= 0) |
3206 |
++ ++urandom_warning.missed; |
3207 |
++ else if (ratelimit_disable || __ratelimit(&urandom_warning)) { |
3208 |
++ --maxwarn; |
3209 |
++ pr_notice("%s: uninitialized urandom read (%zu bytes read)\n", |
3210 |
++ current->comm, iov_iter_count(iter)); |
3211 |
++ } |
3212 |
+ } |
3213 |
+ |
3214 |
+- return 0; |
3215 |
++ return get_random_bytes_user(iter); |
3216 |
+ } |
3217 |
+ |
3218 |
+-static ssize_t random_write(struct file *file, const char __user *buffer, |
3219 |
+- size_t count, loff_t *ppos) |
3220 |
++static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *iter) |
3221 |
+ { |
3222 |
+- size_t ret; |
3223 |
++ int ret; |
3224 |
+ |
3225 |
+- ret = write_pool(buffer, count); |
3226 |
+- if (ret) |
3227 |
++ ret = wait_for_random_bytes(); |
3228 |
++ if (ret != 0) |
3229 |
+ return ret; |
3230 |
+- |
3231 |
+- return (ssize_t)count; |
3232 |
++ return get_random_bytes_user(iter); |
3233 |
+ } |
3234 |
+ |
3235 |
+ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
3236 |
+ { |
3237 |
+- int size, ent_count; |
3238 |
+ int __user *p = (int __user *)arg; |
3239 |
+- int retval; |
3240 |
++ int ent_count; |
3241 |
+ |
3242 |
+ switch (cmd) { |
3243 |
+ case RNDGETENTCNT: |
3244 |
+- /* inherently racy, no point locking */ |
3245 |
+- ent_count = POOL_ENTROPY_BITS(); |
3246 |
+- if (put_user(ent_count, p)) |
3247 |
++ /* Inherently racy, no point locking. */ |
3248 |
++ if (put_user(input_pool.init_bits, p)) |
3249 |
+ return -EFAULT; |
3250 |
+ return 0; |
3251 |
+ case RNDADDTOENTCNT: |
3252 |
+@@ -1834,40 +1329,48 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
3253 |
+ return -EPERM; |
3254 |
+ if (get_user(ent_count, p)) |
3255 |
+ return -EFAULT; |
3256 |
+- return credit_entropy_bits_safe(ent_count); |
3257 |
+- case RNDADDENTROPY: |
3258 |
++ if (ent_count < 0) |
3259 |
++ return -EINVAL; |
3260 |
++ credit_init_bits(ent_count); |
3261 |
++ return 0; |
3262 |
++ case RNDADDENTROPY: { |
3263 |
++ struct iov_iter iter; |
3264 |
++ struct iovec iov; |
3265 |
++ ssize_t ret; |
3266 |
++ int len; |
3267 |
++ |
3268 |
+ if (!capable(CAP_SYS_ADMIN)) |
3269 |
+ return -EPERM; |
3270 |
+ if (get_user(ent_count, p++)) |
3271 |
+ return -EFAULT; |
3272 |
+ if (ent_count < 0) |
3273 |
+ return -EINVAL; |
3274 |
+- if (get_user(size, p++)) |
3275 |
++ if (get_user(len, p++)) |
3276 |
++ return -EFAULT; |
3277 |
++ ret = import_single_range(WRITE, p, len, &iov, &iter); |
3278 |
++ if (unlikely(ret)) |
3279 |
++ return ret; |
3280 |
++ ret = write_pool_user(&iter); |
3281 |
++ if (unlikely(ret < 0)) |
3282 |
++ return ret; |
3283 |
++ /* Since we're crediting, enforce that it was all written into the pool. */ |
3284 |
++ if (unlikely(ret != len)) |
3285 |
+ return -EFAULT; |
3286 |
+- retval = write_pool((const char __user *)p, size); |
3287 |
+- if (retval < 0) |
3288 |
+- return retval; |
3289 |
+- return credit_entropy_bits_safe(ent_count); |
3290 |
++ credit_init_bits(ent_count); |
3291 |
++ return 0; |
3292 |
++ } |
3293 |
+ case RNDZAPENTCNT: |
3294 |
+ case RNDCLEARPOOL: |
3295 |
+- /* |
3296 |
+- * Clear the entropy pool counters. We no longer clear |
3297 |
+- * the entropy pool, as that's silly. |
3298 |
+- */ |
3299 |
++ /* No longer has any effect. */ |
3300 |
+ if (!capable(CAP_SYS_ADMIN)) |
3301 |
+ return -EPERM; |
3302 |
+- if (xchg(&input_pool.entropy_count, 0) && random_write_wakeup_bits) { |
3303 |
+- wake_up_interruptible(&random_write_wait); |
3304 |
+- kill_fasync(&fasync, SIGIO, POLL_OUT); |
3305 |
+- } |
3306 |
+ return 0; |
3307 |
+ case RNDRESEEDCRNG: |
3308 |
+ if (!capable(CAP_SYS_ADMIN)) |
3309 |
+ return -EPERM; |
3310 |
+- if (crng_init < 2) |
3311 |
++ if (!crng_ready()) |
3312 |
+ return -ENODATA; |
3313 |
+- crng_reseed(&primary_crng, true); |
3314 |
+- WRITE_ONCE(crng_global_init_time, jiffies - 1); |
3315 |
++ crng_reseed(); |
3316 |
+ return 0; |
3317 |
+ default: |
3318 |
+ return -EINVAL; |
3319 |
+@@ -1880,55 +1383,56 @@ static int random_fasync(int fd, struct file *filp, int on) |
3320 |
+ } |
3321 |
+ |
3322 |
+ const struct file_operations random_fops = { |
3323 |
+- .read = random_read, |
3324 |
+- .write = random_write, |
3325 |
++ .read_iter = random_read_iter, |
3326 |
++ .write_iter = random_write_iter, |
3327 |
+ .poll = random_poll, |
3328 |
+ .unlocked_ioctl = random_ioctl, |
3329 |
+ .compat_ioctl = compat_ptr_ioctl, |
3330 |
+ .fasync = random_fasync, |
3331 |
+ .llseek = noop_llseek, |
3332 |
++ .splice_read = generic_file_splice_read, |
3333 |
++ .splice_write = iter_file_splice_write, |
3334 |
+ }; |
3335 |
+ |
3336 |
+ const struct file_operations urandom_fops = { |
3337 |
+- .read = urandom_read, |
3338 |
+- .write = random_write, |
3339 |
++ .read_iter = urandom_read_iter, |
3340 |
++ .write_iter = random_write_iter, |
3341 |
+ .unlocked_ioctl = random_ioctl, |
3342 |
+ .compat_ioctl = compat_ptr_ioctl, |
3343 |
+ .fasync = random_fasync, |
3344 |
+ .llseek = noop_llseek, |
3345 |
++ .splice_read = generic_file_splice_read, |
3346 |
++ .splice_write = iter_file_splice_write, |
3347 |
+ }; |
3348 |
+ |
3349 |
+-SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, |
3350 |
+- flags) |
3351 |
+-{ |
3352 |
+- int ret; |
3353 |
+- |
3354 |
+- if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) |
3355 |
+- return -EINVAL; |
3356 |
+- |
3357 |
+- /* |
3358 |
+- * Requesting insecure and blocking randomness at the same time makes |
3359 |
+- * no sense. |
3360 |
+- */ |
3361 |
+- if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM)) |
3362 |
+- return -EINVAL; |
3363 |
+- |
3364 |
+- if (count > INT_MAX) |
3365 |
+- count = INT_MAX; |
3366 |
+- |
3367 |
+- if (!(flags & GRND_INSECURE) && !crng_ready()) { |
3368 |
+- if (flags & GRND_NONBLOCK) |
3369 |
+- return -EAGAIN; |
3370 |
+- ret = wait_for_random_bytes(); |
3371 |
+- if (unlikely(ret)) |
3372 |
+- return ret; |
3373 |
+- } |
3374 |
+- return urandom_read_nowarn(NULL, buf, count, NULL); |
3375 |
+-} |
3376 |
+ |
3377 |
+ /******************************************************************** |
3378 |
+ * |
3379 |
+- * Sysctl interface |
3380 |
++ * Sysctl interface. |
3381 |
++ * |
3382 |
++ * These are partly unused legacy knobs with dummy values to not break |
3383 |
++ * userspace and partly still useful things. They are usually accessible |
3384 |
++ * in /proc/sys/kernel/random/ and are as follows: |
3385 |
++ * |
3386 |
++ * - boot_id - a UUID representing the current boot. |
3387 |
++ * |
3388 |
++ * - uuid - a random UUID, different each time the file is read. |
3389 |
++ * |
3390 |
++ * - poolsize - the number of bits of entropy that the input pool can |
3391 |
++ * hold, tied to the POOL_BITS constant. |
3392 |
++ * |
3393 |
++ * - entropy_avail - the number of bits of entropy currently in the |
3394 |
++ * input pool. Always <= poolsize. |
3395 |
++ * |
3396 |
++ * - write_wakeup_threshold - the amount of entropy in the input pool |
3397 |
++ * below which write polls to /dev/random will unblock, requesting |
3398 |
++ * more entropy, tied to the POOL_READY_BITS constant. It is writable |
3399 |
++ * to avoid breaking old userspaces, but writing to it does not |
3400 |
++ * change any behavior of the RNG. |
3401 |
++ * |
3402 |
++ * - urandom_min_reseed_secs - fixed to the value CRNG_RESEED_INTERVAL. |
3403 |
++ * It is writable to avoid breaking old userspaces, but writing |
3404 |
++ * to it does not change any behavior of the RNG. |
3405 |
+ * |
3406 |
+ ********************************************************************/ |
3407 |
+ |
3408 |
+@@ -1936,25 +1440,28 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, |
3409 |
+ |
3410 |
+ #include <linux/sysctl.h> |
3411 |
+ |
3412 |
+-static int min_write_thresh; |
3413 |
+-static int max_write_thresh = POOL_BITS; |
3414 |
+-static int random_min_urandom_seed = 60; |
3415 |
+-static char sysctl_bootid[16]; |
3416 |
++static int sysctl_random_min_urandom_seed = CRNG_RESEED_INTERVAL / HZ; |
3417 |
++static int sysctl_random_write_wakeup_bits = POOL_READY_BITS; |
3418 |
++static int sysctl_poolsize = POOL_BITS; |
3419 |
++static u8 sysctl_bootid[UUID_SIZE]; |
3420 |
+ |
3421 |
+ /* |
3422 |
+ * This function is used to return both the bootid UUID, and random |
3423 |
+- * UUID. The difference is in whether table->data is NULL; if it is, |
3424 |
++ * UUID. The difference is in whether table->data is NULL; if it is, |
3425 |
+ * then a new UUID is generated and returned to the user. |
3426 |
+- * |
3427 |
+- * If the user accesses this via the proc interface, the UUID will be |
3428 |
+- * returned as an ASCII string in the standard UUID format; if via the |
3429 |
+- * sysctl system call, as 16 bytes of binary data. |
3430 |
+ */ |
3431 |
+-static int proc_do_uuid(struct ctl_table *table, int write, void *buffer, |
3432 |
++static int proc_do_uuid(struct ctl_table *table, int write, void *buf, |
3433 |
+ size_t *lenp, loff_t *ppos) |
3434 |
+ { |
3435 |
+- struct ctl_table fake_table; |
3436 |
+- unsigned char buf[64], tmp_uuid[16], *uuid; |
3437 |
++ u8 tmp_uuid[UUID_SIZE], *uuid; |
3438 |
++ char uuid_string[UUID_STRING_LEN + 1]; |
3439 |
++ struct ctl_table fake_table = { |
3440 |
++ .data = uuid_string, |
3441 |
++ .maxlen = UUID_STRING_LEN |
3442 |
++ }; |
3443 |
++ |
3444 |
++ if (write) |
3445 |
++ return -EPERM; |
3446 |
+ |
3447 |
+ uuid = table->data; |
3448 |
+ if (!uuid) { |
3449 |
+@@ -1969,32 +1476,17 @@ static int proc_do_uuid(struct ctl_table *table, int write, void *buffer, |
3450 |
+ spin_unlock(&bootid_spinlock); |
3451 |
+ } |
3452 |
+ |
3453 |
+- sprintf(buf, "%pU", uuid); |
3454 |
+- |
3455 |
+- fake_table.data = buf; |
3456 |
+- fake_table.maxlen = sizeof(buf); |
3457 |
+- |
3458 |
+- return proc_dostring(&fake_table, write, buffer, lenp, ppos); |
3459 |
++ snprintf(uuid_string, sizeof(uuid_string), "%pU", uuid); |
3460 |
++ return proc_dostring(&fake_table, 0, buf, lenp, ppos); |
3461 |
+ } |
3462 |
+ |
3463 |
+-/* |
3464 |
+- * Return entropy available scaled to integral bits |
3465 |
+- */ |
3466 |
+-static int proc_do_entropy(struct ctl_table *table, int write, void *buffer, |
3467 |
+- size_t *lenp, loff_t *ppos) |
3468 |
++/* The same as proc_dointvec, but writes don't change anything. */ |
3469 |
++static int proc_do_rointvec(struct ctl_table *table, int write, void *buf, |
3470 |
++ size_t *lenp, loff_t *ppos) |
3471 |
+ { |
3472 |
+- struct ctl_table fake_table; |
3473 |
+- int entropy_count; |
3474 |
+- |
3475 |
+- entropy_count = *(int *)table->data >> POOL_ENTROPY_SHIFT; |
3476 |
+- |
3477 |
+- fake_table.data = &entropy_count; |
3478 |
+- fake_table.maxlen = sizeof(entropy_count); |
3479 |
+- |
3480 |
+- return proc_dointvec(&fake_table, write, buffer, lenp, ppos); |
3481 |
++ return write ? 0 : proc_dointvec(table, 0, buf, lenp, ppos); |
3482 |
+ } |
3483 |
+ |
3484 |
+-static int sysctl_poolsize = POOL_BITS; |
3485 |
+ static struct ctl_table random_table[] = { |
3486 |
+ { |
3487 |
+ .procname = "poolsize", |
3488 |
+@@ -2005,62 +1497,42 @@ static struct ctl_table random_table[] = { |
3489 |
+ }, |
3490 |
+ { |
3491 |
+ .procname = "entropy_avail", |
3492 |
++ .data = &input_pool.init_bits, |
3493 |
+ .maxlen = sizeof(int), |
3494 |
+ .mode = 0444, |
3495 |
+- .proc_handler = proc_do_entropy, |
3496 |
+- .data = &input_pool.entropy_count, |
3497 |
++ .proc_handler = proc_dointvec, |
3498 |
+ }, |
3499 |
+ { |
3500 |
+ .procname = "write_wakeup_threshold", |
3501 |
+- .data = &random_write_wakeup_bits, |
3502 |
++ .data = &sysctl_random_write_wakeup_bits, |
3503 |
+ .maxlen = sizeof(int), |
3504 |
+ .mode = 0644, |
3505 |
+- .proc_handler = proc_dointvec_minmax, |
3506 |
+- .extra1 = &min_write_thresh, |
3507 |
+- .extra2 = &max_write_thresh, |
3508 |
++ .proc_handler = proc_do_rointvec, |
3509 |
+ }, |
3510 |
+ { |
3511 |
+ .procname = "urandom_min_reseed_secs", |
3512 |
+- .data = &random_min_urandom_seed, |
3513 |
++ .data = &sysctl_random_min_urandom_seed, |
3514 |
+ .maxlen = sizeof(int), |
3515 |
+ .mode = 0644, |
3516 |
+- .proc_handler = proc_dointvec, |
3517 |
++ .proc_handler = proc_do_rointvec, |
3518 |
+ }, |
3519 |
+ { |
3520 |
+ .procname = "boot_id", |
3521 |
+ .data = &sysctl_bootid, |
3522 |
+- .maxlen = 16, |
3523 |
+ .mode = 0444, |
3524 |
+ .proc_handler = proc_do_uuid, |
3525 |
+ }, |
3526 |
+ { |
3527 |
+ .procname = "uuid", |
3528 |
+- .maxlen = 16, |
3529 |
+ .mode = 0444, |
3530 |
+ .proc_handler = proc_do_uuid, |
3531 |
+ }, |
3532 |
+-#ifdef ADD_INTERRUPT_BENCH |
3533 |
+- { |
3534 |
+- .procname = "add_interrupt_avg_cycles", |
3535 |
+- .data = &avg_cycles, |
3536 |
+- .maxlen = sizeof(avg_cycles), |
3537 |
+- .mode = 0444, |
3538 |
+- .proc_handler = proc_doulongvec_minmax, |
3539 |
+- }, |
3540 |
+- { |
3541 |
+- .procname = "add_interrupt_avg_deviation", |
3542 |
+- .data = &avg_deviation, |
3543 |
+- .maxlen = sizeof(avg_deviation), |
3544 |
+- .mode = 0444, |
3545 |
+- .proc_handler = proc_doulongvec_minmax, |
3546 |
+- }, |
3547 |
+-#endif |
3548 |
+ { } |
3549 |
+ }; |
3550 |
+ |
3551 |
+ /* |
3552 |
+- * rand_initialize() is called before sysctl_init(), |
3553 |
+- * so we cannot call register_sysctl_init() in rand_initialize() |
3554 |
++ * random_init() is called before sysctl_init(), |
3555 |
++ * so we cannot call register_sysctl_init() in random_init() |
3556 |
+ */ |
3557 |
+ static int __init random_sysctls_init(void) |
3558 |
+ { |
3559 |
+@@ -2068,170 +1540,4 @@ static int __init random_sysctls_init(void) |
3560 |
+ return 0; |
3561 |
+ } |
3562 |
+ device_initcall(random_sysctls_init); |
3563 |
+-#endif /* CONFIG_SYSCTL */ |
3564 |
+- |
3565 |
+-struct batched_entropy { |
3566 |
+- union { |
3567 |
+- u64 entropy_u64[CHACHA_BLOCK_SIZE / sizeof(u64)]; |
3568 |
+- u32 entropy_u32[CHACHA_BLOCK_SIZE / sizeof(u32)]; |
3569 |
+- }; |
3570 |
+- unsigned int position; |
3571 |
+- spinlock_t batch_lock; |
3572 |
+-}; |
3573 |
+- |
3574 |
+-/* |
3575 |
+- * Get a random word for internal kernel use only. The quality of the random |
3576 |
+- * number is good as /dev/urandom, but there is no backtrack protection, with |
3577 |
+- * the goal of being quite fast and not depleting entropy. In order to ensure |
3578 |
+- * that the randomness provided by this function is okay, the function |
3579 |
+- * wait_for_random_bytes() should be called and return 0 at least once at any |
3580 |
+- * point prior. |
3581 |
+- */ |
3582 |
+-static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64) = { |
3583 |
+- .batch_lock = __SPIN_LOCK_UNLOCKED(batched_entropy_u64.lock), |
3584 |
+-}; |
3585 |
+- |
3586 |
+-u64 get_random_u64(void) |
3587 |
+-{ |
3588 |
+- u64 ret; |
3589 |
+- unsigned long flags; |
3590 |
+- struct batched_entropy *batch; |
3591 |
+- static void *previous; |
3592 |
+- |
3593 |
+- warn_unseeded_randomness(&previous); |
3594 |
+- |
3595 |
+- batch = raw_cpu_ptr(&batched_entropy_u64); |
3596 |
+- spin_lock_irqsave(&batch->batch_lock, flags); |
3597 |
+- if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0) { |
3598 |
+- extract_crng((u8 *)batch->entropy_u64); |
3599 |
+- batch->position = 0; |
3600 |
+- } |
3601 |
+- ret = batch->entropy_u64[batch->position++]; |
3602 |
+- spin_unlock_irqrestore(&batch->batch_lock, flags); |
3603 |
+- return ret; |
3604 |
+-} |
3605 |
+-EXPORT_SYMBOL(get_random_u64); |
3606 |
+- |
3607 |
+-static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32) = { |
3608 |
+- .batch_lock = __SPIN_LOCK_UNLOCKED(batched_entropy_u32.lock), |
3609 |
+-}; |
3610 |
+-u32 get_random_u32(void) |
3611 |
+-{ |
3612 |
+- u32 ret; |
3613 |
+- unsigned long flags; |
3614 |
+- struct batched_entropy *batch; |
3615 |
+- static void *previous; |
3616 |
+- |
3617 |
+- warn_unseeded_randomness(&previous); |
3618 |
+- |
3619 |
+- batch = raw_cpu_ptr(&batched_entropy_u32); |
3620 |
+- spin_lock_irqsave(&batch->batch_lock, flags); |
3621 |
+- if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0) { |
3622 |
+- extract_crng((u8 *)batch->entropy_u32); |
3623 |
+- batch->position = 0; |
3624 |
+- } |
3625 |
+- ret = batch->entropy_u32[batch->position++]; |
3626 |
+- spin_unlock_irqrestore(&batch->batch_lock, flags); |
3627 |
+- return ret; |
3628 |
+-} |
3629 |
+-EXPORT_SYMBOL(get_random_u32); |
3630 |
+- |
3631 |
+-/* It's important to invalidate all potential batched entropy that might |
3632 |
+- * be stored before the crng is initialized, which we can do lazily by |
3633 |
+- * simply resetting the counter to zero so that it's re-extracted on the |
3634 |
+- * next usage. */ |
3635 |
+-static void invalidate_batched_entropy(void) |
3636 |
+-{ |
3637 |
+- int cpu; |
3638 |
+- unsigned long flags; |
3639 |
+- |
3640 |
+- for_each_possible_cpu(cpu) { |
3641 |
+- struct batched_entropy *batched_entropy; |
3642 |
+- |
3643 |
+- batched_entropy = per_cpu_ptr(&batched_entropy_u32, cpu); |
3644 |
+- spin_lock_irqsave(&batched_entropy->batch_lock, flags); |
3645 |
+- batched_entropy->position = 0; |
3646 |
+- spin_unlock(&batched_entropy->batch_lock); |
3647 |
+- |
3648 |
+- batched_entropy = per_cpu_ptr(&batched_entropy_u64, cpu); |
3649 |
+- spin_lock(&batched_entropy->batch_lock); |
3650 |
+- batched_entropy->position = 0; |
3651 |
+- spin_unlock_irqrestore(&batched_entropy->batch_lock, flags); |
3652 |
+- } |
3653 |
+-} |
3654 |
+- |
3655 |
+-/** |
3656 |
+- * randomize_page - Generate a random, page aligned address |
3657 |
+- * @start: The smallest acceptable address the caller will take. |
3658 |
+- * @range: The size of the area, starting at @start, within which the |
3659 |
+- * random address must fall. |
3660 |
+- * |
3661 |
+- * If @start + @range would overflow, @range is capped. |
3662 |
+- * |
3663 |
+- * NOTE: Historical use of randomize_range, which this replaces, presumed that |
3664 |
+- * @start was already page aligned. We now align it regardless. |
3665 |
+- * |
3666 |
+- * Return: A page aligned address within [start, start + range). On error, |
3667 |
+- * @start is returned. |
3668 |
+- */ |
3669 |
+-unsigned long randomize_page(unsigned long start, unsigned long range) |
3670 |
+-{ |
3671 |
+- if (!PAGE_ALIGNED(start)) { |
3672 |
+- range -= PAGE_ALIGN(start) - start; |
3673 |
+- start = PAGE_ALIGN(start); |
3674 |
+- } |
3675 |
+- |
3676 |
+- if (start > ULONG_MAX - range) |
3677 |
+- range = ULONG_MAX - start; |
3678 |
+- |
3679 |
+- range >>= PAGE_SHIFT; |
3680 |
+- |
3681 |
+- if (range == 0) |
3682 |
+- return start; |
3683 |
+- |
3684 |
+- return start + (get_random_long() % range << PAGE_SHIFT); |
3685 |
+-} |
3686 |
+- |
3687 |
+-/* Interface for in-kernel drivers of true hardware RNGs. |
3688 |
+- * Those devices may produce endless random bits and will be throttled |
3689 |
+- * when our pool is full. |
3690 |
+- */ |
3691 |
+-void add_hwgenerator_randomness(const char *buffer, size_t count, |
3692 |
+- size_t entropy) |
3693 |
+-{ |
3694 |
+- if (unlikely(crng_init == 0)) { |
3695 |
+- size_t ret = crng_fast_load(buffer, count); |
3696 |
+- mix_pool_bytes(buffer, ret); |
3697 |
+- count -= ret; |
3698 |
+- buffer += ret; |
3699 |
+- if (!count || crng_init == 0) |
3700 |
+- return; |
3701 |
+- } |
3702 |
+- |
3703 |
+- /* Throttle writing if we're above the trickle threshold. |
3704 |
+- * We'll be woken up again once below random_write_wakeup_thresh, |
3705 |
+- * when the calling thread is about to terminate, or once |
3706 |
+- * CRNG_RESEED_INTERVAL has lapsed. |
3707 |
+- */ |
3708 |
+- wait_event_interruptible_timeout(random_write_wait, |
3709 |
+- !system_wq || kthread_should_stop() || |
3710 |
+- POOL_ENTROPY_BITS() <= random_write_wakeup_bits, |
3711 |
+- CRNG_RESEED_INTERVAL); |
3712 |
+- mix_pool_bytes(buffer, count); |
3713 |
+- credit_entropy_bits(entropy); |
3714 |
+-} |
3715 |
+-EXPORT_SYMBOL_GPL(add_hwgenerator_randomness); |
3716 |
+- |
3717 |
+-/* Handle random seed passed by bootloader. |
3718 |
+- * If the seed is trustworthy, it would be regarded as hardware RNGs. Otherwise |
3719 |
+- * it would be regarded as device data. |
3720 |
+- * The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER. |
3721 |
+- */ |
3722 |
+-void add_bootloader_randomness(const void *buf, unsigned int size) |
3723 |
+-{ |
3724 |
+- if (IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER)) |
3725 |
+- add_hwgenerator_randomness(buf, size, size * 8); |
3726 |
+- else |
3727 |
+- add_device_randomness(buf, size); |
3728 |
+-} |
3729 |
+-EXPORT_SYMBOL_GPL(add_bootloader_randomness); |
3730 |
++#endif |
3731 |
+diff --git a/drivers/hid/amd-sfh-hid/amd_sfh_client.c b/drivers/hid/amd-sfh-hid/amd_sfh_client.c |
3732 |
+index c5de0ec4f9d03..444acd9e2cd6a 100644 |
3733 |
+--- a/drivers/hid/amd-sfh-hid/amd_sfh_client.c |
3734 |
++++ b/drivers/hid/amd-sfh-hid/amd_sfh_client.c |
3735 |
+@@ -227,6 +227,17 @@ int amd_sfh_hid_client_init(struct amd_mp2_dev *privdata) |
3736 |
+ dev_dbg(dev, "sid 0x%x status 0x%x\n", |
3737 |
+ cl_data->sensor_idx[i], cl_data->sensor_sts[i]); |
3738 |
+ } |
3739 |
++ if (privdata->mp2_ops->discovery_status && |
3740 |
++ privdata->mp2_ops->discovery_status(privdata) == 0) { |
3741 |
++ amd_sfh_hid_client_deinit(privdata); |
3742 |
++ for (i = 0; i < cl_data->num_hid_devices; i++) { |
3743 |
++ devm_kfree(dev, cl_data->feature_report[i]); |
3744 |
++ devm_kfree(dev, in_data->input_report[i]); |
3745 |
++ devm_kfree(dev, cl_data->report_descr[i]); |
3746 |
++ } |
3747 |
++ dev_warn(dev, "Failed to discover, sensors not enabled\n"); |
3748 |
++ return -EOPNOTSUPP; |
3749 |
++ } |
3750 |
+ schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP)); |
3751 |
+ return 0; |
3752 |
+ |
3753 |
+diff --git a/drivers/hid/amd-sfh-hid/amd_sfh_pcie.c b/drivers/hid/amd-sfh-hid/amd_sfh_pcie.c |
3754 |
+index 19fa734a9a793..abd7f65860958 100644 |
3755 |
+--- a/drivers/hid/amd-sfh-hid/amd_sfh_pcie.c |
3756 |
++++ b/drivers/hid/amd-sfh-hid/amd_sfh_pcie.c |
3757 |
+@@ -130,6 +130,12 @@ static int amd_sfh_irq_init_v2(struct amd_mp2_dev *privdata) |
3758 |
+ return 0; |
3759 |
+ } |
3760 |
+ |
3761 |
++static int amd_sfh_dis_sts_v2(struct amd_mp2_dev *privdata) |
3762 |
++{ |
3763 |
++ return (readl(privdata->mmio + AMD_P2C_MSG(1)) & |
3764 |
++ SENSOR_DISCOVERY_STATUS_MASK) >> SENSOR_DISCOVERY_STATUS_SHIFT; |
3765 |
++} |
3766 |
++ |
3767 |
+ void amd_start_sensor(struct amd_mp2_dev *privdata, struct amd_mp2_sensor_info info) |
3768 |
+ { |
3769 |
+ union sfh_cmd_param cmd_param; |
3770 |
+@@ -245,6 +251,7 @@ static const struct amd_mp2_ops amd_sfh_ops_v2 = { |
3771 |
+ .response = amd_sfh_wait_response_v2, |
3772 |
+ .clear_intr = amd_sfh_clear_intr_v2, |
3773 |
+ .init_intr = amd_sfh_irq_init_v2, |
3774 |
++ .discovery_status = amd_sfh_dis_sts_v2, |
3775 |
+ }; |
3776 |
+ |
3777 |
+ static const struct amd_mp2_ops amd_sfh_ops = { |
3778 |
+diff --git a/drivers/hid/amd-sfh-hid/amd_sfh_pcie.h b/drivers/hid/amd-sfh-hid/amd_sfh_pcie.h |
3779 |
+index 97b99861fae25..9aa88a91ac8d1 100644 |
3780 |
+--- a/drivers/hid/amd-sfh-hid/amd_sfh_pcie.h |
3781 |
++++ b/drivers/hid/amd-sfh-hid/amd_sfh_pcie.h |
3782 |
+@@ -39,6 +39,9 @@ |
3783 |
+ |
3784 |
+ #define AMD_SFH_IDLE_LOOP 200 |
3785 |
+ |
3786 |
++#define SENSOR_DISCOVERY_STATUS_MASK GENMASK(5, 3) |
3787 |
++#define SENSOR_DISCOVERY_STATUS_SHIFT 3 |
3788 |
++ |
3789 |
+ /* SFH Command register */ |
3790 |
+ union sfh_cmd_base { |
3791 |
+ u32 ul; |
3792 |
+@@ -143,5 +146,6 @@ struct amd_mp2_ops { |
3793 |
+ int (*response)(struct amd_mp2_dev *mp2, u8 sid, u32 sensor_sts); |
3794 |
+ void (*clear_intr)(struct amd_mp2_dev *privdata); |
3795 |
+ int (*init_intr)(struct amd_mp2_dev *privdata); |
3796 |
++ int (*discovery_status)(struct amd_mp2_dev *privdata); |
3797 |
+ }; |
3798 |
+ #endif |
3799 |
+diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h |
3800 |
+index 411a428ace4d4..481e565cc5c42 100644 |
3801 |
+--- a/include/linux/cpuhotplug.h |
3802 |
++++ b/include/linux/cpuhotplug.h |
3803 |
+@@ -100,6 +100,7 @@ enum cpuhp_state { |
3804 |
+ CPUHP_AP_ARM_CACHE_B15_RAC_DEAD, |
3805 |
+ CPUHP_PADATA_DEAD, |
3806 |
+ CPUHP_AP_DTPM_CPU_DEAD, |
3807 |
++ CPUHP_RANDOM_PREPARE, |
3808 |
+ CPUHP_WORKQUEUE_PREP, |
3809 |
+ CPUHP_POWER_NUMA_PREPARE, |
3810 |
+ CPUHP_HRTIMERS_PREPARE, |
3811 |
+@@ -240,6 +241,7 @@ enum cpuhp_state { |
3812 |
+ CPUHP_AP_PERF_CSKY_ONLINE, |
3813 |
+ CPUHP_AP_WATCHDOG_ONLINE, |
3814 |
+ CPUHP_AP_WORKQUEUE_ONLINE, |
3815 |
++ CPUHP_AP_RANDOM_ONLINE, |
3816 |
+ CPUHP_AP_RCUTREE_ONLINE, |
3817 |
+ CPUHP_AP_BASE_CACHEINFO_ONLINE, |
3818 |
+ CPUHP_AP_ONLINE_DYN, |
3819 |
+diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h |
3820 |
+index 8e6dd908da216..aa1d4da03538b 100644 |
3821 |
+--- a/include/linux/hw_random.h |
3822 |
++++ b/include/linux/hw_random.h |
3823 |
+@@ -60,7 +60,5 @@ extern int devm_hwrng_register(struct device *dev, struct hwrng *rng); |
3824 |
+ /** Unregister a Hardware Random Number Generator driver. */ |
3825 |
+ extern void hwrng_unregister(struct hwrng *rng); |
3826 |
+ extern void devm_hwrng_unregister(struct device *dve, struct hwrng *rng); |
3827 |
+-/** Feed random bits into the pool. */ |
3828 |
+-extern void add_hwgenerator_randomness(const char *buffer, size_t count, size_t entropy); |
3829 |
+ |
3830 |
+ #endif /* LINUX_HWRANDOM_H_ */ |
3831 |
+diff --git a/include/linux/mm.h b/include/linux/mm.h |
3832 |
+index 5744a3fc47169..9cb0ff065e8b1 100644 |
3833 |
+--- a/include/linux/mm.h |
3834 |
++++ b/include/linux/mm.h |
3835 |
+@@ -2678,6 +2678,7 @@ extern int install_special_mapping(struct mm_struct *mm, |
3836 |
+ unsigned long flags, struct page **pages); |
3837 |
+ |
3838 |
+ unsigned long randomize_stack_top(unsigned long stack_top); |
3839 |
++unsigned long randomize_page(unsigned long start, unsigned long range); |
3840 |
+ |
3841 |
+ extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); |
3842 |
+ |
3843 |
+diff --git a/include/linux/prandom.h b/include/linux/prandom.h |
3844 |
+index 056d31317e499..a4aadd2dc153e 100644 |
3845 |
+--- a/include/linux/prandom.h |
3846 |
++++ b/include/linux/prandom.h |
3847 |
+@@ -10,6 +10,7 @@ |
3848 |
+ |
3849 |
+ #include <linux/types.h> |
3850 |
+ #include <linux/percpu.h> |
3851 |
++#include <linux/siphash.h> |
3852 |
+ |
3853 |
+ u32 prandom_u32(void); |
3854 |
+ void prandom_bytes(void *buf, size_t nbytes); |
3855 |
+@@ -27,15 +28,10 @@ DECLARE_PER_CPU(unsigned long, net_rand_noise); |
3856 |
+ * The core SipHash round function. Each line can be executed in |
3857 |
+ * parallel given enough CPU resources. |
3858 |
+ */ |
3859 |
+-#define PRND_SIPROUND(v0, v1, v2, v3) ( \ |
3860 |
+- v0 += v1, v1 = rol64(v1, 13), v2 += v3, v3 = rol64(v3, 16), \ |
3861 |
+- v1 ^= v0, v0 = rol64(v0, 32), v3 ^= v2, \ |
3862 |
+- v0 += v3, v3 = rol64(v3, 21), v2 += v1, v1 = rol64(v1, 17), \ |
3863 |
+- v3 ^= v0, v1 ^= v2, v2 = rol64(v2, 32) \ |
3864 |
+-) |
3865 |
++#define PRND_SIPROUND(v0, v1, v2, v3) SIPHASH_PERMUTATION(v0, v1, v2, v3) |
3866 |
+ |
3867 |
+-#define PRND_K0 (0x736f6d6570736575 ^ 0x6c7967656e657261) |
3868 |
+-#define PRND_K1 (0x646f72616e646f6d ^ 0x7465646279746573) |
3869 |
++#define PRND_K0 (SIPHASH_CONST_0 ^ SIPHASH_CONST_2) |
3870 |
++#define PRND_K1 (SIPHASH_CONST_1 ^ SIPHASH_CONST_3) |
3871 |
+ |
3872 |
+ #elif BITS_PER_LONG == 32 |
3873 |
+ /* |
3874 |
+@@ -43,14 +39,9 @@ DECLARE_PER_CPU(unsigned long, net_rand_noise); |
3875 |
+ * This is weaker, but 32-bit machines are not used for high-traffic |
3876 |
+ * applications, so there is less output for an attacker to analyze. |
3877 |
+ */ |
3878 |
+-#define PRND_SIPROUND(v0, v1, v2, v3) ( \ |
3879 |
+- v0 += v1, v1 = rol32(v1, 5), v2 += v3, v3 = rol32(v3, 8), \ |
3880 |
+- v1 ^= v0, v0 = rol32(v0, 16), v3 ^= v2, \ |
3881 |
+- v0 += v3, v3 = rol32(v3, 7), v2 += v1, v1 = rol32(v1, 13), \ |
3882 |
+- v3 ^= v0, v1 ^= v2, v2 = rol32(v2, 16) \ |
3883 |
+-) |
3884 |
+-#define PRND_K0 0x6c796765 |
3885 |
+-#define PRND_K1 0x74656462 |
3886 |
++#define PRND_SIPROUND(v0, v1, v2, v3) HSIPHASH_PERMUTATION(v0, v1, v2, v3) |
3887 |
++#define PRND_K0 (HSIPHASH_CONST_0 ^ HSIPHASH_CONST_2) |
3888 |
++#define PRND_K1 (HSIPHASH_CONST_1 ^ HSIPHASH_CONST_3) |
3889 |
+ |
3890 |
+ #else |
3891 |
+ #error Unsupported BITS_PER_LONG |
3892 |
+diff --git a/include/linux/random.h b/include/linux/random.h |
3893 |
+index c45b2693e51fb..917470c4490ac 100644 |
3894 |
+--- a/include/linux/random.h |
3895 |
++++ b/include/linux/random.h |
3896 |
+@@ -1,9 +1,5 @@ |
3897 |
+ /* SPDX-License-Identifier: GPL-2.0 */ |
3898 |
+-/* |
3899 |
+- * include/linux/random.h |
3900 |
+- * |
3901 |
+- * Include file for the random number generator. |
3902 |
+- */ |
3903 |
++ |
3904 |
+ #ifndef _LINUX_RANDOM_H |
3905 |
+ #define _LINUX_RANDOM_H |
3906 |
+ |
3907 |
+@@ -14,41 +10,26 @@ |
3908 |
+ |
3909 |
+ #include <uapi/linux/random.h> |
3910 |
+ |
3911 |
+-struct random_ready_callback { |
3912 |
+- struct list_head list; |
3913 |
+- void (*func)(struct random_ready_callback *rdy); |
3914 |
+- struct module *owner; |
3915 |
+-}; |
3916 |
++struct notifier_block; |
3917 |
+ |
3918 |
+-extern void add_device_randomness(const void *, unsigned int); |
3919 |
+-extern void add_bootloader_randomness(const void *, unsigned int); |
3920 |
++void add_device_randomness(const void *buf, size_t len); |
3921 |
++void add_bootloader_randomness(const void *buf, size_t len); |
3922 |
++void add_input_randomness(unsigned int type, unsigned int code, |
3923 |
++ unsigned int value) __latent_entropy; |
3924 |
++void add_interrupt_randomness(int irq) __latent_entropy; |
3925 |
++void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy); |
3926 |
+ |
3927 |
+ #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__) |
3928 |
+ static inline void add_latent_entropy(void) |
3929 |
+ { |
3930 |
+- add_device_randomness((const void *)&latent_entropy, |
3931 |
+- sizeof(latent_entropy)); |
3932 |
++ add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy)); |
3933 |
+ } |
3934 |
+ #else |
3935 |
+-static inline void add_latent_entropy(void) {} |
3936 |
+-#endif |
3937 |
+- |
3938 |
+-extern void add_input_randomness(unsigned int type, unsigned int code, |
3939 |
+- unsigned int value) __latent_entropy; |
3940 |
+-extern void add_interrupt_randomness(int irq) __latent_entropy; |
3941 |
+- |
3942 |
+-extern void get_random_bytes(void *buf, int nbytes); |
3943 |
+-extern int wait_for_random_bytes(void); |
3944 |
+-extern int __init rand_initialize(void); |
3945 |
+-extern bool rng_is_initialized(void); |
3946 |
+-extern int add_random_ready_callback(struct random_ready_callback *rdy); |
3947 |
+-extern void del_random_ready_callback(struct random_ready_callback *rdy); |
3948 |
+-extern int __must_check get_random_bytes_arch(void *buf, int nbytes); |
3949 |
+- |
3950 |
+-#ifndef MODULE |
3951 |
+-extern const struct file_operations random_fops, urandom_fops; |
3952 |
++static inline void add_latent_entropy(void) { } |
3953 |
+ #endif |
3954 |
+ |
3955 |
++void get_random_bytes(void *buf, size_t len); |
3956 |
++size_t __must_check get_random_bytes_arch(void *buf, size_t len); |
3957 |
+ u32 get_random_u32(void); |
3958 |
+ u64 get_random_u64(void); |
3959 |
+ static inline unsigned int get_random_int(void) |
3960 |
+@@ -80,36 +61,38 @@ static inline unsigned long get_random_long(void) |
3961 |
+ |
3962 |
+ static inline unsigned long get_random_canary(void) |
3963 |
+ { |
3964 |
+- unsigned long val = get_random_long(); |
3965 |
+- |
3966 |
+- return val & CANARY_MASK; |
3967 |
++ return get_random_long() & CANARY_MASK; |
3968 |
+ } |
3969 |
+ |
3970 |
++int __init random_init(const char *command_line); |
3971 |
++bool rng_is_initialized(void); |
3972 |
++int wait_for_random_bytes(void); |
3973 |
++int register_random_ready_notifier(struct notifier_block *nb); |
3974 |
++int unregister_random_ready_notifier(struct notifier_block *nb); |
3975 |
++ |
3976 |
+ /* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes). |
3977 |
+ * Returns the result of the call to wait_for_random_bytes. */ |
3978 |
+-static inline int get_random_bytes_wait(void *buf, int nbytes) |
3979 |
++static inline int get_random_bytes_wait(void *buf, size_t nbytes) |
3980 |
+ { |
3981 |
+ int ret = wait_for_random_bytes(); |
3982 |
+ get_random_bytes(buf, nbytes); |
3983 |
+ return ret; |
3984 |
+ } |
3985 |
+ |
3986 |
+-#define declare_get_random_var_wait(var) \ |
3987 |
+- static inline int get_random_ ## var ## _wait(var *out) { \ |
3988 |
++#define declare_get_random_var_wait(name, ret_type) \ |
3989 |
++ static inline int get_random_ ## name ## _wait(ret_type *out) { \ |
3990 |
+ int ret = wait_for_random_bytes(); \ |
3991 |
+ if (unlikely(ret)) \ |
3992 |
+ return ret; \ |
3993 |
+- *out = get_random_ ## var(); \ |
3994 |
++ *out = get_random_ ## name(); \ |
3995 |
+ return 0; \ |
3996 |
+ } |
3997 |
+-declare_get_random_var_wait(u32) |
3998 |
+-declare_get_random_var_wait(u64) |
3999 |
+-declare_get_random_var_wait(int) |
4000 |
+-declare_get_random_var_wait(long) |
4001 |
++declare_get_random_var_wait(u32, u32) |
4002 |
++declare_get_random_var_wait(u64, u32) |
4003 |
++declare_get_random_var_wait(int, unsigned int) |
4004 |
++declare_get_random_var_wait(long, unsigned long) |
4005 |
+ #undef declare_get_random_var |
4006 |
+ |
4007 |
+-unsigned long randomize_page(unsigned long start, unsigned long range); |
4008 |
+- |
4009 |
+ /* |
4010 |
+ * This is designed to be standalone for just prandom |
4011 |
+ * users, but for now we include it from <linux/random.h> |
4012 |
+@@ -120,22 +103,10 @@ unsigned long randomize_page(unsigned long start, unsigned long range); |
4013 |
+ #ifdef CONFIG_ARCH_RANDOM |
4014 |
+ # include <asm/archrandom.h> |
4015 |
+ #else |
4016 |
+-static inline bool __must_check arch_get_random_long(unsigned long *v) |
4017 |
+-{ |
4018 |
+- return false; |
4019 |
+-} |
4020 |
+-static inline bool __must_check arch_get_random_int(unsigned int *v) |
4021 |
+-{ |
4022 |
+- return false; |
4023 |
+-} |
4024 |
+-static inline bool __must_check arch_get_random_seed_long(unsigned long *v) |
4025 |
+-{ |
4026 |
+- return false; |
4027 |
+-} |
4028 |
+-static inline bool __must_check arch_get_random_seed_int(unsigned int *v) |
4029 |
+-{ |
4030 |
+- return false; |
4031 |
+-} |
4032 |
++static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; } |
4033 |
++static inline bool __must_check arch_get_random_int(unsigned int *v) { return false; } |
4034 |
++static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { return false; } |
4035 |
++static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { return false; } |
4036 |
+ #endif |
4037 |
+ |
4038 |
+ /* |
4039 |
+@@ -158,4 +129,13 @@ static inline bool __init arch_get_random_long_early(unsigned long *v) |
4040 |
+ } |
4041 |
+ #endif |
4042 |
+ |
4043 |
++#ifdef CONFIG_SMP |
4044 |
++int random_prepare_cpu(unsigned int cpu); |
4045 |
++int random_online_cpu(unsigned int cpu); |
4046 |
++#endif |
4047 |
++ |
4048 |
++#ifndef MODULE |
4049 |
++extern const struct file_operations random_fops, urandom_fops; |
4050 |
++#endif |
4051 |
++ |
4052 |
+ #endif /* _LINUX_RANDOM_H */ |
4053 |
+diff --git a/include/linux/siphash.h b/include/linux/siphash.h |
4054 |
+index cce8a9acc76cb..3af1428da5597 100644 |
4055 |
+--- a/include/linux/siphash.h |
4056 |
++++ b/include/linux/siphash.h |
4057 |
+@@ -138,4 +138,32 @@ static inline u32 hsiphash(const void *data, size_t len, |
4058 |
+ return ___hsiphash_aligned(data, len, key); |
4059 |
+ } |
4060 |
+ |
4061 |
++/* |
4062 |
++ * These macros expose the raw SipHash and HalfSipHash permutations. |
4063 |
++ * Do not use them directly! If you think you have a use for them, |
4064 |
++ * be sure to CC the maintainer of this file explaining why. |
4065 |
++ */ |
4066 |
++ |
4067 |
++#define SIPHASH_PERMUTATION(a, b, c, d) ( \ |
4068 |
++ (a) += (b), (b) = rol64((b), 13), (b) ^= (a), (a) = rol64((a), 32), \ |
4069 |
++ (c) += (d), (d) = rol64((d), 16), (d) ^= (c), \ |
4070 |
++ (a) += (d), (d) = rol64((d), 21), (d) ^= (a), \ |
4071 |
++ (c) += (b), (b) = rol64((b), 17), (b) ^= (c), (c) = rol64((c), 32)) |
4072 |
++ |
4073 |
++#define SIPHASH_CONST_0 0x736f6d6570736575ULL |
4074 |
++#define SIPHASH_CONST_1 0x646f72616e646f6dULL |
4075 |
++#define SIPHASH_CONST_2 0x6c7967656e657261ULL |
4076 |
++#define SIPHASH_CONST_3 0x7465646279746573ULL |
4077 |
++ |
4078 |
++#define HSIPHASH_PERMUTATION(a, b, c, d) ( \ |
4079 |
++ (a) += (b), (b) = rol32((b), 5), (b) ^= (a), (a) = rol32((a), 16), \ |
4080 |
++ (c) += (d), (d) = rol32((d), 8), (d) ^= (c), \ |
4081 |
++ (a) += (d), (d) = rol32((d), 7), (d) ^= (a), \ |
4082 |
++ (c) += (b), (b) = rol32((b), 13), (b) ^= (c), (c) = rol32((c), 16)) |
4083 |
++ |
4084 |
++#define HSIPHASH_CONST_0 0U |
4085 |
++#define HSIPHASH_CONST_1 0U |
4086 |
++#define HSIPHASH_CONST_2 0x6c796765U |
4087 |
++#define HSIPHASH_CONST_3 0x74656462U |
4088 |
++ |
4089 |
+ #endif /* _LINUX_SIPHASH_H */ |
4090 |
+diff --git a/include/linux/timex.h b/include/linux/timex.h |
4091 |
+index 059b18eb1f1fa..3871b06bd302c 100644 |
4092 |
+--- a/include/linux/timex.h |
4093 |
++++ b/include/linux/timex.h |
4094 |
+@@ -62,6 +62,8 @@ |
4095 |
+ #include <linux/types.h> |
4096 |
+ #include <linux/param.h> |
4097 |
+ |
4098 |
++unsigned long random_get_entropy_fallback(void); |
4099 |
++ |
4100 |
+ #include <asm/timex.h> |
4101 |
+ |
4102 |
+ #ifndef random_get_entropy |
4103 |
+@@ -74,8 +76,14 @@ |
4104 |
+ * |
4105 |
+ * By default we use get_cycles() for this purpose, but individual |
4106 |
+ * architectures may override this in their asm/timex.h header file. |
4107 |
++ * If a given arch does not have get_cycles(), then we fallback to |
4108 |
++ * using random_get_entropy_fallback(). |
4109 |
+ */ |
4110 |
+-#define random_get_entropy() get_cycles() |
4111 |
++#ifdef get_cycles |
4112 |
++#define random_get_entropy() ((unsigned long)get_cycles()) |
4113 |
++#else |
4114 |
++#define random_get_entropy() random_get_entropy_fallback() |
4115 |
++#endif |
4116 |
+ #endif |
4117 |
+ |
4118 |
+ /* |
4119 |
+diff --git a/include/trace/events/random.h b/include/trace/events/random.h |
4120 |
+deleted file mode 100644 |
4121 |
+index a2d9aa16a5d7a..0000000000000 |
4122 |
+--- a/include/trace/events/random.h |
4123 |
++++ /dev/null |
4124 |
+@@ -1,233 +0,0 @@ |
4125 |
+-/* SPDX-License-Identifier: GPL-2.0 */ |
4126 |
+-#undef TRACE_SYSTEM |
4127 |
+-#define TRACE_SYSTEM random |
4128 |
+- |
4129 |
+-#if !defined(_TRACE_RANDOM_H) || defined(TRACE_HEADER_MULTI_READ) |
4130 |
+-#define _TRACE_RANDOM_H |
4131 |
+- |
4132 |
+-#include <linux/writeback.h> |
4133 |
+-#include <linux/tracepoint.h> |
4134 |
+- |
4135 |
+-TRACE_EVENT(add_device_randomness, |
4136 |
+- TP_PROTO(int bytes, unsigned long IP), |
4137 |
+- |
4138 |
+- TP_ARGS(bytes, IP), |
4139 |
+- |
4140 |
+- TP_STRUCT__entry( |
4141 |
+- __field( int, bytes ) |
4142 |
+- __field(unsigned long, IP ) |
4143 |
+- ), |
4144 |
+- |
4145 |
+- TP_fast_assign( |
4146 |
+- __entry->bytes = bytes; |
4147 |
+- __entry->IP = IP; |
4148 |
+- ), |
4149 |
+- |
4150 |
+- TP_printk("bytes %d caller %pS", |
4151 |
+- __entry->bytes, (void *)__entry->IP) |
4152 |
+-); |
4153 |
+- |
4154 |
+-DECLARE_EVENT_CLASS(random__mix_pool_bytes, |
4155 |
+- TP_PROTO(int bytes, unsigned long IP), |
4156 |
+- |
4157 |
+- TP_ARGS(bytes, IP), |
4158 |
+- |
4159 |
+- TP_STRUCT__entry( |
4160 |
+- __field( int, bytes ) |
4161 |
+- __field(unsigned long, IP ) |
4162 |
+- ), |
4163 |
+- |
4164 |
+- TP_fast_assign( |
4165 |
+- __entry->bytes = bytes; |
4166 |
+- __entry->IP = IP; |
4167 |
+- ), |
4168 |
+- |
4169 |
+- TP_printk("input pool: bytes %d caller %pS", |
4170 |
+- __entry->bytes, (void *)__entry->IP) |
4171 |
+-); |
4172 |
+- |
4173 |
+-DEFINE_EVENT(random__mix_pool_bytes, mix_pool_bytes, |
4174 |
+- TP_PROTO(int bytes, unsigned long IP), |
4175 |
+- |
4176 |
+- TP_ARGS(bytes, IP) |
4177 |
+-); |
4178 |
+- |
4179 |
+-DEFINE_EVENT(random__mix_pool_bytes, mix_pool_bytes_nolock, |
4180 |
+- TP_PROTO(int bytes, unsigned long IP), |
4181 |
+- |
4182 |
+- TP_ARGS(bytes, IP) |
4183 |
+-); |
4184 |
+- |
4185 |
+-TRACE_EVENT(credit_entropy_bits, |
4186 |
+- TP_PROTO(int bits, int entropy_count, unsigned long IP), |
4187 |
+- |
4188 |
+- TP_ARGS(bits, entropy_count, IP), |
4189 |
+- |
4190 |
+- TP_STRUCT__entry( |
4191 |
+- __field( int, bits ) |
4192 |
+- __field( int, entropy_count ) |
4193 |
+- __field(unsigned long, IP ) |
4194 |
+- ), |
4195 |
+- |
4196 |
+- TP_fast_assign( |
4197 |
+- __entry->bits = bits; |
4198 |
+- __entry->entropy_count = entropy_count; |
4199 |
+- __entry->IP = IP; |
4200 |
+- ), |
4201 |
+- |
4202 |
+- TP_printk("input pool: bits %d entropy_count %d caller %pS", |
4203 |
+- __entry->bits, __entry->entropy_count, (void *)__entry->IP) |
4204 |
+-); |
4205 |
+- |
4206 |
+-TRACE_EVENT(debit_entropy, |
4207 |
+- TP_PROTO(int debit_bits), |
4208 |
+- |
4209 |
+- TP_ARGS( debit_bits), |
4210 |
+- |
4211 |
+- TP_STRUCT__entry( |
4212 |
+- __field( int, debit_bits ) |
4213 |
+- ), |
4214 |
+- |
4215 |
+- TP_fast_assign( |
4216 |
+- __entry->debit_bits = debit_bits; |
4217 |
+- ), |
4218 |
+- |
4219 |
+- TP_printk("input pool: debit_bits %d", __entry->debit_bits) |
4220 |
+-); |
4221 |
+- |
4222 |
+-TRACE_EVENT(add_input_randomness, |
4223 |
+- TP_PROTO(int input_bits), |
4224 |
+- |
4225 |
+- TP_ARGS(input_bits), |
4226 |
+- |
4227 |
+- TP_STRUCT__entry( |
4228 |
+- __field( int, input_bits ) |
4229 |
+- ), |
4230 |
+- |
4231 |
+- TP_fast_assign( |
4232 |
+- __entry->input_bits = input_bits; |
4233 |
+- ), |
4234 |
+- |
4235 |
+- TP_printk("input_pool_bits %d", __entry->input_bits) |
4236 |
+-); |
4237 |
+- |
4238 |
+-TRACE_EVENT(add_disk_randomness, |
4239 |
+- TP_PROTO(dev_t dev, int input_bits), |
4240 |
+- |
4241 |
+- TP_ARGS(dev, input_bits), |
4242 |
+- |
4243 |
+- TP_STRUCT__entry( |
4244 |
+- __field( dev_t, dev ) |
4245 |
+- __field( int, input_bits ) |
4246 |
+- ), |
4247 |
+- |
4248 |
+- TP_fast_assign( |
4249 |
+- __entry->dev = dev; |
4250 |
+- __entry->input_bits = input_bits; |
4251 |
+- ), |
4252 |
+- |
4253 |
+- TP_printk("dev %d,%d input_pool_bits %d", MAJOR(__entry->dev), |
4254 |
+- MINOR(__entry->dev), __entry->input_bits) |
4255 |
+-); |
4256 |
+- |
4257 |
+-DECLARE_EVENT_CLASS(random__get_random_bytes, |
4258 |
+- TP_PROTO(int nbytes, unsigned long IP), |
4259 |
+- |
4260 |
+- TP_ARGS(nbytes, IP), |
4261 |
+- |
4262 |
+- TP_STRUCT__entry( |
4263 |
+- __field( int, nbytes ) |
4264 |
+- __field(unsigned long, IP ) |
4265 |
+- ), |
4266 |
+- |
4267 |
+- TP_fast_assign( |
4268 |
+- __entry->nbytes = nbytes; |
4269 |
+- __entry->IP = IP; |
4270 |
+- ), |
4271 |
+- |
4272 |
+- TP_printk("nbytes %d caller %pS", __entry->nbytes, (void *)__entry->IP) |
4273 |
+-); |
4274 |
+- |
4275 |
+-DEFINE_EVENT(random__get_random_bytes, get_random_bytes, |
4276 |
+- TP_PROTO(int nbytes, unsigned long IP), |
4277 |
+- |
4278 |
+- TP_ARGS(nbytes, IP) |
4279 |
+-); |
4280 |
+- |
4281 |
+-DEFINE_EVENT(random__get_random_bytes, get_random_bytes_arch, |
4282 |
+- TP_PROTO(int nbytes, unsigned long IP), |
4283 |
+- |
4284 |
+- TP_ARGS(nbytes, IP) |
4285 |
+-); |
4286 |
+- |
4287 |
+-DECLARE_EVENT_CLASS(random__extract_entropy, |
4288 |
+- TP_PROTO(int nbytes, int entropy_count, unsigned long IP), |
4289 |
+- |
4290 |
+- TP_ARGS(nbytes, entropy_count, IP), |
4291 |
+- |
4292 |
+- TP_STRUCT__entry( |
4293 |
+- __field( int, nbytes ) |
4294 |
+- __field( int, entropy_count ) |
4295 |
+- __field(unsigned long, IP ) |
4296 |
+- ), |
4297 |
+- |
4298 |
+- TP_fast_assign( |
4299 |
+- __entry->nbytes = nbytes; |
4300 |
+- __entry->entropy_count = entropy_count; |
4301 |
+- __entry->IP = IP; |
4302 |
+- ), |
4303 |
+- |
4304 |
+- TP_printk("input pool: nbytes %d entropy_count %d caller %pS", |
4305 |
+- __entry->nbytes, __entry->entropy_count, (void *)__entry->IP) |
4306 |
+-); |
4307 |
+- |
4308 |
+- |
4309 |
+-DEFINE_EVENT(random__extract_entropy, extract_entropy, |
4310 |
+- TP_PROTO(int nbytes, int entropy_count, unsigned long IP), |
4311 |
+- |
4312 |
+- TP_ARGS(nbytes, entropy_count, IP) |
4313 |
+-); |
4314 |
+- |
4315 |
+-TRACE_EVENT(urandom_read, |
4316 |
+- TP_PROTO(int got_bits, int pool_left, int input_left), |
4317 |
+- |
4318 |
+- TP_ARGS(got_bits, pool_left, input_left), |
4319 |
+- |
4320 |
+- TP_STRUCT__entry( |
4321 |
+- __field( int, got_bits ) |
4322 |
+- __field( int, pool_left ) |
4323 |
+- __field( int, input_left ) |
4324 |
+- ), |
4325 |
+- |
4326 |
+- TP_fast_assign( |
4327 |
+- __entry->got_bits = got_bits; |
4328 |
+- __entry->pool_left = pool_left; |
4329 |
+- __entry->input_left = input_left; |
4330 |
+- ), |
4331 |
+- |
4332 |
+- TP_printk("got_bits %d nonblocking_pool_entropy_left %d " |
4333 |
+- "input_entropy_left %d", __entry->got_bits, |
4334 |
+- __entry->pool_left, __entry->input_left) |
4335 |
+-); |
4336 |
+- |
4337 |
+-TRACE_EVENT(prandom_u32, |
4338 |
+- |
4339 |
+- TP_PROTO(unsigned int ret), |
4340 |
+- |
4341 |
+- TP_ARGS(ret), |
4342 |
+- |
4343 |
+- TP_STRUCT__entry( |
4344 |
+- __field( unsigned int, ret) |
4345 |
+- ), |
4346 |
+- |
4347 |
+- TP_fast_assign( |
4348 |
+- __entry->ret = ret; |
4349 |
+- ), |
4350 |
+- |
4351 |
+- TP_printk("ret=%u" , __entry->ret) |
4352 |
+-); |
4353 |
+- |
4354 |
+-#endif /* _TRACE_RANDOM_H */ |
4355 |
+- |
4356 |
+-/* This part must be outside protection */ |
4357 |
+-#include <trace/define_trace.h> |
4358 |
+diff --git a/init/main.c b/init/main.c |
4359 |
+index 9a5097b2251a5..0aa2e1c17b1c3 100644 |
4360 |
+--- a/init/main.c |
4361 |
++++ b/init/main.c |
4362 |
+@@ -1035,21 +1035,18 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void) |
4363 |
+ softirq_init(); |
4364 |
+ timekeeping_init(); |
4365 |
+ kfence_init(); |
4366 |
++ time_init(); |
4367 |
+ |
4368 |
+ /* |
4369 |
+ * For best initial stack canary entropy, prepare it after: |
4370 |
+ * - setup_arch() for any UEFI RNG entropy and boot cmdline access |
4371 |
+- * - timekeeping_init() for ktime entropy used in rand_initialize() |
4372 |
+- * - rand_initialize() to get any arch-specific entropy like RDRAND |
4373 |
+- * - add_latent_entropy() to get any latent entropy |
4374 |
+- * - adding command line entropy |
4375 |
++ * - timekeeping_init() for ktime entropy used in random_init() |
4376 |
++ * - time_init() for making random_get_entropy() work on some platforms |
4377 |
++ * - random_init() to initialize the RNG from from early entropy sources |
4378 |
+ */ |
4379 |
+- rand_initialize(); |
4380 |
+- add_latent_entropy(); |
4381 |
+- add_device_randomness(command_line, strlen(command_line)); |
4382 |
++ random_init(command_line); |
4383 |
+ boot_init_stack_canary(); |
4384 |
+ |
4385 |
+- time_init(); |
4386 |
+ perf_event_init(); |
4387 |
+ profile_init(); |
4388 |
+ call_function_init(); |
4389 |
+diff --git a/kernel/cpu.c b/kernel/cpu.c |
4390 |
+index 5601216eb51bd..da871eb075662 100644 |
4391 |
+--- a/kernel/cpu.c |
4392 |
++++ b/kernel/cpu.c |
4393 |
+@@ -34,6 +34,7 @@ |
4394 |
+ #include <linux/scs.h> |
4395 |
+ #include <linux/percpu-rwsem.h> |
4396 |
+ #include <linux/cpuset.h> |
4397 |
++#include <linux/random.h> |
4398 |
+ |
4399 |
+ #include <trace/events/power.h> |
4400 |
+ #define CREATE_TRACE_POINTS |
4401 |
+@@ -1659,6 +1660,11 @@ static struct cpuhp_step cpuhp_hp_states[] = { |
4402 |
+ .startup.single = perf_event_init_cpu, |
4403 |
+ .teardown.single = perf_event_exit_cpu, |
4404 |
+ }, |
4405 |
++ [CPUHP_RANDOM_PREPARE] = { |
4406 |
++ .name = "random:prepare", |
4407 |
++ .startup.single = random_prepare_cpu, |
4408 |
++ .teardown.single = NULL, |
4409 |
++ }, |
4410 |
+ [CPUHP_WORKQUEUE_PREP] = { |
4411 |
+ .name = "workqueue:prepare", |
4412 |
+ .startup.single = workqueue_prepare_cpu, |
4413 |
+@@ -1782,6 +1788,11 @@ static struct cpuhp_step cpuhp_hp_states[] = { |
4414 |
+ .startup.single = workqueue_online_cpu, |
4415 |
+ .teardown.single = workqueue_offline_cpu, |
4416 |
+ }, |
4417 |
++ [CPUHP_AP_RANDOM_ONLINE] = { |
4418 |
++ .name = "random:online", |
4419 |
++ .startup.single = random_online_cpu, |
4420 |
++ .teardown.single = NULL, |
4421 |
++ }, |
4422 |
+ [CPUHP_AP_RCUTREE_ONLINE] = { |
4423 |
+ .name = "RCU/tree:online", |
4424 |
+ .startup.single = rcutree_online_cpu, |
4425 |
+diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c |
4426 |
+index 3b1398fbddaf8..871c912860ed5 100644 |
4427 |
+--- a/kernel/time/timekeeping.c |
4428 |
++++ b/kernel/time/timekeeping.c |
4429 |
+@@ -17,6 +17,7 @@ |
4430 |
+ #include <linux/clocksource.h> |
4431 |
+ #include <linux/jiffies.h> |
4432 |
+ #include <linux/time.h> |
4433 |
++#include <linux/timex.h> |
4434 |
+ #include <linux/tick.h> |
4435 |
+ #include <linux/stop_machine.h> |
4436 |
+ #include <linux/pvclock_gtod.h> |
4437 |
+@@ -2380,6 +2381,20 @@ static int timekeeping_validate_timex(const struct __kernel_timex *txc) |
4438 |
+ return 0; |
4439 |
+ } |
4440 |
+ |
4441 |
++/** |
4442 |
++ * random_get_entropy_fallback - Returns the raw clock source value, |
4443 |
++ * used by random.c for platforms with no valid random_get_entropy(). |
4444 |
++ */ |
4445 |
++unsigned long random_get_entropy_fallback(void) |
4446 |
++{ |
4447 |
++ struct tk_read_base *tkr = &tk_core.timekeeper.tkr_mono; |
4448 |
++ struct clocksource *clock = READ_ONCE(tkr->clock); |
4449 |
++ |
4450 |
++ if (unlikely(timekeeping_suspended || !clock)) |
4451 |
++ return 0; |
4452 |
++ return clock->read(clock); |
4453 |
++} |
4454 |
++EXPORT_SYMBOL_GPL(random_get_entropy_fallback); |
4455 |
+ |
4456 |
+ /** |
4457 |
+ * do_adjtimex() - Accessor function to NTP __do_adjtimex function |
4458 |
+diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug |
4459 |
+index 440fd666c16d1..c7dfe1000111d 100644 |
4460 |
+--- a/lib/Kconfig.debug |
4461 |
++++ b/lib/Kconfig.debug |
4462 |
+@@ -1566,8 +1566,7 @@ config WARN_ALL_UNSEEDED_RANDOM |
4463 |
+ so architecture maintainers really need to do what they can |
4464 |
+ to get the CRNG seeded sooner after the system is booted. |
4465 |
+ However, since users cannot do anything actionable to |
4466 |
+- address this, by default the kernel will issue only a single |
4467 |
+- warning for the first use of unseeded randomness. |
4468 |
++ address this, by default this option is disabled. |
4469 |
+ |
4470 |
+ Say Y here if you want to receive warnings for all uses of |
4471 |
+ unseeded randomness. This will be of use primarily for |
4472 |
+diff --git a/lib/random32.c b/lib/random32.c |
4473 |
+index a57a0e18819d0..976632003ec65 100644 |
4474 |
+--- a/lib/random32.c |
4475 |
++++ b/lib/random32.c |
4476 |
+@@ -41,7 +41,6 @@ |
4477 |
+ #include <linux/bitops.h> |
4478 |
+ #include <linux/slab.h> |
4479 |
+ #include <asm/unaligned.h> |
4480 |
+-#include <trace/events/random.h> |
4481 |
+ |
4482 |
+ /** |
4483 |
+ * prandom_u32_state - seeded pseudo-random number generator. |
4484 |
+@@ -387,7 +386,6 @@ u32 prandom_u32(void) |
4485 |
+ struct siprand_state *state = get_cpu_ptr(&net_rand_state); |
4486 |
+ u32 res = siprand_u32(state); |
4487 |
+ |
4488 |
+- trace_prandom_u32(res); |
4489 |
+ put_cpu_ptr(&net_rand_state); |
4490 |
+ return res; |
4491 |
+ } |
4492 |
+@@ -553,9 +551,11 @@ static void prandom_reseed(struct timer_list *unused) |
4493 |
+ * To avoid worrying about whether it's safe to delay that interrupt |
4494 |
+ * long enough to seed all CPUs, just schedule an immediate timer event. |
4495 |
+ */ |
4496 |
+-static void prandom_timer_start(struct random_ready_callback *unused) |
4497 |
++static int prandom_timer_start(struct notifier_block *nb, |
4498 |
++ unsigned long action, void *data) |
4499 |
+ { |
4500 |
+ mod_timer(&seed_timer, jiffies); |
4501 |
++ return 0; |
4502 |
+ } |
4503 |
+ |
4504 |
+ #ifdef CONFIG_RANDOM32_SELFTEST |
4505 |
+@@ -619,13 +619,13 @@ core_initcall(prandom32_state_selftest); |
4506 |
+ */ |
4507 |
+ static int __init prandom_init_late(void) |
4508 |
+ { |
4509 |
+- static struct random_ready_callback random_ready = { |
4510 |
+- .func = prandom_timer_start |
4511 |
++ static struct notifier_block random_ready = { |
4512 |
++ .notifier_call = prandom_timer_start |
4513 |
+ }; |
4514 |
+- int ret = add_random_ready_callback(&random_ready); |
4515 |
++ int ret = register_random_ready_notifier(&random_ready); |
4516 |
+ |
4517 |
+ if (ret == -EALREADY) { |
4518 |
+- prandom_timer_start(&random_ready); |
4519 |
++ prandom_timer_start(&random_ready, 0, NULL); |
4520 |
+ ret = 0; |
4521 |
+ } |
4522 |
+ return ret; |
4523 |
+diff --git a/lib/siphash.c b/lib/siphash.c |
4524 |
+index 72b9068ab57bf..71d315a6ad623 100644 |
4525 |
+--- a/lib/siphash.c |
4526 |
++++ b/lib/siphash.c |
4527 |
+@@ -18,19 +18,13 @@ |
4528 |
+ #include <asm/word-at-a-time.h> |
4529 |
+ #endif |
4530 |
+ |
4531 |
+-#define SIPROUND \ |
4532 |
+- do { \ |
4533 |
+- v0 += v1; v1 = rol64(v1, 13); v1 ^= v0; v0 = rol64(v0, 32); \ |
4534 |
+- v2 += v3; v3 = rol64(v3, 16); v3 ^= v2; \ |
4535 |
+- v0 += v3; v3 = rol64(v3, 21); v3 ^= v0; \ |
4536 |
+- v2 += v1; v1 = rol64(v1, 17); v1 ^= v2; v2 = rol64(v2, 32); \ |
4537 |
+- } while (0) |
4538 |
++#define SIPROUND SIPHASH_PERMUTATION(v0, v1, v2, v3) |
4539 |
+ |
4540 |
+ #define PREAMBLE(len) \ |
4541 |
+- u64 v0 = 0x736f6d6570736575ULL; \ |
4542 |
+- u64 v1 = 0x646f72616e646f6dULL; \ |
4543 |
+- u64 v2 = 0x6c7967656e657261ULL; \ |
4544 |
+- u64 v3 = 0x7465646279746573ULL; \ |
4545 |
++ u64 v0 = SIPHASH_CONST_0; \ |
4546 |
++ u64 v1 = SIPHASH_CONST_1; \ |
4547 |
++ u64 v2 = SIPHASH_CONST_2; \ |
4548 |
++ u64 v3 = SIPHASH_CONST_3; \ |
4549 |
+ u64 b = ((u64)(len)) << 56; \ |
4550 |
+ v3 ^= key->key[1]; \ |
4551 |
+ v2 ^= key->key[0]; \ |
4552 |
+@@ -389,19 +383,13 @@ u32 hsiphash_4u32(const u32 first, const u32 second, const u32 third, |
4553 |
+ } |
4554 |
+ EXPORT_SYMBOL(hsiphash_4u32); |
4555 |
+ #else |
4556 |
+-#define HSIPROUND \ |
4557 |
+- do { \ |
4558 |
+- v0 += v1; v1 = rol32(v1, 5); v1 ^= v0; v0 = rol32(v0, 16); \ |
4559 |
+- v2 += v3; v3 = rol32(v3, 8); v3 ^= v2; \ |
4560 |
+- v0 += v3; v3 = rol32(v3, 7); v3 ^= v0; \ |
4561 |
+- v2 += v1; v1 = rol32(v1, 13); v1 ^= v2; v2 = rol32(v2, 16); \ |
4562 |
+- } while (0) |
4563 |
++#define HSIPROUND HSIPHASH_PERMUTATION(v0, v1, v2, v3) |
4564 |
+ |
4565 |
+ #define HPREAMBLE(len) \ |
4566 |
+- u32 v0 = 0; \ |
4567 |
+- u32 v1 = 0; \ |
4568 |
+- u32 v2 = 0x6c796765U; \ |
4569 |
+- u32 v3 = 0x74656462U; \ |
4570 |
++ u32 v0 = HSIPHASH_CONST_0; \ |
4571 |
++ u32 v1 = HSIPHASH_CONST_1; \ |
4572 |
++ u32 v2 = HSIPHASH_CONST_2; \ |
4573 |
++ u32 v3 = HSIPHASH_CONST_3; \ |
4574 |
+ u32 b = ((u32)(len)) << 24; \ |
4575 |
+ v3 ^= key->key[1]; \ |
4576 |
+ v2 ^= key->key[0]; \ |
4577 |
+diff --git a/lib/vsprintf.c b/lib/vsprintf.c |
4578 |
+index fbf261bbea950..35cc358f8daee 100644 |
4579 |
+--- a/lib/vsprintf.c |
4580 |
++++ b/lib/vsprintf.c |
4581 |
+@@ -762,14 +762,16 @@ static void enable_ptr_key_workfn(struct work_struct *work) |
4582 |
+ |
4583 |
+ static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); |
4584 |
+ |
4585 |
+-static void fill_random_ptr_key(struct random_ready_callback *unused) |
4586 |
++static int fill_random_ptr_key(struct notifier_block *nb, |
4587 |
++ unsigned long action, void *data) |
4588 |
+ { |
4589 |
+ /* This may be in an interrupt handler. */ |
4590 |
+ queue_work(system_unbound_wq, &enable_ptr_key_work); |
4591 |
++ return 0; |
4592 |
+ } |
4593 |
+ |
4594 |
+-static struct random_ready_callback random_ready = { |
4595 |
+- .func = fill_random_ptr_key |
4596 |
++static struct notifier_block random_ready = { |
4597 |
++ .notifier_call = fill_random_ptr_key |
4598 |
+ }; |
4599 |
+ |
4600 |
+ static int __init initialize_ptr_random(void) |
4601 |
+@@ -783,7 +785,7 @@ static int __init initialize_ptr_random(void) |
4602 |
+ return 0; |
4603 |
+ } |
4604 |
+ |
4605 |
+- ret = add_random_ready_callback(&random_ready); |
4606 |
++ ret = register_random_ready_notifier(&random_ready); |
4607 |
+ if (!ret) { |
4608 |
+ return 0; |
4609 |
+ } else if (ret == -EALREADY) { |
4610 |
+diff --git a/mm/util.c b/mm/util.c |
4611 |
+index d3102081add00..5223d7e2f65ec 100644 |
4612 |
+--- a/mm/util.c |
4613 |
++++ b/mm/util.c |
4614 |
+@@ -343,6 +343,38 @@ unsigned long randomize_stack_top(unsigned long stack_top) |
4615 |
+ #endif |
4616 |
+ } |
4617 |
+ |
4618 |
++/** |
4619 |
++ * randomize_page - Generate a random, page aligned address |
4620 |
++ * @start: The smallest acceptable address the caller will take. |
4621 |
++ * @range: The size of the area, starting at @start, within which the |
4622 |
++ * random address must fall. |
4623 |
++ * |
4624 |
++ * If @start + @range would overflow, @range is capped. |
4625 |
++ * |
4626 |
++ * NOTE: Historical use of randomize_range, which this replaces, presumed that |
4627 |
++ * @start was already page aligned. We now align it regardless. |
4628 |
++ * |
4629 |
++ * Return: A page aligned address within [start, start + range). On error, |
4630 |
++ * @start is returned. |
4631 |
++ */ |
4632 |
++unsigned long randomize_page(unsigned long start, unsigned long range) |
4633 |
++{ |
4634 |
++ if (!PAGE_ALIGNED(start)) { |
4635 |
++ range -= PAGE_ALIGN(start) - start; |
4636 |
++ start = PAGE_ALIGN(start); |
4637 |
++ } |
4638 |
++ |
4639 |
++ if (start > ULONG_MAX - range) |
4640 |
++ range = ULONG_MAX - start; |
4641 |
++ |
4642 |
++ range >>= PAGE_SHIFT; |
4643 |
++ |
4644 |
++ if (range == 0) |
4645 |
++ return start; |
4646 |
++ |
4647 |
++ return start + (get_random_long() % range << PAGE_SHIFT); |
4648 |
++} |
4649 |
++ |
4650 |
+ #ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT |
4651 |
+ unsigned long arch_randomize_brk(struct mm_struct *mm) |
4652 |
+ { |
4653 |
+diff --git a/sound/pci/ctxfi/ctatc.c b/sound/pci/ctxfi/ctatc.c |
4654 |
+index 78f35e88aed6b..fbdb8a3d5b8e5 100644 |
4655 |
+--- a/sound/pci/ctxfi/ctatc.c |
4656 |
++++ b/sound/pci/ctxfi/ctatc.c |
4657 |
+@@ -36,6 +36,7 @@ |
4658 |
+ | ((IEC958_AES3_CON_FS_48000) << 24)) |
4659 |
+ |
4660 |
+ static const struct snd_pci_quirk subsys_20k1_list[] = { |
4661 |
++ SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0021, "SB046x", CTSB046X), |
4662 |
+ SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X), |
4663 |
+ SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X), |
4664 |
+ SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X), |
4665 |
+@@ -64,6 +65,7 @@ static const struct snd_pci_quirk subsys_20k2_list[] = { |
4666 |
+ |
4667 |
+ static const char *ct_subsys_name[NUM_CTCARDS] = { |
4668 |
+ /* 20k1 models */ |
4669 |
++ [CTSB046X] = "SB046x", |
4670 |
+ [CTSB055X] = "SB055x", |
4671 |
+ [CTSB073X] = "SB073x", |
4672 |
+ [CTUAA] = "UAA", |
4673 |
+diff --git a/sound/pci/ctxfi/cthardware.h b/sound/pci/ctxfi/cthardware.h |
4674 |
+index f406b626a28c4..2875cec83b8f2 100644 |
4675 |
+--- a/sound/pci/ctxfi/cthardware.h |
4676 |
++++ b/sound/pci/ctxfi/cthardware.h |
4677 |
+@@ -26,8 +26,9 @@ enum CHIPTYP { |
4678 |
+ |
4679 |
+ enum CTCARDS { |
4680 |
+ /* 20k1 models */ |
4681 |
++ CTSB046X, |
4682 |
++ CT20K1_MODEL_FIRST = CTSB046X, |
4683 |
+ CTSB055X, |
4684 |
+- CT20K1_MODEL_FIRST = CTSB055X, |
4685 |
+ CTSB073X, |
4686 |
+ CTUAA, |
4687 |
+ CT20K1_UNKNOWN, |