Source-Changes-HG archive

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

[src/netbsd-6]: src/sys Pull up:



details:   https://anonhg.NetBSD.org/src/rev/1ba03dc057df
branches:  netbsd-6
changeset: 774140:1ba03dc057df
user:      jdc <jdc%NetBSD.org@localhost>
date:      Mon May 21 16:49:54 2012 +0000

description:
Pull up:

  revision 1.10 src/sys/dev/rndpseudo.c
  revision 1.9  src/sys/kern/subr_cprng.c

(requested by tls in ticket 273).

  Fix two problems that could cause /dev/random to not wake up readers when
  entropy became available.

diffstat:

 sys/dev/rndpseudo.c   |  23 +++++++++++++----------
 sys/kern/subr_cprng.c |  46 +++++++++++++++++++++++++++++-----------------
 2 files changed, 42 insertions(+), 27 deletions(-)

diffs (133 lines):

diff -r 4a8a2a2508d0 -r 1ba03dc057df sys/dev/rndpseudo.c
--- a/sys/dev/rndpseudo.c       Mon May 21 15:26:33 2012 +0000
+++ b/sys/dev/rndpseudo.c       Mon May 21 16:49:54 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rndpseudo.c,v 1.6.2.2 2012/04/20 23:35:20 riz Exp $    */
+/*     $NetBSD: rndpseudo.c,v 1.6.2.3 2012/05/21 16:49:54 jdc Exp $    */
 
 /*-
  * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rndpseudo.c,v 1.6.2.2 2012/04/20 23:35:20 riz Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rndpseudo.c,v 1.6.2.3 2012/05/21 16:49:54 jdc Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_compat_netbsd.h"
@@ -309,14 +309,23 @@
                /* XXX is this _really_ what's wanted? */
                if (ctx->hard) {
                        n = MIN(want, strength - ctx->bytesonkey);
-                       ctx->bytesonkey += n;
+                       if (n < 1) {
+                           cprng_strong_deplete(cprng);
+                           n = MIN(want, strength);
+                           ctx->bytesonkey = 0;
+                           membar_producer();
+                       }
                } else {
                        n = want;
                }
 
                nread = cprng_strong(cprng, bf, n,
                                     (fp->f_flag & FNONBLOCK) ? FNONBLOCK : 0);
-               if (nread != n) {
+
+               if (ctx->hard && nread > 0) {
+                       atomic_add_int(&ctx->bytesonkey, nread);
+               }
+               if (nread < 1) {
                        if (fp->f_flag & FNONBLOCK) {
                                ret = EWOULDBLOCK;
                        } else {
@@ -331,12 +340,6 @@
                }
        }
 out:
-       if (ctx->bytesonkey >= strength) {
-               /* Force reseed of underlying DRBG (prediction resistance) */
-               cprng_strong_deplete(cprng);
-               ctx->bytesonkey = 0;
-       }
-
        pool_cache_put(rp_pc, bf);
        return (ret);
 }
diff -r 4a8a2a2508d0 -r 1ba03dc057df sys/kern/subr_cprng.c
--- a/sys/kern/subr_cprng.c     Mon May 21 15:26:33 2012 +0000
+++ b/sys/kern/subr_cprng.c     Mon May 21 16:49:54 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_cprng.c,v 1.5.2.2 2012/04/20 23:35:20 riz Exp $ */
+/*     $NetBSD: subr_cprng.c,v 1.5.2.3 2012/05/21 16:49:54 jdc Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -46,7 +46,7 @@
 
 #include <sys/cprng.h>
 
-__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.5.2.2 2012/04/20 23:35:20 riz Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.5.2.3 2012/05/21 16:49:54 jdc Exp $");
 
 void
 cprng_init(void)
@@ -144,6 +144,9 @@
 #ifdef RND_VERBOSE
            printf("cprng: sink %s cprng busy, no reseed\n", c->reseed.name);
 #endif
+           if (c->flags & CPRNG_USE_CV) {      /* XXX if flags change? */
+               cv_broadcast(&c->cv);
+           }
            return;
        }
 
@@ -240,23 +243,32 @@
                                      "failed.", c->name);
                        }
                } else {
-                       if (!(flags & FNONBLOCK) && 
-                            (c->flags & CPRNG_USE_CV)) {
-                               int wr;
+                       int wr;
 
+                       do {
                                cprng_strong_sched_reseed(c);
-                               do {
-                                       wr = cv_wait_sig(&c->cv, &c->mtx);
-                                       if (wr == ERESTART) {
-                                               mutex_exit(&c->mtx);
-                                               return 0;
-                                       }
-                               } while (nist_ctr_drbg_generate(&c->drbg, p,
-                                                               len, &cc,
-                                                               sizeof(cc)));
-                       } else {
-                               len = 0;
-                       }
+                               if ((flags & FNONBLOCK) ||
+                                   !(c->flags & CPRNG_USE_CV)) {
+                                       len = 0;
+                                       break;
+                               }
+                       /*
+                        * XXX There's a race with the cv_broadcast
+                        * XXX in cprng_strong_sched_reseed, because
+                        * XXX of the use of tryenter in that function.
+                        * XXX This "timedwait" hack works around it,
+                        * XXX at the expense of occasionaly polling
+                        * XXX for success on a /dev/random rekey.
+                        */
+                               wr = cv_timedwait_sig(&c->cv, &c->mtx,
+                                                     mstohz(100));
+                               if (wr == ERESTART) {
+                                       mutex_exit(&c->mtx);
+                                       return 0;
+                               }
+                       } while (nist_ctr_drbg_generate(&c->drbg, p,
+                                                       len, &cc,
+                                                       sizeof(cc)));
                }
        }
 



Home | Main Index | Thread Index | Old Index