ICU-23485 Fix stack-buffer-underflow in _cmpFold - #4116
Conversation
|
To understand why c2 = -1; (and c1 = -1;) is the correct and necessary assignment, we have to look at the state-machine logic governing _cmpFold's main comparison loop.
The Original (Vulnerable) Code:Executed --s2; to move the pointer back. The Memory-Safe Patch:Safely positions the s2 pointer to the exact beginning of the desired surrogate (clamped to start2). SummarySetting c2 = -1 (and c1 = -1) leverages the native state-machine of the _cmpFold loop to perform the look-behind read implicitly and safely via the existing *s2++ architecture, entirely eliminating the need for *(s-1) pointer-subtraction arithmetic. |
|
I am still reviewing the PR. Please ignore it for now |
Let us know when it's ready (and remove the "incomplete" label). |
It's possible to click Convert to draft on a GitHub PR (upper right corner in the UI, just below the list of reviewers) to clearly signal to humans and machines alike that a PR is not yet ready. It can also be helpful to not assign any reviewers until a PR is actually ready for review. |
c14d70d to
bd5c626
Compare
|
Hooray! The files in the branch are the same across the force-push. 😃 ~ Your Friendly Jira-GitHub PR Checker Bot |
|
please review now. thanks |
074702e to
20dc7db
Compare
|
Notice: the branch changed across the force-push!
~ Your Friendly Jira-GitHub PR Checker Bot |
|
PTAL |
20dc7db to
51bcec1
Compare
|
Notice: the branch changed across the force-push!
~ Your Friendly Jira-GitHub PR Checker Bot |
Fix generated by AI
Summary
Fixes a critical Out-of-Bounds (OOB) Stack Read Underflow (
fold1[-1]/fold2[-1]) in the core_cmpFoldcase-insensitive Unicode string comparison implementation (ustrcase.cpp).Root Cause
During look-behind operations for trail-surrogate case-folding expansions:
level 0source, while the other string is actively emitting from alevel 1stack-allocatedfold1/fold2decomposition buffer.--s; ... c = *(s-1);).foldarray (fold[0]), the*(s-1)dereference targets address space positioned exactly 2 bytes prior to thefoldlocal buffer boundary.ASAN), this results in an immediate Stack-Buffer-Overflow (Read Underflow)SIGABRT.Solution / Patch Architecture
s - start >= 2bounds-validation prior to pointer arithmetic. Look-behinds evaluating below the active buffer'sstartorigin are safely clamped directly tostart.c = -1): Replaces the unguarded and unsafe*(s-1)pointer dereferences entirely by assigning-1to the respective comparative register (c1/c2). This redirects the look-behind fetch operation to_cmpFold's existing, bounds-checked*s++fetch-and-increment machinery on the subsequentfor(;;)loop iteration.m) Origin Protection: Wraps--m2reductions inside strictm > orgboundary validations to prevent secondary underflows of them1/m2string-prefix length cursors.Testing & Validation
strcase.cpp(New Unit Test:TestCmpFoldStackBufferUnderflow): Implements an ICUintltestC++ regression routine utilizing the exact multi-level UTF-16 surrogate alignment sequence isolated from the V8d8(/uiRegExp backreference) vulnerability report.u_strcmpFoldcomparison logic completes deterministically and reportsU_ZERO_ERROR.fold1stack-frame warnings under--config=asan.Jira Ticket Link: ICU-23485
Checklist