Skip to content

Commit 0c2b673

Browse files
committed
Only delete when secure
1 parent a611f8f commit 0c2b673

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

pkg/fleet/installer/packages/datadog_agent_windows.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,14 @@ func removeInstallerIfInstalled(ctx context.Context) (err error) {
360360
return err
361361
}
362362
// remove the old installer directory
363-
err = os.RemoveAll(oldInstallerDir)
364-
if err != nil {
365-
return fmt.Errorf("could not remove old installer directory: %w", err)
363+
// check that owner of oldInstallerDir is admin/system
364+
if nil == paths.IsDirSecure(oldInstallerDir) {
365+
err = os.RemoveAll(oldInstallerDir)
366+
if err != nil {
367+
return fmt.Errorf("could not remove old installer directory: %w", err)
368+
}
369+
} else {
370+
log.Warnf("Old installer directory is not secure, not removing: %s", oldInstallerDir)
366371
}
367372
}
368373
return nil

pkg/fleet/installer/paths/installer_paths_windows.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func secureCreateDirectory(path string, sddl string) error {
124124
// We choose option (b) because it allows us to modify the permissions in the future.
125125
// We check the owner to ensure it is Administrators or SYSTEM before changing the permissions,
126126
// as the owner cannot be set to Administrators by a non-privileged user.
127-
err = isDirSecure(path)
127+
err = IsDirSecure(path)
128128
if err != nil {
129129
// The directory owner is not Administrators or SYSTEM, so may have been created
130130
// by an unknown party. Adjusting the permissions may not be safe, as it won't affect
@@ -152,10 +152,12 @@ func secureCreateDirectory(path string, sddl string) error {
152152
func IsInstallerDataDirSecure() error {
153153
targetDir := DatadogInstallerData
154154
log.Infof("Checking if installer data directory is secure: %s", targetDir)
155-
return isDirSecure(targetDir)
155+
return IsDirSecure(targetDir)
156156
}
157157

158-
func isDirSecure(targetDir string) error {
158+
// IsDirSecure returns nil if the directory is owned by Administrators or SYSTEM,
159+
// otherwise an error is returned.
160+
func IsDirSecure(targetDir string) error {
159161
allowedWellKnownSids := []windows.WELL_KNOWN_SID_TYPE{
160162
windows.WinBuiltinAdministratorsSid,
161163
windows.WinLocalSystemSid,

0 commit comments

Comments
 (0)