mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] getentropy: fail if buffer not completely filled
@ 2022-04-09  0:10 Jason A. Donenfeld
  2022-04-09 13:18 ` Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Jason A. Donenfeld @ 2022-04-09  0:10 UTC (permalink / raw)
  To: musl; +Cc: Jason A. Donenfeld

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. While we're at it, prevent an unexpected infinite
loop if the function returns 0, the same way glibc does, just in case.
---
 src/misc/getentropy.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/misc/getentropy.c b/src/misc/getentropy.c
index 651ea95f..5b2fc7a1 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) {
@@ -18,16 +18,19 @@ int getentropy(void *buffer, size_t len)
 
 	while (len) {
 		ret = getrandom(pos, len, 0);
-		if (ret < 0) {
+		if (ret <= 0) {
 			if (errno == EINTR) continue;
 			else break;
 		}
 		pos += ret;
 		len -= ret;
-		ret = 0;
 	}
 
 	pthread_setcancelstate(cs, 0);
 
-	return ret;
+	if (len) {
+		errno = EIO;
+		return -1;
+	}
+	return 0;
 }
-- 
2.35.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-04-10 15:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-09  0:10 [musl] [PATCH] getentropy: fail if buffer not completely filled Jason A. Donenfeld
2022-04-09 13:18 ` Rich Felker
2022-04-09 22:50   ` Jason A. Donenfeld
2022-04-09 22:58     ` [musl] [PATCH v2] " Jason A. Donenfeld
2022-04-09 22:58       ` [musl] [PATCH v3] " Jason A. Donenfeld
2022-04-10 15:30       ` [musl] [PATCH v2] " Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).