Skip to content
Draft
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
1 change: 1 addition & 0 deletions ext/session/php_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ typedef struct _php_ps_globals {
int module_number;
php_random_status_state_pcgoneseq128xslrr64 random_state;
php_random_algo_with_state random;
bool gc_random_seeded; /* false until first GC check; reseeds after fork */
zend_long gc_probability;
zend_long gc_divisor;
zend_long gc_maxlifetime;
Expand Down
25 changes: 16 additions & 9 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,22 @@ static zend_long php_session_gc(bool immediate)

/* GC must be done before reading session data. */
if ((PS(mod_data) || PS(mod_user_implemented))) {
/* Use probability-based GC if not forced and probability is configured */
if (!collect && PS(gc_probability) > 0) {
/* Reseed PS(random) on first use per process. GINIT runs in the
* master process before SAPIs like PHP-FPM fork their workers, so
* all workers would otherwise inherit and replay the same PCG
* sequence. One CSPRNG call here breaks the correlation. */
if (UNEXPECTED(!PS(gc_random_seeded))) {
php_random_uint128_t seed;
if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
seed = php_random_uint128_constant(
php_random_generate_fallback_seed(),
php_random_generate_fallback_seed()
);
}
php_random_pcgoneseq128xslrr64_seed128(PS(random).state, seed);
PS(gc_random_seeded) = true;
}
collect = php_random_range(PS(random), 0, PS(gc_divisor) - 1) < PS(gc_probability);
}

Expand Down Expand Up @@ -2891,14 +2905,7 @@ static PHP_GINIT_FUNCTION(ps)
.algo = &php_random_algo_pcgoneseq128xslrr64,
.state = &ps_globals->random_state,
};
php_random_uint128_t seed;
if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
seed = php_random_uint128_constant(
php_random_generate_fallback_seed(),
php_random_generate_fallback_seed()
);
}
php_random_pcgoneseq128xslrr64_seed128(ps_globals->random.state, seed);
ps_globals->gc_random_seeded = false;
}

static PHP_MINIT_FUNCTION(session)
Expand Down
Loading