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.

Code

527 commit(s) on main

Put job execution behind an interface Keenan Nemetz · 2h ago de3eec16
history blame raw 6.4 KiB
1//go:build windows
2
3package 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
12import (
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.
24func 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.
55func 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.
96func 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 trusted, err := trustedPrincipals()
111 if err != nil {
112 t.Fatal(err)
113 }
114 if err := verifyACLFor(dir, true, trusted.all, []*windows.SID{trusted.user}); err != nil {
115 t.Fatalf("the directory still inherits: %v", err)
116 }
117
118 file := filepath.Join(dir, "instance.db")
119 if err := os.WriteFile(file, []byte("keys"), 0o600); err != nil {
120 t.Fatal(err)
121 }
122 if err := VerifyACL(file); err != nil {
123 t.Fatalf("a file created in a protected directory is not protected: %v", err)
124 }
125 // The file inherits from a protected directory, which is the arrangement that
126 // makes protection reach files nobody has opened yet; the flag belongs to the
127 // directory.
128 if err := VerifyACL(file); err != nil {
129 t.Fatalf("the file's inherited access list is not restricted: %v", err)
130 }
131}
132
133// TestHardenForAddsEveryRequestedPrincipal is the case that caught a real bug:
134// a list naming SYSTEM and Administrators is a subset of the list the installer
135// wants, so a "nothing untrusted" check passed while the account that had to read
136// the file was missing from it.
137func TestHardenForAddsEveryRequestedPrincipal(t *testing.T) {
138 path := filepath.Join(t.TempDir(), "vole.toml")
139 if err := os.WriteFile(path, []byte("data_dir = 'C:\\data'\n"), 0o600); err != nil {
140 t.Fatal(err)
141 }
142 system, err := windows.CreateWellKnownSid(windows.WinLocalSystemSid)
143 if err != nil {
144 t.Fatal(err)
145 }
146 administrators, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid)
147 if err != nil {
148 t.Fatal(err)
149 }
150 users, err := windows.CreateWellKnownSid(windows.WinBuiltinUsersSid)
151 if err != nil {
152 t.Fatal(err)
153 }
154
155 // Start from the half the package installer would write first.
156 if err := HardenFor(path, false, []*windows.SID{system, administrators}); err != nil {
157 t.Fatal(err)
158 }
159 // Then ask for one more principal, as a service install does for its account.
160 if err := HardenFor(path, false, []*windows.SID{system, administrators, users}); err != nil {
161 t.Fatal(err)
162 }
163 if err := verifyACLFor(path, false, []*windows.SID{system, administrators, users}, []*windows.SID{users}); err != nil {
164 t.Fatalf("the requested principal was not added: %v", err)
165 }
166}
167
168// TestVerifyACLRejectsAnotherAccount checks the half a DACL rewrite cannot fix:
169// a secret owned by an account that has no business holding one is refused rather
170// than accepted as somebody else's problem.
171func TestVerifyACLRejectsAnotherAccount(t *testing.T) {
172 path := filepath.Join(t.TempDir(), "instance.db")
173 if err := os.WriteFile(path, []byte("keys"), 0o600); err != nil {
174 t.Fatal(err)
175 }
176 // Take ownership away from this account. That needs SeRestorePrivilege, so on
177 // a test runner without it the case cannot be built; skipping is better than a
178 // test that silently stops checking anything.
179 world, err := windows.CreateWellKnownSid(windows.WinWorldSid)
180 if err != nil {
181 t.Fatal(err)
182 }
183 if err := windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT,
184 windows.OWNER_SECURITY_INFORMATION, world, nil, nil, nil); err != nil {
185 t.Skipf("cannot set the owner without SeRestorePrivilege: %v", err)
186 }
187 if err := VerifyACL(path); err == nil {
188 t.Fatal("a file owned by Everyone passed verification")
189 }
190}
191