Skip to content

Commit 9d6c59e

Browse files
committed
Fix bug #77418 - Heap overflow in utf32be_mbc_to_code
1 parent b51eaf4 commit 9d6c59e

File tree

6 files changed

+25
-5
lines changed

6 files changed

+25
-5
lines changed

NEWS

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ PHP NEWS
33
?? ??? 2018, PHP 5.6.40
44

55
- GD:
6-
. Fixed bug #77269 (efree() on uninitialized Heap data in imagescale leads to
6+
. Fixed bug #77269 (efree() on uninitialized Heap data in imagescale leads to
77
use-after-free). (cmb)
88
. Fixed bug #77270 (imagecolormatch Out Of Bounds Write on Heap). (cmb)
99

1010
- Mbstring:
1111
. Fixed bug #77370 (Buffer overflow on mb regex functions - fetch_token). (Stas)
12-
. Fixed bug #77371 (heap buffer overflow in mb regex functions
12+
. Fixed bug #77371 (heap buffer overflow in mb regex functions
1313
- compile_string_node). (Stas)
1414
. Fixed bug #77381 (heap buffer overflow in multibyte match_at). (Stas)
15-
. Fixed bug #77382 (heap buffer overflow due to incorrect length in
15+
. Fixed bug #77382 (heap buffer overflow due to incorrect length in
1616
expand_case_fold_string). (Stas)
1717
. Fixed bug #77385 (buffer overflow in fetch_token). (Stas)
1818
. Fixed bug #77394 (Buffer overflow in multibyte case folding - unicode). (Stas)
19+
. Fixed bug #77418 (Heap overflow in utf32be_mbc_to_code). (Stas)
1920

2021
- Phar:
2122
. Fixed bug #77247 (heap buffer overflow in phar_detect_phar_fname_ext). (Stas)

ext/mbstring/oniguruma/enc/utf16_be.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,18 @@ utf16be_is_mbc_newline(const UChar* p, const UChar* end)
7575
}
7676

7777
static OnigCodePoint
78-
utf16be_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
78+
utf16be_mbc_to_code(const UChar* p, const UChar* end)
7979
{
8080
OnigCodePoint code;
8181

8282
if (UTF16_IS_SURROGATE_FIRST(*p)) {
83+
if (end - p < 4) return 0;
8384
code = ((((p[0] - 0xd8) << 2) + ((p[1] & 0xc0) >> 6) + 1) << 16)
8485
+ ((((p[1] & 0x3f) << 2) + (p[2] - 0xdc)) << 8)
8586
+ p[3];
8687
}
8788
else {
89+
if (end - p < 2) return 0;
8890
code = p[0] * 256 + p[1];
8991
}
9092
return code;

ext/mbstring/oniguruma/enc/utf16_le.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@ utf16le_is_mbc_newline(const UChar* p, const UChar* end)
8181
}
8282

8383
static OnigCodePoint
84-
utf16le_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
84+
utf16le_mbc_to_code(const UChar* p, const UChar* end)
8585
{
8686
OnigCodePoint code;
8787
UChar c0 = *p;
8888
UChar c1 = *(p+1);
8989

9090
if (UTF16_IS_SURROGATE_FIRST(c1)) {
91+
if (end - p < 4) return 0;
9192
code = ((((c1 - 0xd8) << 2) + ((c0 & 0xc0) >> 6) + 1) << 16)
9293
+ ((((c0 & 0x3f) << 2) + (p[3] - 0xdc)) << 8)
9394
+ p[2];

ext/mbstring/oniguruma/enc/utf32_be.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ utf32be_is_mbc_newline(const UChar* p, const UChar* end)
6060
static OnigCodePoint
6161
utf32be_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
6262
{
63+
if (end - p < 4) return 0;
6364
return (OnigCodePoint )(((p[0] * 256 + p[1]) * 256 + p[2]) * 256 + p[3]);
6465
}
6566

ext/mbstring/oniguruma/enc/utf32_le.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ utf32le_is_mbc_newline(const UChar* p, const UChar* end)
6060
static OnigCodePoint
6161
utf32le_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
6262
{
63+
if (end - p < 4) return 0;
6364
return (OnigCodePoint )(((p[3] * 256 + p[2]) * 256 + p[1]) * 256 + p[0]);
6465
}
6566

ext/mbstring/tests/bug77418.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Bug #77371 (Heap overflow in utf32be_mbc_to_code)
3+
--SKIPIF--
4+
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
5+
--FILE--
6+
<?php
7+
mb_regex_encoding("UTF-32");
8+
var_dump(mb_split("\x00\x00\x00\x5c\x00\x00\x00B","000000000000000000000000000000"));
9+
?>
10+
--EXPECT--
11+
array(1) {
12+
[0]=>
13+
string(30) "000000000000000000000000000000"
14+
}

0 commit comments

Comments
 (0)