com php-src: Merge remote-tracking branch 'origin/master' into str_size_and_int64: ext/openssl/openssl.c ext /openssl/xp_ssl.c

From: Date: Fri, 21 Feb 2014 16:46:15 +0000
Subject: com php-src: Merge remote-tracking branch 'origin/master' into str_size_and_int64: ext/openssl/openssl.c ext /openssl/xp_ssl.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-76172@lists.php.net to get a copy of this message
Commit: 264c68aed73e5de2ee8fe286639f8cc383c2d7c0 Author: Anatol Belski <ab@php.net> Fri, 21 Feb 2014 17:46:15 +0100 Parents: 14af491490f5fbcd49c5390d63a93fe65e0060ad 1268bd6045ea0fce57c5c10a1faca98a70dd692b Branches: str_size_and_int64 Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=264c68aed73e5de2ee8fe286639f8cc383c2d7c0 Log: Merge remote-tracking branch 'origin/master' into str_size_and_int64 * origin/master: Mitigate client-initiated SSL renegotiation DoS Conflicts: ext/openssl/xp_ssl.c Changed paths: MM ext/openssl/openssl.c MM ext/openssl/xp_ssl.c Diff: diff --cc ext/openssl/openssl.c index 9b3eaac,ba4b3a6..a8c183d mode 100644,100755...100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c diff --cc ext/openssl/xp_ssl.c index 2c4dfee,1e2ff84..9409591 --- 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 (#76172) next »