1bf526b62SAlex Crichton# Security 2bf526b62SAlex Crichton 3a75f383fSAlex CrichtonOne of WebAssembly (and Wasmtime's) main goals is to execute untrusted code in 4a75f383fSAlex Crichtona safe manner inside of a sandbox. WebAssembly is inherently sandboxed by design 5a75f383fSAlex Crichton(must import all functionality, etc). This document is intended to cover the 6a75f383fSAlex Crichtonvarious sandboxing implementation strategies that Wasmtime has as they are 70ee66411SAlex Crichtondeveloped. This has also been documented in a [historical blog post] too. 80ee66411SAlex Crichton 90ee66411SAlex Crichton[historical blog post]: https://bytecodealliance.org/articles/security-and-correctness-in-wasmtime 10a75f383fSAlex Crichton 11a75f383fSAlex CrichtonAt this time Wasmtime implements what's necessary for the WebAssembly 12a75f383fSAlex Crichtonspecification, for example memory isolation between instances. Additionally the 13a75f383fSAlex Crichtonsafe Rust API is intended to mitigate accidental bugs in hosts. 14a75f383fSAlex Crichton 15a75f383fSAlex CrichtonDifferent sandboxing implementation techniques will also come with different 16a75f383fSAlex Crichtontradeoffs in terms of performance and feature limitations, and Wasmtime plans to 17a75f383fSAlex Crichtonoffer users choices of which tradeoffs they want to make. 18a75f383fSAlex Crichton 19a75f383fSAlex Crichton## WebAssembly Core 20a75f383fSAlex Crichton 21a75f383fSAlex CrichtonThe core WebAssembly spec has several features which create a unique sandboxed 22a75f383fSAlex Crichtonenvironment: 23a75f383fSAlex Crichton 24a75f383fSAlex Crichton - The callstack is inaccessible. Unlike most native execution environments, 25a75f383fSAlex Crichton return addresses from calls and spilled registers are not stored in memory 26a75f383fSAlex Crichton accessible to applications. They are stored in memory that only the 27a75f383fSAlex Crichton implementation has access to, which makes traditional stack-smashing attacks 28a75f383fSAlex Crichton targeting return addresses impossible. 29a75f383fSAlex Crichton 30a75f383fSAlex Crichton - Pointers, in source languages which have them, are compiled to offsets 31a75f383fSAlex Crichton into linear memory, so implementations details such as virtual addresses 32a75f383fSAlex Crichton are hidden from applications. And all accesses within linear memory are 33a75f383fSAlex Crichton checked to ensure they stay in bounds. 34a75f383fSAlex Crichton 35a75f383fSAlex Crichton - All control transfers—direct and indirect branches, as well as direct and 36a75f383fSAlex Crichton indirect calls—are to known and type-checked destinations, so it's not 37a75f383fSAlex Crichton possible to accidentally call into the middle of a function or branch 38a75f383fSAlex Crichton outside of a function. 39a75f383fSAlex Crichton 40a75f383fSAlex Crichton - All interaction with the outside world is done through imports and exports. 41a75f383fSAlex Crichton There is no raw access to system calls or other forms of I/O; the only 42a75f383fSAlex Crichton thing a WebAssembly instance can do is what is available through interfaces 43a75f383fSAlex Crichton it has been explicitly linked with. 44a75f383fSAlex Crichton 45a75f383fSAlex Crichton - There is no undefined behavior. Even where the WebAssembly spec permits 46a75f383fSAlex Crichton multiple possible behaviors, it doesn't permit arbitrary behavior. 47a75f383fSAlex Crichton 48a75f383fSAlex Crichton## Defense-in-depth 49a75f383fSAlex Crichton 50a75f383fSAlex CrichtonWhile WebAssembly is designed to be sandboxed bugs or issues inevitably arise so 51a75f383fSAlex CrichtonWasmtime also implements a number of mitigations which are not required for 52a75f383fSAlex Crichtoncorrect execution of WebAssembly but can help mitigate issues if bugs are found: 53a75f383fSAlex Crichton 54a75f383fSAlex Crichton* Linear memories by default are preceded with a 2GB guard region. WebAssembly 55a75f383fSAlex Crichton has no means of ever accessing this memory but this can protect against 56a75f383fSAlex Crichton accidental sign-extension bugs in Cranelift where if an offset is accidentally 57a75f383fSAlex Crichton interpreted as a signed 32-bit offset instead of an unsigned offset it could 58a75f383fSAlex Crichton access memory before the addressable memory for WebAssembly. 59a75f383fSAlex Crichton 60a75f383fSAlex Crichton* Wasmtime uses explicit checks to determine if a WebAssembly function should be 61a75f383fSAlex Crichton considered to stack overflow, but it still uses guard pages on all native 62a75f383fSAlex Crichton thread stacks. These guard pages are never intended to be hit and will abort 63a75f383fSAlex Crichton the program if they're hit. Hitting a guard page within WebAssembly indicates 64a75f383fSAlex Crichton a bug in host configuration or a bug in Cranelift itself. 65a75f383fSAlex Crichton 66a75f383fSAlex Crichton* Where it can Wasmtime will zero memory used by a WebAssembly instance after 67a75f383fSAlex Crichton it's finished. This is not necessary unless the memory is actually reused for 68a75f383fSAlex Crichton instantiation elsewhere but this is done to prevent accidental leakage of 69a75f383fSAlex Crichton information between instances in the face of other bugs. This applies to 70a75f383fSAlex Crichton linear memories, tables, and the memory used to store instance information 71a75f383fSAlex Crichton itself. 72a75f383fSAlex Crichton 73a75f383fSAlex Crichton* The choice of implementation language, Rust, for Wasmtime is also a 74a75f383fSAlex Crichton defense in protecting the authors for Wasmtime from themselves in addition to 75a75f383fSAlex Crichton protecting embedders from themselves. Rust helps catch mistakes when writing 76a75f383fSAlex Crichton Wasmtime itself at compile time. Rust additionally enables Wasmtime developers 77a75f383fSAlex Crichton to create an API that means that embedders can't get it wrong. For example 78a75f383fSAlex Crichton it's guaranteed that Wasmtime won't segfault when using its public API, 79a75f383fSAlex Crichton empowering embedders with confidence that even if the embedding has bugs all 80a75f383fSAlex Crichton of the security guarantees of WebAssembly are still upheld. 81a75f383fSAlex Crichton 82a75f383fSAlex Crichton* Wasmtime is in the [process of implementing control-flow-integrity 830e9121daSFrankReh mechanisms][cfi-rfc] to leverage hardware state for further guaranteeing that 84a75f383fSAlex Crichton WebAssembly stays within its sandbox. In the event of a bug in Cranelift this 85a75f383fSAlex Crichton can help mitigate the impact of where control flow can go to. 86a75f383fSAlex Crichton 87a75f383fSAlex Crichton[cfi-rfc]: https://github.com/bytecodealliance/rfcs/blob/main/accepted/cfi-improvements-with-pauth-and-bti.md 88a75f383fSAlex Crichton 89a75f383fSAlex Crichton## Filesystem Access 90a75f383fSAlex Crichton 91a75f383fSAlex CrichtonWasmtime implements the WASI APIs for filesystem access, which follow a 92a75f383fSAlex Crichtoncapability-based security model, which ensures that applications can only 93a75f383fSAlex Crichtonaccess files and directories they've been given access to. WASI's security 94a75f383fSAlex Crichtonmodel keeps users safe today, and also helps us prepare for shared-nothing 95a75f383fSAlex Crichtonlinking and nanoprocesses in the future. 96a75f383fSAlex Crichton 97a75f383fSAlex CrichtonWasmtime developers are intimately engaged with the WASI standards process, 98a75f383fSAlex Crichtonlibraries, and tooling development, all along the way too. 99a75f383fSAlex Crichton 100a75f383fSAlex Crichton## Terminal Output 101a75f383fSAlex Crichton 102a75f383fSAlex CrichtonIf untrusted code is allowed to print text which is displayed to a terminal, it may 103a75f383fSAlex Crichtonemit ANSI-style escape sequences and other control sequences which, depending on 104a75f383fSAlex Crichtonthe terminal the user is using and how it is configured, can have side effects 105a75f383fSAlex Crichtonincluding writing to files, executing commands, injecting text into the stream 106a75f383fSAlex Crichtonas if the user had typed it, or reading the output of previous commands. ANSI-style 107a75f383fSAlex Crichtonescape sequences can also confuse or mislead users, making other vulnerabilities 108a75f383fSAlex Crichtoneasier to exploit. 109a75f383fSAlex Crichton 110a75f383fSAlex CrichtonOur first priority is to protect users, so Wasmtime now filters writes to output 111a75f383fSAlex Crichtonstreams when they are connected to a terminal to translate escape sequences into 112a75f383fSAlex Crichtoninert replacement sequences. 113a75f383fSAlex Crichton 114a75f383fSAlex CrichtonSome applications need ANSI-style escape sequences, such as terminal-based 115a75f383fSAlex Crichtoneditors and programs that use colors, so we are also developing a proposal for 116a75f383fSAlex Crichtonthe WASI Subgroup for safe and portable ANSI-style escape sequence support, which 117a75f383fSAlex Crichtonwe hope to post more about soon. 118a75f383fSAlex Crichton 119a75f383fSAlex Crichton## Spectre 120a75f383fSAlex Crichton 121a75f383fSAlex CrichtonWasmtime implements a few forms of basic spectre mitigations at this time: 122a75f383fSAlex Crichton 123a75f383fSAlex Crichton* Bounds checks when accessing entries in a function table (e.g. the 124a75f383fSAlex Crichton `call_indirect` instruction) are mitigated. 125a75f383fSAlex Crichton 126a75f383fSAlex Crichton* The `br_table` instruction is mitigated to ensure that speculation goes to a 127a75f383fSAlex Crichton deterministic location. 128a75f383fSAlex Crichton 129a75f383fSAlex Crichton* Wasmtime's default configuration for linear memory means that bounds checks 130a75f383fSAlex Crichton will not be present for memory accesses due to the reliance on page faults to 131a75f383fSAlex Crichton instead detect out-of-bounds accesses. When Wasmtime is configured with 132a75f383fSAlex Crichton "dynamic" memories, however, Cranelift will insert spectre mitigation for the 133a75f383fSAlex Crichton bounds checks performed for all memory accesses. 134a75f383fSAlex Crichton 135a75f383fSAlex CrichtonMitigating Spectre continues to be a subject of ongoing research, and Wasmtime 136a75f383fSAlex Crichtonwill likely grow more mitigations in the future as well. 137*bac0e78fSAlex Crichton 138*bac0e78fSAlex CrichtonNote that on aarch64 the `csdb` instruction is disabled by default due to its 139*bac0e78fSAlex Crichtonsignificant performance penalty, but this can be additionally enabled through 140*bac0e78fSAlex Crichtonthe `use_csdb` Cranelift setting. 141