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.
Run the forge as a Windows service
9cbfa891425adaa3e2769d1b3f88d5f211190dc6 · Keenan Nemetz · 2d ago · parent 33cbbc9d
Run the forge as a Windows service The Windows server ran from a scheduled task because a process started from an SSH session dies with it, and nothing told the service manager what state the forge was in. This makes `serve` a real service and gives it the commands to install itself. The binary now speaks the SCM: `svc.IsWindowsService` decides, `svc.Run` is entered before any work so a slow instance is a pending state rather than a dispatcher timeout, and a Stop or Shutdown cancels the same context an interrupt cancels, which is what lets the drain finish. The service reports running before the listeners are up, so a bind failure arrives as a nonzero exit and the recovery actions installed with it restart the forge; start and stop failures also go to the Application event log, because a service has no stderr. `vole service install|uninstall|start|stop|status` manages the registration with no wrapper and no second definition: auto start with the standard delay, a description, three restarts as recovery, and the virtual account `NT SERVICE\Vole` with a restricted SID. Install also prepares what that account needs — the data directory and everything already in it, the log directory, and read access to the configuration — through `secure.HardenFor`, a new Windows-only helper that writes a protected access list for principals the running process is not. An instance initialized by a person is readable by the service afterwards, which is the upgrade case, not only the fresh one. `serve --log-file` is the file sink the service needs, opened through the secret store so it is not readable by everyone; the console keeps stderr. The command exists off Windows too and answers that services are the platform's business, so the generated reference is the same everywhere. Verified on the Server Core 2022 guest (full record on the issue): install, start, status, stop, uninstall; the data directory, database, key, and log granted only to the service account, SYSTEM, and Administrators; HTTP 200 through the forwarded port; admin CLI and git push over Vole's SSH listener; a reboot brought the forge back with no login; and a second install refuses with a clear message. The Windows-only unit tests (`stateName`, `lookupSID`) pass there too, and `vole workflow run verify` passes locally. Part of 01M8SE6PR5N7M3X48SMAGA8Z18. Packaging, Authenticode, and winget are 01M8SE6GWX1DGYPY6H9YC142C2.
internal/secure/acl_windows.go +79−46
| @@ -3,9 +3,9 @@ | |||
| 3 | 3 | package secure | |
| 4 | 4 | ||
| 5 | 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. | ||
| 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. | ||
| 9 | 9 | // | |
| 10 | 10 | // The rule this file applies is the unix rule in the platform's terms: the file | |
| 11 | 11 | // is owned by the account that runs the service and its access list names that | |
| @@ -59,30 +59,32 @@ func trustedPrincipals() (*trusted, error) { | |||
| 59 | 59 | return t, nil | |
| 60 | 60 | } | |
| 61 | 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 { | ||
| 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 { | ||
| 66 | 77 | switch { | |
| 67 | case t.user.Equals(sid): | ||
| 68 | return "this account" | ||
| 69 | 78 | case sid.IsWellKnown(windows.WinLocalSystemSid): | |
| 70 | 79 | return `NT AUTHORITY\SYSTEM` | |
| 71 | 80 | case sid.IsWellKnown(windows.WinBuiltinAdministratorsSid): | |
| 72 | 81 | return `BUILTIN\Administrators` | |
| 82 | case sid.IsWellKnown(windows.WinWorldSid): | ||
| 83 | return `Everyone` | ||
| 73 | 84 | } | |
| 74 | 85 | return sid.String() | |
| 75 | 86 | } | |
| 76 | 87 | ||
| 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 | 88 | // tightenFile replaces the file's DACL with one that grants access only to the | |
| 87 | 89 | // principals that may hold a secret. It reports whether it had to change | |
| 88 | 90 | // anything. | |
| @@ -100,39 +102,70 @@ func tightenDir(path string, _ os.FileMode) (bool, error) { | |||
| 100 | 102 | // reads the result back. A file that already grants access to the right | |
| 101 | 103 | // principals and nobody else is left alone, so startup does not churn. | |
| 102 | 104 | func harden(path string, dir bool) (bool, error) { | |
| 103 | if err := verifyACL(path, dir); err == nil { | ||
| 105 | t, err := trustedPrincipals() | ||
| 106 | if err != nil { | ||
| 107 | return false, fmt.Errorf("%s: %w", path, err) | ||
| 108 | } | ||
| 109 | if err := verifyACLFor(path, dir, t.all, t.user); err == nil { | ||
| 104 | 110 | return false, nil | |
| 105 | 111 | } | |
| 106 | if err := setACL(path, dir); err != nil { | ||
| 112 | if err := setACLFor(path, dir, t.all); err != nil { | ||
| 107 | 113 | return false, err | |
| 108 | 114 | } | |
| 109 | 115 | // SetNamedSecurityInfo reports that the object stored a DACL, not that the | |
| 110 | 116 | // DACL is the one this package meant. An instance that cannot protect its | |
| 111 | 117 | // secrets must not go on to serve them, so the result is checked. | |
| 112 | if err := verifyACL(path, dir); err != nil { | ||
| 118 | if err := verifyACLFor(path, dir, t.all, t.user); err != nil { | ||
| 113 | 119 | return false, fmt.Errorf("%s: access was rewritten but is still not restricted: %w", path, err) | |
| 114 | 120 | } | |
| 115 | 121 | return true, nil | |
| 116 | 122 | } | |
| 117 | 123 | ||
| 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) | ||
| 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 | } | ||
| 137 | if err := verifyACLFor(path, dir, sids, nil); err == nil { | ||
| 138 | return nil | ||
| 139 | } | ||
| 140 | if err := setACLFor(path, dir, sids); err != nil { | ||
| 141 | return err | ||
| 125 | 142 | } | |
| 143 | if err := verifyACLFor(path, dir, sids, nil); err != nil { | ||
| 144 | return fmt.Errorf("%s: access was rewritten but is still not restricted: %w", path, err) | ||
| 145 | } | ||
| 146 | return nil | ||
| 147 | } | ||
| 148 | |||
| 149 | // setACLFor writes a protected DACL naming only sids. The DACL is explicit | ||
| 150 | // rather than merged because an inherited entry is exactly what must not survive: | ||
| 151 | // a data directory at the root of a volume starts out readable by Users. | ||
| 152 | func setACLFor(path string, dir bool, sids []*windows.SID) error { | ||
| 126 | 153 | inheritance := uint32(windows.NO_INHERITANCE) | |
| 127 | 154 | if dir { | |
| 128 | 155 | inheritance = windows.SUB_CONTAINERS_AND_OBJECTS_INHERIT | |
| 129 | 156 | } | |
| 130 | entries := make([]windows.EXPLICIT_ACCESS, 0, len(t.all)) | ||
| 157 | unique := make([]*windows.SID, 0, len(sids)) | ||
| 158 | for _, sid := range sids { | ||
| 159 | if !includesSID(unique, sid) { | ||
| 160 | unique = append(unique, sid) | ||
| 161 | } | ||
| 162 | } | ||
| 163 | entries := make([]windows.EXPLICIT_ACCESS, 0, len(unique)) | ||
| 131 | 164 | // TrusteeValue holds a uintptr, which the garbage collector does not follow, | |
| 132 | 165 | // so the SIDs are pinned for as long as the entries reference them. | |
| 133 | 166 | var pinner runtime.Pinner | |
| 134 | 167 | defer pinner.Unpin() | |
| 135 | for _, sid := range t.all { | ||
| 168 | for _, sid := range unique { | ||
| 136 | 169 | pinner.Pin(sid) | |
| 137 | 170 | entries = append(entries, windows.EXPLICIT_ACCESS{ | |
| 138 | 171 | AccessPermissions: fileAllAccess, | |
| @@ -165,18 +198,18 @@ func setACL(path string, dir bool) error { | |||
| 165 | 198 | // | |
| 166 | 199 | // A DACL of zero entries and an absent DACL both mean "everyone", which is why | |
| 167 | 200 | // 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 { | ||
| 201 | func VerifyACL(path string) error { | ||
| 176 | 202 | t, err := trustedPrincipals() | |
| 177 | 203 | if err != nil { | |
| 178 | 204 | return err | |
| 179 | 205 | } | |
| 206 | return verifyACLFor(path, false, t.all, t.user) | ||
| 207 | } | ||
| 208 | |||
| 209 | // verifyACLFor is the check behind VerifyACL and HardenFor. requireProtected is | ||
| 210 | // set for directories, and mustHave names a principal the list has to grant, or | ||
| 211 | // nil when the caller only cares that nothing untrusted is named. | ||
| 212 | func verifyACLFor(path string, requireProtected bool, allowed []*windows.SID, mustHave *windows.SID) error { | ||
| 180 | 213 | sd, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, | |
| 181 | 214 | windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION) | |
| 182 | 215 | if err != nil { | |
| @@ -186,8 +219,8 @@ func verifyACL(path string, requireProtected bool) error { | |||
| 186 | 219 | if err != nil { | |
| 187 | 220 | return fmt.Errorf("%s: read its owner: %w", path, err) | |
| 188 | 221 | } | |
| 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)) | ||
| 222 | if owner == nil || !includesSID(allowed, owner) { | ||
| 223 | return fmt.Errorf("%s is owned by %s, expected %s", path, sidName(owner), sidName(allowed[0])) | ||
| 191 | 224 | } | |
| 192 | 225 | if requireProtected { | |
| 193 | 226 | control, _, err := sd.Control() | |
| @@ -205,7 +238,7 @@ func verifyACL(path string, requireProtected bool) error { | |||
| 205 | 238 | if dacl == nil { | |
| 206 | 239 | return fmt.Errorf("%s has no access list, which grants everyone full control", path) | |
| 207 | 240 | } | |
| 208 | granted := false | ||
| 241 | granted := mustHave == nil | ||
| 209 | 242 | for i := 0; i < int(dacl.AceCount); i++ { | |
| 210 | 243 | var ace *windows.ACCESS_ALLOWED_ACE | |
| 211 | 244 | if err := windows.GetAce(dacl, uint32(i), &ace); err != nil { | |
| @@ -215,18 +248,18 @@ func verifyACL(path string, requireProtected bool) error { | |||
| 215 | 248 | return fmt.Errorf("%s has an access entry of type %d, expected an allow entry", path, ace.Header.AceType) | |
| 216 | 249 | } | |
| 217 | 250 | 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)) | ||
| 251 | if !includesSID(allowed, sid) { | ||
| 252 | return fmt.Errorf("%s grants %s access, expected only %s", path, sidName(sid), sidName(allowed[0])) | ||
| 220 | 253 | } | |
| 221 | 254 | 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)) | ||
| 255 | return fmt.Errorf("%s gives %s %#x, expected full control %#x", path, sidName(sid), mask, uint32(fileAllAccess)) | ||
| 223 | 256 | } | |
| 224 | if t.user.Equals(sid) { | ||
| 257 | if mustHave != nil && mustHave.Equals(sid) { | ||
| 225 | 258 | granted = true | |
| 226 | 259 | } | |
| 227 | 260 | } | |
| 228 | 261 | if !granted { | |
| 229 | return fmt.Errorf("%s does not grant %s access", path, t.name(t.user)) | ||
| 262 | return fmt.Errorf("%s does not grant %s access", path, sidName(mustHave)) | ||
| 230 | 263 | } | |
| 231 | 264 | return nil | |
| 232 | 265 | } | |