Skip to content

Commit

Permalink
Switch wasi module cache and CLI to use validated module deserialization
Browse files Browse the repository at this point in the history
Prevents undefined behaviour when loading modules.

This is a much saner/safer default option, since loading modules without
validation can cause UB and segfaults.
  • Loading branch information
theduke committed Mar 31, 2023
1 parent dd91453 commit 484c002
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ impl RunWithPathBuf {
if wasmer_compiler::Artifact::is_deserializable(&contents) {
let engine = wasmer_compiler::EngineBuilder::headless();
let store = Store::new(engine);
let module = unsafe { Module::deserialize_from_file(&store, &self.path)? };
let module = Module::deserialize_from_file_checked(&store, &self.path)?;
return Ok((store, module));
}
let (store, compiler_type) = self.store.get_store()?;
Expand Down
10 changes: 9 additions & 1 deletion lib/wasi/src/bin_factory/module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,15 @@ impl ModuleCache {
let module_bytes = bytes::Bytes::from(data);

// Load the module
let module = unsafe { Module::deserialize(engine, &module_bytes[..]).unwrap() };
let module = match Module::deserialize_checked(engine, &module_bytes[..]) {
Ok(m) => m,
Err(err) => {
tracing::error!(
"failed to deserialize module with hash '{data_hash}': {err}"
);
return None;
}
};

if let Some(cache) = &self.cached_modules {
let mut cache = cache.write().unwrap();
Expand Down

0 comments on commit 484c002

Please sign in to comment.