Skip to content

Commit 99a2c78

Browse files
committed
Merge tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random
Pull random updates from Ted Ts'o: "Some changes to trust cpu-based hwrng (such as RDRAND) for initializing hashed pointers and (optionally, controlled by a config option) to initialize the CRNG to avoid boot hangs" * tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random: random: Make crng state queryable random: remove preempt disabled region random: add a config option to trust the CPU's hwrng vsprintf: Add command line option debug_boot_weak_hash vsprintf: Use hw RNG for ptr_key random: Return nbytes filled from hw RNG random: Fix whitespace pre random-bytes work
2 parents fa3b39c + 9a47249 commit 99a2c78

5 files changed

Lines changed: 85 additions & 16 deletions

File tree

‎Documentation/admin-guide/kernel-parameters.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,14 @@
748748

749749
debug [KNL] Enable kernel debugging (events log level).
750750

751+
debug_boot_weak_hash
752+
[KNL] Enable printing [hashed] pointers early in the
753+
boot sequence. If enabled, we use a weak hash instead
754+
of siphash to hash pointers. Use this option if you are
755+
seeing instances of '(___ptrval___)') and need to see a
756+
value (hashed pointer) instead. Cryptographically
757+
insecure, please do not use on production kernels.
758+
751759
debug_locks_verbose=
752760
[KNL] verbose self-tests
753761
Format=<0|1>

‎drivers/char/Kconfig‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,17 @@ config ADI
554554

555555
endmenu
556556

557+
config RANDOM_TRUST_CPU
558+
bool "Trust the CPU manufacturer to initialize Linux's CRNG"
559+
depends on X86 || S390 || PPC
560+
default n
561+
help
562+
Assume that CPU manufacturer (e.g., Intel or AMD for RDSEED or
563+
RDRAND, IBM for the S390 and Power PC architectures) is trustworthy
564+
for the purposes of initializing Linux's CRNG. Since this is not
565+
something that can be independently audited, this amounts to trusting
566+
that CPU manufacturer (perhaps with the insistence or mandate
567+
of a Nation State's intelligence or law enforcement agencies)
568+
has not installed a hidden back door to compromise the CPU's
569+
random number generation facilities.
570+

‎drivers/char/random.c‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ static void invalidate_batched_entropy(void);
782782
static void crng_initialize(struct crng_state *crng)
783783
{
784784
int i;
785+
int arch_init = 1;
785786
unsigned long rv;
786787

787788
memcpy(&crng->state[0], "expand 32-byte k", 16);
@@ -792,10 +793,18 @@ static void crng_initialize(struct crng_state *crng)
792793
_get_random_bytes(&crng->state[4], sizeof(__u32) * 12);
793794
for (i = 4; i < 16; i++) {
794795
if (!arch_get_random_seed_long(&rv) &&
795-
!arch_get_random_long(&rv))
796+
!arch_get_random_long(&rv)) {
796797
rv = random_get_entropy();
798+
arch_init = 0;
799+
}
797800
crng->state[i] ^= rv;
798801
}
802+
#ifdef CONFIG_RANDOM_TRUST_CPU
803+
if (arch_init) {
804+
crng_init = 2;
805+
pr_notice("random: crng done (trusting CPU's manufacturer)\n");
806+
}
807+
#endif
799808
crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
800809
}
801810

@@ -1122,8 +1131,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
11221131
} sample;
11231132
long delta, delta2, delta3;
11241133

1125-
preempt_disable();
1126-
11271134
sample.jiffies = jiffies;
11281135
sample.cycles = random_get_entropy();
11291136
sample.num = num;
@@ -1161,8 +1168,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
11611168
* and limit entropy entimate to 12 bits.
11621169
*/
11631170
credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
1164-
1165-
preempt_enable();
11661171
}
11671172

11681173
void add_input_randomness(unsigned int type, unsigned int code,
@@ -1658,6 +1663,21 @@ int wait_for_random_bytes(void)
16581663
}
16591664
EXPORT_SYMBOL(wait_for_random_bytes);
16601665

