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:
Alex Crichton
2023-03-17 09:52:54 -05:00
committed by GitHub
parent a66b3e1ab6
commit 28371bfd40
15 changed files with 325 additions and 24 deletions

View File

@@ -5,6 +5,7 @@ use crate::{AsContext, AsContextMut, Engine, MemoryType, StoreContext, StoreCont
use anyhow::{bail, Result};
use std::cell::UnsafeCell;
use std::convert::TryFrom;
use std::ops::Range;
use std::slice;
use std::time::Instant;
use wasmtime_environ::MemoryPlan;
@@ -611,6 +612,10 @@ pub unsafe trait LinearMemory: Send + Sync + 'static {
/// Return the allocated memory as a mutable pointer to u8.
fn as_ptr(&self) -> *mut u8;
/// Returns the range of native addresses that WebAssembly can natively
/// access from this linear memory, including guard pages.
fn wasm_accessible(&self) -> Range<usize>;
}
/// A memory creator. Can be used to provide a memory creator

View File

@@ -97,7 +97,7 @@ use wasmtime_runtime::{
InstanceAllocationRequest, InstanceAllocator, InstanceHandle, ModuleInfo,
OnDemandInstanceAllocator, SignalHandler, StorePtr, VMCallerCheckedFuncRef, VMContext,
VMExternRef, VMExternRefActivationsTable, VMRuntimeLimits, VMSharedSignatureIndex,
VMTrampoline,
VMTrampoline, WasmFault,
};
mod context;
@@ -1501,6 +1501,68 @@ impl StoreOpaque {
pub(crate) fn push_rooted_funcs(&mut self, funcs: Arc<[Definition]>) {
self.rooted_host_funcs.push(funcs);
}
/// Translates a WebAssembly fault at the native `pc` and native `addr` to a
/// WebAssembly-relative fault.
///
/// This function may abort the process if `addr` is not found to actually
/// reside in any linear memory. In such a situation it means that the
/// segfault was erroneously caught by Wasmtime and is possibly indicative
/// of a code generator bug.
///
/// This function returns `None` for dynamically-bounds-checked-memories
/// with spectre mitigations enabled since the hardware fault address is
/// always zero in these situations which means that the trapping context
/// doesn't have enough information to report the fault address.
pub(crate) fn wasm_fault(&self, pc: usize, addr: usize) -> Option<WasmFault> {
// Explicitly bounds-checked memories with spectre-guards enabled will
// cause out-of-bounds accesses to get routed to address 0, so allow
// wasm instructions to fault on the null address.
if addr == 0 {
return None;
}
// Search all known instances in this store for this address. Note that
// this is probably not the speediest way to do this. Traps, however,
// are generally not expected to be super fast and additionally stores
// probably don't have all that many instances or memories.
//
// If this loop becomes hot in the future, however, it should be
// possible to precompute maps about linear memories in a store and have
// a quicker lookup.
let mut fault = None;
for instance in self.instances.iter() {
if let Some(f) = instance.handle.wasm_fault(addr) {
assert!(fault.is_none());
fault = Some(f);
}
}
if fault.is_some() {
return fault;
}
eprintln!(
"\
Wasmtime caught a segfault for a wasm program because the faulting instruction
is allowed to segfault due to how linear memories are implemented. The address
that was accessed, however, is not known to any linear memory in use within this
Store. This may be indicative of a critical bug in Wasmtime's code generation
because all addresses which are known to be reachable from wasm won't reach this
message.
pc: 0x{pc:x}
address: 0x{addr:x}
This is a possible security issue because WebAssembly has accessed something it
shouldn't have been able to. Other accesses may have succeeded and this one just
happened to be caught. The process will now be aborted to prevent this damage
from going any further and to alert what's going on. If this is a security
issue please reach out to the Wasmtime team via its security policy
at https://bytecodealliance.org/security.
"
);
std::process::abort();
}
}
impl<T> StoreContextMut<'_, T> {

View File

@@ -4,6 +4,7 @@ use crate::store::{InstanceId, StoreOpaque};
use crate::MemoryType;
use anyhow::{anyhow, Result};
use std::convert::TryFrom;
use std::ops::Range;
use std::sync::Arc;
use wasmtime_environ::{
DefinedMemoryIndex, DefinedTableIndex, EntityIndex, MemoryPlan, MemoryStyle, Module,
@@ -99,6 +100,10 @@ impl RuntimeLinearMemory for LinearMemoryProxy {
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn wasm_accessible(&self) -> Range<usize> {
self.mem.wasm_accessible()
}
}
#[derive(Clone)]

View File

@@ -103,12 +103,21 @@ pub(crate) fn from_runtime_box(
);
(error, None)
}
wasmtime_runtime::TrapReason::Jit(pc) => {
wasmtime_runtime::TrapReason::Jit { pc, faulting_addr } => {
let code = store
.modules()
.lookup_trap_code(pc)
.unwrap_or(Trap::StackOverflow);
(code.into(), Some(pc))
let mut err: Error = code.into();
// If a fault address was present, for example with segfaults,
// then simultaneously assert that it's within a known linear memory
// and additionally translate it to a wasm-local address to be added
// as context to the error.
if let Some(fault) = faulting_addr.and_then(|addr| store.wasm_fault(pc, addr)) {
err = err.context(fault);
}
(err, Some(pc))
}
wasmtime_runtime::TrapReason::Wasm(trap_code) => (trap_code.into(), None),
};