libc6: fread(buf, 1, 1) from urandom consumes 4096 bytes of entropy

Jonathan Nieder jrnieder at gmail.com
Wed Aug 3 10:52:26 UTC 2011


Philipp Marek wrote:

> I think that on sockets, pipes, character devices, and similar there should 
> be _no_ readahead.

On the contrary, on sockets and pipes, readahead is very useful.
Luckily buffering there works a little closer to what you would expect
--- read() returns early with what is available and fgetc happily
makes use of the result without reading again.

So I agree with Petr on this one.  How about this patch?  It will also
help on every other unix supporting /dev/random I know of.

Signed-off-by: Jonathan Nieder <jrnieder at gmail.com>
---
 src/c/num_rand.d            |    1 +
 src/gmp/tests/rand/findlc.c |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)

diff --git i/src/c/num_rand.d w/src/c/num_rand.d
index 61caa315..1394af36 100644
--- i/src/c/num_rand.d
+++ w/src/c/num_rand.d
@@ -69,6 +69,7 @@ init_random_state()
 	int j;
 #if !defined(ECL_MS_WINDOWS_HOST)
 	FILE *fp = fopen("/dev/urandom","r");
 	if (fp) {
+		setbuf(fp, NULL);
 		fread(mt, sizeof(*mt), MT_N, fp);
 		for (j=0; j < MT_N; j++){
diff --git i/src/gmp/tests/rand/findlc.c w/src/gmp/tests/rand/findlc.c
index c432095d..63f2bdae 100644
--- i/src/gmp/tests/rand/findlc.c
+++ w/src/gmp/tests/rand/findlc.c
@@ -78,6 +78,7 @@ mpz_true_random (mpz_t s, unsigned long int nbits)
 	{
 	  int nread;
 	  fs = fopen ("/dev/random", "r");
+	  setbuf(fs, NULL);
 	  nread = fread (c, 1, 1, fs);
 	  fclose (fs);
 	  if (nread != 0)
-- 
1.7.6



More information about the pkg-common-lisp-devel mailing list