4 ms·
The two (for me) important slides: http://www.openbsd.org/papers/hackfest2014-arc4random/mgp00016.html http://www.openbsd.org/papers/hackfest2014-arc4random/mg
by acqq 12y ago
The two (for me) important slides:
http://www.openbsd.org/papers/hackfest2014-arc4random/mgp00016.html http://www.openbsd.org/papers/hackfest2014-arc4random/mgp000...
rc4random cannot fail
Look at the function definitions:
uint32_t
arc4random(void);
void
arc4random_buf(void *buf, size_t nbytes);
uint32_t
arc4random_uniform(uint32_t upper_bound);
All 3 functions guarantee a result. They will not fail, and cannot
return an error.
http://www.openbsd.org/papers/hackfest2014-arc4random/mgp00041.html http://www.openbsd.org/papers/hackfest2014-arc4random/mgp000...
Linux?
We have encouraged Ted Ts'o to create a getentropy-like interface
for Linux; getrandom()
Too many options, too easy to misuse
Interactions with signal handlers (EINTR)
Hope applications do not directly call getrandom()...
Bit worrying
Really feels like there is pressure against easy-access random..
"Use /dev/random" meme continues damaging effects
Linux's getrandom really has a bunch of error codes:
http://lwn.net/Articles/606202/ http://lwn.net/Articles/606202/
EINVAL An invalid flag was passed to getrandom(2)
EFAULT buf is outside the accessible address space.
EAGAIN The requested entropy was not available, and the
getentropy(2) would have blocked if GRND_BLOCK flag
was set.
EINTR While blocked waiting for entropy, the call was
interrupted by a signal handler; see the description
of how interrupted read(2) calls on "slow" devices
are handled with and without the SA_RESTART flag
in the signal(7) man page.
And a few flags:
GRND_NONBLOCK Don't block and return EAGAIN instead
GRND_RANDOM Use the /dev/random pool instead of /dev/urandom
I agree that the best interface is the one that can't fail.
- shawn-butler 12y agoarc4random has a larger range as well up to 0x100000000, whereas rand/random top out at RAND_MAX of 0x7fffffff?
- ushi 12y agogetrandom() is a generic interface you can use to implement getentropy(), which is used to seed arc4random(). It is not an alternative arc4random implementation... From the OpenBSD Manpage: getentropy() will succeed unless: [EFAULT] The buf parameter points to an invalid address. [EIO] Too many bytes requested, or some other fatal error occurred. See: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2 http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/...
- acqq 12y agoYes, getrandom is not an alternative arc4random implementation, it's a lower level interface. Even the programmers writing for Linux should actually locate arc4random implementation that they can use. And before somebody even considers calling getrandom directly he should evaluate how hard is to just implement getentropy from getrandom, it's harder than it looks at the start, if I correctly remember even Ted Tso's first attempt was wrong -- here's the better (I don't know if it's the last): http://lwn.net/Articles/606552/ http://lwn.net/Articles/606552/ int getentropy(void *buf, size_t buflen) { int ret; if (buflen > 256) goto failure; ret = getrandom(buf, buflen, 0); if (ret < 0) return ret; if (ret == buflen) return 0; failure: errno = EIO; return -1; } Note the magical 256 number. It's important.
- wereHamster 12y ago> EFAULT buf is outside the accessible address space. I wonder how arc4random_buf(NULL, 42) behaves. Or what happens if you pass it a pointer to an invalid memory location. Yeah, can't fail, right...
- acqq 12y agoOnce we commit ourselves to write some C code some kinds of failures are expected and the ones you point to are of that kind. The topic here are actually the ones not expected by a C programmer who wishes to use the API function. I don't want this discussion to digress to talking about some other languages even if we all know they exist and can avoid the kind of errors you describe. That's still off topic.
- acveilleux 12y agoSIGSEGV Not a "failure" of the libc call per se and it terminates the caller app which, frankly, is the only safe thing to do. The Linux equivalent, while it can fail could be wrapped in a libc version that emulates arc4random behaviour (provide random or kill the app really.)