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.
|
33cbbc9d
Keenan Nemetz
2d ago
|
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 | |
|
9cbfa891
Keenan Nemetz
2d ago
|
6 | // of a Windows file record only record the read-only attribute, so "0600" there |
| 7 | // would mean "read-only", which protects nothing and breaks the service that has | |
| 8 | // to write its own key. | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
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 | ||
|
9cbfa891
Keenan Nemetz
2d ago
|
62 | func (t *trusted) includes(sid *windows.SID) bool { return includesSID(t.all, sid) } |
| 63 | ||
| 64 | func includesSID(sids []*windows.SID, sid *windows.SID) bool { | |
| 65 | for _, allowed := range sids { | |
| 66 | if allowed.Equals(sid) { | |
| 67 | return true | |
| 68 | } | |
| 69 | } | |
| 70 | return false | |
| 71 | } | |
| 72 | ||
| 73 | // sidName renders a principal for an error message. The two machine principals | |
| 74 | // have names; anything else is shown as its SID, which is exact and needs no | |
| 75 | // directory round trip. | |
| 76 | func sidName(sid *windows.SID) string { | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
77 | switch { |
| 78 | case sid.IsWellKnown(windows.WinLocalSystemSid): | |
| 79 | return `NT AUTHORITY\SYSTEM` | |
| 80 | case sid.IsWellKnown(windows.WinBuiltinAdministratorsSid): | |
| 81 | return `BUILTIN\Administrators` | |
|
9cbfa891
Keenan Nemetz
2d ago
|
82 | case sid.IsWellKnown(windows.WinWorldSid): |
| 83 | return `Everyone` | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
84 | } |
| 85 | return sid.String() | |
| 86 | } | |
| 87 | ||
| 88 | // tightenFile replaces the file's DACL with one that grants access only to the | |
| 89 | // principals that may hold a secret. It reports whether it had to change | |
| 90 | // anything. | |
| 91 | func tightenFile(path string, _ os.FileMode) (bool, error) { | |
| 92 | return harden(path, false) | |
| 93 | } | |
| 94 | ||
| 95 | // tightenDir does the same for a directory, with its entries inheritable so a | |
| 96 | // file created inside is protected before anything is written to it. | |
| 97 | func tightenDir(path string, _ os.FileMode) (bool, error) { | |
| 98 | return harden(path, true) | |
| 99 | } | |
| 100 | ||
| 101 | // harden rewrites path's access list and removes the entries it inherited, then | |
| 102 | // reads the result back. A file that already grants access to the right | |
| 103 | // principals and nobody else is left alone, so startup does not churn. | |
| 104 | func harden(path string, dir bool) (bool, error) { | |
|
9cbfa891
Keenan Nemetz
2d ago
|
105 | t, err := trustedPrincipals() |
| 106 | if err != nil { | |
| 107 | return false, fmt.Errorf("%s: %w", path, err) | |
| 108 | } | |
|
31560d91
Keenan Nemetz
2d ago
|
109 | if err := verifyACLFor(path, dir, t.all, []*windows.SID{t.user}); err == nil { |
|
33cbbc9d
Keenan Nemetz
2d ago
|
110 | return false, nil |
| 111 | } | |
|
9cbfa891
Keenan Nemetz
2d ago
|
112 | if err := setACLFor(path, dir, t.all); err != nil { |
|
33cbbc9d
Keenan Nemetz
2d ago
|
113 | return false, err |
| 114 | } | |
| 115 | // SetNamedSecurityInfo reports that the object stored a DACL, not that the | |
| 116 | // DACL is the one this package meant. An instance that cannot protect its | |
| 117 | // secrets must not go on to serve them, so the result is checked. | |
|
31560d91
Keenan Nemetz
2d ago
|
118 | if err := verifyACLFor(path, dir, t.all, []*windows.SID{t.user}); err != nil { |
|
33cbbc9d
Keenan Nemetz
2d ago
|
119 | return false, fmt.Errorf("%s: access was rewritten but is still not restricted: %w", path, err) |
| 120 | } | |
| 121 | return true, nil | |
| 122 | } | |
| 123 | ||
|
9cbfa891
Keenan Nemetz
2d ago
|
124 | // HardenFor gives path a protected access list naming exactly sids, each at full |
| 125 | // control, and inheritable when dir is set. It exists for an installer preparing | |
| 126 | // a directory for an account that is not the one running the installer — the | |
| 127 | // Windows service account — so the runtime check that follows, as that account, | |
| 128 | // agrees with what the installer wrote. | |
| 129 | // | |
| 130 | // The caller owns the SID list; VerifyACL is the rule for a running instance and | |
| 131 | // is deliberately not used here, because the process writing this list is not | |
| 132 | // necessarily one of the principals in it. | |
| 133 | func HardenFor(path string, dir bool, sids []*windows.SID) error { | |
| 134 | if len(sids) == 0 { | |
| 135 | return fmt.Errorf("%s: no account to grant access to", path) | |
| 136 | } | |
|
31560d91
Keenan Nemetz
2d ago
|
137 | // Every requested principal has to be present, not merely every present |
| 138 | // principal allowed: a list that already names SYSTEM and Administrators | |
| 139 | // looks valid when the new account is simply missing, and the service then | |
| 140 | // cannot read its own configuration. | |
| 141 | if err := verifyACLFor(path, dir, sids, sids); err == nil { | |
|
9cbfa891
Keenan Nemetz
2d ago
|
142 | return nil |
| 143 | } | |
| 144 | if err := setACLFor(path, dir, sids); err != nil { | |
| 145 | return err | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
146 | } |
|
31560d91
Keenan Nemetz
2d ago
|
147 | if err := verifyACLFor(path, dir, sids, sids); err != nil { |
|
9cbfa891
Keenan Nemetz
2d ago
|
148 | return fmt.Errorf("%s: access was rewritten but is still not restricted: %w", path, err) |
| 149 | } | |
| 150 | return nil | |
| 151 | } | |
| 152 | ||
| 153 | // setACLFor writes a protected DACL naming only sids. The DACL is explicit | |
| 154 | // rather than merged because an inherited entry is exactly what must not survive: | |
| 155 | // a data directory at the root of a volume starts out readable by Users. | |
| 156 | func setACLFor(path string, dir bool, sids []*windows.SID) error { | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
157 | inheritance := uint32(windows.NO_INHERITANCE) |
| 158 | if dir { | |
| 159 | inheritance = windows.SUB_CONTAINERS_AND_OBJECTS_INHERIT | |
| 160 | } | |
|
9cbfa891
Keenan Nemetz
2d ago
|
161 | unique := make([]*windows.SID, 0, len(sids)) |
| 162 | for _, sid := range sids { | |
| 163 | if !includesSID(unique, sid) { | |
| 164 | unique = append(unique, sid) | |
| 165 | } | |
| 166 | } | |
| 167 | entries := make([]windows.EXPLICIT_ACCESS, 0, len(unique)) | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
168 | // TrusteeValue holds a uintptr, which the garbage collector does not follow, |
| 169 | // so the SIDs are pinned for as long as the entries reference them. | |
| 170 | var pinner runtime.Pinner | |
| 171 | defer pinner.Unpin() | |
|
9cbfa891
Keenan Nemetz
2d ago
|
172 | for _, sid := range unique { |
|
33cbbc9d
Keenan Nemetz
2d ago
|
173 | pinner.Pin(sid) |
| 174 | entries = append(entries, windows.EXPLICIT_ACCESS{ | |
| 175 | AccessPermissions: fileAllAccess, | |
| 176 | AccessMode: windows.SET_ACCESS, | |
| 177 | Inheritance: inheritance, | |
| 178 | Trustee: windows.TRUSTEE{ | |
| 179 | TrusteeForm: windows.TRUSTEE_IS_SID, | |
| 180 | TrusteeType: windows.TRUSTEE_IS_USER, | |
| 181 | TrusteeValue: windows.TrusteeValueFromSID(sid), | |
| 182 | }, | |
| 183 | }) | |
| 184 | } | |
| 185 | dacl, err := windows.ACLFromEntries(entries, nil) | |
| 186 | if err != nil { | |
| 187 | return fmt.Errorf("%s: build an access list: %w", path, err) | |
| 188 | } | |
| 189 | err = windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, | |
| 190 | windows.DACL_SECURITY_INFORMATION|windows.PROTECTED_DACL_SECURITY_INFORMATION, | |
| 191 | nil, nil, dacl, nil) | |
| 192 | runtime.KeepAlive(dacl) | |
| 193 | if err != nil { | |
| 194 | return fmt.Errorf("%s: set the access list: %w", path, err) | |
| 195 | } | |
| 196 | return nil | |
| 197 | } | |
| 198 | ||
| 199 | // VerifyACL reports whether path is protected the way a secret has to be: it is | |
| 200 | // owned by a trusted principal and its access list grants only trusted | |
| 201 | // principals, each of them full control, and no entry is a deny or an audit. | |
| 202 | // | |
| 203 | // A DACL of zero entries and an absent DACL both mean "everyone", which is why | |
| 204 | // they are errors here rather than a file to fix later. | |
|
9cbfa891
Keenan Nemetz
2d ago
|
205 | func VerifyACL(path string) error { |
|
33cbbc9d
Keenan Nemetz
2d ago
|
206 | t, err := trustedPrincipals() |
| 207 | if err != nil { | |
| 208 | return err | |
| 209 | } | |
|
31560d91
Keenan Nemetz
2d ago
|
210 | return verifyACLFor(path, false, t.all, []*windows.SID{t.user}) |
|
9cbfa891
Keenan Nemetz
2d ago
|
211 | } |
| 212 | ||
| 213 | // verifyACLFor is the check behind VerifyACL and HardenFor. requireProtected is | |
|
31560d91
Keenan Nemetz
2d ago
|
214 | // set for directories; required names the principals the list has to grant, so |
| 215 | // an installer cannot mistake "nothing untrusted is named" for "the account that | |
| 216 | // needs access has it". | |
| 217 | func verifyACLFor(path string, requireProtected bool, allowed, required []*windows.SID) error { | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
218 | sd, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, |
| 219 | windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION) | |
| 220 | if err != nil { | |
| 221 | return fmt.Errorf("%s: read its security: %w", path, err) | |
| 222 | } | |
| 223 | owner, _, err := sd.Owner() | |
| 224 | if err != nil { | |
| 225 | return fmt.Errorf("%s: read its owner: %w", path, err) | |
| 226 | } | |
|
9cbfa891
Keenan Nemetz
2d ago
|
227 | if owner == nil || !includesSID(allowed, owner) { |
| 228 | return fmt.Errorf("%s is owned by %s, expected %s", path, sidName(owner), sidName(allowed[0])) | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
229 | } |
| 230 | if requireProtected { | |
| 231 | control, _, err := sd.Control() | |
| 232 | if err != nil { | |
| 233 | return fmt.Errorf("%s: read its control flags: %w", path, err) | |
| 234 | } | |
| 235 | if control&windows.SE_DACL_PROTECTED == 0 { | |
| 236 | return fmt.Errorf("%s inherits access from its parent, expected an access list of its own", path) | |
| 237 | } | |
| 238 | } | |
| 239 | dacl, _, err := sd.DACL() | |
| 240 | if err != nil { | |
| 241 | return fmt.Errorf("%s: read its access list: %w", path, err) | |
| 242 | } | |
| 243 | if dacl == nil { | |
| 244 | return fmt.Errorf("%s has no access list, which grants everyone full control", path) | |
| 245 | } | |
|
31560d91
Keenan Nemetz
2d ago
|
246 | var granted []*windows.SID |
|
33cbbc9d
Keenan Nemetz
2d ago
|
247 | for i := 0; i < int(dacl.AceCount); i++ { |
| 248 | var ace *windows.ACCESS_ALLOWED_ACE | |
| 249 | if err := windows.GetAce(dacl, uint32(i), &ace); err != nil { | |
| 250 | return fmt.Errorf("%s: read access entry %d: %w", path, i, err) | |
| 251 | } | |
| 252 | if ace.Header.AceType != windows.ACCESS_ALLOWED_ACE_TYPE { | |
| 253 | return fmt.Errorf("%s has an access entry of type %d, expected an allow entry", path, ace.Header.AceType) | |
| 254 | } | |
| 255 | sid := (*windows.SID)(unsafe.Pointer(&ace.SidStart)) | |
|
9cbfa891
Keenan Nemetz
2d ago
|
256 | if !includesSID(allowed, sid) { |
| 257 | return fmt.Errorf("%s grants %s access, expected only %s", path, sidName(sid), sidName(allowed[0])) | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
258 | } |
| 259 | if mask := uint32(ace.Mask); mask&fileAllAccess != fileAllAccess { | |
|
9cbfa891
Keenan Nemetz
2d ago
|
260 | return fmt.Errorf("%s gives %s %#x, expected full control %#x", path, sidName(sid), mask, uint32(fileAllAccess)) |
|
33cbbc9d
Keenan Nemetz
2d ago
|
261 | } |
|
31560d91
Keenan Nemetz
2d ago
|
262 | if includesSID(required, sid) { |
| 263 | granted = append(granted, sid) | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
264 | } |
| 265 | } | |
|
31560d91
Keenan Nemetz
2d ago
|
266 | for _, sid := range required { |
| 267 | if !includesSID(granted, sid) { | |
| 268 | return fmt.Errorf("%s does not grant %s access", path, sidName(sid)) | |
| 269 | } | |
|
33cbbc9d
Keenan Nemetz
2d ago
|
270 | } |
| 271 | return nil | |
| 272 | } |