Files
w3cjandClaude Opus 5.5 aed6424c09 fix(proxy): a stale proxy.confined no longer hides proxy_old
run/proxy.confined said how the child was confined, but not which broker
said it. After a downgrade, an older devvm starts the VM's proxy as one
process and never writes the file, so the newer devvm's file stayed and
devvm doctor took the older devvm's unconfined proxy for a confined one.

The broker now writes a second line naming itself, its pid and its start
as the system tells it (proxy.BrokerID, through IO.Started), and
ReadConfined takes the file only when it names the running broker, which
doctor finds by the pid file and platform.ProcessStart (kern.proc.pid's
p_starttime on macOS, /proc's starttime on Linux). A file that names
another broker, or none, is one that hasn't said, so another binary's
proxy gets proxy_old, and when doctor can't read the running broker's
start it says it can't tell (proxy_confinement_unknown). The e2e broker
check reads the file's first line and checks the second names
run/proxy.pid's broker.

Tests: TestUnconfinedStaleMarker, TestReadConfined, TestProcessStart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-18 23:07:04 -07:00

211 lines
8.3 KiB
Go

package proxy
import (
"errors"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
"testing/synctest"
"time"
"github.com/devvmhq/devvm/internal/landlock"
)
// A confinement reads back as it was written, and a line that isn't one of the fixed words, in
// their order, with a why that fits its level, is refused.
func TestParseConfinement(t *testing.T) {
t.Parallel()
for _, c := range []Confinement{
{Level: ConfinedFull, By: BySeatbelt},
unconfined(WhyUnsupported),
unconfined(WhySandboxMissing),
unconfined(WhySandboxFailed),
{Level: ConfinedNone, By: BySeatbelt, Why: WhyCheckFailed},
{Level: ConfinedPartial, By: BySeatbelt, Why: WhyCheckFailed},
} {
got, err := ParseConfinement(c.String() + "\n")
if err != nil || got != c {
t.Errorf("%q: %+v, %v", c, got, err)
}
}
for _, bad := range []string{
"", "none", "full", "full unsupported", "partial", "partial unsupported", "all seatbelt",
"full seatbelt extra", "none \x1b[31m", "none unsupported", "none unsupported seatbelt",
"seatbelt full", "full\tseatbelt",
} {
if c, err := ParseConfinement(bad); err == nil {
t.Errorf("%q: taken as %+v", bad, c)
}
}
}
// The check holds when opening the probe is refused for want of permission, and fails when it
// opens, or fails another way.
func TestCheckConfined(t *testing.T) {
t.Parallel()
if os.Geteuid() == 0 {
t.Skip("root opens any file")
}
dir := t.TempDir()
open, closed := filepath.Join(dir, "open"), filepath.Join(dir, "closed")
must(t, os.WriteFile(open, []byte("{}"), 0o600))
must(t, os.WriteFile(closed, []byte("{}"), 0))
if err := checkConfined(closed); err != nil {
t.Errorf("refused: %v", err)
}
if err := checkConfined(open); err == nil {
t.Error("opened: held")
}
if err := checkConfined(filepath.Join(dir, "gone")); !errors.Is(err, fs.ErrNotExist) {
t.Errorf("missing: %v", err)
}
// A child started under a sandbox keeps it only when the check holds; one started unconfined
// stays so, unchecked, and so does one confined by the seccomp filter alone, which leaves its
// files open.
asSpawned := func(spawned Confinement, _ ChildNeeds) (Confinement, error) { return spawned, nil }
confine := func(spawned Confinement, probe string) Confinement {
t.Helper()
c, err := confineWith(asSpawned, spawned, ChildNeeds{}, probe)
must(t, err)
return c
}
sb := Confinement{Level: ConfinedFull, By: BySeatbelt}
if got := confine(sb, closed); got != sb {
t.Errorf("held: %+v", got)
}
if got, want := confine(sb, open), (Confinement{Level: ConfinedNone, By: BySeatbelt, Why: WhyCheckFailed}); got != want {
t.Errorf("didn't hold: %+v", got)
}
if got := confine(unconfined(WhySandboxMissing), open); got != unconfined(WhySandboxMissing) {
t.Errorf("unconfined: %+v", got)
}
ll := Confinement{Level: ConfinedFull, By: ByLandlock}
if got, want := confine(ll, open), (Confinement{Level: ConfinedNone, By: ByLandlock, Why: WhyCheckFailed}); got != want {
t.Errorf("Landlock that didn't hold: %+v", got)
}
sc := Confinement{Level: ConfinedPartial, By: BySeccomp, Why: WhyNoLandlock}
if got := confine(sc, open); got != sc {
t.Errorf("the filter alone: %+v", got)
}
// A confinement in the process that fails fails the child's start.
if _, err := confineWith(func(Confinement, ChildNeeds) (Confinement, error) {
return Confinement{}, errors.New("landlock_restrict_self: EINVAL")
}, sb, ChildNeeds{}, closed); err == nil {
t.Error("a failure: no error")
}
}
// A confinement in the process that takes longer than confineWait fails the start rather than hang
// it.
func TestConfineWithin(t *testing.T) {
t.Parallel()
synctest.Test(t, func(t *testing.T) {
release := make(chan struct{})
began := time.Now()
_, err := confineWith(func(s Confinement, _ ChildNeeds) (Confinement, error) { <-release; return s, nil }, unconfined(WhyUnsupported), ChildNeeds{}, "")
if err == nil || time.Since(began) != confineWait {
t.Errorf("a confinement that hangs: %v after %s", err, time.Since(began))
}
close(release)
})
}
// A Linux child's confinement, by what Landlock and the seccomp filter gave it: fully from ABI 9
// with its signals scoped, partly naming what's missing first, and not at all without either.
func TestLandlockConfinement(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
r landlock.Result
want string
}{
{landlock.Result{}, "none no-landlock-or-seccomp"},
{landlock.Result{Seccomp: true}, "partial seccomp no-landlock"},
{landlock.Result{ABI: 9, SignalsScoped: true}, "partial landlock no-seccomp"},
{landlock.Result{ABI: 3, Seccomp: true}, "partial landlock landlock-below-abi4"},
{landlock.Result{ABI: 5, Seccomp: true}, "partial landlock landlock-signals"},
{landlock.Result{ABI: 7, Seccomp: true}, "partial landlock landlock-signals"},
{landlock.Result{ABI: 6, SignalsScoped: true, Seccomp: true}, "full landlock"},
{landlock.Result{ABI: 8, SignalsScoped: true, Seccomp: true}, "full landlock"},
{landlock.Result{ABI: 9, SignalsScoped: true, Seccomp: true}, "full landlock"},
{landlock.Result{ABI: 12, SignalsScoped: true, Seccomp: true}, "full landlock"},
} {
c := landlockConfinement(tc.r)
if c.String() != tc.want {
t.Errorf("%+v: %q, want %q", tc.r, c, tc.want)
}
if back, err := ParseConfinement(c.String()); err != nil || back != c {
t.Errorf("%q reads back as %+v, %v", c, back, err)
}
}
}
// ReadConfined reads what the running broker wrote beside its config, and nothing that isn't a
// confinement. A file that names another broker, by its pid or its start, or none, or read
// without the running broker's start, is one that hasn't said, as before the broker writes it.
func TestReadConfined(t *testing.T) {
t.Parallel()
config := filepath.Join(t.TempDir(), "proxy.json")
running := BrokerID{PID: 4242, Start: "1700000000.000042"}
if _, err := ReadConfined(config, running); !errors.Is(err, fs.ErrNotExist) {
t.Errorf("none written: %v", err)
}
must(t, writeAtomic(ConfinedFile(config), confinedFile(Confinement{Level: ConfinedFull, By: BySeatbelt}, running), 0o600))
if b, err := os.ReadFile(ConfinedFile(config)); err != nil || string(b) != "full seatbelt\n4242 1700000000.000042\n" {
t.Errorf("the file = %q, %v", b, err)
}
if c, err := ReadConfined(config, running); err != nil || c != (Confinement{Level: ConfinedFull, By: BySeatbelt}) {
t.Errorf("full seatbelt: %+v, %v", c, err)
}
for _, other := range []BrokerID{{PID: 4243, Start: running.Start}, {PID: running.PID, Start: "1700000000.000043"}, {PID: running.PID}} {
if _, err := ReadConfined(config, other); !errors.Is(err, fs.ErrNotExist) {
t.Errorf("read for %+v: %v", other, err)
}
}
for _, file := range []string{"full seatbelt\n", "full seatbelt\n4242 1700000000.000042", "full seatbelt\n4242 1700000000.000042\nmore\n"} {
must(t, writeAtomic(ConfinedFile(config), []byte(file), 0o600))
if _, err := ReadConfined(config, running); !errors.Is(err, fs.ErrNotExist) {
t.Errorf("%q: %v", file, err)
}
}
must(t, writeAtomic(ConfinedFile(config), []byte("full \x1b[2J\n4242 1700000000.000042\n"), 0o600))
if _, err := ReadConfined(config, running); err == nil {
t.Error("a line of other words: taken")
}
}
// limitedChild, set in the environment, makes TestLimitFiles the process it starts, which
// lowers its own limit on open files.
const limitedChild = "DEVVM_LIMIT_FILES_TEST_CHILD"
// The child lowers its limit on open files, soft and hard, to childFilesMax, and can't raise it
// again; this test's binary, run again, does it, as it would lower the test's own process's.
func TestLimitFiles(t *testing.T) {
if os.Getenv(limitedChild) != "" {
must(t, limitFiles(childFilesMax))
var l syscall.Rlimit
must(t, syscall.Getrlimit(syscall.RLIMIT_NOFILE, &l))
if l.Max > childFilesMax || l.Cur > l.Max {
t.Fatalf("the limit: %+v", l)
}
l.Max++
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &l); err == nil {
t.Fatal("raised again")
}
// A limit already lower stays as it is.
must(t, limitFiles(childFilesMax))
return
}
t.Parallel()
self, err := os.Executable()
must(t, err)
cmd := exec.CommandContext(t.Context(), self, "-test.run=^TestLimitFiles$", "-test.v") //nolint:gosec // the test binary itself
cmd.Env = append(os.Environ(), limitedChild+"=1")
if out, err := cmd.CombinedOutput(); err != nil || !strings.Contains(string(out), "--- PASS: TestLimitFiles") {
t.Errorf("%v: %s", err, out)
}
}