1666+
/*
1667+
* Returns whether or not the urandom pool has been seeded and thus guaranteed
1668+
* to supply cryptographically secure random numbers. This applies to: the
1669+
* /dev/urandom device, the get_random_bytes function, and the get_random_{u32,
1670+
* ,u64,int,long} family of functions.
1671+
*
1672+
* Returns: true if the urandom pool has been seeded.
1673+
* false if the urandom pool has not been seeded.
1674+
*/
1675+
bool rng_is_initialized(void)
1676+
{
1677+
return crng_ready();
1678+
}
1679+
EXPORT_SYMBOL(rng_is_initialized);
1680+
16611681
/*
16621682
* Add a callback function that will be invoked when the nonblocking
16631683
* pool is initialised.
@@ -1725,30 +1745,31 @@ EXPORT_SYMBOL(del_random_ready_callback);
17251745
* key known by the NSA). So it's useful if we need the speed, but
17261746
* only if we're willing to trust the hardware manufacturer not to
17271747
* have put in a back door.
1748+
*
1749+
* Return number of bytes filled in.
17281750
*/
1729-
void get_random_bytes_arch(void *buf, int nbytes)
1751+
int __must_check get_random_bytes_arch(void *buf, int nbytes)
17301752
{
1753+
int left = nbytes;
17311754
char *p = buf;
17321755

1733-
trace_get_random_bytes_arch(nbytes, _RET_IP_);
1734-
while (nbytes) {
1756+
trace_get_random_bytes_arch(left, _RET_IP_);
1757+
while (left) {
17351758
unsigned long v;
1736-
int chunk = min(nbytes, (int)sizeof(unsigned long));
1759+
int chunk = min_t(int, left, sizeof(unsigned long));
17371760

17381761
if (!arch_get_random_long(&v))
17391762
break;
1740-
1763+
17411764
memcpy(p, &v, chunk);
17421765
p += chunk;
1743-
nbytes -= chunk;
1766+
left -= chunk;
17441767
}
17451768

1746-
if (nbytes)
1747-
get_random_bytes(p, nbytes);
1769+
return nbytes - left;
17481770
}
17491771
EXPORT_SYMBOL(get_random_bytes_arch);
17501772

1751-
17521773
/*
17531774
* init_std_data - initialize pool with system data
17541775
*

‎include/linux/random.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
3636

3737
extern void get_random_bytes(void *buf, int nbytes);
3838
extern int wait_for_random_bytes(void);
39+
extern bool rng_is_initialized(void);
3940
extern int add_random_ready_callback(struct random_ready_callback *rdy);
4041
extern void del_random_ready_callback(struct random_ready_callback *rdy);
41-
extern void get_random_bytes_arch(void *buf, int nbytes);
42+
extern int __must_check get_random_bytes_arch(void *buf, int nbytes);
4243

4344
#ifndef MODULE
4445
extern const struct file_operations random_fops, urandom_fops;

‎lib/vsprintf.c‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,17 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
16511651
return widen_string(buf, buf - buf_start, end, spec);
16521652
}
16531653

1654+
/* Make pointers available for printing early in the boot sequence. */
1655+
static int debug_boot_weak_hash __ro_after_init;
1656+
1657+
static int __init debug_boot_weak_hash_enable(char *str)
1658+
{
1659+
debug_boot_weak_hash = 1;
1660+
pr_info("debug_boot_weak_hash enabled\n");
1661+
return 0;
1662+
}
1663+
early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
1664+
16541665
static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
16551666
static siphash_key_t ptr_key __read_mostly;
16561667

@@ -1675,8 +1686,16 @@ static struct random_ready_callback random_ready = {
16751686

16761687
static int __init initialize_ptr_random(void)
16771688
{
1678-
int ret = add_random_ready_callback(&random_ready);
1689+
int key_size = sizeof(ptr_key);
1690+
int ret;
16791691

1692+
/* Use hw RNG if available. */
1693+
if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
1694+
static_branch_disable(&not_filled_random_ptr_key);
1695+
return 0;
1696+
}
1697+
1698+
ret = add_random_ready_callback(&random_ready);
16801699
if (!ret) {
16811700
return 0;
16821701
} else if (ret == -EALREADY) {
@@ -1695,6 +1714,12 @@ static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
16951714
const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
16961715
unsigned long hashval;
16971716

1717+
/* When debugging early boot use non-cryptographically secure hash. */
1718+
if (unlikely(debug_boot_weak_hash)) {
1719+
hashval = hash_long((unsigned long)ptr, 32);
1720+
return pointer_string(buf, end, (const void *)hashval, spec);
1721+
}
1722+
16981723
if (static_branch_unlikely(&not_filled_random_ptr_key)) {
16991724
spec.field_width = 2 * sizeof(ptr);
17001725
/* string length must be less than default_width */

0 commit comments

Comments
 (0)