Skip to content

Commit ddf7a5d

Browse files
authored
random: Validate that the arrays do not contain extra elements when unserializing (#9458)
* Apply `var_dump()` in 02_engine/all_serialize_error.phpt This ensures that an undetected serialization error is clear identifiable in the output. * random: Validate that the arrays do not contain extra elements when unserializing
1 parent 15405c6 commit ddf7a5d

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

ext/random/engine_mt19937.c

+11
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ static bool unserialize(php_random_status *status, HashTable *data)
203203
php_random_status_state_mt19937 *s = status->state;
204204
zval *t;
205205

206+
/* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
207+
if (zend_hash_num_elements(data) != (MT_N + 2)) {
208+
return false;
209+
}
210+
206211
for (uint32_t i = 0; i < MT_N; i++) {
207212
t = zend_hash_index_find(data, i);
208213
if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint32_t))) {
@@ -358,6 +363,12 @@ PHP_METHOD(Random_Engine_Mt19937, __unserialize)
358363
Z_PARAM_ARRAY_HT(d);
359364
ZEND_PARSE_PARAMETERS_END();
360365

366+
/* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
367+
if (zend_hash_num_elements(d) != 2) {
368+
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(engine->std.ce->name));
369+
RETURN_THROWS();
370+
}
371+
361372
/* members */
362373
t = zend_hash_index_find(d, 0);
363374
if (!t || Z_TYPE_P(t) != IS_ARRAY) {

ext/random/engine_pcgoneseq128xslrr64.c

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ static bool unserialize(php_random_status *status, HashTable *data)
8383
uint64_t u[2];
8484
zval *t;
8585

86+
/* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
87+
if (zend_hash_num_elements(data) != 2) {
88+
return false;
89+
}
90+
8691
for (uint32_t i = 0; i < 2; i++) {
8792
t = zend_hash_index_find(data, i);
8893
if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint64_t))) {

ext/random/engine_xoshiro256starstar.c

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ static bool unserialize(php_random_status *status, HashTable *data)
131131
php_random_status_state_xoshiro256starstar *s = status->state;
132132
zval *t;
133133

134+
/* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
135+
if (zend_hash_num_elements(data) != 4) {
136+
return false;
137+
}
138+
134139
for (uint32_t i = 0; i < 4; i++) {
135140
t = zend_hash_index_find(data, i);
136141
if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint64_t))) {

ext/random/randomizer.c

+6
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ PHP_METHOD(Random_Randomizer, __unserialize)
272272
Z_PARAM_ARRAY_HT(d);
273273
ZEND_PARSE_PARAMETERS_END();
274274

275+
/* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
276+
if (zend_hash_num_elements(d) != 1) {
277+
zend_throw_exception(NULL, "Invalid serialization data for Random\\Randomizer object", 0);
278+
RETURN_THROWS();
279+
}
280+
275281
members_zv = zend_hash_index_find(d, 0);
276282
if (!members_zv || Z_TYPE_P(members_zv) != IS_ARRAY) {
277283
zend_throw_exception(NULL, "Invalid serialization data for Random\\Randomizer object", 0);

ext/random/tests/02_engine/all_serialize_error.phpt

+24-8
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)