|
| 1 | +// Copyright (c) Tailscale Inc & AUTHORS |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +package atomicfile |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + "unsafe" |
| 10 | + |
| 11 | + "golang.org/x/sys/windows" |
| 12 | +) |
| 13 | + |
| 14 | +var _SECURITY_RESOURCE_MANAGER_AUTHORITY = windows.SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 9}} |
| 15 | + |
| 16 | +// makeRandomSID generates a SID derived from a v4 GUID. |
| 17 | +// This is basically the same algorithm used by browser sandboxes for generating |
| 18 | +// random SIDs. |
| 19 | +func makeRandomSID() (*windows.SID, error) { |
| 20 | + guid, err := windows.GenerateGUID() |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + |
| 25 | + rids := *((*[4]uint32)(unsafe.Pointer(&guid))) |
| 26 | + |
| 27 | + var pSID *windows.SID |
| 28 | + if err := windows.AllocateAndInitializeSid(&_SECURITY_RESOURCE_MANAGER_AUTHORITY, 4, rids[0], rids[1], rids[2], rids[3], 0, 0, 0, 0, &pSID); err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + defer windows.FreeSid(pSID) |
| 32 | + |
| 33 | + // Make a copy that lives on the Go heap |
| 34 | + return pSID.Copy() |
| 35 | +} |
| 36 | + |
| 37 | +func getExistingFileSD(name string) (*windows.SECURITY_DESCRIPTOR, error) { |
| 38 | + const infoFlags = windows.DACL_SECURITY_INFORMATION |
| 39 | + return windows.GetNamedSecurityInfo(name, windows.SE_FILE_OBJECT, infoFlags) |
| 40 | +} |
| 41 | + |
| 42 | +func getExistingFileDACL(name string) (*windows.ACL, error) { |
| 43 | + sd, err := getExistingFileSD(name) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + dacl, _, err := sd.DACL() |
| 49 | + return dacl, err |
| 50 | +} |
| 51 | + |
| 52 | +func addDenyACEForRandomSID(dacl *windows.ACL) (*windows.ACL, error) { |
| 53 | + randomSID, err := makeRandomSID() |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + randomSIDTrustee := windows.TRUSTEE{nil, windows.NO_MULTIPLE_TRUSTEE, |
| 59 | + windows.TRUSTEE_IS_SID, windows.TRUSTEE_IS_UNKNOWN, |
| 60 | + windows.TrusteeValueFromSID(randomSID)} |
| 61 | + |
| 62 | + entries := []windows.EXPLICIT_ACCESS{ |
| 63 | + { |
| 64 | + windows.GENERIC_ALL, |
| 65 | + windows.DENY_ACCESS, |
| 66 | + windows.NO_INHERITANCE, |
| 67 | + randomSIDTrustee, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + return windows.ACLFromEntries(entries, dacl) |
| 72 | +} |
| 73 | + |
| 74 | +func setExistingFileDACL(name string, dacl *windows.ACL) error { |
| 75 | + return windows.SetNamedSecurityInfo(name, windows.SE_FILE_OBJECT, |
| 76 | + windows.DACL_SECURITY_INFORMATION, nil, nil, dacl, nil) |
| 77 | +} |
| 78 | + |
| 79 | +// makeOrigFileWithCustomDACL creates a new, temporary file with a custom |
| 80 | +// DACL that we can check for later. It returns the name of the temporary |
| 81 | +// file and the security descriptor for the file in SDDL format. |
| 82 | +func makeOrigFileWithCustomDACL() (name, sddl string, err error) { |
| 83 | + f, err := os.CreateTemp("", "foo*.tmp") |
| 84 | + if err != nil { |
| 85 | + return "", "", err |
| 86 | + } |
| 87 | + name = f.Name() |
| 88 | + if err := f.Close(); err != nil { |
| 89 | + return "", "", err |
| 90 | + } |
| 91 | + f = nil |
| 92 | + defer func() { |
| 93 | + if err != nil { |
| 94 | + os.Remove(name) |
| 95 | + } |
| 96 | + }() |
| 97 | + |
| 98 | + dacl, err := getExistingFileDACL(name) |
| 99 | + if err != nil { |
| 100 | + return "", "", err |
| 101 | + } |
| 102 | + |
| 103 | + // Add a harmless, deny-only ACE for a random SID that isn't used for anything |
| 104 | + // (but that we can check for later). |
| 105 | + dacl, err = addDenyACEForRandomSID(dacl) |
| 106 | + if err != nil { |
| 107 | + return "", "", err |
| 108 | + } |
| 109 | + |
| 110 | + if err := setExistingFileDACL(name, dacl); err != nil { |
| 111 | + return "", "", err |
| 112 | + } |
| 113 | + |
| 114 | + sd, err := getExistingFileSD(name) |
| 115 | + if err != nil { |
| 116 | + return "", "", err |
| 117 | + } |
| 118 | + |
| 119 | + return name, sd.String(), nil |
| 120 | +} |
| 121 | + |
| 122 | +func TestPreserveSecurityInfo(t *testing.T) { |
| 123 | + // Make a test file with a custom ACL. |
| 124 | + origFileName, want, err := makeOrigFileWithCustomDACL() |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("makeOrigFileWithCustomDACL returned %v", err) |
| 127 | + } |
| 128 | + t.Cleanup(func() { |
| 129 | + os.Remove(origFileName) |
| 130 | + }) |
| 131 | + |
| 132 | + if err := WriteFile(origFileName, []byte{}, 0); err != nil { |
| 133 | + t.Fatalf("WriteFile returned %v", err) |
| 134 | + } |
| 135 | + |
| 136 | + // We expect origFileName's security descriptor to be unchanged despite |
| 137 | + // the WriteFile call. |
| 138 | + sd, err := getExistingFileSD(origFileName) |
| 139 | + if err != nil { |
| 140 | + t.Fatalf("getExistingFileSD(%q) returned %v", origFileName, err) |
| 141 | + } |
| 142 | + |
| 143 | + if got := sd.String(); got != want { |
| 144 | + t.Errorf("security descriptor comparison failed: got %q, want %q", got, want) |
| 145 | + } |
| 146 | +} |
0 commit comments