diff --git a/php-8.1.34-xlts-2.patch b/php-8.1.34-xlts-2.patch new file mode 100644 index 0000000..20843d6 --- /dev/null +++ b/php-8.1.34-xlts-2.patch @@ -0,0 +1,1433 @@ +diff --git a/ext/date/tests/GHSA-gr4f-wgx5-jpfv.phpt b/ext/date/tests/GHSA-gr4f-wgx5-jpfv.phpt +new file mode 100644 +index 0000000..bb81c93 +--- /dev/null ++++ b/ext/date/tests/GHSA-gr4f-wgx5-jpfv.phpt +@@ -0,0 +1,22 @@ ++--TEST-- ++GHSA-gr4f-wgx5-jpfv: DATEG(tzcache) use-after-free ++--CREDITS-- ++Sebastian Bergmann ++--CGI-- ++--ENV-- ++QUERY_STRING=a=1&b=2 ++--EXTENSIONS-- ++session ++zend_test ++--INI-- ++error_log="{TMP}/GHSA-gr4f-wgx5-jpfv.log" ++log_errors=1 ++max_input_vars=1 ++zend_test.warn_on_startup=1 ++--FILE-- ++ ++--EXPECTF-- ++Warning: PHP Request Startup: Input variables exceeded 1. To increase the limit change max_input_vars in php.ini. in Unknown on line 0 ++Hello world%r(?:\n\nWarning: Test startup warning from zend_test in Unknown on line 0)?%r +diff --git a/ext/phar/tests/ghsa-68fq-fh7h-f8gr.phpt b/ext/phar/tests/ghsa-68fq-fh7h-f8gr.phpt +new file mode 100644 +index 0000000..e03123f +--- /dev/null ++++ b/ext/phar/tests/ghsa-68fq-fh7h-f8gr.phpt +@@ -0,0 +1,122 @@ ++--TEST-- ++GHSA-68fq-fh7h-f8gr (OpenSSL signature bypass by downgrading the algorithm to a keyless one) ++--EXTENSIONS-- ++phar ++openssl ++--SKIPIF-- ++ ++--INI-- ++phar.readonly=0 ++phar.require_hash=1 ++--FILE-- ++ __DIR__ . '/files/openssl.cnf']); ++$pubkey = openssl_pkey_get_details($private)['key']; ++ ++function build(string $file, string $pkey): string ++{ ++ $phar = new Phar($file); ++ $phar['main.php'] = 'setStub('setSignatureAlgorithm(Phar::OPENSSL, $pkey); ++ ++ return file_get_contents($file); ++} ++ ++// a phar built in this process comes back from the cache, so each case is read under its own name ++function open(string $file, string $archive, ?string $pubkey): void ++{ ++ file_put_contents($file, $archive); ++ if ($pubkey !== null) { ++ file_put_contents($file . '.pubkey', $pubkey); ++ } ++ ++ try { ++ echo (new Phar($file))->getSignature()['hash_type'], "\n"; ++ } catch (UnexpectedValueException $e) { ++ echo $e->getMessage(), "\n"; ++ } ++} ++ ++function downgrade_phar(string $archive): string ++{ ++ // the last 12 bytes are [length][algorithm]["GBMB"], the signature sits just before them ++ $siglen = unpack('V', substr($archive, -12, 4))[1]; ++ $message = substr($archive, 0, strlen($archive) - 12 - $siglen); ++ ++ return $message . hash('sha256', $message, true) . pack('V', 0x0003) . 'GBMB'; // 0x0003 is PHAR_SIG_SHA256 ++} ++ ++function downgrade_tar(string $archive): string ++{ ++ for ($offset = 0; $offset + 512 <= strlen($archive); ) { ++ $size = (int) octdec(trim(substr($archive, $offset + 124, 12), " \0")); // octal size field of a 512 byte header ++ ++ if (rtrim(substr($archive, $offset, 100), "\0") === '.phar/signature.bin') { ++ // the signed message stops at this header, so rewriting the entry content leaves it alone ++ $blob = pack('VV', 0x0003, 32) . hash('sha256', substr($archive, 0, $offset), true); // [algorithm][digest length][digest] ++ ++ return substr_replace($archive, str_pad($blob, $size, "\0"), $offset + 512, $size); ++ } ++ ++ $offset += 512 + (($size + 511) & ~511); ++ } ++ ++ throw new RuntimeException('no signature entry'); ++} ++ ++function downgrade_zip(string $archive): string ++{ ++ $eocd = strrpos($archive, "PK\x05\x06"); ++ $cdir = unpack('V', substr($archive, $eocd + 16, 4))[1]; // the eocd holds the directory offset at +16 and the archive comment length at +20 ++ $comment = substr($archive, $eocd + 22, unpack('v', substr($archive, $eocd + 20, 2))[1]); ++ ++ for ($record = $cdir; substr($archive, $record, 4) === "PK\x01\x02"; ) { ++ $namelen = unpack('v', substr($archive, $record + 28, 2))[1]; ++ ++ if (substr($archive, $record + 46, $namelen) === '.phar/signature.bin') { ++ $size = unpack('V', substr($archive, $record + 24, 4))[1]; ++ $local = unpack('V', substr($archive, $record + 42, 4))[1]; ++ $content = $local + 30 + unpack('v', substr($archive, $local + 26, 2))[1] + unpack('v', substr($archive, $local + 28, 2))[1]; ++ // the signed message is everything before the local entry plus the directory up to this record ++ $message = substr($archive, 0, $local) . substr($archive, $cdir, $record - $cdir) . $comment; ++ $blob = pack('VV', 0x0003, 32) . hash('sha256', $message, true); ++ ++ return substr_replace($archive, str_pad($blob, $size, "\0"), $content, $size); ++ } ++ ++ $record += 46 + $namelen + unpack('v', substr($archive, $record + 30, 2))[1] + unpack('v', substr($archive, $record + 32, 2))[1]; ++ } ++ ++ throw new RuntimeException('no signature entry'); ++} ++ ++foreach (['phar' => 'downgrade_phar', 'phar.tar' => 'downgrade_tar', 'phar.zip' => 'downgrade_zip'] as $suffix => $downgrade) { ++ $archive = build($base . '.source.' . $suffix, $pkey); ++ open($base . '.signed.' . $suffix, $archive, $pubkey); ++ open($base . '.tampered.' . $suffix, $downgrade($archive), $pubkey); ++ open($base . '.nokey.' . $suffix, $downgrade($archive), null); ++} ++?> ++--CLEAN-- ++ ++--EXPECTF-- ++OpenSSL ++phar "%stampered.phar" SHA256 signature could not be verified: openssl public key found, but the signature does not use openssl ++SHA-256 ++OpenSSL ++phar error: tar-based phar "%stampered.phar.tar" signature cannot be verified: openssl public key found, but the signature does not use openssl ++SHA-256 ++OpenSSL ++phar error: signature cannot be verified: openssl public key found, but the signature does not use openssl in zip-based phar "%stampered.phar.zip" ++SHA-256 +diff --git a/ext/phar/util.c b/ext/phar/util.c +index 62eba21..0fe25ff 100644 +--- a/ext/phar/util.c ++++ b/ext/phar/util.c +@@ -1518,6 +1518,23 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t sig_type, + + php_stream_rewind(fp); + ++ if (sig_type != PHAR_SIG_OPENSSL && sig_type != PHAR_SIG_OPENSSL_SHA256 && sig_type != PHAR_SIG_OPENSSL_SHA512) { ++ php_stream_statbuf ssb; ++ char *pfile; ++ 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_SHA512: + case PHAR_SIG_OPENSSL_SHA256: +diff --git a/ext/session/mod_files.c b/ext/session/mod_files.c +index e774de7..8932b41 100644 +--- a/ext/session/mod_files.c ++++ b/ext/session/mod_files.c +@@ -238,9 +238,9 @@ static int ps_files_write(ps_files *data, zend_string *key, zend_string *val) + return FAILURE; + } + +- /* Truncate file if the amount of new data is smaller than the existing data set. */ +- if (ZSTR_LEN(val) < data->st_size) { +- php_ignore_value(ftruncate(data->fd, 0)); ++ if (ftruncate(data->fd, 0) != 0) { ++ php_error_docref(NULL, E_WARNING, "ftruncate() failed: %s (%d)", strerror(errno), errno); ++ return FAILURE; + } + + #ifdef HAVE_PWRITE +diff --git a/ext/session/session.c b/ext/session/session.c +index d72ae27..d290df8 100644 +--- a/ext/session/session.c ++++ b/ext/session/session.c +@@ -817,7 +817,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", +@@ -2767,6 +2767,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 cc8b58e..da99112 100644 +--- a/ext/soap/php_encoding.c ++++ b/ext/soap/php_encoding.c +@@ -2011,7 +2011,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 340dbd8..a692ba8 100644 +--- a/ext/soap/php_xml.c ++++ b/ext/soap/php_xml.c +@@ -50,6 +50,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..8e3567d +--- /dev/null ++++ b/ext/soap/tests/GHSA-4646-p932-gw34.phpt +@@ -0,0 +1,35 @@ ++--TEST-- ++GHSA-4646-p932-gw34: DTD entity references must not outlive their declarations ++--POST-- ++ ++]> ++ ++ ++ ++ ++ ++ x ++ ++ ++--EXTENSIONS-- ++soap ++--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..1b39fee +--- /dev/null ++++ b/ext/soap/tests/GHSA-q23r-hr55-xmpg-wsdl.phpt +@@ -0,0 +1,80 @@ ++--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 function __construct(public string $arrayType) { ++ parent::__construct(WSDL); ++ } ++ ++ public function __doRequest($request, $location, $action, $version, $oneWay = 0): ?string { ++ 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, ' => ', $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..886b3ff +--- /dev/null ++++ b/ext/soap/tests/GHSA-q23r-hr55-xmpg.phpt +@@ -0,0 +1,65 @@ ++--TEST-- ++GHSA-q23r-hr55-xmpg: Out-of-bounds read on empty SOAP-ENC:arraySize ++--EXTENSIONS-- ++soap ++--FILE-- ++ ++ ++ ++ ++ ++ ++ A ++ B ++ C ++ ++ ++ ++ ++ XML; ++} ++ ++function decode(SoapServer $server, string $request): string { ++ ob_start(); ++ $server->handle($request); ++ $response = ob_get_clean(); ++ ++ if (str_contains($response, '')) { ++ 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 c749117..055a67a 100644 +--- a/ext/standard/ftp_fopen_wrapper.c ++++ b/ext/standard/ftp_fopen_wrapper.c +@@ -141,7 +141,7 @@ static php_stream *php_ftp_fopen_connect(php_stream_wrapper *wrapper, const char + return NULL; + } + +- use_ssl = resource->scheme && (ZSTR_LEN(resource->scheme) > 3) && ZSTR_VAL(resource->scheme)[3] == 's'; ++ use_ssl = resource->scheme && zend_string_equals_literal_ci(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 f2b15f7..4691cef 100644 +--- a/ext/standard/http_fopen_wrapper.c ++++ b/ext/standard/http_fopen_wrapper.c +@@ -206,25 +206,21 @@ static 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 zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, ++ * header line. It returns true if the current line was folded into the last header line, ++ * otherwise false. */ ++static bool php_stream_http_response_headers_parse(php_stream_wrapper *wrapper, + php_stream *stream, php_stream_context *context, int options, +- zend_string *last_header_line_str, char *header_line, size_t *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 = ZSTR_VAL(last_header_line_str); +- size_t last_header_line_length = ZSTR_LEN(last_header_line_str); +- char *last_header_line_end = ZSTR_VAL(last_header_line_str) + ZSTR_LEN(last_header_line_str) - 1; +- + /* Process non empty header line. */ + if (header_line && (*header_line != '\n' && *header_line != '\r')) { + /* Removing trailing white spaces. */ + 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_str; ++ return true; + } + + /* Process folding headers if starting with a space or a tab. */ +@@ -239,18 +235,18 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w + /* 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. */ +- zend_string *extended_header_str = zend_string_concat3( +- last_header_line, last_header_line_length, +- " ", 1, +- http_folded_header_line, http_folded_header_line_length); +- zend_string_efree(last_header_line_str); +- last_header_line_str = extended_header_str; +- /* Return new header line. */ +- return last_header_line_str; ++ /* 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 true; + } + } + ++ zend_string *last_header_line_str = smart_str_extract(last_header); ++ char *last_header_line = ZSTR_VAL(last_header_line_str); ++ size_t last_header_line_length = ZSTR_LEN(last_header_line_str); ++ char *last_header_line_end = ZSTR_VAL(last_header_line_str) + ZSTR_LEN(last_header_line_str) - 1; ++ + /* Find header separator position. */ + char *last_header_value = memchr(last_header_line, ':', last_header_line_length); + if (last_header_value) { +@@ -262,7 +258,7 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w + php_stream_wrapper_log_error(wrapper, options, + "HTTP invalid response format (space in header name)!"); + zend_string_efree(last_header_line_str); +- return NULL; ++ return false; + } + ++last_header_name; + } +@@ -280,7 +276,7 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w + php_stream_wrapper_log_error(wrapper, options, + "HTTP invalid response format (no colon in header line)!"); + zend_string_efree(last_header_line_str); +- return NULL; ++ return false; + } + + bool store_header = true; +@@ -305,7 +301,7 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w + "HTTP Location header size is over the limit of %d bytes", + HTTP_HEADER_MAX_LOCATION_SIZE); + zend_string_efree(last_header_line_str); +- return NULL; ++ return false; + } + if (header_info->location_len == 0) { + header_info->location = emalloc(last_header_value_len + 1); +@@ -353,7 +349,7 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w + zend_string_efree(last_header_line_str); + } + +- return NULL; ++ return false; + } + + static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, +@@ -370,7 +366,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, + zval *ua_zval = NULL, *tmpzval = NULL, ssl_proxy_peer_name; + int reqok = 0; + char *http_header_line = NULL; +- zend_string *last_header_line_str = NULL; ++ smart_str last_header = {0}; + php_stream_http_response_header_info header_info; + char tmp_line[128]; + size_t chunk_size = 0; +@@ -428,7 +424,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, + request_fulluri = zend_is_true(tmpzval); + } + +- use_ssl = resource->scheme && (ZSTR_LEN(resource->scheme) > 4) && ZSTR_VAL(resource->scheme)[4] == 's'; ++ use_ssl = zend_string_equals_literal_ci(resource->scheme, "https"); + /* choose default ports */ + if (use_ssl && resource->port == 0) + resource->port = 443; +@@ -1015,12 +1011,12 @@ finish: + last_line = false; + } + +- if (last_header_line_str != NULL) { ++ if (last_header.s != NULL) { + /* Parse last header line. */ +- last_header_line_str = php_stream_http_response_headers_parse(wrapper, stream, +- context, options, last_header_line_str, http_header_line, ++ bool 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_str == NULL)) { ++ if (EXPECTED(!folded)) { + if (UNEXPECTED(header_info.error)) { + php_stream_close(stream); + stream = NULL; +@@ -1044,20 +1040,20 @@ finish: + } + if (last_line) { + /* For the last line the last header line must be NULL. */ +- ZEND_ASSERT(last_header_line_str == NULL); ++ ZEND_ASSERT(last_header.s == NULL); + break; + } + /* Save current line as the last line so it gets parsed in the next round. */ +- last_header_line_str = zend_string_init(http_header_line, http_header_line_length, 0); ++ 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_str != NULL) { ++ if (last_header.s != NULL) { + php_stream_http_response_headers_parse(wrapper, stream, context, options, +- last_header_line_str, NULL, NULL, response_code, response_header, &header_info); ++ &last_header, NULL, NULL, response_code, response_header, &header_info); + } + + if (!reqok || (header_info.location != NULL && header_info.follow_location)) { +@@ -1186,6 +1182,7 @@ finish: + out: + + smart_str_free(&req_buf); ++ 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..11bfa68 +--- /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.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..8c70015 +--- /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 57c30e5..6979d05 100644 +--- a/main/SAPI.c ++++ b/main/SAPI.c +@@ -494,7 +494,8 @@ SAPI_API void sapi_deactivate_module(void) + 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/fastcgi.c b/main/fastcgi.c +index 49cb8e1..fcd61b4 100644 +--- a/main/fastcgi.c ++++ b/main/fastcgi.c +@@ -1046,6 +1046,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->ended = 0; + req->in_len = 0; +@@ -1309,6 +1313,8 @@ 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; + + req->hook.on_close(); + } +diff --git a/main/main.c b/main/main.c +index b9994cc..1b4b963 100644 +--- a/main/main.c ++++ b/main/main.c +@@ -822,15 +822,7 @@ PHPAPI ZEND_COLD void php_log_err_with_severity(const char *log_message, int sys + zend_string *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); +- } else { +- error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 0); +- } +-#else +- error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 1); +-#endif ++ error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, PG(modules_activated) && EG(active)); + len = spprintf(&tmp, 0, "[%s] %s%s", ZSTR_VAL(error_time_str), log_message, PHP_EOL); + #ifdef PHP_WIN32 + php_flock(fd, LOCK_EX); +diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c +index c7bb7cc..17cec15 100644 +--- a/sapi/cgi/cgi_main.c ++++ b/sapi/cgi/cgi_main.c +@@ -1284,9 +1284,9 @@ static void init_request_info(fcgi_request *request) + */ + size_t slen = len - strlen(pt); + size_t 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/tests/ghsa-5j3f-64r7-f564.phpt b/sapi/cgi/tests/ghsa-5j3f-64r7-f564.phpt +new file mode 100644 +index 0000000..cab27ad +--- /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( ++ [$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/fpm_status.c b/sapi/fpm/fpm/fpm_status.c +index 81292aa..daf07c3 100644 +--- a/sapi/fpm/fpm/fpm_status.c ++++ b/sapi/fpm/fpm/fpm_status.c +@@ -45,6 +45,32 @@ int fpm_status_init_child(struct fpm_worker_pool_s *wp) /* {{{ */ + } + /* }}} */ + ++static char *fpm_status_escape(const char *str, bool encode_html, bool encode_json, zend_string **tmp) ++{ ++ char *escaped; ++ ++ *tmp = NULL; ++ if (str[0] == '\0') { ++ return NULL; ++ } ++ if (encode_html) { ++ *tmp = php_escape_html_entities_ex((const unsigned char *) str, strlen(str), 1, ++ ENT_DISALLOWED | ENT_HTML_DOC_XML1 | ENT_COMPAT, NULL, /* double_encode */ 1, /* quiet */ 0); ++ return ZSTR_VAL(*tmp); ++ } ++ if (encode_json) { ++ *tmp = php_json_encode_string(str, strlen(str), PHP_JSON_INVALID_UTF8_IGNORE); ++ escaped = ZSTR_VAL(*tmp); ++ /* remove quotes around the string */ ++ if (ZSTR_LEN(*tmp) >= 2) { ++ escaped[ZSTR_LEN(*tmp) - 1] = '\0'; ++ ++escaped; ++ } ++ return escaped; ++ } ++ return (char *) str; ++} ++ + int fpm_status_export_to_zval(zval *status) + { + struct fpm_scoreboard_s scoreboard, *scoreboard_p; +@@ -525,8 +551,10 @@ int fpm_status_handle_request(void) /* {{{ */ + if (full_syntax) { + unsigned int i; + int first; +- zend_string *tmp_query_string, *tmp_request_uri_string; +- char *query_string, *request_uri_string; ++ zend_string *tmp_request_method_string, *tmp_request_uri_string, *tmp_query_string; ++ zend_string *tmp_auth_user_string, *tmp_script_filename_string; ++ char *request_method_string, *request_uri_string, *query_string; ++ char *auth_user_string, *script_filename_string; + struct timeval duration, now; + float cpu; + +@@ -551,52 +579,11 @@ int fpm_status_handle_request(void) /* {{{ */ + } + } + +- request_uri_string = NULL; +- tmp_request_uri_string = NULL; +- if (proc->request_uri[0] != '\0') { +- if (encode_html) { +- tmp_request_uri_string = php_escape_html_entities_ex( +- (const unsigned char *) proc->request_uri, +- strlen(proc->request_uri), 1, ENT_DISALLOWED | ENT_HTML_DOC_XML1 | ENT_COMPAT, +- NULL, /* double_encode */ 1, /* quiet */ 0); +- request_uri_string = ZSTR_VAL(tmp_request_uri_string); +- } else if (encode_json) { +- tmp_request_uri_string = php_json_encode_string(proc->request_uri, +- strlen(proc->request_uri), PHP_JSON_INVALID_UTF8_IGNORE); +- request_uri_string = ZSTR_VAL(tmp_request_uri_string); +- /* remove quotes around the string */ +- if (ZSTR_LEN(tmp_request_uri_string) >= 2) { +- request_uri_string[ZSTR_LEN(tmp_request_uri_string) - 1] = '\0'; +- ++request_uri_string; +- } +- } else { +- request_uri_string = proc->request_uri; +- } +- } +- +- query_string = NULL; +- tmp_query_string = NULL; +- if (proc->query_string[0] != '\0') { +- if (encode_html) { +- tmp_query_string = php_escape_html_entities_ex( +- (const unsigned char *) proc->query_string, +- strlen(proc->query_string), 1, ENT_DISALLOWED | ENT_HTML_DOC_XML1 | ENT_COMPAT, +- NULL, /* double_encode */ 1, /* quiet */ 0); +- } else if (encode_json) { +- tmp_query_string = php_json_encode_string(proc->query_string, +- strlen(proc->query_string), PHP_JSON_INVALID_UTF8_IGNORE); +- } else { +- query_string = proc->query_string; +- } +- if (tmp_query_string) { +- query_string = ZSTR_VAL(tmp_query_string); +- /* remove quotes around the string */ +- if (encode_json && ZSTR_LEN(tmp_query_string) >= 2) { +- query_string[ZSTR_LEN(tmp_query_string) - 1] = '\0'; +- ++query_string; +- } +- } +- } ++ request_method_string = fpm_status_escape(proc->request_method, encode_html, encode_json, &tmp_request_method_string); ++ request_uri_string = fpm_status_escape(proc->request_uri, encode_html, encode_json, &tmp_request_uri_string); ++ query_string = fpm_status_escape(proc->query_string, encode_html, encode_json, &tmp_query_string); ++ auth_user_string = fpm_status_escape(proc->auth_user, encode_html, encode_json, &tmp_auth_user_string); ++ script_filename_string = fpm_status_escape(proc->script_filename, encode_html, encode_json, &tmp_script_filename_string); + + /* prevent NaN */ + if (proc->cpu_duration.tv_sec == 0 && proc->cpu_duration.tv_usec == 0) { +@@ -618,24 +605,33 @@ int fpm_status_handle_request(void) /* {{{ */ + (unsigned long) (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 : "-", + proc->request_stage == FPM_REQUEST_ACCEPTING ? cpu : 0., + proc->request_stage == FPM_REQUEST_ACCEPTING ? proc->memory : 0); + PUTS(buffer); + efree(buffer); + ++ if (tmp_request_method_string) { ++ zend_string_free(tmp_request_method_string); ++ } + if (tmp_request_uri_string) { + zend_string_free(tmp_request_uri_string); + } + if (tmp_query_string) { + zend_string_free(tmp_query_string); + } ++ if (tmp_auth_user_string) { ++ zend_string_free(tmp_auth_user_string); ++ } ++ if (tmp_script_filename_string) { ++ zend_string_free(tmp_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..0a40a5a +--- /dev/null ++++ b/sapi/fpm/tests/ghsa-3mg3-cr28-46gc-status-xss.phpt +@@ -0,0 +1,70 @@ ++--TEST-- ++FPM: GHSA-3mg3-cr28-46gc - status xss via request method, auth user and script filename ++--SKIPIF-- ++ ++--FILE-- ++.php'; ++file_put_contents($scriptFilename, $code); ++ ++$tester = new FPM\Tester($cfg, $code); ++$tester->start(); ++$tester->expectLogStartNotices(); ++$responses = $tester ++ ->multiRequest([ ++ [ ++ 'uri' => '/ghsa-3mg3-cr28-46gc-.php', ++ 'headers' => [ ++ 'REQUEST_METHOD' => '', ++ 'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('":x'), ++ 'SCRIPT_FILENAME' => $scriptFilename, ++ ], ++ ], ++ ['uri' => '/status', 'query' => 'full&html', 'delay' => 100000], ++ ['uri' => '/status', 'query' => 'full&json', 'delay' => 100000], ++ ]); ++var_dump(strpos($responses[1]->getBody(), 'getBody(), '<svg')); ++$status = json_decode($responses[2]->getBody('application/json'), true); ++foreach ($status['processes'] as $proc) { ++ if ($proc['request method'] === '') { ++ var_dump($proc['user'], $proc['script'] === $scriptFilename); ++ } ++} ++$tester->terminate(); ++$tester->expectLogTerminatingNotices(); ++$tester->close(); ++ ++?> ++Done ++--EXPECT-- ++bool(false) ++int(4) ++string(15) """ ++bool(true) ++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..42a9d3e +--- /dev/null ++++ b/sapi/fpm/tests/unread-body-keep-conn.phpt +@@ -0,0 +1,125 @@ ++--TEST-- ++FPM: unread request body cannot become a new request on a kept connection ++--SKIPIF-- ++ ++--FILE-- ++ $length < 128 ++ ? chr($length) ++ : pack('N', $length | 0x80000000); ++ foreach ($params as $name => $value) { ++ $encoded .= $encodeLength(strlen($name)) . $encodeLength(strlen($value)) . $name . $value; ++ } ++ return $encoded; ++} ++ ++function request(int $id, bool $keep, array $params, string $stdin): string ++{ ++ 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(string $address, string $request): string ++{ ++ [$host, $port] = explode(':', $address); ++ $socket = fsockopen($host, (int) $port); ++ stream_set_timeout($socket, 3); ++ $written = 0; ++ while ($written < strlen($request)) { ++ $written += fwrite($socket, substr($request, $written)); ++ } ++ stream_socket_shutdown($socket, STREAM_SHUT_WR); ++ $response = stream_get_contents($socket); ++ fclose($socket); ++ return $response; ++} ++ ++$config = <<makeSourceFile(); ++$tester->start(); ++$tester->expectLogStartNotices(); ++ ++$hiddenParams = [ ++ 'GATEWAY_INTERFACE' => '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 = [ ++ '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 ([false, true] as $keep) { ++ $response = transact($tester->getAddr(), 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($tester->getAddr(), request(1, true, $outerParams, $body)); ++echo 'partial frame: ', ++ strpos($response, 'smuggled') !== false ? 'smuggled' : 'safe', "\n"; ++ ++$tester->terminate(); ++$tester->close(); ++?> ++--EXPECT-- ++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..5bd1ea7 +--- /dev/null ++++ b/sapi/fpm/tests/unread-body-partial-record.phpt +@@ -0,0 +1,127 @@ ++--TEST-- ++FPM: partially read STDIN record does not make the worker reject later connections ++--SKIPIF-- ++ ++--FILE-- ++ $length < 128 ++ ? chr($length) ++ : pack('N', $length | 0x80000000); ++ foreach ($params as $name => $value) { ++ $encoded .= $encodeLength(strlen($name)) . $encodeLength(strlen($value)) . $name . $value; ++ } ++ return $encoded; ++} ++ ++function request(int $id, bool $keep, array $params, string $stdin): string ++{ ++ 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(string $address, string $request): string ++{ ++ [$host, $port] = explode(':', $address); ++ $socket = fsockopen($host, (int) $port); ++ stream_set_timeout($socket, 3); ++ $written = 0; ++ while ($written < strlen($request)) { ++ $written += fwrite($socket, substr($request, $written)); ++ } ++ stream_socket_shutdown($socket, STREAM_SHUT_WR); ++ $response = stream_get_contents($socket); ++ fclose($socket); ++ return $response; ++} ++ ++$config = <<makeSourceFile(); ++$tester->start(); ++$tester->expectLogStartNotices(); ++ ++$hiddenParams = [ ++ 'GATEWAY_INTERFACE' => '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 = [ ++ '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 = ['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 ([false, true] as $keep) { ++ $response = transact($tester->getAddr(), 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 ([1, 2] as $n) { ++ $response = transact($tester->getAddr(), request(1, false, $plainParams, '')); ++ echo " next connection $n: ", strpos($response, 'outer 0') !== false ? 'served' : 'not served', "\n"; ++ } ++} ++ ++$tester->terminate(); ++$tester->close(); ++?> ++--EXPECT-- ++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-- ++