Source-Changes-HG archive

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

[src/trunk]: src/sys Put cprng sysctls into subr_cprng.c. Also, make sysctl_...



details:   https://anonhg.NetBSD.org/src/rev/3033022a2bb7
branches:  trunk
changeset: 326066:3033022a2bb7
user:      pooka <pooka%NetBSD.org@localhost>
date:      Fri Jan 17 02:12:48 2014 +0000

description:
Put cprng sysctls into subr_cprng.c.  Also, make sysctl_prng static
in subr_cprng and get rid of SYSCTL_PRIVATE namespace leak macro.

Fixes ping(8) when run against a standalone rump kernel due to appearance
of the kern.urandom sysctl node (in case someone was wondering ...)

diffstat:

 sys/kern/init_sysctl.c |  78 +---------------------------------------
 sys/kern/kern_sysctl.c |  12 +----
 sys/kern/subr_cprng.c  |  97 ++++++++++++++++++++++++++++++++++++++++++++++++-
 sys/sys/sysctl.h       |  10 +----
 4 files changed, 101 insertions(+), 96 deletions(-)

diffs (truncated from 341 to 300 lines):

diff -r 223397e7f4b3 -r 3033022a2bb7 sys/kern/init_sysctl.c
--- a/sys/kern/init_sysctl.c    Fri Jan 17 02:08:56 2014 +0000
+++ b/sys/kern/init_sysctl.c    Fri Jan 17 02:12:48 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: init_sysctl.c,v 1.198 2013/09/14 13:18:02 joerg Exp $ */
+/*     $NetBSD: init_sysctl.c,v 1.199 2014/01/17 02:12:48 pooka Exp $ */
 
 /*-
  * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -30,15 +30,13 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.198 2013/09/14 13:18:02 joerg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.199 2014/01/17 02:12:48 pooka Exp $");
 
 #include "opt_sysv.h"
 #include "opt_compat_netbsd.h"
 #include "opt_modular.h"
 #include "pty.h"
 
-#define SYSCTL_PRIVATE
-
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/sysctl.h>
@@ -115,8 +113,6 @@
 #if NPTY > 0
 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
 #endif /* NPTY > 0 */
-static int sysctl_kern_urnd(SYSCTLFN_PROTO);
-static int sysctl_kern_arnd(SYSCTLFN_PROTO);
 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
