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