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/srandare for games and test data — anywhere the sequence should be reproducible.- If you need uniform integers in a range,
getrandombytes plus rejection sampling orarc4random_uniform;rand() % nalso carries a modulo-bias flaw on top of the predictability. - Fork: re-seed or use
getrandomafterforkif 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
getrandomcan fail withEINTRorEAGAIN(pre-seed orGRND_NONBLOCK); check the return like every other syscall, and loop for a full read — it can return short likereaddoes.- Older code reads
/dev/urandomwithfopen/fread— fine, as long as you check the byte count.getrandomremoves the fd-management edge cases (chroot, sandboxed fds) that made direct reads subtle. - Hardware RNG (
rdrand) feeds the kernel pool — you consume it throughgetrandom, not by rolling your own. - The
randtrap 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?
Keep reading
Related articles from the SudoSecurity blog.
Implementing UART on the ATmega328P
USART on the AtMega328P Needed Documentation * Arduino Nano PCB design * Atmega328P datasheet Materials used * Arduino Nano * Arduino Uno R3 * Relevant cable * Computer Software Used * make (for easy of compiling and building) * avr-gcc (our compiler for avr ISA) *…
Hello World, Embedded Edition: Blink an LED
Embedded C: Hello WorldNeeded DocumentationArduino Nano PCB designAtmega328P datasheetMaterials usedArduino NanoArduino Uno R3Relevant cableComputerSoftware Usedmake (for easy of compiling and building)avr-gcc (our compiler for avr ISA)binutils-avravr-libcavrdude (used to flash board)The…
Build Your Own Stack-Based Bytecode Virtual Machine in C
Ever wonder how Python's CPython, the JVM, or the Lua VM actually execute code under the hood? In this article, we build a stack-based bytecode virtual machine from scratch in C.
CPPFront Project is Aiming to Modernize the C++ Language
The highly vaunted C++ language would become simpler and safer under a proposal dubbed Cppfront, an experimental C++ front end being floated by prominent C++ developer Herb Sutter. Intended as a major C++ evolution, Cppfront is an experimental “syntax 1 to syntax 2” compiler designed to explore…