wow, openbsd did something i actually like:
-
wow, openbsd did something i actually like:
ELFs (elves?) have a section called.openbsd.randomdata
, and any variable you put in there will be filled with random data by the kernel or dynamic linker when the program is started. OpenBSD puts its stack cookies there so they're ready immediately on program start.Linux and FreeBSD both put a fixed number of random bytes into the ELF auxiliary vector (for linux, 16 bytes in
AT_RANDOM
; for FreeBSD8 * sizeof(long)
bytes inAT_CANARY
). Their respective libc implementations then have constructors that initialize the global canary variables.This can be a bit annoying since libc has to make sure its constructor and anything that runs before it don't try to use the stack canary. Normal programs also can't use this mechanism to get syscall-free random numbers since they'd have no way of knowing which are stack canaries they can't leak.
-
To the kernel and dynamic linker the name
.openbsd.randomdata
don't matter, the important part is the program header of typePT_OPENBSD_RANDOMIZE
. The fact that libc always has a.openbsd.randomdata
means that you can get free randomness pretty easily though:#include <stdio.h>
unsigned int random __attribute__((section(".openbsd.randomdata")));
int main() {
printf("random: %d\n", random);
return 0;
}