Add getrandom support for the BSDs
Closed, ResolvedPublic

Description

FreeBSD 12 has a getrandom syscall; see D471 for discussion

Related Objects

werner created this task.Dec 13 2018, 4:28 PM
gniibe added a subscriber: gniibe.Dec 17 2018, 5:20 AM

In FreeBSD, getrandom(3) became available, when getrandom(2) was added. <-- This is my theory.
If this is true, just use getrandom(3), not using getrandom(2) by syscall.

Besides, glibc (>= 2.25) have getrandom(3) now (reference: https://old.lwn.net/Articles/711013/).

OpenBSD uses getentropy(2). glibc (>= 2.25) has getentropy(3), too.

I think we should stick with the syscall for Linux.

Confirmed my theory of getentropy(3): https://reviews.freebsd.org/rS331279

Since our usage of getrandom is with flag 0, which is same as getentropy, I think that it's good to add HAVE_GETENTROPY feature macro to support getentropy for *BSD (both of FreeBSD and OpenBSD in mind).

My point is: Let us support OpenBSD too.

This is mine:

Update to prefer syscall on GNU/Linux (no need to audit libc implementation):

gniibe added a comment.Tue, Jan 8, 1:16 AM

In my patch, for OpenBSD and FreeBSD (well, other than GNU/Linux), it uses getentropy if available. For GNU/Linux, we use the local macro of getentropy (regardless of the availability of the function), keeping exactly same behavior of syscall with __NR_getrandom.

Sorry for long reply, your change looks ok even though dunno it is meaningful those _gcry_pre_syscall ()/_gcry_post_syscall () surrounding get entropy for example.

These are hooks so that co-operative thread libraries (like ntph) are able to yield control to the system's thread's implementation.

All right then, fine by me.

gniibe closed this task as Resolved.Tue, Jan 15, 7:54 AM
gniibe claimed this task.

Pushed to master, fixing about return value of getentropy. Tested on FreeBSD 12. Tested on FreeBSD 11 where getentropy is not available.