Skip to content

Commit 9171097

Browse files
authored
Handle access denied error when setting owner if already owner (#5282)
If we get `ERROR_ACCESS_DENIED` when attempting to set the owner, check if the owner is already correct. If it is, attempt to set just the DACL.
1 parent bdb5faa commit 9171097

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/AppInstallerSharedLib/Filesystem.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,23 @@ namespace AppInstaller::Filesystem
417417
securityInformation |= OWNER_SECURITY_INFORMATION;
418418
}
419419

420-
THROW_IF_WIN32_ERROR(SetNamedSecurityInfoW(&path[0], SE_FILE_OBJECT, securityInformation, ownerSID, nullptr, acl.get(), nullptr));
420+
DWORD result = SetNamedSecurityInfoW(&path[0], SE_FILE_OBJECT, securityInformation, ownerSID, nullptr, acl.get(), nullptr);
421+
422+
// We can be denied access attempting to set the owner when the owner is already correct.
423+
// Determine if the owner is correct; if so, try again without attempting to set the owner.
424+
if (result == ERROR_ACCESS_DENIED && ownerSID)
425+
{
426+
wil::unique_hlocal_security_descriptor securityDescriptor;
427+
PSID currentOwnerSID = nullptr;
428+
DWORD getResult = GetNamedSecurityInfoW(&path[0], SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &currentOwnerSID, nullptr, nullptr, nullptr, &securityDescriptor);
429+
430+
if (SUCCEEDED_WIN32_LOG(getResult) && currentOwnerSID && EqualSid(currentOwnerSID, ownerSID))
431+
{
432+
result = SetNamedSecurityInfoW(&path[0], SE_FILE_OBJECT, securityInformation & ~OWNER_SECURITY_INFORMATION, nullptr, nullptr, acl.get(), nullptr);
433+
}
434+
}
435+
436+
THROW_IF_WIN32_ERROR(result);
421437
}
422438

423439
std::filesystem::path InitializeAndGetPathTo(PathDetails&& details)

0 commit comments

Comments
 (0)