Replace the implementation of DTrace's RAND subroutine for generating

low-quality random numbers with a modern implementation (xoroshiro128+)
that is capable of generating better quality randomness without compromising performance.

Submitted by:	Graeme Jenkinson
Reviewed by:	markj
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D9051
This commit is contained in:
gnn 2017-02-03 22:26:19 +00:00
parent a61eaf8db2
commit 45b8e2daa4
5 changed files with 31 additions and 2 deletions

View File

@ -124,6 +124,7 @@
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/ptrace.h>
#include <sys/random.h>
#include <sys/rwlock.h>
#include <sys/sx.h>
#include <sys/sysctl.h>
@ -136,6 +137,8 @@
#include "dtrace_debug.c"
#endif
#include "dtrace_xoroshiro128_plus.h"
/*
* DTrace Tunable Variables
*
@ -298,7 +301,6 @@ static kmutex_t dtrace_meta_lock; /* meta-provider state lock */
#define vuprintf vprintf
#define ttoproc(_a) ((_a)->td_proc)
#define crgetzoneid(_a) 0
#define NCPU MAXCPU
#define SNOCD 0
#define CPU_ON_INTR(_a) 0
@ -4236,7 +4238,8 @@ dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs,
switch (subr) {
case DIF_SUBR_RAND:
regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875;
regs[rd] = dtrace_xoroshiro128_plus_next(
state->dts_rstate[curcpu]);
break;
#ifdef illumos
@ -14495,6 +14498,7 @@ dtrace_state_create(struct cdev *dev, struct ucred *cred __unused)
dtrace_state_t *state;
dtrace_optval_t *opt;
int bufsize = NCPU * sizeof (dtrace_buffer_t), i;
int cpu_it;
ASSERT(MUTEX_HELD(&dtrace_lock));
ASSERT(MUTEX_HELD(&cpu_lock));
@ -14550,6 +14554,21 @@ dtrace_state_create(struct cdev *dev, struct ucred *cred __unused)
state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
/*
* Allocate and initialise the per-process per-CPU random state.
* SI_SUB_RANDOM < SI_SUB_DTRACE_ANON therefore entropy device is
* assumed to be seeded at this point (if from Fortuna seed file).
*/
(void) read_random(&state->dts_rstate[0], 2 * sizeof(uint64_t));
for (cpu_it = 1; cpu_it < NCPU; cpu_it++) {
/*
* Each CPU is assigned a 2^64 period, non-overlapping
* subsequence.
*/
dtrace_xoroshiro128_plus_jump(state->dts_rstate[cpu_it-1],
state->dts_rstate[cpu_it]);
}
#ifdef illumos
state->dts_cleaner = CYCLIC_NONE;
state->dts_deadman = CYCLIC_NONE;

View File

@ -50,6 +50,7 @@ extern "C" {
*/
#include <sys/dtrace.h>
#ifndef illumos
#ifdef __sparcv9
typedef uint32_t pc_t;
@ -65,6 +66,10 @@ typedef u_long greg_t;
#define DTRACE_MAXPROPLEN 128
#define DTRACE_DYNVAR_CHUNKSIZE 256
#ifdef __FreeBSD__
#define NCPU MAXCPU
#endif /* __FreeBSD__ */
struct dtrace_probe;
struct dtrace_ecb;
struct dtrace_predicate;
@ -1169,6 +1174,7 @@ struct dtrace_state {
dtrace_cred_t dts_cred; /* credentials */
size_t dts_nretained; /* number of retained enabs */
int dts_getf; /* number of getf() calls */
uint64_t dts_rstate[NCPU][2]; /* per-CPU random state */
};
struct dtrace_provider {

View File

@ -12,6 +12,7 @@ ARCHDIR= ${MACHINE_CPUARCH}
KMOD= dtrace
SRCS= dtrace.c \
dtrace_xoroshiro128_plus.c \
dtrace_asm.S \
dtrace_subr.c
@ -42,6 +43,7 @@ CFLAGS+= -I${SYSDIR}/cddl/compat/opensolaris \
-I${SYSDIR}/cddl/dev/dtrace \
-I${SYSDIR}/cddl/dev/dtrace/${ARCHDIR} \
-I${SYSDIR}/cddl/contrib/opensolaris/uts/common \
-I${SYSDIR}/cddl/contrib/opensolaris/uts/common/dtrace \
-I${SYSDIR}/cddl/contrib/opensolaris/common/util \
-I${SYSDIR} -DDIS_MEM

View File

@ -10,6 +10,7 @@ SRCS+= vnode_if.h
CFLAGS+= -I${SYSDIR}/cddl/compat/opensolaris \
-I${SYSDIR}/cddl/contrib/opensolaris/uts/common \
-I${SYSDIR}/cddl/contrib/opensolaris/uts/common/dtrace \
-I${SYSDIR}
.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"

View File

@ -10,6 +10,7 @@ SRCS+= vnode_if.h
CFLAGS+= -I${SYSDIR}/cddl/compat/opensolaris \
-I${SYSDIR}/cddl/contrib/opensolaris/uts/common \
-I${SYSDIR}/cddl/contrib/opensolaris/uts/common/dtrace \
-I${SYSDIR}
.include <bsd.kmod.mk>