Repository navigation
re2: clear thread_local hooks::context on Match return and CompileSet - #706
Open
professor-moody wants to merge 1 commit into
Open
professor-moody wants to merge 1 commit into
professor-moody wants to merge 1 commit into
Conversation
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
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.
Summary
Clear the
thread_localpointerhooks::contextwhenRE2::Match()returns and before the internalSearchDFA()probe inCompiler::CompileSet(), preventing DFA hooks from dereferencing a danglingRE2*.Root cause
In
re2/re2.cc,RE2::Match()assignshooks::context = thisbefore invokingSearchDFA(), and never resetshooks::contextwhenRE2::Match()returns (nor doesRE2::~RE2()).If the matched
RE2object is destroyed (heap or stack) and the same thread subsequently callsRE2::Set::Compile()with a tightmax_membudget:RE2::Set::Compile()callsProg::CompileSet()(re2/set.cc line 128), which entersCompiler::CompileSet()(re2/compile.cc:1219).Compiler::CompileSet()runs an internalprog->SearchDFA()memory probe (re2/compile.cc:1251) without clearinghooks::context(unlikeRE2::Set::Match(), which clearshooks::context = NULLat re2/set.cc line 146).max_mem,Prog::SearchDFA()invokeshooks::GetDFASearchFailureHook()(re2/dfa.cc line 1893) withhooks::contextstill pointing to the destroyedRE2.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
RE2remains alive, leavinghooks::contextpopulated afterRE2::Match()returns leaks that unrelatedRE2*into subsequent non-Match()DFA work on the same thread (such asRE2::Set::Compile()orRE2::PossibleMatchRange()).Changes
re2/re2.cc: Replace the barehooks::context = thisassignment (re2/re2.cc:739) inRE2::Match()with a local RAIIContextGuard(context_guard) guarded byRE2_HAVE_THREAD_LOCALsohooks::contextstays equal tothisduringRE2::Match()and is reset toNULLon every return path.re2/compile.cc: Clearhooks::context = NULL(re2/compile.cc:1250, guarded byRE2_HAVE_THREAD_LOCAL) immediately beforeprog->SearchDFA()inCompiler::CompileSet(), mirroringRE2::Set::Match().re2/testing/set_test.cc: AddCompileClearsDanglingHookContextandMatchHookContextPreservedAndBenignCompileSucceedscovering:RE2(std::unique_ptr<RE2>) followed by failingRE2::Set::Compile()with aDFASearchFailurehook readinghooks::context->pattern().RE2(MatchWithStackRE2) followed by failingRE2::Set::Compile().RE2isolation after matching and non-matchingRE2::FullMatch()/RE2::PartialMatch()return paths.hooks::contexton entry toRE2::Set::Compile()directly exercising theCompiler::CompileSet()clear.DFASearchFailurehook fired duringRE2::Match()still observeshooks::context == &reandre.pattern(), and thathooks::contextisnullptronceRE2::Match()returns.RE2::Set::Compile()andRE2::Set::Match()after destroying a matchedRE2.Testing
re2/re2.ccandre2/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.set_testpass 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 withhooks::context == nullptr.ContextGuardinre2/re2.ccand thehooks::context = NULLclear inre2/compile.ccexecute duringset_test.AI assistance was used in drafting and validating this change; the patch and tests were reviewed and verified by the author.