@@ -491,18 +487,6 @@
                       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
                       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
        sysctl_createv(clog, 0, NULL, NULL,
-                      CTLFLAG_PERMANENT,
-                      CTLTYPE_INT, "urandom",
-                      SYSCTL_DESCR("Random integer value"),
-                      sysctl_kern_urnd, 0, NULL, 0,
-                      CTL_KERN, KERN_URND, CTL_EOL);
-       sysctl_createv(clog, 0, NULL, NULL,
-                      CTLFLAG_PERMANENT,
-                      CTLTYPE_INT, "arandom",
-                      SYSCTL_DESCR("n bytes of random data"),
-                      sysctl_kern_arnd, 0, NULL, 0,
-                      CTL_KERN, KERN_ARND, CTL_EOL);
-       sysctl_createv(clog, 0, NULL, NULL,
                       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
                       CTLTYPE_INT, "labelsector",
                       SYSCTL_DESCR("Sector number containing the disklabel"),
@@ -1298,64 +1282,6 @@
 #endif /* NPTY > 0 */
 
 /*
- * sysctl helper routine for kern.urandom node. Picks a random number
- * for you.
- */
-static int
-sysctl_kern_urnd(SYSCTLFN_ARGS)
-{
-       int v, rv;
-
-       rv = cprng_strong(sysctl_prng, &v, sizeof(v), 0);
-       if (rv == sizeof(v)) {
-               struct sysctlnode node = *rnode;
-               node.sysctl_data = &v;
-               return (sysctl_lookup(SYSCTLFN_CALL(&node)));
-       }
-       else
-               return (EIO);   /*XXX*/
-}
-
-/*
- * sysctl helper routine for kern.arandom node. Picks a random number
- * for you.
- */
-static int
-sysctl_kern_arnd(SYSCTLFN_ARGS)
-{
-       int error;
-       void *v;
-       struct sysctlnode node = *rnode;
-
-       if (*oldlenp == 0)
-               return 0;
-       /*
-        * This code used to allow sucking 8192 bytes at a time out
-        * of the kernel arc4random generator.  Evidently there is some
-        * very old OpenBSD application code that may try to do this.
-        *
-        * Note that this node is documented as type "INT" -- 4 or 8
-        * bytes, not 8192.
-        *
-        * We continue to support this abuse of the "len" pointer here
-        * but only 256 bytes at a time, as, anecdotally, the actual
-        * application use here was to generate RC4 keys in userspace.
-        *
-        * Support for such large requests will probably be removed
-        * entirely in the future.
-        */
-       if (*oldlenp > 256)
-               return E2BIG;
-
-       v = kmem_alloc(*oldlenp, KM_SLEEP);
-       cprng_fast(v, *oldlenp);
-       node.sysctl_data = v;
-       node.sysctl_size = *oldlenp;
-       error = sysctl_lookup(SYSCTLFN_CALL(&node));
-       kmem_free(v, *oldlenp);
-       return error;
-}
-/*
  * sysctl helper routine to do kern.lwp.* work.
  */
 static int
diff -r 223397e7f4b3 -r 3033022a2bb7 sys/kern/kern_sysctl.c
--- a/sys/kern/kern_sysctl.c    Fri Jan 17 02:08:56 2014 +0000
+++ b/sys/kern/kern_sysctl.c    Fri Jan 17 02:12:48 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_sysctl.c,v 1.243 2013/04/27 20:13:16 christos Exp $       */
+/*     $NetBSD: kern_sysctl.c,v 1.244 2014/01/17 02:12:48 pooka Exp $  */
 
 /*-
  * Copyright (c) 2003, 2007, 2008 The NetBSD Foundation, Inc.
@@ -68,13 +68,11 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.243 2013/04/27 20:13:16 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.244 2014/01/17 02:12:48 pooka Exp $");
 
 #include "opt_defcorename.h"
 #include "ksyms.h"
 
-#define SYSCTL_PRIVATE
-
 #include <sys/param.h>
 #define __COMPAT_SYSCTL
 #include <sys/sysctl.h>
@@ -86,7 +84,6 @@
 #include <sys/syscallargs.h>
 #include <sys/kauth.h>
 #include <sys/ktrace.h>
-#include <sys/cprng.h>
 
 #define        MAXDESCLEN      1024
 MALLOC_DEFINE(M_SYSCTLNODE, "sysctlnode", "sysctl node structures");
@@ -167,8 +164,6 @@
 #endif
 char defcorename[MAXPATHLEN] = DEFCORENAME;
 
-cprng_strong_t *sysctl_prng;
-
 /*
  * ********************************************************************
  * Section 0: Some simple glue
@@ -260,8 +255,7 @@
 void
 sysctl_finalize(void)
 {
-        sysctl_prng = cprng_strong_create("sysctl", IPL_NONE,
-                                         CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
+
        sysctl_root.sysctl_flags |= CTLFLAG_PERMANENT;
 }
 
diff -r 223397e7f4b3 -r 3033022a2bb7 sys/kern/subr_cprng.c
--- a/sys/kern/subr_cprng.c     Fri Jan 17 02:08:56 2014 +0000
+++ b/sys/kern/subr_cprng.c     Fri Jan 17 02:12:48 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_cprng.c,v 1.22 2013/07/27 11:19:09 skrll Exp $ */
+/*     $NetBSD: subr_cprng.c,v 1.23 2014/01/17 02:12:48 pooka Exp $ */
 
 /*-
  * Copyright (c) 2011-2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.22 2013/07/27 11:19:09 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.23 2014/01/17 02:12:48 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -42,9 +42,11 @@
 #include <sys/kernel.h>
 #include <sys/kmem.h>
 #include <sys/lwp.h>
+#include <sys/once.h>
 #include <sys/poll.h>          /* XXX POLLIN/POLLOUT/&c. */
 #include <sys/select.h>
 #include <sys/systm.h>
+#include <sys/sysctl.h>
 #include <sys/rnd.h>
 #include <sys/rndsink.h>
 #if DEBUG
@@ -57,6 +59,9 @@
 #include <machine/cpu_counter.h>
 #endif
 
+static int sysctl_kern_urnd(SYSCTLFN_PROTO);
+static int sysctl_kern_arnd(SYSCTLFN_PROTO);
+
 static void    cprng_strong_generate(struct cprng_strong *, void *, size_t);
 static void    cprng_strong_reseed(struct cprng_strong *);
 static void    cprng_strong_reseed_from(struct cprng_strong *, const void *,
@@ -70,7 +75,22 @@
 void
 cprng_init(void)
 {
+       static struct sysctllog *random_sysctllog;
+
        nist_ctr_initialize();
+
+       sysctl_createv(&random_sysctllog, 0, NULL, NULL,
+                      CTLFLAG_PERMANENT,
+                      CTLTYPE_INT, "urandom",
+                      SYSCTL_DESCR("Random integer value"),
+                      sysctl_kern_urnd, 0, NULL, 0,
+                      CTL_KERN, KERN_URND, CTL_EOL);
+       sysctl_createv(&random_sysctllog, 0, NULL, NULL,
+                      CTLFLAG_PERMANENT,
+                      CTLTYPE_INT, "arandom",
+                      SYSCTL_DESCR("n bytes of random data"),
+                      sysctl_kern_arnd, 0, NULL, 0,
+                      CTL_KERN, KERN_ARND, CTL_EOL);
 }
 
 static inline uint32_t
@@ -477,3 +497,76 @@
        cprng_strong_reseed_from(cprng, seed, bytes, true);
        mutex_exit(&cprng->cs_lock);
 }
+
+static cprng_strong_t *sysctl_prng;
+
+static int
+makeprng(void)
+{
+
+       /* can't create in cprng_init(), too early */
+       sysctl_prng = cprng_strong_create("sysctl", IPL_NONE,
+                                         CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
+       return 0;
+}
+
+/*
+ * sysctl helper routine for kern.urandom node. Picks a random number
+ * for you.
+ */
+static int
+sysctl_kern_urnd(SYSCTLFN_ARGS)
+{
+       static ONCE_DECL(control);
+       int v, rv;
+
+       RUN_ONCE(&control, makeprng);
+       rv = cprng_strong(sysctl_prng, &v, sizeof(v), 0);
+       if (rv == sizeof(v)) {
+               struct sysctlnode node = *rnode;
+               node.sysctl_data = &v;
+               return (sysctl_lookup(SYSCTLFN_CALL(&node)));
+       }
+       else
+               return (EIO);   /*XXX*/
+}
+
+/*
+ * sysctl helper routine for kern.arandom node. Picks a random number
+ * for you.
+ */
+static int
+sysctl_kern_arnd(SYSCTLFN_ARGS)
+{
+       int error;
+       void *v;
+       struct sysctlnode node = *rnode;
+
+       if (*oldlenp == 0)
+               return 0;
+       /*
+        * This code used to allow sucking 8192 bytes at a time out
+        * of the kernel arc4random generator.  Evidently there is some
+        * very old OpenBSD application code that may try to do this.
+        *
+        * Note that this node is documented as type "INT" -- 4 or 8
+        * bytes, not 8192.
+        *
+        * We continue to support this abuse of the "len" pointer here
+        * but only 256 bytes at a time, as, anecdotally, the actual
+        * application use here was to generate RC4 keys in userspace.
+        *
+        * Support for such large requests will probably be removed
+        * entirely in the future.
+        */
+       if (*oldlenp > 256)



Home | Main Index | Thread Index | Old Index