Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 38 ยท Patterns and Tooling

The WebAssembly Security Model

Understand WebAssembly security - sandboxed linear memory, bounds checks, validation, protected call stack, typed indirect calls, what the sandbox does not protect and host-boundary rules, with runnable demos.

WebAssembly was designed from the start to run code from strangers: your browser downloads a module from a website and executes it inside a tab. The design therefore treats the module as untrusted by default and builds the safety guarantees into the format and the execution rules, not into a layer of conventions that a program could ignore. Understanding what is guaranteed, and where the guarantees stop, helps you use Wasm confidently and avoid the false belief that "it is Wasm, so it is secure".

This lesson explains the layers of the model, demonstrates the important ones with runnable code, and finishes with an honest list of what the sandbox does not protect.

The layers of protection

Built-in guarantees

Validation Bad modules never start.
Linear memory A private, bounds-checked array.
Protected stack Return addresses are not in memory.
Typed calls Indirect calls check signatures.

Enforced by the engine, not by convention.

1 / 2

1. Validation. Before any code runs, the engine validates the entire module: every instruction must have the right operand types, every branch target must exist, every index must be in range. Validation is a decidable, single-pass check, so a malformed or malicious file is rejected up front. This is why you can safely call WebAssembly.validate(bytes) on untrusted input.

2. Isolated linear memory. A module's memory is one contiguous array of bytes that belongs to that instance alone. Every load and store is bounds-checked: an address outside the memory traps. Engines use guard pages and other tricks to make the check cheap, but the guarantee is the same. A module cannot read the host's memory, other instances' memory, or the engine's own data.

3. A protected call stack. In native code, return addresses sit on the same stack as local arrays, which is what makes stack-smashing attacks possible. In WebAssembly, the call stack is managed by the engine and is not addressable from the module. Locals live in the engine's registers or frames, and a buffer overflow in the module's linear memory cannot overwrite a return address. (Languages like C still keep their own "shadow stack" of address-taken variables inside linear memory, so those can be corrupted; see the limits below.)

4. Control-flow integrity. Branches can only target structured labels within the function, and indirect calls go through a table of function references whose signature is checked at the call. A corrupted function pointer can at worst call another function of the same type that is already in the table; it cannot jump into the middle of code or run injected bytes.

5. No ambient authority. As the plugin lesson showed, a module touches the world only through its imports. See Plugin Systems with WebAssembly.

Demonstration 1: bounds checks and isolation

An out-of-bounds access traps instead of reading whatever lies beyond. Here the module loads from address 65536, one byte past its single page. The host's own memory is never involved, and the host stays healthy:

Node.js An out-of-bounds read traps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Demonstration 2: hostile bytes never run

The validator treats a file as untrusted input. The next example fuzzes a valid module: it flips one random byte at a time (using a fixed-seed generator so the result is repeatable) and asks the engine to validate the result. Most mutations are rejected, a minority happen to remain valid, and none crash the engine or reach execution as invalid code:

Node.js Fuzzing a module: validate before you run

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

(The mutations that remain valid usually changed a byte inside a constant or a name, which is a legitimate different program.)

What the sandbox does not protect

The guarantees are strong but narrow. They protect the host from the module. They do not make the module's own logic correct.

  • Memory bugs inside the module still happen. A C program compiled to Wasm can still have buffer overflows, use-after-free and format-string bugs inside its linear memory. An attacker who controls its input can corrupt that program's data, for example an is_admin flag or a function-pointer table index. The damage is confined to the module's world, but within that world it can be serious.
  • Historically weaker mitigations. Native systems add stack canaries, ASLR and guard pages around objects. Wasm compilers have fewer of these inside linear memory, so exploitation of memory bugs can be easier than natively. Treat C and C++ input handling in Wasm with the same care as native.
  • The host glue is the real attack surface. Imports you provide are ordinary host code. A readFile import that does not validate paths, or a log import that evaluates strings as HTML, hands the module exactly the power you wrote. Validate everything that crosses the boundary in both directions.
  • Side channels. Timing, cache and speculative-execution attacks work across the sandbox. Browsers respond with site isolation and by gating high-resolution timers and SharedArrayBuffer behind cross-origin isolation (see SIMD and Threads).
  • Denial of service. A module can loop forever or allocate its maximum memory. Apply limits (memory maximum, fuel, timeouts, worker termination).
  • Supply chain. A .wasm dependency is code you did not write. Pin versions, verify checksums, and prefer modules you can build from source.
  • Cryptography. Wasm does not guarantee constant-time execution, and keys sit in ordinary linear memory. Use platform crypto (SubtleCrypto) for secrets where you can.

Web-specific rules

In a browser, a Wasm module has exactly the privileges of the JavaScript that instantiated it, no more. It is subject to the same-origin policy, CORS, CSP and permission prompts. A strict Content Security Policy may need 'wasm-unsafe-eval' in script-src to allow compilation of Wasm without allowing eval; prefer that over 'unsafe-eval'. A module cannot open a socket or touch the DOM directly; it must call imports that do.

A practical checklist

  1. Validate or compile untrusted bytes with the engine; never write your own parser for trust decisions.
  2. Give each module only the imports it needs; scope them narrowly and validate arguments.
  3. Set memory, time and recursion limits for server-side use.
  4. Compile native languages with hardening flags where available and run sanitizers in testing.
  5. Treat module output as untrusted data.
  6. Keep one instance per tenant or per request when isolation between users matters.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens when a module reads past the end of its linear memory?

    Every access is bounds-checked. An out-of-range address aborts execution with a trap.
  2. 2. Why can't a buffer overflow in linear memory overwrite a return address?

    Return addresses live in an engine-protected stack the module cannot address. Data in linear memory can still be corrupted.
  3. 3. Which statement about C code compiled to WebAssembly is true?

    The sandbox protects the host. The program's internal logic and memory safety are still your responsibility.
  4. 4. Where is the biggest attack surface in a Wasm plugin host?

    Imports are ordinary host code. Any weakness in them gives the module a way out, so validate arguments and results.

Summary

  • Wasm is validated before execution, uses bounds-checked private linear memory, an engine-protected call stack and type-checked indirect calls.
  • A module has no ambient authority; the host's imports define what it can do.
  • The sandbox protects the host, not the module's internal correctness: memory bugs, side channels, denial of service and supply-chain risks remain.
  • Validate data in both directions at the boundary and apply resource limits.
  • On the web, normal browser policies (same-origin, CSP, cross-origin isolation) still apply.

Next: Project: Image Filter in WebAssembly