NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: lib/56905: getentropy() may return predictable data



The following reply was made to PR lib/56905; it has been noted by GNATS.

From: Andreas Gustafsson <gson%gson.org@localhost>
To: Christos Zoulas <christos%zoulas.com@localhost>
Cc: gnats-bugs%netbsd.org@localhost
Subject: Re: lib/56905: getentropy() may return predictable data
Date: Thu, 30 Jun 2022 15:31:15 +0300

 Christos Zoulas wrote:
 > But it is also supposed to return 0 on success, not the number of bytes.
 
 Good catch, thanks.  Revised patch:
 
 Index: getentropy.c
 ===================================================================
 RCS file: /cvsroot/src/lib/libc/gen/getentropy.c,v
 retrieving revision 1.3
 diff -u -r1.3 getentropy.c
 --- getentropy.c	31 May 2022 13:42:59 -0000	1.3
 +++ getentropy.c	30 Jun 2022 12:32:06 -0000
 @@ -35,8 +35,9 @@
  #include "namespace.h"
  
  #include <sys/param.h>
 -#include <sys/sysctl.h>
 +#include <sys/random.h>
  
 +#include <assert.h>
  #include <errno.h>
  #include <limits.h>
  #include <unistd.h>
 @@ -50,8 +51,7 @@
  int
  getentropy(void *buf, size_t buflen)
  {
 -	size_t len = buflen;
 -	int name[2] = { CTL_KERN, KERN_ARND };
 +	ssize_t r;
  
  	if (buf == NULL && buflen > 0) {
  		errno = EFAULT;
 @@ -63,5 +63,14 @@
  		return -1;
  	}
  
 -	return sysctl(name, 2, buf, &len, NULL, 0);
 +	do {
 +		r = getrandom(buf, buflen, 0);
 +	} while (r == -1 && errno == EINTR);
 +
 +	if (r == -1)
 +		return r;
 +
 +	assert(r == (ssize_t)buflen);
 +
 +	return 0;
  }
 


Home | Main Index | Thread Index | Old Index