BOOL ProcessGetIntegrityLevel()

in PPLGuard/utils.cpp [259:299]


BOOL ProcessGetIntegrityLevel(DWORD dwProcessId, PDWORD pdwIntegrityLevel)
{
	BOOL bReturnValue = FALSE;

	HANDLE hProcess = NULL;
	HANDLE hProcessToken = NULL;
	PTOKEN_MANDATORY_LABEL pLabel = NULL;
	DWORD dwLength = 0;
	DWORD dwIntegrityLevel = 0;

	if (!(hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId)))
		goto end;

	if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken))
		goto end;

	GetTokenInformation(hProcessToken, TokenIntegrityLevel, pLabel, dwLength, &dwLength);
	if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
		goto end;

	pLabel = (PTOKEN_MANDATORY_LABEL)LocalAlloc(LPTR, dwLength);
	if (!pLabel)
		goto end;

	if (!GetTokenInformation(hProcessToken, TokenIntegrityLevel, pLabel, dwLength, &dwLength))
		goto end;

	dwIntegrityLevel = *GetSidSubAuthority(pLabel->Label.Sid, *GetSidSubAuthorityCount(pLabel->Label.Sid) - 1);
	*pdwIntegrityLevel = dwIntegrityLevel;
	bReturnValue = TRUE;

end:
	if (pLabel)
		LocalFree(pLabel);
	if (hProcessToken)
		CloseHandle(hProcessToken);
	if (hProcess)
		CloseHandle(hProcess);

	return bReturnValue;
}