Skip to content

re2: clear thread_local hooks::context on Match return and CompileSet - #706

Open
professor-moody wants to merge 1 commit into
google:mainfrom
professor-moody:fix-hooks-context-dangling-after-match
Open

professor-moody wants to merge 1 commit into
google:mainfrom
professor-moody:fix-hooks-context-dangling-after-match

Conversation

@professor-moody

Copy link
Copy Markdown

Summary

Clear the thread_local pointer hooks::context when RE2::Match() returns and before the internal SearchDFA() probe in Compiler::CompileSet(), preventing DFA hooks from dereferencing a dangling RE2*.

Root cause

In re2/re2.cc, RE2::Match() assigns hooks::context = this before invoking SearchDFA(), and never resets hooks::context when RE2::Match() returns (nor does RE2::~RE2()).

If the matched RE2 object is destroyed (heap or stack) and the same thread subsequently calls RE2::Set::Compile() with a tight max_mem budget:

  1. RE2::Set::Compile() calls Prog::CompileSet() (re2/set.cc line 128), which enters Compiler::CompileSet() (re2/compile.cc:1219).
  2. Compiler::CompileSet() runs an internal prog->SearchDFA() memory probe (re2/compile.cc:1251) without clearing hooks::context (unlike RE2::Set::Match(), which clears hooks::context = NULL at re2/set.cc line 146).
  3. When the probe exhausts max_mem, Prog::SearchDFA() invokes hooks::GetDFASearchFailureHook() (re2/dfa.cc line 1893) with hooks::context still pointing to the destroyed RE2.
  4. A hook reading hooks::context->pattern() (re2/re2.h line 312), as permitted during a hook invocation by re2/re2.h lines 1035-1039, performs a heap-use-after-free (or stack-use-after-return) read.

Even when the earlier RE2 remains alive, leaving hooks::context populated after RE2::Match() returns leaks that unrelated RE2* into subsequent non-Match() DFA work on the same thread (such as RE2::Set::Compile() or RE2::PossibleMatchRange()).

Changes

  • re2/re2.cc: Replace the bare hooks::context = this assignment (re2/re2.cc:739) in RE2::Match() with a local RAII ContextGuard (context_guard) guarded by RE2_HAVE_THREAD_LOCAL so hooks::context stays equal to this during RE2::Match() and is reset to NULL on every return path.
  • re2/compile.cc: Clear hooks::context = NULL (re2/compile.cc:1250, guarded by RE2_HAVE_THREAD_LOCAL) immediately before prog->SearchDFA() in Compiler::CompileSet(), mirroring RE2::Set::Match().
  • re2/testing/set_test.cc: Add CompileClearsDanglingHookContext and MatchHookContextPreservedAndBenignCompileSucceeds covering:
    • Destroyed heap RE2 (std::unique_ptr<RE2>) followed by failing RE2::Set::Compile() with a DFASearchFailure hook reading hooks::context->pattern().
    • Destroyed stack RE2 (MatchWithStackRE2) followed by failing RE2::Set::Compile().
    • Live unrelated RE2 isolation after matching and non-matching RE2::FullMatch() / RE2::PartialMatch() return paths.
    • Pre-populated hooks::context on entry to RE2::Set::Compile() directly exercising the Compiler::CompileSet() clear.
    • Positive control verifying that a DFASearchFailure hook fired during RE2::Match() still observes hooks::context == &re and re.pattern(), and that hooks::context is nullptr once RE2::Match() returns.
    • Benign control verifying normal RE2::Set::Compile() and RE2::Set::Match() after destroying a matched RE2.

Testing

  • Verified with Clang 21.1.8 (ASan+UBSan -O1 -g -fno-omit-frame-pointer and Release -O2 -DNDEBUG) on Linux x86_64:
    • Before the fix in re2/re2.cc and re2/compile.cc, set_test (CompileClearsDanglingHookContext) fails with AddressSanitizer heap-use-after-free READ of size 8 at re2/re2.h line 312 (re2::RE2::pattern()), and a stock Release (-O2 -DNDEBUG) reproducer reads freed memory and crashed with SIGSEGV in the recorded run while the benign control exits 0.
    • With the fix applied, all 11 tests in set_test pass with stack-use-after-return detection enabled, all 20/20 CTest targets pass unfiltered under ASan+UBSan, and the stock Release (-O2 -DNDEBUG) reproducer exits 0 on both control and crafted inputs with hooks::context == nullptr.
    • Verified with llvm-cov that ContextGuard in re2/re2.cc and the hooks::context = NULL clear in re2/compile.cc execute during set_test.

AI assistance was used in drafting and validating this change; the patch and tests were reviewed and verified by the author.

RE2::Match() assigns `hooks::context = this` before running SearchDFA()
and never clears it on return. If the RE2 object is later destroyed
(heap or stack) and the same thread subsequently calls
RE2::Set::Compile() with a tight max_mem budget, Compiler::CompileSet()
runs an internal SearchDFA() probe that can invoke the DFASearchFailure
(or DFAStateCacheReset) hook with `hooks::context` still pointing at
the destroyed RE2. A hook reading `hooks::context->pattern()` then
performs a use-after-free read (or stack-use-after-return). Even when
the earlier RE2 remains alive, `hooks::context` leaks an unrelated RE2
into subsequent non-Match DFA operations such as Set::Compile() or
PossibleMatchRange().

Scope `hooks::context = this` in RE2::Match() with an RAII guard that
resets `hooks::context = NULL` on every return path, and clear
`hooks::context = NULL` before the internal SearchDFA() probe in
Compiler::CompileSet() (mirroring RE2::Set::Match()). Add regression
tests in re2/testing/set_test.cc covering destroyed heap/stack RE2
objects, live unrelated RE2 isolation, pre-populated hooks::context on
CompileSet entry, active Match() hook context preservation, and benign
Set compilation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant