Messages in this thread | | | Date | Thu, 13 Jan 2005 20:54:54 -0800 | From | Ulrich Drepper <> | Subject | short read from /dev/urandom |
| |
The /dev/urandom device is advertised as always returning the requested number of bytes. Yet, it fails to do this under some situations. Compile this
int main (void) { alarm (100); char buf[32]; int fd = open ("/dev/urandom", 0); while (1) if (read (fd, buf, sizeof buf) != sizeof (buf)) abort (); }
with
gcc -o r r.c -g -O2 -pg
Note the -pg at the end to enable profiling. Running this code fails for me after less than a second.
The relevant code in the kernel is this (drivers/char/random.c:extract_entropy)
while (nbytes) { /* * Check if we need to break out or reschedule.... */ if ((flags & EXTRACT_ENTROPY_USER) && need_resched()) { if (signal_pending(current)) { if (ret == 0) ret = -ERESTARTSYS; break; }
Here the loop is left prematurely if a signal is pending.
One solution is to redefine the /dev/urandom interface. The problem is that this will cause program to fail. I know since I found this while debugging such a program.
-- ➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖ [unhandled content-type:application/pgp-signature] | |