Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 6 Oct 2000 11:23:15 -0700
From:      Alfred Perlstein <bright@wintelcom.net>
To:        Warner Losh <imp@village.org>
Cc:        Dmitry Valdov <dv@dv.ru>, Ruslan Ermilov <ru@sunbay.com>, Fernando Schapachnik <fpscha@via-net-works.net.ar>, security@FreeBSD.ORG
Subject:   Re: HERT advisory: FreeBSD IP Spoofing (fwd)
Message-ID:  <20001006112315.A266@fw.wintelcom.net>
In-Reply-To: <20001006103657.D27736@fw.wintelcom.net>; from bright@wintelcom.net on Fri, Oct 06, 2000 at 10:36:57AM -0700
References:  <Pine.BSF.3.95q.1001006163649.9906B-100000@xkis.kis.ru> <200010061713.LAA09674@harmony.village.org> <20001006103657.D27736@fw.wintelcom.net>

next in thread | previous in thread | raw e-mail | index | archive | help
* Alfred Perlstein <bright@wintelcom.net> [001006 10:37] wrote:
> * Warner Losh <imp@village.org> [001006 10:15] wrote:
> > In message <Pine.BSF.3.95q.1001006163649.9906B-100000@xkis.kis.ru> Dmitry Valdov writes:
> > : And how about 2.x branch? 
> > 
> > I don't think anybody has backported it yet.
> 
> I'm on it.

I just booted a box, things look OK, please review as this is a bit
more complex than the other patches because of a lack of arc4 random
in 2.2.x

Index: i386/conf/files.i386
===================================================================
RCS file: /home/ncvs/src/sys/i386/conf/Attic/files.i386,v
retrieving revision 1.141.2.25
diff -u -u -r1.141.2.25 files.i386
--- i386/conf/files.i386	1999/09/05 08:10:53	1.141.2.25
+++ i386/conf/files.i386	2000/10/06 15:56:50
@@ -256,6 +256,7 @@
 libkern/strncpy.c		standard
 libkern/udivdi3.c		standard
 libkern/umoddi3.c		standard
+libkern/arc4random.c		standard
 gnu/i386/fpemul/div_small.s	optional	gpl_math_emulate
 gnu/i386/fpemul/errors.c	optional	gpl_math_emulate
 gnu/i386/fpemul/fpu_arith.c	optional	gpl_math_emulate
