Validate faulting addresses are valid to fault on (#6028)
* Validate faulting addresses are valid to fault on This commit adds a defense-in-depth measure to Wasmtime which is intended to mitigate the impact of CVEs such as GHSA-ff4p-7xrq-q5r8. Currently Wasmtime will catch `SIGSEGV` signals for WebAssembly code so long as the instruction which faulted is an allow-listed instruction (aka has a trap code listed for it). With the recent security issue, however, the problem was that a wasm guest could exploit a compiler bug to access memory outside of its sandbox. If the access was successful there's no real way to detect that, but if the access was unsuccessful then Wasmtime would happily swallow the `SIGSEGV` and report a nominal trap. To embedders, this might look like nothing is going awry. The new strategy implemented here in this commit is to attempt to be more robust towards these sorts of failures. When a `SIGSEGV` is raised the faulting pc is recorded but additionally the address of the inaccessible location is also record. After the WebAssembly stack is unwound and control returns to Wasmtime which has access to a `Store` Wasmtime will now use this inaccessible faulting address to translate it to a wasm address. This process should be guaranteed to succeed as WebAssembly should only be able to access a well-defined region of memory for all linear memories in a `Store`. If no linear memory in a `Store` could contain the faulting address, then Wasmtime now prints a scary message and aborts the process. The purpose of this is to catch these sorts of bugs, make them very loud errors, and hopefully mitigate impact. This would continue to not mitigate the impact of a guest successfully loading data outside of its sandbox, but if a guest was doing a sort of probing strategy trying to find valid addresses then any invalid access would turn into a process crash which would immediately be noticed by embedders. While I was here I went ahead and additionally took a stab at #3120. Traps due to `SIGSEGV` will now report the size of linear memory and the address that was being accessed in addition to the bland "access out of bounds" error. While this is still somewhat bland in the context of a high level source language it's hopefully at least a little bit more actionable for some. I'll note though that this isn't a guaranteed contextual message since only the default configuration for Wasmtime generates `SIGSEGV` on out-of-bounds memory accesses. Dynamically bounds-checked configurations, for example, don't do this. Testing-wise I unfortunately am not aware of a great way to test this. The closet equivalent would be something like an `unsafe` method `Config::allow_wasm_sandbox_escape`. In lieu of adding tests, though, I can confirm that during development the crashing messages works just fine as it took awhile on macOS to figure out where the faulting address was recorded in the exception information which meant I had lots of instances of recording an address of a trap not accessible from wasm. * Fix tests * Review comments * Fix compile after refactor * Fix compile on macOS * Fix trap test for s390x s390x rounds faulting addresses to 4k boundaries.
This commit is contained in:
@@ -6,6 +6,7 @@ mod not_for_windows {
|
||||
use rustix::mm::{mmap_anonymous, mprotect, munmap, MapFlags, MprotectFlags, ProtFlags};
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::ops::Range;
|
||||
use std::ptr::null_mut;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
@@ -74,6 +75,12 @@ mod not_for_windows {
|
||||
fn as_ptr(&self) -> *mut u8 {
|
||||
self.mem as *mut u8
|
||||
}
|
||||
|
||||
fn wasm_accessible(&self) -> Range<usize> {
|
||||
let base = self.mem as usize;
|
||||
let end = base + self.size;
|
||||
base..end
|
||||
}
|
||||
}
|
||||
|
||||
struct CustomMemoryCreator {
|
||||
|
||||
@@ -1279,3 +1279,44 @@ fn div_plus_load_reported_right() -> Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wasm_fault_address_reported_by_default() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let mut store = Store::new(&engine, ());
|
||||
let module = Module::new(
|
||||
&engine,
|
||||
r#"
|
||||
(module
|
||||
(memory 1)
|
||||
(func $start
|
||||
i32.const 0xdeadbeef
|
||||
i32.load
|
||||
drop)
|
||||
(start $start)
|
||||
)
|
||||
"#,
|
||||
)?;
|
||||
let err = Instance::new(&mut store, &module, &[]).unwrap_err();
|
||||
|
||||
// On s390x faulting addressess are rounded to the nearest page boundary
|
||||
// instead of having the precise address reported.
|
||||
let mut expected_addr = 0xdeadbeef_u32;
|
||||
if cfg!(target_arch = "s390x") {
|
||||
expected_addr &= 0xfffff000;
|
||||
}
|
||||
|
||||
// NB: at this time there's no programmatic access to the fault address
|
||||
// because it's not always available for load/store traps. Only static
|
||||
// memories on 32-bit have this information, but bounds-checked memories
|
||||
// use manual trapping instructions and otherwise don't have a means of
|
||||
// communicating the faulting address at this time.
|
||||
let err = format!("{err:?}");
|
||||
assert!(
|
||||
err.contains(&format!(
|
||||
"memory fault at wasm address 0x{expected_addr:x} in linear memory of size 0x10000"
|
||||
)),
|
||||
"bad error: {err}"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user