[KERNEL32] Reformat K32CreateDBMonMutex().

This commit is contained in:
Hermès Bélusca-Maïto 2022-10-30 19:36:20 +01:00
parent 75da7ad212
commit 9f938ea3bb
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0

View file

@ -33,174 +33,139 @@ typedef struct _DBGSS_THREAD_DATA
/* PRIVATE FUNCTIONS *********************************************************/ /* PRIVATE FUNCTIONS *********************************************************/
static static HANDLE
HANDLE K32CreateDBMonMutex(VOID)
K32CreateDBMonMutex(void)
{ {
static SID_IDENTIFIER_AUTHORITY siaNTAuth = {SECURITY_NT_AUTHORITY}; static SID_IDENTIFIER_AUTHORITY siaNTAuth = {SECURITY_NT_AUTHORITY};
static SID_IDENTIFIER_AUTHORITY siaWorldAuth = {SECURITY_WORLD_SID_AUTHORITY}; static SID_IDENTIFIER_AUTHORITY siaWorldAuth = {SECURITY_WORLD_SID_AUTHORITY};
HANDLE hMutex; HANDLE hMutex;
NTSTATUS Status;
/* SIDs to be used in the DACL */ /* SIDs to be used in the DACL */
PSID psidSystem = NULL; PSID psidSystem = NULL;
PSID psidAdministrators = NULL; PSID psidAdministrators = NULL;
PSID psidEveryone = NULL; PSID psidEveryone = NULL;
/* buffer for the DACL */ /* Buffer for the DACL */
PVOID pDaclBuf = NULL; PVOID pDaclBuf = NULL;
/* minimum size of the DACL: an ACL descriptor and three ACCESS_ALLOWED_ACE /* Minimum size of the DACL: an ACL descriptor and three ACCESS_ALLOWED_ACE
headers. We'll add the size of SIDs when we'll know it * headers. We will add the size of SIDs when they are known. */
*/
SIZE_T nDaclBufSize = SIZE_T nDaclBufSize =
sizeof(ACL) + (sizeof(ACCESS_ALLOWED_ACE) - sizeof(ACL) + 3 * FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart);
sizeof(((ACCESS_ALLOWED_ACE*)0)->SidStart)) * 3;
/* security descriptor of the mutex */ /* Security descriptor and attributes of the mutex */
SECURITY_DESCRIPTOR sdMutexSecurity; SECURITY_DESCRIPTOR sdMutexSecurity;
/* attributes of the mutex object we'll create */
SECURITY_ATTRIBUTES saMutexAttribs = {sizeof(saMutexAttribs), SECURITY_ATTRIBUTES saMutexAttribs = {sizeof(saMutexAttribs),
&sdMutexSecurity, &sdMutexSecurity,
TRUE}; TRUE};
NTSTATUS nErrCode; /* Try to open the mutex */
hMutex = OpenMutexW(SYNCHRONIZE | READ_CONTROL | MUTANT_QUERY_STATE,
/* first, try to open the mutex */ TRUE,
hMutex = OpenMutexW (SYNCHRONIZE | READ_CONTROL | MUTANT_QUERY_STATE, L"DBWinMutex");
TRUE,
L"DBWinMutex");
if (hMutex != NULL) if (hMutex != NULL)
{ {
/* success */ /* Success */
return hMutex; return hMutex;
} }
/* error other than the mutex not being found */ /* Error other than the mutex not being found */
else if (GetLastError() != ERROR_FILE_NOT_FOUND) else if (GetLastError() != ERROR_FILE_NOT_FOUND)
{ {
/* failure */ /* Failure */
return NULL; return NULL;
} }
/* if the mutex doesn't exist, create it */ /* If the mutex does not exist, set up its security, then create it */
/* first, set up the mutex security */ /* Allocate the NT AUTHORITY\SYSTEM SID */
/* allocate the NT AUTHORITY\SYSTEM SID */ Status = RtlAllocateAndInitializeSid(&siaNTAuth,
nErrCode = RtlAllocateAndInitializeSid(&siaNTAuth, 1,
1, SECURITY_LOCAL_SYSTEM_RID,
SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0,
0, &psidSystem);
0, if (!NT_SUCCESS(Status))
0, goto Cleanup;
0,
0,
0,
0,
&psidSystem);
/* failure */ /* Allocate the BUILTIN\Administrators SID */
if (!NT_SUCCESS(nErrCode)) goto l_Cleanup; Status = RtlAllocateAndInitializeSid(&siaNTAuth,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&psidAdministrators);
if (!NT_SUCCESS(Status))
goto Cleanup;
/* allocate the BUILTIN\Administrators SID */ /* Allocate the Everyone SID */
nErrCode = RtlAllocateAndInitializeSid(&siaNTAuth, Status = RtlAllocateAndInitializeSid(&siaWorldAuth,
2, 1,
SECURITY_BUILTIN_DOMAIN_RID, SECURITY_WORLD_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, 0,
0, &psidEveryone);
0, if (!NT_SUCCESS(Status))
0, goto Cleanup;
0,
0,
0,
&psidAdministrators);
/* failure */ /* Allocate space for the SIDs too */
if (!NT_SUCCESS(nErrCode)) goto l_Cleanup;
/* allocate the Everyone SID */
nErrCode = RtlAllocateAndInitializeSid(&siaWorldAuth,
1,
0,
0,
0,
0,
0,
0,
0,
0,
&psidEveryone);
/* failure */
if (!NT_SUCCESS(nErrCode)) goto l_Cleanup;
/* allocate space for the SIDs too */
nDaclBufSize += RtlLengthSid(psidSystem); nDaclBufSize += RtlLengthSid(psidSystem);
nDaclBufSize += RtlLengthSid(psidAdministrators); nDaclBufSize += RtlLengthSid(psidAdministrators);
nDaclBufSize += RtlLengthSid(psidEveryone); nDaclBufSize += RtlLengthSid(psidEveryone);
/* allocate the buffer for the DACL */ /* Allocate the buffer for the DACL */
pDaclBuf = GlobalAlloc(GMEM_FIXED, nDaclBufSize); pDaclBuf = GlobalAlloc(GMEM_FIXED, nDaclBufSize);
if (pDaclBuf == NULL)
goto Cleanup;
/* failure */ /* Create the DACL */
if (pDaclBuf == NULL) goto l_Cleanup; Status = RtlCreateAcl(pDaclBuf, nDaclBufSize, ACL_REVISION);
if (!NT_SUCCESS(Status))
goto Cleanup;
/* create the DACL */ /* Grant the minimum required access to Everyone */
nErrCode = RtlCreateAcl(pDaclBuf, nDaclBufSize, ACL_REVISION); Status = RtlAddAccessAllowedAce(pDaclBuf,
ACL_REVISION,
SYNCHRONIZE |
READ_CONTROL |
MUTANT_QUERY_STATE,
psidEveryone);
if (!NT_SUCCESS(Status))
goto Cleanup;
/* failure */ /* Grant full access to BUILTIN\Administrators */
if (!NT_SUCCESS(nErrCode)) goto l_Cleanup; Status = RtlAddAccessAllowedAce(pDaclBuf,
ACL_REVISION,
MUTANT_ALL_ACCESS,
psidAdministrators);
if (!NT_SUCCESS(Status))
goto Cleanup;
/* grant the minimum required access to Everyone */ /* Grant full access to NT AUTHORITY\SYSTEM */
nErrCode = RtlAddAccessAllowedAce(pDaclBuf, Status = RtlAddAccessAllowedAce(pDaclBuf,
ACL_REVISION, ACL_REVISION,
SYNCHRONIZE | MUTANT_ALL_ACCESS,
READ_CONTROL | psidSystem);
MUTANT_QUERY_STATE, if (!NT_SUCCESS(Status))
psidEveryone); goto Cleanup;
/* failure */ /* Create the security descriptor */
if (!NT_SUCCESS(nErrCode)) goto l_Cleanup; Status = RtlCreateSecurityDescriptor(&sdMutexSecurity,
SECURITY_DESCRIPTOR_REVISION);
if (!NT_SUCCESS(Status))
goto Cleanup;
/* grant full access to BUILTIN\Administrators */ /* Set the descriptor's DACL to the created ACL */
nErrCode = RtlAddAccessAllowedAce(pDaclBuf, Status = RtlSetDaclSecurityDescriptor(&sdMutexSecurity,
ACL_REVISION, TRUE,
MUTANT_ALL_ACCESS, pDaclBuf,
psidAdministrators); FALSE);
if (!NT_SUCCESS(Status))
goto Cleanup;
/* failure */ /* Create the mutex */
if (!NT_SUCCESS(nErrCode)) goto l_Cleanup;
/* grant full access to NT AUTHORITY\SYSTEM */
nErrCode = RtlAddAccessAllowedAce(pDaclBuf,
ACL_REVISION,
MUTANT_ALL_ACCESS,
psidSystem);
/* failure */
if (!NT_SUCCESS(nErrCode)) goto l_Cleanup;
/* create the security descriptor */
nErrCode = RtlCreateSecurityDescriptor(&sdMutexSecurity,
SECURITY_DESCRIPTOR_REVISION);
/* failure */
if (!NT_SUCCESS(nErrCode)) goto l_Cleanup;
/* set the descriptor's DACL to the ACL we created */
nErrCode = RtlSetDaclSecurityDescriptor(&sdMutexSecurity,
TRUE,
pDaclBuf,
FALSE);
/* failure */
if (!NT_SUCCESS(nErrCode)) goto l_Cleanup;
/* create the mutex */
hMutex = CreateMutexW(&saMutexAttribs, FALSE, L"DBWinMutex"); hMutex = CreateMutexW(&saMutexAttribs, FALSE, L"DBWinMutex");
l_Cleanup: Cleanup:
/* free the buffers */ /* Free the buffers */
if (pDaclBuf) GlobalFree(pDaclBuf); if (pDaclBuf) GlobalFree(pDaclBuf);
if (psidEveryone) RtlFreeSid(psidEveryone); if (psidEveryone) RtlFreeSid(psidEveryone);
if (psidAdministrators) RtlFreeSid(psidAdministrators); if (psidAdministrators) RtlFreeSid(psidAdministrators);