unkernel-rs 0.8.1

Native Rust + Wasmtime host library and CLI for unkernel (Linux 7.x on WebAssembly) — zero Node, zero JSPI, zero KVM
Documentation
# Future Ideas & Design Notes (`v0.3.0+`)

Captured proposals and inspiration for evolving `unkernel-rs` as a fast, rootless, observable sandbox for AI coding agents (`nono-agent`, MCP/NDJSON controllers) and supply-chain malware analysis (e.g. Shai-Hulud style credential harvesters).

> **Note:** Items in this file are proposals/ideas for future milestones, not committed scope or shipped features.

---

## External Inspiration & Tradeoff Notes

### `tabc` / `tocker` — Browser Tabs as an OCI Container Runtime
- Reference: https://fzakaria.com/2026/11/25/browser-tabs-as-a-container-runtime
- Uses the same underlying `unkernel` Linux-in-Wasm kernel, hosted inside headless or visible Chromium tabs (`docker run --runtime tabc alpine`, `tocker compose up`).
- **Comparison vs. `unkernel-rs`:** Chromium adds ~540 MiB RAM and ~870 ms cold start per container vs. `unkernel-rs`'s ~73 MiB RAM and ~260 ms cold boot on `wasmtime` + Linux Landlock.
- **High-value ideas to borrow:** Arbitrary OCI image pulling (`--image`), host-to-guest port forwarding (`-p`), and potentially an OCI runtime shim.

### Drums — Shared-State Computer Gyms & Checkpointing
- Reference: https://drums.run/
- Runs parallel Ubuntu desktops for RL / computer-use agents with patched CRIU checkpointing (including nested namespaces), fast episode restore/forking, and per-episode integrity gate reports.
- **Comparison vs. `unkernel-rs`:** Drums targets full GUI desktops at native CPU speed; `unkernel-rs` targets headless, ultra-lightweight (~73 MiB), zero-KVM, untamperable sandboxes.
- **Honest architectural boundary:** Because `unkernel-rs` avoids JSPI by running each guest `kthread` on a native Rust OS thread (`src/host.rs`), mid-syscall RAM + thread-stack serialization/forking (CRIU-style) is **not** trivial. By contrast, **disk and 9P filesystem state** are 100% host-owned in pure Rust and can be snapshotted, diffed, rolled back, and forked cheaply and deterministically.
- **High-value ideas to borrow:** Copy-on-Write workspace staging (`--mount-cow`), forkable COW disk branches for parallel agent rollouts, and structured post-run integrity/verdict reports.

---

## Prioritized Candidate Features

### 1. Copy-on-Write 9P Workspace Staging (`-m HOST:GUEST:cow`) — *Implemented*
- Shipped in [`src/fs9p.rs`]src/fs9p.rs and [`src/host.rs`]src/host.rs: pure-Rust in-memory `:cow` overlay with `WorkspaceDiff`, `WorkspaceSnapshot`, `read_workspace_file`, `discard_workspace`, `commit_workspace`, CLI `--show-diff` / `--commit-cow`, and `serve` NDJSON `diff` / `discard` / `commit` commands.
- Remaining optional follow-up: unified text diff formatting and disk-backed spill for huge (>100 MiB) staged artifacts.

