Blob Blame History Raw
From a7afdf46c3193eb102cc6ec2a3b61e8d36794437 Mon Sep 17 00:00:00 2001
From: Mike Christie <michaelc@cs.wisc.edu>
Date: Fri, 14 Dec 2012 12:40:27 -0600
Subject: iscsi tools: fix get_random_bytes error handling

Bug report from Rahul:

There seems to be a bug in function get_random_bytes(). I reported
this earlier as well but somehow it didn't appear here.

get_random_bytes(unsigned char *data, unsigned int length)
{
	long r;
        unsigned n;
	int fd;

	fd = open("/dev/urandom", O_RDONLY);
        	while (length > 0) {

	if (!fd || read(fd, &r, sizeof(long)) != -1)      <<<< the condition is
incorrect
---
 usr/auth.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/usr/auth.c b/usr/auth.c
index c924545..4ff0425 100644
--- a/usr/auth.c
+++ b/usr/auth.c
@@ -189,24 +189,24 @@ get_random_bytes(unsigned char *data, unsigned int length)
 
 	long r;
         unsigned n;
-	int fd;
+	int fd, r_size = sizeof(r);
 
 	fd = open("/dev/urandom", O_RDONLY);
         while (length > 0) {
 
-		if (!fd || read(fd, &r, sizeof(long)) != -1)
+		if (fd == -1 || read(fd, &r, r_size) != r_size)
 			r = rand();
                 r = r ^ (r >> 8);
                 r = r ^ (r >> 4);
                 n = r & 0x7;
 
-		if (!fd || read(fd, &r, sizeof(long)) != -1)
+		if (fd == -1 || read(fd, &r, r_size) != r_size)
 			r = rand();
                 r = r ^ (r >> 8);
                 r = r ^ (r >> 5);
                 n = (n << 3) | (r & 0x7);
 
-		if (!fd || read(fd, &r, sizeof(long)) != -1)
+		if (fd == -1 || read(fd, &r, r_size) != r_size)
 			r = rand();
                 r = r ^ (r >> 8);
                 r = r ^ (r >> 5);
-- 
1.7.11.7