os: replace GenerateRandomData() by custom arc4random_buf() on platforms that missing it

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.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2024-01-31 12:12:53 +01:00
parent 9d1937cd79
commit 70a25209ac
3 changed files with 12 additions and 17 deletions

View File

@ -302,17 +302,3 @@ GenerateAuthorization(unsigned name_length,
} }
#endif /* XCSECURITY */ #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
}

View File

@ -147,7 +147,7 @@ MitGenerateCookie(unsigned data_length,
if (i >= sizeof(cookie)) if (i >= sizeof(cookie))
i = 0; i = 0;
} }
GenerateRandomData(sizeof(cookie), cookie); arc4random_buf(cookie, sizeof(cookie));
status = MitAddCookie(sizeof(cookie), cookie, id); status = MitAddCookie(sizeof(cookie), cookie, id);
if (!status) { if (!status) {
id = -1; id = -1;

View File

@ -130,8 +130,17 @@ extern Bool NewOutputPending;
/* in access.c */ /* in access.c */
extern Bool ComputeLocalClient(ClientPtr client); extern Bool ComputeLocalClient(ClientPtr client);
/* in auth.c */ /* for platforms lacking arc4random_buf() libc function */
extern void GenerateRandomData(int len, char *buf); #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 */
/* OsTimer functions */ /* OsTimer functions */
void TimerInit(void); void TimerInit(void);