Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220409225851.715796-2-Jason@zx2c4.com>
Date: Sun, 10 Apr 2022 00:58:50 +0200
From: "Jason A. Donenfeld" <Jason@...c4.com>
To: Rich Felker <dalias@...c.org>,
	musl@...ts.openwall.com
Cc: "Jason A. Donenfeld" <Jason@...c4.com>
Subject: [PATCH v3] getentropy: fail if buffer not completely filled

The man page for getentropy says that it either completely succeeds or
completely fails for values < 256, so we can simplify this scenario by
omitting the loop. As a safeguard, we still return EIO if it returns
short, but otherwise we pass the error on through to the caller.
---
Changes v2->v3:
- v3 gets rid of the loop entirely.

 src/misc/getentropy.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)

diff --git a/src/misc/getentropy.c b/src/misc/getentropy.c
index 651ea95f..e8cb4d02 100644
--- a/src/misc/getentropy.c
+++ b/src/misc/getentropy.c
@@ -6,8 +6,8 @@
 
 int getentropy(void *buffer, size_t len)
 {
-	int cs, ret = 0;
-	char *pos = buffer;
+	ssize_t ret;
+	int cs;
 
 	if (len > 256) {
 		errno = EIO;
@@ -15,19 +15,13 @@ int getentropy(void *buffer, size_t len)
 	}
 
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
-
-	while (len) {
-		ret = getrandom(pos, len, 0);
-		if (ret < 0) {
-			if (errno == EINTR) continue;
-			else break;
-		}
-		pos += ret;
-		len -= ret;
-		ret = 0;
-	}
-
+	ret = getrandom(buffer, len, 0);
 	pthread_setcancelstate(cs, 0);
 
-	return ret;
+	if (ret != len) {
+		if (ret >= 0)
+			errno = EIO;
+		return -1;
+	}
+	return 0;
 }
-- 
2.35.1

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.