diff --git a/cmd/devbox/pidfile.go b/cmd/devbox/pidfile.go index b2c2bb8..af99a4c 100644 --- a/cmd/devbox/pidfile.go +++ b/cmd/devbox/pidfile.go @@ -33,7 +33,13 @@ func writePid(dir string) error { for { f, err := os.OpenFile(pidPath(dir), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) if err == nil { - _, werr := f.WriteString(pidfileContents()) + // Record "" or, where verifiable, " ". + 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 { werr = cerr } @@ -52,16 +58,6 @@ func writePid(dir string) error { } } -// pidfileContents is the line we record for our own process: "" or, when -// the start token is available, " ". -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)) } // runningPid reports the pid in the pidfile, and whether that process is alive diff --git a/cmd/devbox/pidfile_test.go b/cmd/devbox/pidfile_test.go index 5e69a09..e67e004 100644 --- a/cmd/devbox/pidfile_test.go +++ b/cmd/devbox/pidfile_test.go @@ -34,10 +34,9 @@ func TestRunningPidStartTokenMismatch(t *testing.T) { 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, - // proving the guard rejects only the impostor. - good := []byte(pidfileContents()) - if err := os.WriteFile(pidPath(dir), good, 0o600); err != nil { + // The contents writePid records for us must read as alive, proving the guard + // rejects only the impostor. writePid reclaims the stale file written above. + if err := writePid(dir); err != nil { t.Fatal(err) } if pid, ok := runningPid(dir); !ok || pid != os.Getpid() { diff --git a/pkg/proto/proto.go b/pkg/proto/proto.go index bb3796f..42ef07d 100644 --- a/pkg/proto/proto.go +++ b/pkg/proto/proto.go @@ -50,9 +50,13 @@ type JoinRequest struct { // 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 // 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\0 +// " — 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 { - msg := make([]byte, 0, len(token)+len(pubkey)) - msg = append(msg, token...) + msg := append([]byte("devbox-join\x00"), token...) + msg = append(msg, 0) return append(msg, pubkey...) }