Skip to content

Fix use-after-free race between monitoring reset() and in-flight validations - #214

Open
djw8605 wants to merge 1 commit into
scitokens:masterfrom
djw8605:fix/monitoring-reset-uaf
Open

Fix use-after-free race between monitoring reset() and in-flight validations#214
djw8605 wants to merge 1 commit into
scitokens:masterfrom
djw8605:fix/monitoring-reset-uaf

Conversation

@djw8605

@djw8605 djw8605 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Problem

MonitoringStats::get_issuer_stats() documents that the returned reference "remains valid for the lifetime of the singleton", and Validator::verify() relies on that — it holds an IssuerStats* across the entire validation loop, updating duration counters after every select() wakeup.

MonitoringStats::reset() broke that contract by calling m_issuer_stats.clear(), destroying all entries. Any thread calling scitoken_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 (new IssuerStats::reset_counters()) instead of erasing entries, preserving reference stability.
  • get_json() skips entries whose counters are all zero (new IssuerStats::has_activity()), so a reset still yields an empty report and the existing monitoring tests' expectations (getIssuerCount() == 0 after reset) keep passing.
  • m_failed_issuer_lookups holds plain values only ever accessed under the mutex, so clearing it remains safe.

Testing

  • ctest unit, env_config, and monitoring suites pass (the monitoring suite includes reset + JSON assertions that exercise both changed paths).

🤖 Generated with Claude Code

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>
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.

1 participant