Skip to content

Commit fd25e2d

Browse files
committed
[POWRPROF] Create a security descriptor for power management semaphore
1 parent 56a2c0f commit fd25e2d

File tree

1 file changed

+180
-1
lines changed

1 file changed

+180
-1
lines changed

dll/win32/powrprof/powrprof.c

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,172 @@ CheckPowerActionPolicy(PPOWER_ACTION_POLICY pPAP, SYSTEM_POWER_CAPABILITIES Powe
826826
};
827827
}
828828

829+
/**
830+
* @brief
831+
* Creates a security descriptor for the power
832+
* management registry semaphore.
833+
*
834+
* @param[out] PowrProfSd
835+
* A pointer to an allocated security descriptor
836+
* for the semaphore.
837+
*
838+
* @return
839+
* Returns TRUE if the function succeeds, otherwise
840+
* FALSE is returned.
841+
*
842+
* @remarks
843+
* Authenticated users are only given a subset of specific
844+
* rights for the semaphore access, local system and admins
845+
* have full power.
846+
*/
847+
static BOOLEAN
848+
CreatePowrProfSemaphoreSecurity(_Out_ PSECURITY_DESCRIPTOR *PowrProfSd)
849+
{
850+
BOOLEAN Success = FALSE;
851+
PACL Dacl;
852+
ULONG DaclSize, RelSDSize = 0;
853+
PSID AuthenticatedUsersSid = NULL, SystemSid = NULL, AdminsSid = NULL;
854+
SECURITY_DESCRIPTOR AbsSd;
855+
PSECURITY_DESCRIPTOR RelSd = NULL;
856+
static SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
857+
858+
if (!AllocateAndInitializeSid(&NtAuthority,
859+
1,
860+
SECURITY_AUTHENTICATED_USER_RID,
861+
0, 0, 0, 0, 0, 0, 0,
862+
&AuthenticatedUsersSid))
863+
{
864+
return FALSE;
865+
}
866+
867+
if (!AllocateAndInitializeSid(&NtAuthority,
868+
1,
869+
SECURITY_LOCAL_SYSTEM_RID,
870+
0, 0, 0, 0, 0, 0, 0,
871+
&SystemSid))
872+
{
873+
goto Quit;
874+
}
875+
876+
if (!AllocateAndInitializeSid(&NtAuthority,
877+
2,
878+
SECURITY_BUILTIN_DOMAIN_RID,
879+
DOMAIN_ALIAS_RID_ADMINS,
880+
0, 0, 0, 0, 0, 0,
881+
&AdminsSid))
882+
{
883+
goto Quit;
884+
}
885+
886+
if (!InitializeSecurityDescriptor(&AbsSd, SECURITY_DESCRIPTOR_REVISION))
887+
{
888+
goto Quit;
889+
}
890+
891+
DaclSize = sizeof(ACL) +
892+
sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(AuthenticatedUsersSid) +
893+
sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(SystemSid) +
894+
sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(AdminsSid);
895+
896+
Dacl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, DaclSize);
897+
if (!Dacl)
898+
{
899+
goto Quit;
900+
}
901+
902+
if (!InitializeAcl(Dacl, DaclSize, ACL_REVISION))
903+
{
904+
goto Quit;
905+
}
906+
907+
if (!AddAccessAllowedAce(Dacl,
908+
ACL_REVISION,
909+
SYNCHRONIZE | STANDARD_RIGHTS_READ | 0x3,
910+
AuthenticatedUsersSid))
911+
{
912+
goto Quit;
913+
}
914+
915+
if (!AddAccessAllowedAce(Dacl,
916+
ACL_REVISION,
917+
SEMAPHORE_ALL_ACCESS,
918+
SystemSid))
919+
{
920+
goto Quit;
921+
}
922+
923+
if (!AddAccessAllowedAce(Dacl,
924+
ACL_REVISION,
925+
SEMAPHORE_ALL_ACCESS,
926+
AdminsSid))
927+
{
928+
goto Quit;
929+
}
930+
931+
if (!SetSecurityDescriptorDacl(&AbsSd, TRUE, Dacl, FALSE))
932+
{
933+
goto Quit;
934+
}
935+
936+
if (!SetSecurityDescriptorOwner(&AbsSd, AdminsSid, FALSE))
937+
{
938+
goto Quit;
939+
}
940+
941+
if (!SetSecurityDescriptorGroup(&AbsSd, SystemSid, FALSE))
942+
{
943+
goto Quit;
944+
}
945+
946+
if (!MakeSelfRelativeSD(&AbsSd, NULL, &RelSDSize) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
947+
{
948+
RelSd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, RelSDSize);
949+
if (RelSd == NULL)
950+
{
951+
goto Quit;
952+
}
953+
954+
if (!MakeSelfRelativeSD(&AbsSd, RelSd, &RelSDSize))
955+
{
956+
goto Quit;
957+
}
958+
}
959+
960+
*PowrProfSd = RelSd;
961+
Success = TRUE;
962+
963+
Quit:
964+
if (AuthenticatedUsersSid)
965+
{
966+
FreeSid(AuthenticatedUsersSid);
967+
}
968+
969+
if (SystemSid)
970+
{
971+
FreeSid(SystemSid);
972+
}
973+
974+
if (AdminsSid)
975+
{
976+
FreeSid(AdminsSid);
977+
}
978+
979+
if (Dacl)
980+
{
981+
HeapFree(GetProcessHeap(), 0, Dacl);
982+
}
983+
984+
if (!Success)
985+
{
986+
if (RelSd)
987+
{
988+
HeapFree(GetProcessHeap(), 0, RelSd);
989+
}
990+
}
991+
992+
return Success;
993+
}
994+
829995
static VOID
830996
FixSystemPowerState(PSYSTEM_POWER_STATE Psps, SYSTEM_POWER_CAPABILITIES PowerCaps)
831997
{
@@ -1098,6 +1264,8 @@ DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
10981264
{
10991265
HKEY hKey;
11001266
LONG Err;
1267+
SECURITY_ATTRIBUTES SecAttrs;
1268+
PSECURITY_DESCRIPTOR Sd;
11011269

11021270
DisableThreadLibraryCalls(hinstDLL);
11031271

@@ -1124,7 +1292,18 @@ DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
11241292
RegCloseKey(hKey);
11251293
}
11261294

1127-
PPRegSemaphore = CreateSemaphoreW(NULL, 1, 1, szSemaphoreName);
1295+
if (!CreatePowrProfSemaphoreSecurity(&Sd))
1296+
{
1297+
ERR("Couldn't create POWRPROF semaphore security descriptor!\n");
1298+
return FALSE;
1299+
}
1300+
1301+
SecAttrs.nLength = sizeof(SECURITY_ATTRIBUTES);
1302+
SecAttrs.lpSecurityDescriptor = Sd;
1303+
SecAttrs.bInheritHandle = FALSE;
1304+
1305+
PPRegSemaphore = CreateSemaphoreW(&SecAttrs, 1, 1, szSemaphoreName);
1306+
HeapFree(GetProcessHeap(), 0, Sd);
11281307
if (PPRegSemaphore == NULL)
11291308
{
11301309
ERR("Couldn't create Semaphore: %d\n", GetLastError());

0 commit comments

Comments
 (0)