|
261 | 261 | #include <linux/ptrace.h> |
262 | 262 | #include <linux/workqueue.h> |
263 | 263 | #include <linux/irq.h> |
| 264 | +#include <linux/ratelimit.h> |
264 | 265 | #include <linux/syscalls.h> |
265 | 266 | #include <linux/completion.h> |
266 | 267 | #include <linux/uuid.h> |
@@ -438,6 +439,16 @@ static void _crng_backtrack_protect(struct crng_state *crng, |
438 | 439 | static void process_random_ready_list(void); |
439 | 440 | static void _get_random_bytes(void *buf, int nbytes); |
440 | 441 |
|
| 442 | +static struct ratelimit_state unseeded_warning = |
| 443 | + RATELIMIT_STATE_INIT("warn_unseeded_randomness", HZ, 3); |
| 444 | +static struct ratelimit_state urandom_warning = |
| 445 | + RATELIMIT_STATE_INIT("warn_urandom_randomness", HZ, 3); |
| 446 | + |
| 447 | +static int ratelimit_disable __read_mostly; |
| 448 | + |
| 449 | +module_param_named(ratelimit_disable, ratelimit_disable, int, 0644); |
| 450 | +MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression"); |
| 451 | + |
441 | 452 | /********************************************************************** |
442 | 453 | * |
443 | 454 | * OS independent entropy store. Here are the functions which handle |
@@ -931,6 +942,18 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r) |
931 | 942 | process_random_ready_list(); |
932 | 943 | wake_up_interruptible(&crng_init_wait); |
933 | 944 | pr_notice("random: crng init done\n"); |
| 945 | + if (unseeded_warning.missed) { |
| 946 | + pr_notice("random: %d get_random_xx warning(s) missed " |
| 947 | + "due to ratelimiting\n", |
| 948 | + unseeded_warning.missed); |
| 949 | + unseeded_warning.missed = 0; |
| 950 | + } |
| 951 | + if (urandom_warning.missed) { |
| 952 | + pr_notice("random: %d urandom warning(s) missed " |
| 953 | + "due to ratelimiting\n", |
| 954 | + urandom_warning.missed); |
| 955 | + urandom_warning.missed = 0; |
| 956 | + } |
934 | 957 | } |
935 | 958 | } |
936 | 959 |
|
@@ -1574,8 +1597,9 @@ static void _warn_unseeded_randomness(const char *func_name, void *caller, |
1574 | 1597 | #ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM |
1575 | 1598 | print_once = true; |
1576 | 1599 | #endif |
1577 | | - pr_notice("random: %s called from %pS with crng_init=%d\n", |
1578 | | - func_name, caller, crng_init); |
| 1600 | + if (__ratelimit(&unseeded_warning)) |
| 1601 | + pr_notice("random: %s called from %pS with crng_init=%d\n", |
| 1602 | + func_name, caller, crng_init); |
1579 | 1603 | } |
1580 | 1604 |
|
1581 | 1605 | /* |
@@ -1769,6 +1793,10 @@ static int rand_initialize(void) |
1769 | 1793 | init_std_data(&blocking_pool); |
1770 | 1794 | crng_initialize(&primary_crng); |
1771 | 1795 | crng_global_init_time = jiffies; |
| 1796 | + if (ratelimit_disable) { |
| 1797 | + urandom_warning.interval = 0; |
| 1798 | + unseeded_warning.interval = 0; |
| 1799 | + } |
1772 | 1800 | return 0; |
1773 | 1801 | } |
1774 | 1802 | early_initcall(rand_initialize); |
@@ -1836,9 +1864,10 @@ urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) |
1836 | 1864 |
|
1837 | 1865 | if (!crng_ready() && maxwarn > 0) { |
1838 | 1866 | maxwarn--; |
1839 | | - printk(KERN_NOTICE "random: %s: uninitialized urandom read " |
1840 | | - "(%zd bytes read)\n", |
1841 | | - current->comm, nbytes); |
| 1867 | + if (__ratelimit(&urandom_warning)) |
| 1868 | + printk(KERN_NOTICE "random: %s: uninitialized " |
| 1869 | + "urandom read (%zd bytes read)\n", |
| 1870 | + current->comm, nbytes); |
1842 | 1871 | spin_lock_irqsave(&primary_crng.lock, flags); |
1843 | 1872 | crng_init_cnt = 0; |
1844 | 1873 | spin_unlock_irqrestore(&primary_crng.lock, flags); |
|
0 commit comments