paths_windows.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package paths
  4. import (
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "golang.org/x/sys/windows"
  9. "tailscale.com/util/winutil"
  10. )
  11. func init() {
  12. ensureStateDirPerms = ensureStateDirPermsWindows
  13. }
  14. // ensureStateDirPermsWindows applies a restrictive ACL to the directory specified by dirPath.
  15. // It sets the following security attributes on the directory:
  16. // Owner: The user for the current process;
  17. // Primary Group: The primary group for the current process;
  18. // DACL: Full control to the current user and to the Administrators group.
  19. //
  20. // (We include Administrators so that admin users may still access logs;
  21. // granting access exclusively to LocalSystem would require admins to use
  22. // special tools to access the Log directory)
  23. //
  24. // Inheritance: The directory does not inherit the ACL from its parent.
  25. //
  26. // However, any directories and/or files created within this
  27. // directory *do* inherit the ACL that we are setting.
  28. func ensureStateDirPermsWindows(dirPath string) error {
  29. fi, err := os.Stat(dirPath)
  30. if err != nil {
  31. return err
  32. }
  33. if !fi.IsDir() {
  34. return os.ErrInvalid
  35. }
  36. if strings.ToLower(filepath.Base(dirPath)) != "tailscale" {
  37. return nil
  38. }
  39. // We need the info for our current user as SIDs
  40. sids, err := winutil.GetCurrentUserSIDs()
  41. if err != nil {
  42. return err
  43. }
  44. // We also need the SID for the Administrators group so that admins may
  45. // easily access logs.
  46. adminGroupSid, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid)
  47. if err != nil {
  48. return err
  49. }
  50. // Munge the SIDs into the format required by EXPLICIT_ACCESS.
  51. userTrustee := windows.TRUSTEE{nil, windows.NO_MULTIPLE_TRUSTEE,
  52. windows.TRUSTEE_IS_SID, windows.TRUSTEE_IS_USER,
  53. windows.TrusteeValueFromSID(sids.User)}
  54. adminTrustee := windows.TRUSTEE{nil, windows.NO_MULTIPLE_TRUSTEE,
  55. windows.TRUSTEE_IS_SID, windows.TRUSTEE_IS_WELL_KNOWN_GROUP,
  56. windows.TrusteeValueFromSID(adminGroupSid)}
  57. // We declare our access rights via this array of EXPLICIT_ACCESS structures.
  58. // We set full access to our user and to Administrators.
  59. // We configure the DACL such that any files or directories created within
  60. // dirPath will also inherit this DACL.
  61. explicitAccess := []windows.EXPLICIT_ACCESS{
  62. {
  63. windows.GENERIC_ALL,
  64. windows.SET_ACCESS,
  65. windows.SUB_CONTAINERS_AND_OBJECTS_INHERIT,
  66. userTrustee,
  67. },
  68. {
  69. windows.GENERIC_ALL,
  70. windows.SET_ACCESS,
  71. windows.SUB_CONTAINERS_AND_OBJECTS_INHERIT,
  72. adminTrustee,
  73. },
  74. }
  75. dacl, err := windows.ACLFromEntries(explicitAccess, nil)
  76. if err != nil {
  77. return err
  78. }
  79. // We now reset the file's owner, primary group, and DACL.
  80. // We also must pass PROTECTED_DACL_SECURITY_INFORMATION so that our new ACL
  81. // does not inherit any ACL entries from the parent directory.
  82. const flags = windows.OWNER_SECURITY_INFORMATION |
  83. windows.GROUP_SECURITY_INFORMATION |
  84. windows.DACL_SECURITY_INFORMATION |
  85. windows.PROTECTED_DACL_SECURITY_INFORMATION
  86. return windows.SetNamedSecurityInfo(dirPath, windows.SE_FILE_OBJECT, flags,
  87. sids.User, sids.PrimaryGroup, dacl, nil)
  88. }