### 2. Structured Post-Run Security Verdict / Gate Report (`--report`)
- **Problem:** Raw JSONL streams (`--audit`) are great for live streaming and machine pipelines, but humans and agent orchestrators also want a concise end-of-run verdict (inspired by Drums' episode gate reports).
- **Idea:** Aggregate `src/monitor.rs` telemetry into an optional end-of-run summary (JSON + compact CLI table):
  - Isolation posture (`sandbox_enabled`, `landlock_abi`, `net_mode`, `disk_mode`).
  - Unique contacted domains, DNS queries (allowed vs. refused), TLS SNIs, plaintext HTTP hosts/paths, and total TX/RX bytes per target.
  - Sensitive credential paths touched or blocked (`.env`, `.npmrc`, `.ssh/*`, `.aws/*`, `.kube/*`, etc.).
  - Summary of 9P filesystem mutations (and COW diff summary when paired with `--mount-cow`).
  - Count of interactive approvals requested / allowed / denied and total frozen time.
- **Value:** One-command supply-chain triage ("what did `npm install <pkg>` try to read, modify, and exfiltrate?") and auditable per-turn receipts for `nono-agent`.

### 3. Direct OCI Image Support (`--image <ref>`)
- **Problem:** Currently `unkernel-rs` boots pre-built upstream `unkernel` `.erofs` distros (`alpine`, `debian`). Testing arbitrary language ecosystems (`node:22-alpine`, `python:3.12-slim`, custom Harbor images) requires manual image building or `apk`/`apt-get` into a `--disk`.
- **Idea:** Pull `linux/arm64` OCI images via Registry v2 HTTP API (reusing `ureq` + `sha2` + `flate2`), unpack layers with whiteout handling, and pack/cache an immutable `.erofs` (or ext4) rootfs under `~/.cache/unkernel-rs/images/<digest>/`.
- **Tradeoffs:** Needs careful handling of OCI `ENTRYPOINT`/`CMD`/`ENV`/`WORKDIR` defaults, whiteout files (`.wh.*`), and cross-platform image packing without requiring root or host `mkfs.erofs` binaries.

### 4. Host-to-Guest Port Forwarding (`-p [HOST_IP:]HOST_PORT:GUEST_PORT`)
- **Problem:** `src/net.rs` currently handles outbound guest → host TCP NAT (plus `10.0.2.2 -> 127.0.0.1` when `--allow-lan` is set), but does not expose guest servers to the host.
- **Idea:** Bind a host `TcpListener` (defaulting to `127.0.0.1`) per `-p` rule and inject inbound TCP `SYN` frames into guest `10.0.2.15:<guest_port>` through `NetStack`.
- **Value:** Lets an agent run a local dev server, test harness, or HTTP API inside the sandbox while the user or outer tool queries it on `localhost`.

### 5. Forkable In-Memory Disk Snapshots & Warm VM Pool — *Tier-A Branching Implemented*
- Tier-A forkable snapshots shipped in [`src/disk.rs`]src/disk.rs (`DiskSnapshot`, `BlockDevice::snapshot` / `from_snapshot`) and [`src/host.rs`]src/host.rs (`VmSnapshot`, `Vm::snapshot` / `branch_from_snapshot`, `serve` `snapshot` / `restore`) — enabling parallel counterfactual agent branches from an exact ext4 + `:cow` workspace state.
- Remaining optional follow-up:
  - **Git-style persistent delta overlays (`.ovl`):** Serialize `VmSnapshot` (dirty 4 KiB disk blocks + `:cow` workspace entries/whiteouts + parent/base SHA-256) to compact parent-chained overlay files on disk so branches survive restarts without duplicating base images, plus export `:cow` changes directly as `git diff` / patches.
  - Warm standby VM pool.

### 6. Optional OCI Runtime Shim (`unkernel-rs oci ...`)
- **Idea:** Implement the minimal OCI runtime spec (`create`, `start`, `state`, `kill`, `delete`) if integrating directly with `docker --runtime`, `podman`, or `containerd` becomes useful.
- **Status:** Lower priority than native CLI / Rust library / `serve` NDJSON ergonomics.

### 7. Native MCP Server (`unkernel-rs mcp`)
- **Idea:** Expose `exec`, VMI `procs`, `:cow` `diff`/`commit`/`discard`, `snapshot`/`restore`, and `pause`/`resume` as first-class MCP tools (inspired by `agent-desktop-fleet`) so coding agents can plug in directly without custom NDJSON glue.

---

## Known Gaps & Follow-Ups for VMI & TLS Forensic Inspection (`v0.4.0+`)

- **`--vmi-tls` HTTP/2 & `gzip` decoding:** Passive decryption sees raw HTTP/2 frames + HPACK headers and compressed response bodies when a client (e.g. Go `net/http`) negotiates `h2` (`--tls-keylog` lets Wireshark decode HTTP/2 + gzip offline, and `--mitm-tls` forces readable `HTTP/1.1` via ALPN). Adding a lightweight host-side HTTP/2 frame + HPACK + `gzip` decoder would make passive `net.tls_decrypted` summaries human-readable for `h2` clients too.
- **`--vmi-tls` TLS 1.3 `KeyUpdate`:** Long-lived connections that rotate traffic secrets mid-stream will fail AEAD tag verification after the `KeyUpdate` record; trigger an automatic Pass-1/Pass-3 re-scan on decryption failure (or derive `HKDF-Expand-Label(secret, "traffic upd", "", hash_len)` automatically when inner `ContentType == 0x16` `KeyUpdate` is observed!).
- **`--vmi-tls` Pass-3 scan latency:** Pass 1 (`client_random` anchor for OpenSSL/BoringSSL) takes ~10–25 ms; Pass 3 (parallel full-RAM high-entropy `traffic_secret` scan for Go `crypto/tls` / `rustls`) adds ~100–300 ms once per new TLS connection on the first Application Data record. Narrowing scans to recently dirty guest pages or caching the Go `mspan` region could cut Pass-3 latency by 5–10×.
- **`--vmi-tls` TLS 1.2 without `client_random` + ChaCha20:** Pass 3 targets TLS 1.3 `traffic_secret`s; TLS 1.2 currently relies on Pass 1 (`client_random` adjacency) or Pass 2 (*FindAES* for AES-GCM).
- **Level-3 VMI full `argv` / command line:** Currently reads 15-byte Linux `task_struct.comm` (`pid`, `tgid`, `ppid`, `comm`), not full process `argv`/`cmdline` (which lives in guest user virtual memory via `mm->arg_start..arg_end`).

---

## Explicit Non-Goals (For Now)

- **GUI / Wayland / X11 / Chrome desktops:** Out of scope for headless agent sandboxes; `kwemu` software CPU emulation is a poor fit for desktop browsers (prefer KVM microVMs or Drums-style containers there).
- **Live mid-syscall RAM/kthread checkpointing (CRIU-style):** Not cleanly supported with native OS-thread `kthread`s; prefer COW disk/workspace branching + fast ~260 ms clean boots unless Wasmtime stack-switching matures enough to make single-threaded snapshot/restore simple.
- **Default-on MITM TLS interception:** Kept strictly opt-in (`--mitm-tls`) for forensic labs; default mode stays zero-touch, and `--vmi-tls` extracts TLS 1.3/1.2 session keys directly from `SharedMemory` without touching guest root CAs.