Subject: Re: rpc xid randomness
To: David Laight <david@l8s.co.uk>
From: Jun-ichiro itojun Hagino <itojun@iijlab.net>
List: tech-userlevel
Date: 09/06/2003 22:37:28
>>> > does it have any negative sideeffects? i mean, is there any code that
>>> > relies on xid being generated from timestamp?
>>>
>>> clnt_vc.c: call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
>>> how important "disrupt" is?
>>Does it even matter if RPC xids are predictable?
>>IIRC They are only used to tie responses to commands in the client.
>>So what is really, really important is that there aren't two outstanding
>>requests with the same xid - and I'm not even sure the code above does that!
> if possibility of reuse is the issue, the original code (uses
> time.tv_{u,}sec) is busted already.
> i'll need to bring in something like sys/netinet6/ip6_id.c then.
this should avoid reuse.
itojun
Index: rpc/clnt_bcast.c
===================================================================
RCS file: /cvsroot/src/lib/libc/rpc/clnt_bcast.c,v
retrieving revision 1.10
diff -u -r1.10 clnt_bcast.c
--- rpc/clnt_bcast.c 2003/01/18 11:29:04 1.10
+++ rpc/clnt_bcast.c 2003/09/06 13:36:29
@@ -256,7 +256,6 @@
XDR xdr_stream; /* XDR stream */
XDR *xdrs = &xdr_stream;
struct rpc_msg msg; /* RPC message */
- struct timeval t;
char *outbuf = NULL; /* Broadcast msg buffer */
char *inbuf = NULL; /* Reply buf */
int inlen;
@@ -380,8 +379,7 @@
}
/* Serialize all the arguments which have to be sent */
- (void) gettimeofday(&t, NULL);
- msg.rm_xid = __RPC_GETXID(&t);
+ msg.rm_xid = __RPC_GETXID();
msg.rm_direction = CALL;
msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
msg.rm_call.cb_prog = RPCBPROG;
Index: rpc/clnt_dg.c
===================================================================
RCS file: /cvsroot/src/lib/libc/rpc/clnt_dg.c,v
retrieving revision 1.10
diff -u -r1.10 clnt_dg.c
--- rpc/clnt_dg.c 2003/06/06 00:48:45 1.10
+++ rpc/clnt_dg.c 2003/09/06 13:36:29
@@ -162,7 +162,6 @@
{
CLIENT *cl = NULL; /* client handle */
struct cu_data *cu = NULL; /* private data */
- struct timeval now;
struct rpc_msg call_msg;
#ifdef _REENTRANT
sigset_t mask;
@@ -252,8 +251,7 @@
cu->cu_total.tv_usec = -1;
cu->cu_sendsz = sendsz;
cu->cu_recvsz = recvsz;
- (void) gettimeofday(&now, NULL);
- call_msg.rm_xid = __RPC_GETXID(&now);
+ call_msg.rm_xid = __RPC_GETXID();
call_msg.rm_call.cb_prog = program;
call_msg.rm_call.cb_vers = version;
xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
Index: rpc/clnt_vc.c
===================================================================
RCS file: /cvsroot/src/lib/libc/rpc/clnt_vc.c,v
retrieving revision 1.9
diff -u -r1.9 clnt_vc.c
--- rpc/clnt_vc.c 2003/01/18 11:29:04 1.9
+++ rpc/clnt_vc.c 2003/09/06 13:36:30
@@ -165,9 +165,7 @@
{
CLIENT *h;
struct ct_data *ct = NULL;
- struct timeval now;
struct rpc_msg call_msg;
- static u_int32_t disrupt;
#ifdef _REENTRANT
sigset_t mask;
#endif
@@ -178,9 +176,6 @@
_DIAGASSERT(raddr != NULL);
- if (disrupt == 0)
- disrupt = (u_int32_t)(long)raddr;
-
h = mem_alloc(sizeof(*h));
if (h == NULL) {
warnx("clnt_vc_create: out of memory");
@@ -273,8 +268,7 @@
/*
* Initialize call message
*/
- (void)gettimeofday(&now, NULL);
- call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
+ call_msg.rm_xid = __RPC_GETXID();
call_msg.rm_direction = CALL;
call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
call_msg.rm_call.cb_prog = (u_int32_t)prog;
Index: rpc/rpc_generic.c
===================================================================
RCS file: /cvsroot/src/lib/libc/rpc/rpc_generic.c,v
retrieving revision 1.13
diff -u -r1.13 rpc_generic.c
--- rpc/rpc_generic.c 2003/06/07 07:41:41 1.13
+++ rpc/rpc_generic.c 2003/09/06 13:36:30
@@ -861,3 +861,188 @@
return 0;
}
+
+/* $OpenBSD: ip_id.c,v 1.6 2002/03/15 18:19:52 millert Exp $ */
+
+/*
+ * Copyright 1998 Niels Provos <provos@citi.umich.edu>
+ * All rights reserved.
+ *
+ * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
+ * such a mathematical system to generate more random (yet non-repeating)
+ * ids to solve the resolver/named problem. But Niels designed the
+ * actual system based on the constraints.
+ *
+ * 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. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Niels Provos.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+ */
+
+/*
+ * seed = random 31bit
+ * n = prime, g0 = generator to n,
+ * j = random so that gcd(j,n-1) == 1
+ * g = g0^j mod n will be a generator again.
+ *
+ * X[0] = random seed.
+ * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
+ * with a = 7^(even random) mod m,
+ * b = random with gcd(b,m) == 1
+ * m = 1836660096 and a maximal period of m-1.
+ *
+ * The transaction id is determined by:
+ * id[n] = seed xor (g^X[n] mod n)
+ *
+ * Effectivly the id is restricted to the lower 31 bits, thus
+ * yielding two different cycles by toggling the msb on and off.
+ * This avoids reuse issues caused by reseeding.
+ */
+
+#define RU_OUT 180 /* Time after wich will be reseeded */
+#define RU_MAX 1000000000 /* Uniq cycle, avoid blackjack prediction */
+#define RU_GEN 2 /* Starting generator */
+#define RU_N 2147483629 /* RU_N-1 = 2^2*3^2*59652323 */
+#define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */
+#define RU_M 1836660096 /* RU_M = 2^7*3^15 - don't change */
+
+#define PFAC_N 3
+const static u_int32_t pfacts[PFAC_N] = {
+ 2,
+ 3,
+ 59652323
+};
+
+static u_int32_t ru_x;
+static u_int32_t ru_seed, ru_seed2;
+static u_int32_t ru_a, ru_b;
+static u_int32_t ru_g;
+static u_int32_t ru_counter = 0;
+static u_int32_t ru_msb = 0;
+static long ru_reseed;
+
+static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);
+static void initid(void);
+
+/*
+ * Do a fast modular exponation, returned value will be in the range
+ * of 0 - (mod-1)
+ */
+static u_int32_t
+pmod(u_int32_t gen, u_int32_t exp, u_int32_t mod)
+{
+ u_int64_t s, t, u;
+
+ s = 1;
+ t = gen;
+ u = exp;
+
+ while (u) {
+ if (u & 1)
+ s = (s * t) % mod;
+ u >>= 1;
+ t = (t * t) % mod;
+ }
+ return ((u_int32_t)s & 0xffffffff);
+}
+
+/*
+ * Initalizes the seed and chooses a suitable generator. Also toggles
+ * the msb flag. The msb flag is used to generate two distinct
+ * cycles of random numbers and thus avoiding reuse of ids.
+ *
+ * This function is called from id_randomid() when needed, an
+ * application does not have to worry about it.
+ */
+static void
+initid(void)
+{
+ u_int32_t j, i;
+ int noprime = 1;
+ struct timeval tv;
+
+ ru_x = arc4random() % RU_M;
+
+ /* 15 bits of random seed */
+ ru_seed = arc4random() & INT32_MAX;
+ ru_seed2 = arc4random() & INT32_MAX;;
+
+ /* Determine the LCG we use */
+ ru_b = arc4random() | 1;
+ ru_a = pmod(RU_AGEN, arc4random() & (~1U), RU_M);
+ while (ru_b % 3 == 0)
+ ru_b += 2;
+
+ j = arc4random() % RU_N;
+
+ /*
+ * Do a fast gcd(j,RU_N-1), so we can find a j with
+ * gcd(j, RU_N-1) == 1, giving a new generator for
+ * RU_GEN^j mod RU_N
+ */
+ while (noprime) {
+ for (i = 0; i < PFAC_N; i++)
+ if (j % pfacts[i] == 0)
+ break;
+
+ if (i >= PFAC_N)
+ noprime = 0;
+ else
+ j = (j + 1) % RU_N;
+ }
+
+ ru_g = pmod(RU_GEN, j, RU_N);
+ ru_counter = 0;
+
+ gettimeofday(&tv, NULL);
+ ru_reseed = tv.tv_sec + RU_OUT;
+ ru_msb = ru_msb ? 0 : 0x80000000;
+}
+
+u_int32_t
+__rpc_getxid(void)
+{
+ int i, n;
+ u_int32_t tmp;
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+ if (ru_counter >= RU_MAX || tv.tv_sec > ru_reseed)
+ initid();
+
+ tmp = arc4random();
+
+ /* Skip a random number of ids */
+ n = tmp & 0x3; tmp = tmp >> 2;
+ if (ru_counter + n >= RU_MAX)
+ initid();
+
+ for (i = 0; i <= n; i++) {
+ /* Linear Congruential Generator */
+ ru_x = (ru_a * ru_x + ru_b) % RU_M;
+ }
+
+ ru_counter += i;
+
+ return (ru_seed ^ pmod(ru_g, ru_seed2 ^ ru_x,RU_N)) | ru_msb;
+}
Index: rpc/rpc_internal.h
===================================================================
RCS file: /cvsroot/src/lib/libc/rpc/rpc_internal.h,v
retrieving revision 1.2
diff -u -r1.2 rpc_internal.h
--- rpc/rpc_internal.h 2003/01/18 11:29:05 1.2
+++ rpc/rpc_internal.h 2003/09/06 13:36:30
@@ -34,8 +34,8 @@
char *_get_next_token __P((char *, int));
-#define __RPC_GETXID(now) ((u_int32_t)getpid() ^ (u_int32_t)(now)->tv_sec ^ \
- (u_int32_t)(now)->tv_usec)
+u_int32_t __rpc_getxid __P((void));
+#define __RPC_GETXID() (__rpc_getxid())
extern SVCXPRT **__svc_xports;
extern int __svc_maxrec;