Skip to content

Commit 4a13def

Browse files
Switch random() and rand() to arc4random_uniform() if available
Keep srandom(seed) also if we use arc4random*() Update doc tcl-commands.rst Add dietlibc hints to doc COMPILE-GUIDE
1 parent 028e756 commit 4a13def

8 files changed

Lines changed: 44 additions & 33 deletions

File tree

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ AX_TYPE_SOCKLEN_T
112112
AX_CREATE_STDINT_H([eggint.h])
113113

114114
# Checks for functions and their arguments.
115-
AC_CHECK_FUNCS([clock_gettime dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton memset_s snprintf strlcpy vsnprintf])
115+
AC_CHECK_FUNCS([arc4random_uniform clock_gettime dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton memset_s snprintf strlcpy vsnprintf])
116116
AC_FUNC_SELECT_ARGTYPES
117117
EGG_FUNC_B64_NTOP
118118
AC_FUNC_MMAP

doc/COMPILE-GUIDE

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Eggdrop Compile Guide and FAQ
2-
Last revised: May 14, 2023
2+
Last revised: Jan 31, 2025
33
_____________________________________________________________________
44

55
Eggdrop Compile Guide and FAQ
@@ -48,6 +48,9 @@ Last revised: May 14, 2023
4848
13. I get '/usr/bin/ld: cannot find -lpthread' (OpenWrt)
4949
14. I get 'configure: error: C compiler cannot create executables
5050
(Alpine Linux)
51+
15. I get 'checking build system type... Invalid configuration
52+
'x86_64-pc-linux-dietlibc': OS 'dietlibc' not recognized (dietlibc)
53+
16. I get 'error: implicit declaration of function ‘srandom’' (dietlibc)
5154

5255

5356
Compile Guide
@@ -580,6 +583,15 @@ Last revised: May 14, 2023
580583

581584
Install the musl c library implementation used by Alpine:
582585
apk add musl-dev
586+
587+
15. I get 'checking build system type... Invalid configuration
588+
'x86_64-pc-linux-dietlibc': OS 'dietlibc' not recognized (dietlibc)
589+
590+
./configure --build=x86_64-pc-linux-gnu
591+
592+
16 I get 'error: implicit declaration of function ‘srandom’' (dietlibc)
593+
594+
Add -D_BSD_SOURCE
583595
_____________________________________________________________________
584596

585597
Copyright (C) 1997 Robey Pointer

doc/sphinx_source/using/tcl-commands.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2443,7 +2443,7 @@ myip
24432443
rand <limit>
24442444
^^^^^^^^^^^^
24452445

2446-
Returns: a random integer between 0 and limit-1. Limit must be greater than 0 and equal to or less than RAND_MAX, which is generally 2147483647. The underlying pseudo-random number generator is not cryptographically secure.
2446+
Returns: a random integer between 0 and limit-1. Limit must be greater than 0 and equal to or less than RAND_MAX, which is generally 2147483647. If arc4random_uniform() is available and is a cryptographically secure pseudorandom number generator (CSPRNG) then this function will also be and return uniformly distributed (avoiding modulo bias) values. Otherwise not. In other words, this function rolls the dice as good as it can and probably better than any AI.
24472447

24482448
Module: core
24492449

src/eggdrop.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,19 @@
187187
#include <time.h> /* POSIX 2001 */
188188
#include <sys/resource.h> /* getrusage() setrlimit() after time.h because of BSD */
189189

190+
/* On systems with random(), RANDOM_MAX may or may not be defined.
191+
*
192+
* If RANDOM_MAX isn't defined, we use 0x7FFFFFFF (2^31-1), or 2147483647
193+
* since this follows the 4.3BSD and POSIX.1-2001 standards. This of course
194+
* assumes random() uses a 32 bit long int type per the standards.
195+
*/
196+
#ifndef RANDOM_MAX
197+
# define RANDOM_MAX 0x7FFFFFFF /* random() -- 2^31-1 */
198+
#endif
199+
200+
#ifdef HAVE_ARC4RANDOM_UNIFORM
201+
#define randint arc4random_uniform
202+
#else
190203
/* Yikes...who would have thought finding a usable random() would be so much
191204
* trouble?
192205
* Note: random() is *not* thread safe.
@@ -199,24 +212,13 @@
199212
# undef HAVE_SRANDOM
200213
#endif
201214

202-
/* On systems with random(), RANDOM_MAX may or may not be defined.
203-
*
204-
* If RANDOM_MAX isn't defined, we use 0x7FFFFFFF (2^31-1), or 2147483647
205-
* since this follows the 4.3BSD and POSIX.1-2001 standards. This of course
206-
* assumes random() uses a 32 bit long int type per the standards.
207-
*/
208-
#ifndef RANDOM_MAX
209-
# define RANDOM_MAX 0x7FFFFFFF /* random() -- 2^31-1 */
210-
#endif
211-
212-
213215
/* Use high-order bits for getting the random integer. With a modern
214216
* random() implementation, modulo would probably be sufficient, but on
215217
* systems lacking random(), it may just be a macro for an older rand()
216218
* function.
217219
*/
218-
#define randint(n) (unsigned long) (random() / (RANDOM_MAX + 1.0) * n)
219-
220+
#define randint(n) (uint32_t) (random() / (RANDOM_MAX + 1.0) * n)
221+
#endif /* HAVE_ARC4RANDOM_UNIFORM */
220222

