Skip to content

Commit 469170f

Browse files
committed
Fix GH-15034: Integer overflow on stream_notification_callback byte_max parameter with files bigger than 2GB
We were using atoi, which is only for integers. When the size does not fit in an integer this breaks. Use ZEND_STRTOUL instead. Also make sure invalid data isn't accidentally parsed into a file size.
1 parent 93a9a9b commit 469170f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

ext/standard/http_fopen_wrapper.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,18 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
791791
} else if (!strncasecmp(http_header_line, "Content-Type:", sizeof("Content-Type:")-1)) {
792792
php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_value, 0);
793793
} else if (!strncasecmp(http_header_line, "Content-Length:", sizeof("Content-Length:")-1)) {
794-
file_size = atoi(http_header_value);
795-
php_stream_notify_file_size(context, file_size, http_header_line, 0);
794+
/* https://www.rfc-editor.org/rfc/rfc9110.html#name-content-length */
795+
const char *ptr = http_header_value;
796+
/* must contain only digits, no + or - symbols */
797+
if (*ptr >= '0' && *ptr <= '9') {
798+
char* endptr = NULL;
799+
size_t parsed = ZEND_STRTOUL(ptr, &endptr, 10);
800+
/* check whether there was no garbage in the header value and the conversion was successful */
801+
if (endptr && !*endptr) {
802+
file_size = parsed;
803+
php_stream_notify_file_size(context, file_size, http_header_line, 0);
804+
}
805+
}
796806
} else if (
797807
!strncasecmp(http_header_line, "Transfer-Encoding:", sizeof("Transfer-Encoding:")-1)
798808
&& !strncasecmp(http_header_value, "Chunked", sizeof("Chunked")-1)

ext/standard/tests/http/gh15034.phpt

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

0 commit comments

Comments
 (0)