All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oliver Xymoron <oxymoron@waste.org>
To: Linus Torvalds <torvalds@transmeta.com>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: [PATCH] (4/4) entropy batching update
Date: Sat, 17 Aug 2002 21:32:07 -0500	[thread overview]
Message-ID: <20020818023207.GE21643@waste.org> (raw)
In-Reply-To: <20020818022949.GD21643@waste.org>

This patch lets the entropy batching pool safely wrap around, allowing
untrusted samples to be mixed in without risk of flooding out trusted 
samples.

diff -ur a/drivers/char/random.c b/drivers/char/random.c
--- a/drivers/char/random.c	2002-08-17 19:55:31.000000000 -0500
+++ b/drivers/char/random.c	2002-08-17 19:55:38.000000000 -0500
@@ -596,25 +596,19 @@
  *
  **********************************************************************/
 
-static __u32	*batch_entropy_pool;
-static int	*batch_entropy_credit;
-static int	batch_max;
-static int	batch_head, batch_tail;
+static __u32 *batch_entropy_pool=0;
+static int batch_max, batch_pos, batch_credit, batch_samples;
 static struct tq_struct	batch_tqueue;
 static void batch_entropy_process(void *private_);
 
 /* note: the size must be a power of 2 */
 static int __init batch_entropy_init(int size, struct entropy_store *r)
 {
-	batch_entropy_pool = kmalloc(2*size*sizeof(__u32), GFP_KERNEL);
+	batch_entropy_pool = kmalloc(size*sizeof(__u32), GFP_KERNEL);
 	if (!batch_entropy_pool)
 		return -1;
-	batch_entropy_credit =kmalloc(size*sizeof(int), GFP_KERNEL);
-	if (!batch_entropy_credit) {
-		kfree(batch_entropy_pool);
-		return -1;
-	}
-	batch_head = batch_tail = 0;
+
+	batch_pos = batch_credit = batch_samples = 0;
 	batch_max = size;
 	batch_tqueue.routine = batch_entropy_process;
 	batch_tqueue.data = r;
@@ -622,57 +616,61 @@
 }
 
 /*
- * Changes to the entropy data is put into a queue rather than being
+ * Changes to the entropy data are put into a queue rather than being
  * added to the entropy counts directly.  This is to avoid doing heavy
  * hashing calculations during an interrupt in add_timing_entropy().
  * Instead, the entropy is only added to the pool once per timer tick.
+ *
+ * The batch pool intentionally allows wrap-around, to protect against
+ * flooding of untrusted data. Non-random data will not correlate with
+ * random data and can be safely XORed over existing data.
  */
 
