security: lock down the key dir through descriptors, not paths

LockDownKeyDir ran os.Chmod(keyDir), which follows a symlink on keyDir,
then Lstat and Chmod by path on jwt.key and secret.key, open to a swap
between the two and to a hard link to a file elsewhere.

keyDir is now opened under an os.Root at its parent and must be a
directory, not a symlink, the one opened being the one checked
(traffic.OpenRealDir); its mode is set through that descriptor. Each key
goes through traffic.RestrictFile: Lstat, O_NOFOLLOW open under the root,
regular with a single link after fstat and again by name with
os.SameFile, and never chmodded in place (a copy at 0600 replaces it).

Behaviour change: a keyDir that is a symlink, or that is not a directory,
now fails the start (fail-closed), as does a hard-linked key file.
This commit is contained in:
BaaRai
2026-12-01 18:26:09 +01:00
parent 197224a780
commit 5138f3595b
3 changed files with 121 additions and 12 deletions
+79
View File
@@ -337,3 +337,82 @@ func TestLockDownCAKeyHardlinkUnlinkRace(t *testing.T) {
wantMode(t, victim, 0o644)
t.Logf("%d iterations in %v, %d accepted (the attacker's own file or no key), host file still 0644", iter, time.Since(start).Round(time.Millisecond), accepted)
}
// A key directory that is a symlink to a directory elsewhere is refused (the start
// fails) instead of chmodding that directory and the key files in it.
func TestLockDownKeyDirRefusesSymlinkedKeyDir(t *testing.T) {
outside := filepath.Join(t.TempDir(), "elsewhere")
if err := os.MkdirAll(outside, 0o755); err != nil {
t.Fatal(err)
}
if err := os.Chmod(outside, 0o755); err != nil {
t.Fatal(err)
}
jwt, sec := filepath.Join(outside, "jwt.key"), filepath.Join(outside, "secret.key")
writeMode(t, jwt, 0o644)
writeMode(t, sec, 0o644)
keyDir := filepath.Join(t.TempDir(), "keys")
if err := os.Symlink(outside, keyDir); err != nil {
t.Fatal(err)
}
if err := LockDownKeyDir(keyDir); err == nil {
t.Error("LockDownKeyDir accepted a symlinked key directory")
} else {
t.Logf("refused: %v", err)
}
wantMode(t, outside, 0o755)
wantMode(t, jwt, 0o644)
wantMode(t, sec, 0o644)
}
// A key directory that is not a directory is refused.
func TestLockDownKeyDirRefusesNonDirectory(t *testing.T) {
keyDir := filepath.Join(t.TempDir(), "keys")
writeMode(t, keyDir, 0o644)
if err := LockDownKeyDir(keyDir); err == nil {
t.Error("LockDownKeyDir accepted a regular file as the key directory")
}
wantMode(t, keyDir, 0o644)
}
// A key file hard-linked to a host file is refused, and the host file keeps its
// mode.
func TestLockDownKeyDirRefusesHardlinkedKey(t *testing.T) {
victim := filepath.Join(t.TempDir(), "host-file")
writeMode(t, victim, 0o644)
keyDir := filepath.Join(t.TempDir(), "keys")
if err := os.MkdirAll(keyDir, 0o700); err != nil {
t.Fatal(err)
}
if err := os.Link(victim, filepath.Join(keyDir, "jwt.key")); err != nil {
t.Skipf("hard link: %v", err)
}
if err := LockDownKeyDir(keyDir); err == nil {
t.Error("LockDownKeyDir accepted a hard-linked jwt.key")
} else {
t.Logf("refused: %v", err)
}
wantMode(t, victim, 0o644)
}
// A key at 0644 becomes 0600 with its content intact.
func TestLockDownKeyDirKeepsKeyContent(t *testing.T) {
keyDir := filepath.Join(t.TempDir(), "keys")
key := filepath.Join(keyDir, "secret.key")
writeMode(t, key, 0o644)
if err := LockDownKeyDir(keyDir); err != nil {
t.Fatalf("LockDownKeyDir: %v", err)
}
wantMode(t, key, 0o600)
wantMode(t, keyDir, 0o700)
if b, err := os.ReadFile(key); err != nil || string(b) != "key" {
t.Fatalf("secret.key content %q, %v; want %q", b, err, "key")
}
entries, err := os.ReadDir(keyDir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Fatalf("key dir holds %d entries, want only secret.key", len(entries))
}
}
+28 -12
View File
@@ -88,6 +88,13 @@ func IsolationSelfTest(p ExecPrivilege, t IsolationTargets) error {
// LockDownKeyDir ensures keyDir exists with mode 0700 and tightens an existing
// directory (such as a host bind mount whose default 0755 masks the image's
// 0700) plus any existing jwt.key / secret.key files inside it to 0600.
//
// The server runs as root, so nothing is changed by path: keyDir is opened under
// a root at its parent and must be a directory, not a symlink, the one opened
// being the one checked; its mode is changed through that descriptor, and each
// key goes through traffic.RestrictFile (a regular file with a single link,
// never followed, never chmodded in place). A keyDir that is a symlink or not a
// directory fails the start rather than lead the chmod to a directory elsewhere.
func LockDownKeyDir(keyDir string) error {
if strings.TrimSpace(keyDir) == "" {
return nil
@@ -95,23 +102,32 @@ func LockDownKeyDir(keyDir string) error {
if err := os.MkdirAll(keyDir, 0o700); err != nil {
return fmt.Errorf("create key dir %s: %w", keyDir, err)
}
if err := os.Chmod(keyDir, 0o700); err != nil {
clean := filepath.Clean(keyDir)
parent, err := os.OpenRoot(filepath.Dir(clean))
if err != nil {
return fmt.Errorf("open the parent of key dir %s: %w", keyDir, err)
}
defer parent.Close()
dir, err := traffic.OpenRealDir(parent, filepath.Base(clean))
if err != nil {
return fmt.Errorf("open key dir %s: %w", keyDir, err)
}
defer dir.Close()
d, err := dir.Open(".")
if err != nil {
return fmt.Errorf("open key dir %s: %w", keyDir, err)
}
err = d.Chmod(0o700)
d.Close()
if err != nil {
return fmt.Errorf("chmod 0700 key dir %s: %w", keyDir, err)
}
for _, name := range []string{jwtKeyFilename, "secret.key"} {
p := filepath.Join(keyDir, name)
fi, err := os.Lstat(p)
if err != nil {
if os.IsNotExist(err) {
if err := traffic.RestrictFile(dir, name, 0o600); err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
return fmt.Errorf("stat key file %s: %w", p, err)
}
if fi.Mode()&os.ModeSymlink != 0 {
return fmt.Errorf("key file %s must not be a symlink", p)
}
if err := os.Chmod(p, 0o600); err != nil {
return fmt.Errorf("chmod 0600 key file %s: %w", p, err)
return fmt.Errorf("chmod 0600 key file %s: %w", filepath.Join(keyDir, name), err)
}
}
return nil
+14
View File
@@ -153,6 +153,20 @@ func singleLinkRegular(name string, fi os.FileInfo) error {
return nil
}
// RestrictFile sets root/name to perm with the checks RestrictCAKey describes for
// the key: name must be a regular file with a single link, it is never followed
// as a symlink, and it is never chmodded in place. A missing file returns an
// error that wraps fs.ErrNotExist.
func RestrictFile(root *os.Root, name string, perm os.FileMode) error {
return restrictFile(root, name, name, perm)
}
// OpenRealDir opens parent/name as a root, provided name is a directory and not
// a symlink, and the directory opened is the one checked.
func OpenRealDir(parent *os.Root, name string) (*os.Root, error) {
return openRealDir(parent, name, name)
}
// openRealDir opens parent/name as a root, provided name is a directory and not
// a symlink, and the directory opened is the one checked.
func openRealDir(parent *os.Root, name, shown string) (*os.Root, error) {