The analysis of the security patch (commit 0e6f0d2a8325602c58d6a53ce1c0e6045eb6a490) reveals a classic use-after-free vulnerability in Wasmi's ByteBuffer implementation, which is used for WebAssembly linear memory.
The core of the vulnerability lies in the ByteBuffer::get_vec function. The original implementation reconstructed a Vec<u8> from raw parts (ptr, len, capacity) and returned it to the caller. According to Rust's ownership rules, the caller would then own this Vec. When the Vec went out of scope (for example, at the end of the function that called get_vec, likely the ByteBuffer::grow function mentioned in the commit message), it would be dropped, and its underlying memory buffer would be deallocated.
The problem is that the ByteBuffer instance itself was not consumed or invalidated and still held the now-dangling pointer (self.ptr) to the freed memory. Any subsequent use of this ByteBuffer, such as another memory access or a growth operation, would trigger a use-after-free condition. This could be exploited by a malicious WebAssembly module to corrupt memory, read sensitive data, or achieve code execution.
The patch addresses this by modifying ByteBuffer::get_vec to return a ManuallyDrop<Vec<u8>>. This wrapper type prevents the Vec from being automatically deallocated when it goes out of scope, transferring the responsibility of memory management to the caller. The ByteBuffer::grow_vec function was also updated to accept this ManuallyDrop type, ensuring the memory buffer is handled safely throughout the growth operation.