17 Commits
Author SHA1 Message Date
niklas 8ab77ec9dc fix(#8): ps skips corrupt meta, gc tolerates vanished refs, reject -p 0
One corrupt meta.json aborted `wsbx ps`; gc aborted on ENOENT when a concurrent
pull replaced a ref file; -p 0 could never work on the host side.
2026-10-22 19:55:48 +02:00
niklas b50e846b11 fix(#7): pin busybox by digest, kernel downloads use the filtered agent
busybox:musl was a mutable tag re-resolved daily for the binary that runs as
PID 1 in every VM. Kernel/GitHub downloads now go through the same
public-only resolver as registries.
2026-10-22 19:55:23 +02:00
niklas 56762648e3 fix(#6): connect/read timeouts on every HTTP agent
No ureq call had a timeout, so a silent registry hung pull forever. One
oci::agent() builder (30 s connect + per-read) now backs registry agents,
local ones included, and the kernel downloads.
2026-10-22 19:54:09 +02:00
niklas 2231e4efe1 fix(#5): write sandbox meta.json before creating the disk
Until meta existed the sandbox's image looked unreferenced, so a concurrent
`wsbx gc` deleted it mid-create; a crashed create was also invisible to ps and
refused by rm.
2026-10-22 19:53:03 +02:00
niklas 37c58cb5b2 fix(#4): decide 'local registry' by name, not by its DNS answer
A registry name resolving to loopback (e.g. *.127.0.0.1.nip.io) got the
unfiltered agent for the whole pull. Only localhost or a non-public IP literal
is local now; DNS-named registries always use the filtered resolver.
2026-10-22 19:52:32 +02:00
niklas 2f053b8a5b fix(#3): is_public() rejects IPv6 forms of private IPv4 targets
NAT64 (64:ff9b::/96) and 6to4 (2002::/16) now decode to the embedded IPv4 and
go through the V4 rules; documentation, Teredo and everything outside global
unicast 2000::/3 (incl. IPv4-compatible ::/96) is refused.
2026-10-22 19:52:32 +02:00
niklas fb6efa1558 fix(#2): make image ref-file names injective
ref_path() flattened / : @ to _, so evil_bar:v1 and evil/bar:v1 shared a ref
file and a poisoned one booted the wrong image with no network contact. Append
a hash of the exact reference; old-style ref files are ignored (re-resolved).
2026-10-22 19:52:32 +02:00
niklas 3879b4cc4d fix(#1): skip env entries whose key is not a shell identifier
Image Env (and -e) keys were interpolated unquoted into run.sh, which runs as
root: a hostile image could execute commands as guest root via the key.
2026-10-22 19:51:01 +02:00
niklas b816e435df update dependencies 2026-10-22 19:07:38 +02:00
niklas ae91de745e Merge pull request 'Implement VSCode Support and a better PS command' (#1) from feat/vscode into main
Reviewed-on: #1
2026-10-22 17:04:39 +00:00
niklas 687cafb392 bump to 0.2.0 2026-10-22 19:04:11 +02:00
niklas 1ac5193b0e feat: functionality to open a sandbox workspace in vs code 2026-10-22 18:59:15 +02:00
niklas 230927d8db feat: support a better ps command 2026-10-21 18:10:56 +02:00
niklas bc7512a373 feat: implement exec and forward commands for running commands in sandboxes
release / release (push) Successful in 2s
release / build (macos-arm64) (push) Successful in 21s
release / build (ubuntu-latest) (push) Successful in 1m5s
- Added `exec` command to run a shell or command in a running sandbox, utilizing a new exec server.
- Introduced `forward` command to expose ports of a running sandbox on the host's localhost.
- Updated Cargo.toml and Cargo.lock to reflect version bump and new dependencies.
- Enhanced README.md with usage instructions for new commands.
- Refactored build.rs to include new guest helper files for exec functionality.
- Created new source files for exec and forward logic, including protocol handling and connection management.
2026-10-03 21:48:44 +02:00
niklas 4ef8dd62e9 update Cargo.lock for 0.1.1
release / release (push) Successful in 2s
release / build (macos-arm64) (push) Successful in 19s
release / build (ubuntu-latest) (push) Successful in 1m1s
2026-10-02 17:43:00 +02:00
niklas b758ce3dfe bump version
release / release (push) Successful in 2s
release / build (macos-arm64) (push) Failing after 3s
release / build (ubuntu-latest) (push) Failing after 17s
2026-10-02 17:41:04 +02:00
niklas a1bca2e188 feat: split up code at logical boundaries 2026-10-02 17:40:28 +02:00
23 changed files with 2174 additions and 801 deletions
+4
View File
@@ -1,2 +1,6 @@
/target
/.llm-wiki/
#Ignore vscode AI rules
.github/instructions/codacy.instructions.md
Generated
+79 -79
View File
@@ -78,18 +78,18 @@ checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
[[package]]
name = "block-buffer"
version = "0.10.4"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa"
dependencies = [
"generic-array",
"hybrid-array",
]
[[package]]
name = "cc"
version = "1.5.1"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f360145194ee8e21db5ee7f3fcd4fe52210864c75c985dae33218202c8bbe040"
checksum = "877bf67ac8e52449eb2f9e98a344ea163beb70dd1f9eebe3cc5357dcfeaee4df"
dependencies = [
"find-msvc-tools",
"shlex",
@@ -148,40 +148,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "cpufeatures"
version = "0.2.17"
name = "const-oid"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c"
[[package]]
name = "cpufeatures"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.5.2"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78"
checksum = "0ba106e844045ddc02c20768ac4b0b7cb45f2cc2ecbd833ac9f5dacb183c2055"
dependencies = [
"cfg-if",
]
[[package]]
name = "crypto-common"
version = "0.1.7"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453"
dependencies = [
"generic-array",
"typenum",
"hybrid-array",
]
[[package]]
name = "digest"
version = "0.10.7"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2"
dependencies = [
"block-buffer",
"const-oid",
"crypto-common",
]
@@ -218,9 +224,9 @@ dependencies = [
[[package]]
name = "find-msvc-tools"
version = "0.1.14"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484"
checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48"
[[package]]
name = "flate2"
@@ -242,16 +248,6 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.17"
@@ -276,10 +272,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "icu_collections"
version = "2.3.0"
name = "hybrid-array"
version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa68d21081c4a05d5a901a1c62add574c77048b6a1c67be3b50ce0b60d4ca513"
checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17"
dependencies = [
"typenum",
]
[[package]]
name = "icu_collections"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe090a42a0d3140fed8c63263ed1fecc02422cb2f4bfe55cd8de2de1b8e105dd"
dependencies = [
"displaydoc",
"potential_utf",
@@ -291,9 +296,9 @@ dependencies = [
[[package]]
name = "icu_locale_core"
version = "2.3.0"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb"
checksum = "25d29b57c17c6528d0dc4c2141cfa2d08200465acb537a3909c8b9b8a4a984c0"
dependencies = [
"displaydoc",
"litemap",
@@ -304,9 +309,9 @@ dependencies = [
[[package]]
name = "icu_normalizer"
version = "2.3.0"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12f9cf5f235641ed274641dd81c3f28d870e276763d0797aeeab72317b1c646f"
checksum = "404a3b8c486f205c87fe7f23ab6894d84a4ad76d6e4671ae1981ba7def87c599"
dependencies = [
"icu_collections",
"icu_normalizer_data",
@@ -318,15 +323,15 @@ dependencies = [
[[package]]
name = "icu_normalizer_data"
version = "2.3.0"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1563da1ed3e0b3bf3d74c9b85917ac9c56464d2f57242270c09c9e752f8021a0"
checksum = "20f0088bfb7441f7a37fa0c9e20cb1e9cba1cac6b9ce8cd2a6dc44294f8e8cbc"
[[package]]
name = "icu_properties"
version = "2.3.0"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e7ca276ad3145661a65914e6daf131ca5120cd3dcee8f8f3214b8875184a148"
checksum = "2ea83d991c56158f7cf898bf86436c1dfe7c07ee82fe88951f2695c9f37a379f"
dependencies = [
"displaydoc",
"icu_collections",
@@ -339,15 +344,15 @@ dependencies = [
[[package]]
name = "icu_properties_data"
version = "2.3.0"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e590f038c1464a96894fd6d10127e90a8be4509f56ff7ecef851b15cee0b7caa"
checksum = "b98f29af82d02b163fe50161388350e743bb3107cc8e8c2f7314cb505faddfcf"
[[package]]
name = "icu_provider"
version = "2.3.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73"
checksum = "413099315636c588ee8f011190b329b971a09a8fae9d157e01dcae8379468514"
dependencies = [
"displaydoc",
"icu_locale_core",
@@ -393,9 +398,9 @@ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
[[package]]
name = "libc"
version = "0.2.189"
version = "0.2.191"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
[[package]]
name = "linux-raw-sys"
@@ -505,9 +510,9 @@ dependencies = [
[[package]]
name = "rustls"
version = "0.23.45"
version = "0.23.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634"
checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf"
dependencies = [
"log",
"once_cell",
@@ -529,9 +534,9 @@ dependencies = [
[[package]]
name = "rustls-webpki"
version = "0.103.15"
version = "0.103.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2"
checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281"
dependencies = [
"ring",
"rustls-pki-types",
@@ -569,9 +574,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.151"
version = "1.0.154"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
dependencies = [
"itoa",
"memchr",
@@ -582,9 +587,9 @@ dependencies = [
[[package]]
name = "sha2"
version = "0.10.9"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19"
dependencies = [
"cfg-if",
"cpufeatures",
@@ -605,9 +610,9 @@ checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
[[package]]
name = "smallvec"
version = "1.16.2"
version = "1.16.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9395f0f0eee849a9b707b2f06bb92a6a422090e2123bb2ef8e87a0e61892a8e"
checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b"
[[package]]
name = "stable_deref_trait"
@@ -629,9 +634,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "syn"
version = "3.0.6"
version = "3.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8593e8e72159ed2257d083c7a454a85cbf854f37a0966d8d483aff8c8a3ebcee"
checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
dependencies = [
"proc-macro2",
"quote",
@@ -662,9 +667,9 @@ dependencies = [
[[package]]
name = "tinystr"
version = "0.8.4"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643"
checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a"
dependencies = [
"displaydoc",
"zerovec",
@@ -678,9 +683,9 @@ checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
[[package]]
name = "unicode-ident"
version = "1.0.26"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954"
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
[[package]]
name = "untrusted"
@@ -728,12 +733,6 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
@@ -746,14 +745,14 @@ version = "0.26.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9"
dependencies = [
"webpki-roots 1.0.9",
"webpki-roots 1.0.10",
]
[[package]]
name = "webpki-roots"
version = "1.0.9"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a"
checksum = "1c1c8e20c8a38fc7d65abfe74eb7280e4aa667e8eec8bc84996d2874810748d9"
dependencies = [
"rustls-pki-types",
]
@@ -854,12 +853,13 @@ checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
[[package]]
name = "wsbx"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"anyhow",
"clap",
"flate2",
"hex",
"libc",
"serde_json",
"sha2",
"tar",
@@ -878,9 +878,9 @@ dependencies = [
[[package]]
name = "yoke"
version = "0.8.3"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5"
checksum = "417ef3fcf6c300b7dbd25c85531a746931c02ad536f3cf4786ed1e88223340b3"
dependencies = [
"stable_deref_trait",
"yoke-derive",
@@ -922,9 +922,9 @@ dependencies = [
[[package]]
name = "zeroize"
version = "1.9.0"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e"
checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879"
[[package]]
name = "zerotrie"
@@ -939,9 +939,9 @@ dependencies = [
[[package]]
name = "zerovec"
version = "0.11.8"
version = "0.11.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8"
checksum = "f58a2799cfe9a1670c7ea8edbd52ef8edb93cb37a7f83bf31ba9e35d9dae6a40"
dependencies = [
"yoke",
"zerofrom",
@@ -950,9 +950,9 @@ dependencies = [
[[package]]
name = "zerovec-derive"
version = "0.11.6"
version = "0.11.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34df6fc39dbd26ddc9c10e6a2984476e13acce22e64e4487636ef494369225da"
checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a"
dependencies = [
"proc-macro2",
"quote",
+3 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "wsbx"
version = "0.1.0"
version = "0.2.0"
edition = "2024"
[dependencies]
@@ -8,7 +8,8 @@ anyhow = "1"
clap = { version = "4", features = ["derive"] }
flate2 = "1"
hex = "0.4"
libc = "0.2"
serde_json = "1"
sha2 = "0.10"
sha2 = "0.11"
tar = "0.4"
ureq = "2"
+35 -2
View File
@@ -13,7 +13,13 @@ wsbx pi # pi coding agent, same version + config as
wsbx run alpine # bare VM, interactive shell
wsbx run -w . ubuntu -- make test # one-off command, exit code propagated
wsbx run -p 8000 alpine # host's localhost:8000 reachable as localhost:8000 in the VM
wsbx ps # sandboxes: running/stopped, image, disk use, workspace (alias: ls)
wsbx exec NAME # shell in a running sandbox (alias: sh), as the image's user
wsbx exec -u root NAME -- apk add git # command, exit code propagated
wsbx forward NAME 3000 8080:80 # sandbox's localhost:3000 on the Mac's localhost:3000 (until Ctrl-C; alias fwd)
wsbx code NAME # VS Code in the browser, running in the sandbox (until Ctrl-C)
wsbx code NAME --desktop # desktop VS Code via Remote-SSH; also: ssh NAME.wsbx
wsbx ps # sandboxes: status, CPU, memory, net/block I/O, PIDs, disk, image (alias: ls)
wsbx ps -f # same, refreshed every second like `docker stats`
wsbx rm NAME... # delete stopped sandboxes; --disk deletes only the disk (fresh on next start)
wsbx pull IMAGE
wsbx gc # delete cached images no sandbox uses
@@ -25,6 +31,20 @@ are re-resolved after 24 h (cached copy used when offline); the superseded image
Options: `--name --workspace --cpus --memory --disk --env K[=V] --host-port/-p PORT --fresh --pull --kernel`.
**exec**: host → vfkit unix socket (`exec.sock`) → vsock → guest helper. It runs in the workspace dir with the sandbox's env.
You get a pty (with resize) when stdin and stdout are terminals, plain pipes otherwise, so `wsbx exec x -- cat f > out` is byte-exact.
**forward**: host `127.0.0.1:HOSTPORT` → `exec.sock` → vsock → guest helper → guest `localhost:PORT`, one exec-style session
per connection. Works for services bound only to the guest's loopback, on running sandboxes, no restart. `0:PORT` picks a free host port.
**code**: browser mode fetches the official VS Code CLI into the sandbox (`~/.wsbx-vscode`, needs internet in the VM) and runs
`code serve-web` on the guest's loopback, forwarded like `forward` to a random host port behind a random connection token.
The CLI downloads the matching server on first open (glibc or Alpine images). `--desktop` writes `~/.wsbx/ssh_config` (a `NAME.wsbx`
host per sandbox) and adds an `Include` for it at the top of `~/.ssh/config` once. ssh reaches an SSH server in the guest helper through `wsbx exec`
(`ProxyCommand`): no port, no keys, you're the image user. Remote-SSH then installs its server in the VM as usual.
If the VM was killed mid-download and the server won't start, delete `~/.vscode/cli/serve-web` (browser) or `~/.vscode-server` (desktop) in the sandbox.
Both need a sandbox started by this wsbx version (the guest helper ships with each start).
**Host tunnel** (`-p PORT`): guest `localhost:PORT` → vsock → vfkit → unix socket → host `localhost:PORT`.
Nothing listens on a host network interface, and only the listed ports are reachable.
@@ -48,10 +68,23 @@ The VM is the boundary. The guest (including a prompt-injected agent or a malici
- **vfkit arguments**: paths containing `,` `"` or control characters are refused (vfkit splits options on commas).
Workspaces overlapping `~/.wsbx` are refused.
- **Registry**: references are validated before any request, digest pins are enforced, and every blob/manifest is sha256-verified.
The tag cache is keyed by the exact reference (`a_b:t` and `a/b:t` never share an entry).
Auth realms must be https. For public registries, connections to loopback/private/link-local addresses are refused
(token realms and redirects included).
(token realms and redirects included, IPv6 forms like NAT64/6to4 too). A registry counts as local only by name:
`localhost` or a private IP literal (`192.168.1.5:5000`). LAN registries addressed by a DNS name are refused; use the IP.
All downloads (registries, kernel) time out after 30 s without progress (connect or any single read).
- **Guest base**: the static busybox (PID 1 in every VM) is pulled pinned by digest. The kernel comes from the Kata release
over the same public-addresses-only client and is sha256-checked against the release's published digest.
- **pi config**: allowlisted items only. Symlinks are followed only into `~/.pi/agent` or `~/.agents/skills`, with no cycles
and no special files, capped at 256 MB.
- **exec**: sessions run as root before dropping to the user, so the guest helper accepts them only from the host's vsock CID,
not from guest processes via vsock loopback.
- **forward**: listens on the host's loopback only. Browsers don't isolate `localhost` cookies by port, so a forwarded guest
page can read non-HttpOnly cookies your other localhost apps set. Forward only what you need.
- **code**: the SSH server is only reachable through `wsbx exec` (your user's `~/.wsbx`), so it skips auth, with an ephemeral host key.
It allows sessions and host → guest forwarding only; guest → host forwarding (`-R`) and agent forwarding are refused. Desktop VS Code
auto-forwards ports the guest opens to your localhost (turn off `remote.autoForwardPorts` if unwanted), and its client
trusts the remote server more than a browser tab does. The browser mode has the same localhost-cookie caveat as `forward`.
- **Host tunnel**: only the ports you pass with `-p` (or the local `baseUrl`s in pi's `models.json`), over vsock.
Remaining risks:
+5 -4
View File
@@ -2,17 +2,18 @@
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=guest/fwd.go");
let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("wsbx-fwd");
println!("cargo:rerun-if-changed=guest"); // a directory: cargo watches everything in it
let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("wsbx-guest");
let ok = Command::new("go")
.args(["build", "-trimpath", "-ldflags=-s -w", "-o"])
.arg(&out)
.arg("guest/fwd.go")
.arg(".")
.current_dir("guest")
.env("GOOS", "linux")
.env("GOARCH", "arm64")
.env("CGO_ENABLED", "0")
.status()
.expect("Go is required to build the guest helper (brew install go)")
.success();
assert!(ok, "go build guest/fwd.go failed");
assert!(ok, "go build guest helper failed");
}
+339
View File
@@ -0,0 +1,339 @@
// `wsbx exec` server: host -> vfkit unix socket -> vsock -> here.
//
// Protocol: the host sends one JSON request line, then frames go both ways: kind byte, u32 BE length, payload.
//
// host -> guest: 'd' stdin data, 'e' stdin EOF, 'w' window size (rows, cols: u16 BE)
// guest -> host: 'o' stdout, 'r' stderr, 'k' keepalive (ignored), 'x' exit status (u32 BE), always last
//
// With Tty the command gets a pty (stdout carries everything); otherwise plain pipes.
// With Connect (`wsbx forward`) there's no command: 'd'/'e' go to the guest's localhost:Connect, its output
// comes back as 'o', and 'x' marks its EOF (status 0) or a failed dial (1, after an 'r' message).
package main
import (
"bufio"
"cmp"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
)
type request struct {
Argv []string
User, Term string
Tty bool
Rows, Cols uint16
Connect uint16
}
func serveExec(port uint32) error {
fd, err := syscall.Socket(afVsock, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
if err != nil {
return err
}
sa := vsockAddr(port, 0xFFFFFFFF) // VMADDR_CID_ANY
if _, _, e := syscall.Syscall(syscall.SYS_BIND, uintptr(fd), uintptr(unsafe.Pointer(&sa[0])), 16); e != 0 {
return e
}
if err := syscall.Listen(fd, 16); err != nil {
return err
}
go func() {
for {
var peer [16]byte
n := uint32(len(peer))
c, _, e := syscall.Syscall6(syscall.SYS_ACCEPT4, uintptr(fd), uintptr(unsafe.Pointer(&peer[0])),
uintptr(unsafe.Pointer(&n)), syscall.SOCK_CLOEXEC|syscall.SOCK_NONBLOCK, 0, 0)
if e != 0 {
time.Sleep(10 * time.Millisecond)
continue
}
// Sessions run as root: only the host may open them, not guest processes via vsock loopback.
if binary.LittleEndian.Uint32(peer[8:]) != cidHost {
syscall.Close(int(c))
continue
}
go session(os.NewFile(c, "exec")) // non-blocking fd: Close interrupts pending reads
}
}()
return nil
}
func session(c *os.File) {
defer c.Close()
r := bufio.NewReader(c)
var req request
if line, err := r.ReadBytes('\n'); err != nil || json.Unmarshal(line, &req) != nil {
return
}
var mu sync.Mutex
send := func(kind byte, p []byte) error {
mu.Lock()
defer mu.Unlock()
_, err := c.Write(append(binary.BigEndian.AppendUint32([]byte{kind}, uint32(len(p))), p...))
return err
}
fail := func(err error) {
send('r', []byte("wsbx: "+err.Error()+"\r\n"))
send('x', binary.BigEndian.AppendUint32(nil, 127))
}
if req.Connect != 0 {
forward(r, send, req.Connect)
return
}
cmd, uid := command(req)
var stdin io.WriteCloser
var pty, slave *os.File
output := make(chan struct{})
if req.Tty {
m, s, err := openPty()
if err != nil {
fail(err)
return
}
defer m.Close()
pty, slave, stdin = m, s, m
setWinsize(m, req.Rows, req.Cols)
if uid > 0 {
s.Chown(uid, -1) // like sshd: tools that reopen the tty by name (gpg, screen) need to own it
}
cmd.Stdin, cmd.Stdout, cmd.Stderr = s, s, s
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true} // Ctty 0: the child's stdin
defer s.Close()
} else {
cmd.Stdout, cmd.Stderr = frames{send, 'o'}, frames{send, 'r'}
stdin, _ = cmd.StdinPipe()
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.WaitDelay = time.Second // don't hang on background processes that keep stdout open
close(output) // Wait copies it
}
if err := cmd.Start(); err != nil {
fail(err)
return
}
if pty != nil {
slave.Close() // our copy of the slave: the master reads EIO once the session's are gone
go func() {
b := make([]byte, 32<<10)
for {
n, err := pty.Read(b)
if n > 0 {
send('o', b[:n])
}
if err != nil {
close(output)
return
}
}
}()
}
var exited atomic.Bool
go func() {
for {
kind, p, err := readFrame(r)
if err != nil {
if !exited.Load() {
syscall.Kill(-cmd.Process.Pid, syscall.SIGHUP) // host went away
}
return
}
switch {
case kind == 'd':
stdin.Write(p)
case kind == 'e' && pty == nil:
stdin.Close()
case kind == 'w' && pty != nil && len(p) == 4:
setWinsize(pty, binary.BigEndian.Uint16(p), binary.BigEndian.Uint16(p[2:]))
}
}
}()
// vfkit doesn't pass on the host closing its end; it only closes the vsock once forwarding to the
// gone host fails. Keep sending, so a killed `wsbx exec` hangs up the session within ~2 s.
go func() {
for range time.Tick(2 * time.Second) {
if send('k', nil) != nil {
return
}
}
}()
cmd.Wait()
exited.Store(true)
select {
case <-output:
case <-time.After(500 * time.Millisecond): // a background job still holds the pty
}
send('x', binary.BigEndian.AppendUint32(nil, exitCode(cmd.ProcessState)))
}
// exitCode like a shell's $?: 128+N for a signal.
func exitCode(ps *os.ProcessState) uint32 {
if ws, ok := ps.Sys().(syscall.WaitStatus); ok && ws.Signaled() {
return 128 + uint32(ws.Signal())
}
return uint32(ps.ExitCode())
}
// forward pipes a session to the guest's localhost:port until both sides sent EOF or the host is gone.
func forward(r *bufio.Reader, send func(byte, []byte) error, port uint16) {
t, err := net.Dial("tcp", net.JoinHostPort("localhost", strconv.Itoa(int(port))))
if err != nil {
send('r', []byte(err.Error()))
send('x', binary.BigEndian.AppendUint32(nil, 1))
return
}
tc := t.(*net.TCPConn)
defer tc.Close()
done := make(chan struct{}, 2)
go func() {
io.Copy(frames{send, 'o'}, tc)
send('x', binary.BigEndian.AppendUint32(nil, 0))
done <- struct{}{}
}()
go func() {
defer func() { done <- struct{}{} }()
for {
kind, p, err := readFrame(r)
if err != nil {
tc.Close() // host went away
return
}
switch kind {
case 'd':
tc.Write(p)
case 'e':
tc.CloseWrite()
return
}
}
}()
// Same as exec: vfkit doesn't pass on the host's close, so probe; a failed send means it's gone.
go func() {
for range time.Tick(2 * time.Second) {
if send('k', nil) != nil {
tc.Close()
return
}
}
}()
<-done
<-done
}
// command builds the session command: as the requested user, else the image user (like the main command),
// with run.sh's environment (image env, -e, HOME) and working directory, which this process inherited.
func command(req request) (*exec.Cmd, int) {
argv := req.Argv
if len(argv) == 0 {
argv = []string{"/bin/sh"}
if _, err := os.Stat("/bin/bash"); err == nil {
argv[0] = "/bin/bash"
}
}
env := os.Environ()
if req.Term != "" {
env = append(env, "TERM="+req.Term)
}
user := cmp.Or(req.User, os.Getenv("WSBX_USER"), "root")
uid, home := passwd(user)
if req.User != "" {
env = append(env, "HOME="+home) // run.sh set HOME for the image user
}
if uid != 0 {
argv = append([]string{"/.wsbx/busybox", "chpst", "-u", user}, argv...)
}
cmd := exec.Command(argv[0], argv[1:]...)
cmd.Env = env
return cmd, uid
}
// passwd looks up uid and home of a user (name or uid, optionally ":group"); uid -1 if unknown.
func passwd(user string) (uid int, home string) {
name, _, _ := strings.Cut(user, ":")
uid, home = -1, "/"
if n, err := strconv.Atoi(name); err == nil {
uid = n
}
b, _ := os.ReadFile("/etc/passwd")
for _, l := range strings.Split(string(b), "\n") {
if f := strings.Split(l, ":"); len(f) > 5 && (f[0] == name || f[2] == name) {
uid, _ = strconv.Atoi(f[2])
return uid, f[5]
}
}
return
}
type frames struct {
send func(byte, []byte) error
kind byte
}
func (f frames) Write(p []byte) (int, error) {
if err := f.send(f.kind, p); err != nil {
return 0, err
}
return len(p), nil
}
func readFrame(r *bufio.Reader) (byte, []byte, error) {
var h [5]byte
if _, err := io.ReadFull(r, h[:]); err != nil {
return 0, nil, err
}
n := binary.BigEndian.Uint32(h[1:])
if n > 1<<20 {
return 0, nil, errors.New("frame too large")
}
p := make([]byte, n)
_, err := io.ReadFull(r, p)
return h[0], p, err
}
func openPty() (m, s *os.File, err error) {
if m, err = os.OpenFile("/dev/ptmx", os.O_RDWR|syscall.O_NOCTTY, 0); err != nil {
return
}
var n, unlock uint32
if err = ioctl(m, syscall.TIOCSPTLCK, unsafe.Pointer(&unlock)); err == nil {
if err = ioctl(m, syscall.TIOCGPTN, unsafe.Pointer(&n)); err == nil {
s, err = os.OpenFile(fmt.Sprintf("/dev/pts/%d", n), os.O_RDWR|syscall.O_NOCTTY, 0)
}
}
if err != nil {
m.Close()
}
return
}
func setWinsize(pty *os.File, rows, cols uint16) {
ws := [4]uint16{rows, cols, 0, 0}
ioctl(pty, syscall.TIOCSWINSZ, unsafe.Pointer(&ws))
}
// ioctl via SyscallConn: f.Fd() would switch the fd to blocking mode (Close could then not interrupt Read).
func ioctl(f *os.File, req uintptr, arg unsafe.Pointer) error {
rc, err := f.SyscallConn()
if err != nil {
return err
}
var e syscall.Errno
rc.Control(func(fd uintptr) { _, _, e = syscall.Syscall(syscall.SYS_IOCTL, fd, req, uintptr(arg)) })
if e != 0 {
return e
}
return nil
}
+50 -14
View File
@@ -1,32 +1,43 @@
// wsbx-fwd: guest side of the host tunnel.
// Usage: wsbx-fwd PORT=VSOCKPORT...
// Listens on 127.0.0.1:PORT and [::1]:PORT and pipes each connection to the host (vsock CID 2).
// wsbx-guest: guest helper, started by run.sh as root.
// Usage: wsbx-guest [-exec VSOCKPORT] PORT=VSOCKPORT... | wsbx-guest -ssh-stdio
// -ssh-stdio serves one SSH connection on stdin/stdout (ssh.go), run through `wsbx exec`.
// -exec serves `wsbx exec` sessions (exec.go). Each PORT=VSOCKPORT is a host tunnel: listens on
// 127.0.0.1:PORT and [::1]:PORT and pipes each connection to the host (vsock CID 2).
package main
import (
"encoding/binary"
"flag"
"fmt"
"io"
"net"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"unsafe"
)
const afVsock = 40
const (
afVsock = 40
cidHost = 2
)
// struct sockaddr_vm { u16 family; u16 reserved; u32 port; u32 cid; u8 flags; u8 zero[3]; }
func vsockAddr(port, cid uint32) (sa [16]byte) {
binary.LittleEndian.PutUint16(sa[0:], afVsock)
binary.LittleEndian.PutUint32(sa[4:], port)
binary.LittleEndian.PutUint32(sa[8:], cid)
return
}
func dialHost(port uint32) (*os.File, error) {
fd, err := syscall.Socket(afVsock, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
if err != nil {
return nil, err
}
// struct sockaddr_vm { u16 family; u16 reserved; u32 port; u32 cid; u8 flags; u8 zero[3]; }
var sa [16]byte
binary.LittleEndian.PutUint16(sa[0:], afVsock)
binary.LittleEndian.PutUint32(sa[4:], port)
binary.LittleEndian.PutUint32(sa[8:], 2) // VMADDR_CID_HOST
sa := vsockAddr(port, cidHost)
if _, _, e := syscall.Syscall(syscall.SYS_CONNECT, uintptr(fd), uintptr(unsafe.Pointer(&sa[0])), 16); e != 0 {
syscall.Close(fd)
return nil, e
@@ -38,7 +49,7 @@ func handle(c *net.TCPConn, port uint32) {
defer c.Close()
f, err := dialHost(port)
if err != nil {
fmt.Fprintln(os.Stderr, "wsbx-fwd: vsock:", err)
fmt.Fprintln(os.Stderr, "wsbx-guest: vsock:", err)
return
}
defer f.Close()
@@ -54,17 +65,35 @@ func handle(c *net.TCPConn, port uint32) {
}
func main() {
for _, spec := range os.Args[1:] {
execPort := flag.Uint("exec", 0, "serve `wsbx exec` sessions on this vsock port")
sshStdio := flag.Bool("ssh-stdio", false, "serve one SSH connection on stdin/stdout")
flag.Parse()
if *sshStdio {
if err := serveSSH(); err != nil {
fmt.Fprintln(os.Stderr, "wsbx-guest: ssh:", err)
os.Exit(1)
}
return
}
// ash starts '&' jobs with SIGINT/SIGQUIT ignored, and ignored signals survive exec: Ctrl-C would
// not work in exec sessions. Handled signals are reset to default in children, so take them over.
signal.Notify(make(chan os.Signal, 1), syscall.SIGINT, syscall.SIGQUIT)
if *execPort != 0 {
if err := serveExec(uint32(*execPort)); err != nil {
fmt.Fprintln(os.Stderr, "wsbx-guest: exec:", err)
}
}
for _, spec := range flag.Args() {
p, v, _ := strings.Cut(spec, "=")
vport, err := strconv.ParseUint(v, 10, 32)
if err != nil {
fmt.Fprintln(os.Stderr, "wsbx-fwd: bad spec", spec)
fmt.Fprintln(os.Stderr, "wsbx-guest: bad spec", spec)
os.Exit(2)
}
for _, addr := range []string{"127.0.0.1:" + p, "[::1]:" + p} {
ln, err := net.Listen("tcp", addr)
if err != nil {
fmt.Fprintln(os.Stderr, "wsbx-fwd:", err)
fmt.Fprintln(os.Stderr, "wsbx-guest:", err)
continue
}
go func() {
@@ -79,6 +108,13 @@ func main() {
}
// Signal readiness (run.sh waits on this) before the agent starts.
fmt.Println("ready")
os.Stdout.Close()
// /dev/null over fd 1 instead of closing it: a socket reusing fd 1 would make Go treat EPIPE on it
// as a broken stdout and die of SIGPIPE (e.g. keepalive to a host that's gone).
if null, err := os.OpenFile("/dev/null", os.O_WRONLY, 0); err == nil {
syscall.Dup3(int(null.Fd()), 1, 0)
null.Close()
} else {
os.Stdout.Close()
}
select {}
}
+7
View File
@@ -0,0 +1,7 @@
module wsbx-guest
go 1.26.0
require golang.org/x/crypto v0.58.0
require golang.org/x/sys v0.49.0 // indirect
+6
View File
@@ -0,0 +1,6 @@
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM=
golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY=
+211
View File
@@ -0,0 +1,211 @@
// `wsbx code --desktop`: an SSH server on stdin/stdout for VS Code Remote-SSH (or plain ssh), one per
// connection, started by the host's ProxyCommand: `wsbx exec NAME -- /.wsbx/host/guest -ssh-stdio`.
// The exec channel is the authentication (only the host user can open it), so the client gets in with no
// auth against an ephemeral host key, and runs as the user exec started us as (the image user).
// Supported: sessions (exec, shell, pty, env, window-change) and host -> guest forwarding (direct-tcpip,
// direct-streamlocal). Guest -> host forwarding (tcpip-forward) and agent forwarding are refused.
package main
import (
"crypto/ed25519"
"fmt"
"io"
"net"
"os"
"os/exec"
"strconv"
"syscall"
"time"
"golang.org/x/crypto/ssh"
)
func serveSSH() error {
_, key, err := ed25519.GenerateKey(nil)
if err != nil {
return err
}
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
return err
}
cfg := &ssh.ServerConfig{NoClientAuth: true}
cfg.AddHostKey(signer)
_, chans, reqs, err := ssh.NewServerConn(stdio{}, cfg)
if err != nil {
return err
}
go ssh.DiscardRequests(reqs) // global requests: tcpip-forward etc. are refused
for nc := range chans {
switch nc.ChannelType() {
case "session":
go sshSession(nc)
case "direct-tcpip":
var p struct {
Host string
Port uint32
OrigHost string
OrigPort uint32
}
if ssh.Unmarshal(nc.ExtraData(), &p) != nil {
nc.Reject(ssh.ConnectionFailed, "bad request")
continue
}
go sshForward(nc, "tcp", net.JoinHostPort(p.Host, strconv.Itoa(int(p.Port))))
case "direct-streamlocal@openssh.com":
var p struct {
Path string
Reserved0 string
Reserved1 uint32
}
if ssh.Unmarshal(nc.ExtraData(), &p) != nil {
nc.Reject(ssh.ConnectionFailed, "bad request")
continue
}
go sshForward(nc, "unix", p.Path)
default:
nc.Reject(ssh.UnknownChannelType, "not supported by wsbx")
}
}
return nil
}
func sshForward(nc ssh.NewChannel, network, addr string) {
c, err := net.Dial(network, addr)
if err != nil {
nc.Reject(ssh.ConnectionFailed, err.Error())
return
}
defer c.Close()
ch, reqs, err := nc.Accept()
if err != nil {
return
}
defer ch.Close()
go ssh.DiscardRequests(reqs)
done := make(chan struct{})
go func() {
io.Copy(ch, c)
ch.CloseWrite()
close(done)
}()
io.Copy(c, ch)
if cw, ok := c.(interface{ CloseWrite() error }); ok {
cw.CloseWrite()
}
<-done
}
func sshSession(nc ssh.NewChannel) {
ch, reqs, err := nc.Accept()
if err != nil {
return
}
defer ch.Close()
var env []string
var pty, tty *os.File
defer func() {
if pty != nil {
pty.Close()
}
}()
started, command := false, ""
for req := range reqs {
ok, start := false, false
switch req.Type {
case "env":
var kv struct{ K, V string }
if ssh.Unmarshal(req.Payload, &kv) == nil {
env, ok = append(env, kv.K+"="+kv.V), true
}
case "pty-req":
var p struct {
Term string
Cols, Rows, W, H uint32
Modes string
}
if pty == nil && !started && ssh.Unmarshal(req.Payload, &p) == nil {
if m, s, err := openPty(); err == nil {
pty, tty, ok = m, s, true
setWinsize(m, uint16(p.Rows), uint16(p.Cols))
env = append(env, "TERM="+p.Term)
}
}
case "window-change":
var p struct{ Cols, Rows, W, H uint32 }
if pty != nil && ssh.Unmarshal(req.Payload, &p) == nil {
setWinsize(pty, uint16(p.Rows), uint16(p.Cols))
ok = true
}
case "shell", "exec":
var p struct{ Cmd string }
if !started && (req.Type == "shell" || ssh.Unmarshal(req.Payload, &p) == nil) {
started, ok, start, command = true, true, true, p.Cmd
}
}
if req.WantReply {
req.Reply(ok, nil)
}
if start {
go func(env []string) {
code := sshRun(ch, command, env, pty, tty)
ch.SendRequest("exit-status", false, ssh.Marshal(struct{ C uint32 }{code}))
ch.Close()
}(env)
}
}
}
// sshRun runs command (empty: a login shell) like sshd: `$SHELL -c command`, in run.sh's env and dir.
func sshRun(ch ssh.Channel, command string, env []string, pty, tty *os.File) uint32 {
shell := "/bin/sh"
if _, err := os.Stat("/bin/bash"); err == nil {
shell = "/bin/bash"
}
cmd := exec.Command(shell, "-c", command)
if command == "" {
cmd = exec.Command(shell, "-l")
}
cmd.Env = append(os.Environ(), env...)
if pty != nil {
cmd.Stdin, cmd.Stdout, cmd.Stderr = tty, tty, tty
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}
} else {
cmd.Stdin, cmd.Stdout, cmd.Stderr = ch, ch, ch.Stderr()
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.WaitDelay = time.Second // don't hang on daemons (the VS Code server) that keep stdout open
}
if err := cmd.Start(); err != nil {
fmt.Fprintln(ch.Stderr(), "wsbx:", err)
return 127
}
if pty == nil {
cmd.Wait()
return exitCode(cmd.ProcessState)
}
tty.Close() // our copy: the master reads EIO once the session's are gone
go io.Copy(pty, ch)
done := make(chan struct{})
go func() {
io.Copy(ch, pty)
close(done)
}()
cmd.Wait()
select {
case <-done:
case <-time.After(500 * time.Millisecond): // a background job still holds the pty
}
return exitCode(cmd.ProcessState)
}
// stdio is our stdin/stdout as the net.Conn the SSH server runs on.
type stdio struct{}
func (stdio) Read(b []byte) (int, error) { return os.Stdin.Read(b) }
func (stdio) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
func (stdio) Close() error { os.Stdin.Close(); return os.Stdout.Close() }
func (stdio) LocalAddr() net.Addr { return &net.UnixAddr{Name: "stdio", Net: "unix"} }
func (stdio) RemoteAddr() net.Addr { return &net.UnixAddr{Name: "wsbx", Net: "unix"} }
func (stdio) SetDeadline(time.Time) error { return nil }
func (stdio) SetReadDeadline(time.Time) error { return nil }
func (stdio) SetWriteDeadline(time.Time) error { return nil }
+110
View File
@@ -0,0 +1,110 @@
//! Coding-agent sandboxes (Docker sandbox-templates images) and pi config mirroring.
use crate::vm::{Guest, run};
use crate::util::{base, sq};
use crate::Agent;
use anyhow::{Context, Result};
use serde_json::Value;
use std::fs;
use std::path::{Path, PathBuf};
/// Fallback when pi isn't installed on the host (otherwise the host's package + version are mirrored).
const PI_PKG: &str = "@earendil-works/pi-coding-agent";
/// User-level pi config worth sharing. Never npm/, bin/ (macOS binaries), sessions/, caches.
const PI_CONFIG: &[&str] = &[
"settings.json", "keybindings.json", "models.json", "mcp.json", "AGENTS.override.md", "AGENTS.md", "AGENTS.MD",
"CLAUDE.md", "CLAUDE.MD", "SYSTEM.md", "APPEND_SYSTEM.md", "extensions", "skills", "prompts", "themes",
];
// No official Docker template for pi: the generic `shell` template has node + npm; pi is installed
// (pinned to the host's version) onto the persistent sandbox disk.
pub fn pi(h: &Path, mut a: Agent, auth: bool) -> Result<()> {
let user_home = PathBuf::from(std::env::var_os("HOME").expect("HOME"));
let agent_dir = std::env::var_os("PI_CODING_AGENT_DIR").map(PathBuf::from).unwrap_or_else(|| user_home.join(".pi/agent"));
let agents_skills = user_home.join(".agents/skills"); // Agent Skills location pi also reads
let mut items = PI_CONFIG.to_vec();
if auth {
items.push("auth.json");
}
let mut stage: Vec<(PathBuf, String)> = items.iter().map(|i| (agent_dir.join(i), format!("pi/{i}"))).collect();
stage.push((agents_skills.clone(), "agents-skills".into()));
stage.retain(|(p, _)| p.exists());
let roots = [&agent_dir, &agents_skills].iter().filter_map(|p| fs::canonicalize(p).ok()).collect();
if let Ok(m) = fs::read(agent_dir.join("models.json")) {
let m: Value = serde_json::from_slice(&m).context("models.json")?;
for p in m["providers"].as_object().into_iter().flat_map(|p| p.values()) {
if let Some(port) = p["baseUrl"].as_str().and_then(local_port) {
a.o.host_ports.push(port);
}
}
}
let sh = match host_npm_pkg("pi") {
Some((pkg, v)) => format!(
"[ \"$(pi --version 2>/dev/null)\" = {v} ] || npm install -g --force {}@{v} >&2 || exit 1; exec pi \"$@\"",
sq(&pkg)
),
None => format!("command -v pi >/dev/null || npm install -g --force {PI_PKG} >&2 || exit 1; exec pi \"$@\""),
};
// Link each staged item into the guest's own ~/.pi/agent (which keeps its linux npm/, bin/, sessions/).
let setup = r#"PA="$HOME/.pi/agent"; S=/.wsbx/host/stage; $B mkdir -p "$PA" "$HOME/.agents"
for l in "$PA"/* "$PA"/.[!.]* "$HOME/.agents/skills"; do [ -L "$l" ] && $B rm -f "$l"; done
for f in "$S"/pi/*; do [ -e "$f" ] && $B rm -rf "$PA/${f##*/}" && $B ln -s "$f" "$PA/${f##*/}"; done
[ -e "$S/agents-skills" ] && $B rm -rf "$HOME/.agents/skills" && $B ln -s "$S/agents-skills" "$HOME/.agents/skills"
[ -z "$WSBX_USER" ] || $B chown "$WSBX_USER" "$HOME/.pi" "$PA" "$HOME/.agents"
"#;
let cmd = ["bash", "-c", &sh, "pi"].map(String::from).to_vec();
let keys = ["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "GEMINI_API_KEY", "OPENROUTER_API_KEY", "GROQ_API_KEY", "XAI_API_KEY", "MISTRAL_API_KEY", "CEREBRAS_API_KEY", "ZAI_API_KEY"];
agent(h, a, "shell", "pi", cmd, &keys, Guest { stage, roots, setup: setup.into() })
}
/// npm package (name, version) behind a host command: follow the bin symlink up to its package.json.
fn host_npm_pkg(cmd: &str) -> Option<(String, String)> {
let bin = std::env::split_paths(&std::env::var_os("PATH")?).map(|d| d.join(cmd)).find(|p| p.is_file())?;
let real = fs::canonicalize(bin).ok()?;
let pj = real.ancestors().map(|d| d.join("package.json")).find(|p| p.is_file())?;
let v: Value = serde_json::from_slice(&fs::read(pj).ok()?).ok()?;
let (name, ver) = (v["name"].as_str()?, v["version"].as_str()?);
ver.chars().all(|c| c.is_ascii_alphanumeric() || ".-+".contains(c)).then(|| (name.into(), ver.into()))
}
/// Port of a URL pointing at the host's loopback, e.g. "http://127.0.0.1:8000/v1" -> 8000.
fn local_port(url: &str) -> Option<u16> {
let (scheme, rest) = url.split_once("://")?;
let auth = rest.split('/').next()?;
let (host, port) = match auth.rsplit_once(':') {
Some((h, p)) if !p.ends_with(']') => (h, p.parse().ok()?),
_ => (auth, if scheme == "https" { 443 } else { 80 }),
};
["127.0.0.1", "localhost", "[::1]", "0.0.0.0"].contains(&host).then_some(port)
}
pub fn agent(h: &Path, a: Agent, template: &str, short: &str, cmd: Vec<String>, keys: &[&str], g: Guest) -> Result<()> {
let mut o = a.o;
if o.workspace.is_none() {
o.workspace = Some(std::env::current_dir()?);
}
if o.name.is_none() {
o.name = Some(format!("{short}-{}", base(o.workspace.as_ref().unwrap())));
}
let set: Vec<String> = keys.iter().filter(|k| std::env::var_os(k).is_some()).map(|k| k.to_string()).collect();
if a.keys {
o.env.extend(set);
} else if !set.is_empty() {
eprintln!("wsbx: not forwarding {} (use --keys; code in the VM, e.g. a prompt-injected agent, could read it)", set.join(", "));
}
// Docker's sandbox templates: Cmd is the agent (e.g. `claude --dangerously-skip-permissions`) unless overridden
run(h, o, &format!("docker/sandbox-templates:{template}"), cmd, &a.args, g)
}
#[cfg(test)]
mod tests {
#[test]
fn local_port() {
use super::local_port as lp;
assert_eq!(lp("http://127.0.0.1:8000/v1"), Some(8000));
assert_eq!(lp("http://localhost:11434"), Some(11434));
assert_eq!(lp("http://[::1]:1234/v1"), Some(1234));
assert_eq!(lp("http://localhost/v1"), Some(80));
assert_eq!(lp("http://192.168.1.169:8080/v1"), None);
assert_eq!(lp("https://api.openai.com/v1"), None);
}
}
+121
View File
@@ -0,0 +1,121 @@
//! `wsbx code NAME`: VS Code for a running sandbox.
//! Browser (default): `code serve-web` runs in the VM on its localhost, forwarded to the host's localhost
//! (like `wsbx forward`) behind a random connection token, and opened in the browser.
//! --desktop: desktop VS Code via Remote-SSH. ssh reaches the SSH server in the guest helper (guest/ssh.go)
//! through `wsbx exec` as ProxyCommand: no network port, no keys.
use crate::exec::{self, sock};
use crate::forward;
use crate::util::sq;
use anyhow::{Context, Result, bail};
use serde_json::Value;
use std::fs;
use std::io::Read;
use std::net::TcpListener;
use std::path::Path;
use std::process::Command;
use std::time::{Duration, Instant};
/// Guest side of the browser mode: fetch the VS Code CLI once (static musl binary, onto the sandbox disk),
/// then serve. The CLI downloads the matching server on first use. $1 port, $2 token.
const WEB_SH: &str = r#"set -e
B=/.wsbx/busybox D="$HOME/.wsbx-vscode"
U=https://update.code.visualstudio.com/latest/cli-alpine-arm64/stable
if [ ! -x "$D/code" ]; then
echo "wsbx: downloading the VS Code CLI..."
$B rm -rf "$D/tmp" && $B mkdir -p "$D/tmp"
if command -v curl >/dev/null; then curl -fsSL "$U"; else wget -qO- "$U"; fi | $B tar -xzf - -C "$D/tmp"
$B sync && $B mv "$D/tmp/code" "$D/code" && $B rmdir "$D/tmp" # never a half-written code, even if the VM is killed
fi
exec "$D/code" serve-web --host 127.0.0.1 --port "$1" --connection-token "$2" --accept-server-license-terms
"#;
pub fn code(h: &Path, name: &str, desktop: bool) -> Result<()> {
let sock = sock(h, name)?; // also checks the name and that it's running
let meta: Value = serde_json::from_slice(&fs::read(h.join("sandboxes").join(name).join("meta.json"))?)?;
let folder = enc(meta["workspace"].as_str().unwrap_or("/"));
if desktop {
return self::desktop(h, name, &folder);
}
let l = TcpListener::bind(("127.0.0.1", 0))?;
let port = l.local_addr()?.port(); // the same port in the guest: random and high, so most likely free there too
let mut t = [0u8; 16];
fs::File::open("/dev/urandom")?.read_exact(&mut t)?;
let token = hex::encode(t);
eprintln!("wsbx: starting VS Code in {name} (the first start downloads it)");
eprintln!("wsbx: by using it you accept the VS Code Server license: https://aka.ms/vscode-server-license");
let (s, tk) = (sock.clone(), token.clone());
let server = std::thread::spawn(move || exec::stream(&s, &["/.wsbx/busybox", "sh", "-c", WEB_SH, "sh", &port.to_string(), &tk]));
let t0 = Instant::now();
while !forward::listening(&sock, port) {
if server.is_finished() {
let r = server.join().unwrap();
bail!("VS Code server in {name} stopped ({:?})", r.map_err(|e| e.to_string()));
}
if t0.elapsed() > Duration::from_secs(300) {
bail!("VS Code server in {name} didn't start within 5 minutes");
}
std::thread::sleep(Duration::from_millis(500));
}
let (s, n) = (sock.clone(), name.to_string());
std::thread::spawn(move || forward::serve(&s, &n, vec![(l, port)]));
let url = format!("http://127.0.0.1:{port}/?tkn={token}&folder={folder}");
eprintln!("wsbx: VS Code for {name}: {url}\nwsbx: Ctrl-C stops it");
let _ = Command::new("open").arg(&url).status();
match server.join().unwrap()? {
Some(c) => bail!("VS Code server in {name} exited with {c}"),
None => bail!("lost connection to {name} (VM stopped?)"),
}
}
/// Write ssh config for every sandbox, include it from ~/.ssh/config once, open VS Code on NAME.wsbx.
fn desktop(h: &Path, name: &str, folder: &str) -> Result<()> {
let exe = std::env::current_exe()?;
let mut cfg = String::from("# Written by `wsbx code --desktop`, regenerated each time: don't edit.\n");
for e in fs::read_dir(h.join("sandboxes"))?.flatten().filter(|e| e.path().join("meta.json").exists()) {
let n = e.file_name().to_string_lossy().into_owned(); // sandbox names are [a-z0-9-]
cfg += &format!(
"\nHost {n}.wsbx\n ProxyCommand {} exec {n} -- /.wsbx/host/guest -ssh-stdio\n \
StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n LogLevel ERROR\n ForwardAgent no\n ForwardX11 no\n",
sq(&exe.to_string_lossy())
);
}
let ours = h.join("ssh_config");
fs::write(&ours, cfg)?;
let home = std::path::PathBuf::from(std::env::var_os("HOME").context("HOME")?);
let user_cfg = home.join(".ssh/config");
let include = format!("Include \"{}\"", ours.display());
let cur = match fs::read_to_string(&user_cfg) {
Ok(s) => s,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(e) => return Err(e).context("~/.ssh/config"),
};
if !cur.lines().any(|l| l.trim() == include) {
fs::create_dir_all(home.join(".ssh"))?;
// At the top: an Include after a `Host` line would only apply to that host.
fs::write(&user_cfg, format!("{include}\n\n{cur}"))?;
eprintln!("wsbx: added `{include}` to the top of ~/.ssh/config");
}
let url = format!("vscode://vscode-remote/ssh-remote+{name}.wsbx{folder}");
eprintln!("wsbx: opening {url} (needs the Remote - SSH extension); plain ssh works too: ssh {name}.wsbx");
if !Command::new("open").arg(&url).status()?.success() {
bail!("couldn't open {url} (is VS Code installed?)");
}
Ok(())
}
/// Percent-encode a path for a URL, keeping '/'.
fn enc(p: &str) -> String {
p.bytes()
.map(|b| if b.is_ascii_alphanumeric() || b"/-._~".contains(&b) { (b as char).to_string() } else { format!("%{b:02X}") })
.collect()
}
#[cfg(test)]
mod tests {
#[test]
fn enc() {
assert_eq!(super::enc("/Users/me/my repo/ä&x"), "/Users/me/my%20repo/%C3%A4%26x");
}
}
+65
View File
@@ -0,0 +1,65 @@
//! Sandbox root disk: empty ext4 seeded with a static busybox + stub init.
use crate::oci;
use crate::util::need;
use anyhow::{Result, bail};
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::Command;
/// Baked into each disk once; everything else comes fresh from the host share on every start.
const STUB_INIT: &str = "#!/.wsbx/busybox sh
/.wsbx/busybox mount -t virtiofs wsbx /.wsbx/host && exec /.wsbx/busybox sh /.wsbx/host/init
echo 'wsbx: cannot mount host share'; exec /.wsbx/busybox sh
";
/// busybox:musl pinned by digest: this binary is PID 1 in every VM, so a moved tag must not change it.
/// Bump deliberately (index digest of `docker.io/library/busybox:musl`).
const BUSYBOX: &str = "busybox@sha256:ea2b9914a16a4ac1981994af97b318f7c7d4db76b580c56177f08bf76f4a0be8";
/// Static busybox from the official busybox image; runs PID 1 + helpers in the guest.
fn busybox(h: &Path) -> Result<PathBuf> {
let p = h.join("busybox");
if p.exists() {
return Ok(p);
}
let img = oci::pull(h, BUSYBOX, false)?;
let tmp = h.join("busybox.tmp");
fs::create_dir_all(&tmp)?;
let ok = Command::new("tar").arg("-xf").arg(&img.rootfs).arg("-C").arg(&tmp).arg("bin").status()?.success();
if !ok {
bail!("failed to extract busybox");
}
// Must be a real file: a symlink would make us copy an arbitrary host file into guest disks.
if !fs::symlink_metadata(tmp.join("bin/busybox"))?.is_file() {
bail!("busybox image: bin/busybox is not a regular file");
}
fs::rename(tmp.join("bin/busybox"), &p)?;
fs::remove_dir_all(tmp)?;
Ok(p)
}
pub fn create_disk(h: &Path, disk: &Path, gib: u32) -> Result<()> {
let mke2fs = need("mke2fs", "e2fsprogs")?;
let seed = disk.with_extension("seed");
let _ = fs::remove_dir_all(&seed);
for d in [".wsbx/dev", ".wsbx/host"] {
fs::create_dir_all(seed.join(d))?;
}
fs::copy(busybox(h)?, seed.join(".wsbx/busybox"))?;
fs::write(seed.join(".wsbx/init"), STUB_INIT)?;
for f in ["busybox", "init"] {
fs::set_permissions(seed.join(".wsbx").join(f), fs::Permissions::from_mode(0o755))?;
}
let out = Command::new(mke2fs)
.args(["-q", "-F", "-t", "ext4", "-E", "root_owner=0:0", "-d"])
.arg(&seed)
.arg(disk)
.arg(format!("{gib}G"))
.output()?;
fs::remove_dir_all(&seed)?;
if !out.status.success() {
bail!("mke2fs failed: {}", String::from_utf8_lossy(&out.stderr));
}
Ok(())
}
+150
View File
@@ -0,0 +1,150 @@
//! `wsbx exec`: shell or command in a running sandbox, via vfkit's exec.sock -> vsock -> guest helper.
//! Protocol (see guest/exec.go): one JSON request line, then frames both ways: kind u8, len u32 BE, payload.
use crate::sandbox::lock_stopped;
use crate::util::sanitize;
use crate::vm::{stty, term_size};
use anyhow::{Result, anyhow, bail};
use serde_json::json;
use std::io::{self, IsTerminal, Read, Write};
use std::os::unix::net::UnixStream;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
/// Guest vsock port of the exec server.
pub const VPORT: u32 = 2222;
/// exec.sock of a running sandbox.
pub fn sock(h: &Path, name: &str) -> Result<PathBuf> {
let dir = h.join("sandboxes").join(name);
if name.is_empty() || sanitize(name) != name || !dir.join("meta.json").exists() {
bail!("no sandbox {name:?} (see wsbx ps)");
}
if lock_stopped(&dir).is_ok() {
bail!("sandbox {name} is not running");
}
Ok(dir.join("exec.sock"))
}
pub fn connect(sock: &Path, name: &str) -> Result<UnixStream> {
UnixStream::connect(sock).map_err(|e| anyhow!("can't reach {name}: {e} (started by an older wsbx?)"))
}
pub fn frame(kind: u8, p: &[u8]) -> Vec<u8> {
[&[kind][..], &(p.len() as u32).to_be_bytes(), p].concat()
}
pub fn exec(h: &Path, name: &str, user: Option<String>, argv: Vec<String>) -> Result<()> {
let s = connect(&sock(h, name)?, name)?;
// Like ssh: a pty only when we're interactive, so `wsbx exec x -- cat f > out` stays byte-exact.
let tty = io::stdin().is_terminal() && io::stdout().is_terminal();
let (rows, cols) = if tty { term_size().unwrap_or((24, 80)) } else { (0, 0) };
let w = Arc::new(Mutex::new(s.try_clone()?));
let req = json!({"argv": argv, "user": user, "tty": tty, "term": std::env::var("TERM").ok(), "rows": rows, "cols": cols});
w.lock().unwrap().write_all(format!("{req}\n").as_bytes())?;
let saved = if tty { stty(&["-g"]) } else { None };
if saved.is_some() {
stty(&["raw", "-echo"]);
}
let wi = w.clone();
std::thread::spawn(move || {
let (mut stdin, mut b) = (io::stdin().lock(), vec![0u8; 32 << 10]);
loop {
match stdin.read(&mut b) {
Ok(0) => break,
Ok(n) if send(&wi, b'd', &b[..n]).is_err() => return,
Ok(_) => {}
Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(_) => break,
}
}
let _ = send(&wi, b'e', &[]);
});
if tty {
// ponytail: polls instead of a SIGWINCH handler; resizes land within 250 ms.
std::thread::spawn(move || {
let mut last = (rows, cols);
loop {
std::thread::sleep(Duration::from_millis(250));
if let Some((r, c)) = term_size().filter(|&sz| sz != last) {
last = (r, c);
if send(&w, b'w', &[r.to_be_bytes(), c.to_be_bytes()].concat()).is_err() {
return;
}
}
}
});
}
let code = output(s, &mut io::stdout().lock(), &mut io::stderr().lock());
if let Some(s) = saved {
stty(&[s.trim()]);
}
match code? {
Some(c) => std::process::exit(c),
None => bail!("lost connection to {name} (VM stopped?)"),
}
}
fn send(w: &Mutex<UnixStream>, kind: u8, p: &[u8]) -> io::Result<()> {
w.lock().unwrap().write_all(&frame(kind, p))
}
/// Run a command as root in a running sandbox (no tty, empty stdin) and return its stdout, at most 64 KiB.
pub fn capture(sock: &Path, argv: &[&str]) -> Result<String> {
// uid 0, not "root": works even if the image has no /etc/passwd
let s = start(sock, argv, Some("0"))?;
s.set_read_timeout(Some(Duration::from_secs(2)))?;
const CAP: usize = 64 << 10;
let mut buf = vec![0u8; CAP];
let mut w = &mut buf[..]; // a slice writer errors once full: the guest can't make us buffer more
match output(s, &mut w, &mut io::sink())? {
Some(0) => {
let n = CAP - w.len();
Ok(String::from_utf8_lossy(&buf[..n]).into_owned())
}
c => bail!("guest command failed ({c:?})"),
}
}
/// Start a command without tty, with empty stdin; user None: the image user.
fn start(sock: &Path, argv: &[&str], user: Option<&str>) -> Result<UnixStream> {
let mut s = UnixStream::connect(sock)?;
s.write_all(format!("{}\n", json!({"argv": argv, "user": user, "tty": false})).as_bytes())?;
s.write_all(&frame(b'e', &[]))?;
Ok(s)
}
/// Run a command as the image user, all its output to our stderr. None: the connection ended first.
pub fn stream(sock: &Path, argv: &[&str]) -> Result<Option<i32>> {
Ok(output(start(sock, argv, None)?, &mut io::stderr(), &mut io::stderr())?)
}
/// Copy guest output frames to out/err until the exit status arrives (None: connection closed first).
fn output(s: UnixStream, out: &mut impl Write, err: &mut impl Write) -> io::Result<Option<i32>> {
let mut r = io::BufReader::new(s);
loop {
let mut h = [0u8; 5];
if r.read_exact(&mut h).is_err() {
return Ok(None);
}
let mut p = (&mut r).take(u32::from_be_bytes([h[1], h[2], h[3], h[4]]).into());
match h[0] {
b'o' => {
io::copy(&mut p, out)?;
out.flush()?;
}
b'r' => {
io::copy(&mut p, err)?;
}
b'x' => {
let mut c = [0u8; 4];
p.read_exact(&mut c)?;
return Ok(Some(i32::from_be_bytes(c)));
}
_ => {
io::copy(&mut p, &mut io::sink())?;
}
}
}
}
+119
View File
@@ -0,0 +1,119 @@
//! `wsbx forward NAME [HOSTPORT:]PORT...`: host 127.0.0.1:HOSTPORT -> exec.sock -> vsock -> guest helper ->
//! guest localhost:PORT. Framed like exec (see guest/exec.go), so closes and a vanished host are noticed.
use crate::exec::{connect, frame, sock};
use anyhow::{Context, Result, bail};
use serde_json::json;
use std::io::{self, BufReader, Read, Write};
use std::net::{Shutdown, TcpListener, TcpStream};
use std::path::Path;
use std::thread;
pub fn forward(h: &Path, name: &str, specs: Vec<String>) -> Result<()> {
let sock = sock(h, name)?;
let mut ls = Vec::new();
for spec in specs {
let (host, guest) = spec.split_once(':').unwrap_or((&spec, &spec));
let (Ok(host), Ok(guest)) = (host.parse::<u16>(), guest.parse::<u16>()) else {
bail!("bad port {spec:?} (PORT or HOSTPORT:PORT)");
};
if guest == 0 {
bail!("bad port {spec:?}");
}
// Loopback only: nothing on the LAN reaches the sandbox. [::1] is best effort.
let l = TcpListener::bind(("127.0.0.1", host)).with_context(|| format!("localhost:{host}"))?;
let host = l.local_addr()?.port(); // 0 picks a free one
if let Ok(l6) = TcpListener::bind(("::1", host)) {
ls.push((l6, guest));
}
ls.push((l, guest));
eprintln!("wsbx: localhost:{host} -> {name}:{guest}");
}
serve(&sock, name, ls);
Ok(())
}
/// Forward connections on each listener to its guest port, forever.
pub fn serve(sock: &Path, name: &str, ls: Vec<(TcpListener, u16)>) {
// ponytail: thread per connection, fine for dev servers; async if someone forwards thousands of sockets.
let threads: Vec<_> = ls
.into_iter()
.map(|(l, port)| {
let (sock, name) = (sock.to_path_buf(), name.to_string());
thread::spawn(move || {
for c in l.incoming().flatten() {
let (sock, name) = (sock.clone(), name.clone());
thread::spawn(move || {
if let Err(e) = conn(&sock, &name, c, port) {
eprintln!("wsbx: {name}:{port}: {e}");
}
});
}
})
})
.collect();
for t in threads {
let _ = t.join();
}
}
/// Whether something accepts connections on the guest's localhost:port.
pub fn listening(sock: &Path, port: u16) -> bool {
let Ok(mut s) = std::os::unix::net::UnixStream::connect(sock) else { return false };
let req = format!("{}\n", json!({"connect": port}));
let mut h = [0u8; 1];
// A failed dial answers with an 'r' (error) frame; a connected one with output, keepalive or EOF ('x').
s.write_all(req.as_bytes()).is_ok() && s.write_all(&frame(b'e', &[])).is_ok() && s.read_exact(&mut h).is_ok() && h[0] != b'r'
}
fn conn(sock: &Path, name: &str, c: TcpStream, port: u16) -> Result<()> {
let s = connect(sock, name)?;
let mut w = s.try_clone()?;
w.write_all(format!("{}\n", json!({"connect": port})).as_bytes())?;
let mut cr = c.try_clone()?;
let up = thread::spawn(move || {
let mut b = vec![0u8; 32 << 10];
loop {
match cr.read(&mut b) {
Ok(0) | Err(_) => break,
Ok(n) if w.write_all(&frame(b'd', &b[..n])).is_err() => return,
Ok(_) => {}
}
}
let _ = w.write_all(&frame(b'e', &[]));
});
let res = down(BufReader::new(s), &c);
// Clean EOF from the service: only our write side ends, the client may still be sending.
// Anything else (dial failed, VM gone, client gone): drop both, which also ends `up`.
let _ = c.shutdown(if matches!(res, Ok(true)) { Shutdown::Write } else { Shutdown::Both });
let _ = up.join();
res.map(|_| ())
}
/// Guest frames -> client, until 'x'. Ok(true): the service closed cleanly.
fn down(mut r: BufReader<std::os::unix::net::UnixStream>, mut c: &TcpStream) -> Result<bool> {
loop {
let mut h = [0u8; 5];
if r.read_exact(&mut h).is_err() {
bail!("lost connection (VM stopped?)");
}
let mut p = (&mut r).take(u32::from_be_bytes([h[1], h[2], h[3], h[4]]).into());
match h[0] {
b'o' => {
io::copy(&mut p, &mut c)?;
}
b'r' => {
let mut m = String::new();
p.read_to_string(&mut m)?;
bail!("{m}");
}
b'x' => {
let mut x = [0u8; 4];
p.read_exact(&mut x)?;
return Ok(x == [0; 4]);
}
_ => {
io::copy(&mut p, &mut io::sink())?;
}
}
}
}
+85
View File
@@ -0,0 +1,85 @@
//! Guest kernel: Kata Containers release kernel, downloaded and sha256-verified.
use crate::oci;
use anyhow::{Context, Result, anyhow, bail};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
const KATA_REPO: &str = "kata-containers/kata-containers";
/// Where Kata's release tarball keeps the guest kernel (VM-ready arm64 Image, all virtio built in).
const KATA_KERNEL: &str = "opt/kata/share/kata-containers/vmlinux.container";
pub fn installed_kernel(h: &Path) -> Option<String> {
let v: Value = serde_json::from_slice(&fs::read(h.join("kernel/source.json")).ok()?).ok()?;
v["version"].as_str().map(String::from)
}
/// Guest kernel from the latest (or pinned) Kata Containers release. Never re-downloads implicitly:
/// runs use whatever is installed; `wsbx kernel --update` moves to a newer release.
pub fn kernel(h: &Path, update: bool, version: Option<String>) -> Result<PathBuf> {
let k = h.join("kernel/vmlinux");
let installed = installed_kernel(h);
let pin = version.or_else(|| std::env::var("WSBX_KATA_VERSION").ok());
if k.exists() && !update && (pin.is_none() || pin == installed) {
return Ok(k);
}
if let Some(v) = &pin
&& (v.is_empty() || !v.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c)))
{
bail!("invalid Kata version {v:?}");
}
let api = match &pin {
Some(v) => format!("https://api.github.com/repos/{KATA_REPO}/releases/tags/{v}"),
None => format!("https://api.github.com/repos/{KATA_REPO}/releases/latest"),
};
let agent = oci::agent_for("api.github.com"); // public-only resolver, like registries (redirects to the CDN included)
let rel: Value = serde_json::from_reader(agent.get(&api).call().with_context(|| format!("GET {api}"))?.into_reader())?;
let tag = rel["tag_name"].as_str().ok_or_else(|| anyhow!("no tag in {api}"))?;
if k.exists() && installed.as_deref() == Some(tag) {
eprintln!("wsbx: kernel already at Kata {tag}");
return Ok(k);
}
let asset = rel["assets"]
.as_array()
.into_iter()
.flatten()
.find(|a| a["name"].as_str().is_some_and(|n| n.starts_with("kata-static-") && n.ends_with("-arm64.tar.zst")))
.ok_or_else(|| anyhow!("Kata {tag} has no kata-static arm64 asset"))?;
let url = asset["browser_download_url"].as_str().ok_or_else(|| anyhow!("asset without url"))?;
eprintln!(
"wsbx: fetching kernel from Kata Containers {tag} ({} MB, streamed; only the kernel is kept)",
asset["size"].as_u64().unwrap_or(0) / 1_000_000
);
let tmp = h.join("kernel.tmp");
let _ = fs::remove_dir_all(&tmp);
fs::create_dir_all(&tmp)?;
// Stream download -> sha256 + bsdtar (zstd); nothing but the kernel touches the disk.
let mut tar = Command::new("tar")
.args(["-x", "--zstd", "-f", "-", "-C"])
.arg(&tmp)
.arg(format!("{}*", KATA_KERNEL.trim_end_matches(".container")))
.stdin(Stdio::piped())
.spawn()?;
let mut w = oci::HashWriter(tar.stdin.take().unwrap(), Sha256::new());
std::io::copy(&mut agent.get(url).call()?.into_reader(), &mut w)?;
let oci::HashWriter(stdin, hasher) = w;
drop(stdin);
if !tar.wait()?.success() {
bail!("kernel extraction failed");
}
let got = format!("sha256:{}", hex::encode(hasher.finalize()));
match asset["digest"].as_str() {
Some(want) if want != got => bail!("kernel download checksum mismatch: want {want}, got {got}"),
Some(_) => {}
None => eprintln!("wsbx: warning: release publishes no digest; download not verified"),
}
let src = fs::canonicalize(tmp.join(KATA_KERNEL))?;
fs::create_dir_all(k.parent().unwrap())?;
fs::rename(src, &k)?;
let _ = fs::remove_file(h.join("kernel/SOURCE")); // pre-source.json format
fs::write(h.join("kernel/source.json"), serde_json::to_vec_pretty(&json!({"version": tag, "url": url, "sha256": got}))?)?;
fs::remove_dir_all(&tmp)?;
Ok(k)
}
+66 -683
View File
@@ -1,33 +1,27 @@
//! wsbx — Docker-Sandboxes-style microVMs on macOS (Virtualization.framework via vfkit).
mod agents;
mod code;
mod disk;
mod exec;
mod forward;
mod kernel;
mod oci;
mod sandbox;
mod stage;
mod tunnel;
mod util;
mod vm;
use anyhow::{Context, Result, anyhow, bail};
use anyhow::Result;
use clap::{Args, Parser, Subcommand};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use std::fs;
use std::io::{IsTerminal, Read};
use std::os::unix::fs::DirBuilderExt;
use std::net::{Shutdown, TcpStream};
use std::os::unix::net::{UnixListener, UnixStream};
use std::os::unix::fs::{MetadataExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
const KATA_REPO: &str = "kata-containers/kata-containers";
/// Where Kata's release tarball keeps the guest kernel (VM-ready arm64 Image, all virtio built in).
const KATA_KERNEL: &str = "opt/kata/share/kata-containers/vmlinux.container";
const INIT: &str = include_str!("init.sh");
/// Baked into each disk once; everything else comes fresh from the host share on every start.
const STUB_INIT: &str = "#!/.wsbx/busybox sh
/.wsbx/busybox mount -t virtiofs wsbx /.wsbx/host && exec /.wsbx/busybox sh /.wsbx/host/init
echo 'wsbx: cannot mount host share'; exec /.wsbx/busybox sh
";
const DHCP: &str = include_str!("dhcp.sh");
const FWD: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wsbx-fwd"));
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::process::Command;
use vm::Guest;
#[derive(Parser)]
#[command(name = "wsbx", about = "Launch coding agents or bare Linux in microVMs")]
#[command(name = "wsbx", version, about = "Launch coding agents or bare Linux in microVMs")]
struct Cli {
#[command(subcommand)]
cmd: Cmd,
@@ -69,9 +63,39 @@ enum Cmd {
#[arg(long)]
pi_auth: bool,
},
/// List sandboxes and whether they're running
/// Shell (default) or command in a running sandbox
#[command(visible_alias = "sh")]
Exec {
name: String,
/// Run as this user (default: the image's user, like the sandbox's main command)
#[arg(short, long)]
user: Option<String>,
#[arg(last = true)]
cmd: Vec<String>,
},
/// Expose ports of a running sandbox on the host's localhost (until Ctrl-C). Works for services
/// bound to the guest's localhost too. Note: browsers share localhost cookies across ports.
#[command(visible_alias = "fwd")]
Forward {
name: String,
/// PORT, or HOSTPORT:PORT (HOSTPORT 0: any free port)
#[arg(required = true)]
ports: Vec<String>,
},
/// Open VS Code in a running sandbox: in the browser (default), or desktop VS Code via Remote-SSH
Code {
name: String,
/// Desktop VS Code via Remote-SSH (adds an Include line to ~/.ssh/config once)
#[arg(long)]
desktop: bool,
},
/// List sandboxes with live stats (CPU, memory, network, block I/O, processes)
#[command(visible_alias = "ps")]
Ls,
Ls {
/// Keep refreshing every second, like `docker stats` (Ctrl-C to stop)
#[arg(short, long)]
follow: bool,
},
/// Remove stopped sandboxes
Rm {
#[arg(required = true)]
@@ -82,6 +106,8 @@ enum Cmd {
},
/// Delete cached images that no sandbox uses
Gc,
/// Print the wsbx version
Version,
}
#[derive(Args)]
@@ -126,21 +152,10 @@ struct Opts {
#[arg(long)]
kernel: Option<PathBuf>,
/// Tunnel the host's localhost:PORT to localhost:PORT in the VM (vsock, no network exposure)
#[arg(short = 'p', long = "host-port")]
#[arg(short = 'p', long = "host-port", value_parser = clap::value_parser!(u16).range(1..))]
host_ports: Vec<u16>,
}
/// Agent-specific guest preparation.
#[derive(Default)]
struct Guest {
/// (host path, name under /.wsbx/host/stage) copied before each start, symlinks resolved
stage: Vec<(PathBuf, String)>,
/// Symlinks inside staged trees may only resolve into these (canonical) roots
roots: Vec<PathBuf>,
/// Shell run as root after HOME/env are set, before dropping to the image user
setup: String,
}
fn home() -> Result<PathBuf> {
let h = std::env::var_os("WSBX_HOME")
.map(PathBuf::from)
@@ -149,172 +164,30 @@ fn home() -> Result<PathBuf> {
fs::set_permissions(&h, fs::Permissions::from_mode(0o700))?; // holds run scripts with forwarded env
Ok(h)
}
fn main() -> Result<()> {
let h = home()?;
match Cli::parse().cmd {
Cmd::Kernel { update, version } => {
let k = kernel(&h, update, version)?;
println!("{} (Kata {})", k.display(), installed_kernel(&h).unwrap_or("unknown".into()));
let k = kernel::kernel(&h, update, version)?;
println!("{} (Kata {})", k.display(), kernel::installed_kernel(&h).unwrap_or("unknown".into()));
Ok(())
}
Cmd::Pull { image } => oci::pull(&h, &image, true).map(|i| println!("{} {}", image, i.digest)),
Cmd::Run { o, image, cmd } => run(&h, o, &image, cmd, &[], Guest::default()),
Cmd::Claude(a) => agent(&h, a, "claude-code", "claude", vec![], &["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"], Guest::default()),
Cmd::Codex(a) => agent(&h, a, "codex", "codex", vec![], &["OPENAI_API_KEY"], Guest::default()),
Cmd::Gemini(a) => agent(&h, a, "gemini", "gemini", vec![], &["GEMINI_API_KEY", "GOOGLE_API_KEY"], Guest::default()),
Cmd::Pi { a, pi_auth } => pi(&h, a, pi_auth),
Cmd::Ls => ls(&h),
Cmd::Gc => {
let used: Vec<String> = fs::read_dir(h.join("sandboxes"))
.into_iter()
.flatten()
.flatten()
.filter_map(|e| serde_json::from_slice::<Value>(&fs::read(e.path().join("meta.json")).ok()?).ok())
.filter_map(|m| m["image"].as_str().map(String::from))
.collect();
println!("freed {} MB", oci::gc(&h, &used)? / 1_000_000);
Ok(())
}
Cmd::Rm { names, disk } => {
for n in names {
if n.is_empty() || sanitize(&n) != n {
bail!("invalid sandbox name {n:?}");
}
let d = h.join("sandboxes").join(&n);
if !d.join("meta.json").exists() {
bail!("no sandbox {n}");
}
// Held across the delete so the sandbox can't be started meanwhile.
let _lock = lock_stopped(&d).with_context(|| format!("sandbox {n} is running"))?;
if disk {
match fs::remove_file(d.join("disk.img")) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => println!("removed disk of {n}"),
}
} else {
fs::remove_dir_all(d)?;
println!("removed {n}");
}
}
Ok(())
}
Cmd::Run { o, image, cmd } => vm::run(&h, o, &image, cmd, &[], Guest::default()),
Cmd::Claude(a) => agents::agent(&h, a, "claude-code", "claude", vec![], &["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"], Guest::default()),
Cmd::Codex(a) => agents::agent(&h, a, "codex", "codex", vec![], &["OPENAI_API_KEY"], Guest::default()),
Cmd::Gemini(a) => agents::agent(&h, a, "gemini", "gemini", vec![], &["GEMINI_API_KEY", "GOOGLE_API_KEY"], Guest::default()),
Cmd::Pi { a, pi_auth } => agents::pi(&h, a, pi_auth),
Cmd::Exec { name, user, cmd } => exec::exec(&h, &name, user, cmd),
Cmd::Forward { name, ports } => forward::forward(&h, &name, ports),
Cmd::Code { name, desktop } => code::code(&h, &name, desktop),
Cmd::Ls { follow } => sandbox::ls(&h, follow),
Cmd::Gc => sandbox::gc(&h),
Cmd::Rm { names, disk } => sandbox::rm(&h, names, disk),
Cmd::Version => Ok(println!("wsbx {}", env!("CARGO_PKG_VERSION"))),
}
}
/// Fallback when pi isn't installed on the host (otherwise the host's package + version are mirrored).
const PI_PKG: &str = "@earendil-works/pi-coding-agent";
/// User-level pi config worth sharing. Never npm/, bin/ (macOS binaries), sessions/, caches.
const PI_CONFIG: &[&str] = &[
"settings.json", "keybindings.json", "models.json", "mcp.json", "AGENTS.override.md", "AGENTS.md", "AGENTS.MD",
"CLAUDE.md", "CLAUDE.MD", "SYSTEM.md", "APPEND_SYSTEM.md", "extensions", "skills", "prompts", "themes",
];
// No official Docker template for pi: the generic `shell` template has node + npm; pi is installed
// (pinned to the host's version) onto the persistent sandbox disk.
fn pi(h: &Path, mut a: Agent, auth: bool) -> Result<()> {
let user_home = PathBuf::from(std::env::var_os("HOME").expect("HOME"));
let agent_dir = std::env::var_os("PI_CODING_AGENT_DIR").map(PathBuf::from).unwrap_or_else(|| user_home.join(".pi/agent"));
let agents_skills = user_home.join(".agents/skills"); // Agent Skills location pi also reads
let mut items = PI_CONFIG.to_vec();
if auth {
items.push("auth.json");
}
let mut stage: Vec<(PathBuf, String)> = items.iter().map(|i| (agent_dir.join(i), format!("pi/{i}"))).collect();
stage.push((agents_skills.clone(), "agents-skills".into()));
stage.retain(|(p, _)| p.exists());
let roots = [&agent_dir, &agents_skills].iter().filter_map(|p| fs::canonicalize(p).ok()).collect();
if let Ok(m) = fs::read(agent_dir.join("models.json")) {
let m: Value = serde_json::from_slice(&m).context("models.json")?;
for p in m["providers"].as_object().into_iter().flat_map(|p| p.values()) {
if let Some(port) = p["baseUrl"].as_str().and_then(local_port) {
a.o.host_ports.push(port);
}
}
}
let sh = match host_npm_pkg("pi") {
Some((pkg, v)) => format!(
"[ \"$(pi --version 2>/dev/null)\" = {v} ] || npm install -g --force {}@{v} >&2 || exit 1; exec pi \"$@\"",
sq(&pkg)
),
None => format!("command -v pi >/dev/null || npm install -g --force {PI_PKG} >&2 || exit 1; exec pi \"$@\""),
};
// Link each staged item into the guest's own ~/.pi/agent (which keeps its linux npm/, bin/, sessions/).
let setup = r#"PA="$HOME/.pi/agent"; S=/.wsbx/host/stage; $B mkdir -p "$PA" "$HOME/.agents"
for l in "$PA"/* "$PA"/.[!.]* "$HOME/.agents/skills"; do [ -L "$l" ] && $B rm -f "$l"; done
for f in "$S"/pi/*; do [ -e "$f" ] && $B rm -rf "$PA/${f##*/}" && $B ln -s "$f" "$PA/${f##*/}"; done
[ -e "$S/agents-skills" ] && $B rm -rf "$HOME/.agents/skills" && $B ln -s "$S/agents-skills" "$HOME/.agents/skills"
[ -z "$WSBX_USER" ] || $B chown "$WSBX_USER" "$HOME/.pi" "$PA" "$HOME/.agents"
"#;
let cmd = ["bash", "-c", &sh, "pi"].map(String::from).to_vec();
let keys = ["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "GEMINI_API_KEY", "OPENROUTER_API_KEY", "GROQ_API_KEY", "XAI_API_KEY", "MISTRAL_API_KEY", "CEREBRAS_API_KEY", "ZAI_API_KEY"];
agent(h, a, "shell", "pi", cmd, &keys, Guest { stage, roots, setup: setup.into() })
}
/// npm package (name, version) behind a host command: follow the bin symlink up to its package.json.
fn host_npm_pkg(cmd: &str) -> Option<(String, String)> {
let bin = std::env::split_paths(&std::env::var_os("PATH")?).map(|d| d.join(cmd)).find(|p| p.is_file())?;
let real = fs::canonicalize(bin).ok()?;
let pj = real.ancestors().map(|d| d.join("package.json")).find(|p| p.is_file())?;
let v: Value = serde_json::from_slice(&fs::read(pj).ok()?).ok()?;
let (name, ver) = (v["name"].as_str()?, v["version"].as_str()?);
ver.chars().all(|c| c.is_ascii_alphanumeric() || ".-+".contains(c)).then(|| (name.into(), ver.into()))
}
/// Port of a URL pointing at the host's loopback, e.g. "http://127.0.0.1:8000/v1" -> 8000.
fn local_port(url: &str) -> Option<u16> {
let (scheme, rest) = url.split_once("://")?;
let auth = rest.split('/').next()?;
let (host, port) = match auth.rsplit_once(':') {
Some((h, p)) if !p.ends_with(']') => (h, p.parse().ok()?),
_ => (auth, if scheme == "https" { 443 } else { 80 }),
};
["127.0.0.1", "localhost", "[::1]", "0.0.0.0"].contains(&host).then_some(port)
}
fn agent(h: &Path, a: Agent, template: &str, short: &str, cmd: Vec<String>, keys: &[&str], g: Guest) -> Result<()> {
let mut o = a.o;
if o.workspace.is_none() {
o.workspace = Some(std::env::current_dir()?);
}
if o.name.is_none() {
o.name = Some(format!("{short}-{}", base(o.workspace.as_ref().unwrap())));
}
let set: Vec<String> = keys.iter().filter(|k| std::env::var_os(k).is_some()).map(|k| k.to_string()).collect();
if a.keys {
o.env.extend(set);
} else if !set.is_empty() {
eprintln!("wsbx: not forwarding {} (use --keys; code in the VM, e.g. a prompt-injected agent, could read it)", set.join(", "));
}
// Docker's sandbox templates: Cmd is the agent (e.g. `claude --dangerously-skip-permissions`) unless overridden
run(h, o, &format!("docker/sandbox-templates:{template}"), cmd, &a.args, g)
}
fn base(p: &Path) -> String {
p.file_name().map(|s| s.to_string_lossy().into_owned()).unwrap_or_else(|| "root".into())
}
fn sanitize(s: &str) -> String {
s.chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' { c.to_ascii_lowercase() } else { '-' }).collect()
}
fn sq(s: &str) -> String {
format!("'{}'", s.replace('\'', r"'\''"))
}
/// Find a tool on PATH, else in its Homebrew keg (keg-only formulae like e2fsprogs aren't linked).
fn need(tool: &str, formula: &str) -> Result<PathBuf> {
let on_path = std::env::var_os("PATH").map(|p| std::env::split_paths(&p).collect::<Vec<_>>()).unwrap_or_default();
let find = |dirs: &[PathBuf]| dirs.iter().map(|d| d.join(tool)).find(|p| p.is_file());
find(&on_path)
.or_else(|| {
let o = Command::new("brew").args(["--prefix", formula]).output().ok()?;
let keg = PathBuf::from(String::from_utf8_lossy(&o.stdout).trim());
find(&[keg.join("sbin"), keg.join("bin")])
})
.ok_or_else(|| anyhow!("{tool} not found — install it with: brew install {formula}"))
}
fn default_cpus() -> u32 {
std::thread::available_parallelism().map(|n| n.get() as u32 / 2).unwrap_or(2).max(1)
}
@@ -324,493 +197,3 @@ fn default_memory() -> u32 {
let bytes: u64 = host.and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok()).unwrap_or(8 << 30);
((bytes >> 20) / 4).clamp(1024, 8192) as u32
}
fn installed_kernel(h: &Path) -> Option<String> {
let v: Value = serde_json::from_slice(&fs::read(h.join("kernel/source.json")).ok()?).ok()?;
v["version"].as_str().map(String::from)
}
/// Guest kernel from the latest (or pinned) Kata Containers release. Never re-downloads implicitly:
/// runs use whatever is installed; `wsbx kernel --update` moves to a newer release.
fn kernel(h: &Path, update: bool, version: Option<String>) -> Result<PathBuf> {
let k = h.join("kernel/vmlinux");
let installed = installed_kernel(h);
let pin = version.or_else(|| std::env::var("WSBX_KATA_VERSION").ok());
if k.exists() && !update && (pin.is_none() || pin == installed) {
return Ok(k);
}
if let Some(v) = &pin
&& (v.is_empty() || !v.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c)))
{
bail!("invalid Kata version {v:?}");
}
let api = match &pin {
Some(v) => format!("https://api.github.com/repos/{KATA_REPO}/releases/tags/{v}"),
None => format!("https://api.github.com/repos/{KATA_REPO}/releases/latest"),
};
let rel: Value = serde_json::from_reader(ureq::get(&api).call().with_context(|| format!("GET {api}"))?.into_reader())?;
let tag = rel["tag_name"].as_str().ok_or_else(|| anyhow!("no tag in {api}"))?;
if k.exists() && installed.as_deref() == Some(tag) {
eprintln!("wsbx: kernel already at Kata {tag}");
return Ok(k);
}
let asset = rel["assets"]
.as_array()
.into_iter()
.flatten()
.find(|a| a["name"].as_str().is_some_and(|n| n.starts_with("kata-static-") && n.ends_with("-arm64.tar.zst")))
.ok_or_else(|| anyhow!("Kata {tag} has no kata-static arm64 asset"))?;
let url = asset["browser_download_url"].as_str().ok_or_else(|| anyhow!("asset without url"))?;
eprintln!(
"wsbx: fetching kernel from Kata Containers {tag} ({} MB, streamed; only the kernel is kept)",
asset["size"].as_u64().unwrap_or(0) / 1_000_000
);
let tmp = h.join("kernel.tmp");
let _ = fs::remove_dir_all(&tmp);
fs::create_dir_all(&tmp)?;
// Stream download -> sha256 + bsdtar (zstd); nothing but the kernel touches the disk.
let mut tar = Command::new("tar")
.args(["-x", "--zstd", "-f", "-", "-C"])
.arg(&tmp)
.arg(format!("{}*", KATA_KERNEL.trim_end_matches(".container")))
.stdin(Stdio::piped())
.spawn()?;
let mut w = oci::HashWriter(tar.stdin.take().unwrap(), Sha256::new());
std::io::copy(&mut ureq::get(url).call()?.into_reader(), &mut w)?;
let oci::HashWriter(stdin, hasher) = w;
drop(stdin);
if !tar.wait()?.success() {
bail!("kernel extraction failed");
}
let got = format!("sha256:{}", hex::encode(hasher.finalize()));
match asset["digest"].as_str() {
Some(want) if want != got => bail!("kernel download checksum mismatch: want {want}, got {got}"),
Some(_) => {}
None => eprintln!("wsbx: warning: release publishes no digest; download not verified"),
}
let src = fs::canonicalize(tmp.join(KATA_KERNEL))?;
fs::create_dir_all(k.parent().unwrap())?;
fs::rename(src, &k)?;
let _ = fs::remove_file(h.join("kernel/SOURCE")); // pre-source.json format
fs::write(h.join("kernel/source.json"), serde_json::to_vec_pretty(&json!({"version": tag, "url": url, "sha256": got}))?)?;
fs::remove_dir_all(&tmp)?;
Ok(k)
}
/// Static busybox from the official busybox image; runs PID 1 + helpers in the guest.
fn busybox(h: &Path) -> Result<PathBuf> {
let p = h.join("busybox");
if p.exists() {
return Ok(p);
}
let img = oci::pull(h, "busybox:musl", false)?;
let tmp = h.join("busybox.tmp");
fs::create_dir_all(&tmp)?;
let ok = Command::new("tar").arg("-xf").arg(&img.rootfs).arg("-C").arg(&tmp).arg("bin").status()?.success();
if !ok {
bail!("failed to extract busybox");
}
// Must be a real file: a symlink would make us copy an arbitrary host file into guest disks.
if !fs::symlink_metadata(tmp.join("bin/busybox"))?.is_file() {
bail!("busybox image: bin/busybox is not a regular file");
}
fs::rename(tmp.join("bin/busybox"), &p)?;
fs::remove_dir_all(tmp)?;
Ok(p)
}
fn create_disk(h: &Path, disk: &Path, gib: u32) -> Result<()> {
let mke2fs = need("mke2fs", "e2fsprogs")?;
let seed = disk.with_extension("seed");
let _ = fs::remove_dir_all(&seed);
for d in [".wsbx/dev", ".wsbx/host"] {
fs::create_dir_all(seed.join(d))?;
}
fs::copy(busybox(h)?, seed.join(".wsbx/busybox"))?;
fs::write(seed.join(".wsbx/init"), STUB_INIT)?;
for f in ["busybox", "init"] {
fs::set_permissions(seed.join(".wsbx").join(f), fs::Permissions::from_mode(0o755))?;
}
let out = Command::new(mke2fs)
.args(["-q", "-F", "-t", "ext4", "-E", "root_owner=0:0", "-d"])
.arg(&seed)
.arg(disk)
.arg(format!("{gib}G"))
.output()?;
fs::remove_dir_all(&seed)?;
if !out.status.success() {
bail!("mke2fs failed: {}", String::from_utf8_lossy(&out.stderr));
}
Ok(())
}
fn strs(v: &Value) -> Vec<String> {
v.as_array().map(|a| a.iter().filter_map(|s| s.as_str().map(String::from)).collect()).unwrap_or_default()
}
fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g: Guest) -> Result<()> {
let vfkit = need("vfkit", "vfkit")?;
let kernel = match &o.kernel {
Some(k) => k.clone(),
None => kernel(h, false, None)?,
};
let ws = o.workspace.as_ref().map(fs::canonicalize).transpose().context("workspace")?;
vf(h)?; // everything vfkit gets lives under h, plus kernel + workspace: refuse bad paths before creating anything
vf(&kernel)?;
if let Some(ws) = &ws {
vf(ws)?;
let hc = fs::canonicalize(h)?;
if ws.starts_with(&hc) || hc.starts_with(ws) {
bail!("workspace {} overlaps the wsbx state dir {} (disks, kernel, control files)", ws.display(), hc.display());
}
}
let img = oci::pull(h, image, o.pull)?;
let short = image.rsplit('/').next().unwrap().split([':', '@']).next().unwrap();
let name = sanitize(&o.name.clone().unwrap_or_else(|| format!("{short}-{}", ws.as_deref().map(base).unwrap_or("vm".into()))));
let dir = h.join("sandboxes").join(&name);
fs::create_dir_all(&dir)?;
let _lock = lock_stopped(&dir).map_err(|_| anyhow!("sandbox {name} is already running"))?;
// The control share is writable by the guest (virtio-fs has no host-enforced read-only).
// Recreate it empty on every start -- while this sandbox's VM is stopped and we hold the lock --
// so nothing a previous guest left there (e.g. `run.sh -> ~/.zshrc`) is ever written through.
// Persistent state lives in meta.json, which the guest cannot reach.
let share = dir.join("share");
match fs::remove_dir_all(&share) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => {}
}
std::fs::DirBuilder::new().mode(0o700).create(&share)?;
let disk = dir.join("disk.img");
let meta_path = dir.join("meta.json");
if o.fresh {
let _ = fs::remove_file(&disk);
}
let mut meta: Value = fs::read(&meta_path).ok().and_then(|m| serde_json::from_slice(&m).ok()).unwrap_or(Value::Null);
if disk.exists() && meta["digest"] != img.digest.as_str() {
eprintln!("wsbx: note: {name} was created from an older image; use --fresh to rebuild");
}
if !disk.exists() {
eprintln!("wsbx: creating sandbox {name}");
create_disk(h, &disk, o.disk)?;
meta = json!({"image": image, "digest": img.digest, "workspace": ws, "created": now(), "provisioned": false});
fs::write(&meta_path, serde_json::to_vec_pretty(&meta)?)?;
}
// Build the guest-side run script (executed as root by init, then drops to the image user).
let c = &img.config;
let mut argv = strs(&c["Entrypoint"]);
argv.extend(if cmd.is_empty() { strs(&c["Cmd"]) } else { cmd });
argv.extend(extra.iter().cloned());
if argv.is_empty() {
argv.push("/bin/sh".into());
}
let user = c["User"].as_str().unwrap_or("").to_string();
let wd = ws
.as_ref()
.map(|p| p.to_string_lossy().into_owned())
.or_else(|| c["WorkingDir"].as_str().filter(|s| !s.is_empty()).map(String::from))
.unwrap_or("/".into());
let mut s = String::from("B=/.wsbx/busybox\n");
s += &format!("$B date -s @{} >/dev/null\n", now());
s += &format!("$B hostname {name}; echo {name} > /etc/hostname; $B grep -q ' {name}$' /etc/hosts 2>/dev/null || echo '127.0.1.1 {name}' >> /etc/hosts\n");
if let Some((rows @ 1.., cols @ 1..)) = term_size() {
s += &format!("$B stty rows {rows} cols {cols}\n");
}
if let Some(ws) = &ws {
let w = sq(&ws.to_string_lossy());
s += &format!("$B mkdir -p {w} && $B mount -t virtiofs ws {w}\n");
}
let u = user.split(':').next().unwrap_or("");
s += &format!("export HOME=$($B awk -F: -v u={} '$1==u||$3==u{{print $6;exit}}' /etc/passwd)\n", sq(if u.is_empty() { "root" } else { u }));
s += "[ -n \"$HOME\" ] || export HOME=/root\n";
s += &format!("export TERM={} TINI_SUBREAPER=1\n", sq(&std::env::var("TERM").unwrap_or("xterm-256color".into())));
let mut env = strs(&c["Env"]);
for e in &o.env {
match e.split_once('=') {
Some(_) => env.push(e.clone()),
None => env.push(format!("{e}={}", std::env::var(e).with_context(|| format!("env {e} not set on host"))?)),
}
}
for e in env {
if let Some((k, v)) = e.split_once('=') {
s += &format!("export {k}={}\n", sq(v));
}
}
s += &format!("export WSBX_USER={}\n", sq(&user));
let stage = share.join("stage");
fs::create_dir(&stage)?;
let (mut budget, mut seen) = (STAGE_LIMIT, std::collections::HashSet::new());
for (src, to) in &g.stage {
let dst = stage.join(to);
fs::create_dir_all(dst.parent().unwrap())?;
copy_tree(src, &dst, &g.roots, &mut budget, &mut seen)?;
}
s += &g.setup;
// Host tunnel: guest wsbx-fwd listens on localhost:PORT -> vsock -> vfkit -> unix socket -> host localhost:PORT
let mut ports = o.host_ports.clone();
ports.sort();
ports.dedup();
let mut vsock = Vec::new();
if !ports.is_empty() {
let fwd = share.join("fwd");
fs::write(&fwd, FWD)?;
fs::set_permissions(&fwd, fs::Permissions::from_mode(0o755))?;
let mut specs = Vec::new();
for (i, &p) in ports.iter().enumerate() {
let vport = 10000 + i as u32;
let sock = dir.join(format!("p{p}.sock"));
tunnel(&sock, p)?;
vsock.push(format!("virtio-vsock,port={vport},socketURL={},listen", vf(&sock)?));
specs.push(format!("{p}={vport}"));
}
s += "$B mkfifo /run/wsbx-fwd\n";
s += &format!("/.wsbx/host/fwd {} >/run/wsbx-fwd 2>/.wsbx/host/fwd.log &\n", specs.join(" "));
s += "read _ </run/wsbx-fwd\n";
eprintln!("wsbx: host localhost:{{{}}} tunneled into the VM", ports.iter().map(u16::to_string).collect::<Vec<_>>().join(","));
}
s += &format!("cd {} 2>/dev/null || cd /\n", sq(&wd));
let argv: Vec<String> = argv.iter().map(|a| sq(a)).collect();
if u.is_empty() || u == "root" || u == "0" {
s += &format!("exec {}\n", argv.join(" "));
} else {
s += &format!("exec $B chpst -u {} {}\n", sq(&user), argv.join(" "));
}
fs::write(share.join("run.sh"), s)?;
for (f, body) in [("init", INIT), ("dhcp", DHCP)] {
fs::write(share.join(f), body)?;
fs::set_permissions(share.join(f), fs::Permissions::from_mode(0o755))?;
}
let _ = fs::remove_file(share.join("exit"));
let initrd = h.join("empty.initrd"); // vfkit insists on an initrd; an empty one is fine
if !initrd.exists() {
fs::write(&initrd, "")?;
}
let mac = {
let x = name.bytes().fold(0xcbf29ce484222325u64, |a, b| (a ^ b as u64).wrapping_mul(0x100000001b3));
format!("02:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", x as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8, (x >> 32) as u8)
};
// panic=1 reboots, which VZ turns into a restart; init powers off on any second boot (see init.sh).
let cmdline = "console=hvc0 root=/dev/vda rw rootfstype=ext4 init=/.wsbx/init quiet panic=1";
let tty = std::io::stdin().is_terminal();
let mut v = Command::new(vfkit);
v.args(["--cpus", &o.cpus.to_string(), "--memory", &o.memory.to_string(), "--log-level", "error"])
.arg("--bootloader")
.arg(format!("linux,kernel={},initrd={},cmdline=\"{cmdline}\"", vf(&kernel)?, vf(&initrd)?))
.arg("--device")
.arg(format!("virtio-blk,path={}", vf(&disk)?))
.arg("--device")
.arg(format!("virtio-net,nat,mac={mac}"))
.arg("--device")
.arg(format!("virtio-fs,sharedDir={},mountTag=wsbx", vf(&share)?))
.args(["--device", "virtio-rng", "--device"])
// vfkit's stdio console needs a TTY; otherwise stream output only (no stdin)
.arg(if tty { "virtio-serial,stdio".into() } else { format!("virtio-serial,logFilePath={}", vf(&out_path())?) })
.stderr(fs::File::create(dir.join("vfkit.log"))?);
if let Some(ws) = &ws {
v.arg("--device").arg(format!("virtio-fs,sharedDir={},mountTag=ws", vf(ws)?));
}
// Image tar is only needed until the guest has unpacked it; read-only so sandboxes can share it.
let provisioned = meta["provisioned"].as_bool().unwrap_or(false);
if !provisioned {
v.arg("--device").arg(format!("virtio-blk,path={},readonly", vf(&img.rootfs)?));
}
for d in vsock {
v.arg("--device").arg(d);
}
let saved = stty(&["-g"]);
if saved.is_some() {
stty(&["raw", "-echo"]);
}
let status = v.stdin(Stdio::inherit()).status();
if let Some(s) = saved {
stty(&[s.trim()]);
}
let status = status?;
// The VM is gone now, so the guest can't race these reads; still never follow its symlinks.
let code = guest_output(&share, "exit").and_then(|s| s.trim().parse().ok());
if !provisioned && guest_output(&share, "provisioned").is_some() && meta.is_object() {
meta["provisioned"] = json!(true);
fs::write(&meta_path, serde_json::to_vec_pretty(&meta)?)?;
}
let _ = fs::remove_dir_all(&share); // run.sh contains forwarded env
match code {
Some(c) => std::process::exit(c),
None if !status.success() => bail!("vm failed, see {}", dir.join("vfkit.log").display()),
None => bail!("VM stopped without an exit status (kernel panic, reboot or poweroff inside the VM)"),
}
}
/// Small result file written by the guest; regular files only (no symlinks), size-capped.
fn guest_output(share: &Path, name: &str) -> Option<String> {
let p = share.join(name);
fs::symlink_metadata(&p).ok().filter(|m| m.is_file())?;
let mut s = String::new();
fs::File::open(p).ok()?.take(64).read_to_string(&mut s).ok()?;
Some(s)
}
/// vfkit splits device options on ',' and the bootloader cmdline is quoted: a path containing
/// those (e.g. a workspace named `repo,sharedDir=/Users/me`) would inject options. Refuse them.
fn vf(p: &Path) -> Result<String> {
let s = p.to_str().ok_or_else(|| anyhow!("non-UTF-8 path {}", p.display()))?;
if s.chars().any(|c| c == ',' || c == '"' || c.is_control()) {
bail!("unsupported character in path {s:?} (',', '\"' and control characters would inject vfkit options)");
}
Ok(s.to_string())
}
/// Where our stdout points (vfkit only accepts a path for non-TTY console output).
fn out_path() -> PathBuf {
fs::read_link("/dev/fd/1").unwrap_or("/dev/stdout".into())
}
/// Host end of a tunnel: vfkit dials this unix socket for each guest vsock connection.
fn tunnel(sock: &Path, port: u16) -> Result<()> {
if sock.as_os_str().len() > 100 {
bail!("socket path too long: {} (set WSBX_HOME to something shorter)", sock.display());
}
let _ = fs::remove_file(sock);
let l = UnixListener::bind(sock)?;
std::thread::spawn(move || {
for u in l.incoming().flatten() {
std::thread::spawn(move || {
if let Ok(t) = TcpStream::connect(("localhost", port)) {
pipe(u, t);
}
});
}
});
Ok(())
}
fn pipe(mut u: UnixStream, mut t: TcpStream) {
let (Ok(mut u2), Ok(mut t2)) = (u.try_clone(), t.try_clone()) else { return };
let up = std::thread::spawn(move || {
let _ = std::io::copy(&mut u2, &mut t2);
let _ = t2.shutdown(Shutdown::Write);
});
let _ = std::io::copy(&mut t, &mut u);
let _ = u.shutdown(Shutdown::Write);
let _ = up.join();
}
const STAGE_LIMIT: u64 = 256 << 20;
/// Copy config into the stage, resolving symlinks (they'd dangle in the VM) but only when they
/// resolve inside `roots`; no cycles, regular files and dirs only (a FIFO would hang fs::copy).
fn copy_tree(src: &Path, dst: &Path, roots: &[PathBuf], budget: &mut u64, seen: &mut std::collections::HashSet<PathBuf>) -> Result<()> {
let Ok(real) = fs::canonicalize(src) else { return Ok(()) }; // dangling symlink
if !roots.iter().any(|r| real.starts_with(r)) {
eprintln!("wsbx: not sharing {} -> {} (outside the shared config)", src.display(), real.display());
return Ok(());
}
let m = fs::metadata(&real)?;
if m.is_dir() {
if !seen.insert(real.clone()) {
return Ok(()); // symlink cycle or already copied
}
fs::create_dir_all(dst)?;
for e in fs::read_dir(&real)? {
let e = e?;
copy_tree(&e.path(), &dst.join(e.file_name()), roots, budget, seen)?;
}
} else if m.is_file() {
*budget = budget.checked_sub(m.len()).ok_or_else(|| anyhow!("shared config exceeds {} MB", STAGE_LIMIT >> 20))?;
fs::copy(&real, dst)?;
}
Ok(())
}
fn stty(args: &[&str]) -> Option<String> {
let out = Command::new("stty").args(args).stdin(Stdio::inherit()).stderr(Stdio::null()).output().ok()?;
out.status.success().then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}
fn term_size() -> Option<(u32, u32)> {
let s = stty(&["size"])?;
let mut it = s.split_whitespace().map(|x| x.parse().ok());
Some((it.next()??, it.next()??))
}
fn now() -> u64 {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()
}
/// The lock `run` holds for the VM's lifetime; Err if the sandbox is running.
fn lock_stopped(dir: &Path) -> Result<fs::File> {
let f = fs::File::create(dir.join("lock"))?;
f.try_lock().map_err(|_| anyhow!("locked by a running VM"))?;
Ok(f)
}
fn ls(h: &Path) -> Result<()> {
println!("{:<28} {:<8} {:<40} {:>8} WORKSPACE", "NAME", "STATUS", "IMAGE", "DISK");
let Ok(rd) = fs::read_dir(h.join("sandboxes")) else { return Ok(()) };
let mut rows: Vec<_> = rd.flatten().collect();
rows.sort_by_key(|e| e.file_name());
for e in rows {
let Ok(m) = fs::read(e.path().join("meta.json")) else { continue };
let m: Value = serde_json::from_slice(&m)?;
let used = fs::metadata(e.path().join("disk.img")).map(|m| m.blocks() * 512).unwrap_or(0);
let status = if lock_stopped(&e.path()).is_err() { "running" } else { "stopped" };
println!(
"{:<28} {:<8} {:<40} {:>6}MB {}",
e.file_name().to_string_lossy(),
status,
m["image"].as_str().unwrap_or("?"),
used / 1_000_000,
m["workspace"].as_str().unwrap_or("-")
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use std::path::Path;
#[test]
fn vfkit_injection_rejected() {
assert!(super::vf(Path::new("/Users/me/repo")).is_ok());
for bad in ["/shared/repo/,sharedDir=/Users/me", "/a\"b", "/a\nb"] {
assert!(super::vf(Path::new(bad)).is_err(), "{bad}");
}
}
#[test]
fn copy_tree_contains_symlinks() {
use std::os::unix::fs::symlink;
let t = std::env::temp_dir().join(format!("wsbx-ct-{}", std::process::id()));
let (root, out, secret) = (t.join("root"), t.join("out"), t.join("secret"));
std::fs::create_dir_all(root.join("skills/a")).unwrap();
std::fs::create_dir_all(&secret).unwrap();
std::fs::write(root.join("skills/a/SKILL.md"), "ok").unwrap();
std::fs::write(secret.join("id_rsa"), "key").unwrap();
symlink(&secret, root.join("skills/leak")).unwrap(); // escapes the root
symlink(root.join("skills"), root.join("skills/a/loop")).unwrap(); // cycle
let roots = vec![std::fs::canonicalize(&root).unwrap()];
let (mut budget, mut seen) = (super::STAGE_LIMIT, Default::default());
super::copy_tree(&root.join("skills"), &out, &roots, &mut budget, &mut seen).unwrap();
assert_eq!(std::fs::read_to_string(out.join("a/SKILL.md")).unwrap(), "ok");
assert!(!out.join("leak").exists());
std::fs::remove_dir_all(&t).unwrap();
}
#[test]
fn local_port() {
use super::local_port as lp;
assert_eq!(lp("http://127.0.0.1:8000/v1"), Some(8000));
assert_eq!(lp("http://localhost:11434"), Some(11434));
assert_eq!(lp("http://[::1]:1234/v1"), Some(1234));
assert_eq!(lp("http://localhost/v1"), Some(80));
assert_eq!(lp("http://192.168.1.169:8080/v1"), None);
assert_eq!(lp("https://api.openai.com/v1"), None);
}
}
+60 -17
View File
@@ -6,7 +6,7 @@ use sha2::{Digest, Sha256};
use std::collections::HashSet;
use std::fs::{self, File};
use std::io::{BufReader, Read, Write};
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs};
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
@@ -60,23 +60,40 @@ pub fn is_public(ip: IpAddr) -> bool {
|| o[0] == 0
|| (o[0] == 100 && o[1] & 0xc0 == 64)) // CGNAT 100.64/10
}
IpAddr::V6(v) => match v.to_ipv4_mapped() {
Some(v4) => is_public(v4.into()),
None => !(v.is_loopback() || v.is_unspecified() || v.is_multicast() || v.is_unique_local() || v.is_unicast_link_local()),
},
IpAddr::V6(v) => {
let v4 = |hi: u16, lo: u16| is_public(Ipv4Addr::from((hi as u32) << 16 | lo as u32).into());
match v.segments() {
[0, 0, 0, 0, 0, 0xffff, hi, lo] => v4(hi, lo), // IPv4-mapped
[0x64, 0xff9b, 0, 0, 0, 0, hi, lo] => v4(hi, lo), // NAT64 well-known prefix
[0x2002, hi, lo, ..] => v4(hi, lo), // 6to4
[0x2001, 0xdb8, ..] | [0x2001, 0, ..] => false, // documentation, Teredo
s => s[0] & 0xe000 == 0x2000, // global unicast 2000::/3 only
}
}
}
}
/// Every HTTP agent: a silent server (connect or any single read, TLS handshake included) fails after 30 s instead of hanging.
pub fn agent() -> ureq::AgentBuilder {
let t = std::time::Duration::from_secs(30);
ureq::AgentBuilder::new().timeout_connect(t).timeout_read(t)
}
/// Registry the user named as local: `localhost` or a non-public IP literal. Decided by name only --
/// a DNS name pointing at loopback (e.g. *.127.0.0.1.nip.io) is controlled by whoever owns the zone.
fn explicit_local(registry: &str) -> bool {
let host = registry.split_once(':').map_or(registry, |(h, _)| h);
host == "localhost" || host.parse().is_ok_and(|ip| !is_public(ip))
}
/// HTTP agent for a registry. Public registries get an agent that refuses to connect to non-public
/// addresses (covers token realms and blob redirects too); a registry that is itself local
/// (e.g. localhost:5000) was chosen explicitly by the user and keeps the default agent.
fn agent_for(registry: &str) -> ureq::Agent {
let addr = if registry.contains(':') { registry.to_string() } else { format!("{registry}:443") };
let local = addr.to_socket_addrs().map(|mut a| a.any(|a| !is_public(a.ip()))).unwrap_or(false);
if local {
return ureq::agent();
/// addresses (covers token realms and blob redirects too); an explicitly local registry
/// (e.g. localhost:5000, 192.168.1.5:5000) was chosen by the user and keeps the default agent.
pub fn agent_for(registry: &str) -> ureq::Agent {
if explicit_local(registry) {
return agent().build();
}
ureq::AgentBuilder::new()
agent()
.resolver(|addr: &str| -> std::io::Result<Vec<SocketAddr>> {
let ok: Vec<SocketAddr> = addr.to_socket_addrs()?.filter(|a| is_public(a.ip())).collect();
if ok.is_empty() {
@@ -199,8 +216,11 @@ pub fn pull(home: &Path, image: &str, refresh: bool) -> Result<Image> {
}
}
/// Flattened name for humans plus a hash of the exact reference, so `a_b:t` and `a/b:t` never share a ref file.
/// (Pre-hash ref files are simply ignored: the image is re-resolved and gc drops them.)
fn ref_path(home: &Path, image: &str) -> PathBuf {
home.join("images/refs").join(image.replace(['/', ':', '@'], "_"))
let h = hex::encode(Sha256::digest(image));
home.join("images/refs").join(format!("{}-{}", image.replace(['/', ':', '@'], "_"), &h[..16]))
}
fn image_dir(home: &Path, digest: &str) -> PathBuf {
@@ -224,7 +244,10 @@ pub fn gc(home: &Path, keep: &[String]) -> Result<u64> {
for e in fs::read_dir(home.join("images/refs"))?.flatten() {
match fs::read_to_string(e.path()) {
Ok(d) if keep.contains(&e.path()) => drop(live.insert(image_dir(home, d.trim()))),
_ => fs::remove_file(e.path())?,
_ => match fs::remove_file(e.path()) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()), // gone: a concurrent pull renamed it
_ => {}
},
}
}
let mut freed = 0;
@@ -467,14 +490,26 @@ mod tests {
#[test]
fn public_addresses() {
for ip in ["127.0.0.1", "10.1.2.3", "172.16.0.1", "192.168.64.1", "169.254.169.254", "100.64.0.1", "0.0.0.0", "::1", "fe80::1", "fd00::1", "::ffff:127.0.0.1"] {
for ip in ["127.0.0.1", "10.1.2.3", "172.16.0.1", "192.168.64.1", "169.254.169.254", "100.64.0.1", "0.0.0.0", "::1", "fe80::1", "fd00::1", "::ffff:127.0.0.1",
"64:ff9b::7f00:1", "64:ff9b::a00:1", "64:ff9b:1::1", "2002:7f00:1::1", "2002:c0a8:101::", "::7f00:1", "::a00:1",
"2001:db8::1", "2001::1", "ff02::1"] {
assert!(!is_public(ip.parse().unwrap()), "{ip} must be rejected");
}
for ip in ["8.8.8.8", "140.82.121.4", "2606:4700::6810:84e5"] {
for ip in ["8.8.8.8", "140.82.121.4", "2606:4700::6810:84e5", "64:ff9b::808:808", "2002:808:808::1", "::ffff:8.8.8.8"] {
assert!(is_public(ip.parse().unwrap()), "{ip} must be allowed");
}
}
#[test]
fn local_registries() {
for l in ["localhost:5000", "localhost", "127.0.0.1:5000", "192.168.1.5:5000"] {
assert!(explicit_local(l), "{l}");
}
for r in ["evil.127.0.0.1.nip.io:8443", "localhost.evil.com", "ghcr.io", "8.8.8.8:443", "registry-1.docker.io"] {
assert!(!explicit_local(r), "{r}");
}
}
#[test]
fn bad_refs() {
for bad in ["alpine@sha256:../../x", "../x", "a/../b", "alpine:../x", "Alpine", "a//b"] {
@@ -485,6 +520,14 @@ mod tests {
}
}
#[test]
fn ref_paths_distinct() {
let h = Path::new("/h");
assert_ne!(ref_path(h, "evil_bar:v1"), ref_path(h, "evil/bar:v1"));
assert_ne!(ref_path(h, "docker_sandbox-templates_claude-code"), ref_path(h, "docker/sandbox-templates:claude-code"));
assert_eq!(ref_path(h, "alpine"), ref_path(h, "alpine"));
}
#[test]
fn digests() {
assert!(check_digest(&format!("sha256:{}", "a".repeat(64))).is_ok());
+223
View File
@@ -0,0 +1,223 @@
//! Sandbox bookkeeping: listing, removal, image gc.
use crate::exec;
use crate::oci;
use crate::util::sanitize;
use anyhow::{Context, Result, anyhow, bail};
use serde_json::Value;
use std::collections::HashMap;
use std::fs;
use std::io::Write;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::time::Duration;
/// The lock `run` holds for the VM's lifetime; Err if the sandbox is running.
pub fn lock_stopped(dir: &Path) -> Result<fs::File> {
let f = fs::File::create(dir.join("lock"))?;
f.try_lock().map_err(|_| anyhow!("locked by a running VM"))?;
Ok(f)
}
/// Guest counters, read from /proc through the exec channel.
#[derive(Default, Debug, PartialEq)]
struct Stats {
busy: u64,
total: u64,
ncpu: u64,
mem_used: u64,
mem_total: u64,
rx: u64,
tx: u64,
rd: u64,
wr: u64,
pids: u64,
}
const STATS_SH: &str = "cd /proc; B=/.wsbx/busybox; $B grep ^cpu stat; $B grep -E '^Mem(Total|Available):' meminfo; \
$B tail -n +3 net/dev; $B grep ' vda ' diskstats; set -- [0-9]*; echo pids $#";
fn parse(out: &str) -> Stats {
let (mut s, mut avail) = (Stats::default(), 0);
for l in out.lines() {
let f: Vec<&str> = l.split_whitespace().collect();
let num = |i: usize| f.get(i).and_then(|v| v.parse::<u64>().ok()).unwrap_or(0);
match f.first().copied() {
Some("cpu") => {
let v: Vec<u64> = (1..=8).map(num).collect(); // user nice system idle iowait irq softirq steal
s.total = v.iter().sum();
s.busy = s.total.saturating_sub(v[3] + v[4]);
}
Some(c) if c.starts_with("cpu") => s.ncpu += 1,
Some("MemTotal:") => s.mem_total = num(1) * 1024,
Some("MemAvailable:") => avail = num(1) * 1024,
Some("pids") => s.pids = num(1),
_ if f.get(2) == Some(&"vda") => (s.rd, s.wr) = (num(5) * 512, num(9) * 512),
_ => {
// net/dev: " eth0:123 ..." (no space once rx bytes outgrow the column)
if let Some((ifc, rest)) = l.split_once(':')
&& ifc.trim() != "lo"
{
let n: Vec<u64> = rest.split_whitespace().filter_map(|v| v.parse().ok()).collect();
s.rx += n.first().copied().unwrap_or(0);
s.tx += n.get(8).copied().unwrap_or(0);
}
}
}
}
s.mem_used = s.mem_total.saturating_sub(avail);
s
}
fn human(b: u64) -> String {
let (mut v, mut u) = (b as f64, 0);
while v >= 1024.0 && u < 4 {
v /= 1024.0;
u += 1;
}
if u == 0 { format!("{b}B") } else { format!("{v:.1}{}", ["", "KiB", "MiB", "GiB", "TiB"][u]) }
}
struct Row {
name: String,
dir: PathBuf,
meta: Value,
running: bool,
}
fn rows(h: &Path) -> Result<Vec<Row>> {
let Ok(rd) = fs::read_dir(h.join("sandboxes")) else { return Ok(vec![]) };
let mut rows = vec![];
for e in rd.flatten() {
let Some(meta) = fs::read(e.path().join("meta.json")).ok().and_then(|m| serde_json::from_slice(&m).ok()) else { continue };
let running = lock_stopped(&e.path()).is_err();
rows.push(Row { name: e.file_name().to_string_lossy().into(), dir: e.path(), meta, running });
}
rows.sort_by(|a, b| a.name.cmp(&b.name));
Ok(rows)
}
/// Stats of all running sandboxes, sampled in parallel; sandboxes that don't answer are left out.
fn sample(rows: &[Row]) -> HashMap<String, Stats> {
std::thread::scope(|sc| {
let hs: Vec<_> = rows
.iter()
.filter(|r| r.running)
.map(|r| (r.name.clone(), sc.spawn(|| exec::capture(&r.dir.join("exec.sock"), &["/.wsbx/busybox", "sh", "-c", STATS_SH]))))
.collect();
hs.into_iter().filter_map(|(n, j)| Some((n, parse(&j.join().ok()?.ok()?)))).collect()
})
}
fn table(rows: &[Row], prev: &HashMap<String, Stats>, cur: &HashMap<String, Stats>) -> Vec<[String; 11]> {
let head = ["NAME", "STATUS", "CPU %", "MEM USAGE / LIMIT", "MEM %", "NET I/O", "BLOCK I/O", "PIDS", "DISK", "IMAGE", "WORKSPACE"];
let mut t = vec![head.map(String::from)];
for r in rows {
let pct = |a: u64, b: u64| format!("{:.1}%", a as f64 * 100.0 / b.max(1) as f64);
let st = cur.get(&r.name).map(|c| {
// like docker: 100% = one vCPU busy
let cpu = prev.get(&r.name).filter(|p| c.total > p.total).map(|p| pct(c.busy.saturating_sub(p.busy) * c.ncpu.max(1), c.total - p.total));
[
cpu.unwrap_or("-".into()),
format!("{} / {}", human(c.mem_used), human(c.mem_total)),
pct(c.mem_used, c.mem_total),
format!("{} / {}", human(c.rx), human(c.tx)),
format!("{} / {}", human(c.rd), human(c.wr)),
c.pids.to_string(),
]
});
let [cpu, mem, memp, net, blk, pids] = st.unwrap_or_else(|| std::array::from_fn(|_| "-".into()));
let disk = fs::metadata(r.dir.join("disk.img")).map(|m| m.blocks() * 512).unwrap_or(0);
let s = |k: &str| r.meta[k].as_str().unwrap_or("-").to_string();
let status = if r.running { "running" } else { "stopped" };
t.push([r.name.clone(), status.into(), cpu, mem, memp, net, blk, pids, human(disk), s("image"), s("workspace")]);
}
t
}
/// `wsbx ps`; with `follow`, redraws every second like `docker stats` until Ctrl-C.
pub fn ls(h: &Path, follow: bool) -> Result<()> {
let mut prev = HashMap::new();
let mut first = true;
loop {
let rows = rows(h)?;
let cur = sample(&rows);
if first && !cur.is_empty() {
// CPU % needs two samples
(prev, first) = (cur, false);
std::thread::sleep(Duration::from_millis(500));
continue;
}
let t = table(&rows, &prev, &cur);
let w: Vec<usize> = (0..11).map(|i| t.iter().map(|r| r[i].chars().count()).max().unwrap_or(0)).collect();
let mut out = String::new();
for r in &t {
let line: String = r
.iter()
.enumerate()
.map(|(i, c)| if (2..9).contains(&i) { format!("{c:>w$} ", w = w[i]) } else { format!("{c:<w$} ", w = w[i]) })
.collect();
out += line.trim_end();
out += if follow { "\x1b[K\n" } else { "\n" };
}
if !follow {
print!("{out}");
return Ok(());
}
print!("\x1b[H{out}\x1b[J"); // home, overwrite, clear the rest: no flicker
std::io::stdout().flush()?;
(prev, first) = (cur, false);
std::thread::sleep(Duration::from_secs(1));
}
}
pub fn rm(h: &Path, names: Vec<String>, disk: bool) -> Result<()> {
for n in names {
if n.is_empty() || sanitize(&n) != n {
bail!("invalid sandbox name {n:?}");
}
let d = h.join("sandboxes").join(&n);
if !d.join("meta.json").exists() {
bail!("no sandbox {n}");
}
// Held across the delete so the sandbox can't be started meanwhile.
let _lock = lock_stopped(&d).with_context(|| format!("sandbox {n} is running"))?;
if disk {
match fs::remove_file(d.join("disk.img")) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => println!("removed disk of {n}"),
}
} else {
fs::remove_dir_all(d)?;
println!("removed {n}");
}
}
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
fn parse_proc() {
let out = "cpu 100 0 50 800 50 0 0 0 0 0\ncpu0 1 2 3\ncpu1 1 2 3\nMemTotal: 2000 kB\nMemAvailable: 500 kB\n\
\x20 lo: 999 0 0 0 0 0 0 0 999 0\n eth0:123456789 1 0 0 0 0 0 0 42 1 0 0 0 0 0 0\n\
\x20254 0 vda 10 0 4 0 7 0 8 0 0 0 0\npids 17\n";
let s = super::parse(out);
assert_eq!((s.total, s.busy, s.ncpu), (1000, 150, 2));
assert_eq!((s.mem_total, s.mem_used), (2000 * 1024, 1500 * 1024));
assert_eq!((s.rx, s.tx, s.rd, s.wr, s.pids), (123456789, 42, 2048, 4096, 17));
assert_eq!(super::human(1536), "1.5KiB");
}
}
/// Delete cached images no sandbox was created from.
pub fn gc(h: &Path) -> Result<()> {
let used: Vec<String> = fs::read_dir(h.join("sandboxes"))
.into_iter()
.flatten()
.flatten()
.filter_map(|e| serde_json::from_slice::<Value>(&fs::read(e.path().join("meta.json")).ok()?).ok())
.filter_map(|m| m["image"].as_str().map(String::from))
.collect();
println!("freed {} MB", oci::gc(h, &used)? / 1_000_000);
Ok(())
}
+53
View File
@@ -0,0 +1,53 @@
//! Copying host config into the guest share without following symlinks out of it.
use anyhow::{Result, anyhow};
use std::fs;
use std::path::{Path, PathBuf};
pub const STAGE_LIMIT: u64 = 256 << 20;
/// Copy config into the stage, resolving symlinks (they'd dangle in the VM) but only when they
/// resolve inside `roots`; no cycles, regular files and dirs only (a FIFO would hang fs::copy).
pub fn copy_tree(src: &Path, dst: &Path, roots: &[PathBuf], budget: &mut u64, seen: &mut std::collections::HashSet<PathBuf>) -> Result<()> {
let Ok(real) = fs::canonicalize(src) else { return Ok(()) }; // dangling symlink
if !roots.iter().any(|r| real.starts_with(r)) {
eprintln!("wsbx: not sharing {} -> {} (outside the shared config)", src.display(), real.display());
return Ok(());
}
let m = fs::metadata(&real)?;
if m.is_dir() {
if !seen.insert(real.clone()) {
return Ok(()); // symlink cycle or already copied
}
fs::create_dir_all(dst)?;
for e in fs::read_dir(&real)? {
let e = e?;
copy_tree(&e.path(), &dst.join(e.file_name()), roots, budget, seen)?;
}
} else if m.is_file() {
*budget = budget.checked_sub(m.len()).ok_or_else(|| anyhow!("shared config exceeds {} MB", STAGE_LIMIT >> 20))?;
fs::copy(&real, dst)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
fn copy_tree_contains_symlinks() {
use std::os::unix::fs::symlink;
let t = std::env::temp_dir().join(format!("wsbx-ct-{}", std::process::id()));
let (root, out, secret) = (t.join("root"), t.join("out"), t.join("secret"));
std::fs::create_dir_all(root.join("skills/a")).unwrap();
std::fs::create_dir_all(&secret).unwrap();
std::fs::write(root.join("skills/a/SKILL.md"), "ok").unwrap();
std::fs::write(secret.join("id_rsa"), "key").unwrap();
symlink(&secret, root.join("skills/leak")).unwrap(); // escapes the root
symlink(root.join("skills"), root.join("skills/a/loop")).unwrap(); // cycle
let roots = vec![std::fs::canonicalize(&root).unwrap()];
let (mut budget, mut seen) = (super::STAGE_LIMIT, Default::default());
super::copy_tree(&root.join("skills"), &out, &roots, &mut budget, &mut seen).unwrap();
assert_eq!(std::fs::read_to_string(out.join("a/SKILL.md")).unwrap(), "ok");
assert!(!out.join("leak").exists());
std::fs::remove_dir_all(&t).unwrap();
}
}
+36
View File
@@ -0,0 +1,36 @@
//! Host side of `-p PORT`: vfkit vsock unix socket -> host localhost:PORT.
use anyhow::{Result, bail};
use std::fs;
use std::net::{Shutdown, TcpStream};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::Path;
/// Host end of a tunnel: vfkit dials this unix socket for each guest vsock connection.
pub fn tunnel(sock: &Path, port: u16) -> Result<()> {
if sock.as_os_str().len() > 100 {
bail!("socket path too long: {} (set WSBX_HOME to something shorter)", sock.display());
}
let _ = fs::remove_file(sock);
let l = UnixListener::bind(sock)?;
std::thread::spawn(move || {
for u in l.incoming().flatten() {
std::thread::spawn(move || {
if let Ok(t) = TcpStream::connect(("localhost", port)) {
pipe(u, t);
}
});
}
});
Ok(())
}
fn pipe(mut u: UnixStream, mut t: TcpStream) {
let (Ok(mut u2), Ok(mut t2)) = (u.try_clone(), t.try_clone()) else { return };
let up = std::thread::spawn(move || {
let _ = std::io::copy(&mut u2, &mut t2);
let _ = t2.shutdown(Shutdown::Write);
});
let _ = std::io::copy(&mut t, &mut u);
let _ = u.shutdown(Shutdown::Write);
let _ = up.join();
}
+38
View File
@@ -0,0 +1,38 @@
//! Small helpers shared across modules.
use anyhow::{Result, anyhow};
use serde_json::Value;
use std::path::{Path, PathBuf};
use std::process::Command;
pub fn base(p: &Path) -> String {
p.file_name().map(|s| s.to_string_lossy().into_owned()).unwrap_or_else(|| "root".into())
}
pub fn sanitize(s: &str) -> String {
s.chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' { c.to_ascii_lowercase() } else { '-' }).collect()
}
pub fn sq(s: &str) -> String {
format!("'{}'", s.replace('\'', r"'\''"))
}
/// Find a tool on PATH, else in its Homebrew keg (keg-only formulae like e2fsprogs aren't linked).
pub fn need(tool: &str, formula: &str) -> Result<PathBuf> {
let on_path = std::env::var_os("PATH").map(|p| std::env::split_paths(&p).collect::<Vec<_>>()).unwrap_or_default();
let find = |dirs: &[PathBuf]| dirs.iter().map(|d| d.join(tool)).find(|p| p.is_file());
find(&on_path)
.or_else(|| {
let o = Command::new("brew").args(["--prefix", formula]).output().ok()?;
let keg = PathBuf::from(String::from_utf8_lossy(&o.stdout).trim());
find(&[keg.join("sbin"), keg.join("bin")])
})
.ok_or_else(|| anyhow!("{tool} not found — install it with: brew install {formula}"))
}
pub fn strs(v: &Value) -> Vec<String> {
v.as_array().map(|a| a.iter().filter_map(|s| s.as_str().map(String::from)).collect()).unwrap_or_default()
}
pub fn now() -> u64 {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()
}
+309
View File
@@ -0,0 +1,309 @@
//! Booting a sandbox: run script, vfkit invocation, guest results.
use crate::disk::create_disk;
use crate::exec;
use crate::kernel::kernel;
use crate::oci;
use crate::sandbox::lock_stopped;
use crate::stage::{STAGE_LIMIT, copy_tree};
use crate::tunnel::tunnel;
use crate::util::{base, need, now, sanitize, sq, strs};
use crate::Opts;
use anyhow::{Context, Result, anyhow, bail};
use serde_json::{Value, json};
use std::fs;
use std::io::{IsTerminal, Read};
use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
const INIT: &str = include_str!("init.sh");
const DHCP: &str = include_str!("dhcp.sh");
const GUEST: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wsbx-guest"));
/// Agent-specific guest preparation.
#[derive(Default)]
pub struct Guest {
/// (host path, name under /.wsbx/host/stage) copied before each start, symlinks resolved
pub stage: Vec<(PathBuf, String)>,
/// Symlinks inside staged trees may only resolve into these (canonical) roots
pub roots: Vec<PathBuf>,
/// Shell run as root after HOME/env are set, before dropping to the image user
pub setup: String,
}
pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g: Guest) -> Result<()> {
let vfkit = need("vfkit", "vfkit")?;
let kernel = match &o.kernel {
Some(k) => k.clone(),
None => kernel(h, false, None)?,
};
let ws = o.workspace.as_ref().map(fs::canonicalize).transpose().context("workspace")?;
vf(h)?; // everything vfkit gets lives under h, plus kernel + workspace: refuse bad paths before creating anything
vf(&kernel)?;
if let Some(ws) = &ws {
vf(ws)?;
let hc = fs::canonicalize(h)?;
if ws.starts_with(&hc) || hc.starts_with(ws) {
bail!("workspace {} overlaps the wsbx state dir {} (disks, kernel, control files)", ws.display(), hc.display());
}
}
let img = oci::pull(h, image, o.pull)?;
let short = image.rsplit('/').next().unwrap().split([':', '@']).next().unwrap();
let name = sanitize(&o.name.clone().unwrap_or_else(|| format!("{short}-{}", ws.as_deref().map(base).unwrap_or("vm".into()))));
let dir = h.join("sandboxes").join(&name);
fs::create_dir_all(&dir)?;
let _lock = lock_stopped(&dir).map_err(|_| anyhow!("sandbox {name} is already running"))?;
// The control share is writable by the guest (virtio-fs has no host-enforced read-only).
// Recreate it empty on every start -- while this sandbox's VM is stopped and we hold the lock --
// so nothing a previous guest left there (e.g. `run.sh -> ~/.zshrc`) is ever written through.
// Persistent state lives in meta.json, which the guest cannot reach.
let share = dir.join("share");
match fs::remove_dir_all(&share) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => {}
}
std::fs::DirBuilder::new().mode(0o700).create(&share)?;
let disk = dir.join("disk.img");
let meta_path = dir.join("meta.json");
if o.fresh {
let _ = fs::remove_file(&disk);
}
let mut meta: Value = fs::read(&meta_path).ok().and_then(|m| serde_json::from_slice(&m).ok()).unwrap_or(Value::Null);
if disk.exists() && meta["digest"] != img.digest.as_str() {
eprintln!("wsbx: note: {name} was created from an older image; use --fresh to rebuild");
}
if !disk.exists() {
eprintln!("wsbx: creating sandbox {name}");
// meta first: it marks the image as in use for `wsbx gc` and makes a crashed create visible to ps/rm
meta = json!({"image": image, "digest": img.digest, "workspace": ws, "created": now(), "provisioned": false});
fs::write(&meta_path, serde_json::to_vec_pretty(&meta)?)?;
create_disk(h, &disk, o.disk)?;
}
// Build the guest-side run script (executed as root by init, then drops to the image user).
let c = &img.config;
let mut argv = strs(&c["Entrypoint"]);
argv.extend(if cmd.is_empty() { strs(&c["Cmd"]) } else { cmd });
argv.extend(extra.iter().cloned());
if argv.is_empty() {
argv.push("/bin/sh".into());
}
let user = c["User"].as_str().unwrap_or("").to_string();
let wd = ws
.as_ref()
.map(|p| p.to_string_lossy().into_owned())
.or_else(|| c["WorkingDir"].as_str().filter(|s| !s.is_empty()).map(String::from))
.unwrap_or("/".into());
let mut s = String::from("B=/.wsbx/busybox\n");
s += &format!("$B date -s @{} >/dev/null\n", now());
// Some images ship no /etc/hosts: without a localhost entry, Go and others resolve it via DNS and fail.
s += "$B grep -qw localhost /etc/hosts 2>/dev/null || printf '127.0.0.1 localhost\\n::1 localhost\\n' >> /etc/hosts\n";
s += &format!("$B hostname {name}; echo {name} > /etc/hostname; $B grep -q ' {name}$' /etc/hosts 2>/dev/null || echo '127.0.1.1 {name}' >> /etc/hosts\n");
if let Some((rows, cols)) = term_size() {
s += &format!("$B stty rows {rows} cols {cols}\n");
}
if let Some(ws) = &ws {
let w = sq(&ws.to_string_lossy());
s += &format!("$B mkdir -p {w} && $B mount -t virtiofs ws {w}\n");
}
let u = user.split(':').next().unwrap_or("");
s += &format!("export HOME=$($B awk -F: -v u={} '$1==u||$3==u{{print $6;exit}}' /etc/passwd)\n", sq(if u.is_empty() { "root" } else { u }));
s += "[ -n \"$HOME\" ] || export HOME=/root\n";
s += &format!("export TERM={} TINI_SUBREAPER=1\n", sq(&std::env::var("TERM").unwrap_or("xterm-256color".into())));
let mut env = strs(&c["Env"]);
for e in &o.env {
match e.split_once('=') {
Some(_) => env.push(e.clone()),
None => env.push(format!("{e}={}", std::env::var(e).with_context(|| format!("env {e} not set on host"))?)),
}
}
for e in env {
match export(&e) {
Some(line) => s += &line,
None => eprintln!("wsbx: skipping env entry with invalid name: {e:?}"),
}
}
s += &format!("export WSBX_USER={}\n", sq(&user));
let stage = share.join("stage");
fs::create_dir(&stage)?;
let (mut budget, mut seen) = (STAGE_LIMIT, std::collections::HashSet::new());
for (src, to) in &g.stage {
let dst = stage.join(to);
fs::create_dir_all(dst.parent().unwrap())?;
copy_tree(src, &dst, &g.roots, &mut budget, &mut seen)?;
}
s += &g.setup;
s += &format!("cd {} 2>/dev/null || cd /\n", sq(&wd));
// Guest helper (guest/*.go), started after the cd so exec sessions inherit run.sh's env and working dir.
// `wsbx exec`: host -> vfkit listening on exec.sock -> vsock -> helper (runs the command, pty or pipes)
// Host tunnel: helper listens on guest localhost:PORT -> vsock -> vfkit -> unix socket -> host localhost:PORT
fs::write(share.join("guest"), GUEST)?;
fs::set_permissions(share.join("guest"), fs::Permissions::from_mode(0o755))?;
let (mut vsock, mut args) = (Vec::new(), Vec::new());
let exec_sock = dir.join("exec.sock");
let _ = fs::remove_file(&exec_sock); // left over if vfkit was killed; vfkit can't listen on it then
if exec_sock.as_os_str().len() <= 100 {
// `connect`: vfkit listens on the unix socket and dials the guest (vfkit defaults to `listen`)
vsock.push(format!("virtio-vsock,port={},socketURL={},connect", exec::VPORT, vf(&exec_sock)?));
args.push(format!("-exec {}", exec::VPORT));
} else {
eprintln!("wsbx: note: `wsbx exec` unavailable, socket path too long (set WSBX_HOME to something shorter)");
}
let mut ports = o.host_ports.clone();
ports.sort();
ports.dedup();
for (i, &p) in ports.iter().enumerate() {
let vport = 10000 + i as u32;
let sock = dir.join(format!("p{p}.sock"));
tunnel(&sock, p)?;
vsock.push(format!("virtio-vsock,port={vport},socketURL={},listen", vf(&sock)?));
args.push(format!("{p}={vport}"));
}
if !ports.is_empty() {
eprintln!("wsbx: host localhost:{{{}}} tunneled into the VM", ports.iter().map(u16::to_string).collect::<Vec<_>>().join(","));
}
s += "$B mkfifo /run/wsbx-guest\n";
s += &format!("/.wsbx/host/guest {} >/run/wsbx-guest 2>/.wsbx/host/guest.log &\n", args.join(" "));
s += "read _ </run/wsbx-guest\n";
let argv: Vec<String> = argv.iter().map(|a| sq(a)).collect();
if u.is_empty() || u == "root" || u == "0" {
s += &format!("exec {}\n", argv.join(" "));
} else {
s += &format!("exec $B chpst -u {} {}\n", sq(&user), argv.join(" "));
}
fs::write(share.join("run.sh"), s)?;
for (f, body) in [("init", INIT), ("dhcp", DHCP)] {
fs::write(share.join(f), body)?;
fs::set_permissions(share.join(f), fs::Permissions::from_mode(0o755))?;
}
let _ = fs::remove_file(share.join("exit"));
let initrd = h.join("empty.initrd"); // vfkit insists on an initrd; an empty one is fine
if !initrd.exists() {
fs::write(&initrd, "")?;
}
let mac = {
let x = name.bytes().fold(0xcbf29ce484222325u64, |a, b| (a ^ b as u64).wrapping_mul(0x100000001b3));
format!("02:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", x as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8, (x >> 32) as u8)
};
// panic=1 reboots, which VZ turns into a restart; init powers off on any second boot (see init.sh).
let cmdline = "console=hvc0 root=/dev/vda rw rootfstype=ext4 init=/.wsbx/init quiet panic=1";
let tty = std::io::stdin().is_terminal();
let mut v = Command::new(vfkit);
v.args(["--cpus", &o.cpus.to_string(), "--memory", &o.memory.to_string(), "--log-level", "error"])
.arg("--bootloader")
.arg(format!("linux,kernel={},initrd={},cmdline=\"{cmdline}\"", vf(&kernel)?, vf(&initrd)?))
.arg("--device")
.arg(format!("virtio-blk,path={}", vf(&disk)?))
.arg("--device")
.arg(format!("virtio-net,nat,mac={mac}"))
.arg("--device")
.arg(format!("virtio-fs,sharedDir={},mountTag=wsbx", vf(&share)?))
.args(["--device", "virtio-rng", "--device"])
// vfkit's stdio console needs a TTY; otherwise stream output only (no stdin)
.arg(if tty { "virtio-serial,stdio".into() } else { format!("virtio-serial,logFilePath={}", vf(&out_path())?) })
.stderr(fs::File::create(dir.join("vfkit.log"))?);
if let Some(ws) = &ws {
v.arg("--device").arg(format!("virtio-fs,sharedDir={},mountTag=ws", vf(ws)?));
}
// Image tar is only needed until the guest has unpacked it; read-only so sandboxes can share it.
let provisioned = meta["provisioned"].as_bool().unwrap_or(false);
if !provisioned {
v.arg("--device").arg(format!("virtio-blk,path={},readonly", vf(&img.rootfs)?));
}
for d in vsock {
v.arg("--device").arg(d);
}
let saved = stty(&["-g"]);
if saved.is_some() {
stty(&["raw", "-echo"]);
}
let status = v.stdin(Stdio::inherit()).status();
if let Some(s) = saved {
stty(&[s.trim()]);
}
let status = status?;
// The VM is gone now, so the guest can't race these reads; still never follow its symlinks.
let code = guest_output(&share, "exit").and_then(|s| s.trim().parse().ok());
if !provisioned && guest_output(&share, "provisioned").is_some() && meta.is_object() {
meta["provisioned"] = json!(true);
fs::write(&meta_path, serde_json::to_vec_pretty(&meta)?)?;
}
let _ = fs::remove_dir_all(&share); // run.sh contains forwarded env
match code {
Some(c) => std::process::exit(c),
None if !status.success() => bail!("vm failed, see {}", dir.join("vfkit.log").display()),
None => bail!("VM stopped without an exit status (kernel panic, reboot or poweroff inside the VM)"),
}
}
/// `export K='v'` line; None unless K is a shell identifier (image Env is hostile and run.sh runs as root).
fn export(e: &str) -> Option<String> {
let (k, v) = e.split_once('=')?;
let ok = k.bytes().next().is_some_and(|b| b.is_ascii_alphabetic() || b == b'_') && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_');
ok.then(|| format!("export {k}={}\n", sq(v)))
}
/// Small result file written by the guest; regular files only (no symlinks), size-capped.
fn guest_output(share: &Path, name: &str) -> Option<String> {
let p = share.join(name);
fs::symlink_metadata(&p).ok().filter(|m| m.is_file())?;
let mut s = String::new();
fs::File::open(p).ok()?.take(64).read_to_string(&mut s).ok()?;
Some(s)
}
/// vfkit splits device options on ',' and the bootloader cmdline is quoted: a path containing
/// those (e.g. a workspace named `repo,sharedDir=/Users/me`) would inject options. Refuse them.
fn vf(p: &Path) -> Result<String> {
let s = p.to_str().ok_or_else(|| anyhow!("non-UTF-8 path {}", p.display()))?;
if s.chars().any(|c| c == ',' || c == '"' || c.is_control()) {
bail!("unsupported character in path {s:?} (',', '\"' and control characters would inject vfkit options)");
}
Ok(s.to_string())
}
/// Where our stdout points (vfkit only accepts a path for non-TTY console output).
fn out_path() -> PathBuf {
fs::read_link("/dev/fd/1").unwrap_or("/dev/stdout".into())
}
pub fn stty(args: &[&str]) -> Option<String> {
let out = Command::new("stty").args(args).stdin(Stdio::inherit()).stderr(Stdio::null()).output().ok()?;
out.status.success().then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}
/// Rows and columns of the terminal on stdin.
pub fn term_size() -> Option<(u16, u16)> {
// SAFETY: TIOCGWINSZ only writes a winsize struct.
let mut ws: libc::winsize = unsafe { std::mem::zeroed() };
let ok = unsafe { libc::ioctl(0, libc::TIOCGWINSZ, &mut ws) } == 0;
(ok && ws.ws_row > 0 && ws.ws_col > 0).then_some((ws.ws_row, ws.ws_col))
}
#[cfg(test)]
mod tests {
use std::path::Path;
#[test]
fn env_keys() {
assert_eq!(super::export("_A1=x y").as_deref(), Some("export _A1='x y'\n"));
for bad in ["PWN$(id > /ws/pwn)=x", "=x", "1A=x", "A B=x", "A;id=x", "noequals"] {
assert!(super::export(bad).is_none(), "{bad}");
}
}
#[test]
fn vfkit_injection_rejected() {
assert!(super::vf(Path::new("/Users/me/repo")).is_ok());
for bad in ["/shared/repo/,sharedDir=/Users/me", "/a\"b", "/a\nb"] {
assert!(super::vf(Path::new(bad)).is_err(), "{bad}");
}
}
}