Repository navigation
wasm2c: (Security) fix runtime overflows when running Wasm modules with memory64 - #2783
Conversation
sbc100
left a comment
There was a problem hiding this comment.
Does it make sense to write a test for this? Maybe tricky?
|
|
||
| #endif /* WASM_RT_USE_MMAP */ | ||
|
|
||
| static bool u64_mult_overflow(uint64_t a, uint64_t b, uint64_t* result) { |
There was a problem hiding this comment.
Is there nothing in libc or compiler-rt for this? I guess gcc and clang have __builtin_mul_overflow, but maybe nothing more portable than that?
Maybe mention in a comment why can't use __builtin_mul_overflow?
There was a problem hiding this comment.
For maximum portability, I was generally trying to avoid any compiler specific builtins where (1) they are not strictly needed (2) and are not in the performance-critical path. We could add a path to use __builtin_mul_overflow if available, but this feels like overkill here given that memory grow should be relatively rare. I'll plan to leave it as, but let me know if you have a difference preference
It's a bit tricky to write a generic test that tests functionality for such scenarios because a wasm engine will likely fail to allocate enough memory to cause these sort of overflows anwyay. I could write a very-specific test for this failure, but I don't think that that would achieve anything after this patch lands. So I think its best to leave this one as is. |
As part of routine audit we found a bugs in the wasm2c runtime for modules supporting memory64. Memory32 is not affected.
Security bug When a memory64 module calls
memory.growwith a delta value that causesnew_pages * page_sizeto overflowuint64_tto zero,realloc(ptr, 0)is called, which frees the linear memory buffer on glibc.expand_data_allocationthen returns an error code asreallocreturns NULL. The callergrow_memory_implreturns-1(indicating failure) without updatingmemory->data, leaving a dangling pointer. Any subsequent memory access from the Wasm module reads or writes freed heap memory.Non security bug the delta check for a
0delta has a typo. So there is a code path wherenew_pageswraps to 0 and returns 0 indicating successful memory grow even though memory wasn't updated. Since this doesn't update the memory field, this is just a functional bug.