wrapper: add a helper to generate numbers from a CSPRNG

There are many situations in which having access to a cryptographically secure pseudorandom number generator (CSPRNG) is helpful. In the future, we'll encounter one of these when dealing with temporary files. To make this possible, let's add a function which reads from a system CSPRNG and returns some bytes. We know that all systems will have such an interface. A CSPRNG is required for a secure TLS or SSH implementation and a Git implementation which provided neither would be of little practical use. In addition, POSIX is set to standardize getentropy(2) in the next version, so in the (potentially distant) future we can rely on that. For systems which lack one of the other interfaces, we provide the ability to use OpenSSL's CSPRNG. OpenSSL is highly portable and functions on practically every known OS, and we know it will have access to some source of cryptographically secure randomness. We also provide support for the arc4random in libbsd for folks who would prefer to use that. Because this is a security sensitive interface, we take some precautions. We either succeed by filling the buffer completely as we requested, or we fail. We don't return partial data because the caller will almost never find that to be a useful behavior. Specify a makefile knob which users can use to specify one or more suitable CSPRNGs, and turn the multiple string options into a set of defines, since we cannot match on strings in the preprocessor. We allow multiple options to make the job of handling this in autoconf easier. The order of options is important here. On systems with arc4random, which is most of the BSDs, we use that, since, except on MirBSD and macOS, it uses ChaCha20, which is extremely fast, and sits entirely in userspace, avoiding a system call. We then prefer getrandom over getentropy, because the former has been available longer on Linux, and then OpenSSL. Finally, if none of those are available, we use /dev/urandom, because most Unix-like operating systems provide that API. We prefer options that don't involve device files when possible because those work in some restricted environments where device files may not be available. Set the configuration variables appropriately for Linux and the BSDs, including macOS, as well as Windows and NonStop. We specifically only consider versions which receive publicly available security support here. For the same reason, we don't specify getrandom(2) on Linux, because CentOS 7 doesn't support it in glibc (although its kernel does) and we don't want to resort to making syscalls. Finally, add a test helper to allow this to be tested by hand and in tests. We don't add any tests, since invoking the CSPRNG is not likely to produce interesting, reproducible results. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Jan 17, 2022 at 21:56 UTC 05cd988dce58ba4cd46d4b3e79deacd86b34dc52
9 files changed +165 -1
Makefile
+34
@@ -234,6 +234,14 @@ all::
234 # Define NO_TRUSTABLE_FILEMODE if your filesystem may claim to support
235 # the executable mode bit, but doesn't really do so.
236 #
237 +# Define CSPRNG_METHOD to "arc4random" if your system has arc4random and
238 +# arc4random_buf, "libbsd" if your system has those functions from libbsd,
239 +# "getrandom" if your system has getrandom, "getentropy" if your system has
240 +# getentropy, "rtlgenrandom" for RtlGenRandom (Windows only), or "openssl" if
241 +# you'd want to use the OpenSSL CSPRNG. You may set multiple options with
242 +# spaces, in which case a suitable option will be chosen. If unset or set to
243 +# anything else, defaults to using "/dev/urandom".
244 +#
245 # Define NEEDS_MODE_TRANSLATION if your OS strays from the typical file type
246 # bits in mode values (e.g. z/OS defines I_SFMT to 0xFF000000 as opposed to the
247 # usual 0xF000).
@@ -693,6 +701,7 @@ TEST_BUILTINS_OBJS += test-bloom.o
701 TEST_BUILTINS_OBJS += test-chmtime.o
702 TEST_BUILTINS_OBJS += test-config.o
703 TEST_BUILTINS_OBJS += test-crontab.o
704 +TEST_BUILTINS_OBJS += test-csprng.o
705 TEST_BUILTINS_OBJS += test-ctype.o
706 TEST_BUILTINS_OBJS += test-date.o
707 TEST_BUILTINS_OBJS += test-delta.o
@@ -1908,6 +1917,31 @@ ifdef HAVE_GETDELIM
1917 BASIC_CFLAGS += -DHAVE_GETDELIM
1918 endif
1919
1920 +ifneq ($(findstring arc4random,$(CSPRNG_METHOD)),)
1921 + BASIC_CFLAGS += -DHAVE_ARC4RANDOM
1922 +endif
1923 +
1924 +ifneq ($(findstring libbsd,$(CSPRNG_METHOD)),)
1925 + BASIC_CFLAGS += -DHAVE_ARC4RANDOM_LIBBSD
1926 + EXTLIBS += -lbsd
1927 +endif
1928 +
1929 +ifneq ($(findstring getrandom,$(CSPRNG_METHOD)),)
1930 + BASIC_CFLAGS += -DHAVE_GETRANDOM
1931 +endif
1932 +
1933 +ifneq ($(findstring getentropy,$(CSPRNG_METHOD)),)
1934 + BASIC_CFLAGS += -DHAVE_GETENTROPY
1935 +endif
1936 +
1937 +ifneq ($(findstring rtlgenrandom,$(CSPRNG_METHOD)),)
1938 + BASIC_CFLAGS += -DHAVE_RTLGENRANDOM
1939 +endif
1940 +
1941 +ifneq ($(findstring openssl,$(CSPRNG_METHOD)),)
1942 + BASIC_CFLAGS += -DHAVE_OPENSSL_CSPRNG
1943 +endif
1944 +
1945 ifneq ($(PROCFS_EXECUTABLE_PATH),)
1946 procfs_executable_path_SQ = $(subst ','\'',$(PROCFS_EXECUTABLE_PATH))
1947 BASIC_CFLAGS += '-DPROCFS_EXECUTABLE_PATH="$(procfs_executable_path_SQ)"'
compat/winansi.c
+6
@@ -3,6 +3,12 @@
3 */
4
5 #undef NOGDI
6 +
7 +/*
8 + * Including the appropriate header file for RtlGenRandom causes MSVC to see a
9 + * redefinition of types in an incompatible way when including headers below.
10 + */
11 +#undef HAVE_RTLGENRANDOM
12 #include "../git-compat-util.h"
13 #include <wingdi.h>
14 #include <winreg.h>
config.mak.uname
+8
@@ -141,6 +141,7 @@ ifeq ($(uname_S),Darwin)
141 HAVE_BSD_SYSCTL = YesPlease
142 FREAD_READS_DIRECTORIES = UnfortunatelyYes
143 HAVE_NS_GET_EXECUTABLE_PATH = YesPlease
144 + CSPRNG_METHOD = arc4random
145
146 # Workaround for `gettext` being keg-only and not even being linked via
147 # `brew link --force gettext`, should be obsolete as of
@@ -256,6 +257,7 @@ ifeq ($(uname_S),FreeBSD)
257 HAVE_PATHS_H = YesPlease
258 HAVE_BSD_SYSCTL = YesPlease
259 HAVE_BSD_KERN_PROC_SYSCTL = YesPlease
260 + CSPRNG_METHOD = arc4random
261 PAGER_ENV = LESS=FRX LV=-c MORE=FRX
262 FREAD_READS_DIRECTORIES = UnfortunatelyYes
263 FILENO_IS_A_MACRO = UnfortunatelyYes
@@ -274,6 +276,7 @@ ifeq ($(uname_S),OpenBSD)
276 HAVE_PATHS_H = YesPlease
277 HAVE_BSD_SYSCTL = YesPlease
278 HAVE_BSD_KERN_PROC_SYSCTL = YesPlease
279 + CSPRNG_METHOD = arc4random
280 PROCFS_EXECUTABLE_PATH = /proc/curproc/file
281 FREAD_READS_DIRECTORIES = UnfortunatelyYes
282 FILENO_IS_A_MACRO = UnfortunatelyYes
@@ -285,6 +288,7 @@ ifeq ($(uname_S),MirBSD)
288 NEEDS_LIBICONV = YesPlease
289 HAVE_PATHS_H = YesPlease
290 HAVE_BSD_SYSCTL = YesPlease
291 + CSPRNG_METHOD = arc4random
292 endif
293 ifeq ($(uname_S),NetBSD)
294 ifeq ($(shell expr "$(uname_R)" : '[01]\.'),2)
@@ -296,6 +300,7 @@ ifeq ($(uname_S),NetBSD)
300 HAVE_PATHS_H = YesPlease
301 HAVE_BSD_SYSCTL = YesPlease
302 HAVE_BSD_KERN_PROC_SYSCTL = YesPlease
303 + CSPRNG_METHOD = arc4random
304 PROCFS_EXECUTABLE_PATH = /proc/curproc/exe
305 endif
306 ifeq ($(uname_S),AIX)
@@ -425,6 +430,7 @@ ifeq ($(uname_S),Windows)
430 NO_STRTOUMAX = YesPlease
431 NO_MKDTEMP = YesPlease
432 NO_INTTYPES_H = YesPlease
433 + CSPRNG_METHOD = rtlgenrandom
434 # VS2015 with UCRT claims that snprintf and friends are C99 compliant,
435 # so we don't need this:
436 #
@@ -593,6 +599,7 @@ ifeq ($(uname_S),NONSTOP_KERNEL)
599 NO_MMAP = YesPlease
600 NO_POLL = YesPlease
601 NO_INTPTR_T = UnfortunatelyYes
602 + CSPRNG_METHOD = openssl
603 SANE_TOOL_PATH = /usr/coreutils/bin:/usr/local/bin
604 SHELL_PATH = /usr/coreutils/bin/bash
605 endif
@@ -628,6 +635,7 @@ ifeq ($(uname_S),MINGW)
635 NO_POSIX_GOODIES = UnfortunatelyYes
636 DEFAULT_HELP_FORMAT = html
637 HAVE_PLATFORM_PROCINFO = YesPlease
638 + CSPRNG_METHOD = rtlgenrandom
639 BASIC_LDFLAGS += -municode
640 COMPAT_CFLAGS += -DNOGDI -Icompat -Icompat/win32
641 COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
contrib/buildsystems/CMakeLists.txt
+1 -1
@@ -260,7 +260,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
260 _CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe" NO_SYMLINK_HEAD UNRELIABLE_FSTAT
261 NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
262 USE_NED_ALLOCATOR OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
263 - UNICODE _UNICODE HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET)
263 + UNICODE _UNICODE HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET HAVE_RTLGENRANDOM)
264 list(APPEND compat_SOURCES compat/mingw.c compat/winansi.c compat/win32/path-utils.c
265 compat/win32/pthread.c compat/win32mmap.c compat/win32/syslog.c
266 compat/win32/trace2_win32_process_info.c compat/win32/dirent.c
git-compat-util.h
+19
@@ -188,6 +188,12 @@
188 #endif
189 #include <windows.h>
190 #define GIT_WINDOWS_NATIVE
191 +#ifdef HAVE_RTLGENRANDOM
192 +/* This is required to get access to RtlGenRandom. */
193 +#define SystemFunction036 NTAPI SystemFunction036
194 +#include <NTSecAPI.h>
195 +#undef SystemFunction036
196 +#endif
197 #endif
198
199 #include <unistd.h>
@@ -258,6 +264,12 @@
264 #else
265 #include <stdint.h>
266 #endif
267 +#ifdef HAVE_ARC4RANDOM_LIBBSD
268 +#include <bsd/stdlib.h>
269 +#endif
270 +#ifdef HAVE_GETRANDOM
271 +#include <sys/random.h>
272 +#endif
273 #ifdef NO_INTPTR_T
274 /*
275 * On I16LP32, ILP32 and LP64 "long" is the safe bet, however
@@ -1421,4 +1433,11 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
1433
1434 void sleep_millisec(int millisec);
1435
1436 +/*
1437 + * Generate len bytes from the system cryptographically secure PRNG.
1438 + * Returns 0 on success and -1 on error, setting errno. The inability to
1439 + * satisfy the full request is an error.
1440 + */
1441 +int csprng_bytes(void *buf, size_t len);
1442 +
1443 #endif
t/helper/test-csprng.c new
+29
@@ -0,0 +1,29 @@
1 +#include "test-tool.h"
2 +#include "git-compat-util.h"
3 +
4 +
5 +int cmd__csprng(int argc, const char **argv)
6 +{
7 + unsigned long count;
8 + unsigned char buf[1024];
9 +
10 + if (argc > 2) {
11 + fprintf(stderr, "usage: %s [<size>]\n", argv[0]);
12 + return 2;
13 + }
14 +
15 + count = (argc == 2) ? strtoul(argv[1], NULL, 0) : -1L;
16 +
17 + while (count) {
18 + unsigned long chunk = count < sizeof(buf) ? count : sizeof(buf);
19 + if (csprng_bytes(buf, chunk) < 0) {
20 + perror("failed to read");
21 + return 5;
22 + }
23 + if (fwrite(buf, chunk, 1, stdout) != chunk)
24 + return 1;
25 + count -= chunk;
26 + }
27 +
28 + return 0;
29 +}
t/helper/test-tool.c
+1
@@ -20,6 +20,7 @@ static struct test_cmd cmds[] = {
20 { "chmtime", cmd__chmtime },
21 { "config", cmd__config },
22 { "crontab", cmd__crontab },
23 + { "csprng", cmd__csprng },
24 { "ctype", cmd__ctype },
25 { "date", cmd__date },
26 { "delta", cmd__delta },
t/helper/test-tool.h
+1
@@ -10,6 +10,7 @@ int cmd__bloom(int argc, const char **argv);
10 int cmd__chmtime(int argc, const char **argv);
11 int cmd__config(int argc, const char **argv);
12 int cmd__crontab(int argc, const char **argv);
13 +int cmd__csprng(int argc, const char **argv);
14 int cmd__ctype(int argc, const char **argv);
15 int cmd__date(int argc, const char **argv);
16 int cmd__delta(int argc, const char **argv);
wrapper.c
+66
@@ -702,3 +702,69 @@ int open_nofollow(const char *path, int flags)
702 return open(path, flags);
703 #endif
704 }
705 +
706 +int csprng_bytes(void *buf, size_t len)
707 +{
708 +#if defined(HAVE_ARC4RANDOM) || defined(HAVE_ARC4RANDOM_LIBBSD)
709 + /* This function never returns an error. */
710 + arc4random_buf(buf, len);
711 + return 0;
712 +#elif defined(HAVE_GETRANDOM)
713 + ssize_t res;
714 + char *p = buf;
715 + while (len) {
716 + res = getrandom(p, len, 0);
717 + if (res < 0)
718 + return -1;
719 + len -= res;
720 + p += res;
721 + }
722 + return 0;
723 +#elif defined(HAVE_GETENTROPY)
724 + int res;
725 + char *p = buf;
726 + while (len) {
727 + /* getentropy has a maximum size of 256 bytes. */
728 + size_t chunk = len < 256 ? len : 256;
729 + res = getentropy(p, chunk);
730 + if (res < 0)
731 + return -1;
732 + len -= chunk;
733 + p += chunk;
734 + }
735 + return 0;
736 +#elif defined(HAVE_RTLGENRANDOM)
737 + if (!RtlGenRandom(buf, len))
738 + return -1;
739 + return 0;
740 +#elif defined(HAVE_OPENSSL_CSPRNG)
741 + int res = RAND_bytes(buf, len);
742 + if (res == 1)
743 + return 0;
744 + if (res == -1)
745 + errno = ENOTSUP;
746 + else
747 + errno = EIO;
748 + return -1;
749 +#else
750 + ssize_t res;
751 + char *p = buf;
752 + int fd, err;
753 + fd = open("/dev/urandom", O_RDONLY);
754 + if (fd < 0)
755 + return -1;
756 + while (len) {
757 + res = xread(fd, p, len);
758 + if (res < 0) {
759 + err = errno;
760 + close(fd);
761 + errno = err;
762 + return -1;
763 + }
764 + len -= res;
765 + p += res;
766 + }
767 + close(fd);
768 + return 0;
769 +#endif
770 +}