Repository navigation
[8.6] [MOD-18088] strconv: fix heap-buffer-overflow read in unicode_tolower for a dangling multi-byte UTF-8 lead byte - #11487
Conversation
… for a dangling multi-byte UTF-8 lead byte (#11202) * test_tags: add regression test reproducing a tag lowering overread unicode_tolower (src/util/strconv.h) decodes a case-insensitive tag token with nu_utf8_read, which has no bounds checking: for a lead byte >= 0xf0 it unconditionally reads the 3 bytes following it (utf8_4b in deps/libnu/utf8_internal.h). A token ending in such a byte -- valid or not, 0xf0-0xff all dispatch to the same 4-byte path -- makes the decoder read past the token's allocation. Add testTagInvalidUtf8LoweringOverflow, covering the exact-token, prefix, and wildcard branches (each of which calls tag_strtolower independently in src/query.c). The read doesn't reliably crash a plain build, so the test's own value is as a regression guard under AddressSanitizer, verified by CI rather than this run -- matching how a prior, unrelated strToLowerRunes overflow fix in this codebase tests the same class of bug (survive-and-still-serve, rather than asserting exact output). See https://github.com/RediSearch/RediSearch/issues/11135. * strconv: fix heap-buffer-overflow read in unicode_tolower for a dangling multi-byte UTF-8 lead byte nu_utf8_read (deps/libnu/utf8.h) has no bounds checking: for a lead byte declaring a 2/3/4-byte sequence it unconditionally reads that many bytes via utf8_2b/utf8_3b/utf8_4b (deps/libnu/utf8_internal.h). Both decoding passes in unicode_tolower (the nu_strtransformnlen measuring pass and the main decode loop) call it once per codepoint up to the string's length, so a lead byte near the end whose declared sequence extends past that length reads past the caller's allocation -- for a 3- or 4-byte lead this is genuinely out of bounds, since only the NUL terminator every caller here supplies is ever guaranteed to exist past the string's logical length. 0xf5-0xff are not even valid UTF-8 lead bytes, but nu_utf8_read still reads them as 4-byte leads (the dispatch only checks >= 0xf0). This is reachable through an ordinary FT.SEARCH: a case-insensitive TAG field lowercases its query token through this path, and any exact, prefix, or wildcard tag token ending in such a byte triggers it. Find how many trailing bytes belong to such a dangling sequence -- at most 3, since UTF-8's longest sequence is 4 bytes -- before either decoding pass runs, and exclude them: they are copied through unchanged in the output instead of being folded, since they cannot be decoded without reading past the allocation. Add direct unit test coverage in test_cpp_unicode_tolower.cpp for a dangling 2/3/4-byte lead (including a byte that is not a valid lead at all), each pinning the exact output. The pytest regression test in the previous commit (testTagInvalidUtf8LoweringOverflow) exercises the same fix end-to-end through FT.SEARCH. (cherry picked from commit e4cc48b)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a7c5af56b5
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| # Prefix token, through Query_EvalTagPrefixNode. | ||
| _assertSurvivesInvalidUtf8TagToken(env, conn, b'@t:{caf' + _INVALID_UTF8_LEAD + b'*}') | ||
| # Wildcard token, through Query_EvalTagWildcardNode. | ||
| _assertSurvivesInvalidUtf8TagToken(env, conn, b"@t:{w'caf" + _INVALID_UTF8_LEAD + b"*'}") |
There was a problem hiding this comment.
Execute the wildcard check with dialect 2
This test uses the server's default dialect 1 (src/config.h:310), but the w'…' wildcard token is emitted only by the v2 lexer (src/query_parser/v2/lexer.rl). Consequently this command is parsed as dialect-1 terms/affixes or rejected, and the helper swallows any rejection, so the claimed Query_EvalTagWildcardNode regression path is never verified. Add DIALECT 2 to this command or construct the test environment with DEFAULT_DIALECT 2.
Useful? React with 👍 / 👎.
|



Backport of #11202 to
8.6.Original PR
e4cc48bd6b307f22aaa8fa1379afa566f6a0ba21Cherry-pick result
Conflict Log
tests/pytests/test_tags.pyRelease notes
🤖 Generated by the auto-backport workflow.