Index: libkern/arc4random.c
===================================================================
RCS file: arc4random.c
diff -N arc4random.c
--- /dev/null	Fri Oct  6 02:19:19 2000
+++ arc4random.c	Fri Oct  6 09:18:44 2000
@@ -0,0 +1,111 @@
+/*-
+ * THE BEER-WARE LICENSE
+ *
+ * <dan@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
+ * can do whatever you want with this stuff.  If we meet some day, and you
+ * think this stuff is worth it, you can buy me a beer in return.
+ *
+ * Dan Moschuk
+ *
+ * $FreeBSD: src/sys/libkern/arc4random.c,v 1.6 2000/09/11 19:34:04 jhb Exp $
+ */
+
+#include <sys/types.h>
+#include <sys/libkern.h>
+
+#define	ARC4_MAXRUNS 64
+
+static u_int8_t arc4_i, arc4_j;
+static int arc4_initialized = 0;
+static int arc4_numruns = 0;
+static u_int8_t arc4_sbox[256];
+
+static __inline void
+arc4_swap(u_int8_t *a, u_int8_t *b)
+{
+	u_int8_t c;
+
+	c = *a;
+	*a = *b;
+	*b = c;
+}	
+
+/*
+ * Stir our S-box.
+ */
+static void
+arc4_randomstir (void)
+{
+	u_int8_t key[256];
+	int r, n;
+
+	/* r = read_random(key, sizeof(key)); */
+	r = 0; /* XXX MarkM - revisit this when /dev/random is done */
+	/* if r == 0 || -1, just use what was on the stack */
+	if (r > 0)
+	{
+		for (n = r; n < sizeof(key); n++)
+			key[n] = key[n % r];
+	}
+
+	for (n = 0; n < 256; n++)
+	{
+		arc4_j = (arc4_j + arc4_sbox[n] + key[n]) % 256;
+		arc4_swap(&arc4_sbox[n], &arc4_sbox[arc4_j]);
+	}
+}
+
+/*
+ * Initialize our S-box to its beginning defaults.
+ */
+static void
+arc4_init(void)
+{
+	int n;
+
+	arc4_i = arc4_j = 0;
+	for (n = 0; n < 256; n++)
+		arc4_sbox[n] = (u_int8_t) n;
+
+	arc4_randomstir();
+	arc4_initialized = 1;
+}
+
+/*
+ * Generate a random byte.
+ */
+static u_int8_t
+arc4_randbyte(void)
+{
+	u_int8_t arc4_t;
+
+	arc4_i = (arc4_i + 1) % 256;
+	arc4_j = (arc4_j + arc4_sbox[arc4_i]) % 256;
+
+	arc4_swap(&arc4_sbox[arc4_i], &arc4_sbox[arc4_j]);
+
+	arc4_t = (arc4_sbox[arc4_i] + arc4_sbox[arc4_j]) % 256;
+	return arc4_sbox[arc4_t];
+}
+
+u_int32_t
+arc4random(void)
+{
+	u_int32_t ret;
+
+	/* Initialize array if needed. */
+	if (!arc4_initialized)
+		arc4_init();
+	if (++arc4_numruns > ARC4_MAXRUNS)
+	{
+		arc4_randomstir();
+		arc4_numruns = 0;
+	}
+
+	ret = arc4_randbyte();
+	ret |= arc4_randbyte() << 8;
+	ret |= arc4_randbyte() << 16;
+	ret |= arc4_randbyte() << 24;
+
+	return ret;
+}
Index: netinet/tcp_seq.h
===================================================================
RCS file: /home/ncvs/src/sys/netinet/tcp_seq.h,v
retrieving revision 1.6.4.1
diff -u -u -r1.6.4.1 tcp_seq.h
--- netinet/tcp_seq.h	1999/09/05 08:18:43	1.6.4.1
+++ netinet/tcp_seq.h	2000/10/06 15:57:39
@@ -91,7 +91,7 @@
  * number in the range [0-0x3ffff] that is hard to predict.
  */
 #ifndef tcp_random18
-#define	tcp_random18()	((random() >> 14) & 0x3ffff)
+#define	tcp_random18()	(arc4random() & 0x3ffff)
 #endif
 #define	TCP_ISSINCR	(122*1024 + tcp_random18())
 
Index: netinet/tcp_subr.c
===================================================================
RCS file: /home/ncvs/src/sys/netinet/tcp_subr.c,v
retrieving revision 1.31.2.5
diff -u -u -r1.31.2.5 tcp_subr.c
--- netinet/tcp_subr.c	1999/09/05 08:18:43	1.31.2.5
+++ netinet/tcp_subr.c	2000/10/06 15:58:29
@@ -104,7 +104,7 @@
 tcp_init()
 {
 
-	tcp_iss = random();	/* wrong, but better than a constant */
+	tcp_iss = arc4random();	/* wrong, but better than a constant */
 	tcp_ccgen = 1;
 	tcp_cleartaocache();
 	LIST_INIT(&tcb);
Index: sys/libkern.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/libkern.h,v
retrieving revision 1.14.2.1
diff -u -u -r1.14.2.1 libkern.h
--- sys/libkern.h	1999/09/05 08:22:31	1.14.2.1
+++ sys/libkern.h	2000/10/06 16:19:38
@@ -61,6 +61,7 @@
 static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
 
 /* Prototypes for non-quad routines. */
+u_int32_t	arc4random __P((void));
 int	 bcmp __P((const void *, const void *, size_t));
 #ifndef HAVE_INLINE_FFS
 int	 ffs __P((int));



thanks,
-- 
-Alfred Perlstein - [bright@wintelcom.net|alfred@freebsd.org]
"I have the heart of a child; I keep it in a jar on my desk."


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-security" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20001006112315.A266>