From 8ea43b98782dcf3b44fc33f85dc11918368e5901 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Sat, 11 Apr 2026 21:27:57 -0400 Subject: [PATCH] Fix GH-21731: Random\Engine\Xoshiro256StarStar::__unserialize() accepts all-zero state 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 --- NEWS | 4 ++++ ext/random/engine_xoshiro256starstar.c | 4 ++++ .../xoshiro256starstar_unserialize_zero_state.phpt | 14 ++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 ext/random/tests/02_engine/xoshiro256starstar_unserialize_zero_state.phpt diff --git a/NEWS b/NEWS index 07653ef6a37f9..24846881de960 100644 --- a/NEWS +++ b/NEWS @@ -31,6 +31,10 @@ PHP NEWS - OpenSSL: . Fix a bunch of memory leaks and crashes on edge cases. (ndossche) +- Random: + . Fixed bug GH-21731 (Random\Engine\Xoshiro256StarStar::__unserialize() + accepts all-zero state). (iliaal) + - SPL: . Fixed bug GH-21499 (RecursiveArrayIterator getChildren UAF after parent free). (Girgias) diff --git a/ext/random/engine_xoshiro256starstar.c b/ext/random/engine_xoshiro256starstar.c index 1a054362f0652..12db8198978dc 100644 --- a/ext/random/engine_xoshiro256starstar.c +++ b/ext/random/engine_xoshiro256starstar.c @@ -151,6 +151,10 @@ static bool unserialize(void *state, HashTable *data) } } + if (UNEXPECTED(s->state[0] == 0 && s->state[1] == 0 && s->state[2] == 0 && s->state[3] == 0)) { + return false; + } + return true; } diff --git a/ext/random/tests/02_engine/xoshiro256starstar_unserialize_zero_state.phpt b/ext/random/tests/02_engine/xoshiro256starstar_unserialize_zero_state.phpt new file mode 100644 index 0000000000000..6ebcd03e85704 --- /dev/null +++ b/ext/random/tests/02_engine/xoshiro256starstar_unserialize_zero_state.phpt @@ -0,0 +1,14 @@ +--TEST-- +GH-21731: Xoshiro256StarStar::__unserialize() must reject the all-zero state +--FILE-- +getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +Invalid serialization data for Random\Engine\Xoshiro256StarStar object