|
1 | 1 | --- ./drivers/char/random.c.dist 2020-12-13 16:41:30.000000000 -0600 |
2 | | -+++ ./drivers/char/random.c 2025-06-28 10:11:47.329492006 -0500 |
| 2 | ++++ ./drivers/char/random.c 2025-07-02 11:59:07.220250957 -0500 |
3 | 3 | @@ -344,6 +344,260 @@ |
4 | 4 | #include <asm/irq_regs.h> |
5 | 5 | #include <asm/io.h> |
|
261 | 261 | #define CREATE_TRACE_POINTS |
262 | 262 | #include <trace/events/random.h> |
263 | 263 |
|
264 | | -@@ -461,7 +715,13 @@ static struct crng_state primary_crng = |
| 264 | +@@ -461,7 +715,22 @@ static struct crng_state primary_crng = |
265 | 265 | * its value (from 0->1->2). |
266 | 266 | */ |
267 | 267 | static int crng_init = 0; |
268 | | --#define crng_ready() (likely(crng_init > 1)) |
269 | 268 | + |
| 269 | + #define crng_ready() (likely(crng_init > 1)) |
270 | 270 | +#ifdef WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS |
271 | | -+ #define crng_ready() (atomic_read(&random_bytes_cb_refcnt) ? call_crng_ready_cb() : (likely(crng_init > 1))) |
| 271 | ++ #define crng_ready_by_cb() (atomic_read(&random_bytes_cb_refcnt) && call_crng_ready_cb()) |
| 272 | ++ #define crng_ready_maybe_cb() (atomic_read(&random_bytes_cb_refcnt) ? (call_crng_ready_cb() || crng_ready()) : crng_ready()) |
272 | 273 | +#else |
273 | | -+ #define crng_ready() (likely(crng_init > 1)) |
| 274 | ++ #define crng_ready_maybe_cb() crng_ready() |
| 275 | ++#endif |
| 276 | ++ |
| 277 | ++#ifdef WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS |
| 278 | ++ #define crng_ready_by_cb() (atomic_read(&random_bytes_cb_refcnt) && call_crng_ready_cb()) |
| 279 | ++ #define crng_ready_maybe_cb() (atomic_read(&random_bytes_cb_refcnt) ? (call_crng_ready_cb() || crng_ready()) : crng_ready()) |
| 280 | ++#else |
| 281 | ++ #define crng_ready_maybe_cb() crng_ready() |
274 | 282 | +#endif |
275 | 283 | + |
276 | 284 | static int crng_init_cnt = 0; |
277 | 285 | static unsigned long crng_global_init_time = 0; |
278 | 286 | #define CRNG_INIT_CNT_THRESH (2*CHACHA_KEY_SIZE) |
279 | | -@@ -593,6 +853,11 @@ static void mix_pool_bytes(struct entrop |
| 287 | +@@ -593,6 +862,11 @@ static void mix_pool_bytes(struct entrop |
280 | 288 | { |
281 | 289 | unsigned long flags; |
282 | 290 |
|
|
288 | 296 | trace_mix_pool_bytes(r->name, nbytes, _RET_IP_); |
289 | 297 | spin_lock_irqsave(&r->lock, flags); |
290 | 298 | _mix_pool_bytes(r, in, nbytes); |
291 | | -@@ -664,6 +929,10 @@ static void credit_entropy_bits(struct e |
| 299 | +@@ -664,6 +938,10 @@ static void credit_entropy_bits(struct e |
292 | 300 | const int pool_size = r->poolinfo->poolfracbits; |
293 | 301 | int nfrac = nbits << ENTROPY_SHIFT; |
294 | 302 |
|
|
299 | 307 | if (!nbits) |
300 | 308 | return; |
301 | 309 |
|
302 | | -@@ -954,6 +1223,11 @@ static void crng_reseed(struct crng_stat |
303 | | - __u32 key[8]; |
304 | | - } buf; |
305 | | - |
306 | | -+#ifdef WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS |
307 | | -+ (void)call_crng_reseed_cb(); |
308 | | -+ /* fall through to reseed native crng too. */ |
309 | | -+#endif |
310 | | -+ |
311 | | - if (r) { |
312 | | - num = extract_entropy(r, &buf, 32, 16, 0); |
313 | | - if (num == 0) |
314 | | -@@ -1069,6 +1343,18 @@ static ssize_t extract_crng_user(void __ |
| 310 | +@@ -1069,6 +1347,18 @@ static ssize_t extract_crng_user(void __ |
315 | 311 | __u8 tmp[CHACHA_BLOCK_SIZE] __aligned(4); |
316 | 312 | int large_request = (nbytes > 256); |
317 | 313 |
|
|
330 | 326 | while (nbytes) { |
331 | 327 | if (large_request && need_resched()) { |
332 | 328 | if (signal_pending(current)) { |
333 | | -@@ -1552,6 +1838,14 @@ static void _get_random_bytes(void *buf, |
| 329 | +@@ -1523,7 +1813,7 @@ static void _warn_unseeded_randomness(co |
| 330 | + #endif |
| 331 | + |
| 332 | + if (print_once || |
| 333 | +- crng_ready() || |
| 334 | ++ crng_ready_maybe_cb() || |
| 335 | + (previous && (caller == READ_ONCE(*previous)))) |
| 336 | + return; |
| 337 | + WRITE_ONCE(*previous, caller); |
| 338 | +@@ -1552,6 +1842,14 @@ static void _get_random_bytes(void *buf, |
334 | 339 |
|
335 | 340 | trace_get_random_bytes(nbytes, _RET_IP_); |
336 | 341 |
|
|
345 | 350 | while (nbytes >= CHACHA_BLOCK_SIZE) { |
346 | 351 | extract_crng(buf); |
347 | 352 | buf += CHACHA_BLOCK_SIZE; |
| 353 | +@@ -1638,12 +1936,12 @@ static void try_to_generate_entropy(void |
| 354 | + */ |
| 355 | + int wait_for_random_bytes(void) |
| 356 | + { |
| 357 | +- if (likely(crng_ready())) |
| 358 | ++ if (likely(crng_ready_maybe_cb())) |
| 359 | + return 0; |
| 360 | + |
| 361 | + do { |
| 362 | + int ret; |
| 363 | +- ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ); |
| 364 | ++ ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready_maybe_cb(), HZ); |
| 365 | + if (ret) |
| 366 | + return ret > 0 ? 0 : ret; |
| 367 | + |
| 368 | +@@ -1665,7 +1963,7 @@ EXPORT_SYMBOL(wait_for_random_bytes); |
| 369 | + */ |
| 370 | + bool rng_is_initialized(void) |
| 371 | + { |
| 372 | +- return crng_ready(); |
| 373 | ++ return crng_ready_maybe_cb(); |
| 374 | + } |
| 375 | + EXPORT_SYMBOL(rng_is_initialized); |
| 376 | + |
| 377 | +@@ -1843,7 +2141,7 @@ urandom_read(struct file *file, char __u |
| 378 | + unsigned long flags; |
| 379 | + static int maxwarn = 10; |
| 380 | + |
| 381 | +- if (!crng_ready() && maxwarn > 0) { |
| 382 | ++ if (!crng_ready_maybe_cb() && maxwarn > 0) { |
| 383 | + maxwarn--; |
| 384 | + if (__ratelimit(&urandom_warning)) |
| 385 | + pr_notice("%s: uninitialized urandom read (%zd bytes read)\n", |
| 386 | +@@ -1872,6 +2170,11 @@ random_poll(struct file *file, poll_tabl |
| 387 | + { |
| 388 | + __poll_t mask; |
| 389 | + |
| 390 | ++#ifdef WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS |
| 391 | ++ if (crng_ready_by_cb()) |
| 392 | ++ return EPOLLIN | EPOLLRDNORM; |
| 393 | ++#endif |
| 394 | ++ |
| 395 | + poll_wait(file, &crng_init_wait, wait); |
| 396 | + poll_wait(file, &random_write_wait, wait); |
| 397 | + mask = 0; |
| 398 | +@@ -1970,6 +2273,16 @@ static long random_ioctl(struct file *f, |
| 399 | + case RNDRESEEDCRNG: |
| 400 | + if (!capable(CAP_SYS_ADMIN)) |
| 401 | + return -EPERM; |
| 402 | ++#ifdef WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS |
| 403 | ++ /* fall through to reseed native crng too. */ |
| 404 | ++ if (call_crng_reseed_cb() == 0) { |
| 405 | ++ if (crng_init >= 2) { |
| 406 | ++ crng_reseed(&primary_crng, &input_pool); |
| 407 | ++ crng_global_init_time = jiffies - 1; |
| 408 | ++ } |
| 409 | ++ return 0; |
| 410 | ++ } |
| 411 | ++#endif |
| 412 | + if (crng_init < 2) |
| 413 | + return -ENODATA; |
| 414 | + crng_reseed(&primary_crng, NULL); |
| 415 | +@@ -2022,7 +2335,7 @@ SYSCALL_DEFINE3(getrandom, char __user * |
| 416 | + if (count > INT_MAX) |
| 417 | + count = INT_MAX; |
| 418 | + |
| 419 | +- if (!(flags & GRND_INSECURE) && !crng_ready()) { |
| 420 | ++ if (!(flags & GRND_INSECURE) && !crng_ready_maybe_cb()) { |
| 421 | + if (flags & GRND_NONBLOCK) |
| 422 | + return -EAGAIN; |
| 423 | + ret = wait_for_random_bytes(); |
348 | 424 | --- ./include/linux/random.h.dist 2020-12-13 16:41:30.000000000 -0600 |
349 | 425 | +++ ./include/linux/random.h 2025-06-30 12:05:59.106440700 -0500 |
350 | 426 | @@ -158,4 +158,37 @@ static inline bool __init arch_get_rando |
|
0 commit comments