-void batch_entropy_store(u32 a, u32 b, int num)
+void batch_entropy_store(u32 val, int bits)
 {
-	int	new;
-
 	if (!batch_max)
 		return;
 	
-	batch_entropy_pool[2*batch_head] = a;
-	batch_entropy_pool[(2*batch_head) + 1] = b;
-	batch_entropy_credit[batch_head] = num;
-
-	new = (batch_head+1) & (batch_max-1);
-	if (new != batch_tail) {
-		queue_task(&batch_tqueue, &tq_timer);
-		batch_head = new;
-	} else {
-		DEBUG_ENT("batch entropy buffer full\n");
-	}
+	batch_entropy_pool[batch_pos] ^= val;
+	batch_credit+=bits;
+	batch_samples++;
+	batch_pos = (batch_pos+1) & (batch_max-1);
+
+	queue_task(&batch_tqueue, &tq_timer);
 }
 
 /*
- * Flush out the accumulated entropy operations, adding entropy to the passed
- * store (normally random_state).  If that store has enough entropy, alternate
- * between randomizing the data of the primary and secondary stores.
+ * Flush out the accumulated entropy operations, adding entropy to the
+ * passed store (normally random_state). Alternate between randomizing
+ * the data of the primary and secondary stores.
  */
 static void batch_entropy_process(void *private_)
 {
-	struct entropy_store *r	= (struct entropy_store *) private_, *p;
-	int max_entropy = r->poolinfo.POOLBITS;
-
+	struct entropy_store *r	= (struct entropy_store *) private_;
+	int samples, credit;
+	
 	if (!batch_max)
 		return;
 
-	p = r;
-	while (batch_head != batch_tail) {
-		if (r->entropy_count >= max_entropy) {
-			r = (r == sec_random_state) ?	random_state :
-							sec_random_state;
-			max_entropy = r->poolinfo.POOLBITS;
-		}
-		add_entropy_words(r, batch_entropy_pool + 2*batch_tail, 2);
-		credit_entropy_store(r, batch_entropy_credit[batch_tail]);
-		batch_tail = (batch_tail+1) & (batch_max-1);
+	/* switch pools if current full */
+	if (r->entropy_count >= r->poolinfo.POOLBITS) {
+		r = (r == sec_random_state) ? 
+			random_state : sec_random_state;
 	}
-	if (p->entropy_count >= random_read_wakeup_thresh)
+
+	credit=batch_credit;
+	samples=batch_samples;
+	batch_pos = batch_credit = batch_samples = 0;
+
+	/* Don't allow more credit BITS > pool WORDS */
+	if(credit > batch_max) credit=batch_max;
+	/* Check for pool wrap-around */
+	if(samples > batch_max) samples=batch_max;
+
+	add_entropy_words(r, batch_entropy_pool, samples);
+	credit_entropy_store(r, credit);
+
+	if (r->entropy_count >= random_read_wakeup_thresh)
 		wake_up_interruptible(&random_read_wait);
 }
 
@@ -766,7 +764,7 @@
 		delta>>=es->shift;
 		bits=benford[int_log2_16bits(delta & 0xffff)];
 	}
