Remove arc4random() and arc4randbytes() from the kernel API. Replace

arc4random() hacks in rump with stubs that call the host arc4random() to
get numbers that are hopefully actually random (arc4random() keyed with
stack junk is not).  This should fix some of the currently failing anita
tests -- we should no longer generate duplicate "random" MAC addresses in
the test environment.
This commit is contained in:
tls 2011-11-28 08:05:05 +00:00
parent 0bbf9b7ae6
commit f27d6532f5
15 changed files with 125 additions and 58 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: rumpuser.c,v 1.15 2011/02/06 21:05:53 pooka Exp $ */
/* $NetBSD: rumpuser.c,v 1.16 2011/11/28 08:05:05 tls Exp $ */
/*
* Copyright (c) 2007-2010 Antti Kantee. All Rights Reserved.
@ -27,7 +27,7 @@
#include <sys/cdefs.h>
#if !defined(lint)
__RCSID("$NetBSD: rumpuser.c,v 1.15 2011/02/06 21:05:53 pooka Exp $");
__RCSID("$NetBSD: rumpuser.c,v 1.16 2011/11/28 08:05:05 tls Exp $");
#endif /* !lint */
/* thank the maker for this */
@ -640,3 +640,9 @@ rumpuser_getnhostcpu(void)
return 1;
#endif
}
uint32_t
rumpuser_arc4random(void)
{
return arc4random();
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: ip_compat.h,v 1.29 2010/06/01 08:53:20 plunky Exp $ */
/* $NetBSD: ip_compat.h,v 1.30 2011/11/28 08:05:05 tls Exp $ */
/*
* Copyright (C) 1993-2001, 2003 by Darren Reed.
@ -216,7 +216,7 @@ typedef unsigned int u_32_t;
# ifdef _KERNEL
# define NEED_LOCAL_RAND 1
# define ipf_random arc4random
# define ipf_random cprng_fast32
# define KRWLOCK_T krwlock_t
# define KMUTEX_T kmutex_t
@ -582,7 +582,8 @@ typedef struct {
# ifdef _KERNEL
# define NEED_LOCAL_RAND 1
# define ipf_random arc4random
#include <sys/cprng.h>
# define ipf_random cprng_fast32
# define ATOMIC_INC(x) { MUTEX_ENTER(&ipf_rw); \
(x)++; MUTEX_EXIT(&ipf_rw); }
# define ATOMIC_DEC(x) { MUTEX_ENTER(&ipf_rw); \
@ -806,7 +807,9 @@ typedef unsigned int u_32_t;
# endif
# endif
#ifndef _KERNEL
# define ipf_random arc4random
#endif
# if (__NetBSD_Version__ >= 499000000)
# ifdef _KERNEL
@ -834,7 +837,8 @@ typedef unsigned int u_32_t;
# endif
# ifdef _KERNEL
# define ipf_random arc4random
# include <sys/cprng.h>
# define ipf_random cprng_fast32
# if (__NetBSD_Version__ >= 399001400)
# define KMALLOCS(a, b, c) (a) = (b)malloc((c), _M_IPF, M_NOWAIT)
# endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: ip_state.c,v 1.36 2010/04/17 21:00:44 darrenr Exp $ */
/* $NetBSD: ip_state.c,v 1.37 2011/11/28 08:05:05 tls Exp $ */
/*
* Copyright (C) 1995-2003 by Darren Reed.
@ -115,7 +115,7 @@ struct file;
#if !defined(lint)
#if defined(__NetBSD__)
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ip_state.c,v 1.36 2010/04/17 21:00:44 darrenr Exp $");
__KERNEL_RCSID(0, "$NetBSD: ip_state.c,v 1.37 2011/11/28 08:05:05 tls Exp $");
#else
static const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed";
static const char rcsid[] = "@(#)Id: ip_state.c,v 2.186.2.100 2010/01/31 16:22:55 darrenr Exp";
@ -228,7 +228,7 @@ int fr_stateinit()
* XXX - ips_seed[X] should be a random number of sorts.
*/
#if !defined(NEED_LOCAL_RAND) && defined(_KERNEL)
ips_seed[i] = arc4random();
ips_seed[i] = cprng_fast32();
#else
ips_seed[i] = ((u_long)ips_seed + i) * fr_statesize;
ips_seed[i] += tv.tv_sec;

View File

@ -1,4 +1,4 @@
/* $NetBSD: pf_ioctl.c,v 1.45 2011/08/30 19:05:12 jmcneill Exp $ */
/* $NetBSD: pf_ioctl.c,v 1.46 2011/11/28 08:05:05 tls Exp $ */
/* $OpenBSD: pf_ioctl.c,v 1.182 2007/06/24 11:17:13 mcbride Exp $ */
/*
@ -37,7 +37,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pf_ioctl.c,v 1.45 2011/08/30 19:05:12 jmcneill Exp $");
__KERNEL_RCSID(0, "$NetBSD: pf_ioctl.c,v 1.46 2011/11/28 08:05:05 tls Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@ -66,6 +66,7 @@ __KERNEL_RCSID(0, "$NetBSD: pf_ioctl.c,v 1.45 2011/08/30 19:05:12 jmcneill Exp $
#include <sys/lwp.h>
#include <sys/kauth.h>
#include <sys/module.h>
#include <sys/cprng.h>
#endif /* __NetBSD__ */
#include <net/if.h>
@ -300,7 +301,7 @@ pfattach(int num)
pf_status.debug = PF_DEBUG_URGENT;
/* XXX do our best to avoid a conflict */
pf_status.hostid = arc4random();
pf_status.hostid = cprng_fast32();
/* require process context to purge states, so perform in a thread */
#ifdef __NetBSD__
@ -3176,7 +3177,7 @@ pfioctl(dev_t dev, u_long cmd, void *addr, int flags, struct lwp *l)
u_int32_t *hid = (u_int32_t *)addr;
if (*hid == 0)
pf_status.hostid = arc4random();
pf_status.hostid = cprng_fast32();
else
pf_status.hostid = *hid;
break;

View File

@ -1,4 +1,4 @@
/* $NetBSD: pf_norm.c,v 1.25 2011/08/29 09:50:04 jmcneill Exp $ */
/* $NetBSD: pf_norm.c,v 1.26 2011/11/28 08:05:05 tls Exp $ */
/* $OpenBSD: pf_norm.c,v 1.109 2007/05/28 17:16:39 henning Exp $ */
/*
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pf_norm.c,v 1.25 2011/08/29 09:50:04 jmcneill Exp $");
__KERNEL_RCSID(0, "$NetBSD: pf_norm.c,v 1.26 2011/11/28 08:05:05 tls Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@ -47,6 +47,7 @@ __KERNEL_RCSID(0, "$NetBSD: pf_norm.c,v 1.25 2011/08/29 09:50:04 jmcneill Exp $"
#ifdef __NetBSD__
#include <sys/rnd.h>
#include <sys/cprng.h>
#else
#include <dev/rndvar.h>
#endif /* !__NetBSD__ */
@ -1446,7 +1447,7 @@ pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
src->scrub->pfss_flags |=
PFSS_TIMESTAMP;
src->scrub->pfss_ts_mod =
htonl(arc4random());
htonl(cprng_fast32());
/* note PFSS_PAWS not set yet */
memcpy(&tsval, &opt[2],

View File

@ -1,4 +1,4 @@
/* $NetBSD: arc4random.c,v 1.26 2011/11/27 00:09:04 tsutsui Exp $ */
/* $NetBSD: arc4random.c,v 1.27 2011/11/28 08:05:05 tls Exp $ */
/*-
* Copyright (c) 2002, 2011 The NetBSD Foundation, Inc.
@ -112,7 +112,8 @@ kmutex_t arc4_mtx;
static inline u_int8_t arc4_randbyte(void);
static inline void arc4randbytes_unlocked(void *, size_t);
void _arc4randbytes(void *, size_t);
uint32_t _arc4random(void);
static inline void
arc4_swap(u_int8_t *a, u_int8_t *b)
@ -283,18 +284,6 @@ arc4_randbyte(void)
return arc4_sbox[arc4_t];
}
u_int32_t
arc4random(void)
{
u_int32_t ret;
u_int8_t *retc;
retc = (u_int8_t *)&ret;
arc4randbytes(retc, sizeof(u_int32_t));
return ret;
}
static inline void
arc4randbytes_unlocked(void *p, size_t len)
{
@ -306,7 +295,7 @@ arc4randbytes_unlocked(void *p, size_t len)
}
void
arc4randbytes(void *p, size_t len)
_arc4randbytes(void *p, size_t len)
{
/* Initialize array if needed. */
if (!arc4_initialized) {
@ -323,3 +312,15 @@ arc4randbytes(void *p, size_t len)
arc4_randrekey(NULL);
}
}
u_int32_t
_arc4random(void)
{
u_int32_t ret;
u_int8_t *retc;
retc = (u_int8_t *)&ret;
_arc4randbytes(retc, sizeof(u_int32_t));
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: libkern.h,v 1.103 2011/10/01 16:06:24 chs Exp $ */
/* $NetBSD: libkern.h,v 1.104 2011/11/28 08:05:06 tls Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -313,8 +313,6 @@ char *intoa(u_int32_t);
void *memchr(const void *, int, size_t);
void *memmove(void *, const void *, size_t);
int pmatch(const char *, const char *, const char **);
u_int32_t arc4random(void);
void arc4randbytes(void *, size_t);
#ifndef SMALL_RANDOM
void srandom(unsigned long);
char *initstate(unsigned long, char *, size_t);

View File

@ -1,4 +1,4 @@
/* $NetBSD: nfs_vnops.c,v 1.292 2011/09/27 01:05:08 christos Exp $ */
/* $NetBSD: nfs_vnops.c,v 1.293 2011/11/28 08:05:06 tls Exp $ */
/*
* Copyright (c) 1989, 1993
@ -39,7 +39,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nfs_vnops.c,v 1.292 2011/09/27 01:05:08 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: nfs_vnops.c,v 1.293 2011/11/28 08:05:06 tls Exp $");
#ifdef _KERNEL_OPT
#include "opt_nfs.h"
@ -68,6 +68,7 @@ __KERNEL_RCSID(0, "$NetBSD: nfs_vnops.c,v 1.292 2011/09/27 01:05:08 christos Exp
#include <sys/stat.h>
#include <sys/unistd.h>
#include <sys/kauth.h>
#include <sys/cprng.h>
#include <uvm/uvm_extern.h>
#include <uvm/uvm.h>
@ -1638,8 +1639,8 @@ again:
if (excl_mode == NFSV3CREATE_EXCLUSIVE) {
*tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
*tl++ = arc4random();
*tl = arc4random();
*tl++ = cprng_fast32();
*tl = cprng_fast32();
} else {
*tl = txdr_unsigned(excl_mode);
nfsm_v3attrbuild(vap, false);

View File

@ -1,4 +1,4 @@
/* $NetBSD: cryptosoft.c,v 1.38 2011/06/07 15:57:51 drochner Exp $ */
/* $NetBSD: cryptosoft.c,v 1.39 2011/11/28 08:05:06 tls Exp $ */
/* $FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $ */
/* $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $ */
@ -24,7 +24,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.38 2011/06/07 15:57:51 drochner Exp $");
__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.39 2011/11/28 08:05:06 tls Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -106,7 +106,7 @@ swcr_encdec(struct cryptodesc *crd, const struct swcr_data *sw, void *bufv,
for (i = 0;
i + sizeof (u_int32_t) <= EALG_MAX_BLOCK_LEN;
i += sizeof (u_int32_t)) {
u_int32_t temp = arc4random();
u_int32_t temp = cprng_fast32();
memcpy(iv + i, &temp, sizeof(u_int32_t));
}
@ -116,7 +116,7 @@ swcr_encdec(struct cryptodesc *crd, const struct swcr_data *sw, void *bufv,
* what arc4random() returns ?
*/
if (EALG_MAX_BLOCK_LEN % sizeof (u_int32_t) != 0) {
u_int32_t temp = arc4random();
u_int32_t temp = cprng_fast32();
bcopy (&temp, iv + i,
EALG_MAX_BLOCK_LEN - i);
@ -628,7 +628,7 @@ swcr_combined(struct cryptop *crp, int outtype)
} else if (exf->reinit)
exf->reinit(swe->sw_kschedule, 0, iv);
else
arc4randbytes(iv, ivlen);
cprng_fast(iv, ivlen);
/* Do we need to write the IV */
if (!(crde->crd_flags & CRD_F_IV_PRESENT))

View File

@ -1,4 +1,4 @@
/* $NetBSD: cryptosoft_xform.c,v 1.24 2011/06/07 15:57:52 drochner Exp $ */
/* $NetBSD: cryptosoft_xform.c,v 1.25 2011/11/28 08:05:06 tls Exp $ */
/* $FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $ */
/* $OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $ */
@ -40,7 +40,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(1, "$NetBSD: cryptosoft_xform.c,v 1.24 2011/06/07 15:57:52 drochner Exp $");
__KERNEL_RCSID(1, "$NetBSD: cryptosoft_xform.c,v 1.25 2011/11/28 08:05:06 tls Exp $");
#include <crypto/blowfish/blowfish.h>
#include <crypto/cast128/cast128.h>
@ -744,7 +744,7 @@ aes_ctr_setkey(u_int8_t **sched, const u_int8_t *key, int len)
}
memcpy(ctx->ac_block, key + len - AESCTR_NONCESIZE, AESCTR_NONCESIZE);
/* random start value for simple counter */
arc4randbytes(&ctx->ivgenctx.lastiv, sizeof(ctx->ivgenctx.lastiv));
cprng_fast(&ctx->ivgenctx.lastiv, sizeof(ctx->ivgenctx.lastiv));
*sched = (void *)ctx;
return 0;
}
@ -808,7 +808,7 @@ aes_gmac_setkey(u_int8_t **sched, const u_int8_t *key, int len)
return ENOMEM;
/* random start value for simple counter */
arc4randbytes(&ctx->ivgenctx.lastiv, sizeof(ctx->ivgenctx.lastiv));
cprng_fast(&ctx->ivgenctx.lastiv, sizeof(ctx->ivgenctx.lastiv));
*sched = (void *)ctx;
return 0;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: rumpuser.h,v 1.70 2011/08/29 20:48:36 joerg Exp $ */
/* $NetBSD: rumpuser.h,v 1.71 2011/11/28 08:05:06 tls Exp $ */
/*
* Copyright (c) 2007-2011 Antti Kantee. All Rights Reserved.
@ -112,6 +112,8 @@ int rumpuser_dprintf(const char *, ...);
int rumpuser_getnhostcpu(void);
uint32_t rumpuser_arc4random(void);
/* rumpuser_pth */
void rumpuser_thrinit(kernel_lockfn, kernel_unlockfn, int);
__dead void rumpuser_biothread(void *);

View File

@ -1,4 +1,4 @@
/* $NetBSD: cprng_stub.c,v 1.2 2011/11/21 13:42:37 tsutsui Exp $ */
/* $NetBSD: cprng_stub.c,v 1.3 2011/11/28 08:05:07 tls Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
@ -40,6 +40,8 @@
#include <sys/cprng.h>
#include <rump/rumpuser.h>
/*
* This is all stubbed out because of rump build dependency issues I
* cannot fix. One is more-or-less caused by the longstanding bogosity
@ -89,3 +91,41 @@ void cprng_strong_destroy(cprng_strong_t *c)
memset(c, 0, sizeof(*c));
kmem_free(c, sizeof(*c));
}
size_t
cprng_fast(void *p, size_t len)
{
uint8_t *resp, *pchar = (uint8_t *)p;
uint32_t res;
size_t i;
do {
res = rumpuser_arc4random();
resp = (uint8_t *)&res;
for (i = 0; i < sizeof(res); i++) {
*pchar++ = resp[i];
if (pchar == (uint8_t *)p + len) {
return len;
}
}
} while(1);
}
uint32_t
cprng_fast32(void)
{
return rumpuser_arc4random();
}
uint64_t
cprng_fast64(void)
{
uint64_t ret;
uint32_t *ret32;
ret32 = (uint32_t *)&ret;
ret32[0] = rumpuser_arc4random();
ret32[1] = rumpuser_arc4random();
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: cprng.h,v 1.1 2011/11/19 22:51:31 tls Exp $ */
/* $NetBSD: cprng.h,v 1.2 2011/11/28 08:05:07 tls Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
@ -42,26 +42,38 @@
*/
#define CPRNG_MAX_LEN 524288
#ifndef _RUMPKERNEL
/*
* We do not want an arc4random() prototype available to anyone.
*/
void _arc4randbytes(void *, size_t);
uint32_t _arc4random(void);
static inline size_t
cprng_fast(void *p, size_t len)
{
arc4randbytes(p, len);
_arc4randbytes(p, len);
return len;
}
static inline uint32_t
cprng_fast32(void)
{
return arc4random();
return _arc4random();
}
static inline uint64_t
cprng_fast64(void)
{
uint64_t r;
arc4randbytes(&r, sizeof(r));
_arc4randbytes(&r, sizeof(r));
return r;
}
#else
size_t cprng_fast(void *, size_t);
uint32_t cprng_fast32(void);
uint64_t cprng_fast64(void);
#endif
typedef struct _cprng_strong {
kmutex_t mtx;

View File

@ -1,4 +1,4 @@
/* $NetBSD: param.h,v 1.396 2011/11/23 23:07:36 jmcneill Exp $ */
/* $NetBSD: param.h,v 1.397 2011/11/28 08:05:07 tls Exp $ */
/*-
* Copyright (c) 1982, 1986, 1989, 1993
@ -63,7 +63,7 @@
* 2.99.9 (299000900)
*/
#define __NetBSD_Version__ 599005700 /* NetBSD 5.99.57 */
#define __NetBSD_Version__ 599005800 /* NetBSD 5.99.58 */
#define __NetBSD_Prereq__(M,m,p) (((((M) * 100000000) + \
(m) * 1000000) + (p) * 100) <= __NetBSD_Version__)

View File

@ -1,4 +1,4 @@
/* $NetBSD: ffs_alloc.c,v 1.129 2011/09/20 14:01:32 chs Exp $ */
/* $NetBSD: ffs_alloc.c,v 1.130 2011/11/28 08:05:07 tls Exp $ */
/*-
* Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@ -70,7 +70,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.129 2011/09/20 14:01:32 chs Exp $");
__KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.130 2011/11/28 08:05:07 tls Exp $");
#if defined(_KERNEL_OPT)
#include "opt_ffs.h"
@ -81,6 +81,7 @@ __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.129 2011/09/20 14:01:32 chs Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/cprng.h>
#include <sys/fstrans.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
@ -1388,7 +1389,7 @@ gotit:
* Don't bother to swap, it's supposed to be
* random, after all.
*/
dp2->di_gen = (arc4random() & INT32_MAX) / 2 + 1;
dp2->di_gen = (cprng_fast32() & INT32_MAX) / 2 + 1;
dp2++;
}
initediblk += INOPB(fs);