db forward task 8: carlos system db-worker, the stdin socket and peer checks, Linux only
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+171
−1
internal/dbreads/worker_linux.go+83 −0internal/dbreads/worker_linux_test.go+59 −0internal/dbreads/worker_other.go+23 −0system.go+6 −1
diff --git a/internal/dbreads/worker_linux.go b/internal/dbreads/worker_linux.go| new file mode 100644 |
| index 000000000..bcff41825 |
| --- /dev/null |
| +++ b/internal/dbreads/worker_linux.go |
| @@ -0,0 +1,83 @@ |
| +//go:build linux |
| + |
| +package dbreads |
| + |
| +import ( |
| + "errors" |
| + "fmt" |
| + "log" |
| + "net" |
| + "os" |
| + "os/user" |
| + "strconv" |
| + |
| + "golang.org/x/sys/unix" |
| +) |
| + |
| +var errNotSocket = errors.New("stdin is not a connected unix stream socket") |
| + |
| +// RunWorker is `carlos system db-worker`, which carlos-dbread@.service runs |
| +// once per connection to carlos-dbread.socket (spec §8.3). |
| +func RunWorker(args []string) { |
| + if len(args) != 0 { |
| + fmt.Fprintln(os.Stderr, "db-worker: takes no arguments") |
| + os.Exit(2) |
| + } |
| + RunWorkerWith(WorkerOptions{Scratch: "/var/tmp"}) |
| +} |
| + |
| +// RunWorkerWith is RunWorker with its options supplied: the seam the root |
| +// runner's acceptance tests use to make a restore slow (Task 15). |
| +func RunWorkerWith(opt WorkerOptions) { |
| + conn, err := StdinConn(os.Stdin, CarlosUID) |
| + if err != nil { |
| + fmt.Fprintf(os.Stderr, "db-worker: %v\n", err) |
| + os.Exit(2) |
| + } |
| + if err := ServeWorker(conn, opt); err != nil { |
| + log.Printf("db-worker: %v", err) |
| + os.Exit(1) |
| + } |
| +} |
| + |
| +// CarlosUID is the agent's user, the only peer the worker serves. |
| +func CarlosUID() (uint32, error) { |
| + u, err := user.Lookup("carlos") |
| + if err != nil { |
| + return 0, err |
| + } |
| + uid, err := strconv.ParseUint(u.Uid, 10, 32) |
| + return uint32(uid), err |
| +} |
| + |
| +// StdinConn checks f is a connected unix stream socket (SO_TYPE, the socket |
| +// name's family, SO_PEERCRED) whose peer is wantUID, and returns it as a |
| +// connection. |
| +func StdinConn(f *os.File, wantUID func() (uint32, error)) (net.Conn, error) { |
| + fd := int(f.Fd()) |
| + typ, err := unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_TYPE) |
| + if err != nil || typ != unix.SOCK_STREAM { |
| + return nil, errNotSocket |
| + } |
| + if sa, err := unix.Getsockname(fd); err != nil { |
| + return nil, errNotSocket |
| + } else if _, ok := sa.(*unix.SockaddrUnix); !ok { |
| + return nil, errNotSocket |
| + } |
| + cred, err := unix.GetsockoptUcred(fd, unix.SOL_SOCKET, unix.SO_PEERCRED) |
| + if err != nil || cred.Pid == 0 { |
| + return nil, errNotSocket |
| + } |
| + want, err := wantUID() |
| + if err != nil { |
| + return nil, err |
| + } |
| + if cred.Uid != want { |
| + return nil, fmt.Errorf("the peer is uid %d, not the agent's uid %d", cred.Uid, want) |
| + } |
| + c, err := net.FileConn(f) |
| + if err != nil { |
| + return nil, errNotSocket |
| + } |
| + return c, nil |
| +} |
diff --git a/internal/dbreads/worker_linux_test.go b/internal/dbreads/worker_linux_test.go| new file mode 100644 |
| index 000000000..f548d0992 |
| --- /dev/null |
| +++ b/internal/dbreads/worker_linux_test.go |
| @@ -0,0 +1,59 @@ |
| +//go:build linux |
| + |
| +package dbreads |
| + |
| +import ( |
| + "os" |
| + "path/filepath" |
| + "testing" |
| + |
| + "golang.org/x/sys/unix" |
| +) |
| + |
| +func me() (uint32, error) { return uint32(os.Getuid()), nil } |
| + |
| +func other() (uint32, error) { return uint32(os.Getuid()) + 1, nil } |
| + |
| +func socketpairFile(t *testing.T, typ int) (*os.File, *os.File) { |
| + t.Helper() |
| + fds, err := unix.Socketpair(unix.AF_UNIX, typ, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + a, b := os.NewFile(uintptr(fds[0]), "a"), os.NewFile(uintptr(fds[1]), "b") |
| + t.Cleanup(func() { a.Close(); b.Close() }) |
| + return a, b |
| +} |
| + |
| +// The worker runs only on a connected unix stream socket whose peer is the |
| +// agent's user (spec §8.3): it cannot be started by hand or pointed at a |
| +// file. |
| +func TestStdinConnRefusesWhatIsNotTheAgentsSocket(t *testing.T) { |
| + t.Parallel() |
| + file, err := os.Create(filepath.Join(t.TempDir(), "f")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer file.Close() |
| + r, w, err := os.Pipe() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer r.Close() |
| + defer w.Close() |
| + dgram, _ := socketpairFile(t, unix.SOCK_DGRAM) |
| + stream, _ := socketpairFile(t, unix.SOCK_STREAM) |
| + for name, f := range map[string]*os.File{"a file": file, "a pipe": r, "a datagram socket": dgram} { |
| + if _, err := StdinConn(f, me); err == nil { |
| + t.Errorf("%s accepted", name) |
| + } |
| + } |
| + if _, err := StdinConn(stream, other); err == nil { |
| + t.Fatal("a peer that is not the agent's user accepted") |
| + } |
| + c, err := StdinConn(stream, me) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + c.Close() |
| +} |
diff --git a/internal/dbreads/worker_other.go b/internal/dbreads/worker_other.go| new file mode 100644 |
| index 000000000..1fab0af3b |
| --- /dev/null |
| +++ b/internal/dbreads/worker_other.go |
| @@ -0,0 +1,23 @@ |
| +//go:build !linux |
| + |
| +package dbreads |
| + |
| +import ( |
| + "errors" |
| + "fmt" |
| + "net" |
| + "os" |
| +) |
| + |
| +var errUnsupported = errors.New("db-worker runs only on Linux") |
| + |
| +func RunWorker([]string) { |
| + fmt.Fprintln(os.Stderr, "db-worker:", errUnsupported) |
| + os.Exit(2) |
| +} |
| + |
| +func RunWorkerWith(WorkerOptions) { RunWorker(nil) } |
| + |
| +func CarlosUID() (uint32, error) { return 0, errUnsupported } |
| + |
| +func StdinConn(*os.File, func() (uint32, error)) (net.Conn, error) { return nil, errUnsupported } |
diff --git a/system.go b/system.go| index 0717c9333..f3ae7a1c2 100644 |
| --- a/system.go |
| +++ b/system.go |
| @@ -14,6 +14,7 @@ import ( |
| "strings" |
| "time" |
| + "github.com/carlosframework/platform/internal/dbreads" |
| "github.com/carlosframework/platform/internal/host" |
| "github.com/carlosframework/platform/internal/hostenv" |
| ) |
| @@ -50,7 +51,7 @@ func systemVerb(args []string) (verb string, rest []string, component string) { |
| // untouched in rest for runSystemAntivirusVerb to read. |
| // layout is a family too: migrate|revert|status stay in rest for |
| // runSystemLayout. |
| - case "claims", "files", "guard-arm", "guard-exec", "journal-migrate", "floor-allows-prev", "update", "status", "rollback", "cancel-activation", "apply", "antivirus", "layout", "geoip", "seeds-floor", "legacy-routes", "box", "group", "install-channel", "join", "host-env", "help", "-h", "--help": |
| + case "claims", "files", "guard-arm", "guard-exec", "journal-migrate", "floor-allows-prev", "update", "status", "rollback", "cancel-activation", "apply", "antivirus", "layout", "geoip", "seeds-floor", "legacy-routes", "box", "group", "install-channel", "join", "host-env", "db-worker", "help", "-h", "--help": |
| return args[0], args[1:], component |
| } |
| return "", args, component |
| @@ -106,6 +107,10 @@ func runSystem(args []string) { |
| case "host-env": |
| // host.env from and into the deployment's parameters (§5 step 2). |
| hostenv.RunSystemHostEnv(rest) |
| + case "db-worker": |
| + // The db-read worker (db region forward spec §8.3): systemd starts |
| + // it per connection to carlos-dbread.socket. Never run by hand. |
| + dbreads.RunWorker(rest) |
| case "help", "-h", "--help": |
| systemUsage() |
| default: |