Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Protect secrets with access lists on Windows

33cbbc9d3dad71cf1bad5cf63508399a560e3b54 · Keenan Nemetz · 2d ago · parent 215f8aeb

Protect secrets with access lists on Windows

The Windows build compiled but refused to start: file modes are the secret
store's guarantee on unix and Windows has no such thing. This replaces the mode
check with the platform's own control rather than with a weaker one.

On Windows a secret is now a DACL: the service account, NT AUTHORITY\SYSTEM and
BUILTIN\Administrators at full control, inherited entries removed, and the result
read back, because SetNamedSecurityInfo reporting success does not mean the ACL
is the one meant. Directories must also have stopped inheriting — a directory
that follows its parent can be opened by a later change to the parent, which is
the kind of change nobody notices. Files inside a protected directory inherit
from it, so they carry no flag of their own.

internal/audit holds the log to the same rule through the same function, so the
log cannot drift to a weaker one than the files beside it. The unix halves keep
their checks, messages and order unchanged; the mode-based tests are built for
unix, and the new DACL tests run on a real Windows machine, which is the only
place they can run.

ssh_socket gains a per-platform default: %ProgramData%\vole\ssh.sock. The unix
path it had made configuration validation refuse every Windows start before any
of the above could run.

Verified on a Server Core 2022 guest (see the issue for the full record): init,
account creation, serve, HTTP, the admin CLI over SSH, and git push and clone all
work, with every secret and the audit log granted only to trusted principals and
the data directory protected. go build, go vet, and the secure and audit tests
pass natively on Windows as well as in the verify workflow.

Part of 01M8SCVR9GZTH7BBXHF97H817P.

internal/secure/acl_windows_test.go +151

@@ -0,0 +1,151 @@
1 //go:build windows
2
3 package secure
4
5 // The unix tests in secure_test.go assert permission bits, which Windows does not
6 // have in the form those names mean; these tests assert the access list instead.
7 // They need a real Windows machine — CI cross-compiles the tree but does not run
8 // it — so they are also the thing to run in the guest when this file changes:
9 //
10 // GOOS=windows go test ./internal/secure
11
12 import (
13 "os"
14 "path/filepath"
15 "runtime"
16 "testing"
17
18 "golang.org/x/sys/windows"
19 )
20
21 // grantEveryone rewrites path's access list so that Everyone has full control,
22 // the state a secret lands in when it is created somewhere an installer made
23 // world-readable, or inherits from a directory that was.
24 func grantEveryone(t *testing.T, path string) {
25 t.Helper()
26 world, err := windows.CreateWellKnownSid(windows.WinWorldSid)
27 if err != nil {
28 t.Fatal(err)
29 }
30 var pinner runtime.Pinner
31 pinner.Pin(world)
32 defer pinner.Unpin()
33 dacl, err := windows.ACLFromEntries([]windows.EXPLICIT_ACCESS{{
34 AccessPermissions: fileAllAccess,
35 AccessMode: windows.SET_ACCESS,
36 Trustee: windows.TRUSTEE{
37 TrusteeForm: windows.TRUSTEE_IS_SID,
38 TrusteeType: windows.TRUSTEE_IS_USER,
39 TrusteeValue: windows.TrusteeValueFromSID(world),
40 },
41 }}, nil)
42 if err != nil {
43 t.Fatal(err)
44 }
45 if err := windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT,
46 windows.DACL_SECURITY_INFORMATION|windows.PROTECTED_DACL_SECURITY_INFORMATION,
47 nil, nil, dacl, nil); err != nil {
48 t.Fatal(err)
49 }
50 }
51
52 // TestHardenFileReplacesAnOpenAccessList is the upgrade case in Windows terms: a
53 // file created with an inherited or rewritten list that grants Everyone is fixed
54 // by starting the instance.
55 func TestHardenFileReplacesAnOpenAccessList(t *testing.T) {
56 path := filepath.Join(t.TempDir(), "instance.db")
57 if err := os.WriteFile(path, []byte("keys"), 0o600); err != nil {
58 t.Fatal(err)
59 }
60 grantEveryone(t, path)
61 if err := VerifyACL(path); err == nil {
62 t.Fatal("a file granting Everyone full control passed verification")
63 }
64
65 changed, err := HardenFile(path)
66 if err != nil {
67 t.Fatal(err)
68 }
69 if !changed {
70 t.Fatal("a file granting Everyone was reported as already correct")
71 }
72 if err := VerifyACL(path); err != nil {
73 t.Fatalf("still not protected after hardening: %v", err)
74 }
75 // The content is untouched.
76 data, err := os.ReadFile(path)
77 if err != nil {
78 t.Fatal(err)
79 }
80 if string(data) != "keys" {
81 t.Fatalf("content = %q", data)
82 }
83 // And a second pass is a no-op, so startup does not churn.
84 changed, err = HardenFile(path)
85 if err != nil {
86 t.Fatal(err)
87 }
88 if changed {
89 t.Fatal("an already protected file was reported as changed")
90 }
91 }
92
93 // TestHardenDirProtectsFilesCreatedInside is the inheritance case: the directory
94 // is closed, and a file created in it afterwards is protected before anything
95 // writes to it.
96 func TestHardenDirProtectsFilesCreatedInside(t *testing.T) {
97 dir := filepath.Join(t.TempDir(), "data")
98 if err := os.Mkdir(dir, 0o700); err != nil {
99 t.Fatal(err)
100 }
101 grantEveryone(t, dir)
102 if _, err := HardenDir(dir); err != nil {
103 t.Fatal(err)
104 }
105 if err := VerifyACL(dir); err != nil {
106 t.Fatalf("the directory is not protected: %v", err)
107 }
108 // A directory that still inherits would follow its parent if the parent were
109 // opened later, so HardenDir has to own the list rather than share it.
110 if err := verifyACL(dir, true); err != nil {
111 t.Fatalf("the directory still inherits: %v", err)
112 }
113
114 file := filepath.Join(dir, "instance.db")
115 if err := os.WriteFile(file, []byte("keys"), 0o600); err != nil {
116 t.Fatal(err)
117 }
118 if err := VerifyACL(file); err != nil {
119 t.Fatalf("a file created in a protected directory is not protected: %v", err)
120 }
121 // The file inherits from a protected directory, which is the arrangement that
122 // makes protection reach files nobody has opened yet; the flag belongs to the
123 // directory.
124 if err := verifyACL(file, false); err != nil {
125 t.Fatalf("the file's inherited access list is not restricted: %v", err)
126 }
127 }
128
129 // TestVerifyACLRejectsAnotherAccount checks the half a DACL rewrite cannot fix:
130 // a secret owned by an account that has no business holding one is refused rather
131 // than accepted as somebody else's problem.
132 func TestVerifyACLRejectsAnotherAccount(t *testing.T) {
133 path := filepath.Join(t.TempDir(), "instance.db")
134 if err := os.WriteFile(path, []byte("keys"), 0o600); err != nil {
135 t.Fatal(err)
136 }
137 // Take ownership away from this account. That needs SeRestorePrivilege, so on
138 // a test runner without it the case cannot be built; skipping is better than a
139 // test that silently stops checking anything.
140 world, err := windows.CreateWellKnownSid(windows.WinWorldSid)
141 if err != nil {
142 t.Fatal(err)
143 }
144 if err := windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT,
145 windows.OWNER_SECURITY_INFORMATION, world, nil, nil, nil); err != nil {
146 t.Skipf("cannot set the owner without SeRestorePrivilege: %v", err)
147 }
148 if err := VerifyACL(path); err == nil {
149 t.Fatal("a file owned by Everyone passed verification")
150 }
151 }