fix: avoid use-after-free in rate_limiter periodic cleanup - #70
Draft
cursor[bot] wants to merge 1 commit into
Draft
fix: avoid use-after-free in rate_limiter periodic cleanup#70cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
check_limit held a reference into std::flat_map requests[key], then erase_if'd empty entries on every 1000th call. flat_map erase invalidates all references, so the following size()/push_back was UAF whenever cleanup removed anything. Record the allow/deny decision before cleanup; add a regression covering the orphan-key path. Co-authored-by: Kaius Ruokonen <ruoka@users.noreply.github.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.
Bug and impact
http::middleware::rate_limiter::check_limitcould use-after-free on every 1000th call that removed an empty key from itsstd::flat_map. That is undefined behavior in production rate limiting (crash / memory corruption under sustained traffic).Root cause
check_limitheldauto& key_requests = requests[key], then ranstd::erase_ifto drop empty entries.flat_maperase invalidates all references, so the followingsize()/push_backoperated on a dangling reference whenever cleanup removed anything (e.g. an orphan key left bymax_requests == 0).Fix
Record the allow/deny decision and
push_backbeforeerase_if, then return the saved result.Validation
rate_limiter check_limit survives periodic empty-key cleanuptools/CB.sh debug test rate_limiter→ 4/4 passed (1002 assertions)