Skip to content

Commit af830d8

Browse files
committed
Merge branch 'PHP-8.3'
* PHP-8.3: Fix GH-15034: Integer overflow on stream_notification_callback byte_max parameter with files bigger than 2GB
2 parents 9b78e5f + c26d1a3 commit af830d8

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

ext/standard/http_fopen_wrapper.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,19 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
792792
} else if (!strncasecmp(http_header_line, "Content-Type:", sizeof("Content-Type:")-1)) {
793793
php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_value, 0);
794794
} else if (!strncasecmp(http_header_line, "Content-Length:", sizeof("Content-Length:")-1)) {
795-
file_size = atoi(http_header_value);
796-
php_stream_notify_file_size(context, file_size, http_header_line, 0);
795+
/* https://www.rfc-editor.org/rfc/rfc9110.html#name-content-length */
796+
const char *ptr = http_header_value;
797+
/* must contain only digits, no + or - symbols */
798+
if (*ptr >= '0' && *ptr <= '9') {
799+
char *endptr = NULL;
800+
size_t parsed = ZEND_STRTOUL(ptr, &endptr, 10);
801+
/* check whether there was no garbage in the header value and the conversion was successful */
802+
if (endptr && !*endptr) {
803+
/* truncate for 32-bit such that no negative file sizes occur */
804+
file_size = MIN(parsed, ZEND_LONG_MAX);
805+
php_stream_notify_file_size(context, file_size, http_header_line, 0);
806+
}
807+
}
797808
} else if (
798809
!strncasecmp(http_header_line, "Transfer-Encoding:", sizeof("Transfer-Encoding:")-1)
799810
&& !strncasecmp(http_header_value, "Chunked", sizeof("Chunked")-1)

ext/standard/tests/http/gh15034.phpt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
GH-15034 (Integer overflow on stream_notification_callback byte_max parameter with files bigger than 2GB)
3+
--SKIPIF--
4+
<?php
5+
require 'server.inc';
6+
http_server_skipif();
7+
if (PHP_INT_SIZE != 8) die("skip 64-bit only");
8+
?>
9+
--INI--
10+
allow_url_fopen=1
11+
--FILE--
12+
<?php
13+
require 'server.inc';
14+
15+
$responses = [
16+
"data://text/plain,HTTP/1.1 200 OK\r\n"
17+
. "Content-Type: text/plain\r\n"
18+
. "Content-Length: 3000000000\r\n\r\n"
19+
. "foo\n",
20+
];
21+
['pid' => $pid, 'uri' => $uri] = http_server($responses);
22+
23+
$params = ['notification' => function(
24+
int $notification_code,
25+
int $severity,
26+
?string $message,
27+
int $message_code,
28+
int $bytes_transferred,
29+
int $bytes_max
30+
) {
31+
global $max;
32+
$max = $bytes_max;
33+
}];
34+
$contextResource = stream_context_create([], $params);
35+
36+
$resource = fopen($uri, 'r', false, $contextResource);
37+
fclose($resource);
38+
39+
http_server_kill($pid);
40+
41+
var_dump($max);
42+
?>
43+
--EXPECT--
44+
int(3000000000)

0 commit comments

Comments
 (0)