Module Name:    src
Committed By:   riastradh
Date:           Tue Mar 11 14:30:28 UTC 2025

Modified Files:
        src/lib/libc/gen: arc4random.c
        src/sys/kern: kern_entropy.c
        src/sys/sys: sysctl.h

Log Message:
Assign static MIB numbers for kern.entropy.epoch.

This sidesteps any need for dynamically sized memory in userland to
resolve sysctl names to read it out, or for a new syscall interface
to sysctl resolution by name.

It would really be better to expose this through a page shared with
userland, so querying it doesn't cost a syscall, but this will serve
for now.

PR lib/59148: arc4random calls malloc so it can't be used in an ELF
constructor


To generate a diff of this commit:
cvs rdiff -u -r1.49 -r1.50 src/lib/libc/gen/arc4random.c
cvs rdiff -u -r1.72 -r1.73 src/sys/kern/kern_entropy.c
cvs rdiff -u -r1.239 -r1.240 src/sys/sys/sysctl.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/gen/arc4random.c
diff -u src/lib/libc/gen/arc4random.c:1.49 src/lib/libc/gen/arc4random.c:1.50
--- src/lib/libc/gen/arc4random.c:1.49	Tue Mar 11 13:35:54 2025
+++ src/lib/libc/gen/arc4random.c	Tue Mar 11 14:30:27 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: arc4random.c,v 1.49 2025/03/11 13:35:54 riastradh Exp $	*/
+/*	$NetBSD: arc4random.c,v 1.50 2025/03/11 14:30:27 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -51,7 +51,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: arc4random.c,v 1.49 2025/03/11 13:35:54 riastradh Exp $");
+__RCSID("$NetBSD: arc4random.c,v 1.50 2025/03/11 14:30:27 riastradh Exp $");
 
 #include "namespace.h"
 #include "reentrant.h"
@@ -64,7 +64,6 @@ __RCSID("$NetBSD: arc4random.c,v 1.49 20
 
 #include <assert.h>
 #include <sha2.h>
-#include <stdatomic.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -511,35 +510,10 @@ crypto_onetimestream_selftest(void)
 static unsigned
 entropy_epoch(void)
 {
-	static atomic_int mib0[3];
-	static atomic_bool initialized = false;
-	int mib[3];
+	const int mib[] = { CTL_KERN, KERN_ENTROPY, KERN_ENTROPY_EPOCH };
 	unsigned epoch = (unsigned)-1;
 	size_t epochlen = sizeof(epoch);
 
-	/*
-	 * Resolve kern.entropy.epoch if we haven't already.  Cache it
-	 * for the next caller.  Initialization is idempotent, so it's
-	 * OK if two threads do it at once.
-	 */
-	if (atomic_load_explicit(&initialized, memory_order_acquire)) {
-		mib[0] = atomic_load_explicit(&mib0[0], memory_order_relaxed);
-		mib[1] = atomic_load_explicit(&mib0[1], memory_order_relaxed);
-		mib[2] = atomic_load_explicit(&mib0[2], memory_order_relaxed);
-	} else {
-		size_t nmib = __arraycount(mib);
-
-		if (sysctlnametomib("kern.entropy.epoch", mib, &nmib) == -1)
-			return (unsigned)-1;
-		if (nmib != __arraycount(mib))
-			return (unsigned)-1;
-		atomic_store_explicit(&mib0[0], mib[0], memory_order_relaxed);
-		atomic_store_explicit(&mib0[1], mib[1], memory_order_relaxed);
-		atomic_store_explicit(&mib0[2], mib[2], memory_order_relaxed);
-		atomic_store_explicit(&initialized, true,
-		    memory_order_release);
-	}
-
 	if (sysctl(mib, __arraycount(mib), &epoch, &epochlen, NULL, 0) == -1)
 		return (unsigned)-1;
 	if (epochlen != sizeof(epoch))

Index: src/sys/kern/kern_entropy.c
diff -u src/sys/kern/kern_entropy.c:1.72 src/sys/kern/kern_entropy.c:1.73
--- src/sys/kern/kern_entropy.c:1.72	Tue Aug 27 00:56:47 2024
+++ src/sys/kern/kern_entropy.c	Tue Mar 11 14:30:28 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_entropy.c,v 1.72 2024/08/27 00:56:47 riastradh Exp $	*/
+/*	$NetBSD: kern_entropy.c,v 1.73 2025/03/11 14:30:28 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -77,7 +77,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.72 2024/08/27 00:56:47 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.73 2025/03/11 14:30:28 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -358,7 +358,7 @@ entropy_init(void)
 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "entropy",
 	    SYSCTL_DESCR("Entropy (random number sources) options"),
 	    NULL, 0, NULL, 0,
-	    CTL_KERN, CTL_CREATE, CTL_EOL);
+	    CTL_KERN, KERN_ENTROPY, CTL_EOL);
 
 	/* Create the sysctl knobs.  */
 	/* XXX These shouldn't be writable at securelevel>0.  */
@@ -402,7 +402,7 @@ entropy_init(void)
 	sysctl_createv(&entropy_sysctllog, 0, &entropy_sysctlroot, NULL,
 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_INT,
 	    "epoch", SYSCTL_DESCR("Entropy epoch"),
-	    NULL, 0, &E->epoch, 0, CTL_CREATE, CTL_EOL);
+	    NULL, 0, &E->epoch, 0, KERN_ENTROPY_EPOCH, CTL_EOL);
 
 	/* Initialize the global state for multithreaded operation.  */
 	mutex_init(&E->lock, MUTEX_DEFAULT, IPL_SOFTSERIAL);

Index: src/sys/sys/sysctl.h
diff -u src/sys/sys/sysctl.h:1.239 src/sys/sys/sysctl.h:1.240
--- src/sys/sys/sysctl.h:1.239	Sat Jan 20 13:15:46 2024
+++ src/sys/sys/sysctl.h	Tue Mar 11 14:30:28 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: sysctl.h,v 1.239 2024/01/20 13:15:46 christos Exp $	*/
+/*	$NetBSD: sysctl.h,v 1.240 2025/03/11 14:30:28 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -275,6 +275,7 @@ struct ctlname {
 #define	KERN_BOOTTIME		83	/* struct: time kernel was booted */
 #define	KERN_EVCNT		84	/* struct: evcnts */
 #define	KERN_SOFIXEDBUF		85	/* bool: fixed socket buffer sizes */
+#define	KERN_ENTROPY		86	/* node: entropy(9) subsystem */
 
 /*
  *  KERN_CLOCKRATE structure
@@ -780,6 +781,12 @@ typedef int	(*hashstat_func_t)(struct ha
 void		hashstat_register(const char *, hashstat_func_t);
 
 /*
+ * kern.entropy.* variables
+ */
+
+#define	KERN_ENTROPY_EPOCH	1	/* int: PRNG reseed epoch */
+
+/*
  * CTL_VM identifiers in <uvm/uvm_param.h>
  */
 

Reply via email to