274 lines
8.8 KiB
Diff
274 lines
8.8 KiB
Diff
From 4b97692c181f44717a5cf940fe1e6451c974f4b4 Mon Sep 17 00:00:00 2001
|
|
From: Jeremy Cline <jeremy@jcline.org>
|
|
Date: Tue, 1 May 2018 18:52:14 -0400
|
|
Subject: [PATCH 1/4] Revert "random: add new ioctl RNDRESEEDCRNG"
|
|
|
|
This reverts commit e21e58679d3e9db0258106ffd3c8db76e66c5e6e.
|
|
|
|
Signed-off-by: Jeremy Cline <jeremy@jcline.org>
|
|
---
|
|
drivers/char/random.c | 13 +------------
|
|
include/uapi/linux/random.h | 3 ---
|
|
2 files changed, 1 insertion(+), 15 deletions(-)
|
|
|
|
diff --git a/drivers/char/random.c b/drivers/char/random.c
|
|
index 38729baed6ee..b3571a1c5fb7 100644
|
|
--- a/drivers/char/random.c
|
|
+++ b/drivers/char/random.c
|
|
@@ -429,7 +429,6 @@ struct crng_state primary_crng = {
|
|
static int crng_init = 0;
|
|
#define crng_ready() (likely(crng_init > 1))
|
|
static int crng_init_cnt = 0;
|
|
-static unsigned long crng_global_init_time = 0;
|
|
#define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE)
|
|
static void _extract_crng(struct crng_state *crng,
|
|
__u32 out[CHACHA20_BLOCK_WORDS]);
|
|
@@ -906,8 +905,7 @@ static void _extract_crng(struct crng_state *crng,
|
|
unsigned long v, flags;
|
|
|
|
if (crng_ready() &&
|
|
- (time_after(crng_global_init_time, crng->init_time) ||
|
|
- time_after(jiffies, crng->init_time + CRNG_RESEED_INTERVAL)))
|
|
+ time_after(jiffies, crng->init_time + CRNG_RESEED_INTERVAL))
|
|
crng_reseed(crng, crng == &primary_crng ? &input_pool : NULL);
|
|
spin_lock_irqsave(&crng->lock, flags);
|
|
if (arch_get_random_long(&v))
|
|
@@ -1740,7 +1738,6 @@ static int rand_initialize(void)
|
|
init_std_data(&input_pool);
|
|
init_std_data(&blocking_pool);
|
|
crng_initialize(&primary_crng);
|
|
- crng_global_init_time = jiffies;
|
|
|
|
#ifdef CONFIG_NUMA
|
|
pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL|__GFP_NOFAIL);
|
|
@@ -1927,14 +1924,6 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
|
|
input_pool.entropy_count = 0;
|
|
blocking_pool.entropy_count = 0;
|
|
return 0;
|
|
- case RNDRESEEDCRNG:
|
|
- if (!capable(CAP_SYS_ADMIN))
|
|
- return -EPERM;
|
|
- if (crng_init < 2)
|
|
- return -ENODATA;
|
|
- crng_reseed(&primary_crng, NULL);
|
|
- crng_global_init_time = jiffies - 1;
|
|
- return 0;
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
diff --git a/include/uapi/linux/random.h b/include/uapi/linux/random.h
|
|
index 26ee91300e3e..c34f4490d025 100644
|
|
--- a/include/uapi/linux/random.h
|
|
+++ b/include/uapi/linux/random.h
|
|
@@ -35,9 +35,6 @@
|
|
/* Clear the entropy pool and associated counters. (Superuser only.) */
|
|
#define RNDCLEARPOOL _IO( 'R', 0x06 )
|
|
|
|
-/* Reseed CRNG. (Superuser only.) */
|
|
-#define RNDRESEEDCRNG _IO( 'R', 0x07 )
|
|
-
|
|
struct rand_pool_info {
|
|
int entropy_count;
|
|
int buf_size;
|
|
--
|
|
2.17.0
|
|
|
|
From 5084e3e3af02cdc4e247e66561215b6b06ccffc8 Mon Sep 17 00:00:00 2001
|
|
From: Jeremy Cline <jeremy@jcline.org>
|
|
Date: Tue, 1 May 2018 18:52:17 -0400
|
|
Subject: [PATCH 2/4] Revert "random: crng_reseed() should lock the crng
|
|
instance that it is modifying"
|
|
|
|
This reverts commit 6efa23d5851f1702a3cddbdde63607ea6588b665.
|
|
|
|
Signed-off-by: Jeremy Cline <jeremy@jcline.org>
|
|
---
|
|
drivers/char/random.c | 4 ++--
|
|
1 file changed, 2 insertions(+), 2 deletions(-)
|
|
|
|
diff --git a/drivers/char/random.c b/drivers/char/random.c
|
|
index b3571a1c5fb7..fda8214543cc 100644
|
|
--- a/drivers/char/random.c
|
|
+++ b/drivers/char/random.c
|
|
@@ -879,7 +879,7 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
|
|
_crng_backtrack_protect(&primary_crng, buf.block,
|
|
CHACHA20_KEY_SIZE);
|
|
}
|
|
- spin_lock_irqsave(&crng->lock, flags);
|
|
+ spin_lock_irqsave(&primary_crng.lock, flags);
|
|
for (i = 0; i < 8; i++) {
|
|
unsigned long rv;
|
|
if (!arch_get_random_seed_long(&rv) &&
|
|
@@ -889,7 +889,7 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
|
|
}
|
|
memzero_explicit(&buf, sizeof(buf));
|
|
crng->init_time = jiffies;
|
|
- spin_unlock_irqrestore(&crng->lock, flags);
|
|
+ spin_unlock_irqrestore(&primary_crng.lock, flags);
|
|
if (crng == &primary_crng && crng_init < 2) {
|
|
invalidate_batched_entropy();
|
|
crng_init = 2;
|
|
--
|
|
2.17.0
|
|
|
|
From f3035506194216b5220be6fd8a2c5666412978a0 Mon Sep 17 00:00:00 2001
|
|
From: Jeremy Cline <jeremy@jcline.org>
|
|
Date: Tue, 1 May 2018 18:52:19 -0400
|
|
Subject: [PATCH 3/4] Revert "random: use a different mixing algorithm for
|
|
add_device_randomness()"
|
|
|
|
This reverts commit 89b59f050347d376c2ace8b1ceb908a218cfdc2e.
|
|
|
|
Signed-off-by: Jeremy Cline <jeremy@jcline.org>
|
|
---
|
|
drivers/char/random.c | 55 ++++---------------------------------------
|
|
1 file changed, 4 insertions(+), 51 deletions(-)
|
|
|
|
diff --git a/drivers/char/random.c b/drivers/char/random.c
|
|
index fda8214543cc..d0b2f89e9c6e 100644
|
|
--- a/drivers/char/random.c
|
|
+++ b/drivers/char/random.c
|
|
@@ -786,10 +786,6 @@ static void crng_initialize(struct crng_state *crng)
|
|
crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
|
|
}
|
|
|
|
-/*
|
|
- * crng_fast_load() can be called by code in the interrupt service
|
|
- * path. So we can't afford to dilly-dally.
|
|
- */
|
|
static int crng_fast_load(const char *cp, size_t len)
|
|
{
|
|
unsigned long flags;
|
|
@@ -816,51 +812,6 @@ static int crng_fast_load(const char *cp, size_t len)
|
|
return 1;
|
|
}
|
|
|
|
-/*
|
|
- * crng_slow_load() is called by add_device_randomness, which has two
|
|
- * attributes. (1) We can't trust the buffer passed to it is
|
|
- * guaranteed to be unpredictable (so it might not have any entropy at
|
|
- * all), and (2) it doesn't have the performance constraints of
|
|
- * crng_fast_load().
|
|
- *
|
|
- * So we do something more comprehensive which is guaranteed to touch
|
|
- * all of the primary_crng's state, and which uses a LFSR with a
|
|
- * period of 255 as part of the mixing algorithm. Finally, we do
|
|
- * *not* advance crng_init_cnt since buffer we may get may be something
|
|
- * like a fixed DMI table (for example), which might very well be
|
|
- * unique to the machine, but is otherwise unvarying.
|
|
- */
|
|
-static int crng_slow_load(const char *cp, size_t len)
|
|
-{
|
|
- unsigned long flags;
|
|
- static unsigned char lfsr = 1;
|
|
- unsigned char tmp;
|
|
- unsigned i, max = CHACHA20_KEY_SIZE;
|
|
- const char * src_buf = cp;
|
|
- char * dest_buf = (char *) &primary_crng.state[4];
|
|
-
|
|
- if (!spin_trylock_irqsave(&primary_crng.lock, flags))
|
|
- return 0;
|
|
- if (crng_init != 0) {
|
|
- spin_unlock_irqrestore(&primary_crng.lock, flags);
|
|
- return 0;
|
|
- }
|
|
- if (len > max)
|
|
- max = len;
|
|
-
|
|
- for (i = 0; i < max ; i++) {
|
|
- tmp = lfsr;
|
|
- lfsr >>= 1;
|
|
- if (tmp & 1)
|
|
- lfsr ^= 0xE1;
|
|
- tmp = dest_buf[i % CHACHA20_KEY_SIZE];
|
|
- dest_buf[i % CHACHA20_KEY_SIZE] ^= src_buf[i % len] ^ lfsr;
|
|
- lfsr += (tmp << 3) | (tmp >> 5);
|
|
- }
|
|
- spin_unlock_irqrestore(&primary_crng.lock, flags);
|
|
- return 1;
|
|
-}
|
|
-
|
|
static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
|
|
{
|
|
unsigned long flags;
|
|
@@ -1030,8 +981,10 @@ void add_device_randomness(const void *buf, unsigned int size)
|
|
unsigned long time = random_get_entropy() ^ jiffies;
|
|
unsigned long flags;
|
|
|
|
- if (!crng_ready() && size)
|
|
- crng_slow_load(buf, size);
|
|
+ if (!crng_ready()) {
|
|
+ crng_fast_load(buf, size);
|
|
+ return;
|
|
+ }
|
|
|
|
trace_add_device_randomness(size, _RET_IP_);
|
|
spin_lock_irqsave(&input_pool.lock, flags);
|
|
--
|
|
2.17.0
|
|
|
|
From fc6be05c0f6503dab8e2c5c69cafda38d9d9417a Mon Sep 17 00:00:00 2001
|
|
From: Jeremy Cline <jeremy@jcline.org>
|
|
Date: Tue, 1 May 2018 18:52:20 -0400
|
|
Subject: [PATCH 4/4] Revert "random: fix crng_ready() test"
|
|
|
|
This reverts commit cd8d7a5778a4abf76ee8fe8f1bfcf78976029f8d.
|
|
|
|
Signed-off-by: Jeremy Cline <jeremy@jcline.org>
|
|
---
|
|
drivers/char/random.c | 10 +++++-----
|
|
1 file changed, 5 insertions(+), 5 deletions(-)
|
|
|
|
diff --git a/drivers/char/random.c b/drivers/char/random.c
|
|
index d0b2f89e9c6e..11c23ca57430 100644
|
|
--- a/drivers/char/random.c
|
|
+++ b/drivers/char/random.c
|
|
@@ -427,7 +427,7 @@ struct crng_state primary_crng = {
|
|
* its value (from 0->1->2).
|
|
*/
|
|
static int crng_init = 0;
|
|
-#define crng_ready() (likely(crng_init > 1))
|
|
+#define crng_ready() (likely(crng_init > 0))
|
|
static int crng_init_cnt = 0;
|
|
#define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE)
|
|
static void _extract_crng(struct crng_state *crng,
|
|
@@ -793,7 +793,7 @@ static int crng_fast_load(const char *cp, size_t len)
|
|
|
|
if (!spin_trylock_irqsave(&primary_crng.lock, flags))
|
|
return 0;
|
|
- if (crng_init != 0) {
|
|
+ if (crng_ready()) {
|
|
spin_unlock_irqrestore(&primary_crng.lock, flags);
|
|
return 0;
|
|
}
|
|
@@ -855,7 +855,7 @@ static void _extract_crng(struct crng_state *crng,
|
|
{
|
|
unsigned long v, flags;
|
|
|
|
- if (crng_ready() &&
|
|
+ if (crng_init > 1 &&
|
|
time_after(jiffies, crng->init_time + CRNG_RESEED_INTERVAL))
|
|
crng_reseed(crng, crng == &primary_crng ? &input_pool : NULL);
|
|
spin_lock_irqsave(&crng->lock, flags);
|
|
@@ -1141,7 +1141,7 @@ void add_interrupt_randomness(int irq, int irq_flags)
|
|
fast_mix(fast_pool);
|
|
add_interrupt_bench(cycles);
|
|
|
|
- if (unlikely(crng_init == 0)) {
|
|
+ if (!crng_ready()) {
|
|
if ((fast_pool->count >= 64) &&
|
|
crng_fast_load((char *) fast_pool->pool,
|
|
sizeof(fast_pool->pool))) {
|
|
@@ -2214,7 +2214,7 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,
|
|
{
|
|
struct entropy_store *poolp = &input_pool;
|
|
|
|
- if (unlikely(crng_init == 0)) {
|
|
+ if (!crng_ready()) {
|
|
crng_fast_load(buffer, count);
|
|
return;
|
|
}
|
|
--
|
|
2.17.0
|
|
|