public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Theodore Ts'o" <tytso@mit.edu>
To: linux-kernel@vger.kernel.org
Cc: akpm@osdl.org
Subject: [PATCH] [3/4] /dev/random: Use separate entropy store for /dev/urandom
Date: Fri, 20 Aug 2004 00:57:20 -0400	[thread overview]
Message-ID: <E1By1Sq-0001TP-BV@thunk.org> (raw)


This patch adds a separate pool for use with /dev/urandom.  This
prevents a /dev/urandom read from being able to completely drain the
entropy in the /dev/random pool, and also makes it much more difficult
for an attacker to carry out a state extension attack.

patch-random-3-urandom-pool

--- random.c	2004/08/19 22:49:48	1.3
+++ random.c	2004/08/19 22:50:19	1.4
@@ -401,6 +401,7 @@
  */
 static struct entropy_store *random_state; /* The default global store */
 static struct entropy_store *sec_random_state; /* secondary store */
+static struct entropy_store *urandom_state; /* For urandom */
 static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
 static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
 
@@ -1474,14 +1475,21 @@
  */
 void get_random_bytes(void *buf, int nbytes)
 {
-	if (sec_random_state)  
-		extract_entropy(sec_random_state, (char *) buf, nbytes, 
-				EXTRACT_ENTROPY_SECONDARY);
-	else if (random_state)
-		extract_entropy(random_state, (char *) buf, nbytes, 0);
-	else
+	struct entropy_store *r = urandom_state;
+	int flags = EXTRACT_ENTROPY_SECONDARY;
+
+	if (!r)
+		r = sec_random_state;
+	if (!r) {
+		r = random_state;
+		flags = 0;
+	}
+	if (!r) {
 		printk(KERN_NOTICE "get_random_bytes called before "
 				   "random driver initialization\n");
+		return;
+	}
+	extract_entropy(r, (char *) buf, nbytes, flags);
 }
 
 EXPORT_SYMBOL(get_random_bytes);
@@ -1532,8 +1540,12 @@
 	if (create_entropy_store(SECONDARY_POOL_SIZE, "secondary", 
 				 &sec_random_state))
 		goto err;
+	if (create_entropy_store(SECONDARY_POOL_SIZE, "urandom", 
+				 &urandom_state))
+		goto err;
 	clear_entropy_store(random_state);
 	clear_entropy_store(sec_random_state);
+	clear_entropy_store(urandom_state);
 	init_std_data(random_state);
 #ifdef CONFIG_SYSCTL
 	sysctl_init_random(random_state);
@@ -1667,9 +1679,15 @@
 urandom_read(struct file * file, char __user * buf,
 		      size_t nbytes, loff_t *ppos)
 {
-	return extract_entropy(sec_random_state, buf, nbytes,
-			       EXTRACT_ENTROPY_USER |
-			       EXTRACT_ENTROPY_SECONDARY);
+	int flags = EXTRACT_ENTROPY_USER;
+	unsigned long cpuflags;
+
+	spin_lock_irqsave(&random_state->lock, cpuflags);
+	if (random_state->entropy_count > random_state->poolinfo.POOLBITS)
+		flags |= EXTRACT_ENTROPY_SECONDARY;
+	spin_unlock_irqrestore(&random_state->lock, cpuflags);
+
+	return extract_entropy(urandom_state, buf, nbytes, flags);
 }
 
 static unsigned int

             reply	other threads:[~2004-08-20  4:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-20  4:57 Theodore Ts'o [this message]
2004-08-24 21:22 ` [PATCH] [3/4] /dev/random: Use separate entropy store for /dev/urandom Matt Mackall
  -- strict thread matches above, loose matches on Subject: below --
2004-08-28 10:29 Balint Marton
2004-08-30  2:10 ` Theodore Ts'o

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1By1Sq-0001TP-BV@thunk.org \
    --to=tytso@mit.edu \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox