Files
devvm/internal/cli/proxy.go
T
w3cjandClaude Opus 5.5 941c63c696 feat(proxy): the proxy's child runs under Seatbelt on macOS
On macOS the broker starts devvm _proxy-child through
/usr/bin/sandbox-exec -p PROFILE, by devvm's real path, with a
deny-default profile, so the child is confined from its first
instruction: devvm's binary to run and read, its folder's metadata,
dyld-support.sb, /usr/lib and /System/Library, the time zone and
resolver files, its logs folder and the shared Nix cache (regular
files only, no hard links, no folder removed, no flags, ACL, owner,
set-id or xattr changes), stat alone on the watched files and every
link on the way, trustd and trustd.agent, mDNSResponder's socket, TCP
and UDP out (loopback left to the floor: a rule for a loopback port
applies at once, which a profile fixed at the start can't follow),
sysctl but kern.proc, signals to itself. No home folder, no devvm folder but its
own, no Keychain, no other unix socket, no bind, fork, exec, signal to
or look at another process (process-info, the process list).

- internal/seatbelt: the shared owner of Seatbelt (Exec, String,
  Path, Links, ChildProfile, Sandbox.ProxyChild); sbString, sbPath and
  sandboxExec move here from the Lima backend, which uses them.
  ChildProfile refuses a relative path and the root.
- internal/proxy/confine.go: the confinement report every platform
  shares (Confinement, ParseConfinement, ReadConfined: one line of
  fixed words in run/proxy.confined, refused unless it parses, and
  unless it fits what the broker started the child with), the child's
  self-check (checkConfined: its broker's config must be refused), and
  the in-process hook (confineSelf, confineself_linux.go and
  confineself_other.go) for the Landlock follow-up.
- internal/proxy/childstart.go: startChild, childSandbox, childNeeds;
  IO.Seatbelt, set by devvm _proxy on macOS.
  A missing sandbox-exec, or one that failed before devvm's code ran
  (exit 65 or 71, or SIGABRT or SIGKILL, before the child answered,
  decided before the broker's own stop), starts the child again
  unconfined within the broker's 4 seconds, with a warning in
  proxy.log; any other failure fails the start. The broker makes the
  Nix cache's folders first. start carries how the broker started the
  child (Spawned).
- watched.get: a stat refused for want of permission reads through the
  broker each time (a watched link pointed elsewhere after the start).
- daemon.Handle.ExitCode and ExitSignal.
- devvm doctor warns for each running VM whose proxy's child isn't
  fully confined (netpolicy.Unconfined; proxy_unconfined,
  proxy_unconfined_unsupported, _no_sandbox, _sandbox_failed,
  _check_failed).
- Tests: TestParseConfinement, TestCheckConfined, TestReadConfined,
  TestStartChildSandbox (an over-claiming ready, SIGTERM),
  TestSandboxFailed, TestChildNeeds,
  TestWatchedStatRefused, TestUnconfined, TestString, TestLinks,
  TestChildProfile, TestProxyChild, TestChildProfileConfines (the test
  binary run again under the profile on macOS: flags, rmdir, the
  process list), doctor's report, ExitCode and
  ExitSignal; e2e network checks run/proxy.confined is full seatbelt
  on macOS.
- Docs: How it works' The child's sandbox, Processes, the design
  note's confinement and threat check (loopback as open work, other processes'
  details and kern.procargs2, the shared folders' flags, mDNSResponder's
  records, trustd.agent's trust settings, the fallback's signals, a
  ready that claims more), Troubleshooting's The proxy runs without a
  sandbox, Staying safe's escaping the VM, doctor's
  checks, Contributing's packages, the proxy's README, AGENTS.md,
  CHANGELOG.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 22:04:32 -07:00

90 lines
3.1 KiB
Go

package cli
import (
"runtime"
"github.com/spf13/cobra"
"github.com/devvmhq/devvm/assets"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/netpolicy"
"github.com/devvmhq/devvm/internal/paths"
"github.com/devvmhq/devvm/internal/proxy"
"github.com/devvmhq/devvm/internal/seatbelt"
"github.com/devvmhq/devvm/internal/ui"
"github.com/devvmhq/devvm/internal/vmlog"
)
// newProxyCmd is devvm _proxy: one VM's proxy, run with its config
// (-config FILE): the broker, which starts its child, devvm _proxy-child,
// and answers it. Hidden: it's not for users. Its flag is the proxy's own
// (one dash or two), parsed by proxy.Main; its event log and log lines
// are data, written to devvm's stdout and stderr, which its child gets as
// its own. Its secrets' values come from devvm's store, looked up in this
// process, and the logins it captures go there too. It holds the logs the
// VM's backend writes as the VM runs to their cap (vmlog.KeepVM). A signal
// waits for it to stop its child.
func newProxyCmd(a *app) *cobra.Command {
u := a.u
return &cobra.Command{
Use: "_proxy [flags]",
Short: "Run a VM's proxy",
Hidden: true,
DisableFlagParsing: true,
RunE: func(cmd *cobra.Command, args []string) error {
u.StopsItsOwn() // a signal waits for the broker to stop its child
store, err := a.secrets()
var d paths.Dirs
if err == nil {
d, err = a.dirs()
}
if err == nil {
out, errOut := ui.Files(u)
std := proxy.IO{
Stdout: u.Stdout(), Stderr: u.Stderr(), OutFile: out, ErrFile: errOut,
FS: assets.FS, // the built-in policies, read from the binary
Secret: store.Get,
Capture: netpolicy.Capturer{Dirs: d, Secrets: store},
// The VM's backend's logs, held to their cap while the VM runs.
KeepLogs: vmlog.KeepVM,
}
// On macOS the child starts under Seatbelt, confined from its first instruction.
if runtime.GOOS == "darwin" {
std.Seatbelt = seatbelt.Sandbox{Exec: seatbelt.Exec}.ProxyChild
}
err = proxy.Main(cmd.Context(), args, std)
}
if err != nil {
return msg.ProxyFailed(err.Error()).Because(err)
}
return nil
},
}
}
// newProxyChildCmd is devvm _proxy-child: the child of a VM's proxy's
// broker, which reads the VM's network (-for FILE, the broker's config,
// which it never opens, only so it can be told apart). Hidden: only the
// broker starts it, with its socket and listener. Its built-in policies
// come from devvm's own files in the binary; it looks up nothing itself.
func newProxyChildCmd(a *app) *cobra.Command {
u := a.u
return &cobra.Command{
Use: proxy.ChildCommand + " [flags]",
Short: "Run a VM's proxy's child",
Hidden: true,
DisableFlagParsing: true,
RunE: func(_ *cobra.Command, args []string) error {
out, errOut := ui.Files(u)
err := proxy.ChildMain(args, proxy.IO{
Stdout: u.Stdout(), Stderr: u.Stderr(), OutFile: out, ErrFile: errOut,
FS: assets.FS, // the built-in policies, read from the binary
})
if err != nil {
return msg.ProxyFailed(err.Error()).Because(err)
}
return nil
},
}
}