Skip to content

Commit 5dac36a

Browse files
jborean93chrisdent-de
authored andcommitted
Set-Acl: Do not fail on untranslatable SID (PowerShell#21096)
1 parent d1d12fb commit 5dac36a

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/System.Management.Automation/namespaces/FileSystemSecurity.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,29 @@ public void SetSecurityDescriptor(
168168
{
169169
// Get the security descriptor of the destination path
170170
ObjectSecurity existingDescriptor = new FileInfo(path).GetAccessControl();
171-
Type ntAccountType = typeof(System.Security.Principal.NTAccount);
171+
// Use SecurityIdentifier to avoid having the below comparison steps
172+
// fail when dealing with an untranslatable SID in the SD
173+
Type identityType = typeof(System.Security.Principal.SecurityIdentifier);
172174

173175
AccessControlSections sections = AccessControlSections.All;
174176

175177
// If they didn't modify any audit information, don't try to set
176178
// the audit section.
177-
int auditRuleCount = sd.GetAuditRules(true, true, ntAccountType).Count;
179+
int auditRuleCount = sd.GetAuditRules(true, true, identityType).Count;
178180
if ((auditRuleCount == 0) &&
179181
(sd.AreAuditRulesProtected == existingDescriptor.AreAccessRulesProtected))
180182
{
181183
sections &= ~AccessControlSections.Audit;
182184
}
183185

184186
// If they didn't modify the owner, don't try to set that section.
185-
if (sd.GetOwner(ntAccountType) == existingDescriptor.GetOwner(ntAccountType))
187+
if (sd.GetOwner(identityType) == existingDescriptor.GetOwner(identityType))
186188
{
187189
sections &= ~AccessControlSections.Owner;
188190
}
189191

190192
// If they didn't modify the group, don't try to set that section.
191-
if (sd.GetGroup(ntAccountType) == existingDescriptor.GetGroup(ntAccountType))
193+
if (sd.GetGroup(identityType) == existingDescriptor.GetGroup(identityType))
192194
{
193195
sections &= ~AccessControlSections.Group;
194196
}

test/powershell/Modules/Microsoft.PowerShell.Security/AclCmdlets.Tests.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ Describe "Acl cmdlets are available and operate properly" -Tag CI {
7777
$newrule | Should -Not -BeNullOrEmpty
7878
}
7979

80+
It "Can edit SD that contains an orphaned SID" {
81+
$badSid = [System.Security.Principal.SecurityIdentifier]::new("S-1-5-1234-5678")
82+
$currentUserSid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User
83+
84+
$testFilePath = "TestDrive:\pwsh-acl-test.txt"
85+
$testFile = New-Item -Path $testFilePath -ItemType File -Value 'foo' -Force
86+
87+
# We should be able to set an SD entry to an untranslatable SID
88+
$fileSecurity = $testFilePath | Get-Acl
89+
$fileSecurity.SetGroup($badSid)
90+
Set-Acl -Path $testFile -AclObject $fileSecurity
91+
92+
# We should be able to get the SD with an untranslatable SID
93+
$setSD = Get-Acl -Path $testFile
94+
$setSD.GetGroup([System.Security.Principal.SecurityIdentifier]) | Should -Be $badSid
95+
96+
# We should be able to set it back to a known SID
97+
$setSD.SetGroup($currentUserSid)
98+
Set-Acl -Path $testFile -AclObject $setSD
99+
100+
$actual = Get-Acl -Path $testFile
101+
$actualGroup = $actual.GetGroup([System.Security.Principal.SecurityIdentifier])
102+
$actualGroup | Should -Be $currentUserSid
103+
}
104+
80105
AfterAll {
81106
$PSDefaultParameterValues.Remove("It:Skip")
82107
}

0 commit comments

Comments
 (0)