From 3c13466645f3eb77c43e98e587badbc6e10183e2 Mon Sep 17 00:00:00 2001 From: James Tucker Date: Fri, 23 Oct 2026 23:48:18 +0000 Subject: [PATCH] ssh/tailssh: write private host keys with 0600 permissions Private host keys are only ever read by tailscaled itself; writing them 0700 needlessly allows group/other to walk into the key directory. Use 0600, and pin it in TestSSHKeyGen. Updates tailscale/corp#50380 Signed-off-by: James Tucker --- ssh/tailssh/hostkeys.go | 5 ++++- ssh/tailssh/hostkeys_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ssh/tailssh/hostkeys.go b/ssh/tailssh/hostkeys.go index b76cc76fa..c22d3a266 100644 --- a/ssh/tailssh/hostkeys.go +++ b/ssh/tailssh/hostkeys.go @@ -131,7 +131,10 @@ func hostKeyFileOrCreate(keyDir, typ string) ([]byte, error) { return nil, err } pemGen := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: mk}) - err = os.WriteFile(path, pemGen, 0700) + // Private host keys are for tailscaled only: no group/other access, + // so 0600 rather than 0700 (which would needlessly allow the key to + // be walked into). + err = os.WriteFile(path, pemGen, 0600) return pemGen, err } diff --git a/ssh/tailssh/hostkeys_test.go b/ssh/tailssh/hostkeys_test.go index 24a876454..ad0a9b5dc 100644 --- a/ssh/tailssh/hostkeys_test.go +++ b/ssh/tailssh/hostkeys_test.go @@ -6,6 +6,8 @@ package tailssh import ( + "os" + "path/filepath" "reflect" "testing" ) @@ -16,6 +18,17 @@ func TestSSHKeyGen(t *testing.T) { if err != nil { t.Fatal(err) } + // Private host keys must be readable by the owner only: 0600, not 0700, + // which would allow the key directory to be walked into by others. + for _, typ := range keyTypes { + fi, err := os.Stat(filepath.Join(dir, "ssh", "ssh_host_"+typ+"_key")) + if err != nil { + t.Fatalf("stat host key: %v", err) + } + if got := fi.Mode().Perm(); got != 0600 { + t.Errorf("host key %s mode = %o; want 0600", typ, got) + } + } got := map[string]bool{} for _, k := range keys { got[k.PublicKey().Type()] = true