These are available in sandboxes without access to files or to devices. Also they are considered safer and more straightforward to use than /dev/urandom as reading from a file has quite a few edge cases:
- Linux:
getrandom(buf, buflen, 0). getrandom(2) was introduced in version 3.17 of the Linux kernel. - OpenBSD:
getentropy(buf, buflen). The getentropy(2) function appeared in OpenBSD 5.6. - FreeBSD and NetBSD:
sysctl(KERN_ARND). Not sure when this was added but it has existed for quite a while.
Alternatives:
- Linux has sysctl
CTL_KERN/KERN_RANDOM/RANDOM_UUIDwhich gives 16 bytes of randomness. This may be available on older kernels, however sysctl is deprecated on Linux and even removed in some distros so we shouldn't use it.
Add tests for GetOSRand():
- Test that no error happens (otherwise
RandFailure()which aborts) - Test that all 32 bytes are overwritten (initialize with zeros, try multiple times)
Discussion:
- When to use these? Currently they are always used when available. Another option would be to use them only when
/dev/urandomis not available. But this would mean these code paths receive less testing, and I'm not sure there is any reason to prefer/dev/urandom.
Closes: #9676. I've tested this on all three OS-es.