fix(wasm): prevent u32 arithmetic overflow in memory region checks#1305
Open
AftAb-25 wants to merge 1 commit intomofa-org:mainfrom
Open
fix(wasm): prevent u32 arithmetic overflow in memory region checks#1305AftAb-25 wants to merge 1 commit intomofa-org:mainfrom
AftAb-25 wants to merge 1 commit intomofa-org:mainfrom
Conversation
Fixes mofa-org#1303 Three sites in wasm_runtime/memory.rs used unchecked u32 arithmetic that overflowed when memory regions were positioned near the end of the 32-bit address space: - MemoryRegion::contains: rewrote start + size comparison using subtraction (�ddr - start < size) to avoid the addition that wraps. - WasmMemory::size: replaced pages * PAGE_SIZE with pages.saturating_mul(PAGE_SIZE) to prevent wrapping when the simulated page count is extreme. - MemoryAllocator::return_block: replaced + with saturating_add in both coalescing branches to prevent overflow when merging large adjacent free blocks. Three unit tests are added to confirm each site no longer panics or produces incorrect results at the u32 boundary.
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.
Fixes #1303
Three sites in wasm_runtime/memory.rs used unchecked u32 arithmetic that overflowed when memory regions were positioned near the end of the 32-bit address space:
MemoryRegion::contains: rewrotestart + sizecomparison using subtraction (addr - start < size) to avoid the addition that wraps.WasmMemory::size: replacedpages * PAGE_SIZEwithpages.saturating_mul(PAGE_SIZE)to prevent wrapping when the simulated page count is extreme.MemoryAllocator::return_block: replaced+withsaturating_addin both coalescing branches to prevent overflow when merging large adjacent free blocks.Three unit tests are added to confirm each site no longer panics or produces incorrect results at the u32 boundary.
📋 Summary
This PR prevents arithmetic overflow bugs in the WASM memory management module. When memory regions hit the bounds of a 32-bit address space, unchecked addition/multiplication wrapped around to incorrect smaller numbers. This fixes the bounds-checking math to ensure memory safety isn't compromised at the edges.
🔗 Related Issues
Closes #1303
🧠 Context
Because Rust defaults to wrapping arithmetic in release mode and panicking in debug mode, operations near
u32::MAXwould result in either runtime crashes or incorrect security checks (e.g. low addresses matching incorrectly inside a wrapped high-address bounding box).🛠️ Changes
MemoryRegion::containsinequality boundary check without addition.saturating_multoWasmMemory::sizeandsaturating_addtoMemoryAllocator::return_block.u32::MAX.🧪 How you Tested
cargo test -p mofa-plugins)cargo clippyrun locally to ensure adherence to syntax constraints.📸 Screenshots / Logs (if applicable)
If breaking:
🧹 Checklist
Code Quality
cargo fmtruncargo clippypasses without warningsTesting
cargo testpasses locally without any errorDocumentation
PR Hygiene
main