MSWasm: Soundly Enforcing Memory-Safe Execution of Unsafe Code

Alexandra E. Michael, Anitha Gollamudi, Jay Bosamiya, Craig Disselkoen, Aidan Denlinger, Conrad Watt, Bryan Parno, Marco Patrignani, Marco Vassena, Deian Stefan · DOI

Abstract

WebAssembly (Wasm) provides a robust sandbox for isolating applications from the host system, yet it offers minimal protection against vulnerabilities within the application itself. Because most Wasm modules are compiled from memory-unsafe languages like C and C++, internal errors such as buffer overflows and use-after-free (UAF) vulnerabilities can lead to total compromise of the module's linear memory. This paper introduces MSWasm, an extension to WebAssembly that incorporates low-level memory-safety abstractions—specifically segments and handles—to provide robust, fine-grained protection. By decoupling safety policy from enforcement, MSWasm offers a formally verified path to memory-safe execution with performance overheads that are poised to decrease significantly with emerging hardware support.

Introduction

WebAssembly's security model is built around a "linear memory" abstraction—a single, contiguous array of bytes accessible by the module. While this design effectively sandboxes the module from the host, it creates a monolithic heap where a single out-of-bounds write can corrupt any data within the application, including function pointers and sensitive variables. Furthermore, the absence of traditional native defenses like Address Space Layout Randomization (ASLR) or read-only pages makes Wasm binaries potentially easier to exploit than their native counterparts.

MSWasm addresses this "internal" memory unsafety by replacing the monolithic linear memory with structured abstractions that enforce spatial and temporal safety at the bytecode level.

The MSWasm Architecture

MSWasm introduces two primary primitives to the WebAssembly instruction set:

  1. Segments: Instead of one giant memory block, MSWasm utilizes multiple segments. Each memory allocation (e.g., a malloc call) is represented as an individual segment with its own boundaries.
  2. Handles: Handles are unforgeable, well-typed pointers that act as capabilities. A handle encapsulates a segment's base address, an offset, and the bounds of the allocation.

Handle Integrity and the isCorrupted Flag

A core innovation in MSWasm is its approach to pointer integrity. To maintain performance, MSWasm allows for pointer arithmetic but tracks the validity of the resulting handle. If a program attempts to "forge" a pointer—such as by casting an arbitrary integer to a pointer or performing bitwise operations on a handle—the handle is marked with an isCorrupted flag. The system only triggers a trap when a corrupted handle is actually used for a memory access, rather than during the arithmetic itself. This optimization significantly reduces the frequency of runtime checks.

Intra-Object Safety

To prevent attackers from overflowing a buffer to reach adjacent fields within the same struct, MSWasm introduces a slice instruction. This allows the compiler to narrow the bounds of a handle to a specific sub-region, ensuring that an access intended for one field cannot spill into another.

Formal Verification and Implementation

The authors developed a color-based memory-safety monitor to formally prove MSWasm's security properties. In this model, every allocation is assigned a unique "color," and memory access is permitted only if the pointer's color matches the memory's color. This framework allowed the team to prove that MSWasm is "robustly memory safe," ensuring that a safe module remains protected even when linked against potentially malicious or compromised modules.

For practical evaluation, the authors developed:

  • A C-to-MSWasm Compiler: Built on the CHERI fork of Clang/LLVM, this tool automatically translates standard C pointers into MSWasm handles.
  • Runtimes: Two distinct implementations were created: an ahead-of-time (AOT) compiler based on rWasm and a just-in-time (JIT) compiler based on GraalWasm.

Performance Evaluation

The system was evaluated using PolyBenchC, the standard benchmark for WebAssembly performance. The overhead of MSWasm varies depending on the level of safety enforced:

  • Spatial Safety: Preventing buffer overflows resulted in a 21.4% overhead in the AOT runtime.
  • Spatial + Temporal Safety: Adding protection against use-after-free errors increased the overhead to 52.2%.
  • Full Safety: Enforcing full handle integrity in software (the most rigorous mode) led to a 197.5% overhead.

Notably, the JIT implementation (GraalWasm) performed better under pressure, showing only a 42.3% overhead for combined spatial and temporal safety. This suggests that modern JIT optimization techniques can effectively mitigate the costs of these safety checks.

Discussion and Future Work

While a ~200% overhead for full software-enforced safety is substantial, MSWasm is designed with future hardware in mind. Capability-based hardware extensions, such as Arm’s Memory Tagging Extension (MTE) or CHERI, are expected to reduce these overheads to below 20%, making full memory safety viable for production environments.

Current limitations include the need for broader C++ support (specifically regarding type confusion) and more granular stack protection. At present, MSWasm uses a single large segment for the C stack to preserve performance, which protects the heap from stack-based attacks but does not yet prevent overflows between individual stack frames. Future iterations will also look toward better interoperability with legacy Wasm modules that still rely on standard linear memory.

Summary updated

BibTeX
@article{michael2023mswasm,
  author = {Alexandra E. Michael and Anitha Gollamudi and Jay Bosamiya and Craig Disselkoen and Aidan Denlinger and Conrad Watt and Bryan Parno and Marco Patrignani and Marco Vassena and Deian Stefan},
  title = {{MSWasm: Soundly Enforcing Memory-Safe Execution of Unsafe Code}},
  journal = {Proc. ACM Program. Lang.},
  year = {2023},
  doi = {10.1145/3571208},
}
Figure