Skip to content

Fix GH-21731: Random\Engine\Xoshiro256StarStar::__unserialize() accepts all-zero state#21732

Open
iliaal wants to merge 1 commit intophp:PHP-8.4from
iliaal:fix/gh-21731-xoshiro-unserialize-zero-state
Open

Fix GH-21731: Random\Engine\Xoshiro256StarStar::__unserialize() accepts all-zero state#21732
iliaal wants to merge 1 commit intophp:PHP-8.4from
iliaal:fix/gh-21731-xoshiro-unserialize-zero-state

Conversation

@iliaal
Copy link
Copy Markdown
Contributor

@iliaal iliaal commented Apr 12, 2026

Fixes #21731.

Random\Engine\Xoshiro256StarStar::__construct() rejects a seed that would leave the internal state all zero, because xoshiro256** with zero state returns 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 unserialize too. The Mt19937-aliased __unserialize() wrapper maps the false return into the standard "Invalid serialization data for ... object" exception, so the wrapper needs no changes.

…cepts 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 phpGH-21731
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run this actually through unserialize(), as in ext/random/tests/02_engine/all_serialize_error.phpt.

--TEST--
GH-21731: Xoshiro256StarStar::__unserialize() must reject the all-zero state
--FILE--
<?php declare(strict_types = 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests should not contain unrelated logic.

Comment on lines +154 to +155
/* An all-zero state generates zero forever. The constructor rejects
* such a seed; reject it here for symmetry. */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment provides no value.

Suggested change
/* An all-zero state generates zero forever. The constructor rejects
* such a seed; reject it here for symmetry. */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants