Skip to content

Lesson 11 of 14 · 15m

Random Numbers Worth Trusting: getrandom vs rand()

rand() seeded with time is a clock reading; getrandom(2) and arc4random_buf are the CSPRNG — the Debian OpenSSL fiasco is why it matters.

srand(time(NULL)); token = rand(); ships in password-reset flows with depressing regularity, and it reduces your "unguessable" token to at most the number of seconds in the window an attacker guesses you generated it — brute-forcing every time(NULL) value for the last day is under 90,000 tries, and glibc's rand then replays the exact sequence deterministically. Your token wasn't random; it was a clock reading.

The wrong mental model: "rand gives random numbers." It gives pseudorandom numbers from a deterministic generator — glibc's is an additive lagged-Fibonacci; POSIX's is even weaker — and determinism is the entire problem: same seed in, same sequence out. Seeding with the wall clock means your whole security property is "nobody guesses when I ran this," which fails the moment the attacker knows the request happened around 3 PM. srand(getpid()), srand(time(NULL) ^ getpid()) — every variant is the same bug with extra steps.

The mechanism that actually exists for this on Linux is getrandom(2) (kernel 3.17, October 2014): one syscall, bytes from the kernel's CSPRNG, no fd to manage, and it blocks until the kernel pool is seeded at boot — which is the only blocking that matters. arc4random_buf does the same job in userspace (BSD forever; glibc added it in 2.36, August 2022). Reading /dev/urandom directly also works and always has; /dev/random's "more secure because it blocks" story is a myth — post-boot, both draw from the same CSPRNG, and the old entropy-estimate blocking behavior caused real production stalls while adding nothing.

#include <sys/random.h>
#include <stdio.h>

unsigned char tok[32];
if (getrandom(tok, sizeof tok, 0) != (ssize_t)sizeof tok)
    return -1;
for (size_t i = 0; i < sizeof tok; i++)
    printf("%02x", tok[i]);     /* 256-bit token, hex */
putchar('\n');

The receipt that should end the argument: Debian's OpenSSL bug, CVE-2008-0166. A Debian maintainer silenced two Valgrind warnings in September 2006 by removing the lines that stirred uninitialized memory into OpenSSL's PRNG seed — which reduced the entropy of every generated key to the process ID: ~15 bits, 32,768 possible keys per architecture. Every SSH host key, TLS key, and certificate generated on Debian/Ubuntu for twenty months was brute-forceable in minutes; DSA-1571 landed in May 2008 with blacklists still distributed today. Two lines about "random-looking" data, twenty months of compromised keys.

The rules that keep you out of the advisory:

  • Tokens, session IDs, reset codes, salts, keys, nonces: getrandom, arc4random_buf, or /dev/urandom. Nothing else.
  • rand/random/drand48/srand are for games and test data — anywhere the sequence should be reproducible.
  • If you need uniform integers in a range, getrandom bytes plus rejection sampling or arc4random_uniform; rand() % n also carries a modulo-bias flaw on top of the predictability.
  • Fork: re-seed or use getrandom after fork if you pre-seeded a userspace PRNG — child processes inherit the identical state. (The kernel CSPRNG doesn't have this problem, which is another reason to just call it.)

Try this

srand(time(NULL)); printf("%d\n", rand()); in one terminal, then in another srand(t) for t across the last hour and print the first value of each — watch the token you "generated" show up in the replay list. That's the whole attack.

Field notes

  • getrandom can fail with EINTR or EAGAIN (pre-seed or GRND_NONBLOCK); check the return like every other syscall, and loop for a full read — it can return short like read does.
  • Older code reads /dev/urandom with fopen/fread — fine, as long as you check the byte count. getrandom removes the fd-management edge cases (chroot, sandboxed fds) that made direct reads subtle.
  • Hardware RNG (rdrand) feeds the kernel pool — you consume it through getrandom, not by rolling your own.
  • The rand trap generalizes: any "unpredictable" value derived from time, PID, or a counter is attacker-derivable. If the security property depends on the bytes, the source must be a CSPRNG.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.