Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev When reading from /dev/random, block at most once in...



details:   https://anonhg.NetBSD.org/src/rev/6f6a72734c2d
branches:  trunk
changeset: 788816:6f6a72734c2d
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sun Jul 21 22:30:19 2013 +0000

description:
When reading from /dev/random, block at most once in cprng_strong.

We are not obligated to return exactly as many bytes as requested,
and many applications -- notably those that use stdio or otherwise
buffered I/O to read from /dev/random -- try to read many more than
32 bytes at a time from /dev/random even if all they are about to use
is 32 bytes.

In this case, blocking until we have enough entropy to fill a large
buffer causes needless application delays, e.g. causing cgdconfig
(which reads from /dev/random with stdio) to hang at boot when trying
to configure a random-keyed device for swap.

Patch tested by Aran Clauson.  Fixes PR kern/48028.

diffstat:

 sys/dev/rndpseudo.c |  20 ++++++++++++++++++--
 1 files changed, 18 insertions(+), 2 deletions(-)

diffs (41 lines):

diff -r 1874e9d90e8e -r 6f6a72734c2d sys/dev/rndpseudo.c
--- a/sys/dev/rndpseudo.c       Sun Jul 21 15:29:04 2013 +0000
+++ b/sys/dev/rndpseudo.c       Sun Jul 21 22:30:19 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rndpseudo.c,v 1.15 2013/07/02 13:27:42 pgoyette Exp $  */
+/*     $NetBSD: rndpseudo.c,v 1.16 2013/07/21 22:30:19 riastradh Exp $ */
 
 /*-
  * Copyright (c) 1997-2013 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rndpseudo.c,v 1.15 2013/07/02 13:27:42 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rndpseudo.c,v 1.16 2013/07/21 22:30:19 riastradh Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_compat_netbsd.h"
@@ -387,6 +387,22 @@
                error = uiomove(buf, n_read, uio);
                if (error)
                        goto out;
+
+               /*
+                * Do at most one iteration for /dev/random and return
+                * a short read without hanging further.  Hanging
+                * breaks applications worse than short reads.  Reads
+                * can't be zero unless nonblocking from /dev/random;
+                * in that case, ask caller to retry, instead of just
+                * returning zero bytes, which means EOF.
+                */
+               KASSERT((0 < n_read) || (ctx->rc_hard &&
+                       ISSET(fp->f_flag, FNONBLOCK)));
+               if (ctx->rc_hard) {
+                       if (n_read == 0)
+                               error = EAGAIN;
+                       goto out;
+               }
        }
 
 out:   pool_cache_put(rnd_temp_buffer_cache, buf);



Home | Main Index | Thread Index | Old Index