Skip to content

Commit 76dbabb

Browse files
tytsogregkh
authored andcommitted
random: rate limit unseeded randomness warnings
commit 4e00b33 upstream. On systems without sufficient boot randomness, no point spamming dmesg. Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ffc5b50 commit 76dbabb

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

‎drivers/char/random.c‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@
261261
#include <linux/ptrace.h>
262262
#include <linux/workqueue.h>
263263
#include <linux/irq.h>
264+
#include <linux/ratelimit.h>
264265
#include <linux/syscalls.h>
265266
#include <linux/completion.h>
266267
#include <linux/uuid.h>
@@ -438,6 +439,16 @@ static void _crng_backtrack_protect(struct crng_state *crng,
438439
static void process_random_ready_list(void);
439440
static void _get_random_bytes(void *buf, int nbytes);
440441

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+
441452
/**********************************************************************
442453
*
443454
* 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)
931942
process_random_ready_list();
932943
wake_up_interruptible(&crng_init_wait);
933944
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+
}
934957
}
935958
}
936959

@@ -1574,8 +1597,9 @@ static void _warn_unseeded_randomness(const char *func_name, void *caller,
15741597
#ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM
15751598
print_once = true;
15761599
#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);
15791603
}
15801604

15811605
/*
@@ -1769,6 +1793,10 @@ static int rand_initialize(void)
17691793
init_std_data(&blocking_pool);
17701794
crng_initialize(&primary_crng);
17711795
crng_global_init_time = jiffies;
1796+
if (ratelimit_disable) {
1797+
urandom_warning.interval = 0;
1798+
unseeded_warning.interval = 0;
1799+
}
17721800
return 0;
17731801
}
17741802
early_initcall(rand_initialize);
@@ -1836,9 +1864,10 @@ urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
18361864

18371865
if (!crng_ready() && maxwarn > 0) {
18381866
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);
18421871
spin_lock_irqsave(&primary_crng.lock, flags);
18431872
crng_init_cnt = 0;
18441873
spin_unlock_irqrestore(&primary_crng.lock, flags);

0 commit comments

Comments
 (0)