Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F132575325
D16807.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
D16807.diff
View Options
Index: head/lib/libc/gen/getentropy.c
===================================================================
--- head/lib/libc/gen/getentropy.c
+++ head/lib/libc/gen/getentropy.c
@@ -34,10 +34,14 @@
#include <sys/sysctl.h>
#include <errno.h>
+#include <stdbool.h>
#include <stdlib.h>
#include "libc_private.h"
+/* First __FreeBSD_version bump after introduction of getrandom(2) (r331279) */
+#define GETRANDOM_FIRST 1200061
+
extern int __sysctl(int *, u_int, void *, size_t *, void *, size_t);
static size_t
@@ -99,21 +103,38 @@
getentropy(void *buf, size_t buflen)
{
ssize_t rd;
+ bool have_getrandom;
if (buflen > 256) {
errno = EIO;
return (-1);
}
+ have_getrandom = (__getosreldate() >= GETRANDOM_FIRST);
+
while (buflen > 0) {
- rd = getrandom(buf, buflen, 0);
- if (rd == -1) {
- if (errno == EINTR)
- continue;
- else if (errno == ENOSYS || errno == ECAPMODE)
- return (getentropy_fallback(buf, buflen));
- else
- return (-1);
+ if (have_getrandom) {
+ rd = getrandom(buf, buflen, 0);
+ if (rd == -1) {
+ switch (errno) {
+ case ECAPMODE:
+ /*
+ * Kernel >= r331280 and < r337999
+ * will return ECAPMODE when the
+ * caller is already in capability
+ * mode, fallback to traditional
+ * method in this case.
+ */
+ have_getrandom = false;
+ continue;
+ case EINTR:
+ continue;
+ default:
+ return (-1);
+ }
+ }
+ } else {
+ return (getentropy_fallback(buf, buflen));
}
/* This cannot happen. */
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Oct 19, 2:43 AM (6 h, 35 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
23906517
Default Alt Text
D16807.diff (1 KB)
Attached To
Mode
D16807: Use osreldate to check the existence of getrandom(2).
Attached
Detach File
Event Timeline
Log In to Comment