Repository navigation
Fix pointer overflows when splitting holes near the end of the address space - #93
Open
AlixANNERAUD wants to merge 3 commits into
Open
AlixANNERAUD wants to merge 3 commits into
AlixANNERAUD wants to merge 3 commits into
Conversation
Pure refactoring, no functional change. Computing the split without access to the cursor makes it testable for holes at arbitrary addresses.
The address calculations for splitting a hole used wrapping pointer arithmetic and compared the results against the end of the hole. For holes close to the end of the address space, the calculated addresses could wrap around, so that unsuitable holes were considered a fit: - the aligned start address wrapped around to zero for large alignments - the end address of the allocation wrapped around - the end address of the back padding hole node wrapped around As a result, hole nodes were written to addresses outside of the heap and the hole list was corrupted. Use checked arithmetic instead and treat such holes as unsuitable.
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.
Problem
Cursor::split_currentcomputes the placement of an allocation inside a hole with wrapping pointer arithmetic and then compares the results against the end of the hole. If the hole is close to the end of the address space, these calculations can wrap around, and a hole that cannot serve the request is accepted. There are three places where this happens:align_up(new_start, required_align)wraps around to0when the next aligned address is behind the end of the address space.aligned_addr.wrapping_add(required_size)wraps around when the aligned address is valid, but the allocation does not fit below the end of the address space.back_padding_start.wrapping_add(hole_layout.size())wraps around when the hole ends less than one hole node below the end of the address space.In all three cases the wrapped address compares as
<= hole_end, so the split is carried out. Hole nodes are then written to addresses outside of the heap and the hole list is corrupted. This is reachable through the safeHeap::allocate_first_fit(andGlobalAlloc::alloc) with a validLayout.It requires a heap that is located near the end of the address space (closer than the requested alignment or size), so most users are not affected. Heaps in the higher half of the address space combined with large alignments are the realistic case.
Reproduction
I reproduced it on
wasm32-unknown-unknown(linear memory grown to 4 GiB, run with Node.js), because that allows placing a heap at the end of the address space:0xF000_0000, allocateLayout(16, align 1 GiB)(case 1)Err, but writes a hole node to address0x10. A following 8192 byte allocation returns0x10. Debug: panics with a subtraction overflow.Err, no write, following allocation fails0xFFFF_FFF8, allocate 16 bytes, thenLayout(600 KiB, align 512 KiB)(case 2)0xFFF8_0000(the allocation wraps around the address space) and writes a hole node to address0x16000, in debug and releaseErr, no writeFix
split_currentinto a function that only does address calculations (split_hole). It is a pure move without functional changes. This makes it possible to test the calculation for holes at arbitrary addresses, without needing memory there.Testing
cargo teston nightly with--all-features, on stable with and withoutuse_spin, andcargo miri test --all-featureswith-Zmiri-strict-provenanceall pass.