short read from /dev/urandom

From: Ulrich Drepper
Date: Fri Jan 14 2005 - 00:01:21 EST


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 â

Attachment: signature.asc
Description: OpenPGP digital signature