Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop out-of-bounds read, and ensure all bytes are used, in FuncIntRandomMT #2716

Merged
merged 2 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/intfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,34 @@ void initGRMT(UInt4 *mt, UInt4 s)
mt[624] = mti;
}

// Read s[pos], returning 0 if pos is past the error of the array
static inline UChar checkedReadChar(UChar * s, UInt4 pos, UInt4 len)
{
if (pos < len)
return s[pos];
else
return 0;
}

/* to read a seed string independently of endianness */
static inline UInt4 uint4frombytes(UChar *s)
static inline UInt4 uint4frombytes(UChar * s, UInt4 pos, UInt4 len)
{
UInt4 res;
res = s[3]; res <<= 8;
res += s[2]; res <<= 8;
res += s[1]; res <<= 8;
res += s[0];
res = checkedReadChar(s, pos + 3, len);
res <<= 8;
res += checkedReadChar(s, pos + 2, len);
res <<= 8;
res += checkedReadChar(s, pos + 1, len);
res <<= 8;
res += checkedReadChar(s, pos + 0, len);
return res;
}

Obj FuncInitRandomMT( Obj self, Obj initstr)
{
Obj str;
UChar *init_key;
UInt4 *mt, key_length, i, j, k, N=624;
UInt4 *mt, key_length, byte_key_length, i, j, k, N = 624;

/* check the seed, given as string */
while (! IsStringConv(initstr)) {
Expand All @@ -97,15 +109,16 @@ Obj FuncInitRandomMT( Obj self, Obj initstr)
i=1; j=0;
/* Do not set these up until all garbage collection is done */
init_key = CHARS_STRING(initstr);
key_length = GET_LEN_STRING(initstr) / 4;
byte_key_length = GET_LEN_STRING(initstr);
key_length = byte_key_length / 4;
k = (N>key_length ? N : key_length);
for (; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ uint4frombytes(init_key+4*j) + j;
mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL)) +
uint4frombytes(init_key, 4 * j, byte_key_length) + j;
mt[i] &= 0xffffffffUL;
i++; j++;
if (i>=N) { mt[0] = mt[N-1]; i=1; }
if (j>=key_length) j=0;
if (4 * j >= byte_key_length) j=0;
}
for (k=N-1; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - i;
Expand Down
25 changes: 25 additions & 0 deletions tst/testinstall/random.tst
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,30 @@ gap> randomTest([1,-6,"cheese", Group(())], Random);
gap> randomTest(PadicExtensionNumberFamily(3, 5, [1,1,1], [1,1]), Random, function(x,y) return IsPadicExtensionNumber(x); end);
gap> randomTest(PurePadicNumberFamily(2,20), Random, function(x,y) return IsPurePadicNumber(x); end);

# Test initialising random number generator
# We take a string and 0-pad it to 4 bytes
gap> getOneInt := function(str)
> Init(GlobalMersenneTwister, str);
> return Random([1..100000]);
> end;;
gap> getOneInt("") = getOneInt("\000");
true
gap> getOneInt("a") = getOneInt("b");
false
gap> getOneInt("") = getOneInt("\000\000\000\000");
true
gap> getOneInt("a") = getOneInt("a\000");
true
gap> getOneInt("a") = getOneInt("a\000\000\000");
true
gap> getOneInt("a") = getOneInt("a\000\000\000\000");
false
gap> getOneInt("a") = getOneInt("a\000\000\000a\000\000\000");
false
gap> getOneInt("a\000\000\000a\000\000\000") = getOneInt("a\000\000\000a");
true
gap> getOneInt("a") = getOneInt("a\000\000\000b");
false

#
gap> STOP_TEST("random.tst", 1);