CVE-2021-39216: Use After Free (CWE-416)
value: Wasmtime is an open source runtime for WebAssembly & WASI. In Wasmtime from version 0.19.0 and before version 0.30.0 there was a use-after-free bug when passing `externref`s from the host to guest Wasm content. To trigger the bug, you have to explicitly pass multiple `externref`s from the host to a Wasm instance at the same time, either by passing multiple `externref`s as arguments from host code to a Wasm function, or returning multiple `externref`s to Wasm from a multi-value return function defined in the host. If you do not have host code that matches one of these shapes, then you are not impacted. If Wasmtime's `VMExternRefActivationsTable` became filled to capacity after passing the first `externref` in, then passing in the second `externref` could trigger a garbage collection. However the first `externref` is not rooted until we pass control to Wasm, and therefore could be reclaimed by the collector if nothing else was holding a reference to it or otherwise keeping it alive. Then, when control was passed to Wasm after the garbage collection, Wasm could use the first `externref`, which at this point has already been freed. We have reason to believe that the effective impact of this bug is relatively small because usage of `externref` is currently quite rare. The bug has been fixed, and users should upgrade to Wasmtime 0.30.0. If you cannot upgrade Wasmtime yet, you can avoid the bug by disabling reference types support in Wasmtime by passing `false` to `wasmtime::Config::wasm_reference_types`.
CWE description: 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.
CVSS v3 base score: 6.3
Published: 2021-09-17T20:15Z
Repository: https://github.com/bytecodealliance/wasmtime
Language: Unknown
Commit date: 2021-09-17 10:27:29 -0700
Commit message: Merge pull request from GHSA-v4cp-h94r-m7xf

Fix a use-after-free bug when passing `ExternRef`s to Wasm
Diff: diff --cc crates/types/src/lib.rs
index 14518496b,33f1cef8c..573c699f4
--- a/crates/types/src/lib.rs
+++ b/crates/types/src/lib.rs
@@@ -67,32 -66,55 +67,70 @@@ impl From<WasmType> for wasmparser::Typ
              WasmType::ExnRef => wasmparser::Type::ExnRef,
          }
      }
  }
  
 +impl fmt::Display for WasmType {
 +    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 +        match self {
 +            WasmType::I32 => write!(f, "i32"),
 +            WasmType::I64 => write!(f, "i64"),
 +            WasmType::F32 => write!(f, "f32"),
 +            WasmType::F64 => write!(f, "f64"),
 +            WasmType::V128 => write!(f, "v128"),
 +            WasmType::ExternRef => write!(f, "externref"),
 +            WasmType::FuncRef => write!(f, "funcref"),
 +            WasmType::ExnRef => write!(f, "exnref"),
 +        }
 +    }
 +}
 +
  /// WebAssembly function type -- equivalent of `wasmparser`'s FuncType.
  #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
  pub struct WasmFuncType {
+     params: Box<[WasmType]>,
+     externref_params_count: usize,
+     returns: Box<[WasmType]>,
+     externref_returns_count: usize,
+ }
+ 
+ impl WasmFuncType {
+     #[inline]
+     pub fn new(params: Box<[WasmType]>, returns: Box<[WasmType]>) -> Self {
+         let externref_params_count = params.iter().filter(|p| **p == WasmType::ExternRef).count();
+         let externref_returns_count = params.iter().filter(|r| **r == WasmType::ExternRef).count();
+         WasmFuncType {
+             params,
+             externref_params_count,
+             returns,
+             externref_returns_count,
+         }
+     }
+ 
      /// Function params types.
-     pub params: Box<[WasmType]>,
+     #[inline]
+     pub fn params(&self) -> &[WasmType] {
+         &self.params
+     }
+ 
+     /// How many `externref`s are in this function's params?
+     #[inline]
+     pub fn externref_params_count(&self) -> usize {
+         self.externref_params_count
+     }
+ 
      /// Returns params types.
-     pub returns: Box<[WasmType]>,
+     #[inline]
+     pub fn returns(&self) -> &[WasmType] {
+         &self.returns
+     }
+ 
+     /// How many `externref`s are in this function's returns?
+     #[inline]
+     pub fn externref_returns_count(&self) -> usize {
+         self.externref_returns_count
+     }
  }
  
  impl TryFrom<wasmparser::FuncType> for WasmFuncType {
      type Error = WasmError;
      fn try_from(ty: wasmparser::FuncType) -> Result<Self, Self::Error> {