carlos / platform Public

Wire Linux devbox identity and service socket ACLs

Configure cached Tailscale identity and registrar hooks on Linux while
preserving the existing Darwin serve configuration. Prepare the private
socket directory, add POSIX ACL helpers, and preserve runtime sockets
across service stops.

Cover wiring, directory refusals, ACL encoding and kernel read-back.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 4edb2b392c82f2b78609a91c995f9755aedba6f4 parent d07e7ad
6 files changed, +406 −0
  • devbox_acl_linux.go +46 −0
  • devbox_acl_linux_test.go +105 −0
  • devbox_acl_other.go +9 −0
  • devbox_cli.go +69 −0
  • devbox_identity_linux_test.go +175 −0
  • infra/devbox/carlos-devbox.service +2 −0

CI failed — run details

diff --git a/devbox_acl_linux.go b/devbox_acl_linux.go
new file mode 100644
index 000000000..621dac834
--- /dev/null
+++ b/devbox_acl_linux.go
@@ -0,0 +1,46 @@
+//go:build linux
+
+package main
+
+import (
+ "encoding/binary"
+ "os"
+
+ "golang.org/x/sys/unix"
+)
+
+func grantServiceACL(path string, serviceUID uint32) error {
+ if err := os.Chmod(path, 0o600); err != nil {
+ return err
+ }
+ return unix.Setxattr(path, "system.posix_acl_access", devboxServiceACL(serviceUID), 0)
+}
+
+func devboxServiceACL(serviceUID uint32) []byte {
+ const (
+ aclUserObj = 0x01
+ aclUser = 0x02
+ aclGroupObj = 0x04
+ aclMask = 0x10
+ aclOther = 0x20
+ )
+ entries := [...]struct {
+ tag, perm uint16
+ id uint32
+ }{
+ {aclUserObj, 6, 0xffffffff},
+ {aclUser, 6, serviceUID},
+ {aclGroupObj, 0, 0xffffffff},
+ {aclMask, 6, 0xffffffff},
+ {aclOther, 0, 0xffffffff},
+ }
+ data := make([]byte, 4+8*len(entries))
+ binary.LittleEndian.PutUint32(data, 2)
+ for i, entry := range entries {
+ offset := 4 + 8*i
+ binary.LittleEndian.PutUint16(data[offset:], entry.tag)
+ binary.LittleEndian.PutUint16(data[offset+2:], entry.perm)
+ binary.LittleEndian.PutUint32(data[offset+4:], entry.id)
+ }
+ return data
+}
diff --git a/devbox_acl_linux_test.go b/devbox_acl_linux_test.go
new file mode 100644
index 000000000..1de320bec
--- /dev/null
+++ b/devbox_acl_linux_test.go
@@ -0,0 +1,105 @@
+//go:build linux
+
+package main
+
+import (
+ "encoding/binary"
+ "errors"
+ "net"
+ "os"
+ "os/user"
+ "path/filepath"
+ "reflect"
+ "strconv"
+ "testing"
+
+ "golang.org/x/sys/unix"
+)
+
+type devboxACLEntry struct {
+ tag, perm uint16
+ id uint32
+}
+
+func decodeDevboxACL(t *testing.T, data []byte) []devboxACLEntry {
+ t.Helper()
+ if len(data) != 44 || binary.LittleEndian.Uint32(data[:4]) != 2 {
+ t.Fatalf("ACL header/length = %x", data)
+ }
+ var entries []devboxACLEntry
+ for offset := 4; offset < len(data); offset += 8 {
+ entries = append(entries, devboxACLEntry{binary.LittleEndian.Uint16(data[offset:]), binary.LittleEndian.Uint16(data[offset+2:]), binary.LittleEndian.Uint32(data[offset+4:])})
+ }
+ return entries
+}
+
+func TestGrantServiceACLEncodes(t *testing.T) {
+ for _, uid := range []uint32{0, 1001, 0x12345678} {
+ want := []devboxACLEntry{{1, 6, 0xffffffff}, {2, 6, uid}, {4, 0, 0xffffffff}, {16, 6, 0xffffffff}, {32, 0, 0xffffffff}}
+ if got := decodeDevboxACL(t, devboxServiceACL(uid)); !reflect.DeepEqual(got, want) {
+ t.Fatalf("ACL = %+v, want %+v", got, want)
+ }
+ }
+}
+
+func TestGrantServiceACLSocket(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "acl.sock")
+ ln, err := net.Listen("unix", path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer ln.Close()
+ if err := os.Chmod(path, 0o777); err != nil {
+ t.Fatal(err)
+ }
+ err = grantServiceACL(path, uint32(os.Getuid()))
+ if errors.Is(err, unix.EOPNOTSUPP) || errors.Is(err, unix.ENOSYS) {
+ fi, statErr := os.Stat(path)
+ if statErr != nil || fi.Mode().Perm() != 0o600 {
+ t.Fatalf("unsupported ACL left permissions open: %v, %v", fi, statErr)
+ }
+ t.Skipf("socket ACL unsupported: %v", err)
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ data := make([]byte, 256)
+ n, err := unix.Getxattr(path, "system.posix_acl_access", data)
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := decodeDevboxACL(t, devboxServiceACL(uint32(os.Getuid())))
+ if got := decodeDevboxACL(t, data[:n]); !reflect.DeepEqual(got, want) {
+ t.Fatalf("read-back ACL = %+v, want %+v", got, want)
+ }
+ fi, err := os.Stat(path)
+ if err != nil || fi.Mode().Perm() != 0o660 {
+ t.Fatalf("mode with ACL mask = %v, %v", fi, err)
+ }
+}
+
+func TestDevboxServiceUID(t *testing.T) {
+ u, err := user.LookupId(strconv.Itoa(os.Getuid()))
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("CARLOS_DEVBOX_SERVICE_USER", u.Username)
+ uid, err := devboxServiceUID()
+ if err != nil || uid != uint32(os.Getuid()) {
+ t.Fatalf("service UID = %d, %v", uid, err)
+ }
+ t.Setenv("CARLOS_DEVBOX_SERVICE_USER", "carlos-devbox-test-nonexistent-user")
+ if _, err := devboxServiceUID(); err == nil {
+ t.Fatal("missing service account accepted")
+ }
+ t.Setenv("CARLOS_DEVBOX_SERVICE_USER", "")
+ want, lookupErr := user.Lookup("carlos-devbox")
+ uid, err = devboxServiceUID()
+ if lookupErr != nil {
+ if err == nil {
+ t.Fatal("missing default service account accepted")
+ }
+ } else if err != nil || strconv.FormatUint(uint64(uid), 10) != want.Uid {
+ t.Fatalf("default service UID = %d, %v; want %s", uid, err, want.Uid)
+ }
+}
diff --git a/devbox_acl_other.go b/devbox_acl_other.go
new file mode 100644
index 000000000..ee744b687
--- /dev/null
+++ b/devbox_acl_other.go
@@ -0,0 +1,9 @@
+//go:build !linux
+
+package main
+
+import "errors"
+
+func grantServiceACL(path string, serviceUID uint32) error {
+ return errors.ErrUnsupported
+}
diff --git a/devbox_cli.go b/devbox_cli.go
index 529796fa4..a266df938 100644
--- a/devbox_cli.go
+++ b/devbox_cli.go
@@ -38,6 +38,7 @@ import (
"github.com/carlosframework/platform/internal/edge"
"github.com/carlosframework/platform/internal/launchd"
"github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/tsidentity"
)
// Copy deck rows (DBX-Cn). Each is the row's text, byte for byte.
@@ -662,6 +663,9 @@ func runDevboxServe(args []string) error {
LookupUser: lookupUsername,
}
cfg := edge.DevboxConfig{Registry: reg, ListenAddr: listen, Listener: given, CertDir: filepath.Join(dataDir, "certs"), SpoolRoot: spool}
+ if err := configureDevboxIdentity(runtime.GOOS, r, &cfg); err != nil {
+ return fmt.Errorf("devbox serve: %w", err)
+ }
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer stop()
@@ -670,6 +674,71 @@ func runDevboxServe(args []string) error {
})
}
+func configureDevboxIdentity(goos string, r *devbox.Registrar, cfg *edge.DevboxConfig) error {
+ if goos != "linux" {
+ return nil
+ }
+ dir := os.Getenv("CARLOS_DEVBOX_SOCKETS_DIR")
+ if dir == "" {
+ dir = devbox.DefaultSocketsDir
+ }
+ if err := prepareDevboxSocketsDir(dir, os.Geteuid()); err != nil {
+ return err
+ }
+ r.SocketsDir = dir
+ socket := os.Getenv("CARLOS_DEVBOX_TAILSCALED_SOCKET")
+ if socket == "" {
+ socket = "/var/run/tailscale/tailscaled.sock"
+ }
+ cfg.Identity = edge.NewCachedIdentity(tsidentity.New(socket, 2*time.Second), time.Now)
+ for _, node := range strings.Split(os.Getenv("CARLOS_DEVBOX_SHARED_NODES"), ",") {
+ if node = strings.TrimSpace(node); node != "" {
+ cfg.SharedNodes = append(cfg.SharedNodes, node)
+ }
+ }
+ cfg.AllowFor = r.AllowFor
+ cfg.SocketOwnerFor = r.SocketOwnerFor
+ return nil
+}
+
+func prepareDevboxSocketsDir(path string, uid int) error {
+ const mode = os.ModeSticky | 0o733
+ mkdirErr := os.Mkdir(path, mode)
+ if mkdirErr != nil && !errors.Is(mkdirErr, os.ErrExist) {
+ return mkdirErr
+ }
+ fi, err := os.Lstat(path)
+ if err != nil {
+ return err
+ }
+ if fi.Mode()&os.ModeSymlink != 0 {
+ return &os.PathError{Op: "lstat", Path: path, Err: syscall.ELOOP}
+ }
+ if !fi.IsDir() {
+ return &os.PathError{Op: "lstat", Path: path, Err: syscall.ENOTDIR}
+ }
+ if owner, ok := fileOwner(fi); !ok || owner != uid {
+ return &os.PathError{Op: "lstat", Path: path, Err: os.ErrPermission}
+ }
+ if mkdirErr == nil || fi.Mode() != os.ModeDir|mode {
+ return os.Chmod(path, mode)
+ }
+ return nil
+}
+
+func devboxServiceUID() (uint32, error) {
+ name := os.Getenv("CARLOS_DEVBOX_SERVICE_USER")
+ if name == "" {
+ name = "carlos-devbox"
+ }
+ u, err := user.Lookup(name)
+ if err != nil {
+ return 0, err
+ }
+ uid, err := strconv.ParseUint(u.Uid, 10, 32)
+ return uint32(uid), err
+}
+
// devboxLaunchdListener takes the one listening socket launchd opened for
// the plist's Sockets entry called name, refusing in the deck's words what
// it cannot serve on. take is launchd.Listeners (a test replaces it).
diff --git a/devbox_identity_linux_test.go b/devbox_identity_linux_test.go
new file mode 100644
index 000000000..799022207
--- /dev/null
+++ b/devbox_identity_linux_test.go
@@ -0,0 +1,175 @@
+//go:build linux
+
+package main
+
+import (
+ "context"
+ "errors"
+ "net"
+ "net/http"
+ "net/netip"
+ "os"
+ "path/filepath"
+ "reflect"
+ "sync/atomic"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/devbox"
+ "github.com/carlosframework/platform/internal/edge"
+)
+
+func TestDevboxServeIdentityWiring(t *testing.T) {
+ dir := t.TempDir()
+ sockets := filepath.Join(dir, "sockets")
+ t.Setenv("CARLOS_DEVBOX_SOCKETS_DIR", sockets)
+ t.Setenv("CARLOS_DEVBOX_SHARED_NODES", " , shared-one,shared-two , ,")
+ sock := filepath.Join(dir, "ts.sock")
+ t.Setenv("CARLOS_DEVBOX_TAILSCALED_SOCKET", sock)
+ ln, err := net.Listen("unix", sock)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var calls atomic.Int32
+ srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ calls.Add(1)
+ if r.URL.Path != "/localapi/v0/whois" || r.URL.Query().Get("addr") != "100.64.0.2:1234" {
+ t.Errorf("whois request = %s", r.URL)
+ }
+ _, _ = w.Write([]byte(`{"Node":{"ComputedName":"laptop"},"UserProfile":{"LoginName":"alice@github","DisplayName":"Alice"}}`))
+ })}
+ go srv.Serve(ln)
+ t.Cleanup(func() { srv.Close() })
+ links, err := devbox.OpenLinks(filepath.Join(dir, "links.json"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ r := &devbox.Registrar{Links: links, Domain: "devbox.example.com"}
+ cfg := edge.DevboxConfig{ListenAddr: "127.0.0.1:8443"}
+ if err := configureDevboxIdentity("linux", r, &cfg); err != nil {
+ t.Fatal(err)
+ }
+ if r.SocketsDir != sockets || cfg.Identity == nil || cfg.AllowFor == nil || cfg.SocketOwnerFor == nil {
+ t.Fatalf("incomplete wiring: registrar %+v, config %+v", r, cfg)
+ }
+ if !reflect.DeepEqual(cfg.SharedNodes, []string{"shared-one", "shared-two"}) || cfg.ListenAddr != "127.0.0.1:8443" {
+ t.Fatalf("config = %+v", cfg)
+ }
+ for _, port := range []uint16{1234, 5678} {
+ visitor, ok, err := cfg.Identity.Who(context.Background(), netip.AddrPortFrom(netip.MustParseAddr("100.64.0.2"), port))
+ if err != nil || !ok || visitor != (edge.Visitor{Login: "alice@github", Name: "Alice", Node: "laptop"}) {
+ t.Fatalf("Who = %+v, %v, %v", visitor, ok, err)
+ }
+ }
+ if calls.Load() != 1 {
+ t.Fatalf("LocalAPI calls = %d, want cached answer", calls.Load())
+ }
+ link := devbox.Link{Name: "shop", Kind: devbox.KindSocket, OwnerUID: 1234, Socket: filepath.Join(sockets, "shop.sock"), Allow: []string{"alice@github"}}
+ for _, allow := range [][]string{{"alice@github"}, {"bob@github"}} {
+ link.Allow = allow
+ if err := links.Put(link); err != nil {
+ t.Fatal(err)
+ }
+ if got := cfg.AllowFor("shop.devbox.example.com"); !reflect.DeepEqual(got, allow) {
+ t.Fatalf("AllowFor = %v, want %v", got, allow)
+ }
+ path, uid, ok := cfg.SocketOwnerFor("shop.devbox.example.com")
+ if !ok || path != link.Socket || uid != link.OwnerUID {
+ t.Fatalf("SocketOwnerFor = %q, %d, %v", path, uid, ok)
+ }
+ }
+}
+
+func TestDevboxServeDarwinIdentityUnchanged(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "sockets")
+ t.Setenv("CARLOS_DEVBOX_SOCKETS_DIR", path)
+ t.Setenv("CARLOS_DEVBOX_SHARED_NODES", "shared-one")
+ t.Setenv("CARLOS_DEVBOX_TAILSCALED_SOCKET", "/nonexistent")
+ r := &devbox.Registrar{}
+ cfg := edge.DevboxConfig{}
+ if err := configureDevboxIdentity("darwin", r, &cfg); err != nil {
+ t.Fatal(err)
+ }
+ if r.SocketsDir != "" || cfg.Identity != nil || cfg.SharedNodes != nil || cfg.AllowFor != nil || cfg.SocketOwnerFor != nil {
+ t.Fatalf("darwin identity enabled: %+v, %+v", r, cfg)
+ }
+ if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("darwin sockets directory: %v", err)
+ }
+}
+
+func TestDevboxSocketsDir(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "sockets")
+ for _, mode := range []os.FileMode{0, 0o755, 0o777 | os.ModeSetgid, 0o733 | os.ModeSticky} {
+ if mode != 0 {
+ if err := os.Chmod(path, mode); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := prepareDevboxSocketsDir(path, os.Geteuid()); err != nil {
+ t.Fatal(err)
+ }
+ fi, err := os.Lstat(path)
+ if err != nil || fi.Mode() != os.ModeDir|os.ModeSticky|0o733 {
+ t.Fatalf("sockets directory = %v, %v", fi, err)
+ }
+ }
+}
+
+func TestDevboxSocketsDirRefuses(t *testing.T) {
+ for _, kind := range []string{"file", "symlink", "dangling", "owner"} {
+ t.Run(kind, func(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "sockets")
+ uid := os.Geteuid()
+ var err error
+ switch kind {
+ case "file":
+ err = os.WriteFile(path, nil, 0o600)
+ case "symlink":
+ err = os.Symlink(dir, path)
+ case "dangling":
+ err = os.Symlink(filepath.Join(dir, "missing"), path)
+ case "owner":
+ err = os.Mkdir(path, 0o700)
+ uid++
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ before, err := os.Lstat(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := prepareDevboxSocketsDir(path, uid); err == nil {
+ t.Fatal("unsafe sockets directory accepted")
+ }
+ after, err := os.Lstat(path)
+ if err != nil || before.Mode() != after.Mode() {
+ t.Fatalf("refused path changed: %v, %v", after, err)
+ }
+ })
+ }
+}
+
+func TestDevboxServeSocketsDirFailure(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "sockets")
+ if err := os.WriteFile(path, nil, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ for key, value := range map[string]string{
+ "CARLOS_DEVBOX_LISTEN": "127.0.0.1:8443",
+ "CARLOS_DEVBOX_DOMAIN": "devbox.example.com",
+ "CARLOS_WILDCARD_DOMAINS": "devbox.example.com",
+ "CARLOS_DATA_DIR": dir,
+ "CARLOS_DEVBOX_LAUNCHD_SOCKET": "",
+ "CARLOS_DEVBOX_SOCKETS_DIR": path,
+ } {
+ t.Setenv(key, value)
+ }
+ err := runDevboxServe(nil)
+ var pe *os.PathError
+ if !errors.As(err, &pe) || pe.Path != path || err.Error() != "devbox serve: "+pe.Error() {
+ t.Fatalf("serve = %v, want wrapped path error", err)
+ }
+}
diff --git a/infra/devbox/carlos-devbox.service b/infra/devbox/carlos-devbox.service
index 252b6e487..69b61ff3a 100644
--- a/infra/devbox/carlos-devbox.service
+++ b/infra/devbox/carlos-devbox.service
@@ -25,6 +25,8 @@ Group=carlos-devbox
# the file). 0755 so every account can reach the socket.
RuntimeDirectory=carlos-devbox
RuntimeDirectoryMode=0755
+# Keep users' sockets when the service stops.
+RuntimeDirectoryPreserve=yes
# /var/lib/carlos-devbox is CARLOS_DATA_DIR: registry.db, links.json, certs/
# and spool/. Private to the service user.
StateDirectory=carlos-devbox