Fix use-after-free race between monitoring reset() and in-flight validations - #214
Open
djw8605 wants to merge 1 commit into
Open
Fix use-after-free race between monitoring reset() and in-flight validations#214djw8605 wants to merge 1 commit into
djw8605 wants to merge 1 commit into
Conversation
MonitoringStats::get_issuer_stats() documents that returned references remain valid for the lifetime of the singleton, and Validator::verify() holds an IssuerStats pointer across the entire validation loop. But MonitoringStats::reset() called m_issuer_stats.clear(), destroying the entries. Any thread calling scitoken_reset_monitoring_stats() while another thread had a verification in flight left that thread updating freed memory. Zero the counters in place instead of erasing the entries, and skip all-zero entries when serializing to JSON so reset still produces an empty report. The failed-issuer map holds plain values only accessed under the mutex, so clearing it remains safe. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Problem
MonitoringStats::get_issuer_stats()documents that the returned reference "remains valid for the lifetime of the singleton", andValidator::verify()relies on that — it holds anIssuerStats*across the entire validation loop, updating duration counters after everyselect()wakeup.MonitoringStats::reset()broke that contract by callingm_issuer_stats.clear(), destroying all entries. Any thread callingscitoken_reset_monitoring_stats()while another thread has a verification in flight leaves the verifying thread incrementing freed atomics — a use-after-free data race.Fix
reset()zeroes every entry's counters in place (newIssuerStats::reset_counters()) instead of erasing entries, preserving reference stability.get_json()skips entries whose counters are all zero (newIssuerStats::has_activity()), so a reset still yields an empty report and the existing monitoring tests' expectations (getIssuerCount() == 0after reset) keep passing.m_failed_issuer_lookupsholds plain values only ever accessed under the mutex, so clearing it remains safe.Testing
ctestunit, env_config, and monitoring suites pass (the monitoring suite includes reset + JSON assertions that exercise both changed paths).🤖 Generated with Claude Code