Skip to content

[MOD-18088] strconv: fix heap-buffer-overflow read in unicode_tolower for a dangling multi-byte UTF-8 lead byte - #11202

Merged
gdesmott merged 2 commits into
masterfrom
gd_tag_over_read
Aug 28, 2026
Merged

gdesmott merged 2 commits into
masterfrom
gd_tag_over_read

Conversation

@gdesmott

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

  1. Current: unicode_tolower (src/util/strconv.h) decodes a token with
    nu_utf8_read, which has no bounds checking — a lead byte >= 0xf0
    (valid or not) makes it unconditionally read the 3 bytes following it.
    A case-insensitive TAG query token ending in such a byte makes the
    decoder read past the token's allocation, a heap-buffer-overflow read
    reachable from FT.SEARCH.
  2. Change: bound the decode/measure passes to a safe_len that excludes
    any trailing lead byte whose declared multi-byte sequence would
    extend past the token; those excluded tail bytes are passed through
    unchanged instead of being decoded.
  3. Outcome: invalid or truncated UTF-8 at the end of a tag token no
    longer reads out of bounds. The server stays up and still answers
    queries after such a token; the excluded tail is left unmodified in
    the token.

Which additional issues this PR fixes

  1. https://github.com/RediSearch/RediSearch/issues/11135

Main objects this PR modified

  1. unicode_tolower (src/util/strconv.h) — the fix itself.
  2. tag_strtolower / TAG query evaluation (src/query.c) — the reachable
    call site from FT.SEARCH on a case-insensitive TAG field.
  3. test_cpp_unicode_tolower.cpp — 4 new unit tests exercising dangling
    4-byte, 3-byte, and invalid (0xff) lead bytes directly.
  4. test_tags.py — testTagInvalidUtf8LoweringOverflow, covering the
    exact-token, prefix, and wildcard tag-query branches end to end.

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Release Notes

  • This PR requires release notes
  • This PR does not require release notes

If a release note is required (bug fix / new feature / enhancement), describe the user impact of this PR in the title.


Note

Medium Risk
Security-relevant memory-safety fix in shared string lowering used by query parsing; behavior for malformed UTF-8 tails changes (pass-through vs unsafe decode) but scope is bounded to invalid/truncated suffixes.

Overview
Fixes a heap-buffer-overflow when unicode_tolower decodes tokens whose last byte looks like a multi-byte UTF-8 lead (e.g. 0xff) via unbounded nu_utf8_read. That path is hit on case-insensitive TAG queries (FT.SEARCH) when tokens are lowercased.

The change adds a forward safe-length scan: decode/measure only bytes before the first incomplete sequence or decoded NUL (including overlong 0xC0 0x80). Any excluded suffix is copied through unchanged after the lowered prefix, using in-place memmove or a larger longer_dst when casing expansion needs more room.

Tests add C++ coverage for dangling/invalid leads, embedded NUL, and expansion + tail; Python testTagInvalidUtf8LoweringOverflow checks the server stays alive for exact, prefix, and wildcard tag tokens ending in 0xff.

Reviewed by Cursor Bugbot for commit a622aaf8ee36b95f0f84ab69ee9602e6d312d266. Bugbot is set up for automated code reviews on this repo. Configure here.

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.
@gdesmott
gdesmott requested a review from memark August 27, 2026 08:36
@gdesmott

Copy link
Copy Markdown
Collaborator Author

@memark pointed out:

It also seems the bug this PR solves might be present in more places:
strToLowerRunes, strToSingleCodepointFoldedRunes, strToRunes all drive nu_utf8_read/nu_strnlen/nu_readnstr over > utf8_len with no dangling-sequence guard.

Might make sense to fix in same PR (extracting some common code perhaps) or in a follow-up.

@codecov

codecov Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.62500% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.25%. Comparing base (38959af) to head (5712120).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
src/util/strconv.h 90.62% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11202      +/-   ##
==========================================
- Coverage   83.77%   83.25%   -0.53%     
==========================================
  Files         631      657      +26     
  Lines       83137    84590    +1453     
  Branches    38390    39635    +1245     
==========================================
+ Hits        69652    70427     +775     
- Misses      13130    13800     +670     
- Partials      355      363       +8     
Flag Coverage Δ
flow 84.62% <90.62%> (+0.03%) ⬆️
unit 57.51% <78.12%> (-0.22%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@memark memark left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@memark pointed out:

It also seems the bug this PR solves might be present in more places:
strToLowerRunes, strToSingleCodepointFoldedRunes, strToRunes all drive nu_utf8_read/nu_strnlen/nu_readnstr over > utf8_len with no dangling-sequence guard.
Might make sense to fix in same PR (extracting some common code perhaps) or in a follow-up.

What did we decide to do with it? 😄

Comment thread tests/pytests/test_tags.py Resolved
Comment thread src/util/strconv.h Resolved
@gdesmott

Copy link
Copy Markdown
Collaborator Author

@memark pointed out:

It also seems the bug this PR solves might be present in more places:
strToLowerRunes, strToSingleCodepointFoldedRunes, strToRunes all drive nu_utf8_read/nu_strnlen/nu_readnstr over > utf8_len with no dangling-sequence guard.
Might make sense to fix in same PR (extracting some common code perhaps) or in a follow-up.

What did we decide to do with it? 😄

I agree but don't have time to look at those now. Hopefully next week, if not I'll open a task to remind us about it.

…ing 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.
@gdesmott
gdesmott requested a review from memark August 27, 2026 14:26
@sonarqubecloud

Copy link
Copy Markdown

@gdesmott
gdesmott added this pull request to the merge queue Aug 28, 2026
Merged via the queue into master with commit e4cc48b Aug 28, 2026
61 checks passed
@gdesmott
gdesmott deleted the gd_tag_over_read branch August 28, 2026 09:39
@nafraf

nafraf commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

/backport

@nafraf

nafraf commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

/backport-agent 8.10 8.8-rse 8.8

@nafraf

nafraf commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

/backport 8.10

@redisearch-backport-pull-request

redisearch-backport-pull-request Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Auto-backport summary

Target Status Backport PR
8.10 clean #11484

(PR #11202, e4cc48bd6 — generated by the auto-backport workflow.)

Workflow run · Retry unfinished targets with /backport <branches>.

@nafraf

nafraf commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

/backport 8.8 8.8-rse 8.6 8.6-rse 8.4

@redisearch-backport-pull-request

redisearch-backport-pull-request Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Auto-backport summary

Target Status Backport PR
8.8 conflicts(1) #11486
8.8-rse clean #11485
8.6 conflicts(1) #11487
8.6-rse conflicts(1) #11488
8.4 conflicts(1) #11489

(PR #11202, e4cc48bd6 — generated by the auto-backport workflow.)

Workflow run · Retry unfinished targets with /backport <branches>.

pull Bot pushed a commit to Mu-L/RediSearch that referenced this pull request Sep 29, 2026
* query_eval: test lowering a tag token ending in a dangling UTF-8 lead byte

RediSearch#11202 fixed the
unicode_tolower() bug so we can now test this.

* redisearch: expose the BAD_POINTER sentinel address to Rust

Define BAD_POINTER in terms of an integer BAD_POINTER_ADDR macro and bind
it, so Rust callers can recognize the sentinel some C helpers return.

* c_trie: read the clock deadline from a request timeout

Add QueryRequestTimeoutHandle::clock_deadline, which reports the deadline
of a clock-driven timeout cycle and None for any other source.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants