2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-09-21 21:00:30 +00:00
|
|
|
|
|
|
|
package paths
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-09-24 21:24:16 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-09-21 21:00:30 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2022-05-25 21:51:54 +00:00
|
|
|
"tailscale.com/util/winutil"
|
2021-09-21 21:00:30 +00:00
|
|
|
)
|
|
|
|
|
2023-08-24 04:19:11 +00:00
|
|
|
func init() {
|
|
|
|
ensureStateDirPerms = ensureStateDirPermsWindows
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureStateDirPermsWindows applies a restrictive ACL to the directory specified by dirPath.
|
2021-09-21 21:00:30 +00:00
|
|
|
// It sets the following security attributes on the directory:
|
|
|
|
// Owner: The user for the current process;
|
|
|
|
// Primary Group: The primary group for the current process;
|
|
|
|
// DACL: Full control to the current user and to the Administrators group.
|
2022-08-02 16:33:46 +00:00
|
|
|
//
|
|
|
|
// (We include Administrators so that admin users may still access logs;
|
|
|
|
// granting access exclusively to LocalSystem would require admins to use
|
|
|
|
// special tools to access the Log directory)
|
|
|
|
//
|
2021-09-21 21:00:30 +00:00
|
|
|
// Inheritance: The directory does not inherit the ACL from its parent.
|
2022-08-02 16:33:46 +00:00
|
|
|
//
|
|
|
|
// However, any directories and/or files created within this
|
|
|
|
// directory *do* inherit the ACL that we are setting.
|
2023-08-24 04:19:11 +00:00
|
|
|
func ensureStateDirPermsWindows(dirPath string) error {
|
2021-09-21 21:00:30 +00:00
|
|
|
fi, err := os.Stat(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
2021-09-24 21:24:16 +00:00
|
|
|
if strings.ToLower(filepath.Base(dirPath)) != "tailscale" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-21 21:00:30 +00:00
|
|
|
|
|
|
|
// We need the info for our current user as SIDs
|
2022-05-25 21:51:54 +00:00
|
|
|
sids, err := winutil.GetCurrentUserSIDs()
|
2021-09-21 21:00:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We also need the SID for the Administrators group so that admins may
|
|
|
|
// easily access logs.
|
|
|
|
adminGroupSid, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Munge the SIDs into the format required by EXPLICIT_ACCESS.
|
|
|
|
userTrustee := windows.TRUSTEE{nil, windows.NO_MULTIPLE_TRUSTEE,
|
|
|
|
windows.TRUSTEE_IS_SID, windows.TRUSTEE_IS_USER,
|
|
|
|
windows.TrusteeValueFromSID(sids.User)}
|
|
|
|
|
|
|
|
adminTrustee := windows.TRUSTEE{nil, windows.NO_MULTIPLE_TRUSTEE,
|
|
|
|
windows.TRUSTEE_IS_SID, windows.TRUSTEE_IS_WELL_KNOWN_GROUP,
|
|
|
|
windows.TrusteeValueFromSID(adminGroupSid)}
|
|
|
|
|
|
|
|
// We declare our access rights via this array of EXPLICIT_ACCESS structures.
|
|
|
|
// We set full access to our user and to Administrators.
|
|
|
|
// We configure the DACL such that any files or directories created within
|
|
|
|
// dirPath will also inherit this DACL.
|
|
|
|
explicitAccess := []windows.EXPLICIT_ACCESS{
|
2021-12-15 16:42:25 +00:00
|
|
|
{
|
2021-09-21 21:00:30 +00:00
|
|
|
windows.GENERIC_ALL,
|
|
|
|
windows.SET_ACCESS,
|
|
|
|
windows.SUB_CONTAINERS_AND_OBJECTS_INHERIT,
|
|
|
|
userTrustee,
|
|
|
|
},
|
2021-12-15 16:42:25 +00:00
|
|
|
{
|
2021-09-21 21:00:30 +00:00
|
|
|
windows.GENERIC_ALL,
|
|
|
|
windows.SET_ACCESS,
|
|
|
|
windows.SUB_CONTAINERS_AND_OBJECTS_INHERIT,
|
|
|
|
adminTrustee,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
dacl, err := windows.ACLFromEntries(explicitAccess, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We now reset the file's owner, primary group, and DACL.
|
|
|
|
// We also must pass PROTECTED_DACL_SECURITY_INFORMATION so that our new ACL
|
|
|
|
// does not inherit any ACL entries from the parent directory.
|
|
|
|
const flags = windows.OWNER_SECURITY_INFORMATION |
|
|
|
|
windows.GROUP_SECURITY_INFORMATION |
|
|
|
|
windows.DACL_SECURITY_INFORMATION |
|
|
|
|
windows.PROTECTED_DACL_SECURITY_INFORMATION
|
|
|
|
return windows.SetNamedSecurityInfo(dirPath, windows.SE_FILE_OBJECT, flags,
|
|
|
|
sids.User, sids.PrimaryGroup, dacl, nil)
|
|
|
|
}
|