Repository navigation
Fix GH-23106 / GH-25025: Clamp pos in offset_to_pointer_utf8 to prevent out-of-bounds read on truncated UTF-8 - #25167
Closed
AdamMagued wants to merge 1 commit into
Closed
AdamMagued wants to merge 1 commit into
AdamMagued wants to merge 1 commit into
Conversation
… haystack Backport upstream fix cc22056 (phpGH-23106 / PR php#23107 by Lazizbek Ergashev) to PHP-8.3. When offset_to_pointer_utf8() performs a forward walk over a haystack ending in a truncated multi-byte sequence, the lead byte length exceeds the remaining buffer length, causing the returned pointer to overshoot the end pointer. This violates the end >= p invariant expected by callers such as zend_memnstr() and zend_memnrstr(), leading to assertion failures in debug builds and out-of-bounds reads in release builds. Clamp pos to end after the walk identical to master and PHP-8.4. Add regression test covering forward and negative offsets with truncated haystacks. Fixes phpGH-25025. Fixes phpGH-23106.
Member
|
Hi, 8.3 is a security-only branch and will not accept any general bug fixes. If you can prove to 8.3 RMs that this qualifies as a security issue, you can reopen this. Also, Yuya has mentioned this in the github issue. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes GH-25025 and GH-23106 for the PHP-8.3 branch.
Backport of master/8.4 commit cc22056 (PR #23107, credited to Lazizbek Ergashev), which resolved GH-23106 in 8.4.27 and 8.5.12.
Root Cause
In
ext/mbstring/mbstring.c,offset_to_pointer_utf8()walks the haystack forward usingmbfl_encoding_utf8.mblen_table:When
strends in a truncated multi-byte sequence (such as\xf0\x90),u8_tbl[*pos]advancesposby the full expected sequence length (4 bytes) rather than the remaining bytes in the buffer. The function then returns a pointer beyondend.When callers like
zend_memnstr()orzend_memnrstr()receive this pointer:assert(end >= p)invariant check inzend_operators.h.Fix
Add the clamp
if (pos > end) { pos = end; }identical to master/8.4 before returningpos.Regression Test
Ported and extended regression test in
ext/mbstring/tests/gh23106.phptverifying truncated UTF-8 haystacks with positive and negative offsets undermb_strpos()andmb_strrpos().Verification
gh23106.phptreproduces the out-of-bounds read (mb_strpos("AA\xf0\x90", "x", 3)returnsint(29)instead ofbool(false)).gh23106.phptpasses cleanly.ext/mbstringtest suite in container sandbox: 416 passed, 0 failed, 9 skipped.