Skip to content

Commit abd159c

Browse files
committed
Fix bug #72114 - int/size_t confusion in fread
1 parent 95ed19a commit abd159c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

ext/standard/file.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,12 @@ PHPAPI PHP_FUNCTION(fread)
17581758
RETURN_FALSE;
17591759
}
17601760

1761+
if (len > INT_MAX) {
1762+
/* string length is int in 5.x so we can not read more than int */
1763+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be no more than %d", INT_MAX);
1764+
RETURN_FALSE;
1765+
}
1766+
17611767
Z_STRVAL_P(return_value) = emalloc(len + 1);
17621768
Z_STRLEN_P(return_value) = php_stream_read(stream, Z_STRVAL_P(return_value), len);
17631769

ext/standard/tests/file/bug72114.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Bug #72114 (Integer underflow / arbitrary null write in fread/gzread)
3+
--FILE--
4+
<?php
5+
ini_set('memory_limit', "2500M");
6+
$fp = fopen("/dev/zero", "r");
7+
fread($fp, 2147483648);
8+
?>
9+
Done
10+
--EXPECTF--
11+
Warning: fread(): Length parameter must be no more than 2147483647 in %s/bug72114.php on line %d
12+
Done

0 commit comments

Comments
 (0)