221223
#ifdef TLS
222224
# include <openssl/ssl.h>

src/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@
6363
#include "modules.h"
6464
#include "bg.h"
6565

66+
#ifndef HAVE_ARC4RANDOM_UNIFORM
6667
#ifdef HAVE_GETRANDOM
6768
# include <sys/random.h>
6869
#endif
70+
#endif /* HAVE_ARC4RANDOM_UNIFORM */
6971

7072
#ifndef _POSIX_SOURCE
7173
# define _POSIX_SOURCE 1 /* Solaris needs this */
@@ -916,8 +918,13 @@ static void mainloop(int toplevel)
916918
}
917919
}
918920

921+
/* We keep srandom(seed) also if we use arc4random*() because 3rd party modules
922+
* could use random() and rely on eggdrop core seeding it */
919923
static void init_random(void) {
920924
unsigned int seed;
925+
#ifdef HAVE_ARC4RANDOM_UNIFORM
926+
seed = arc4random();
927+
#else
921928
#ifdef HAVE_GETRANDOM
922929
if (getrandom(&seed, sizeof seed, 0) != (sizeof seed)) {
923930
if (errno != ENOSYS) {
@@ -940,9 +947,11 @@ static void init_random(void) {
940947
}
941948
}
942949
#endif
950+
#endif /* HAVE_ARC4RANDOM_UNIFORM */
943951
srandom(seed);
944952
}
945953

954+
946955
int main(int arg_c, char **arg_v)
947956
{
948957
int i, j, xx;

src/mod/blowfish.mod/blowfish.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static char *encrypt_string_cbc(char *key, char *str)
378378
slen = strlen(str) + 8;
379379
s = nmalloc(slen + 9);
380380
for (i = 0; i < 8; ++i) {
381-
s[i] = (char) (random() % 256);
381+
s[i] = (char) randint(256);
382382
}
383383
strcpy(s + 8, str);
384384
if ((!key) || (!key[0]))

src/mod/uptime.mod/uptime.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static int minutes = 0;
7979
static int seconds = 0;
8080
static int next_seconds = 0;
8181
static int next_minutes = 0;
82-
static int update_interval = 720; /* rand(0..12) hours: ~6 hour average. */
82+
static int update_interval = 12 * 60 * 60; /* randint(0..12) hours: ~6 hour average. */
8383
static time_t next_update = 0;
8484
static int uptimesock;
8585
static int uptimecount;
@@ -161,10 +161,7 @@ static int init_uptime(void)
161161
}
162162
fcntl(uptimesock, F_SETFL, O_NONBLOCK | fcntl(uptimesock, F_GETFL));
163163

164-
next_minutes = rand() % update_interval; /* Initial update delay */
165-
next_seconds = rand() % 59;
166-
next_update = (time_t) ((time(NULL) / 60 * 60) + (next_minutes * 60) +
167-
next_seconds);
164+
next_update = (time_t) ((time(NULL) / 60 * 60) + randint(update_interval));
168165

169166
return 0;
170167
}
@@ -251,10 +248,7 @@ void check_secondly()
251248

252249
minutes = 0; /* Reset for the next countdown. */
253250
seconds = 0;
254-
next_minutes = rand() % update_interval;
255-
next_seconds = rand() % 59;
256-
next_update = (time_t) ((time(NULL) / 60 * 60) + (next_minutes * 60) +
257-
next_seconds);
251+
next_update = (time_t) ((time(NULL) / 60 * 60) + randint(update_interval));
258252

259253
/* Go back to checking every minute. */
260254
add_hook(HOOK_MINUTELY, (Function) check_minutely);

src/tclmisc.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,6 @@ static int tcl_myip STDVAR
455455
static int tcl_rand STDVAR
456456
{
457457
long i;
458-
unsigned long x;
459-
char s[11];
460458

461459
BADARGS(2, 2, " limit");
462460

@@ -471,11 +469,7 @@ static int tcl_rand STDVAR
471469
return TCL_ERROR;
472470
}
473471

474-
x = randint(i);
475-
476-
egg_snprintf(s, sizeof s, "%lu", x);
477-
478-
Tcl_AppendResult(irp, s, NULL);
472+
Tcl_SetObjResult(interp, Tcl_NewIntObj(randint(i)));
479473
return TCL_OK;
480474
}
481475

0 commit comments

Comments
 (0)