Files
w3cjandClaude Opus 5.5 f2eac89211 fix(lima): on macOS, mount the project folder by its device and inode
A parent folder's VM can move a nested project folder aside and put a
link in its place. The checks around a start refuse what they see, but a
swap timed for the moment Virtualization.framework opens the share, and
put back before devvm's last check, made the nested VM mount the link's
target. On vz, the Lima backend now mounts the project folder as
location /.vol/<dev>/<ino>, from the folder devvm recorded (the state's
folder, passed as the new Want.ProjectID), with mountPoint set to its
path. macOS's volfs opens the folder by its inode wherever it is, so the
share is the recorded folder, never what's at the path.

Checked first on a real vz VM (Lima 2.2.1, macOS 26): vz and Lima accept
the /.vol location; with the folder swapped for a link before limactl
start the VM read the real folder's files at the project path, and wrote
there; limactl list --json keeps the location and mountPoint as set.
vz serves the share by the path the folder had at the start, so once
the folder is put back the VM gets Operation not permitted (as for any
rename while it runs) until its next start, and replacing that path with
a link while it runs gives the same: never the decoy.

The Lima backend alone decides it: projectMount (vz with a recorded
folder: by inode; otherwise, and on QEMU/9p, by path), used by CreateVM
(the image's vmType, which its clone has) and Start's wantEdits, which
now compares the project mount whole (location, path in the VM,
writable) through keptMounts, so a start edits nothing when they match
and limactl list's filled-in fields never cause an edit. Instance.Project
is the mount's path in the VM, so devvm's comparisons, devvm ls, devvm
ports and .git protection keep using the host path. mount_comma checks
the path in the VM too. platform.VolPath parses a FolderID. The checks
from the nested-replaced fix stay, as defence in depth and for Linux,
where 9p has no equivalent.

Docs: internals/lima.md (the mount, the compare), internals/overview.md
(what's checked and what stays open per OS), safety/attack-vectors.md
(the residual gap is Linux's only), using/vms.md.

Tests: TestVolPath (parsing; on macOS the path opens a moved folder),
TestProjectMountByInode (wantEdits on vz: as it is, another inode, moved,
a comma, a path mount before, no folder recorded, --clone; QEMU by path;
Instance.Project), TestCreateVMByInode (the clone's mount, no mount
edit), TestUp (spec and start carry the state's folder), TestUpClone
(none); e2e's project section: the folder swapped as limactl start runs
and put back when it returns, through the parent's VM (macOS: the VM
never sees the decoy, its mount is /.vol/<dev>/<ino> at the path;
Linux: noted), and the next start runs no limactl edit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 08:13:01 -07:00

92 lines
2.7 KiB
Go

//go:build e2e
package e2e
import (
"errors"
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"
)
// What the images, tools and clone sections read of the sandbox and its
// VMs.
// RunLogs is devvm's run logs by name, which starts with the second each
// run began (logs/runs/20261103-062206-PID.log): oldest first, but for
// runs that began in the same second (NewestLog goes by their times).
func (p Paths) RunLogs() []string {
m, _ := filepath.Glob(filepath.Join(p.Devvm, "logs", "runs", "*.log"))
return m
}
// RunLogHas is whether one of devvm's run logs has line, a whole line
// (grep -qx in logs/runs/*.log): what a build's script printed.
func (p Paths) RunLogHas(line string) bool {
for _, f := range p.RunLogs() {
if b, err := os.ReadFile(f); err == nil && HasLine(string(b), line) {
return true
}
}
return false
}
// MountLine is the host folders the VM's Lima config mounts, each with a
// space after it, as limactl list's template prints them: nothing for a
// VM that mounts none, or none at all. A folder mounted by its device and
// inode (macOS's /.vol, the project folder's) is its path in the VM,
// which is its path here.
func MountLine(s *S, vm string) string {
r, err := s.Limactl("list", "--format", "{{range .Config.Mounts}}{{.Location}}|{{.MountPoint}}\n{{end}}", vm)
if err != nil {
return ""
}
var line string
for _, m := range strings.Split(strings.TrimRight(r.Stdout, "\n"), "\n") {
loc, at, ok := strings.Cut(m, "|")
if !ok {
continue
}
if strings.HasPrefix(loc, "/.vol/") {
loc = at
}
line += loc + " "
}
return line
}
// MountsOf is MountLine's folders, one a line.
func MountsOf(s *S, vm string) string { return strings.Join(strings.Fields(MountLine(s, vm)), "\n") }
// HasLine is whether line is one of text's lines (grep -qx).
func HasLine(text, line string) bool { return slices.Contains(strings.Split(text, "\n"), line) }
// CaptureLine is the first group of the first of text's lines that
// matches re (sed -n 's/re/\1/p' | head -1): nothing when none does.
func CaptureLine(text, re string) string {
if m := regexp.MustCompile("(?m)" + re).FindStringSubmatch(text); len(m) > 1 {
return m[1]
}
return ""
}
// gitTool and sshTool are host git and ssh, with what installs them.
func gitTool() tool { return tool{"git", "git", "git", "git"} }
func sshTool() tool { return tool{"ssh", "openssh", "openssh-client", "openssh-clients"} }
// ExitStatus is the exit status a command's error says (Run's), 0 for
// none and -1 for one that never ran.
func ExitStatus(err error) int {
var ee *exec.ExitError
switch {
case err == nil:
return 0
case errors.As(err, &ee):
return ee.ExitCode()
}
return -1
}