From owner-svn-src-all@freebsd.org Thu Dec 26 19:41:11 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 120001D7C3F; Thu, 26 Dec 2019 19:41:11 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47kL126jHDz4GSV; Thu, 26 Dec 2019 19:41:10 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id E18325934; Thu, 26 Dec 2019 19:41:10 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id xBQJfADF053713; Thu, 26 Dec 2019 19:41:10 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id xBQJf9mL053706; Thu, 26 Dec 2019 19:41:09 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201912261941.xBQJf9mL053706@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Thu, 26 Dec 2019 19:41:09 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r356097 - in head: share/man/man9 sys/compat/ndis sys/dev/oce sys/kern sys/libkern sys/sys X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: in head: share/man/man9 sys/compat/ndis sys/dev/oce sys/kern sys/libkern sys/sys X-SVN-Commit-Revision: 356097 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Dec 2019 19:41:11 -0000 Author: cem Date: Thu Dec 26 19:41:09 2019 New Revision: 356097 URL: https://svnweb.freebsd.org/changeset/base/356097 Log: random(9): Deprecate random(9), remove meaningless srandom(9) srandom(9) is meaningless on SMP systems or any system with, say, interrupts. One could never rely on random(9) to produce a reproducible sequence of outputs on the basis of a specific srandom() seed because the global state was shared by all kernel contexts. As such, removing it is literally indistinguishable to random(9) consumers (as compared with retaining it). Mark random(9) as deprecated and slated for quick removal. This is not to say we intend to remove all fast, non-cryptographic PRNG(s) in the kernel. It/they just won't be random(9), as it exists today, in either name or implementation. Before random(9) is removed, a replacement will be provided and in-tree consumers will be converted. Note that despite the name, the random(9) interface does not bear any resemblance to random(3). Instead, it is the same crummy 1988 Park-Miller LCG used in libc rand(3). Modified: head/share/man/man9/random.9 head/sys/compat/ndis/subr_ntoskrnl.c head/sys/dev/oce/oce_mbox.c head/sys/kern/init_main.c head/sys/kern/subr_stats.c head/sys/libkern/random.c head/sys/sys/libkern.h Modified: head/share/man/man9/random.9 ============================================================================== --- head/share/man/man9/random.9 Thu Dec 26 19:32:11 2019 (r356096) +++ head/share/man/man9/random.9 Thu Dec 26 19:41:09 2019 (r356097) @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" " -.Dd April 16, 2019 +.Dd December 26, 2019 .Dt RANDOM 9 .Os .Sh NAME @@ -36,8 +36,7 @@ .Nm is_random_seeded , .Nm random , .Nm read_random , -.Nm read_random_uio , -.Nm srandom +.Nm read_random_uio .Nd supply pseudo-random numbers .Sh SYNOPSIS .In sys/libkern.h @@ -57,8 +56,6 @@ .Fn read_random_uio "struct uio *uio" "bool nonblock" .Ss LEGACY ROUTINES .In sys/libkern.h -.Ft void -.Fn srandom "u_long seed" .Ft u_long .Fn random "void" .Sh DESCRIPTION @@ -134,19 +131,16 @@ Otherwise, this function may block interruptibly until If the function is interrupted before the random device is seeded, no data is returned. .Pp -The legacy -.Fn random -function will produce a sequence of numbers that can be duplicated by calling -.Fn srandom -with some constant as the -.Fa seed . -The legacy -.Fn srandom -function may be called with any -.Fa seed -value. +The deprecated +.Xr random 9 +function will produce a sequence of pseudorandom numbers using a similar weak +linear congruential generator as +.Xr rand 3 +(the 1988 Park-Miller LCG). +It is obsolete and scheduled to be removed in +.Fx 13.0 . It is strongly advised that the -.Fn random +.Xr random 9 function not be used to generate random numbers. See .Sx SECURITY CONSIDERATIONS . @@ -173,23 +167,6 @@ the number of bytes placed in .Fn read_random_uio returns zero when successful, otherwise an error code is returned. -.Pp -The legacy -.Fn random -function uses -a non-linear additive feedback random number generator -employing a default table -of size 31 -containing long integers -to return successive pseudo-random -numbers in the range from 0 to -.if t 2\u\s731\s10\d\(mi1. -.if n (2**31)\(mi1. -The period of this random number generator -is very large, -approximately -.if t 16\(mu(2\u\s731\s10\d\(mi1). -.if n 16*((2**31)\(mi1). .Sh ERRORS .Fn read_random_uio may fail if: @@ -212,8 +189,6 @@ wrote .Sh SECURITY CONSIDERATIONS Do not use .Fn random -or -.Fn srandom in new code. .Pp It is important to remember that the Modified: head/sys/compat/ndis/subr_ntoskrnl.c ============================================================================== --- head/sys/compat/ndis/subr_ntoskrnl.c Thu Dec 26 19:32:11 2019 (r356096) +++ head/sys/compat/ndis/subr_ntoskrnl.c Thu Dec 26 19:41:09 2019 (r356097) @@ -3195,10 +3195,8 @@ rand(void) } static void -srand(unsigned int seed) +srand(unsigned int seed __unused) { - - srandom(seed); } static uint8_t Modified: head/sys/dev/oce/oce_mbox.c ============================================================================== --- head/sys/dev/oce/oce_mbox.c Thu Dec 26 19:32:11 2019 (r356096) +++ head/sys/dev/oce/oce_mbox.c Thu Dec 26 19:41:09 2019 (r356097) @@ -859,7 +859,6 @@ oce_config_nic_rss(POCE_SOFTC sc, uint32_t if_id, uint fwcmd->params.req.if_id = LE_32(if_id); - srandom(arc4random()); /* random entropy seed */ read_random(fwcmd->params.req.hash, sizeof(fwcmd->params.req.hash)); rc = oce_rss_itbl_init(sc, fwcmd); Modified: head/sys/kern/init_main.c ============================================================================== --- head/sys/kern/init_main.c Thu Dec 26 19:32:11 2019 (r356096) +++ head/sys/kern/init_main.c Thu Dec 26 19:41:09 2019 (r356097) @@ -624,7 +624,6 @@ SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc static void proc0_post(void *dummy __unused) { - struct timespec ts; struct proc *p; struct rusage ru; struct thread *td; @@ -656,27 +655,8 @@ proc0_post(void *dummy __unused) sx_sunlock(&allproc_lock); PCPU_SET(switchtime, cpu_ticks()); PCPU_SET(switchticks, ticks); - - /* - * Give the ``random'' number generator a thump. - */ - nanotime(&ts); - srandom(ts.tv_sec ^ ts.tv_nsec); } SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL); - -static void -random_init(void *dummy __unused) -{ - - /* - * After CPU has been started we have some randomness on most - * platforms via get_cyclecount(). For platforms that don't - * we will reseed random(9) in proc0_post() as well. - */ - srandom(get_cyclecount()); -} -SYSINIT(random, SI_SUB_RANDOM, SI_ORDER_FIRST, random_init, NULL); /* *************************************************************************** Modified: head/sys/kern/subr_stats.c ============================================================================== --- head/sys/kern/subr_stats.c Thu Dec 26 19:32:11 2019 (r356096) +++ head/sys/kern/subr_stats.c Thu Dec 26 19:41:09 2019 (r356097) @@ -2963,7 +2963,14 @@ stats_v1_vsd_tdgst_compress(enum vsd_dtype vs_dtype, * re-inserting the mu/cnt of each as a value and corresponding weight. */ -#define bitsperrand 31 /* Per random(3). */ + /* + * XXXCEM: random(9) is currently rand(3), not random(3). rand(3) + * RAND_MAX happens to be approximately 31 bits (range [0, + * 0x7ffffffd]), so the math kinda works out. When/if this portion of + * the code is compiled in userspace, it gets the random(3) behavior, + * which has expected range [0, 0x7fffffff]. + */ +#define bitsperrand 31 ebits = 0; nebits = 0; bitsperidx = fls(maxctds); @@ -2971,7 +2978,6 @@ stats_v1_vsd_tdgst_compress(enum vsd_dtype vs_dtype, ("%s: bitsperidx=%d, ebits=%d", __func__, bitsperidx, (int)(sizeof(ebits) << 3))); idxmask = (UINT64_C(1) << bitsperidx) - 1; - srandom(stats_sbinuptime()); /* Initialise the free list with randomised centroid indices. */ for (; remctds > 0; remctds--) { Modified: head/sys/libkern/random.c ============================================================================== --- head/sys/libkern/random.c Thu Dec 26 19:32:11 2019 (r356096) +++ head/sys/libkern/random.c Thu Dec 26 19:41:09 2019 (r356097) @@ -34,31 +34,30 @@ #include __FBSDID("$FreeBSD$"); +#include #include +#include -#define NSHUFF 50 /* to drop some "seed -> 1st value" linearity */ - static u_long randseed = 937186357; /* after srandom(1), NSHUFF counted */ -void -srandom(u_long seed) -{ - int i; - - randseed = seed; - for (i = 0; i < NSHUFF; i++) - (void)random(); -} - /* * Pseudo-random number generator for perturbing the profiling clock, * and whatever else we might use it for. The result is uniform on * [0, 2^31 - 1]. */ u_long -random() +random(void) { + static bool warned = false; + long x, hi, lo, t; + + /* Warn only once, or it gets very spammy. */ + if (!warned) { + gone_in(13, + "random(9) is the obsolete Park-Miller LCG from 1988"); + warned = true; + } /* * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1). Modified: head/sys/sys/libkern.h ============================================================================== --- head/sys/sys/libkern.h Thu Dec 26 19:32:11 2019 (r356096) +++ head/sys/sys/libkern.h Thu Dec 26 19:41:09 2019 (r356097) @@ -166,7 +166,6 @@ void qsort_r(void *base, size_t nmemb, size_t size, v int (*compar)(void *, const void *, const void *)); u_long random(void); int scanc(u_int, const u_char *, const u_char *, int); -void srandom(u_long); int strcasecmp(const char *, const char *); char *strcat(char * __restrict, const char * __restrict); char *strchr(const char *, int);