Fix panic-safety in LinkedHashMap::clear (double-free on panicking value Drop) - #9
Open
tooson9010-spec wants to merge 1 commit into
Open
tooson9010-spec wants to merge 1 commit into
tooson9010-spec wants to merge 1 commit into
Conversation
…lue Drop) clear frees the value nodes (running each value's user-controlled Drop) and resets the sentinel's guard links afterwards. A value Drop that panics partway leaves the reset unrun, so the guard still points into freed memory; the map's own destructor then walks that chain again and frees it a second time -- a double-free reachable from safe Rust. Reset the guard links before freeing so unwinding leaves an empty, self-consistent list. LinkedHashSet::clear delegates to LinkedHashMap::clear and is fixed by the same change. Adds a regression test.
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.
Found while auditing this crate's unsafe teardown paths for panic-safety.
Summary
clear frees the value nodes, running each value's Drop, then resets the guard
links. If a value's Drop panics after some nodes are freed, the reset is skipped
and the sentinel still points at freed memory. The map's destructor later walks
that chain and frees it again, a double-free (CWE-415) reachable from safe Rust.
Fix
Reset the guard links before freeing instead of after. The sentinel then points
at an empty list, so a panic during freeing can't reach the second traversal. On
panic the unreached nodes leak, which is safe. LinkedHashSet::clear delegates to
LinkedHashMap::clear and is fixed by the same change.
Verification
Added clear_panicking_value_drop_is_sound: values that panic on Drop are cleared
under catch_unwind, then the map is dropped. Without the fix the second traversal
double-frees the chain (glibc "double free detected", SIGABRT); with the fix it
unwinds cleanly. Existing tests pass. Confirmed on 0.3.2.