Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sun, 10 Apr 2022 00:58:49 +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 v2] getentropy: fail if buffer not completely filled

The man page for getentropy says that it either completely succeeds or
completely fails, and indeed this is what glibc does. However, musl has
a condition where it breaks out of the loop early, yet still returns a
success. This patch fixes that by returning a success only if the buffer
is completely filled.
---
Changes v2->v3:
- This gets rid of the ret==0 check like glibc uses.

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

diff --git a/src/misc/getentropy.c b/src/misc/getentropy.c
index 651ea95f..964b8c10 100644
--- a/src/misc/getentropy.c
+++ b/src/misc/getentropy.c
@@ -6,7 +6,7 @@
 
 int getentropy(void *buffer, size_t len)
 {
-	int cs, ret = 0;
+	int cs, ret;
 	char *pos = buffer;
 
 	if (len > 256) {
@@ -24,10 +24,13 @@ int getentropy(void *buffer, size_t len)
 		}
 		pos += ret;
 		len -= ret;
-		ret = 0;
 	}
 
 	pthread_setcancelstate(cs, 0);
 
-	return ret;
+	if (len) {
+		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.