carlos / platform Public

edge: return the unix listener itself; refuse abstract addresses (Astra review)

P2: the wrapper removed the path after closing the descriptor, the
reverse of Go's order, so a goroutine that rebound after Accept returned
could lose its new socket; and SetUnlinkOnClose(false) on it did nothing.
SecureUnixListen now returns the *net.UnixListener with
SetUnlinkOnClose(true), as net.Listen("unix") gives.

An '@' address is abstract: no file, no permissions, any uid can connect.
It is refused before bind (the old code had the same gap).

Tests: the umask test's two goroutines now start together and the
directory maker runs as long as the binds do, so they always overlap (the
old umask bind fails it 5 of 5); the cleanup test is renamed to what it
covers; new tests for the abstract refusal and the listener type.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 8fb4ccb16e6376758ec302ed290632051d889c75 parent 9ea4bc1
2 files changed, +58 −26
  • internal/edge/purge_listen_linux.go +9 −18
  • internal/edge/purge_listen_linux_test.go +49 −8
diff --git a/internal/edge/purge_listen_linux.go b/internal/edge/purge_listen_linux.go
index ea6971351..e6ac55bee 100644
--- a/internal/edge/purge_listen_linux.go
+++ b/internal/edge/purge_listen_linux.go
@@ -7,7 +7,6 @@ import (
"fmt"
"net"
"os"
- "sync"
"syscall"
)
@@ -31,6 +30,11 @@ import (
// directory lost its execute bit with them (amadan #337). Nothing here
// touches the umask.
func SecureUnixListen(socketPath string) (net.Listener, error) {
+ // An abstract address (a leading @) has no file and so no permissions:
+ // any uid could connect to it. Refuse it rather than serve it.
+ if socketPath == "" || socketPath[0] == '@' {
+ return nil, fmt.Errorf("unix socket %q: need a filesystem path, which is what makes it owner-only", socketPath)
+ }
if err := os.Remove(socketPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
@@ -80,22 +84,9 @@ func SecureUnixListen(socketPath string) (net.Listener, error) {
fl.Close()
return nil, fmt.Errorf("unix socket %s: listener is %T, not a unix listener", socketPath, fl)
}
+ // A listener made from a file does not remove its path on Close; one
+ // from net.Listen("unix") does, and callers rely on it.
+ ln.SetUnlinkOnClose(true)
bound = false
- return &ownerOnlyListener{UnixListener: ln, path: socketPath}, nil
-}
-
-// ownerOnlyListener removes socketPath on Close, once, as a listener from
-// net.Listen("unix", ...) does; one built from a file does not. It embeds
-// the *net.UnixListener so SetDeadline and the rest stay reachable: the cmd
-// stream's Drain finds SetDeadline through an interface assertion.
-type ownerOnlyListener struct {
- *net.UnixListener
- path string
- unlink sync.Once
-}
-
-func (l *ownerOnlyListener) Close() error {
- err := l.UnixListener.Close()
- l.unlink.Do(func() { os.Remove(l.path) })
- return err
+ return ln, nil
}
diff --git a/internal/edge/purge_listen_linux_test.go b/internal/edge/purge_listen_linux_test.go
index c4bb515de..70b086839 100644
--- a/internal/edge/purge_listen_linux_test.go
+++ b/internal/edge/purge_listen_linux_test.go
@@ -26,11 +26,17 @@ func TestSecureUnixListenLeavesTheUmaskAlone(t *testing.T) {
t.Fatal(err)
}
+ // The directory maker runs for as long as the binder does, starting
+ // together, so the two always overlap.
const rounds = 300
+ start := make(chan struct{})
+ binding := make(chan struct{})
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
+ defer close(binding)
+ <-start
for i := range rounds {
ln, err := SecureUnixListen(filepath.Join(root, "l", "s"+strconv.Itoa(i)+".sock"))
if err != nil {
@@ -40,18 +46,29 @@ func TestSecureUnixListenLeavesTheUmaskAlone(t *testing.T) {
ln.Close()
}
}()
- dirs := make([]string, rounds)
+ var dirs []string
go func() {
defer wg.Done()
- for i := range rounds {
- dirs[i] = filepath.Join(root, "d", strconv.Itoa(i))
- if err := os.MkdirAll(dirs[i], 0o755); err != nil {
+ <-start
+ for i := 0; ; i++ {
+ select {
+ case <-binding:
+ return
+ default:
+ }
+ d := filepath.Join(root, "d", strconv.Itoa(i))
+ if err := os.MkdirAll(d, 0o755); err != nil {
t.Error(err)
return
}
+ dirs = append(dirs, d)
}
}()
+ close(start)
wg.Wait()
+ if len(dirs) == 0 {
+ t.Fatal("no directory was made while the binds ran")
+ }
for _, d := range dirs {
fi, err := os.Stat(d)
if err != nil {
@@ -106,10 +123,9 @@ func TestSecureUnixListenServesAtItsPathAndLeavesNothingBehind(t *testing.T) {
}
}
-// A bind that cannot be put in place (here: socketPath is a directory with
-// something in it) fails and leaves neither a private directory nor a
-// listening socket behind.
-func TestSecureUnixListenFailureCleansUp(t *testing.T) {
+// A path that cannot be cleared (here: a directory with something in it)
+// fails before anything is bound, and is left as it was.
+func TestSecureUnixListenRefusesAPathItCannotClear(t *testing.T) {
t.Parallel()
dir := t.TempDir()
sock := filepath.Join(dir, "c.sock")
@@ -125,6 +141,31 @@ func TestSecureUnixListenFailureCleansUp(t *testing.T) {
}
}
+// An abstract address has no file, so no permissions: refused, not bound.
+func TestSecureUnixListenRefusesAnAbstractAddress(t *testing.T) {
+ t.Parallel()
+ for _, p := range []string{"@carlos-test-abstract", ""} {
+ if ln, err := SecureUnixListen(p); err == nil {
+ ln.Close()
+ t.Errorf("SecureUnixListen(%q) bound; want a refusal", p)
+ }
+ }
+}
+
+// The listener is a *net.UnixListener, as net.Listen("unix") gives, so
+// callers keep SetDeadline, File and SetUnlinkOnClose.
+func TestSecureUnixListenGivesAUnixListener(t *testing.T) {
+ t.Parallel()
+ ln, err := SecureUnixListen(filepath.Join(t.TempDir(), "c.sock"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer ln.Close()
+ if _, ok := ln.(*net.UnixListener); !ok {
+ t.Fatalf("listener is %T, want *net.UnixListener", ln)
+ }
+}
+
// A second Close must not remove a socket a later listener has since bound
// at the same path.
func TestSecureUnixListenClosesItsPathOnce(t *testing.T) {