//-------------------------------------------------------------------------------------- // Free's the handle to a boundary descriptor, a SID and a handle to a privatenamespace //-------------------------------------------------------------------------------------- HRESULT IPCShared::FreeHandles(HANDLE & hBoundaryDescriptor, PSID & pSID, HANDLE & hPrivateNamespace) { WRAPPER_NO_CONTRACT; HRESULT hr = S_OK; hr = IPCShared::FreeHandles(hBoundaryDescriptor,pSID); if(!SUCCEEDED(hr)) return hr; if(hPrivateNamespace != NULL) { static HMODULE hKernel32 = NULL; if(hKernel32 == NULL) hKernel32 = WszGetModuleHandle(L"kernel32.dll"); if(hKernel32 == NULL) { hr = HRESULT_FROM_GetLastError(); return hr; } typedef WINBASEAPI BOOL (WINAPI CLOSE_PRIVATE_NAMESPACE)(HANDLE, ULONG); static CLOSE_PRIVATE_NAMESPACE * pClosePrivateNamespace; if(pClosePrivateNamespace == NULL) pClosePrivateNamespace = (CLOSE_PRIVATE_NAMESPACE *)GetProcAddress(hKernel32, "ClosePrivateNamespace"); _ASSERTE(pClosePrivateNamespace != NULL); if (pClosePrivateNamespace == NULL) { hr = ERROR_PROC_NOT_FOUND; } else { BOOL isClosed = (*pClosePrivateNamespace)(hPrivateNamespace,0); hPrivateNamespace = NULL; if(!isClosed) { hr = HRESULT_FROM_GetLastError(); } } } return hr; }
//----------------------------------------------------------------------------- // Free's the handle to a boundary descriptor and a SID //----------------------------------------------------------------------------- HRESULT IPCShared::FreeHandles(HANDLE & hBoundaryDescriptor, PSID & pSID) { WRAPPER_NO_CONTRACT; HRESULT hr = S_OK; if(hBoundaryDescriptor != NULL) { static HMODULE hKernel32 = NULL; if(hKernel32 == NULL) hKernel32 = WszGetModuleHandle(L"kernel32.dll"); if(hKernel32 == NULL) { hr = HRESULT_FROM_GetLastError(); return hr; } typedef WINBASEAPI VOID (WINAPI DELETE_BOUNDARY_DESCRIPTOR)(HANDLE); static DELETE_BOUNDARY_DESCRIPTOR * pDeleteBoundaryDescriptor = NULL; if(pDeleteBoundaryDescriptor == NULL) pDeleteBoundaryDescriptor = (DELETE_BOUNDARY_DESCRIPTOR *)GetProcAddress(hKernel32, "DeleteBoundaryDescriptor"); _ASSERTE(pDeleteBoundaryDescriptor != NULL); if (pDeleteBoundaryDescriptor == NULL) { hr = ERROR_PROC_NOT_FOUND; } else { (*pDeleteBoundaryDescriptor)(hBoundaryDescriptor); hBoundaryDescriptor = NULL; } } if(pSID != NULL) { FreeSid(pSID); pSID = NULL; } return hr; }
//----------------------------------------------------------------------------- // Based on the pid, write a unique name for the IPCBlockTable on Vista and Higher //----------------------------------------------------------------------------- HRESULT IPCShared::GenerateBlockTableName(DWORD pid, SString & sName, HANDLE & pBoundaryDesc, HANDLE & pPrivateNamespace, PSID* pSID, BOOL bCreate) { WRAPPER_NO_CONTRACT; HRESULT hr = E_FAIL; #define SIZE 100 const WCHAR * szFormat = CorSxSPublicIPCBlock; static HMODULE hKernel32 = NULL; if(hKernel32 == NULL) hKernel32 = WszGetModuleHandle(L"kernel32.dll"); if(hKernel32 == NULL) { hr = HRESULT_FROM_GetLastError(); return hr; } //We are using static function pointers so that we dont call GetProcAddress every time //We know that the Writer will call this function only once and the reader (perfmon) is a single //threaded App. Therefore its safe to assign static local variables in this case. typedef WINBASEAPI BOOL (WINAPI ADD_SID_TO_BOUNDARY_DESCRIPTOR)(HANDLE*, PSID); static ADD_SID_TO_BOUNDARY_DESCRIPTOR * pAddSIDToBoundaryDescriptor = NULL; typedef WINBASEAPI HANDLE (WINAPI CREATE_BOUNDARY_DESCRIPTOR)(LPCWSTR,ULONG); static CREATE_BOUNDARY_DESCRIPTOR * pCreateBoundaryDescriptor = NULL; typedef WINBASEAPI HANDLE (WINAPI CREATE_PRIVATE_NAMESPACE )(LPSECURITY_ATTRIBUTES, LPVOID, LPCWSTR); static CREATE_PRIVATE_NAMESPACE * pCreatePrivateNamespace = NULL; typedef WINBASEAPI HANDLE (WINAPI OPEN_PRIVATE_NAMESPACE)(LPVOID,LPCWSTR); static OPEN_PRIVATE_NAMESPACE * pOpenPrivateNamespace = NULL; if(pAddSIDToBoundaryDescriptor == NULL) pAddSIDToBoundaryDescriptor = (ADD_SID_TO_BOUNDARY_DESCRIPTOR *)GetProcAddress(hKernel32, "AddSIDToBoundaryDescriptor"); if(pCreateBoundaryDescriptor == NULL) pCreateBoundaryDescriptor = (CREATE_BOUNDARY_DESCRIPTOR *)GetProcAddress(hKernel32, "CreateBoundaryDescriptorW"); if(pCreatePrivateNamespace == NULL) pCreatePrivateNamespace = (CREATE_PRIVATE_NAMESPACE *)GetProcAddress(hKernel32, "CreatePrivateNamespaceW"); if(pOpenPrivateNamespace==NULL) pOpenPrivateNamespace = (OPEN_PRIVATE_NAMESPACE *)GetProcAddress(hKernel32, "OpenPrivateNamespaceW"); _ASSERTE((pAddSIDToBoundaryDescriptor != NULL) && (pCreateBoundaryDescriptor != NULL) && (pCreatePrivateNamespace != NULL) && (pOpenPrivateNamespace != NULL)); if ((pAddSIDToBoundaryDescriptor == NULL) || (pCreateBoundaryDescriptor == NULL) || (pCreatePrivateNamespace == NULL) || (pOpenPrivateNamespace == NULL)) { return ERROR_PROC_NOT_FOUND; } WCHAR wsz[SIZE]; swprintf_s(wsz,SIZE, CorSxSBoundaryDescriptor, pid); ULONG flags = 0; if (RunningOnWin8()) { // on win8 we specify this flag regardless if the process is inside an appcontainer, the kernel will do the right thing. // note that for appcontainers this flag is necessary regardless of producer or consumer, ie you can't create a boundary // descriptor in an appcontainer process without adding the appcontainer SID (the API call will fail). flags |= CREATE_BOUNDARY_DESCRIPTOR_ADD_APPCONTAINER_SID; } pBoundaryDesc = (*pCreateBoundaryDescriptor)((LPCWSTR)&wsz, flags); if(!pBoundaryDesc) { hr = HRESULT_FROM_GetLastError(); return hr; } SID_IDENTIFIER_AUTHORITY SIDWorldAuth = SECURITY_WORLD_SID_AUTHORITY; if(!AllocateAndInitializeSid( &SIDWorldAuth, 1,SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, pSID)) { hr = HRESULT_FROM_GetLastError(); return hr; } if(!(*pAddSIDToBoundaryDescriptor) (&pBoundaryDesc,*pSID)) { hr = HRESULT_FROM_GetLastError(); return hr; } #ifndef FEATURE_CORECLR // when pid != GetCurrentProcessId() it means we're the consumer opening other process perf counter data if (pid != GetCurrentProcessId()) { // if the target process is inside an appcontainer we need to add the appcontainer SID to the boundary descriptor. NewArrayHolder<BYTE> pbTokenMem; hr = AppX::GetAppContainerTokenInfoForProcess(pid, pbTokenMem); if (FAILED(hr)) { // failed to open the target's process, continue on // assuming that the process isn't in an AppContainer. _ASSERTE(pbTokenMem == NULL); } else { if (hr == S_FALSE) { // not an appcontainer _ASSERTE(pbTokenMem == NULL); } else { // process is an appcontainer so add the SID PTOKEN_APPCONTAINER_INFORMATION pAppContainerTokenInfo = reinterpret_cast<PTOKEN_APPCONTAINER_INFORMATION>(pbTokenMem.GetValue()); _ASSERTE(pAppContainerTokenInfo); _ASSERTE(pAppContainerTokenInfo->TokenAppContainer); if (!(*pAddSIDToBoundaryDescriptor)(&pBoundaryDesc, pAppContainerTokenInfo->TokenAppContainer)) return HRESULT_FROM_WIN32(GetLastError()); } } } #endif // FEATURE_CORECLR if(bCreate) { SECURITY_ATTRIBUTES *pSA = NULL; IPCShared::CreateWinNTDescriptor(pid, FALSE, &pSA, PrivateNamespace, eDescriptor_Public); pPrivateNamespace = (*pCreatePrivateNamespace)(pSA, (VOID *)(pBoundaryDesc), (LPCWSTR)CorSxSWriterPrivateNamespacePrefix); if(!pPrivateNamespace) { hr = HRESULT_FROM_GetLastError(); } IPCShared::DestroySecurityAttributes(pSA); if(!pPrivateNamespace) { //if already created by a different version of the runtime we return OK. if(hr ==HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS)) { hr = S_OK; } else { return hr; } } } else { pPrivateNamespace = (*pOpenPrivateNamespace)((VOID *)(pBoundaryDesc), (LPCWSTR)CorSxSReaderPrivateNamespacePrefix); if(!pPrivateNamespace) { hr = HRESULT_FROM_GetLastError(); return hr; } } szFormat = (bCreate ? CorSxSWriterPrivateNamespacePrefix L"\\" CorSxSVistaPublicIPCBlock : CorSxSReaderPrivateNamespacePrefix L"\\" CorSxSVistaPublicIPCBlock); sName.Printf(szFormat); hr=S_OK; return hr; }
// Assumptions: // The argument type must be valid. // EventReporter::EventReporter(EventReporterType type) { CONTRACTL { THROWS; GC_NOTRIGGER; MODE_ANY; } CONTRACTL_END; m_eventType = type; HMODULE hModule = WszGetModuleHandle(NULL); PathString appPath; DWORD ret = WszGetModuleFileName(hModule, appPath); fBufferFull = FALSE; InlineSString<256> ssMessage; if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_APPLICATION)) m_Description.Append(W("Application: ")); else { m_Description.Append(ssMessage); } // If we were able to get an app name.
const USHORT BuildNumber = VER_ASSEMBLYBUILD; // Import from mscorwks.obj HINSTANCE GetModuleInst(); #if defined(_DEBUG) static void DumpSD(PSECURITY_DESCRIPTOR sd) { CONTRACTL { NOTHROW; GC_NOTRIGGER; } CONTRACTL_END; HINSTANCE hDll = WszGetModuleHandle(L"advapi32"); // Get the pointer to the requested function FARPROC pProcAddr = GetProcAddress(hDll, "ConvertSecurityDescriptorToStringSecurityDescriptorW"); // If the proc address was not found, return error if (pProcAddr == NULL) { LOG((LF_CORDB, LL_INFO10, "IPCWI::DumpSD: GetProcAddr (ConvertSecurityDescriptorToStringSecurityDescriptorW) failed.\n")); goto ErrorExit; } typedef BOOL WINAPI SDTOSTR(PSECURITY_DESCRIPTOR, DWORD, SECURITY_INFORMATION, LPSTR *, PULONG); LPSTR str = NULL;