Skip to content

Commit 1a428e5

Browse files
authored
Fix GH-21731: Random\Engine\Xoshiro256StarStar::__unserialize() accepts all-zero state (#21732)
The constructor rejects a seed that would leave the internal state all zero, because xoshiro256** with zero state produces 0 on every call forever. The unserialize callback didn't check the same invariant. A caller feeding a crafted serialized payload through __unserialize() ended up with a live engine that returned 0 from every operation. Match the constructor: reject the all-zero state from the unserialize callback too. The Mt19937-aliased __unserialize() wrapper turns the false return into the standard "Invalid serialization data" exception. Closes GH-21731
1 parent 5e6b90e commit 1a428e5

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ PHP NEWS
3131
- OpenSSL:
3232
. Fix a bunch of memory leaks and crashes on edge cases. (ndossche)
3333

34+
- Random:
35+
. Fixed bug GH-21731 (Random\Engine\Xoshiro256StarStar::__unserialize()
36+
accepts all-zero state). (iliaal)
37+
3438
- SPL:
3539
. Fixed bug GH-21499 (RecursiveArrayIterator getChildren UAF after parent
3640
free). (Girgias)

ext/random/engine_xoshiro256starstar.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ static bool unserialize(void *state, HashTable *data)
151151
}
152152
}
153153

154+
if (UNEXPECTED(s->state[0] == 0 && s->state[1] == 0 && s->state[2] == 0 && s->state[3] == 0)) {
155+
return false;
156+
}
157+
154158
return true;
155159
}
156160

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-21731: Xoshiro256StarStar::__unserialize() must reject the all-zero state
3+
--FILE--
4+
<?php
5+
6+
try {
7+
var_dump(unserialize('O:32:"Random\Engine\Xoshiro256StarStar":2:{i:0;a:0:{}i:1;a:4:{i:0;s:16:"0000000000000000";i:1;s:16:"0000000000000000";i:2;s:16:"0000000000000000";i:3;s:16:"0000000000000000";}}'));
8+
} catch (\Exception $e) {
9+
echo $e->getMessage(), PHP_EOL;
10+
}
11+
12+
?>
13+
--EXPECT--
14+
Invalid serialization data for Random\Engine\Xoshiro256StarStar object

0 commit comments

Comments
 (0)