
# Security

**The security boundary is the VM, not the container.** A container is a set of Linux namespaces
and a cgroup inside one guest kernel; every container of a runtime shares that kernel, its memory
and its network. What keeps the guest away from your machine is that the whole guest, kernel
included, is a WebAssembly program: it runs in the WebAssembly sandbox of the JavaScript engine
(V8 in Node and Chrome, SpiderMonkey in Firefox, JavaScriptCore in Safari), and it touches the
outside world only through what untainer and unkernel hand it.

So think of a runtime as one small virtual machine with a few processes in it:

- containers of the same runtime can see each other on the network (they share `localhost`) and
  compete for the same memory; one that is root in its container is not root of the guest, but a
  kernel bug in the guest is shared by all of them;
- what one runtime does cannot reach another runtime, except through what you connect yourself
  (a shared volume, a port you publish and connect to).

## What the guest gets

Nothing by default. Each of these is something you give it:

| Given with | What the guest then reaches |
| --- | --- |
| `net`, `publishPort()` (Node) | The network, in or out: see [Network](/concepts/network). Without them the guest has no interface but `lo`, and nothing listens on the host. |
| `hostVolume(dir)`, `untainer run -v` (Node) | That directory, read and write unless read-only, with the rights of the host process. Symlinks in it resolve inside the guest, so neither a symlink nor `..` leads out of it. |
| `memoryVolume()`, `tmpfsVolume()` | Memory of this process, or of the guest. |
| `disk`, `diskVolume()` | Its own ext4 disk: a file under `~/.cache/untainer/disks/` (Node), or IndexedDB of the page's origin (browser). |
| `rt.vm` | The unkernel instance: whatever unkernel can do. Not for untrusted code. |

Tested by [`test/security.vm.test.ts`](https://github.com/OskarLebuda/untainer/blob/main/packages/untainer/test/security.vm.test.ts) and
[`test/volumes.vm.test.ts`](https://github.com/OskarLebuda/untainer/blob/main/packages/untainer/test/volumes.vm.test.ts) ("a host directory
volume cannot be left through symlinks or ..").

## In the browser

The guest runs in the tab, in the page's origin, with the tab's limits. It can reach nothing the
page does not give it: no network without `net`, no files but the images and disks the page
mounts, no access to the page's DOM, cookies or other scripts. The images it reads are fetched by
the page (and checked against their hashes); the chunks it read are kept in the origin's storage.

## In Node

The guest runs inside your Node process, by default in a worker thread. The process's user,
files, environment and network are out of the guest's reach except through the table above, but
nothing more separates them: the WebAssembly sandbox and the code that serves the guest's
requests (unkernel's 9p server for host directories, its network stack, untainer's own host code)
are the whole boundary. A bug there is a way out.

## Resources

A runtime has a fixed memory (`memoryMiB`), allocated up front; the guest cannot take more of the
host's. CPU is not limited: a busy guest takes a core.

In a worker, the default, a guest kernel that loops holds the worker only, and the runtime ends
with `ERR_VM` once the guest's heartbeat stops. On the calling thread (`worker: false`, or with a
network policy) it can hold the host's event loop for minutes: seen 7 times in 660 runs close to
out of memory ([`docs/findings.md`](https://github.com/OskarLebuda/untainer/blob/main/docs/findings.md)).

## When not to run an untrusted image

- **With a host directory, a disk or a published port you care about**: the guest can write to
  the first two and anyone on the bound address can reach the third.
- **With `net` on a machine that has internal services**: with `directBackend()` alone the guest
  makes connections as the host process, to the LAN and to services bound to the host's address.
  Wrap it in `policyBackend()`, as the CLI's `--net` does; see [Network](/concepts/network).
- **In a Node process that holds secrets or serves others**, where a sandbox escape (a bug in the
  WebAssembly engine or in the code that serves the guest) would matter: prefer a browser tab,
  another process, or a VM with hardware isolation.
- **Where availability matters**: an untrusted guest can use all of its memory and a whole core;
  give it its own runtime, in a worker (the default), and a `memoryMiB` you can afford.
