carlos / platform Public

devbox on macOS (#488) review fixes: env file values as written, no FIFO hang; fileacl

From Astra's whole-branch review (xhigh), the two P2s and the groundwork
for the P1:

- Values are everything after the first '=' exactly as written; only a
  CRLF file's \r is dropped. Blank and comment lines are judged by their
  first byte that is not a space or tab. (Was: the whole line trimmed,
  so 'TOKEN=abc ' became 'abc', against DBX-C60.)
- The env file opens with O_NONBLOCK on unix, so a FIFO (or a symlink to
  one) with no writer is refused by the regular-file check at once
  instead of hanging the open. Symlinks are still followed (no
  O_NOFOLLOW): every check is on the file the open reached.
- internal/fileacl: Extended(f) reports an extended ACL with one or more
  entries on darwin (acl_get_fd, acl_get_entry, acl_free from libSystem,
  no cgo, x/sys/unix's trampoline form; syscallPtr for the NULL return).
  Elsewhere it reports none: on Linux a POSIX ACL's mask is the mode's
  group bits, which the 0600 check already bounds. Not wired into the
  loader yet: its refusal needs a new deck row.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 6faa42f05dbdff67c7479a0161edc6eb85be646e parent d40e0be
11 files changed, +298 −6
  • devbox_cli_test.go +72 −0
  • devbox_envfile.go +12 −6
  • devbox_envfile_other.go +3 −0
  • devbox_envfile_unix.go +10 −0
  • internal/fileacl/fileacl.go +15 −0
  • internal/fileacl/fileacl_darwin.go +76 −0
  • internal/fileacl/fileacl_darwin_amd64.s +19 −0
  • internal/fileacl/fileacl_darwin_arm64.s +19 −0
  • internal/fileacl/fileacl_darwin_test.go +40 −0
  • internal/fileacl/fileacl_other.go +8 −0
  • internal/fileacl/fileacl_other_test.go +24 −0

Intent

Human

<task-notification> <task-id>adb6e89c9529fd992</task-id> <tool-use-id>toolu_01UkmJsuy9QL6PGqHaiFqNDV</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/9b370a04-693c-4bda-b2eb-9a4b08f7626d/tasks/adb6e89c9529fd992.output</output-file> <status>completed</status> <summary>Agent "Build devbox macOS support (#488)" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE_WITH_CONCERNS. Tasks 1 to 4 are built and committed; nothing is pushed. Commits on devbox-macos (base 6c6cde48): - `6d47d84a` copy rows (now approved) - `2f14384a` `--env-file` and the darwin socket default - `f73c96f4` launchd socket handoff, LOCAL_PEERCRED, edge `Listener` - `d40e0bea` plist and runbook macOS section; DBX-C58 to C92 approved and added to `devboxCopyBuilt` Tests: the touched Linux tests pass, each with a RED run first, and the copy gate passes. Darwin arm64 and amd64 vet and test compile pass for `.`, `internal/devbox` and `internal/launchd`, and the darwin, windows and linux/arm64 builds pass. The darwin binary imports `launch_activate_socket` and `free` with no cgo. Copy rows: DBX-C58 to DBX-C92, all approved and built. Concerns: 1. Nothing has run on a Mac yet. Before installing, run `go test ./internal/launchd ./internal/devbox` and `plutil -lint` on the plist there, and check the runbook's dscl, install, sed and lsof commands. 2. `GOOS=darwin go vet ./internal/edge`, from the plan's list, fails on main already: `readerConn` is defined in a test file that only builds on Linux. It is not from this branch, and CI's cross-build still passes. 3. The launchd and peer code was written before its tests, since those tests only run on darwin. Report: /home/paulca/.cache/tmp/devbox-macos-report.md</result> <usage><subagent_tokens>188618</subagent_tokens><tool_uses>66</tool_uses><duration_ms>1775744</duration_ms></usage> </task-notification>

Human

<task-notification> <task-id>bhu02yjm8</task-id> <tool-use-id>toolu_01Bp1JfY5yob9niWi6omCs4z</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/9b370a04-693c-4bda-b2eb-9a4b08f7626d/tasks/bhu02yjm8.output</output-file> <status>completed</status> <summary>Background command "Run Astra whole-branch review of #488" completed (exit code 0)</summary> </task-notification>

Attributed to this commit: 19.8 g CO₂e (2,587,873 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on devbox-macos, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/devbox_cli_test.go b/devbox_cli_test.go
index 057c51441..d0b3f241c 100644
--- a/devbox_cli_test.go
+++ b/devbox_cli_test.go
@@ -545,6 +545,78 @@ func TestDevboxServeEnvFileLoads(t *testing.T) {
}
}
+// Values are kept exactly as written after the first '=': trailing spaces
+// and whitespace-only values survive. Only a CRLF file's \r is dropped, and
+// blank and comment lines are told apart by their first non-space byte.
+func TestDevboxServeEnvFileKeepsValues(t *testing.T) {
+ for _, k := range []string{"DBX_TEST_A", "DBX_TEST_B", "DBX_TEST_C", "DBX_TEST_D", "DBX_TEST_E"} {
+ t.Setenv(k, "")
+ }
+ body := "DBX_TEST_A=abc \n" + // trailing spaces
+ "DBX_TEST_B= \n" + // whitespace only
+ "DBX_TEST_C= lead\ttab\t\r\n" + // CRLF; the tab before it stays
+ " \t\r\n" + // a blank line, CRLF
+ " # a comment = with an equals sign\r\n" +
+ "\tDBX_TEST_D=x # y\n" +
+ "DBX_TEST_E=no newline at the end "
+ p := writeDevboxEnvFile(t, body, 0o600)
+ if err := loadDevboxEnvFile(p, os.Geteuid()); err != nil {
+ t.Fatalf("load = %v", err)
+ }
+ for k, want := range map[string]string{
+ "DBX_TEST_A": "abc ",
+ "DBX_TEST_B": " ",
+ "DBX_TEST_C": " lead\ttab\t",
+ "DBX_TEST_D": "x # y",
+ "DBX_TEST_E": "no newline at the end ",
+ } {
+ if got := os.Getenv(k); got != want {
+ t.Errorf("%s = %q, want %q", k, got, want)
+ }
+ }
+}
+
+// A FIFO, named directly or through a symlink, is refused at once: the
+// open does not wait for a writer that never comes.
+func TestDevboxServeEnvFileRefusesFIFO(t *testing.T) {
+ dir := t.TempDir()
+ fifo := filepath.Join(dir, "fifo")
+ if err := syscall.Mkfifo(fifo, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ link := filepath.Join(dir, "link")
+ if err := os.Symlink(fifo, link); err != nil {
+ t.Fatal(err)
+ }
+ for _, p := range []string{fifo, link} {
+ done := make(chan error, 1)
+ go func() { done <- loadDevboxEnvFile(p, os.Geteuid()) }()
+ select {
+ case err := <-done:
+ want := "could not read the environment file " + p + ". check that it exists and that this account can read it\n"
+ if err == nil || !strings.HasPrefix(err.Error(), want) {
+ t.Errorf("%s: load = %v, want %q then the system's words", p, err, want)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatalf("%s: load is still waiting on the FIFO", p)
+ }
+ }
+}
+
+// A symlink to a proper env file is followed: the checks are on the file
+// it names.
+func TestDevboxServeEnvFileFollowsSymlink(t *testing.T) {
+ p := writeDevboxEnvFile(t, "DBX_TEST_A=one\n", 0o600)
+ link := filepath.Join(t.TempDir(), "env")
+ if err := os.Symlink(p, link); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("DBX_TEST_A", "")
+ if err := loadDevboxEnvFile(link, os.Geteuid()); err != nil || os.Getenv("DBX_TEST_A") != "one" {
+ t.Fatalf("load via symlink = %v (DBX_TEST_A %q)", err, os.Getenv("DBX_TEST_A"))
+ }
+}
+
func TestDevboxServeEnvFileRefusesOpenMode(t *testing.T) {
for _, perm := range []os.FileMode{0o640, 0o604, 0o620, 0o602, 0o601, 0o660} {
p := writeDevboxEnvFile(t, "DBX_TEST_A=one\n", perm)
diff --git a/devbox_envfile.go b/devbox_envfile.go
index 80fcd6d9c..0105da086 100644
--- a/devbox_envfile.go
+++ b/devbox_envfile.go
@@ -8,8 +8,10 @@ package main
//
// The rules: the file belongs to the user the service runs as, and no other
// account may read or change it; every line is KEY=VALUE, a # comment or
-// blank; a key appears once; and a setting in the file replaces one already
-// in the environment. Nothing is set unless the whole file passes. No
+// blank (judged by its first byte that is not a space or tab); a key
+// appears once; a value is everything after the first '=' exactly as
+// written, less a CRLF file's \r; and a setting in the file replaces one
+// already in the environment. Nothing is set unless the whole file passes. No
// refusal repeats a value, or any part of a line that is not a setting.
import (
@@ -27,7 +29,7 @@ const devboxEnvFileMax = 1 << 20
// process's environment. euid is the user the file must belong to (the
// caller passes os.Geteuid(); a test passes another).
func loadDevboxEnvFile(path string, euid int) error {
- f, err := os.Open(path)
+ f, err := openDevboxEnvFile(path)
if err != nil {
return errors.Join(errors.New(devboxEnvUnreadable(path)), err)
}
@@ -61,11 +63,15 @@ func loadDevboxEnvFile(path string, euid int) error {
seen := map[string]int{}
for i, line := range strings.Split(string(raw), "\n") {
n := i + 1
- line = strings.TrimSpace(line) // also drops a CRLF file's \r
- if line == "" || strings.HasPrefix(line, "#") {
+ // A CRLF file's \r is the line ending, not part of the value.
+ // Everything else after the first '=' is the value as written,
+ // spaces and all.
+ line = strings.TrimSuffix(line, "\r")
+ lead := strings.TrimLeft(line, " \t")
+ if lead == "" || strings.HasPrefix(lead, "#") {
continue
}
- key, value, ok := strings.Cut(line, "=")
+ key, value, ok := strings.Cut(lead, "=")
if !ok || !devboxEnvKey(key) {
return errors.New(devboxEnvBadLine(n, path))
}
diff --git a/devbox_envfile_other.go b/devbox_envfile_other.go
index 6624ce8fd..a6defb38a 100644
--- a/devbox_envfile_other.go
+++ b/devbox_envfile_other.go
@@ -4,6 +4,9 @@ package main
import "os"
+// openDevboxEnvFile is a plain open here: there are no FIFOs to wait on.
+func openDevboxEnvFile(path string) (*os.File, error) { return os.Open(path) }
+
// fileOwner has no uid to report here, so no environment file passes the
// owner check: devbox serve runs only on Linux and macOS.
func fileOwner(fi os.FileInfo) (int, bool) { return 0, false }
diff --git a/devbox_envfile_unix.go b/devbox_envfile_unix.go
index 18859f463..ed2d4cb32 100644
--- a/devbox_envfile_unix.go
+++ b/devbox_envfile_unix.go
@@ -7,6 +7,16 @@ import (
"syscall"
)
+// openDevboxEnvFile opens path without blocking: a FIFO with no writer
+// would otherwise hold the open forever, before the regular-file check on
+// the opened file could refuse it. O_NONBLOCK changes nothing for reading
+// a regular file. A symlink is followed on purpose (no O_NOFOLLOW): an env
+// file kept elsewhere and linked into place is legitimate, and every check
+// is on the file the open reached, never on the path.
+func openDevboxEnvFile(path string) (*os.File, error) {
+ return os.OpenFile(path, os.O_RDONLY|syscall.O_NONBLOCK, 0)
+}
+
// fileOwner is the uid that owns the file fi describes.
func fileOwner(fi os.FileInfo) (int, bool) {
st, ok := fi.Sys().(*syscall.Stat_t)
diff --git a/internal/fileacl/fileacl.go b/internal/fileacl/fileacl.go
new file mode 100644
index 000000000..7d408e2d7
--- /dev/null
+++ b/internal/fileacl/fileacl.go
@@ -0,0 +1,15 @@
+// Package fileacl reports whether an open file carries an extended access
+// control list: entries beyond its owner, group and other mode bits that can
+// grant another account access the mode does not show. On macOS an ACL is
+// evaluated before the mode bits, so a 0600 file can still be readable by
+// someone else (devbox mode's env file, #488).
+//
+// Linux is not covered and needs no cover for a mode check: with a POSIX
+// ACL, the group bits of st_mode are the ACL's mask, which bounds every
+// named user and group entry, so a mode with no group or other bits leaves
+// those entries no access. Elsewhere Extended reports none.
+//
+// There is no cgo. On darwin the package calls acl_get_fd(3),
+// acl_get_entry(3) and acl_free(3) in libSystem the way golang.org/x/sys/unix
+// calls libc, as internal/launchd does.
+package fileacl
diff --git a/internal/fileacl/fileacl_darwin.go b/internal/fileacl/fileacl_darwin.go
new file mode 100644
index 000000000..34ce570d3
--- /dev/null
+++ b/internal/fileacl/fileacl_darwin.go
@@ -0,0 +1,76 @@
+//go:build darwin
+
+package fileacl
+
+import (
+ "fmt"
+ "os"
+ "syscall"
+ "unsafe"
+)
+
+// From <sys/acl.h>.
+const aclFirstEntry = 0 // ACL_FIRST_ENTRY
+
+// Extended reports whether f carries an extended ACL with one or more
+// entries. It reads the open descriptor, never the path again. A file
+// system without ACLs has none. Any other failure is an error, so a caller
+// that refuses on error fails closed.
+func Extended(f *os.File) (bool, error) {
+ raw, err := f.SyscallConn()
+ if err != nil {
+ return false, err
+ }
+ var has bool
+ var cerr error
+ if err := raw.Control(func(fd uintptr) { has, cerr = extended(int(fd)) }); err != nil {
+ return false, err
+ }
+ return has, cerr
+}
+
+func extended(fd int) (bool, error) {
+ // acl_t acl_get_fd(int fd): NULL and errno when there is none.
+ acl, _, e := syscall_syscallPtr(libc_acl_get_fd_trampoline_addr, uintptr(fd), 0, 0)
+ if acl == 0 {
+ switch e {
+ case syscall.ENOENT, syscall.ENOTSUP, syscall.EOPNOTSUPP:
+ return false, nil // no extended ACL, or none possible here
+ }
+ return false, fmt.Errorf("fileacl: acl_get_fd: %w", e)
+ }
+ // int acl_get_entry(acl_t acl, int entry_id, acl_entry_t *entry_p):
+ // 0 when there is a first entry. The entry points into acl and is
+ // not freed on its own.
+ var entry uintptr
+ r, _, e := syscall_syscall(libc_acl_get_entry_trampoline_addr, acl, aclFirstEntry, uintptr(unsafe.Pointer(&entry)))
+ // int acl_free(void *obj_p)
+ syscall_syscall(libc_acl_free_trampoline_addr, acl, 0, 0)
+ switch {
+ case int32(r) == 0:
+ return true, nil
+ case e == syscall.EINVAL:
+ return false, nil // an ACL with no entries
+ }
+ return false, fmt.Errorf("fileacl: acl_get_entry: %w", e)
+}
+
+// The syscall package's libc calls, marked for linkname use by x/sys/unix
+// (unix/syscall_darwin_libSystem.go). syscallPtr reports errno when r1 is 0
+// (a NULL pointer); syscall when r1 is -1.
+func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
+func syscall_syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
+
+//go:linkname syscall_syscall syscall.syscall
+//go:linkname syscall_syscallPtr syscall.syscallPtr
+
+// The trampolines are in fileacl_darwin_$GOARCH.s.
+var (
+ libc_acl_get_fd_trampoline_addr uintptr
+ libc_acl_get_entry_trampoline_addr uintptr
+ libc_acl_free_trampoline_addr uintptr
+)
+
+//go:cgo_import_dynamic fileacl_libc_acl_get_fd acl_get_fd "/usr/lib/libSystem.B.dylib"
+//go:cgo_import_dynamic fileacl_libc_acl_get_entry acl_get_entry "/usr/lib/libSystem.B.dylib"
+//go:cgo_import_dynamic fileacl_libc_acl_free acl_free "/usr/lib/libSystem.B.dylib"
diff --git a/internal/fileacl/fileacl_darwin_amd64.s b/internal/fileacl/fileacl_darwin_amd64.s
new file mode 100644
index 000000000..05777366f
--- /dev/null
+++ b/internal/fileacl/fileacl_darwin_amd64.s
@@ -0,0 +1,19 @@
+// The libc trampolines for fileacl_darwin.go, in x/sys/unix's form
+// (unix/zsyscall_darwin_*.s), as internal/launchd has them.
+
+#include "textflag.h"
+
+TEXT libc_acl_get_fd_trampoline<>(SB),NOSPLIT,$0-0
+ JMP fileacl_libc_acl_get_fd(SB)
+GLOBL ·libc_acl_get_fd_trampoline_addr(SB), RODATA, $8
+DATA ·libc_acl_get_fd_trampoline_addr(SB)/8, $libc_acl_get_fd_trampoline<>(SB)
+
+TEXT libc_acl_get_entry_trampoline<>(SB),NOSPLIT,$0-0
+ JMP fileacl_libc_acl_get_entry(SB)
+GLOBL ·libc_acl_get_entry_trampoline_addr(SB), RODATA, $8
+DATA ·libc_acl_get_entry_trampoline_addr(SB)/8, $libc_acl_get_entry_trampoline<>(SB)
+
+TEXT libc_acl_free_trampoline<>(SB),NOSPLIT,$0-0
+ JMP fileacl_libc_acl_free(SB)
+GLOBL ·libc_acl_free_trampoline_addr(SB), RODATA, $8
+DATA ·libc_acl_free_trampoline_addr(SB)/8, $libc_acl_free_trampoline<>(SB)
diff --git a/internal/fileacl/fileacl_darwin_arm64.s b/internal/fileacl/fileacl_darwin_arm64.s
new file mode 100644
index 000000000..05777366f
--- /dev/null
+++ b/internal/fileacl/fileacl_darwin_arm64.s
@@ -0,0 +1,19 @@
+// The libc trampolines for fileacl_darwin.go, in x/sys/unix's form
+// (unix/zsyscall_darwin_*.s), as internal/launchd has them.
+
+#include "textflag.h"
+
+TEXT libc_acl_get_fd_trampoline<>(SB),NOSPLIT,$0-0
+ JMP fileacl_libc_acl_get_fd(SB)
+GLOBL ·libc_acl_get_fd_trampoline_addr(SB), RODATA, $8
+DATA ·libc_acl_get_fd_trampoline_addr(SB)/8, $libc_acl_get_fd_trampoline<>(SB)
+
+TEXT libc_acl_get_entry_trampoline<>(SB),NOSPLIT,$0-0
+ JMP fileacl_libc_acl_get_entry(SB)
+GLOBL ·libc_acl_get_entry_trampoline_addr(SB), RODATA, $8
+DATA ·libc_acl_get_entry_trampoline_addr(SB)/8, $libc_acl_get_entry_trampoline<>(SB)
+
+TEXT libc_acl_free_trampoline<>(SB),NOSPLIT,$0-0
+ JMP fileacl_libc_acl_free(SB)
+GLOBL ·libc_acl_free_trampoline_addr(SB), RODATA, $8
+DATA ·libc_acl_free_trampoline_addr(SB)/8, $libc_acl_free_trampoline<>(SB)
diff --git a/internal/fileacl/fileacl_darwin_test.go b/internal/fileacl/fileacl_darwin_test.go
new file mode 100644
index 000000000..130d5e013
--- /dev/null
+++ b/internal/fileacl/fileacl_darwin_test.go
@@ -0,0 +1,40 @@
+//go:build darwin
+
+package fileacl
+
+import (
+ "os"
+ "os/exec"
+ "path/filepath"
+ "testing"
+)
+
+// A 0600 file has no ACL until chmod +a gives it one, which grants another
+// account read and write that the mode does not show; chmod -N removes it.
+func TestExtendedDarwin(t *testing.T) {
+ p := filepath.Join(t.TempDir(), "env")
+ if err := os.WriteFile(p, []byte("A=1\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ check := func(want bool) {
+ t.Helper()
+ f, err := os.Open(p)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f.Close()
+ got, err := Extended(f)
+ if err != nil || got != want {
+ t.Fatalf("Extended = %v, %v, want %v", got, err, want)
+ }
+ }
+ check(false)
+ if out, err := exec.Command("/bin/chmod", "+a", "everyone allow read,write", p).CombinedOutput(); err != nil {
+ t.Fatalf("chmod +a: %v: %s", err, out)
+ }
+ check(true)
+ if out, err := exec.Command("/bin/chmod", "-N", p).CombinedOutput(); err != nil {
+ t.Fatalf("chmod -N: %v: %s", err, out)
+ }
+ check(false)
+}
diff --git a/internal/fileacl/fileacl_other.go b/internal/fileacl/fileacl_other.go
new file mode 100644
index 000000000..baf5b5502
--- /dev/null
+++ b/internal/fileacl/fileacl_other.go
@@ -0,0 +1,8 @@
+//go:build !darwin
+
+package fileacl
+
+import "os"
+
+// Extended reports no ACL: see the package comment.
+func Extended(f *os.File) (bool, error) { return false, nil }
diff --git a/internal/fileacl/fileacl_other_test.go b/internal/fileacl/fileacl_other_test.go
new file mode 100644
index 000000000..a73373249
--- /dev/null
+++ b/internal/fileacl/fileacl_other_test.go
@@ -0,0 +1,24 @@
+//go:build !darwin
+
+package fileacl
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestExtendedElsewhereReportsNone(t *testing.T) {
+ p := filepath.Join(t.TempDir(), "f")
+ if err := os.WriteFile(p, nil, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ f, err := os.Open(p)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f.Close()
+ if got, err := Extended(f); got || err != nil {
+ t.Fatalf("Extended = %v, %v", got, err)
+ }
+}