Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ext/standard/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ PHP_FUNCTION(request_parse_body)
Z_PARAM_ARRAY_HT_OR_NULL(options)
ZEND_PARSE_PARAMETERS_END();

if (SG(request_parse_body_context).in_progress) {
zend_throw_error(zend_ce_request_parse_body_exception, "Cannot call request_parse_body() while the request body is already being parsed");
RETURN_THROWS();
}

SG(request_parse_body_context).in_progress = true;
SG(request_parse_body_context).throw_exceptions = true;
if (options) {
if (cache_request_parse_body_options(options) == FAILURE) {
Expand Down Expand Up @@ -390,6 +396,7 @@ PHP_FUNCTION(request_parse_body)
RETVAL_ARR(zend_new_pair(&post, &files));

exit:
SG(request_parse_body_context).in_progress = false;
SG(request_parse_body_context).throw_exceptions = false;
memset(&SG(request_parse_body_context).options_cache, 0, sizeof(SG(request_parse_body_context).options_cache));
}
Expand Down
39 changes: 39 additions & 0 deletions ext/standard/tests/http/request_parse_body/gh24802.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
GH-24802 (request_parse_body() reentrancy double-frees protected variables)
--INI--
display_errors=0
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data; boundary=----X
------X
Content-Disposition: form-data; name="a[x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x]"

val
------X--
--FILE--
<?php
set_error_handler(function ($no, $str) {
static $done = false;
if (!$done) {
$done = true;
try {
request_parse_body();
} catch (Throwable $e) {
echo 'nested: ', get_class($e), ': ', $e->getMessage(), "\n";
}
}
return true;
});

[$_POST, $_FILES] = request_parse_body();
echo "outer done\n";
var_dump($_POST, $_FILES);
?>
--EXPECT--
nested: RequestParseBodyException: Cannot call request_parse_body() while the request body is already being parsed
outer done
array(0) {
}
array(0) {
}
1 change: 1 addition & 0 deletions main/SAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ SAPI_API void sapi_activate(void)
}
SG(rfc1867_uploaded_files) = NULL;
SG(request_parse_body_context).throw_exceptions = false;
SG(request_parse_body_context).in_progress = false;
memset(&SG(request_parse_body_context).options_cache, 0, sizeof(SG(request_parse_body_context).options_cache));

/* Handle request method */
Expand Down
1 change: 1 addition & 0 deletions main/SAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ typedef struct {

typedef struct {
bool throw_exceptions;
bool in_progress;
struct {
bool set;
zend_long value;
Expand Down
2 changes: 2 additions & 0 deletions sapi/cli/php_cli_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,7 @@ static void php_cli_server_request_shutdown(php_cli_server *server, php_cli_serv
SG(server_context) = NULL;
SG(rfc1867_uploaded_files) = NULL;
SG(request_parse_body_context).throw_exceptions = false;
SG(request_parse_body_context).in_progress = false;
memset(&SG(request_parse_body_context).options_cache, 0, sizeof(SG(request_parse_body_context).options_cache));
}
/* }}} */
Expand Down Expand Up @@ -2355,6 +2356,7 @@ static zend_result php_cli_server_dispatch(php_cli_server *server, php_cli_serve
SG(sapi_headers).send_default_content_type = 1;
SG(rfc1867_uploaded_files) = NULL;
SG(request_parse_body_context).throw_exceptions = false;
SG(request_parse_body_context).in_progress = false;
memset(&SG(request_parse_body_context).options_cache, 0, sizeof(SG(request_parse_body_context).options_cache));
}
if (FAILURE == php_cli_server_begin_send_static(server, client)) {
Expand Down
Loading