diff --git a/ext/phar/util.c b/ext/phar/util.c index 264bd1f..a44bca7 100644 --- a/ext/phar/util.c +++ b/ext/phar/util.c @@ -1556,6 +1556,23 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, php_uint32 sig_typ php_stream_rewind(fp); + if (sig_type != PHAR_SIG_OPENSSL) { + php_stream_statbuf ssb; + char *pfile; + zend_bool has_pubkey; + + spprintf(&pfile, 0, "%s.pubkey", fname); + has_pubkey = php_stream_stat_path_ex(pfile, PHP_STREAM_URL_STAT_QUIET, &ssb, NULL) == 0; + efree(pfile); + + if (has_pubkey) { + if (error) { + spprintf(error, 0, "openssl public key found, but the signature does not use openssl"); + } + return FAILURE; + } + } + switch (sig_type) { case PHAR_SIG_OPENSSL: { #ifdef PHAR_HAVE_OPENSSL diff --git a/ext/session/mod_files.c b/ext/session/mod_files.c index 5c69ae3..9bf4ff6 100644 --- a/ext/session/mod_files.c +++ b/ext/session/mod_files.c @@ -414,10 +414,9 @@ PS_WRITE_FUNC(files) return FAILURE; } - /* Truncate file if the amount of new data is smaller than the existing data set. */ - - if (vallen < (int)data->st_size) { - php_ignore_value(ftruncate(data->fd, 0)); + if (ftruncate(data->fd, 0) != 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "ftruncate() failed: %s (%d)", strerror(errno), errno); + return FAILURE; } #if defined(HAVE_PWRITE) diff --git a/ext/session/session.c b/ext/session/session.c index 6d535f9..f27e426 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -820,7 +820,7 @@ PHP_INI_BEGIN() /* Upload progress */ STD_PHP_INI_BOOLEAN("session.upload_progress.enabled", - "1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_enabled, php_ps_globals, ps_globals) + "0", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_enabled, php_ps_globals, ps_globals) STD_PHP_INI_BOOLEAN("session.upload_progress.cleanup", "1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_cleanup, php_ps_globals, ps_globals) STD_PHP_INI_ENTRY("session.upload_progress.prefix", @@ -2438,6 +2438,7 @@ static PHP_GINIT_FUNCTION(ps) /* {{{ */ ps_globals->mod = NULL; ps_globals->serializer = NULL; ps_globals->mod_data = NULL; + ps_globals->rfc1867_enabled = 0; ps_globals->session_status = php_session_none; ps_globals->default_mod = NULL; ps_globals->mod_user_implemented = 0; diff --git a/ext/soap/php_encoding.c b/ext/soap/php_encoding.c index bbfadc5..a176708 100644 --- a/ext/soap/php_encoding.c +++ b/ext/soap/php_encoding.c @@ -2145,7 +2145,7 @@ static int calc_dimension_12(const char* str) } str++; } - return i; + return i != 0 ? i : 1; } static int* get_position_12(int dimension, const char* str) diff --git a/ext/soap/php_xml.c b/ext/soap/php_xml.c index b5562e2..7131dac 100644 --- a/ext/soap/php_xml.c +++ b/ext/soap/php_xml.c @@ -53,6 +53,9 @@ static void cleanup_xml_node(xmlNodePtr node) xmlUnlinkNode(trav); xmlFreeNode(trav); } + } else if (trav->type == XML_DTD_NODE) { + /* Attribute-value entity references can share content with the DTD. + * Keep it alive until the document itself is freed. */ } else if ((trav->type != XML_ELEMENT_NODE) && (trav->type != XML_CDATA_SECTION_NODE)) { xmlUnlinkNode(trav); diff --git a/ext/soap/tests/GHSA-4646-p932-gw34.phpt b/ext/soap/tests/GHSA-4646-p932-gw34.phpt new file mode 100644 index 0000000..7ae4cc3 --- /dev/null +++ b/ext/soap/tests/GHSA-4646-p932-gw34.phpt @@ -0,0 +1,33 @@ +--TEST-- +GHSA-4646-p932-gw34: DTD entity references must not outlive their declarations +--POST-- + +]> + + + + + + x + + +--FILE-- + 'urn:t', + 'style' => SOAP_RPC, + 'use' => SOAP_ENCODED, +]); +$server->addFunction('echo_'); +$server->handle(); +?> +--EXPECT-- + +SOAP-ENV:ServerDTD are not supported by SOAP diff --git a/ext/soap/tests/GHSA-q23r-hr55-xmpg-wsdl.phpt b/ext/soap/tests/GHSA-q23r-hr55-xmpg-wsdl.phpt new file mode 100644 index 0000000..d560504 --- /dev/null +++ b/ext/soap/tests/GHSA-q23r-hr55-xmpg-wsdl.phpt @@ -0,0 +1,83 @@ +--TEST-- +GHSA-q23r-hr55-xmpg: Unbounded array walk on empty wsdl:arraySize +--EXTENSIONS-- +soap +--INI-- +soap.wsdl_cache_enabled=0 +--FILE-- + + + + + + + +XML; +} + +class ReplayClient extends SoapClient +{ + public $arrayType; + + public function __construct($arrayType) { + $this->arrayType = $arrayType; + parent::__construct(WSDL); + } + + public function __doRequest($request, $location, $action, $version, $oneWay = 0) { + return << + + + + arrayType}Response> + + A + B + C + + arrayType}Response> + + +XML; + } +} + +$server = new SoapServer(WSDL); +$server->addFunction(['itemType', 'sizeOnly']); + +foreach (['itemType', 'sizeOnly'] as $operation) { + ob_start(); + $server->handle(request($operation)); + $response = ob_get_clean(); + + preg_match('/arraySize="[^"]*"/', $response, $matches); + echo 'encode ', $operation, ' => ', isset($matches[0]) ? $matches[0] : 'none', "\n"; +} + +foreach (['ItemType', 'SizeOnly'] as $arrayType) { + $client = new ReplayClient($arrayType); + echo 'decode ', $arrayType, ' => ', json_encode($client->{lcfirst($arrayType)}()), "\n"; +} + +?> +--EXPECT-- +encode itemType => arraySize="3" +encode sizeOnly => arraySize="3" +decode ItemType => ["A","B","C"] +decode SizeOnly => ["A","B","C"] diff --git a/ext/soap/tests/GHSA-q23r-hr55-xmpg-wsdl.wsdl b/ext/soap/tests/GHSA-q23r-hr55-xmpg-wsdl.wsdl new file mode 100644 index 0000000..55b5b92 --- /dev/null +++ b/ext/soap/tests/GHSA-q23r-hr55-xmpg-wsdl.wsdl @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ext/soap/tests/GHSA-q23r-hr55-xmpg.phpt b/ext/soap/tests/GHSA-q23r-hr55-xmpg.phpt new file mode 100644 index 0000000..18e22de --- /dev/null +++ b/ext/soap/tests/GHSA-q23r-hr55-xmpg.phpt @@ -0,0 +1,66 @@ +--TEST-- +GHSA-q23r-hr55-xmpg: Out-of-bounds read on empty SOAP-ENC:arraySize +--EXTENSIONS-- +json +soap +--FILE-- + + + + + + + A + B + C + + + + +XML; +} + +function decode(SoapServer $server, $request) { + ob_start(); + $server->handle($request); + $response = ob_get_clean(); + + if (strpos($response, '') !== false) { + return 'fault: ' . strip_tags($response); + } + + return json_encode(array_pop($GLOBALS['decoded'])); +} + +$server = new SoapServer(null, ['uri' => 'http://test']); +$server->addFunction('echo_back'); + +$GLOBALS['decoded'] = []; + +foreach (['', ' ', 'abc', '*', '3', '2 2'] as $arraySize) { + echo var_export($arraySize, true), ' => ', decode($server, request($arraySize)), "\n"; +} + +echo "itemType => ", decode($server, request('', 'xsd:string')), "\n"; + +?> +--EXPECT-- +'' => ["A","B","C"] +' ' => ["A","B","C"] +'abc' => ["A","B","C"] +'*' => ["A","B","C"] +'3' => ["A","B","C"] +'2 2' => [["A","B"],["C"]] +itemType => ["A","B","C"] diff --git a/ext/standard/ftp_fopen_wrapper.c b/ext/standard/ftp_fopen_wrapper.c index 5bffa47..736359a 100644 --- a/ext/standard/ftp_fopen_wrapper.c +++ b/ext/standard/ftp_fopen_wrapper.c @@ -150,7 +150,7 @@ static php_stream *php_ftp_fopen_connect(php_stream_wrapper *wrapper, const char return NULL; } - use_ssl = resource->scheme && (strlen(resource->scheme) > 3) && resource->scheme[3] == 's'; + use_ssl = resource->scheme && !strcasecmp(resource->scheme, "ftps"); /* use port 21 if one wasn't specified */ if (resource->port == 0) diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 5a08f49..2f9e404 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -215,15 +215,17 @@ static zend_bool php_stream_http_response_header_trim(char *http_header_line, } /* Process folding headers of the current line and if there are none, parse last full response - * header line. It returns NULL if the last header is finished, otherwise it returns updated - * last header line. */ -static char *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, + * header line. It returns 1 if the current line was folded into the last header line, + * otherwise 0. */ +static zend_bool php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, php_stream *stream, php_stream_context *context, int options, - char **last_header_line, size_t *last_header_line_length, + smart_str *last_header, char *header_line, size_t *header_line_length, int response_code, zval *response_header, php_stream_http_response_header_info *header_info) { - char *last_header_line_end = *last_header_line + *last_header_line_length - 1; + char *last_header_line; + size_t last_header_line_length; + char *last_header_line_end; /* Process non empty header line. */ if (header_line && (*header_line != '\n' && *header_line != '\r')) { @@ -231,7 +233,7 @@ static char *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, if (php_stream_http_response_header_trim(header_line, header_line_length) && *header_line_length == 0) { /* Only spaces so treat as an empty folding header. */ - return *last_header_line; + return 1; } /* Process folding headers if starting with a space or a tab. */ @@ -246,33 +248,34 @@ static char *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, /* It has to have some characters because it would get returned after the call * php_stream_http_response_header_trim above. */ ZEND_ASSERT(http_folded_header_line_length > 0); - /* Concatenate last header line, space and current header line. */ - size_t len = *last_header_line_length + 1 + http_folded_header_line_length; - char *extended_header = emalloc(len + 1); - strncpy(extended_header, *last_header_line, *last_header_line_length); - extended_header[*last_header_line_length] = ' '; - strncpy(extended_header + *last_header_line_length + 1, http_folded_header_line, http_folded_header_line_length); - extended_header[len] = '\0'; - efree(*last_header_line); - *last_header_line = extended_header; - *last_header_line_length = len; - /* Return new header line. */ - return *last_header_line; + /* Append space and current header line to the last header line. */ + smart_str_appendc(last_header, ' '); + smart_str_appendl(last_header, http_folded_header_line, http_folded_header_line_length); + return 1; } } + /* Take the complete last header line out of the buffer. */ + smart_str_0(last_header); + last_header_line = last_header->c; + last_header_line_length = last_header->len; + last_header_line_end = last_header_line + last_header_line_length - 1; + last_header->c = NULL; + last_header->len = 0; + last_header->a = 0; + /* Find header separator position. */ - char *last_header_value = memchr(*last_header_line, ':', *last_header_line_length); + char *last_header_value = memchr(last_header_line, ':', last_header_line_length); if (last_header_value) { /* Verify there is no space in header name */ - char *last_header_name = *last_header_line + 1; + char *last_header_name = last_header_line + 1; while (last_header_name < last_header_value) { if (*last_header_name == ' ' || *last_header_name == '\t') { header_info->error = 1; php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP invalid response format (space in header name)!"); - efree(*last_header_line); - return NULL; + efree(last_header_line); + return 0; } ++last_header_name; } @@ -289,14 +292,14 @@ static char *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, header_info->error = 1; php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP invalid response format (no colon in header line)!"); - efree(*last_header_line); - return NULL; + efree(last_header_line); + return 0; } zend_bool store_header = 1; zval **tmpzval = NULL; - if (!strncasecmp(*last_header_line, "Location:", sizeof("Location:")-1)) { + if (!strncasecmp(last_header_line, "Location:", sizeof("Location:")-1)) { /* Check if the location should be followed. */ if (context && php_stream_context_get_option(context, "http", "follow_location", &tmpzval) == SUCCESS) { SEPARATE_ZVAL(tmpzval); @@ -316,8 +319,8 @@ static char *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP Location header size is over the limit of %d bytes", HTTP_HEADER_MAX_LOCATION_SIZE); - efree(*last_header_line); - return NULL; + efree(last_header_line); + return 0; } if (header_info->location_len == 0) { header_info->location = emalloc(last_header_value_len + 1); @@ -326,13 +329,13 @@ static char *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, } header_info->location_len = last_header_value_len; memcpy(header_info->location, last_header_value, last_header_value_len + 1); - } else if (!strncasecmp(*last_header_line, "Content-Type:", sizeof("Content-Type:")-1)) { + } else if (!strncasecmp(last_header_line, "Content-Type:", sizeof("Content-Type:")-1)) { php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, last_header_value, 0); - } else if (!strncasecmp(*last_header_line, "Content-Length:", sizeof("Content-Length:")-1)) { + } else if (!strncasecmp(last_header_line, "Content-Length:", sizeof("Content-Length:")-1)) { header_info->file_size = atoi(last_header_value); - php_stream_notify_file_size(context, header_info->file_size, *last_header_line, 0); + php_stream_notify_file_size(context, header_info->file_size, last_header_line, 0); } else if ( - !strncasecmp(*last_header_line, "Transfer-Encoding:", sizeof("Transfer-Encoding:")-1) + !strncasecmp(last_header_line, "Transfer-Encoding:", sizeof("Transfer-Encoding:")-1) && !strncasecmp(last_header_value, "Chunked", sizeof("Chunked")-1) ) { /* Create filter to decode response body. */ @@ -362,13 +365,13 @@ static char *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, if (store_header) { zval *http_header; MAKE_STD_ZVAL(http_header); - ZVAL_STRINGL(http_header, *last_header_line, *last_header_line_length, 1); + ZVAL_STRINGL(http_header, last_header_line, last_header_line_length, 0); zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_header, sizeof(zval *), NULL); } else { - efree(*last_header_line); + efree(last_header_line); } - return NULL; + return 0; } php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, @@ -388,7 +391,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, zval *response_header = NULL; int reqok = 0; char *http_header_line = NULL; - char *last_header_line = NULL; + smart_str last_header = {0}; php_stream_http_response_header_info header_info; char tmp_line[128]; size_t chunk_size = 0; @@ -450,7 +453,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, zval_dtor(&ztmp); } - use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's'; + use_ssl = resource->scheme && !strcasecmp(resource->scheme, "https"); /* choose default ports */ if (use_ssl && resource->port == 0) resource->port = 443; @@ -1021,7 +1024,6 @@ finish: } /* read past HTTP headers */ - size_t last_header_line_len = 0; while (!body && !php_stream_eof(stream)) { size_t http_header_line_length; @@ -1029,7 +1031,7 @@ finish: efree(http_header_line); } if ((http_header_line = php_stream_get_line(stream, NULL, 0, &http_header_line_length))) { - zend_bool last_line; + zend_bool last_line, folded; if (*http_header_line == '\r') { if (http_header_line[1] != '\n') { php_stream_close(stream); @@ -1045,13 +1047,13 @@ finish: last_line = 0; } - if (last_header_line != NULL) { + if (last_header.c != NULL) { /* Parse last header line. */ - last_header_line = php_stream_http_response_headers_parse(wrapper, stream, - context, options, &last_header_line, &last_header_line_len, + folded = php_stream_http_response_headers_parse(wrapper, stream, + context, options, &last_header, http_header_line, &http_header_line_length, response_code, response_header, &header_info); - if (EXPECTED(last_header_line == NULL)) { + if (EXPECTED(!folded)) { if (UNEXPECTED(header_info.error)) { php_stream_close(stream); stream = NULL; @@ -1075,23 +1077,20 @@ finish: } if (last_line) { /* For the last line the last header line must be NULL. */ - assert(last_header_line == NULL); + assert(last_header.c == NULL); break; } /* Save current line as the last line so it gets parsed in the next round. */ - last_header_line = emalloc(http_header_line_length + 1); - strncpy(last_header_line, http_header_line, http_header_line_length); - last_header_line[http_header_line_length] = '\0'; - last_header_line_len = http_header_line_length; + smart_str_appendl(&last_header, http_header_line, http_header_line_length); } else { break; } } /* If the stream was closed early, we still want to process the last line to keep BC. */ - if (last_header_line != NULL) { - last_header_line = php_stream_http_response_headers_parse(wrapper, stream, context, - options, &last_header_line, &last_header_line_len, + if (last_header.c != NULL) { + php_stream_http_response_headers_parse(wrapper, stream, context, + options, &last_header, NULL, NULL, response_code, response_header, &header_info); } @@ -1222,6 +1221,8 @@ out: efree(protocol_version); } + smart_str_free(&last_header); + if (http_header_line) { efree(http_header_line); } diff --git a/ext/standard/tests/http/ghsa-f6gg-rchx-xw8j.phpt b/ext/standard/tests/http/ghsa-f6gg-rchx-xw8j.phpt new file mode 100644 index 0000000..4ee5d31 --- /dev/null +++ b/ext/standard/tests/http/ghsa-f6gg-rchx-xw8j.phpt @@ -0,0 +1,61 @@ +--TEST-- +GHSA-f6gg-rchx-xw8j: mixed-case HTTPS URLs must use TLS +--EXTENSIONS-- +openssl +--SKIPIF-- + +--FILE-- + [ + 'method' => 'POST', + 'content' => 'opensec-sensitive-body', + 'header' => "X-Opensec-Request: validation\r\n", + ], + 'ssl' => [ + 'verify_peer' => false, + 'verify_peer_name' => false, + ], + ]); + var_dump(@file_get_contents('HTTPS://alice:secret@{{ ADDR }}/secret', false, $context)); + echo phpt_wait(); +CODE; + +include sprintf('%s/../../../openssl/tests/ServerClientTestCase-8.1.inc', __DIR__); +ServerClientTestCase::getInstance()->run($clientCode, $serverCode); +?> +--EXPECT-- +bool(false) +TLS diff --git a/ext/standard/tests/streams/ghsa-f6gg-rchx-xw8j.phpt b/ext/standard/tests/streams/ghsa-f6gg-rchx-xw8j.phpt new file mode 100644 index 0000000..f15134f --- /dev/null +++ b/ext/standard/tests/streams/ghsa-f6gg-rchx-xw8j.phpt @@ -0,0 +1,34 @@ +--TEST-- +GHSA-f6gg-rchx-xw8j: mixed-case FTPS URLs must enable TLS +--EXTENSIONS-- +openssl +--SKIPIF-- + +--FILE-- +run($clientCode, $serverCode); +?> +--EXPECT-- +bool(false) +AUTH TLS diff --git a/main/SAPI.c b/main/SAPI.c index 660012d..9ccde5f 100644 --- a/main/SAPI.c +++ b/main/SAPI.c @@ -515,7 +515,8 @@ SAPI_API void sapi_deactivate(TSRMLS_D) zend_llist_destroy(&SG(sapi_headers).headers); if (SG(request_info).request_body) { SG(request_info).request_body = NULL; - } else if (SG(server_context)) { + } + if (SG(server_context)) { if (!SG(post_read)) { /* make sure we've consumed all request input data */ char dummy[SAPI_POST_BLOCK_SIZE]; diff --git a/main/main.c b/main/main.c index 68678ae..589b5f5 100644 --- a/main/main.c +++ b/main/main.c @@ -704,15 +704,7 @@ PHPAPI void php_log_err(char *log_message TSRMLS_DC) char *error_time_str; time(&error_time); -#ifdef ZTS - if (!php_during_module_startup()) { - error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 1 TSRMLS_CC); - } else { - error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 0 TSRMLS_CC); - } -#else - error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 1 TSRMLS_CC); -#endif + error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, PG(modules_activated) && EG(active) TSRMLS_CC); len = spprintf(&tmp, 0, "[%s] %s%s", error_time_str, log_message, PHP_EOL); #ifdef PHP_WIN32 php_flock(fd, 2); diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index ccb873a..276f307 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -1269,9 +1269,9 @@ static void init_request_info(fcgi_request *request TSRMLS_DC) */ int slen = len - strlen(pt); int pilen = env_path_info ? strlen(env_path_info) : 0; - char *path_info = env_path_info ? env_path_info + pilen - slen : NULL; + char *path_info = (env_path_info && pilen > slen) ? env_path_info + pilen - slen : NULL; - if (orig_path_info != path_info) { + if (path_info && orig_path_info != path_info) { if (orig_path_info) { char old; diff --git a/sapi/cgi/fastcgi.c b/sapi/cgi/fastcgi.c index 5502c99..ac758e0 100644 --- a/sapi/cgi/fastcgi.c +++ b/sapi/cgi/fastcgi.c @@ -885,6 +885,10 @@ static int fcgi_read_request(fcgi_request *req) int len, padding; unsigned char buf[FCGI_MAX_LENGTH+8]; + if (UNEXPECTED(req->in_len != 0)) { + return 0; + } + req->keep = 0; req->closed = 0; req->in_len = 0; @@ -1140,6 +1144,8 @@ static inline void fcgi_close(fcgi_request *req, int force, int destroy) req->nodelay = 0; #endif req->fd = -1; + /* a partially read STDIN record does not outlive its connection */ + req->in_len = 0; } } diff --git a/sapi/cgi/tests/ghsa-5j3f-64r7-f564.phpt b/sapi/cgi/tests/ghsa-5j3f-64r7-f564.phpt new file mode 100644 index 0000000..73c34f9 --- /dev/null +++ b/sapi/cgi/tests/ghsa-5j3f-64r7-f564.phpt @@ -0,0 +1,60 @@ +--TEST-- +GHSA-5j3f-64r7-f564 (PATH_INFO underflow when the stripped suffix is longer than PATH_INFO) +--SKIPIF-- + +--FILE-- + getenv("PATH"), + "SystemRoot" => getenv("SystemRoot"), + ], 'is_string'); + $p = proc_open( + escapeshellarg($php) . " -n -d cgi.fix_pathinfo=1", + [0 => ["pipe", "r"], 1 => ["pipe", "w"]], + $pipes, + null, + $env + ["REDIRECT_STATUS" => "1", "REQUEST_METHOD" => "GET"] + $base_env + ); + fclose($pipes[0]); + $out = stream_get_contents($pipes[1]); + fclose($pipes[1]); + proc_close($p); + echo substr($out, strpos($out, "\r\n\r\n") + 4); +} + +echo "Suffix shorter than PATH_INFO\n"; +run_cgi($php, ["SCRIPT_FILENAME" => "$script/b", "PATH_INFO" => "/a/b"]); + +echo "Suffix longer than PATH_INFO\n"; +run_cgi($php, ["SCRIPT_FILENAME" => "$script/xxxxxxxxxxxxxxxx", "PATH_INFO" => "/x"]); + +echo "Suffix taken from PATH_TRANSLATED\n"; +run_cgi($php, [ + "SCRIPT_FILENAME" => $php, + "PATH_TRANSLATED" => "$script/xxxxxxxxxxxxxxxx", + "REDIRECT_URL" => "/redirected", + "PATH_INFO" => "/x", +]); +?> +--CLEAN-- + +--EXPECT-- +Suffix shorter than PATH_INFO +string(2) "/b" +string(4) "/a/b" +Suffix longer than PATH_INFO +string(2) "/x" +NULL +Suffix taken from PATH_TRANSLATED +string(2) "/x" +NULL diff --git a/sapi/fpm/fpm/fastcgi.c b/sapi/fpm/fpm/fastcgi.c index 6ac568b..b827ea8 100644 --- a/sapi/fpm/fpm/fastcgi.c +++ b/sapi/fpm/fpm/fastcgi.c @@ -510,6 +510,10 @@ static int fcgi_read_request(fcgi_request *req) int len, padding; unsigned char buf[FCGI_MAX_LENGTH+8]; + if (UNEXPECTED(req->in_len != 0)) { + return 0; + } + req->keep = 0; req->closed = 0; req->in_len = 0; @@ -762,6 +766,8 @@ void fcgi_close(fcgi_request *req, int force, int destroy) close(req->fd); #endif req->fd = -1; + /* a partially read STDIN record does not outlive its connection */ + req->in_len = 0; fpm_request_finished(); } } diff --git a/sapi/fpm/fpm/fpm_children.c b/sapi/fpm/fpm/fpm_children.c index 74c2f38..b42a480 100644 --- a/sapi/fpm/fpm/fpm_children.c +++ b/sapi/fpm/fpm/fpm_children.c @@ -69,7 +69,11 @@ static void fpm_child_close(struct fpm_child_s *child, int in_event_loop) /* {{{ fpm_event_fire(&child->ev_stdout); } if (child->fd_stdout != -1) { + if (in_event_loop) { + fpm_event_del(&child->ev_stdout); + } close(child->fd_stdout); + child->fd_stdout = -1; } } @@ -78,7 +82,11 @@ static void fpm_child_close(struct fpm_child_s *child, int in_event_loop) /* {{{ fpm_event_fire(&child->ev_stderr); } if (child->fd_stderr != -1) { + if (in_event_loop) { + fpm_event_del(&child->ev_stderr); + } close(child->fd_stderr); + child->fd_stderr = -1; } } diff --git a/sapi/fpm/fpm/fpm_status.c b/sapi/fpm/fpm/fpm_status.c index d9a8263..ca3d7d8 100644 --- a/sapi/fpm/fpm/fpm_status.c +++ b/sapi/fpm/fpm/fpm_status.c @@ -384,8 +384,9 @@ int fpm_status_handle_request(TSRMLS_D) /* {{{ */ /* no need to test the var 'full' */ if (full_syntax) { int i, first; - size_t len, len_uri; + size_t len, len_uri, len_method, len_user, len_script; char *query_string, *request_uri_string; + char *request_method_string, *auth_user_string, *script_filename_string; struct timeval duration, now; #ifdef HAVE_FPM_LQ float cpu; @@ -430,6 +431,33 @@ int fpm_status_handle_request(TSRMLS_D) /* {{{ */ query_string = php_escape_html_entities_ex((unsigned char *)proc.query_string, strlen(proc.query_string), &len, 1, ENT_DISALLOWED | ENT_HTML_DOC_XML1 | ENT_COMPAT, NULL, 1 TSRMLS_CC); } } + request_method_string = NULL; + len_method = 0; + if (proc.request_method[0] != '\0') { + if (!encode) { + request_method_string = proc.request_method; + } else { + request_method_string = php_escape_html_entities_ex((unsigned char *)proc.request_method, strlen(proc.request_method), &len_method, 1, ENT_DISALLOWED | ENT_HTML_DOC_XML1 | ENT_COMPAT, NULL, 1 TSRMLS_CC); + } + } + auth_user_string = NULL; + len_user = 0; + if (proc.auth_user[0] != '\0') { + if (!encode) { + auth_user_string = proc.auth_user; + } else { + auth_user_string = php_escape_html_entities_ex((unsigned char *)proc.auth_user, strlen(proc.auth_user), &len_user, 1, ENT_DISALLOWED | ENT_HTML_DOC_XML1 | ENT_COMPAT, NULL, 1 TSRMLS_CC); + } + } + script_filename_string = NULL; + len_script = 0; + if (proc.script_filename[0] != '\0') { + if (!encode) { + script_filename_string = proc.script_filename; + } else { + script_filename_string = php_escape_html_entities_ex((unsigned char *)proc.script_filename, strlen(proc.script_filename), &len_script, 1, ENT_DISALLOWED | ENT_HTML_DOC_XML1 | ENT_COMPAT, NULL, 1 TSRMLS_CC); + } + } #ifdef HAVE_FPM_LQ /* prevent NaN */ @@ -453,13 +481,13 @@ int fpm_status_handle_request(TSRMLS_D) /* {{{ */ now_epoch - proc.start_epoch, proc.requests, duration.tv_sec * 1000000UL + duration.tv_usec, - proc.request_method[0] != '\0' ? proc.request_method : "-", + request_method_string ? request_method_string : "-", request_uri_string ? request_uri_string: "-", query_string ? "?" : "", query_string ? query_string : "", proc.content_length, - proc.auth_user[0] != '\0' ? proc.auth_user : "-", - proc.script_filename[0] != '\0' ? proc.script_filename : "-", + auth_user_string ? auth_user_string : "-", + script_filename_string ? script_filename_string : "-", #ifdef HAVE_FPM_LQ proc.request_stage == FPM_REQUEST_ACCEPTING ? cpu : 0., #endif @@ -473,6 +501,15 @@ int fpm_status_handle_request(TSRMLS_D) /* {{{ */ if (len_uri > 0 && request_uri_string) { efree(request_uri_string); } + if (len_method > 0 && request_method_string) { + efree(request_method_string); + } + if (len_user > 0 && auth_user_string) { + efree(auth_user_string); + } + if (len_script > 0 && script_filename_string) { + efree(script_filename_string); + } } if (full_post) { diff --git a/sapi/fpm/tests/ghsa-3mg3-cr28-46gc-status-xss.phpt b/sapi/fpm/tests/ghsa-3mg3-cr28-46gc-status-xss.phpt new file mode 100644 index 0000000..dc04901 --- /dev/null +++ b/sapi/fpm/tests/ghsa-3mg3-cr28-46gc-status-xss.phpt @@ -0,0 +1,73 @@ +--TEST-- +FPM: GHSA-3mg3-cr28-46gc - status xss via request method, auth user and script filename +--SKIPIF-- + +--FILE-- +.php'; +file_put_contents($script, ".php'; + $client = new Adoy\FastCGI\Client('127.0.0.1', $port); + $id = $client->async_request(array( + 'GATEWAY_INTERFACE' => 'FastCGI/1.0', + 'REQUEST_METHOD' => '', + 'SCRIPT_FILENAME' => $script, + 'SCRIPT_NAME' => $uri, + 'QUERY_STRING' => '', + 'REQUEST_URI' => $uri, + 'DOCUMENT_URI' => $uri, + 'SERVER_SOFTWARE' => 'php/fcgiclient', + 'REMOTE_ADDR' => '127.0.0.1', + 'REMOTE_PORT' => '9985', + 'SERVER_ADDR' => '127.0.0.1', + 'SERVER_PORT' => '80', + 'SERVER_NAME' => php_uname('n'), + 'SERVER_PROTOCOL' => 'HTTP/1.1', + 'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('":x'), + 'CONTENT_TYPE' => '', + 'CONTENT_LENGTH' => 0 + ), false); + usleep(100000); + $html = run_request('127.0.0.1', $port, '/status', 'full&html'); + $client->wait_for_response_data($id); + var_dump(strpos($html, ' +Done +--EXPECTF-- +[%d-%s-%d %d:%d:%d] NOTICE: fpm is running, pid %d +[%d-%s-%d %d:%d:%d] NOTICE: ready to handle connections +bool(false) +int(4) +Done +--CLEAN-- +.php'); +?> diff --git a/sapi/fpm/tests/unread-body-keep-conn.phpt b/sapi/fpm/tests/unread-body-keep-conn.phpt new file mode 100644 index 0000000..7c60842 --- /dev/null +++ b/sapi/fpm/tests/unread-body-keep-conn.phpt @@ -0,0 +1,141 @@ +--TEST-- +FPM: unread request body cannot become a new request on a kept connection +--SKIPIF-- + +--FILE-- + $value) { + $encoded .= encode_length(strlen($name)) . encode_length(strlen($value)) . $name . $value; + } + return $encoded; +} + +function request($id, $keep, array $params, $stdin) +{ + return record(1, pack('nCxxxxx', 1, $keep), $id) + . record(4, params($params), $id) + . record(4, '', $id) + . record(5, $stdin, $id) + . record(5, '', $id); +} + +function transact($port, $request) +{ + $socket = @fsockopen('127.0.0.1', $port); + if (!$socket) { + return ''; + } + stream_set_timeout($socket, 3); + $written = 0; + while ($written < strlen($request)) { + $n = fwrite($socket, substr($request, $written)); + if (!$n) { + break; + } + $written += $n; + } + stream_socket_shutdown($socket, STREAM_SHUT_WR); + $response = stream_get_contents($socket); + fclose($socket); + return $response; +} + +$logfile = dirname(__FILE__).'/php-fpm.log.tmp'; +$port = 9000+PHP_INT_SIZE; +$script = dirname(__FILE__).'/unread-body-keep-conn.src.php'; +file_put_contents($script, <<<'PHP' + 'FastCGI/1.0', + 'REQUEST_METHOD' => 'GET', + 'SCRIPT_FILENAME' => $script, + 'SCRIPT_NAME' => '/smuggled.php', + 'REQUEST_URI' => '/smuggled.php', + 'SERVER_PROTOCOL' => 'HTTP/1.1', + 'REMOTE_ADDR' => '203.0.113.1', + 'CONTENT_LENGTH' => '0', + ); + $hidden = request(2, false, $hiddenParams, ''); + $body = str_pad("--B\r\nContent-Disposition: form-data\r\n\r\n", 12000, 'A'); + $body = substr_replace($body, $hidden, 5120, strlen($hidden)); + + $outerParams = array( + 'GATEWAY_INTERFACE' => 'FastCGI/1.0', + 'REQUEST_METHOD' => 'POST', + 'SCRIPT_FILENAME' => $script, + 'SCRIPT_NAME' => '/open.php', + 'REQUEST_URI' => '/open.php', + 'SERVER_PROTOCOL' => 'HTTP/1.1', + 'REMOTE_ADDR' => '127.0.0.1', + 'CONTENT_TYPE' => 'multipart/form-data; boundary=B', + 'CONTENT_LENGTH' => (string) strlen($body), + ); + foreach (array(false, true) as $keep) { + $response = transact($port, request(1, $keep, $outerParams, $body)); + echo 'keep ', $keep ? 'on' : 'off', ': ', + strpos($response, 'smuggled') !== false ? 'smuggled' : 'safe', "\n"; + } + + $body = 'A' . $hidden; + $outerParams['CONTENT_TYPE'] = 'application/octet-stream'; + $outerParams['CONTENT_LENGTH'] = '1'; + $response = transact($port, request(1, true, $outerParams, $body)); + echo 'partial frame: ', + strpos($response, 'smuggled') !== false ? 'smuggled' : 'safe', "\n"; + + proc_terminate($fpm); + stream_get_contents($tail); + fclose($tail); + proc_close($fpm); +} +?> +--EXPECTF-- +[%d-%s-%d %d:%d:%d] NOTICE: fpm is running, pid %d +[%d-%s-%d %d:%d:%d] NOTICE: ready to handle connections +keep off: safe +keep on: safe +partial frame: safe +--CLEAN-- + diff --git a/sapi/fpm/tests/unread-body-partial-record.phpt b/sapi/fpm/tests/unread-body-partial-record.phpt new file mode 100644 index 0000000..33542c4 --- /dev/null +++ b/sapi/fpm/tests/unread-body-partial-record.phpt @@ -0,0 +1,143 @@ +--TEST-- +FPM: partially read STDIN record does not make the worker reject later connections +--SKIPIF-- + +--FILE-- + $value) { + $encoded .= encode_length(strlen($name)) . encode_length(strlen($value)) . $name . $value; + } + return $encoded; +} + +function request($id, $keep, array $params, $stdin) +{ + return record(1, pack('nCxxxxx', 1, $keep), $id) + . record(4, params($params), $id) + . record(4, '', $id) + . record(5, $stdin, $id) + . record(5, '', $id); +} + +function transact($port, $request) +{ + $socket = @fsockopen('127.0.0.1', $port); + if (!$socket) { + return ''; + } + stream_set_timeout($socket, 3); + $written = 0; + while ($written < strlen($request)) { + $n = fwrite($socket, substr($request, $written)); + if (!$n) { + break; + } + $written += $n; + } + stream_socket_shutdown($socket, STREAM_SHUT_WR); + $response = stream_get_contents($socket); + fclose($socket); + return $response; +} + +$logfile = dirname(__FILE__).'/php-fpm.log.tmp'; +$port = 9000+PHP_INT_SIZE; +$script = dirname(__FILE__).'/unread-body-partial-record.src.php'; +file_put_contents($script, <<<'PHP' + 'FastCGI/1.0', + 'REQUEST_METHOD' => 'GET', + 'SCRIPT_FILENAME' => $script, + 'SCRIPT_NAME' => '/smuggled.php', + 'REQUEST_URI' => '/smuggled.php', + 'SERVER_PROTOCOL' => 'HTTP/1.1', + 'REMOTE_ADDR' => '203.0.113.1', + 'CONTENT_LENGTH' => '0', + ); + $hidden = request(2, false, $hiddenParams, ''); + $outerParams = array( + 'GATEWAY_INTERFACE' => 'FastCGI/1.0', + 'REQUEST_METHOD' => 'POST', + 'SCRIPT_FILENAME' => $script, + 'SCRIPT_NAME' => '/open.php', + 'REQUEST_URI' => '/open.php', + 'SERVER_PROTOCOL' => 'HTTP/1.1', + 'REMOTE_ADDR' => '127.0.0.1', + 'CONTENT_TYPE' => 'application/octet-stream', + 'CONTENT_LENGTH' => '1', + ); + $plainParams = array('REQUEST_METHOD' => 'GET', 'CONTENT_LENGTH' => '0') + $outerParams; + unset($plainParams['CONTENT_TYPE']); + + /* The STDIN record is longer than CONTENT_LENGTH so it is left partially read. */ + foreach (array(false, true) as $keep) { + $response = transact($port, request(1, $keep, $outerParams, 'A' . $hidden)); + echo 'keep ', $keep ? 'on' : 'off', ': ', + strpos($response, 'smuggled') !== false ? 'smuggled' : 'safe', ', ', + strpos($response, 'outer 1') !== false ? 'served' : 'not served', "\n"; + /* The only worker must still serve new connections. */ + foreach (array(1, 2) as $n) { + $response = transact($port, request(1, false, $plainParams, '')); + echo " next connection $n: ", strpos($response, 'outer 0') !== false ? 'served' : 'not served', "\n"; + } + } + + proc_terminate($fpm); + stream_get_contents($tail); + fclose($tail); + proc_close($fpm); +} +?> +--EXPECTF-- +[%d-%s-%d %d:%d:%d] NOTICE: fpm is running, pid %d +[%d-%s-%d %d:%d:%d] NOTICE: ready to handle connections +keep off: safe, served + next connection 1: served + next connection 2: served +keep on: safe, served + next connection 1: served + next connection 2: served +--CLEAN-- +