From df290e52a1943840688d83ed9725a65453ff6016 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Wed, 31 Jan 2024 12:12:53 +0100 Subject: [PATCH] os: replace GenerateRandomData() by own arc4random_buf() if missing arc4random_buf() is a pretty standard libc function on Unix'oid platforms, but not all our targets have it, thus we need a fallback there. Currently we have GenerateRandomData(), which either just wraps arc4random_buf() or provides some fallback implementation. For those cases it's easier to just implement missing functions directly instead of having custom wrapper functions. So, drop GenerateRandomData() in favor of arc4random_buf() and provide fallback implementation for where it is missing. GenerateRandomData() isn't declared _X_EXPORT, thus no API/ABI break. X-ABI-Break: no Signed-off-by: Enrico Weigelt, metux IT consult --- os/auth.c | 14 -------------- os/mitauth.c | 2 +- os/osdep.h | 13 +++++++++++-- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/os/auth.c b/os/auth.c index 6e3f53152..caa03c40e 100644 --- a/os/auth.c +++ b/os/auth.c @@ -312,17 +312,3 @@ GenerateAuthorization(unsigned name_length, } #endif /* XCSECURITY */ - -void -GenerateRandomData(int len, char *buf) -{ -#ifdef HAVE_ARC4RANDOM_BUF - arc4random_buf(buf, len); -#else - int fd; - - fd = open("/dev/urandom", O_RDONLY); - read(fd, buf, len); - close(fd); -#endif -} diff --git a/os/mitauth.c b/os/mitauth.c index 44e3329c2..40a0450f3 100644 --- a/os/mitauth.c +++ b/os/mitauth.c @@ -149,7 +149,7 @@ MitGenerateCookie(unsigned data_length, if (i >= sizeof(cookie)) i = 0; } - GenerateRandomData(sizeof(cookie), cookie); + arc4random_buf(cookie, sizeof(cookie)); status = MitAddCookie(sizeof(cookie), cookie, id); if (!status) { id = -1; diff --git a/os/osdep.h b/os/osdep.h index 5b64f1a38..ce23ceaa5 100644 --- a/os/osdep.h +++ b/os/osdep.h @@ -128,7 +128,16 @@ extern WorkQueuePtr workQueue; /* in access.c */ extern Bool ComputeLocalClient(ClientPtr client); -/* in auth.c */ -extern void GenerateRandomData(int len, char *buf); +/* for platforms lacking arc4random_buf() libc function */ +#ifndef HAVE_ARC4RANDOM_BUF +static inline void arc4random_buf(char *buf, size_t len) +{ + int fd; + + fd = open("/dev/urandom", O_RDONLY); + read(fd, buf, len); + close(fd); +} +#endif /* HAVE_ARC4RANDOM_BUF */ #endif /* _OSDEP_H_ */