-	batch_entropy_store(datum, time, bits);
+	batch_entropy_store(datum^time, bits);
 }
 
 /******************************************************************
diff -ur a/include/linux/random.h b/include/linux/random.h
--- a/include/linux/random.h	2002-08-17 19:55:31.000000000 -0500
+++ b/include/linux/random.h	2002-08-17 19:55:38.000000000 -0500
@@ -44,7 +44,7 @@
 
 extern void rand_initialize(void);
 
-extern void batch_entropy_store(u32 a, u32 b, int num);
+extern void batch_entropy_store(u32 val, int bits);
 extern void *create_entropy_source(int granularity_khz);
 extern void free_entropy_source(void *src);
 extern void add_timing_entropy(void *src, unsigned datum);

-- 
 "Love the dolphins," she advised him. "Write by W.A.S.T.E.." 

  reply	other threads:[~2002-08-18  2:28 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-18  2:15 [PATCH] (0/4) Entropy accounting fixes Oliver Xymoron
2002-08-18  2:23 ` [PATCH] (1/4) " Oliver Xymoron
2002-08-18  2:26   ` [PATCH] (2/4) Update input drivers Oliver Xymoron
2002-08-18  2:29     ` [PATCH] (3/4) SA_RANDOM user fixup Oliver Xymoron
2002-08-18  2:32       ` Oliver Xymoron [this message]
2002-08-18  2:30 ` [PATCH] (0/4) Entropy accounting fixes Linus Torvalds
2002-08-18  2:59   ` Oliver Xymoron
2002-08-18  3:08     ` Linus Torvalds
2002-08-18  3:25       ` Linus Torvalds
2002-08-18  4:42         ` Oliver Xymoron
2002-08-18  4:53           ` Linus Torvalds
2002-08-18  5:05             ` Dmitri
2002-08-18  6:18               ` Oliver Xymoron
2002-08-22  3:33             ` David Wagner
2002-08-18 10:30         ` Alan Cox
2002-08-18 15:08           ` Oliver Xymoron
2002-08-18 17:31           ` Jonathan Lundell
2002-08-22  3:27         ` David Wagner
2002-08-18  4:30       ` Oliver Xymoron
2002-08-21  8:44       ` Rogier Wolff
2002-08-21 12:47         ` Oliver Xymoron
2002-08-18  5:28     ` Andreas Dilger
2002-08-18  5:53       ` Oliver Xymoron
2002-08-22  3:25   ` David Wagner
2002-08-18  3:05 ` Linus Torvalds
2002-08-18  3:51   ` Robert Love
2002-08-18  4:01     ` Linus Torvalds
2002-08-18  5:38       ` Oliver Xymoron
2002-08-19  4:21         ` Theodore Ts'o
2002-08-19 10:15           ` Marco Colombo
2002-08-19 10:25             ` Oliver Neukum
2002-08-19 11:03               ` Marco Colombo
2002-08-19 14:22                 ` Oliver Neukum
2002-08-19 15:21                   ` Marco Colombo
2002-08-19 16:29                     ` Oliver Neukum
2002-08-19 12:39           ` Oliver Xymoron
2002-08-18  6:31       ` Robert Love
2002-08-18  6:48         ` Oliver Xymoron
2002-08-18  4:06     ` dean gaudet
2002-08-18  4:44       ` Oliver Xymoron
2002-08-18  7:31       ` Bernd Eckenfels
2002-08-18  9:48         ` Ralf Baechle
2002-08-20 12:51           ` Bernd Eckenfels
2002-08-18 16:58         ` Robert Love
2002-08-18 10:25       ` Alan Cox
2002-08-19 10:47         ` Marco Colombo
2002-08-19 12:29           ` Alan Cox
2002-08-19 12:56             ` Marco Colombo
2002-09-08  3:43             ` D. Hugh Redelmeier
2002-09-08 18:03               ` David Wagner
2002-09-09 16:53                 ` Oliver Xymoron
2002-09-09 16:58                   ` David Wagner
2002-09-09 19:47                     ` Oliver Xymoron
2002-09-09 23:22                       ` David Wagner
2002-09-16 22:51                       ` dean gaudet
2002-09-17  1:18                         ` Oliver Xymoron
2002-09-09 18:54                   ` Kent Borg
2002-09-09 19:57                     ` Oliver Xymoron
2002-09-09 20:11                       ` Kent Borg
2002-08-18  4:57     ` Oliver Xymoron
2002-08-18  4:28   ` Oliver Xymoron
2002-08-18  4:51     ` Linus Torvalds
2002-08-18  5:24       ` Oliver Xymoron
2002-08-18 16:59         ` Linus Torvalds
2001-11-02 10:34           ` Pavel Machek
2002-08-23 20:16             ` Linus Torvalds
2002-08-18 17:03           ` Robert Love
2002-08-18 17:31           ` Oliver Xymoron
2002-08-18 16:54     ` Robert Love
2002-08-18 17:18       ` Oliver Xymoron
2002-08-18 17:20         ` Robert Love
2002-08-19  5:43 ` Theodore Ts'o
2001-11-02 10:05   ` Pavel Machek
2002-08-19  6:06   ` *Challenge* Finding a solution (When kernel boots it does not display any system info) louie miranda
2002-08-19  7:30     ` Gilad Ben-Yossef
2002-08-19  7:30     ` Ryan Cumming
2002-08-20  0:55       ` louie miranda
2002-08-19 13:52   ` [PATCH] (0/4) Entropy accounting fixes Oliver Xymoron
2002-08-20  8:59     ` Tommi Kyntola
2002-08-20 13:21       ` Oliver Xymoron
2002-08-20 16:19         ` Tommi Kyntola
2002-08-20 17:22           ` Oliver Xymoron
2002-09-08  3:51             ` D. Hugh Redelmeier
2002-09-08  4:31               ` Oliver Xymoron

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=20020818023207.GE21643@waste.org \
    --to=oxymoron@waste.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@transmeta.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.