From 4e75d7f504b61ef031d14e4283fc5842cf126357 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Fri, 16 Dec 2016 23:10:19 +0100 Subject: Export getentropy and getrandom calls getentropy per OpenBSD http://man.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2 getrandom per Linux http://man7.org/linux/man-pages/man2/getrandom.2.html Note that GRND_NONBLOCK is not handled --- winsup/cygwin/common.din | 2 ++ winsup/cygwin/include/cygwin/version.h | 7 +++-- winsup/cygwin/include/sys/random.h | 29 ++++++++++++++++++++ winsup/cygwin/miscfuncs.cc | 50 ++++++++++++++++++++++++++++++++-- winsup/cygwin/release/2.7.0 | 14 ++++++++++ 5 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 winsup/cygwin/include/sys/random.h create mode 100644 winsup/cygwin/release/2.7.0 diff --git a/winsup/cygwin/common.din b/winsup/cygwin/common.din index b0064c877..8c9af216b 100644 --- a/winsup/cygwin/common.din +++ b/winsup/cygwin/common.din @@ -604,6 +604,7 @@ getdelim = __getdelim SIGFE getdomainname SIGFE getdtablesize NOSIGFE getegid NOSIGFE +getentropy SIGFE getenv NOSIGFE geteuid NOSIGFE getgid NOSIGFE @@ -650,6 +651,7 @@ getpwnam SIGFE getpwnam_r SIGFE getpwuid SIGFE getpwuid_r SIGFE +getrandom SIGFE getrlimit SIGFE getrusage SIGFE gets SIGFE diff --git a/winsup/cygwin/include/cygwin/version.h b/winsup/cygwin/include/cygwin/version.h index 6ba602efb..0da3d370a 100644 --- a/winsup/cygwin/include/cygwin/version.h +++ b/winsup/cygwin/include/cygwin/version.h @@ -10,8 +10,8 @@ details. */ the Cygwin shared library". This version is used to track important changes to the DLL and is mainly informative in nature. */ -#define CYGWIN_VERSION_DLL_MAJOR 2006 -#define CYGWIN_VERSION_DLL_MINOR 1 +#define CYGWIN_VERSION_DLL_MAJOR 2007 +#define CYGWIN_VERSION_DLL_MINOR 0 /* Major numbers before CYGWIN_VERSION_DLL_EPOCH are incompatible. */ @@ -470,12 +470,13 @@ details. */ 303: Export pthread_getname_np, pthread_setname_np. 304: Export strerror_l, strptime_l, wcsftime_l. 305: [f]pathconf flag _PC_CASE_INSENSITIVE added. + 306: Export getentropy, getrandom. Note that we forgot to bump the api for ualarm, strtoll, strtoull, sigaltstack, sethostname. */ #define CYGWIN_VERSION_API_MAJOR 0 -#define CYGWIN_VERSION_API_MINOR 305 +#define CYGWIN_VERSION_API_MINOR 306 /* There is also a compatibity version number associated with the shared memory regions. It is incremented when incompatible changes are made to the shared diff --git a/winsup/cygwin/include/sys/random.h b/winsup/cygwin/include/sys/random.h new file mode 100644 index 000000000..5422718a5 --- /dev/null +++ b/winsup/cygwin/include/sys/random.h @@ -0,0 +1,29 @@ +/* sys/random.h header file for Cygwin. + +This file is part of Cygwin. + +This software is a copyrighted work licensed under the terms of the +Cygwin license. Please consult the file "CYGWIN_LICENSE" for details. */ + +#ifndef _SYS_RANDOM_H +#define _SYS_RANDOM_H + +#include <_ansi.h> +#include + +/* getrandom flags */ +#define GRND_NONBLOCK 1 +#define GRND_RANDOM 2 + +#ifdef __cplusplus +extern "C" { +#endif + +ssize_t getrandom (void *__ptr, size_t __len, unsigned int __flags); +int getentropy (void *__ptr, size_t __len); + +#ifdef __cplusplus +} +#endif + +#endif /* _SYS_RANDOM_H */ diff --git a/winsup/cygwin/miscfuncs.cc b/winsup/cygwin/miscfuncs.cc index e92ccbabb..044d72787 100644 --- a/winsup/cygwin/miscfuncs.cc +++ b/winsup/cygwin/miscfuncs.cc @@ -14,6 +14,7 @@ details. */ #include #include #include +#include #include #include "cygtls.h" #include "ntdll.h" @@ -234,19 +235,62 @@ check_iovec (const struct iovec *iov, int iovcnt, bool forwrite) return -1; } -/* Used by arc2random, fhandler_socket and fhandler_random. */ extern "C" int getentropy (void *ptr, size_t len) { - if (!RtlGenRandom (ptr, len)) + /* Per BSD man page: The maximum buffer size permitted is 256 bytes. + If buflen exceeds this, an error of EIO will be indicated. */ + if (len > 256) { - debug_printf ("%E = RtlGenRandom()"); + debug_printf ("len (%U) > 256", len); set_errno (EIO); return -1; } + __try + { + if (!RtlGenRandom (ptr, len)) + { + debug_printf ("RtlGenRandom() = FALSE"); + set_errno (EIO); + return -1; + } + } + __except (EFAULT) + { + return -1; + } + __endtry return 0; } +extern "C" ssize_t +getrandom (void *ptr, size_t len, unsigned int flags) +{ + if (flags & ~(GRND_NONBLOCK | GRND_RANDOM)) + { + debug_printf ("invalid flags: %y", flags); + set_errno (EINVAL); + return -1; + } + /* Max. bytes returned by Linux call. */ + len = MAX (len, (flags & GRND_RANDOM) ? 512 : 33554431); + __try + { + if (!RtlGenRandom (ptr, len)) + { + debug_printf ("RtlGenRandom() = FALSE"); + set_errno (EIO); + return -1; + } + } + __except (EFAULT) + { + return -1; + } + __endtry + return len; +} + /* Try hard to schedule another thread. Remember not to call this in a lock condition or you'll potentially suffer starvation. */ diff --git a/winsup/cygwin/release/2.7.0 b/winsup/cygwin/release/2.7.0 new file mode 100644 index 000000000..1d0acb2e0 --- /dev/null +++ b/winsup/cygwin/release/2.7.0 @@ -0,0 +1,14 @@ +What's new: +----------- + +- New API: getentropy, getrandom. + + +What changed: +------------- + + +Bug Fixes +--------- + + -- cgit v1.2.3