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.go +232

@@ -0,0 +1,232 @@
1 //go:build windows
2
3 package secure
4
5 // A secret file is protected with a DACL on Windows, not a mode: the mode bits
6 // of a Windows file record only the read-only attribute, so "0600" there would
7 // mean "read-only", which protects nothing and breaks the service that has to
8 // write its own key.
9 //
10 // The rule this file applies is the unix rule in the platform's terms: the file
11 // is owned by the account that runs the service and its access list names that
12 // account, the local system account, and the local administrators, and nobody
13 // else. Administrators are included for the same reason root is not excluded on
14 // unix — they can take the file with a privilege anyway — and system is included
15 // because services are commonly launched that way.
16
17 import (
18 "fmt"
19 "os"
20 "runtime"
21 "unsafe"
22
23 "golang.org/x/sys/windows"
24 )
25
26 // fileAllAccess is FILE_ALL_ACCESS: what an account that may hold a secret gets,
27 // and the only access a DACL this package writes is allowed to name.
28 const fileAllAccess = windows.STANDARD_RIGHTS_REQUIRED | windows.SYNCHRONIZE | 0x1ff
29
30 // trusted is the set of principals that may hold a secret, with the service
31 // account first: it is the one that must always be present.
32 type trusted struct {
33 user *windows.SID
34 all []*windows.SID
35 }
36
37 // trustedPrincipals reads the account this process runs as and the two machine
38 // principals that stand in for unix's root. Reading it per call rather than once
39 // at startup keeps a test that changes identity from seeing a stale answer.
40 func trustedPrincipals() (*trusted, error) {
41 tokenUser, err := windows.GetCurrentProcessToken().GetTokenUser()
42 if err != nil {
43 return nil, fmt.Errorf("read the account this process runs as: %w", err)
44 }
45 system, err := windows.CreateWellKnownSid(windows.WinLocalSystemSid)
46 if err != nil {
47 return nil, fmt.Errorf("build the local system SID: %w", err)
48 }
49 administrators, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid)
50 if err != nil {
51 return nil, fmt.Errorf("build the administrators SID: %w", err)
52 }
53 t := &trusted{user: tokenUser.User.Sid, all: []*windows.SID{tokenUser.User.Sid}}
54 for _, sid := range []*windows.SID{system, administrators} {
55 if !t.user.Equals(sid) {
56 t.all = append(t.all, sid)
57 }
58 }
59 return t, nil
60 }
61
62 // name renders a principal for an error message. The two machine principals and
63 // the service account have names; anything else is shown as its SID, which is
64 // exact and needs no directory round trip.
65 func (t *trusted) name(sid *windows.SID) string {
66 switch {
67 case t.user.Equals(sid):
68 return "this account"
69 case sid.IsWellKnown(windows.WinLocalSystemSid):
70 return `NT AUTHORITY\SYSTEM`
71 case sid.IsWellKnown(windows.WinBuiltinAdministratorsSid):
72 return `BUILTIN\Administrators`
73 }
74 return sid.String()
75 }
76
77 func (t *trusted) includes(sid *windows.SID) bool {
78 for _, allowed := range t.all {
79 if allowed.Equals(sid) {
80 return true
81 }
82 }
83 return false
84 }
85
86 // tightenFile replaces the file's DACL with one that grants access only to the
87 // principals that may hold a secret. It reports whether it had to change
88 // anything.
89 func tightenFile(path string, _ os.FileMode) (bool, error) {
90 return harden(path, false)
91 }
92
93 // tightenDir does the same for a directory, with its entries inheritable so a
94 // file created inside is protected before anything is written to it.
95 func tightenDir(path string, _ os.FileMode) (bool, error) {
96 return harden(path, true)
97 }
98
99 // harden rewrites path's access list and removes the entries it inherited, then
100 // reads the result back. A file that already grants access to the right
101 // principals and nobody else is left alone, so startup does not churn.
102 func harden(path string, dir bool) (bool, error) {
103 if err := verifyACL(path, dir); err == nil {
104 return false, nil
105 }
106 if err := setACL(path, dir); err != nil {
107 return false, err
108 }
109 // SetNamedSecurityInfo reports that the object stored a DACL, not that the
110 // DACL is the one this package meant. An instance that cannot protect its
111 // secrets must not go on to serve them, so the result is checked.
112 if err := verifyACL(path, dir); err != nil {
113 return false, fmt.Errorf("%s: access was rewritten but is still not restricted: %w", path, err)
114 }
115 return true, nil
116 }
117
118 // setACL writes a protected DACL naming only the trusted principals. The DACL is
119 // explicit rather than merged because an inherited entry is exactly what must not
120 // survive: a data directory at the root of a volume starts out readable by Users.
121 func setACL(path string, dir bool) error {
122 t, err := trustedPrincipals()
123 if err != nil {
124 return fmt.Errorf("%s: %w", path, err)
125 }
126 inheritance := uint32(windows.NO_INHERITANCE)
127 if dir {
128 inheritance = windows.SUB_CONTAINERS_AND_OBJECTS_INHERIT
129 }
130 entries := make([]windows.EXPLICIT_ACCESS, 0, len(t.all))
131 // TrusteeValue holds a uintptr, which the garbage collector does not follow,
132 // so the SIDs are pinned for as long as the entries reference them.
133 var pinner runtime.Pinner
134 defer pinner.Unpin()
135 for _, sid := range t.all {
136 pinner.Pin(sid)
137 entries = append(entries, windows.EXPLICIT_ACCESS{
138 AccessPermissions: fileAllAccess,
139 AccessMode: windows.SET_ACCESS,
140 Inheritance: inheritance,
141 Trustee: windows.TRUSTEE{
142 TrusteeForm: windows.TRUSTEE_IS_SID,
143 TrusteeType: windows.TRUSTEE_IS_USER,
144 TrusteeValue: windows.TrusteeValueFromSID(sid),
145 },
146 })
147 }
148 dacl, err := windows.ACLFromEntries(entries, nil)
149 if err != nil {
150 return fmt.Errorf("%s: build an access list: %w", path, err)
151 }
152 err = windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT,
153 windows.DACL_SECURITY_INFORMATION|windows.PROTECTED_DACL_SECURITY_INFORMATION,
154 nil, nil, dacl, nil)
155 runtime.KeepAlive(dacl)
156 if err != nil {
157 return fmt.Errorf("%s: set the access list: %w", path, err)
158 }
159 return nil
160 }
161
162 // VerifyACL reports whether path is protected the way a secret has to be: it is
163 // owned by a trusted principal and its access list grants only trusted
164 // principals, each of them full control, and no entry is a deny or an audit.
165 //
166 // A DACL of zero entries and an absent DACL both mean "everyone", which is why
167 // they are errors here rather than a file to fix later.
168 func VerifyACL(path string) error { return verifyACL(path, false) }
169
170 // verifyACL is VerifyACL with the directory rule: a directory that may hold
171 // secrets must also have stopped inheriting. A file inside a protected directory
172 // inherits entries that the directory's own protection keeps fixed, so files do
173 // not need the flag themselves; a directory that still inherits can be opened by
174 // a later change to its parent, which is the kind of change nobody notices.
175 func verifyACL(path string, requireProtected bool) error {
176 t, err := trustedPrincipals()
177 if err != nil {
178 return err
179 }
180 sd, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT,
181 windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION)
182 if err != nil {
183 return fmt.Errorf("%s: read its security: %w", path, err)
184 }
185 owner, _, err := sd.Owner()
186 if err != nil {
187 return fmt.Errorf("%s: read its owner: %w", path, err)
188 }
189 if owner == nil || !t.includes(owner) {
190 return fmt.Errorf("%s is owned by %s, expected %s", path, t.name(owner), t.name(t.user))
191 }
192 if requireProtected {
193 control, _, err := sd.Control()
194 if err != nil {
195 return fmt.Errorf("%s: read its control flags: %w", path, err)
196 }
197 if control&windows.SE_DACL_PROTECTED == 0 {
198 return fmt.Errorf("%s inherits access from its parent, expected an access list of its own", path)
199 }
200 }
201 dacl, _, err := sd.DACL()
202 if err != nil {
203 return fmt.Errorf("%s: read its access list: %w", path, err)
204 }
205 if dacl == nil {
206 return fmt.Errorf("%s has no access list, which grants everyone full control", path)
207 }
208 granted := false
209 for i := 0; i < int(dacl.AceCount); i++ {
210 var ace *windows.ACCESS_ALLOWED_ACE
211 if err := windows.GetAce(dacl, uint32(i), &ace); err != nil {
212 return fmt.Errorf("%s: read access entry %d: %w", path, i, err)
213 }
214 if ace.Header.AceType != windows.ACCESS_ALLOWED_ACE_TYPE {
215 return fmt.Errorf("%s has an access entry of type %d, expected an allow entry", path, ace.Header.AceType)
216 }
217 sid := (*windows.SID)(unsafe.Pointer(&ace.SidStart))
218 if !t.includes(sid) {
219 return fmt.Errorf("%s grants %s access, expected only %s", path, t.name(sid), t.name(t.user))
220 }
221 if mask := uint32(ace.Mask); mask&fileAllAccess != fileAllAccess {
222 return fmt.Errorf("%s gives %s %#x, expected full control %#x", path, t.name(sid), mask, uint32(fileAllAccess))
223 }
224 if t.user.Equals(sid) {
225 granted = true
226 }
227 }
228 if !granted {
229 return fmt.Errorf("%s does not grant %s access", path, t.name(t.user))
230 }
231 return nil
232 }