Skip to content

fix: use-after-free in cabi_realloc free_list on repeated export calls#319

Open
chaynabors wants to merge 1 commit intobytecodealliance:mainfrom
chaynabors:fix/free-list-use-after-free
Open

fix: use-after-free in cabi_realloc free_list on repeated export calls#319
chaynabors wants to merge 1 commit intobytecodealliance:mainfrom
chaynabors:fix/free-list-use-after-free

Conversation

@chaynabors
Copy link

Fixes #224

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.

This PR removes indiscriminate tracking from cabi_realloc. Only the retptr allocated explicitly in call() is tracked and freed by post_call. retptr is a temporary buffer that JS never references directly, which is why it needs to be managed explicitly.

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
@chaynabors
Copy link
Author

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.

@chaynabors
Copy link
Author

chaynabors commented Feb 25, 2026

Accidentally closed #318 while doing some spring cleaning. Sorry for duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Out of bounds memory access when using fetch

1 participant