Re:Arc4random – randomization for all occasions
by ushi on Saturday November 22, 2014 @04:21AM
(#8645187)
getrandom() 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/...
Re:Arc4random – randomization for all occasions
by acqq on Saturday November 22, 2014 @05:23AM
(#8645268)
Yes, 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/
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.
Re:Arc4random – randomization for all occasions
by wereHamster on Saturday November 22, 2014 @04:54AM
(#8645235)
> 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...
Re:Arc4random – randomization for all occasions
by acqq on Saturday November 22, 2014 @05:59AM
(#8645313)
Once 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.
Re:Arc4random – randomization for all occasions
by acveilleux on Saturday November 22, 2014 @10:33AM
(#8645725)
SIGSEGV
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.)
Re:Arc4random – randomization for all occasions
by shawn-butler on Saturday November 22, 2014 @12:42AM
(#8644873)
arc4random has a larger range as well up to 0x100000000, whereas rand/random top out at RAND_MAX of 0x7fffffff?
http://www.openbsd.org/papers/hackfest2014-arc4random/mgp000...
http://www.openbsd.org/papers/hackfest2014-arc4random/mgp000... Linux's getrandom really has a bunch of error codes:http://lwn.net/Articles/606202/
And a few flags: I agree that the best interface is the one that can't fail.