Skip to content

Commit 05cd988

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dcc0cd0 commit 05cd988

File tree

9 files changed

+165
-1
lines changed

9 files changed

+165
-1
lines changed

Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ all::
234234
# Define NO_TRUSTABLE_FILEMODE if your filesystem may claim to support
235235
# the executable mode bit, but doesn't really do so.
236236
#
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+
#
237245
# Define NEEDS_MODE_TRANSLATION if your OS strays from the typical file type
238246
# bits in mode values (e.g. z/OS defines I_SFMT to 0xFF000000 as opposed to the
239247
# usual 0xF000).
@@ -693,6 +701,7 @@ TEST_BUILTINS_OBJS += test-bloom.o
693701
TEST_BUILTINS_OBJS += test-chmtime.o
694702
TEST_BUILTINS_OBJS += test-config.o
695703
TEST_BUILTINS_OBJS += test-crontab.o
704+
TEST_BUILTINS_OBJS += test-csprng.o
696705
TEST_BUILTINS_OBJS += test-ctype.o
697706
TEST_BUILTINS_OBJS += test-date.o
698707
TEST_BUILTINS_OBJS += test-delta.o
@@ -1908,6 +1917,31 @@ ifdef HAVE_GETDELIM
19081917
BASIC_CFLAGS += -DHAVE_GETDELIM
19091918
endif
19101919

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+
19111945
ifneq ($(PROCFS_EXECUTABLE_PATH),)
19121946
procfs_executable_path_SQ = $(subst ','\'',$(PROCFS_EXECUTABLE_PATH))
19131947
BASIC_CFLAGS += '-DPROCFS_EXECUTABLE_PATH="$(procfs_executable_path_SQ)"'

compat/winansi.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
*/
44

55
#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
612
#include "../git-compat-util.h"
713
#include <wingdi.h>
814
#include <winreg.h>

config.mak.uname

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ ifeq ($(uname_S),Darwin)
141141
HAVE_BSD_SYSCTL = YesPlease
142142
FREAD_READS_DIRECTORIES = UnfortunatelyYes
143143
HAVE_NS_GET_EXECUTABLE_PATH = YesPlease
144+
CSPRNG_METHOD = arc4random
144145

