Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ProgramData Directory permissions #32117

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Each section from every release note are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
security:
- |
Removes Datadog user's full control of the Datadog data directory on windows.
3 changes: 2 additions & 1 deletion test/new-e2e/tests/windows/install-test/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
windows "github.com/DataDog/datadog-agent/test/new-e2e/tests/windows/common"
windowsAgent "github.com/DataDog/datadog-agent/test/new-e2e/tests/windows/common/agent"

"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

// AssertInstalledUserInRegistry checks the registry for the installed user and domain
Expand Down
69 changes: 65 additions & 4 deletions test/new-e2e/tests/windows/install-test/installtester.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ func (t *Tester) testInstalledFilePermissions(tt *testing.T, ddAgentUserIdentity
path string
expectedSecurity func(t *testing.T) windows.ObjectSecurity
}{
//ConfigRoot is only owned by SYSTEM and Administrators
{
name: "ConfigRoot",
path: t.expectedConfigRoot,
Expand All @@ -478,11 +479,27 @@ func (t *Tester) testInstalledFilePermissions(tt *testing.T, ddAgentUserIdentity
expected.Access = append(expected.Access,
windows.NewExplicitAccessRuleWithFlags(
ddAgentUserIdentity,
windows.FileFullControl,
windows.FileReadAndExecute|windows.SYNCHRONIZE,
windows.AccessControlTypeAllow,
windows.InheritanceFlagsContainer|windows.InheritanceFlagsObject,
windows.PropagationFlagsNone,
),
windows.NewExplicitAccessRuleWithFlags(
ddAgentUserIdentity,
windows.FileWrite|windows.SYNCHRONIZE,
windows.AccessControlTypeAllow,
windows.InheritanceFlagsNone,
windows.PropagationFlagsNone,
),
// add creator owner permissions
windows.NewExplicitAccessRuleWithFlags(
windows.GetIdentityForSID("S-1-3-0"),
windows.FileFullControl,
windows.AccessControlTypeAllow,
windows.InheritanceFlagsContainer|windows.InheritanceFlagsObject,
// yes this flag is wrong, but go has the wrong value for it
windows.PropagationFlagsNoPropagate,
Comment on lines +500 to +501
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could create our own flag in common/ace.go to make it less confusing

),
)
return expected
},
Expand All @@ -497,11 +514,17 @@ func (t *Tester) testInstalledFilePermissions(tt *testing.T, ddAgentUserIdentity
return expected
}
expected.Access = append(expected.Access,
windows.NewInheritedAccessRule(
windows.NewExplicitAccessRule(
ddAgentUserIdentity,
windows.FileFullControl,
windows.AccessControlTypeAllow,
),
// extra inherited rule for ddagentuser
windows.NewInheritedAccessRule(
ddAgentUserIdentity,
windows.FileReadAndExecute|windows.SYNCHRONIZE,
windows.AccessControlTypeAllow,
),
)
return expected
},
Expand All @@ -515,15 +538,53 @@ func (t *Tester) testInstalledFilePermissions(tt *testing.T, ddAgentUserIdentity
if windows.IsIdentityLocalSystem(ddAgentUserIdentity) {
return expected
}
expected.Access = append(expected.Access,
expected.Access = []windows.AccessRule{
windows.NewInheritedAccessRuleWithFlags(
windows.GetIdentityForSID(windows.LocalSystemSID),
windows.FileFullControl,
windows.AccessControlTypeAllow,
windows.InheritanceFlagsContainer|windows.InheritanceFlagsObject,
windows.PropagationFlagsNoPropagate,
),
windows.NewInheritedAccessRuleWithFlags(
windows.GetIdentityForSID(windows.AdministratorsSID),
windows.FileFullControl,
windows.AccessControlTypeAllow,
windows.InheritanceFlagsContainer|windows.InheritanceFlagsObject,
windows.PropagationFlagsNone,
),
windows.NewExplicitAccessRuleWithFlags(
ddAgentUserIdentity,
windows.FileFullControl,
windows.AccessControlTypeAllow,
windows.InheritanceFlagsContainer|windows.InheritanceFlagsObject,
windows.PropagationFlagsNone,
),
// extra inherited rule for ddagentuser
windows.NewInheritedAccessRuleWithFlags(
ddAgentUserIdentity,
windows.FileReadAndExecute|windows.SYNCHRONIZE,
windows.AccessControlTypeAllow,
windows.InheritanceFlagsContainer|windows.InheritanceFlagsObject,
windows.PropagationFlagsNone,
),
// add creator owner permissions
windows.NewInheritedAccessRuleWithFlags(
windows.GetIdentityForSID("S-1-3-0"),
windows.FileFullControl,
windows.AccessControlTypeAllow,
windows.InheritanceFlagsContainer|windows.InheritanceFlagsObject,
windows.PropagationFlagsNoPropagate,
),
// create owner inherited permissions
windows.NewInheritedAccessRuleWithFlags(
windows.GetIdentityForSID(windows.LocalSystemSID),
windows.FileFullControl,
windows.AccessControlTypeAllow,
windows.PropagationFlagsNone,
windows.InheritanceFlagsNone,
),
)
}
return expected
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,103 @@ private void GrantAgentAccessPermissions()
}
}

private void AddDatadogUserToDataFolder()
{
var dataDirectory = _session.Property("APPLICATIONDATADIRECTORY");

FileSystemSecurity fileSystemSecurity;
try
{
fileSystemSecurity = _fileSystemServices.GetAccessControl(dataDirectory, AccessControlSections.All);
}
catch (Exception e)
{
_session.Log($"Failed to get ACLs on {dataDirectory}: {e}");
throw;
}
// ddagentuser Read and execute permissions, enable child inheritance of this ACE
fileSystemSecurity.AddAccessRule(new FileSystemAccessRule(
_ddAgentUserSID,
FileSystemRights.ReadAndExecute | FileSystemRights.Synchronize,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));

// datadog write on this folder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// datadog write on this folder
// datadog write on this folder
// This allows creating new files/folders, but not deleting or modifying permissions.

fileSystemSecurity.AddAccessRule(new FileSystemAccessRule(
_ddAgentUserSID,
FileSystemRights.WriteData | FileSystemRights.AppendData | FileSystemRights.WriteAttributes | FileSystemRights.WriteExtendedAttributes | FileSystemRights.Synchronize,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Allow));

// add full control to CREATOR OWNER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// add full control to CREATOR OWNER
// add full control to CREATOR OWNER
// Grants FullControl to any files/directories created by the Agent user
// Marked InherityOnly so it applies only to children and not this directory

fileSystemSecurity.AddAccessRule(new FileSystemAccessRule(
new SecurityIdentifier(WellKnownSidType.CreatorOwnerSid, null),
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.InheritOnly,
AccessControlType.Allow));
try
{
UpdateAndLogAccessControl(dataDirectory, fileSystemSecurity);
}
catch (Exception e)
{
_session.Log($"Failed to set ACLs on {dataDirectory}: {e}");
throw;
}

}

private void RemoveDatadogUserFromDataFolder(SecurityIdentifier sid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should set it to SYSTEM/Admin only instead of removing agent user ACE's ?

{
var dataDirectory = _session.Property("APPLICATIONDATADIRECTORY");

FileSystemSecurity fileSystemSecurity;
try
{
fileSystemSecurity = _fileSystemServices.GetAccessControl(dataDirectory, AccessControlSections.All);
}
catch (Exception e)
{
_session.Log($"Failed to get ACLs on {dataDirectory}: {e}");
throw;
}

// Remove ddagentuser from data folder
fileSystemSecurity.RemoveAccessRule(new FileSystemAccessRule(
sid,
FileSystemRights.ReadAndExecute | FileSystemRights.Synchronize,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
fileSystemSecurity.RemoveAccessRule(new FileSystemAccessRule(
sid,
FileSystemRights.Write,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Allow));

// remove full control to CREATOR OWNER
fileSystemSecurity.RemoveAccessRule(new FileSystemAccessRule(
new SecurityIdentifier(WellKnownSidType.CreatorOwnerSid, null),
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.InheritOnly,
AccessControlType.Allow));

try
{
UpdateAndLogAccessControl(dataDirectory, fileSystemSecurity);
}
catch (Exception e)
{
_session.Log($"Failed to set ACLs on {dataDirectory}: {e}");
throw;
}
}

private void ConfigureFilePermissions()
{
try
Expand All @@ -471,6 +568,7 @@ private void ConfigureFilePermissions()

if (_ddAgentUserSID != new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null))
{
AddDatadogUserToDataFolder();
GrantAgentAccessPermissions();
}
}
Expand Down Expand Up @@ -579,14 +677,19 @@ public static ActionResult ConfigureUserRollback(Session session)

private List<string> PathsWithAgentAccess()
{
return new List<string>
{
// agent needs to be able to write logs/
// agent GUI needs to be able to edit config
// agent needs to be able to write to run/
// agent needs to be able to create auth_token
_session.Property("APPLICATIONDATADIRECTORY"),
};
var configRoot = _session.Property("APPLICATIONDATADIRECTORY");

return new List<string> {
Path.Combine(configRoot, "conf.d"),
Path.Combine(configRoot, "checks.d"),
Path.Combine(configRoot, "run"),
Path.Combine(configRoot, "logs"),
Path.Combine(configRoot, "datadog.yaml"),
Path.Combine(configRoot, "system-probe.yaml"),
Path.Combine(configRoot, "auth_token"),
Path.Combine(configRoot, "install_info"),
Path.Combine(configRoot, "python-cache"),
}; ;
Comment on lines +682 to +692
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new List<string> {
Path.Combine(configRoot, "conf.d"),
Path.Combine(configRoot, "checks.d"),
Path.Combine(configRoot, "run"),
Path.Combine(configRoot, "logs"),
Path.Combine(configRoot, "datadog.yaml"),
Path.Combine(configRoot, "system-probe.yaml"),
Path.Combine(configRoot, "auth_token"),
Path.Combine(configRoot, "install_info"),
Path.Combine(configRoot, "python-cache"),
}; ;
var filesInConfigRoot = new[]
{
"conf.d",
"checks.d",
"run",
"logs",
"datadog.yaml",
"system-probe.yaml",
"auth_token",
"install_info",
"python-cache",
}.Select(x => Path.Combine(configRoot, x)).ToList();
return filesInConfigRoot;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like this better.

}

/// <summary>
Expand Down Expand Up @@ -679,6 +782,8 @@ public ActionResult UninstallUser()
_session.Log($"Failed to remove {ddAgentUserName} from {filePath}: {e}");
}
}
//remove access to root folder
RemoveDatadogUserFromDataFolder(securityIdentifier);
}

// We intentionally do NOT delete the ddagentuser account.
Expand Down
Loading