From 50e096bc9d642b4b0f55a4582ec7f57559056a53 Mon Sep 17 00:00:00 2001 From: Tom Yu Date: Fri, 31 Aug 2007 21:38:28 +0000 Subject: [PATCH] pull up r19845 from trunk r19845@cathode-dark-space: epeisach | 2007-08-20 10:50:41 -0400 ticket: new subject: read_entropy_from_device on partial read will not fill buffer tags: pullup read_entropy_from_device() will loop in read until the desired number of bytes are read from the device (/dev/random, /dev/urandom). I have observed that for /dev/random, if there is not enough bits available for reading - it will return a partial read. The code would loop in this case, but never advance the location to place the new bytes - hence the start of the buffer would be filled again - leaving the tail end as stack garbage. ticket: 5666 version_fixed: 1.6.3 git-svn-id: svn://anonsvn.mit.edu/krb5/branches/krb5-1-6@19906 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/crypto/prng.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/crypto/prng.c b/src/lib/crypto/prng.c index 54a68e067..32506d835 100644 --- a/src/lib/crypto/prng.c +++ b/src/lib/crypto/prng.c @@ -162,7 +162,7 @@ read_entropy_from_device (krb5_context context, const char *device) krb5_data data; struct stat sb; int fd; - unsigned char buf[YARROW_SLOW_THRESH/8]; + unsigned char buf[YARROW_SLOW_THRESH/8], *bp; int left; fd = open (device, O_RDONLY); if (fd == -1) @@ -173,14 +173,16 @@ read_entropy_from_device (krb5_context context, const char *device) close(fd); return 0; } - for (left = sizeof (buf); left > 0;) { + + for (bp = &buf, left = sizeof (buf); left > 0;) { ssize_t count; - count = read (fd, &buf, (unsigned) left); + count = read (fd, bp, (unsigned) left); if (count <= 0) { close(fd); return 0; } left -= count; + bp += count; } close (fd); data.length = sizeof (buf); @@ -199,7 +201,7 @@ krb5_c_random_os_entropy (krb5_context context, int unused; int *oursuccess = success?success:&unused; *oursuccess = 0; - /* If we are getting strong data then try that first. We aare + /* If we are getting strong data then try that first. We are guaranteed to cause a reseed of some kind if strong is true and we have both /dev/random and /dev/urandom. We want the strong data included in the reseed so we get it first.*/ -- 2.26.2