fix: use-after-free in cabi_realloc free_list on repeated export calls#319
fix: use-after-free in cabi_realloc free_list on repeated export calls#319chaynabors wants to merge 2 commits intobytecodealliance:mainfrom
Conversation
cabi_realloc tracked all allocations in Runtime.free_list, which post_call freed after each export invocation. When the host calls cabi_realloc during an import to write a return value into guest memory, those allocations may still be referenced by live JS objects across repeated export calls. post_call would free them, causing use-after-free on the next invocation. Fix: remove indiscriminate tracking from cabi_realloc. Only the retptr allocated explicitly in call() is tracked and freed by post_call. Fixes bytecodealliance#224
|
This is a step closer to the ideal solution, but may be a partial regression in that some buffers may no longer be freed if they were copied during lifting. The lifetimes aren't clear enough to me at this point to validate this holistically. I'm trying to convince my team to adopt WASM. We can stomach a small bounded memory leak, but the current use-after-free makes Componentize unusable for us. A more comprehensive solution might involve some sort of deallocator in the splicer, but this is far more involved and risky for me to implement than the proposed solution. I'm happy to take a stab at it if I can get WASM adopted, but won't have time otherwise. |
|
Accidentally closed #318 while doing some spring cleaning. Sorry for duplicate. |
|
Hey @chaynabors , sorry for the delay. I think this is pragmatic approach to fix double-free issue. I wonder how difficult would be to add the test that replicates #224 as part of the fix? wdyt? The next step should be to eliminate leaks by implementing proper deallocation in the splicer but that we can track in a separate issue. |
Fixes #224
cabi_realloctracked all allocations inRuntime.free_list, whichpost_callfreed after each export invocation. When the host callscabi_reallocduring an import to write a return value into guest memory, those allocations may still be referenced by live JS objects across repeated export calls.post_callwould free them, causing use-after-free on the next invocation.This PR removes indiscriminate tracking from
cabi_realloc. Only theretptrallocated explicitly incall()is tracked and freed bypost_call.retptris a temporary buffer that JS never references directly, which is why it needs to be managed explicitly.