com php-src: Merge remote-tracking branch 'origin/PHP-5.6' into str_size_and_int64_56_backport: ext/opens sl/openssl.c ext/openssl/xp_ssl.c

From: Date: Fri, 21 Feb 2014 16:52:53 +0000
Subject: com php-src: Merge remote-tracking branch 'origin/PHP-5.6' into str_size_and_int64_56_backport: ext/opens sl/openssl.c ext/openssl/xp_ssl.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-76185@lists.php.net to get a copy of this message
Commit: ff5f498f5893807f29bd1e37fe164e23bdf3cd9b Author: Anatol Belski <ab@php.net> Fri, 21 Feb 2014 17:52:53 +0100 Parents: bb9b9d7d2951ee206bcfa632515fd4819cb46806 5389d0963c28d3a08609eef787d288f8cb015c5a Branches: str_size_and_int64_56_backport Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=ff5f498f5893807f29bd1e37fe164e23bdf3cd9b Log: Merge remote-tracking branch 'origin/PHP-5.6' into str_size_and_int64_56_backport * origin/PHP-5.6: Mitigate client-initiated SSL renegotiation DoS Changed paths: MM ext/openssl/openssl..c MM ext/openssl/xp_ssl.c Diff: diff --cc ext/openssl/openssl.c index e9cd037,c813a7e..7c79d6a mode 100644,100755..100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c diff --cc ext/openssl/xp_ssl.c index 61eec4f,c122f48..f5a8713 --- a/ext/openssl/xp_ssl.c +++ b/ext/openssl/xp_ssl.c @@@ -297,8 -308,124 +308,124 @@@ static int php_openssl_sockop_stat(php_ return php_stream_socket_ops.stat(stream, ssb TSRMLS_CC); } + static inline void limit_handshake_reneg(const SSL *ssl) /* {{{ */ + { + php_stream *stream; + php_openssl_netstream_data_t *sslsock; + struct timeval now; + long elapsed_time; + + stream = php_openssl_get_stream_from_ssl_handle(ssl); + sslsock = (php_openssl_netstream_data_t*)stream->abstract; + gettimeofday(&now, NULL); + + /* The initial handshake is never rate-limited */ + if (sslsock->reneg->prev_handshake == 0) { + sslsock->reneg->prev_handshake = now.tv_sec; + return; + } + + elapsed_time = (now.tv_sec - sslsock->reneg->prev_handshake); + sslsock->reneg->prev_handshake = now.tv_sec; + sslsock->reneg->tokens -= (elapsed_time * (sslsock->reneg->limit / sslsock->reneg->window)); + + if (sslsock->reneg->tokens < 0) { + sslsock->reneg->tokens = 0; + } + ++sslsock->reneg->tokens; + + /* The token level exceeds our allowed limit */ + if (sslsock->reneg->tokens > sslsock->reneg->limit) { + zval **val; + + TSRMLS_FETCH(); + + sslsock->reneg->should_close = 1; + + if (stream->context && SUCCESS == php_stream_context_get_option(stream->context, + "ssl", "reneg_limit_callback", &val) + ) { + zval *param, **params[1], *retval; + + MAKE_STD_ZVAL(param); + php_stream_to_zval(stream, param); + params[0] = &param; + + /* Closing the stream inside this callback would segfault! */ + stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE; + if (FAILURE == call_user_function_ex(EG(function_table), NULL, *val, &retval, 1, params, 0, NULL TSRMLS_CC)) { + php_error(E_WARNING, "SSL: failed invoking reneg limit notification callback"); + } + stream->flags ^= PHP_STREAM_FLAG_NO_FCLOSE; + + /* If the reneg_limit_callback returned true don't auto-close */ + if (retval != NULL && Z_TYPE_P(retval) == IS_BOOL && Z_BVAL_P(retval) == 1) { + sslsock->reneg->should_close = 0; + } + + FREE_ZVAL(param); + if (retval != NULL) { + zval_ptr_dtor(&retval); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "SSL: client-initiated handshake rate limit exceeded by peer"); + } + } + } + /* }}} */ + + static void php_openssl_info_callback(const SSL *ssl, int where, int ret) /* {{{ */ + { + /* Rate-limit client-initiated handshake renegotiation to prevent DoS */ + if (where & SSL_CB_HANDSHAKE_START) { + limit_handshake_reneg(ssl); + } + } + /* }}} */ + + static inline void init_handshake_limiting(php_stream *stream, php_openssl_netstream_data_t *sslsock) /* {{{ */ + { + zval **val; + long limit = DEFAULT_RENEG_LIMIT; + long window = DEFAULT_RENEG_WINDOW; + + if (stream->context && + SUCCESS == php_stream_context_get_option(stream->context, + "ssl", "reneg_limit", &val) + ) { + convert_to_long(*val); + limit = Z_LVAL_PP(val); + } + + /* No renegotiation rate-limiting */ + if (limit < 0) { + return; + } + + if (stream->context && + SUCCESS == php_stream_context_get_option(stream->context, + "ssl", "reneg_window", &val) + ) { + convert_to_long(*val); + window = Z_LVAL_PP(val); + } + + sslsock->reneg = (void*)pemalloc(sizeof(php_openssl_handshake_bucket_t), + php_stream_is_persistent(stream) + ); + + sslsock->reneg->limit = limit; + sslsock->reneg->window = window; + sslsock->reneg->prev_handshake = 0; + sslsock->reneg->tokens = 0; + sslsock->reneg->should_close = 0; + + SSL_CTX_set_info_callback(sslsock->ctx, php_openssl_info_callback); + } + /* }}} */ -static const SSL_METHOD *php_select_crypto_method(long method_value, int is_client TSRMLS_DC) +static const SSL_METHOD *php_select_crypto_method(php_int_t method_value, int is_client TSRMLS_DC) { if (method_value == STREAM_CRYPTO_METHOD_SSLv2) { #ifndef OPENSSL_NO_SSL2

« previous php.cvs (#76185) next »