Skip to content

Commit eff7f7e

Browse files
committed
ext/openssl: Reorder reneg rate-limit decay to avoid integer divide to zero
php_openssl_limit_handshake_reneg() computes the bucket decay as elapsed * (limit / window). Both operands are zend_long, so with the documented defaults limit=2 and window=300 the inner division truncates to 0 and the decay term collapses to 0 for every elapsed value. The leaky bucket stops leaking and the cap fires after exactly limit renegotiations regardless of how widely spaced in time, not "limit per window seconds" as documented. Compute (elapsed * limit) / window so the truncation only applies once, after the multiply that brings the operand into a useful range. Guard against window <= 0 to keep the divide safe under user-supplied values the existing init handler does not validate.
1 parent 05afc37 commit eff7f7e

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

ext/openssl/xp_ssl.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,9 @@ static void php_openssl_limit_handshake_reneg(const SSL *ssl) /* {{{ */
11311131

11321132
elapsed_time = (now.tv_sec - sslsock->reneg->prev_handshake);
11331133
sslsock->reneg->prev_handshake = now.tv_sec;
1134-
sslsock->reneg->tokens -= (elapsed_time * (sslsock->reneg->limit / sslsock->reneg->window));
1134+
if (sslsock->reneg->window > 0) {
1135+
sslsock->reneg->tokens -= (elapsed_time * sslsock->reneg->limit) / sslsock->reneg->window;
1136+
}
11351137

11361138
if (sslsock->reneg->tokens < 0) {
11371139
sslsock->reneg->tokens = 0;

0 commit comments

Comments
 (0)