Skip to content

Commit

Permalink
libc: Fix getentropy POSIX 2024 conformance issues
Browse files Browse the repository at this point in the history
GETENTROPY_MAX should be defined in limits.h.  EINVAL is the return
value for buflen > GETENTROPY_MAX.

PR:		282783
Reviewed by:	markj, asomers, jhb
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D47689
  • Loading branch information
emaste committed Jan 17, 2025
1 parent 98bebc2 commit 473681a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
4 changes: 4 additions & 0 deletions include/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@

#define MB_LEN_MAX 6 /* 31-bit UTF-8 */

#if __POSIX_VISIBLE >= 202405
#define GETENTROPY_MAX 256
#endif

#include <sys/limits.h>

#if __POSIX_VISIBLE
Expand Down
14 changes: 4 additions & 10 deletions lib/libc/gen/getentropy.3
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd November 20, 2024
.Dd January 17, 2025
.Dt GETENTROPY 3
.Os
.Sh NAME
Expand Down Expand Up @@ -60,16 +60,16 @@ The
.Fa buf
parameter points to an
invalid address.
.It Bq Er EIO
Too many bytes requested, or some other fatal error occurred.
.It Bq Er EINVAL
Too many bytes requested.
.El
.Sh SEE ALSO
.Xr getrandom 2 ,
.Xr arc4random 3 ,
.Xr random 4
.Sh STANDARDS
.Fn getentropy
nearly conforms to
conforms to
.St -p1003.1-2024 .
.Sh HISTORY
The
Expand All @@ -80,9 +80,3 @@ The
.Fx
libc compatibility shim first appeared in
.Fx 12.0 .
.Sh BUGS
.In limits.h
does not define
.Dv GETENTROPY_MAX .
Some error values do not match
.St -p1003.1-2024 .
5 changes: 3 additions & 2 deletions lib/libc/gen/getentropy.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <sys/random.h>

#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <unistd.h>
#include <ssp/ssp.h>
Expand All @@ -47,8 +48,8 @@ __ssp_real(getentropy)(void *buf, size_t buflen)
{
ssize_t rd;

if (buflen > 256) {
errno = EIO;
if (buflen > GETENTROPY_MAX) {
errno = EINVAL;
return (-1);
}

Expand Down

0 comments on commit 473681a

Please sign in to comment.