From 678c1255c92eed9c2c7564d340a5563b17395158 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 1 Feb 2012 19:57:15 -0200 Subject: random seed used in the hash of all strings to avoid intentional collisions --- lstate.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'lstate.c') diff --git a/lstate.c b/lstate.c index bc13897b..5031a0a1 100644 --- a/lstate.c +++ b/lstate.c @@ -1,11 +1,12 @@ /* -** $Id: lstate.c,v 2.91 2011/08/23 17:24:34 roberto Exp roberto $ +** $Id: lstate.c,v 2.92 2011/10/03 17:54:25 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ #include +#include #define lstate_c #define LUA_CORE @@ -41,6 +42,17 @@ #define MEMERRMSG "not enough memory" +/* +** a macro to help the creation of a unique random seed when a state is +** created; the seed is used to randomize hashes. +*/ +#if !defined(luai_makeseed) +#include +#define luai_makeseed(L) cast(size_t, time(NULL)) +#endif + + + /* ** thread state + extra space */ @@ -65,6 +77,28 @@ typedef struct LG { #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) +/* +** Compute an initial seed as random as possible. In ANSI, rely on +** Address Space Layour Randomization (if present) to increase +** randomness.. +*/ +#define addbuff(b,p,e) \ + { size_t t = cast(size_t, e); \ + memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } + +static unsigned int makeseed (lua_State *L) { + char buff[4 * sizeof(size_t)]; + unsigned int h = luai_makeseed(); + int p = 0; + addbuff(buff, p, L); /* heap variable */ + addbuff(buff, p, &h); /* local variable */ + addbuff(buff, p, luaO_nilobject); /* global variable */ + addbuff(buff, p, &lua_newstate); /* public function */ + lua_assert(p == sizeof(buff)); + return luaS_hash(buff, p, h); +} + + /* ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) ** invariant @@ -242,6 +276,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->frealloc = f; g->ud = ud; g->mainthread = L; + g->seed = makeseed(L); g->uvhead.u.l.prev = &g->uvhead; g->uvhead.u.l.next = &g->uvhead; g->gcrunning = 0; /* no GC while building state */ -- cgit v1.2.3-55-g6feb