diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2008-10-15 23:23:52 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2008-10-15 23:23:52 +0000 |
commit | 402af595bcaa120a104360ebebeb3d8f5eac8859 (patch) | |
tree | 246ce2dffcd538c7674e8ed19b552cbadd811192 /sys | |
parent | 01ed2ea8940e2739cafb585b7d5f8d8db553c397 (diff) |
make random(9) return per-cpu values (by saving the seed in the cpuinfo),
which are uniform for the profclock on each cpu in a SMP system (but using
a different seed for each cpu). on all cpus, avoid seeding with a value out
of the [0, 2^31-1] range (since that is not stable)
ok kettenis drahn
Diffstat (limited to 'sys')
-rw-r--r-- | sys/arch/alpha/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/arch/amd64/amd64/cpu.c | 3 | ||||
-rw-r--r-- | sys/arch/amd64/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/arch/arm/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/arch/hppa/include/cpu.h | 5 | ||||
-rw-r--r-- | sys/arch/i386/i386/cpu.c | 3 | ||||
-rw-r--r-- | sys/arch/i386/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/arch/m68k/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/arch/m88k/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/arch/macppc/macppc/cpu.c | 3 | ||||
-rw-r--r-- | sys/arch/mips64/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/arch/powerpc/include/cpu.h | 4 | ||||
-rw-r--r-- | sys/arch/sh/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/arch/sparc/sparc/cpuvar.h | 3 | ||||
-rw-r--r-- | sys/arch/sparc64/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/arch/sparc64/sparc64/cpu.c | 3 | ||||
-rw-r--r-- | sys/arch/vax/include/cpu.h | 3 | ||||
-rw-r--r-- | sys/conf/files | 3 | ||||
-rw-r--r-- | sys/kern/init_main.c | 4 | ||||
-rw-r--r-- | sys/lib/libkern/libkern.h | 6 | ||||
-rw-r--r-- | sys/lib/libkern/random.c | 24 | ||||
-rw-r--r-- | sys/lib/libkern/srandom.c | 44 |
22 files changed, 59 insertions, 76 deletions
diff --git a/sys/arch/alpha/include/cpu.h b/sys/arch/alpha/include/cpu.h index 3351d04a32a..508c3d00c58 100644 --- a/sys/arch/alpha/include/cpu.h +++ b/sys/arch/alpha/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.35 2008/10/10 08:36:28 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.36 2008/10/15 23:23:44 deraadt Exp $ */ /* $NetBSD: cpu.h,v 1.45 2000/08/21 02:03:12 thorpej Exp $ */ /*- @@ -202,6 +202,7 @@ struct cpu_info { u_long ci_flags; /* flags; see below */ u_long ci_ipis; /* interprocessor interrupts pending */ #endif + u_int32_t ci_randseed; }; #define CPUF_PRIMARY 0x01 /* CPU is primary CPU */ diff --git a/sys/arch/amd64/amd64/cpu.c b/sys/arch/amd64/amd64/cpu.c index c7222a901d4..ee47764724e 100644 --- a/sys/arch/amd64/amd64/cpu.c +++ b/sys/arch/amd64/amd64/cpu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.c,v 1.20 2008/10/06 20:42:02 chl Exp $ */ +/* $OpenBSD: cpu.c,v 1.21 2008/10/15 23:23:46 deraadt Exp $ */ /* $NetBSD: cpu.c,v 1.1 2003/04/26 18:39:26 fvdl Exp $ */ /*- @@ -397,6 +397,7 @@ cpu_boot_secondary_processors(void) ci = cpu_info[i]; if (ci == NULL) continue; + ci->ci_randseed = random(); if (ci->ci_idle_pcb == NULL) continue; if ((ci->ci_flags & CPUF_PRESENT) == 0) diff --git a/sys/arch/amd64/include/cpu.h b/sys/arch/amd64/include/cpu.h index 248a9659c6e..8bbe67e6ebc 100644 --- a/sys/arch/amd64/include/cpu.h +++ b/sys/arch/amd64/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.37 2008/07/18 23:43:31 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.38 2008/10/15 23:23:46 deraadt Exp $ */ /* $NetBSD: cpu.h,v 1.1 2003/04/26 18:39:39 fvdl Exp $ */ /*- @@ -71,6 +71,7 @@ struct cpu_info { u_int ci_apicid; u_long ci_spin_locks; u_long ci_simple_locks; + u_int32_t ci_randseed; u_int64_t ci_scratch; diff --git a/sys/arch/arm/include/cpu.h b/sys/arch/arm/include/cpu.h index 45673233d2f..c517b9c5268 100644 --- a/sys/arch/arm/include/cpu.h +++ b/sys/arch/arm/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.22 2008/10/10 08:36:28 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.23 2008/10/15 23:23:46 deraadt Exp $ */ /* $NetBSD: cpu.h,v 1.34 2003/06/23 11:01:08 martin Exp $ */ /* @@ -216,6 +216,7 @@ struct cpu_info { #ifdef MULTIPROCESSOR MP_CPU_INFO_MEMBERS #endif + u_int32_t ci_randseed; }; #ifndef MULTIPROCESSOR diff --git a/sys/arch/hppa/include/cpu.h b/sys/arch/hppa/include/cpu.h index d908c3ba425..8763c036dca 100644 --- a/sys/arch/hppa/include/cpu.h +++ b/sys/arch/hppa/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.58 2008/10/10 08:36:28 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.59 2008/10/15 23:23:47 deraadt Exp $ */ /* * Copyright (c) 2000-2004 Michael Shalayeff @@ -68,9 +68,10 @@ #include <sys/sched.h> struct cpu_info { - struct proc *ci_curproc; + struct proc *ci_curproc; struct schedstate_percpu ci_schedstate; + u_int32_t ci_randseed; }; extern struct cpu_info cpu_info_primary; diff --git a/sys/arch/i386/i386/cpu.c b/sys/arch/i386/i386/cpu.c index 23e5ae3911e..b994c40610b 100644 --- a/sys/arch/i386/i386/cpu.c +++ b/sys/arch/i386/i386/cpu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.c,v 1.31 2008/10/05 16:57:36 kettenis Exp $ */ +/* $OpenBSD: cpu.c,v 1.32 2008/10/15 23:23:47 deraadt Exp $ */ /* $NetBSD: cpu.c,v 1.1.2.7 2000/06/26 02:04:05 sommerfeld Exp $ */ /*- @@ -355,6 +355,7 @@ cpu_boot_secondary_processors() ci = cpu_info[i]; if (ci == NULL) continue; + ci->ci_randseed = random(); if (ci->ci_idle_pcb == NULL) continue; if ((ci->ci_flags & CPUF_PRESENT) == 0) diff --git a/sys/arch/i386/include/cpu.h b/sys/arch/i386/include/cpu.h index 53d9764f966..48fd4f35843 100644 --- a/sys/arch/i386/include/cpu.h +++ b/sys/arch/i386/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.104 2008/09/28 20:32:17 brad Exp $ */ +/* $OpenBSD: cpu.h,v 1.105 2008/10/15 23:23:47 deraadt Exp $ */ /* $NetBSD: cpu.h,v 1.35 1996/05/05 19:29:26 christos Exp $ */ /*- @@ -86,6 +86,7 @@ struct cpu_info { u_long ci_spin_locks; /* # of spin locks held */ u_long ci_simple_locks; /* # of simple locks held */ #endif + u_int32_t ci_randseed; /* * Private members. diff --git a/sys/arch/m68k/include/cpu.h b/sys/arch/m68k/include/cpu.h index f498b8a3b0c..2fd7b60f243 100644 --- a/sys/arch/m68k/include/cpu.h +++ b/sys/arch/m68k/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.21 2008/10/10 08:36:28 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.22 2008/10/15 23:23:48 deraadt Exp $ */ /* $NetBSD: cpu.h,v 1.3 1997/02/02 06:56:57 thorpej Exp $ */ /* @@ -73,6 +73,7 @@ struct cpu_info { struct proc *ci_curproc; struct schedstate_percpu ci_schedstate; + u_int32_t ci_randseed; }; extern struct cpu_info cpu_info_store; diff --git a/sys/arch/m88k/include/cpu.h b/sys/arch/m88k/include/cpu.h index c4b5d6fbc25..f4de3b9eb8e 100644 --- a/sys/arch/m88k/include/cpu.h +++ b/sys/arch/m88k/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.37 2008/10/10 08:36:28 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.38 2008/10/15 23:23:48 deraadt Exp $ */ /* * Copyright (c) 1996 Nivas Madhur * Copyright (c) 1992, 1993 @@ -110,6 +110,7 @@ struct cpu_info { #define CI_DDB_PAUSE 3 int ci_softintr; /* pending soft interrupts */ + u_int32_t ci_randseed; #ifdef MULTIPROCESSOR diff --git a/sys/arch/macppc/macppc/cpu.c b/sys/arch/macppc/macppc/cpu.c index 1c7e58ea528..d443aa4f15a 100644 --- a/sys/arch/macppc/macppc/cpu.c +++ b/sys/arch/macppc/macppc/cpu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.c,v 1.58 2008/09/18 03:56:25 drahn Exp $ */ +/* $OpenBSD: cpu.c,v 1.59 2008/10/15 23:23:49 deraadt Exp $ */ /* * Copyright (c) 1997 Per Fogelstrom @@ -672,6 +672,7 @@ cpu_boot_secondary_processors(void) ci = &cpu_info[i]; if (ci->ci_cpuid == 0) continue; + ci->ci_randseed = random(); sched_init_cpu(ci); diff --git a/sys/arch/mips64/include/cpu.h b/sys/arch/mips64/include/cpu.h index e56b62491c1..fec803b246a 100644 --- a/sys/arch/mips64/include/cpu.h +++ b/sys/arch/mips64/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.28 2008/10/10 08:36:28 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.29 2008/10/15 23:23:49 deraadt Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -374,6 +374,7 @@ struct cpu_info { struct schedstate_percpu ci_schedstate; struct proc *ci_curproc; + u_int32_t ci_randseed; }; extern struct cpu_info cpu_info_primary; diff --git a/sys/arch/powerpc/include/cpu.h b/sys/arch/powerpc/include/cpu.h index b14490768a1..5bb8462ef08 100644 --- a/sys/arch/powerpc/include/cpu.h +++ b/sys/arch/powerpc/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.40 2008/10/10 08:05:45 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.41 2008/10/15 23:23:49 deraadt Exp $ */ /* $NetBSD: cpu.h,v 1.1 1996/09/30 16:34:21 ws Exp $ */ /* @@ -80,7 +80,7 @@ struct cpu_info { #define CI_DDB_ENTERDDB 3 #define CI_DDB_INDDB 4 - u_long ci_randseed; + u_int32_t ci_randseed; }; static __inline struct cpu_info * diff --git a/sys/arch/sh/include/cpu.h b/sys/arch/sh/include/cpu.h index 39d386f893d..681b799c039 100644 --- a/sys/arch/sh/include/cpu.h +++ b/sys/arch/sh/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.17 2008/10/10 08:36:28 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.18 2008/10/15 23:23:49 deraadt Exp $ */ /* $NetBSD: cpu.h,v 1.41 2006/01/21 04:24:12 uwe Exp $ */ /*- @@ -59,6 +59,7 @@ struct cpu_info { struct proc *ci_curproc; struct schedstate_percpu ci_schedstate; /* scheduler state */ + u_int32_t ci_randseed; }; extern struct cpu_info cpu_info_store; diff --git a/sys/arch/sparc/sparc/cpuvar.h b/sys/arch/sparc/sparc/cpuvar.h index 65cc5202053..63b12a0d66f 100644 --- a/sys/arch/sparc/sparc/cpuvar.h +++ b/sys/arch/sparc/sparc/cpuvar.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpuvar.h,v 1.15 2008/10/10 08:36:28 art Exp $ */ +/* $OpenBSD: cpuvar.h,v 1.16 2008/10/15 23:23:50 deraadt Exp $ */ /* $NetBSD: cpuvar.h,v 1.4 1997/07/06 21:14:25 pk Exp $ */ /* @@ -79,6 +79,7 @@ struct cpu_info { struct cpu_info *ci_next; struct schedstate_percpu ci_schedstate; + u_int32_t ci_randseed; }; #define curcpu() (&cpuinfo.ci) diff --git a/sys/arch/sparc64/include/cpu.h b/sys/arch/sparc64/include/cpu.h index 1178403d9e2..bd1a79ffcb8 100644 --- a/sys/arch/sparc64/include/cpu.h +++ b/sys/arch/sparc64/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.68 2008/10/10 08:05:45 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.69 2008/10/15 23:23:50 deraadt Exp $ */ /* $NetBSD: cpu.h,v 1.28 2001/06/14 22:56:58 thorpej Exp $ */ /* @@ -119,6 +119,7 @@ struct cpu_info { int ci_itid; #endif int ci_node; + u_int32_t ci_randseed; struct schedstate_percpu ci_schedstate; /* scheduler state */ int ci_want_resched; diff --git a/sys/arch/sparc64/sparc64/cpu.c b/sys/arch/sparc64/sparc64/cpu.c index adf48401b8d..3678a7fafad 100644 --- a/sys/arch/sparc64/sparc64/cpu.c +++ b/sys/arch/sparc64/sparc64/cpu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.c,v 1.49 2008/08/17 15:55:55 kettenis Exp $ */ +/* $OpenBSD: cpu.c,v 1.50 2008/10/15 23:23:50 deraadt Exp $ */ /* $NetBSD: cpu.c,v 1.13 2001/05/26 21:27:15 chs Exp $ */ /* @@ -701,6 +701,7 @@ cpu_boot_secondary_processors(void) for (ci = cpus; ci != NULL; ci = ci->ci_next) { if (ci->ci_upaid == cpu_myid()) continue; + ci->ci_randseed = random(); if (CPU_ISSUN4V) cpuid = ci->ci_upaid; diff --git a/sys/arch/vax/include/cpu.h b/sys/arch/vax/include/cpu.h index a7af7b5a25c..68e9478ee17 100644 --- a/sys/arch/vax/include/cpu.h +++ b/sys/arch/vax/include/cpu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.h,v 1.32 2008/10/10 08:36:28 art Exp $ */ +/* $OpenBSD: cpu.h,v 1.33 2008/10/15 23:23:50 deraadt Exp $ */ /* $NetBSD: cpu.h,v 1.41 1999/10/21 20:01:36 ragge Exp $ */ /* @@ -54,6 +54,7 @@ struct cpu_info { struct proc *ci_curproc; struct schedstate_percpu ci_schedstate; /* scheduler state */ + u_int32_t ci_randseed; }; extern struct cpu_info cpu_info_store; diff --git a/sys/conf/files b/sys/conf/files index 4391aad2531..49c5c5f2c08 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1,4 +1,4 @@ -# $OpenBSD: files,v 1.443 2008/10/10 20:21:40 deraadt Exp $ +# $OpenBSD: files,v 1.444 2008/10/15 23:23:51 deraadt Exp $ # $NetBSD: files,v 1.87 1996/05/19 17:17:50 jonathan Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 @@ -1033,7 +1033,6 @@ file compat/common/vm_43.c !small_kernel file lib/libkern/mcount.c gprof file lib/libkern/getsn.c file lib/libkern/random.c -file lib/libkern/srandom.c file lib/libkern/arch/${MACHINE_ARCH}/strchr.S | lib/libkern/strchr.c file lib/libkern/arch/${MACHINE_ARCH}/strrchr.S | lib/libkern/strrchr.c file lib/libkern/arch/${MACHINE_ARCH}/__main.S | lib/libkern/__main.c diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 2dfe7ccba78..0226b96b3e1 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: init_main.c,v 1.153 2008/10/14 18:27:29 guenther Exp $ */ +/* $OpenBSD: init_main.c,v 1.154 2008/10/15 23:23:51 deraadt Exp $ */ /* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */ /* @@ -513,7 +513,7 @@ main(void *framep) #endif /* CRYPTO */ microtime(&rtv); - srandom((u_long)(rtv.tv_sec ^ rtv.tv_usec)); + srandom((u_int32_t)(rtv.tv_sec ^ rtv.tv_usec) ^ arc4random()); randompid = 1; diff --git a/sys/lib/libkern/libkern.h b/sys/lib/libkern/libkern.h index a357f0ece48..7eac2cc7bf8 100644 --- a/sys/lib/libkern/libkern.h +++ b/sys/lib/libkern/libkern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: libkern.h,v 1.24 2007/05/04 22:17:30 deraadt Exp $ */ +/* $OpenBSD: libkern.h,v 1.25 2008/10/15 23:23:51 deraadt Exp $ */ /* $NetBSD: libkern.h,v 1.7 1996/03/14 18:52:08 christos Exp $ */ /*- @@ -155,8 +155,8 @@ void bzero(void *, size_t); int ffs(int); void *memchr(const void *, int, size_t); int memcmp(const void *, const void *, size_t); -u_long random(void); -void srandom(u_long); +u_int32_t random(void); +void srandom(u_int32_t); int scanc(u_int, const u_char *, const u_char [], int); int skpc(int, size_t, u_char *); size_t strlen(const char *); diff --git a/sys/lib/libkern/random.c b/sys/lib/libkern/random.c index 258a9df94fb..d78e1b0607a 100644 --- a/sys/lib/libkern/random.c +++ b/sys/lib/libkern/random.c @@ -1,4 +1,4 @@ -/* $OpenBSD: random.c,v 1.7 2004/08/07 00:38:33 deraadt Exp $ */ +/* $OpenBSD: random.c,v 1.8 2008/10/15 23:23:51 deraadt Exp $ */ /* $NetBSD: random.c,v 1.2 1994/10/26 06:42:42 cgd Exp $ */ /*- @@ -32,6 +32,7 @@ * @(#)random.c 8.1 (Berkeley) 6/10/93 */ +#include <sys/param.h> #include <sys/types.h> #include <lib/libkern/libkern.h> @@ -41,12 +42,23 @@ * and whatever else we might use it for. The result is uniform on * [0, 2^31 - 1]. */ -u_long _randseed = 1; -u_long +void +srandom(u_int32_t seed) +{ + struct cpu_info *ci = curcpu(); + + seed &= 0x7fffffff; + if (seed == 0) + seed = 1; + ci->ci_randseed = seed; +} + +u_int32_t random(void) { - long x, hi, lo, t; + struct cpu_info *ci = curcpu(); + int32_t x, hi, lo, t; /* * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1). @@ -54,12 +66,12 @@ random(void) * Park and Miller, Communications of the ACM, vol. 31, no. 10, * October 1988, p. 1195. */ - x = _randseed; + x = ci->ci_randseed; hi = x / 127773; lo = x % 127773; t = 16807 * lo - 2836 * hi; if (t <= 0) t += 0x7fffffff; - _randseed = t; + ci->ci_randseed = t; return (t); } diff --git a/sys/lib/libkern/srandom.c b/sys/lib/libkern/srandom.c deleted file mode 100644 index 33cab523e04..00000000000 --- a/sys/lib/libkern/srandom.c +++ /dev/null @@ -1,44 +0,0 @@ -/* $OpenBSD: srandom.c,v 1.4 2004/08/07 00:38:33 deraadt Exp $ */ - -/*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * @(#)random.c 8.1 (Berkeley) 6/10/93 - */ - -#include <sys/types.h> - -#include <lib/libkern/libkern.h> - -extern u_long _randseed; - -void -srandom(u_long seed) -{ - _randseed = seed; -} |