Commit 115e3f94 authored by Matt Mackall's avatar Matt Mackall Committed by Linus Torvalds

[PATCH] random: Reseed pointer in pool struct

Put pointer to reseed pool in pool struct and automatically pull entropy from
it if it is set.  This lets us remove the EXTRACT_SECONDARY flag.
Signed-off-by: default avatarMatt Mackall <mpm@selenic.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 2fb4f725
...@@ -406,12 +406,14 @@ module_param(debug, bool, 0644); ...@@ -406,12 +406,14 @@ module_param(debug, bool, 0644);
* *
**********************************************************************/ **********************************************************************/
struct entropy_store;
struct entropy_store { struct entropy_store {
/* mostly-read data: */ /* mostly-read data: */
struct poolinfo *poolinfo; struct poolinfo *poolinfo;
__u32 *pool; __u32 *pool;
const char *name; const char *name;
int limit; int limit;
struct entropy_store *pull;
/* read-write data: */ /* read-write data: */
spinlock_t lock ____cacheline_aligned_in_smp; spinlock_t lock ____cacheline_aligned_in_smp;
...@@ -436,6 +438,7 @@ static struct entropy_store blocking_pool = { ...@@ -436,6 +438,7 @@ static struct entropy_store blocking_pool = {
.poolinfo = &poolinfo_table[1], .poolinfo = &poolinfo_table[1],
.name = "blocking", .name = "blocking",
.limit = 1, .limit = 1,
.pull = &input_pool,
.lock = SPIN_LOCK_UNLOCKED, .lock = SPIN_LOCK_UNLOCKED,
.pool = blocking_pool_data .pool = blocking_pool_data
}; };
...@@ -443,6 +446,7 @@ static struct entropy_store blocking_pool = { ...@@ -443,6 +446,7 @@ static struct entropy_store blocking_pool = {
static struct entropy_store nonblocking_pool = { static struct entropy_store nonblocking_pool = {
.poolinfo = &poolinfo_table[1], .poolinfo = &poolinfo_table[1],
.name = "nonblocking", .name = "nonblocking",
.pull = &input_pool,
.lock = SPIN_LOCK_UNLOCKED, .lock = SPIN_LOCK_UNLOCKED,
.pool = nonblocking_pool_data .pool = nonblocking_pool_data
}; };
...@@ -1180,7 +1184,6 @@ static void MD5Transform(__u32 buf[HASH_BUFFER_SIZE], __u32 const in[16]) ...@@ -1180,7 +1184,6 @@ static void MD5Transform(__u32 buf[HASH_BUFFER_SIZE], __u32 const in[16])
*********************************************************************/ *********************************************************************/
#define EXTRACT_ENTROPY_USER 1 #define EXTRACT_ENTROPY_USER 1
#define EXTRACT_ENTROPY_SECONDARY 2
#define TMP_BUF_SIZE (HASH_BUFFER_SIZE + HASH_EXTRA_SIZE) #define TMP_BUF_SIZE (HASH_BUFFER_SIZE + HASH_EXTRA_SIZE)
#define SEC_XFER_SIZE (TMP_BUF_SIZE*4) #define SEC_XFER_SIZE (TMP_BUF_SIZE*4)
...@@ -1195,7 +1198,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void * buf, ...@@ -1195,7 +1198,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void * buf,
static inline void xfer_secondary_pool(struct entropy_store *r, static inline void xfer_secondary_pool(struct entropy_store *r,
size_t nbytes, __u32 *tmp) size_t nbytes, __u32 *tmp)
{ {
if (r->entropy_count < nbytes * 8 && if (r->pull && r->entropy_count < nbytes * 8 &&
r->entropy_count < r->poolinfo->POOLBITS) { r->entropy_count < r->poolinfo->POOLBITS) {
int bytes = max_t(int, random_read_wakeup_thresh / 8, int bytes = max_t(int, random_read_wakeup_thresh / 8,
min_t(int, nbytes, TMP_BUF_SIZE)); min_t(int, nbytes, TMP_BUF_SIZE));
...@@ -1205,7 +1208,7 @@ static inline void xfer_secondary_pool(struct entropy_store *r, ...@@ -1205,7 +1208,7 @@ static inline void xfer_secondary_pool(struct entropy_store *r,
"(%d of %d requested)\n", "(%d of %d requested)\n",
r->name, bytes * 8, nbytes * 8, r->entropy_count); r->name, bytes * 8, nbytes * 8, r->entropy_count);
bytes=extract_entropy(&input_pool, tmp, bytes, bytes=extract_entropy(r->pull, tmp, bytes,
random_read_wakeup_thresh / 8, rsvd, 0); random_read_wakeup_thresh / 8, rsvd, 0);
add_entropy_words(r, tmp, (bytes + 3) / 4); add_entropy_words(r, tmp, (bytes + 3) / 4);
credit_entropy_store(r, bytes*8); credit_entropy_store(r, bytes*8);
...@@ -1219,10 +1222,6 @@ static inline void xfer_secondary_pool(struct entropy_store *r, ...@@ -1219,10 +1222,6 @@ static inline void xfer_secondary_pool(struct entropy_store *r,
* number of bytes that are actually obtained. If the EXTRACT_ENTROPY_USER * number of bytes that are actually obtained. If the EXTRACT_ENTROPY_USER
* flag is given, then the buf pointer is assumed to be in user space. * flag is given, then the buf pointer is assumed to be in user space.
* *
* If the EXTRACT_ENTROPY_SECONDARY flag is given, then we are actually
* extracting entropy from the secondary pool, and can refill from the
* primary pool if needed.
*
* The min parameter specifies the minimum amount we can pull before * The min parameter specifies the minimum amount we can pull before
* failing to avoid races that defeat catastrophic reseeding while the * failing to avoid races that defeat catastrophic reseeding while the
* reserved parameter indicates how much entropy we must leave in the * reserved parameter indicates how much entropy we must leave in the
...@@ -1242,8 +1241,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void * buf, ...@@ -1242,8 +1241,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void * buf,
if (r->entropy_count > r->poolinfo->POOLBITS) if (r->entropy_count > r->poolinfo->POOLBITS)
r->entropy_count = r->poolinfo->POOLBITS; r->entropy_count = r->poolinfo->POOLBITS;
if (flags & EXTRACT_ENTROPY_SECONDARY) xfer_secondary_pool(r, nbytes, tmp);
xfer_secondary_pool(r, nbytes, tmp);
/* Hold lock while accounting */ /* Hold lock while accounting */
spin_lock_irqsave(&r->lock, cpuflags); spin_lock_irqsave(&r->lock, cpuflags);
...@@ -1358,8 +1356,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void * buf, ...@@ -1358,8 +1356,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void * buf,
*/ */
void get_random_bytes(void *buf, int nbytes) void get_random_bytes(void *buf, int nbytes)
{ {
extract_entropy(&nonblocking_pool, (char *) buf, nbytes, 0, 0, extract_entropy(&nonblocking_pool, (char *) buf, nbytes, 0, 0, 0);
EXTRACT_ENTROPY_SECONDARY);
} }
EXPORT_SYMBOL(get_random_bytes); EXPORT_SYMBOL(get_random_bytes);
...@@ -1449,8 +1446,7 @@ random_read(struct file * file, char __user * buf, size_t nbytes, loff_t *ppos) ...@@ -1449,8 +1446,7 @@ random_read(struct file * file, char __user * buf, size_t nbytes, loff_t *ppos)
DEBUG_ENT("reading %d bits\n", n*8); DEBUG_ENT("reading %d bits\n", n*8);
n = extract_entropy(&blocking_pool, buf, n, 0, 0, n = extract_entropy(&blocking_pool, buf, n, 0, 0,
EXTRACT_ENTROPY_USER | EXTRACT_ENTROPY_USER);
EXTRACT_ENTROPY_SECONDARY);
DEBUG_ENT("read got %d bits (%d still needed)\n", DEBUG_ENT("read got %d bits (%d still needed)\n",
n*8, (nbytes-n)*8); n*8, (nbytes-n)*8);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment