CVE-2021-43790 (Rust, published 2021-11-30T00:15Z)
Description: value: Lucet is a native WebAssembly compiler and runtime. There is a bug in the main branch of `lucet-runtime` affecting all versions published to crates.io that allows a use-after-free in an Instance object that could result in memory corruption, data race, or other related issues. This bug was introduced early in the development of Lucet and is present in all releases. As a result of this bug, and dependent on the memory backing for the Instance objects, it is possible to trigger a use-after-free when the Instance is dropped. Users should upgrade to the main branch of the Lucet repository. Lucet no longer provides versioned releases on crates.io. There is no way to remediate this vulnerability without upgrading.
Severity: MEDIUM (CVSS v3 base score: 8.1)
Weakness: CWE-416 \u2014 Use After Free
The product reuses or references memory after it has been freed. At some point afterward, the memory may be allocated again and saved in another pointer, while the original pointer references a location somewhere within the new allocation. Any operations using the original pointer are no longer valid because the memory "belongs" to the code that operates on the new pointer.
Repository: https://github.com/bytecodealliance/lucet
Files changed:
lucet-runtime/lucet-runtime-internals/src/instance.rs
Commit message: Merge pull request from GHSA-hf79-8hjp-rrvq

* Use manual drop

* Add some comments to `ManuallyDrop` usage

* rustfmt

Co-authored-by: Aaron Turon <aturon@fastly.com>
Co-authored-by: Alex Crichton <alex@alexcrichton.com>

Vulnerable code:
use crate::region::RegionInternal;
                // Grab a handle to the region to ensure it outlives `inst`.
                // This ensures that the region won't be dropped by `inst` being
                // dropped, which could result in `inst` being unmapped by the
                // Region *during* drop of the Instance's fields.
                let region: Arc<dyn RegionInternal> = inst.alloc().region.clone();
                // and now we can drop what may be the last Arc<Region>. If it is
                // it can safely do what it needs with memory; we're not running
                // destructors on it anymore.
                mem::drop(region);
    /// The memory allocated for this instance
    alloc: Alloc,
            alloc,

Fixed code:
use std::mem::ManuallyDrop;
                // The `inst.alloc` field manages the memory of the instance
                // itself. Note, though, that this field is in a `ManuallyDrop`
                // so it won't get dropped automatically in `drop_in_place`.
                // This is the point where we take over that precise drop.
                // By using `take` here we're basically calling `ptr::read`
                // which "duplicates" the `alloc` since the `alloc` local
                // variable here is the exact same as `inst.alloc`. All we do
                // with `inst`, though, is call `drop_in_place`, which
                // invalidates every other field in `inst`.
                let alloc: Alloc = ManuallyDrop::take(&mut inst.alloc);
                // Now that we're 100% done with the instance, destructors and
                // all, we can release the memory of the instance back to the
                // original allocator from whence it came (be it mmap or uffd
                // based). This will run the "official" destructor for `Alloc`
                // which internally does the release. Note that after this
                // operation the `inst` pointer is invalid and can no longer be
                // used.
                drop(alloc);
    /// The memory allocated for this instance.
    ///
    /// Note that this is in a `ManuallyDrop` because this manages the memory of
    /// this `Instance` itself. To have precise control over this memory we
    /// handle this in `Drop for InstanceHandle`.
    alloc: ManuallyDrop<Alloc>,
            alloc: ManuallyDrop::new(alloc),