diff --git a/Cargo.lock b/Cargo.lock index b995bfa..884f323 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -854,12 +854,13 @@ checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" [[package]] name = "wsbx" -version = "0.1.1" +version = "0.1.2" dependencies = [ "anyhow", "clap", "flate2", "hex", + "libc", "serde_json", "sha2", "tar", diff --git a/Cargo.toml b/Cargo.toml index 9783292..05ddc38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wsbx" -version = "0.1.1" +version = "0.1.2" edition = "2024" [dependencies] @@ -8,6 +8,7 @@ anyhow = "1" clap = { version = "4", features = ["derive"] } flate2 = "1" hex = "0.4" +libc = "0.2" serde_json = "1" sha2 = "0.10" tar = "0.4" diff --git a/README.md b/README.md index e63bcab..c5b618e 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,9 @@ 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 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 ps # sandboxes: running/stopped, image, disk use, workspace (alias: ls) wsbx rm NAME... # delete stopped sandboxes; --disk deletes only the disk (fresh on next start) wsbx pull IMAGE @@ -25,6 +28,12 @@ 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. + **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. @@ -52,6 +61,10 @@ The VM is the boundary. The guest (including a prompt-injected agent or a malici (token realms and redirects included). - **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. - **Host tunnel**: only the ports you pass with `-p` (or the local `baseUrl`s in pi's `models.json`), over vsock. Remaining risks: diff --git a/build.rs b/build.rs index a060594..65ebd5e 100644 --- a/build.rs +++ b/build.rs @@ -2,17 +2,20 @@ 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"); + const SRC: [&str; 2] = ["guest/fwd.go", "guest/exec.go"]; + for f in SRC { + println!("cargo:rerun-if-changed={f}"); + } + 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") + .args(SRC) .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"); } diff --git a/guest/exec.go b/guest/exec.go new file mode 100644 index 0000000..ca89a76 --- /dev/null +++ b/guest/exec.go @@ -0,0 +1,333 @@ +// `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 + } + code := uint32(cmd.ProcessState.ExitCode()) + if ws, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok && ws.Signaled() { + code = 128 + uint32(ws.Signal()) + } + send('x', binary.BigEndian.AppendUint32(nil, code)) +} + +// 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 +} diff --git a/guest/fwd.go b/guest/fwd.go index e027bad..b060e01 100644 --- a/guest/fwd.go +++ b/guest/fwd.go @@ -1,32 +1,42 @@ -// 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... +// -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 +48,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 +64,27 @@ 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") + flag.Parse() + // 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 +99,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 {} } diff --git a/src/exec.rs b/src/exec.rs new file mode 100644 index 0000000..a509b78 --- /dev/null +++ b/src/exec.rs @@ -0,0 +1,121 @@ +//! `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 { + 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::connect(sock).map_err(|e| anyhow!("can't reach {name}: {e} (started by an older wsbx?)")) +} + +pub fn frame(kind: u8, p: &[u8]) -> Vec { + [&[kind][..], &(p.len() as u32).to_be_bytes(), p].concat() +} + +pub fn exec(h: &Path, name: &str, user: Option, argv: Vec) -> 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); + 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, kind: u8, p: &[u8]) -> io::Result<()> { + w.lock().unwrap().write_all(&frame(kind, p)) +} + +/// Copy guest output frames to stdout/stderr until the exit status arrives (None: connection closed first). +fn output(s: UnixStream) -> io::Result> { + let mut r = io::BufReader::new(s); + let (mut out, mut err) = (io::stdout().lock(), io::stderr().lock()); + 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, &mut out)?; + out.flush()?; + } + b'r' => { + io::copy(&mut p, &mut 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())?; + } + } + } +} diff --git a/src/forward.rs b/src/forward.rs new file mode 100644 index 0000000..d36cf0a --- /dev/null +++ b/src/forward.rs @@ -0,0 +1,105 @@ +//! `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) -> 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::(), guest.parse::()) 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}"); + } + // 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.clone(), 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(); + } + Ok(()) +} + +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, mut c: &TcpStream) -> Result { + 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())?; + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 3ace98e..e0c0dc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ //! wsbx — Docker-Sandboxes-style microVMs on macOS (Virtualization.framework via vfkit). mod agents; mod disk; +mod exec; +mod forward; mod kernel; mod oci; mod sandbox; @@ -18,7 +20,7 @@ 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, @@ -60,6 +62,25 @@ enum Cmd { #[arg(long)] pi_auth: bool, }, + /// 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, + #[arg(last = true)] + cmd: Vec, + }, + /// 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, + }, /// List sandboxes and whether they're running #[command(visible_alias = "ps")] Ls, @@ -73,6 +94,8 @@ enum Cmd { }, /// Delete cached images that no sandbox uses Gc, + /// Print the wsbx version + Version, } #[derive(Args)] @@ -143,9 +166,12 @@ fn main() -> Result<()> { 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::Ls => sandbox::ls(&h), Cmd::Gc => sandbox::gc(&h), Cmd::Rm { names, disk } => sandbox::rm(&h, names, disk), + Cmd::Version => Ok(println!("wsbx {}", env!("CARGO_PKG_VERSION"))), } } diff --git a/src/vm.rs b/src/vm.rs index 53e3e3a..f215e00 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -1,5 +1,6 @@ //! 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; @@ -17,7 +18,7 @@ use std::process::{Command, Stdio}; const INIT: &str = include_str!("init.sh"); const DHCP: &str = include_str!("dhcp.sh"); -const FWD: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wsbx-fwd")); +const GUEST: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wsbx-guest")); /// Agent-specific guest preparation. #[derive(Default)] @@ -97,7 +98,7 @@ pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec, extra: &[String], g 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() { + if let Some((rows, cols)) = term_size() { s += &format!("$B stty rows {rows} cols {cols}\n"); } if let Some(ws) = &ws { @@ -132,30 +133,39 @@ pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec, extra: &[String], g } s += &g.setup; - // Host tunnel: guest wsbx-fwd listens on localhost:PORT -> vsock -> vfkit -> unix socket -> host localhost:PORT + 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(); - let mut vsock = 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)?)); + args.push(format!("{p}={vport}")); + } 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 _ >().join(",")); } - - s += &format!("cd {} 2>/dev/null || cd /\n", sq(&wd)); + 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 _ = argv.iter().map(|a| sq(a)).collect(); if u.is_empty() || u == "root" || u == "0" { s += &format!("exec {}\n", argv.join(" ")); @@ -253,15 +263,17 @@ fn out_path() -> PathBuf { fs::read_link("/dev/fd/1").unwrap_or("/dev/stdout".into()) } -fn stty(args: &[&str]) -> Option { +pub fn stty(args: &[&str]) -> Option { 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()??)) +/// 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)]