harden: domain-separate the join challenge + inline pidfile wrapper (ponytail) 🪪♻️

Ponytail review of the hardening batch:
- JoinChallenge was a bare token||pubkey concat — unambiguous only because
  ed25519 keys are fixed 32 bytes. Now domain-separated + NUL-delimited
  ('devbox-join\0<token>\0<pubkey>') so the boundary is explicit regardless of
  key length, and the sig can't collide with any other devbox signing use.
- inlined the one-caller pidfileContents() into writePid.
(Kept the Linux /proc start-time guard: the daemon's real home is the Pi
cluster, where PID-reuse-after-crash is a genuine window; the magic field index
is already a named const. Declined the flock rewrite of fleet-verified code.)
This commit is contained in:
shoemoney 2026-06-23 03:25:28 -05:00
parent b7b9459cca
commit f3f24feea9
3 changed files with 16 additions and 17 deletions

View file

@ -33,7 +33,13 @@ func writePid(dir string) error {
for { for {
f, err := os.OpenFile(pidPath(dir), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) f, err := os.OpenFile(pidPath(dir), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
if err == nil { if err == nil {
_, werr := f.WriteString(pidfileContents()) // Record "<pid>" or, where verifiable, "<pid> <starttoken>".
pid := os.Getpid()
line := strconv.Itoa(pid)
if tok, ok := processStartToken(pid); ok {
line += " " + tok
}
_, werr := f.WriteString(line)
if cerr := f.Close(); werr == nil { if cerr := f.Close(); werr == nil {
werr = cerr werr = cerr
} }
@ -52,16 +58,6 @@ func writePid(dir string) error {
} }
} }
// pidfileContents is the line we record for our own process: "<pid>" or, when
// the start token is available, "<pid> <starttoken>".
func pidfileContents() string {
pid := os.Getpid()
if tok, ok := processStartToken(pid); ok {
return strconv.Itoa(pid) + " " + tok
}
return strconv.Itoa(pid)
}
func removePid(dir string) { _ = os.Remove(pidPath(dir)) } func removePid(dir string) { _ = os.Remove(pidPath(dir)) }
// runningPid reports the pid in the pidfile, and whether that process is alive // runningPid reports the pid in the pidfile, and whether that process is alive

View file

@ -34,10 +34,9 @@ func TestRunningPidStartTokenMismatch(t *testing.T) {
t.Fatalf("runningPid = (%d,true) for a mismatched start token; want not-alive (stale)", pid) t.Fatalf("runningPid = (%d,true) for a mismatched start token; want not-alive (stale)", pid)
} }
// The matching token (the one writePid records for us) must read as alive, // The contents writePid records for us must read as alive, proving the guard
// proving the guard rejects only the impostor. // rejects only the impostor. writePid reclaims the stale file written above.
good := []byte(pidfileContents()) if err := writePid(dir); err != nil {
if err := os.WriteFile(pidPath(dir), good, 0o600); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if pid, ok := runningPid(dir); !ok || pid != os.Getpid() { if pid, ok := runningPid(dir); !ok || pid != os.Getpid() {

View file

@ -50,9 +50,13 @@ type JoinRequest struct {
// prove it actually holds the key behind Pubkey (the device id is derived from // prove it actually holds the key behind Pubkey (the device id is derived from
// Pubkey, so without this anyone could claim another device's identity). Binding // Pubkey, so without this anyone could claim another device's identity). Binding
// the one-time token means the signature can't be replayed for a different join. // the one-time token means the signature can't be replayed for a different join.
//
// The message is domain-separated and NUL-delimited — "devbox-join\0<token>\0
// <pubkey>" — so the (token, pubkey) boundary is unambiguous regardless of key
// length, and the signature can't be confused with any other devbox signing use.
func JoinChallenge(token string, pubkey []byte) []byte { func JoinChallenge(token string, pubkey []byte) []byte {
msg := make([]byte, 0, len(token)+len(pubkey)) msg := append([]byte("devbox-join\x00"), token...)
msg = append(msg, token...) msg = append(msg, 0)
return append(msg, pubkey...) return append(msg, pubkey...)
} }