145146
# Workaround for `gettext` being keg-only and not even being linked via
146147
# `brew link --force gettext`, should be obsolete as of
@@ -256,6 +257,7 @@ ifeq ($(uname_S),FreeBSD)
256257
HAVE_PATHS_H = YesPlease
257258
HAVE_BSD_SYSCTL = YesPlease
258259
HAVE_BSD_KERN_PROC_SYSCTL = YesPlease
260+
CSPRNG_METHOD = arc4random
259261
PAGER_ENV = LESS=FRX LV=-c MORE=FRX
260262
FREAD_READS_DIRECTORIES = UnfortunatelyYes
261263
FILENO_IS_A_MACRO = UnfortunatelyYes
@@ -274,6 +276,7 @@ ifeq ($(uname_S),OpenBSD)
274276
HAVE_PATHS_H = YesPlease
275277
HAVE_BSD_SYSCTL = YesPlease
276278
HAVE_BSD_KERN_PROC_SYSCTL = YesPlease
279+
CSPRNG_METHOD = arc4random
277280
PROCFS_EXECUTABLE_PATH = /proc/curproc/file
278281
FREAD_READS_DIRECTORIES = UnfortunatelyYes
279282
FILENO_IS_A_MACRO = UnfortunatelyYes
@@ -285,6 +288,7 @@ ifeq ($(uname_S),MirBSD)
285288
NEEDS_LIBICONV = YesPlease
286289
HAVE_PATHS_H = YesPlease
287290
HAVE_BSD_SYSCTL = YesPlease
291+
CSPRNG_METHOD = arc4random
288292
endif
289293
ifeq ($(uname_S),NetBSD)
290294
ifeq ($(shell expr "$(uname_R)" : '[01]\.'),2)
@@ -296,6 +300,7 @@ ifeq ($(uname_S),NetBSD)
296300
HAVE_PATHS_H = YesPlease
297301
HAVE_BSD_SYSCTL = YesPlease
298302
HAVE_BSD_KERN_PROC_SYSCTL = YesPlease
303+
CSPRNG_METHOD = arc4random
299304
PROCFS_EXECUTABLE_PATH = /proc/curproc/exe
300305
endif
301306
ifeq ($(uname_S),AIX)
@@ -425,6 +430,7 @@ ifeq ($(uname_S),Windows)
425430
NO_STRTOUMAX = YesPlease
426431
NO_MKDTEMP = YesPlease
427432
NO_INTTYPES_H = YesPlease
433+
CSPRNG_METHOD = rtlgenrandom
428434
# VS2015 with UCRT claims that snprintf and friends are C99 compliant,
429435
# so we don't need this:
430436
#
@@ -593,6 +599,7 @@ ifeq ($(uname_S),NONSTOP_KERNEL)
593599
NO_MMAP = YesPlease
594600
NO_POLL = YesPlease
595601
NO_INTPTR_T = UnfortunatelyYes
602+
CSPRNG_METHOD = openssl
596603
SANE_TOOL_PATH = /usr/coreutils/bin:/usr/local/bin
597604
SHELL_PATH = /usr/coreutils/bin/bash
598605
endif
@@ -628,6 +635,7 @@ ifeq ($(uname_S),MINGW)
628635
NO_POSIX_GOODIES = UnfortunatelyYes
629636
DEFAULT_HELP_FORMAT = html
630637
HAVE_PLATFORM_PROCINFO = YesPlease
638+
CSPRNG_METHOD = rtlgenrandom
631639
BASIC_LDFLAGS += -municode
632640
COMPAT_CFLAGS += -DNOGDI -Icompat -Icompat/win32
633641
COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"

contrib/buildsystems/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
260260
_CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe" NO_SYMLINK_HEAD UNRELIABLE_FSTAT
261261
NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
262262
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)
264264
list(APPEND compat_SOURCES compat/mingw.c compat/winansi.c compat/win32/path-utils.c
265265
compat/win32/pthread.c compat/win32mmap.c compat/win32/syslog.c
266266
compat/win32/trace2_win32_process_info.c compat/win32/dirent.c

git-compat-util.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@
188188
#endif
189189
#include <windows.h>
190190
#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
191197
#endif
192198

193199
#include <unistd.h>
@@ -258,6 +264,12 @@
258264
#else
259265
#include <stdint.h>
260266
#endif
267+
#ifdef HAVE_ARC4RANDOM_LIBBSD
268+
#include <bsd/stdlib.h>
269+
#endif
270+
#ifdef HAVE_GETRANDOM
271+
#include <sys/random.h>
272+
#endif
261273
#ifdef NO_INTPTR_T
262274
/*
263275
* 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)
14211433

14221434
void sleep_millisec(int millisec);
14231435

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+
14241443
#endif

t/helper/test-csprng.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static struct test_cmd cmds[] = {
2020
{ "chmtime", cmd__chmtime },
2121
{ "config", cmd__config },
2222
{ "crontab", cmd__crontab },
23+
{ "csprng", cmd__csprng },
2324
{ "ctype", cmd__ctype },
2425
{ "date", cmd__date },
2526
{ "delta", cmd__delta },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ int cmd__bloom(int argc, const char **argv);
1010
int cmd__chmtime(int argc, const char **argv);
1111
int cmd__config(int argc, const char **argv);
1212
int cmd__crontab(int argc, const char **argv);
13+
int cmd__csprng(int argc, const char **argv);
1314
int cmd__ctype(int argc, const char **argv);
1415
int cmd__date(int argc, const char **argv);
1516
int cmd__delta(int argc, const char **argv);

wrapper.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,69 @@ int open_nofollow(const char *path, int flags)
702702
return open(path, flags);
703703
#endif
704704
}
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+
}

0 commit comments

Comments
 (0)