Skip to content

Commit eecabf5

Browse files
committed
random: suppress spammy warnings about unseeded randomness
Unfortunately, on some models of some architectures getting a fully seeded CRNG is extremely difficult, and so this can result in dmesg getting spammed for a surprisingly long time. This is really bad from a security perspective, and so architecture maintainers really need to do what they can to get the CRNG seeded sooner after the system is booted. However, users can't do anything actionble to address this, and spamming the kernel messages log will only just annoy people. For developers who want to work on improving this situation, CONFIG_WARN_UNSEEDED_RANDOM has been renamed to CONFIG_WARN_ALL_UNSEEDED_RANDOM. By default the kernel will always print the first use of unseeded randomness. This way, hopefully the security obsessed will be happy that there is _some_ indication when the kernel boots there may be a potential issue with that architecture or subarchitecture. To see all uses of unseeded randomness, developers can enable CONFIG_WARN_ALL_UNSEEDED_RANDOM. Signed-off-by: Theodore Ts'o <[email protected]>
1 parent d06bfd1 commit eecabf5

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

drivers/char/random.c

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ static void _extract_crng(struct crng_state *crng,
436436
static void _crng_backtrack_protect(struct crng_state *crng,
437437
__u8 tmp[CHACHA20_BLOCK_SIZE], int used);
438438
static void process_random_ready_list(void);
439+
static void _get_random_bytes(void *buf, int nbytes);
439440

440441
/**********************************************************************
441442
*
@@ -776,7 +777,7 @@ static void crng_initialize(struct crng_state *crng)
776777
_extract_entropy(&input_pool, &crng->state[4],
777778
sizeof(__u32) * 12, 0);
778779
else
779-
get_random_bytes(&crng->state[4], sizeof(__u32) * 12);
780+
_get_random_bytes(&crng->state[4], sizeof(__u32) * 12);
780781
for (i = 4; i < 16; i++) {
781782
if (!arch_get_random_seed_long(&rv) &&
782783
!arch_get_random_long(&rv))
@@ -1466,6 +1467,30 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
14661467
return ret;
14671468
}
14681469

1470+
#define warn_unseeded_randomness(previous) \
1471+
_warn_unseeded_randomness(__func__, (void *) _RET_IP_, (previous))
1472+
1473+
static void _warn_unseeded_randomness(const char *func_name, void *caller,
1474+
void **previous)
1475+
{
1476+
#ifdef CONFIG_WARN_ALL_UNSEEDED_RANDOM
1477+
const bool print_once = false;
1478+
#else
1479+
static bool print_once __read_mostly;
1480+
#endif
1481+
1482+
if (print_once ||
1483+
crng_ready() ||
1484+
(previous && (caller == READ_ONCE(*previous))))
1485+
return;
1486+
WRITE_ONCE(*previous, caller);
1487+
#ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM
1488+
print_once = true;
1489+
#endif
1490+
pr_notice("random: %s called from %pF with crng_init=%d\n",
1491+
func_name, caller, crng_init);
1492+
}
1493+
14691494
/*
14701495
* This function is the exported kernel interface. It returns some
14711496
* number of good random numbers, suitable for key generation, seeding
@@ -1476,15 +1501,10 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
14761501
* wait_for_random_bytes() should be called and return 0 at least once
14771502
* at any point prior.
14781503
*/
1479-
void get_random_bytes(void *buf, int nbytes)
1504+
static void _get_random_bytes(void *buf, int nbytes)
14801505
{
14811506
__u8 tmp[CHACHA20_BLOCK_SIZE];
14821507

1483-
#ifdef CONFIG_WARN_UNSEEDED_RANDOM
1484-
if (!crng_ready())
1485-
printk(KERN_NOTICE "random: %pF get_random_bytes called "
1486-
"with crng_init = %d\n", (void *) _RET_IP_, crng_init);
1487-
#endif
14881508
trace_get_random_bytes(nbytes, _RET_IP_);
14891509

14901510
while (nbytes >= CHACHA20_BLOCK_SIZE) {
@@ -1501,6 +1521,14 @@ void get_random_bytes(void *buf, int nbytes)
15011521
crng_backtrack_protect(tmp, CHACHA20_BLOCK_SIZE);
15021522
memzero_explicit(tmp, sizeof(tmp));
15031523
}
1524+
1525+
void get_random_bytes(void *buf, int nbytes)
1526+
{
1527+
static void *previous;
1528+
1529+
warn_unseeded_randomness(&previous);
1530+
_get_random_bytes(buf, nbytes);
1531+
}
15041532
EXPORT_SYMBOL(get_random_bytes);
15051533

15061534
/*
@@ -2064,6 +2092,7 @@ u64 get_random_u64(void)
20642092
bool use_lock = READ_ONCE(crng_init) < 2;
20652093
unsigned long flags = 0;
20662094
struct batched_entropy *batch;
2095+
static void *previous;
20672096

20682097
#if BITS_PER_LONG == 64
20692098
if (arch_get_random_long((unsigned long *)&ret))
@@ -2074,11 +2103,7 @@ u64 get_random_u64(void)
20742103
return ret;
20752104
#endif
20762105

2077-
#ifdef CONFIG_WARN_UNSEEDED_RANDOM
2078-
if (!crng_ready())
2079-
printk(KERN_NOTICE "random: %pF get_random_u64 called "
2080-
"with crng_init = %d\n", (void *) _RET_IP_, crng_init);
2081-
#endif
2106+
warn_unseeded_randomness(&previous);
20822107

20832108
batch = &get_cpu_var(batched_entropy_u64);
20842109
if (use_lock)
@@ -2102,15 +2127,12 @@ u32 get_random_u32(void)
21022127
bool use_lock = READ_ONCE(crng_init) < 2;
21032128
unsigned long flags = 0;
21042129
struct batched_entropy *batch;
2130+
static void *previous;
21052131

21062132
if (arch_get_random_int(&ret))
21072133
return ret;
21082134

2109-
#ifdef CONFIG_WARN_UNSEEDED_RANDOM
2110-
if (!crng_ready())
2111-
printk(KERN_NOTICE "random: %pF get_random_u32 called "
2112-
"with crng_init = %d\n", (void *) _RET_IP_, crng_init);
2113-
#endif
2135+
warn_unseeded_randomness(&previous);
21142136

21152137
batch = &get_cpu_var(batched_entropy_u32);
21162138
if (use_lock)

lib/Kconfig.debug

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,9 @@ config STACKTRACE
12091209
It is also used by various kernel debugging features that require
12101210
stack trace generation.
12111211

1212-
config WARN_UNSEEDED_RANDOM
1213-
bool "Warn when kernel uses unseeded randomness"
1214-
default y
1215-
depends on DEBUG_KERNEL
1212+
config WARN_ALL_UNSEEDED_RANDOM
1213+
bool "Warn for all uses of unseeded randomness"
1214+
default n
12161215
help
12171216
Some parts of the kernel contain bugs relating to their use of
12181217
cryptographically secure random numbers before it's actually possible
@@ -1222,8 +1221,21 @@ config WARN_UNSEEDED_RANDOM
12221221
are going wrong, so that they might contact developers about fixing
12231222
it.
12241223

1225-
Say Y here, unless you simply do not care about using unseeded
1226-
randomness and do not want a potential warning message in your logs.
1224+
Unfortunately, on some models of some architectures getting
1225+
a fully seeded CRNG is extremely difficult, and so this can
1226+
result in dmesg getting spammed for a surprisingly long
1227+
time. This is really bad from a security perspective, and
1228+
so architecture maintainers really need to do what they can
1229+
to get the CRNG seeded sooner after the system is booted.
1230+
However, since users can not do anything actionble to
1231+
address this, by default the kernel will issue only a single
1232+
warning for the first use of unseeded randomness.
1233+
1234+
Say Y here if you want to receive warnings for all uses of
1235+
unseeded randomness. This will be of use primarily for
1236+
those developers interersted in improving the security of
1237+
Linux kernels running on their architecture (or
1238+
subarchitecture).
12271239

12281240
config DEBUG_KOBJECT
12291241
bool "kobject debugging"

0 commit comments

Comments
 (0)