From f610d4183f3ef5b906f15f350c8e6b38f640fc63 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 18 May 2016 21:35:51 +0200 Subject: [PATCH] Event tracing support added --- EAPMethods/build/Common.props | 5 +- EAPMethods/build/EAPTTLS.props | 3 + EAPMethods/build/EAPTTLS.rc | Bin 4868 -> 4932 bytes EAPMethods/build/EAPTTLS.vcxproj | 17 +++ EAPMethods/build/EAPTTLS.vcxproj.filters | 5 + EAPMethods/include/StdAfx.h | 6 + EAPMethods/res/EAPMethod.man | Bin 0 -> 7136 bytes EAPMethods/src/Main.cpp | 150 ++++++++++++++++++++++- lib/WinStd | 2 +- 9 files changed, 184 insertions(+), 4 deletions(-) create mode 100644 EAPMethods/res/EAPMethod.man diff --git a/EAPMethods/build/Common.props b/EAPMethods/build/Common.props index c2a4d0a..992a7e5 100644 --- a/EAPMethods/build/Common.props +++ b/EAPMethods/build/Common.props @@ -8,8 +8,11 @@ - ..\include;..\..\lib\WinStd\include;%(AdditionalIncludeDirectories) + $(IntDir);..\include;..\..\lib\WinStd\include;%(AdditionalIncludeDirectories) + + $(IntDir);%(AdditionalIncludeDirectories) + \ No newline at end of file diff --git a/EAPMethods/build/EAPTTLS.props b/EAPMethods/build/EAPTTLS.props index 809659b..e2974e7 100644 --- a/EAPMethods/build/EAPTTLS.props +++ b/EAPMethods/build/EAPTTLS.props @@ -7,6 +7,9 @@ + + EAPMETHOD_TYPE=21;%(PreprocessorDefinitions) + \ No newline at end of file diff --git a/EAPMethods/build/EAPTTLS.rc b/EAPMethods/build/EAPTTLS.rc index 0e00fce35a6db70b8d3d9d075391eccab875bc14..45ddcc53c0fb8c659aa332fe6e73450b26f83c9e 100644 GIT binary patch delta 60 zcmZosJEFE>5!+-hHW77K21kYf2499$h7yJhhJ1z;23LjWtLi08^upi+D06f+P-v9sr diff --git a/EAPMethods/build/EAPTTLS.vcxproj b/EAPMethods/build/EAPTTLS.vcxproj index 1320714..496f275 100644 --- a/EAPMethods/build/EAPTTLS.vcxproj +++ b/EAPMethods/build/EAPTTLS.vcxproj @@ -98,6 +98,23 @@ {47399d91-7eb9-41de-b521-514ba5db0c43} + + + Document + mc.exe "%(Identity)" -u -b -z "EAPMethodETW" -e "h" -h "$(IntDir)." -r "$(IntDir)." + mc.exe "%(Identity)" -u -b -z "EAPMethodETW" -e "h" -h "$(IntDir)." -r "$(IntDir)." + mc.exe "%(Identity)" -u -b -z "EAPMethodETW" -e "h" -h "$(IntDir)." -r "$(IntDir)." + mc.exe "%(Identity)" -u -b -z "EAPMethodETW" -e "h" -h "$(IntDir)." -r "$(IntDir)." + Compiling manifest... + Compiling manifest... + Compiling manifest... + Compiling manifest... + $(IntDir)EAPMethodETW.h;$(IntDir)EAPMethodETW.rc;$(IntDir)EAPMethodETW_MSG00001.bin;$(IntDir)EAPMethodETWTEMP.BIN;%(Outputs) + $(IntDir)EAPMethodETW.h;$(IntDir)EAPMethodETW.rc;$(IntDir)EAPMethodETW_MSG00001.bin;$(IntDir)EAPMethodETWTEMP.BIN;%(Outputs) + $(IntDir)EAPMethodETW.h;$(IntDir)EAPMethodETW.rc;$(IntDir)EAPMethodETW_MSG00001.bin;$(IntDir)EAPMethodETWTEMP.BIN;%(Outputs) + $(IntDir)EAPMethodETW.h;$(IntDir)EAPMethodETW.rc;$(IntDir)EAPMethodETW_MSG00001.bin;$(IntDir)EAPMethodETWTEMP.BIN;%(Outputs) + + diff --git a/EAPMethods/build/EAPTTLS.vcxproj.filters b/EAPMethods/build/EAPTTLS.vcxproj.filters index 1c80dcd..ac58632 100644 --- a/EAPMethods/build/EAPTTLS.vcxproj.filters +++ b/EAPMethods/build/EAPTTLS.vcxproj.filters @@ -32,4 +32,9 @@ Resource Files + + + Resource Files + + \ No newline at end of file diff --git a/EAPMethods/include/StdAfx.h b/EAPMethods/include/StdAfx.h index 512c1d4..773bd73 100644 --- a/EAPMethods/include/StdAfx.h +++ b/EAPMethods/include/StdAfx.h @@ -20,6 +20,12 @@ #pragma once +#include #include +#include + +#include #include + +#include diff --git a/EAPMethods/res/EAPMethod.man b/EAPMethods/res/EAPMethod.man new file mode 100644 index 0000000000000000000000000000000000000000..5e9f56ed11bbbcd569bdd58432a9816bcdb78907 GIT binary patch literal 7136 zcmeHM-)|E~5T4f{@jo1>NbLiTourgT24sj02pg)zPI-%h&m<`R5#^j1iu$h?zHi5y z+ugf!Y{w;eKqtrRyV==q=bM?Gn|%APD);0eu1Ko#Og_s%`WRnGPtGKfPco9BJdyiS zk&=}09pTAPy0{Tewq}$N1YbJ0yz4>`C{{^|q&Tvg>j^7v-{RE2bC-Cs=Pbmd}vLBAH<31iPHb zCjO4F_ao@hJytM!FD+@xo@1x`Qzpp9+Le4LYimZ*wN!eaI!=wvFJR#q_B;b?Ls*FH z67fr^OHJOwFPesf6Lu|W5_8YuC<0;oEvSN{}p>{z?*)@oCa8{VEzke zcx?P^3*$dz2lxKORhH)%J;I$$j5fgY7G~9q%yaoSSS67!K=}dd_YG2ust&BL3HxIFq+FIRG5-d6<=thfDknho1s+K6i=oZ+4fPyf z!uO5_Y{V>LGr$Y%Q*97|8^-mC;iimCPyvDncNSyqjrm|IZj@aEZnnJ8qp!e-+E$_242G#*0v=3WyY^5~Ll%G&0 zGHk@g?k(^dgClxx6XQb@tD)VP1!A+pVG#Z5EwLJe}< z-5wCxsGMaDH+pZv20MtZckt95aJIZPioN2x_OS7p=N-jfy9>`6BU{k-7xP-X-tG6^ zSJV8`L0q?QDSO$7UIk}onKYP&_EYf^49I{?- zqmJE09A@*G&*P@a6hC=nHZ0-o%zq2i`ipPRL!440)WfmdJf{aZ7t?At^!)kwW!?>E zuI_vc?P%YoXW_oth;^C14Gf1>amdwtEHg`oGyi{b>D(i)lF z?T0uI*uhC>6OsGY-o_dqFtg{$gi?G%){abZhmN&p{^fgZwj&$i4Ha(ikMU7qE#h!`~*J=)h>me0^mE-Ken%b|+o!L&c= rcLA7v+<3@~YRYhz?tcbsmeV{%KPk>VJW;4)b~bB)S@*wFnt8qh{a66W literal 0 HcmV?d00001 diff --git a/EAPMethods/src/Main.cpp b/EAPMethods/src/Main.cpp index 78512d4..2c3943b 100644 --- a/EAPMethods/src/Main.cpp +++ b/EAPMethods/src/Main.cpp @@ -20,15 +20,161 @@ #include +using namespace std; +using namespace winstd; + + +#define ETW_FN_VOID event_fn_auto < &EAPMETHOD_TRACE_EVT_FN_CALL, &EAPMETHOD_TRACE_EVT_FN_RETURN > _event_auto(*g_ep, __FUNCTION__) +#define ETW_FN_DWORD(res) event_fn_auto_ret _event_auto(*g_ep, __FUNCTION__, res) +#define ETW_FN_HRESULT(res) event_fn_auto_ret _event_auto(*g_ep, __FUNCTION__, res) + + +event_provider *g_ep = NULL; + + +// event_fn_auto actually and winstd::event_auto_res<> do not need an assignment operator actually, so the C4512 warning is safely ignored. +#pragma warning(push) +#pragma warning(disable: 4512) + +/// +/// Helper class to write an event on entry/exit of scope. +/// +/// It writes one string event at creation and another at destruction. +/// +template +class event_fn_auto +{ +public: + inline event_fn_auto(_In_ event_provider &ep, _In_z_ LPCSTR pszFnName) : m_ep(ep) + { + EventDataDescCreate(&m_fn_name, pszFnName, (ULONG)(strlen(pszFnName) + 1)*sizeof(*pszFnName)); + m_ep.write(event_cons, 1, &m_fn_name); + } + + inline ~event_fn_auto() + { + m_ep.write(event_dest, 1, &m_fn_name); + } + +protected: + event_provider &m_ep; ///< Reference to event provider in use + EVENT_DATA_DESCRIPTOR m_fn_name; ///< Function name +}; + + +/// +/// Helper template to write an event on entry/exit of scope with one parameter (typically result). +/// +/// It writes one string event at creation and another at destruction, with allowing one sprintf type parameter for string event at destruction. +/// +template +class event_fn_auto_ret +{ +public: + inline event_fn_auto_ret(_In_ event_provider &ep, _In_z_ LPCSTR pszFnName, T &result) : m_ep(ep), m_result(result) + { + EventDataDescCreate(m_desc + 0, pszFnName, (ULONG)(strlen(pszFnName) + 1)*sizeof(*pszFnName)); + m_ep.write(event_cons, 1, m_desc); + } + + inline ~event_fn_auto_ret() + { + EventDataDescCreate(m_desc + 1, &m_result, sizeof(T)); + m_ep.write(event_dest, 2, m_desc); + } + +protected: + event_provider &m_ep; ///< Reference to event provider in use + T &m_result; ///< Function result + EVENT_DATA_DESCRIPTOR m_desc[2]; ///< Function name and return value +}; + +#pragma warning(pop) + + BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved) { + UNREFERENCED_PARAMETER(hinstDLL); UNREFERENCED_PARAMETER(lpvReserved); if (fdwReason == DLL_PROCESS_ATTACH) { - winstd::tstring filename; - GetModuleFileName(hinstDLL, filename); + g_ep = new event_provider(); + assert(g_ep); + g_ep->create(&EAPMETHOD_TRACE_EVENT_PROVIDER); + g_ep->write(&EAPMETHOD_TRACE_EVT_MODULE_LOAD, EAPMETHOD_TYPE); + } else if (fdwReason == DLL_PROCESS_DETACH) { + assert(g_ep); + g_ep->write(&EAPMETHOD_TRACE_EVT_MODULE_UNLOAD, EAPMETHOD_TYPE); + delete g_ep; + + assert(!_CrtDumpMemoryLeaks()); } return TRUE; } + + +//#pragma comment(linker, "/EXPORT:DllRegisterServer,PRIVATE") +//#pragma comment(linker, "/EXPORT:DllUnregisterServer,PRIVATE") + + +extern "C" +{ + ///// + ///// Registers the EAP method. + ///// + //HRESULT STDAPICALLTYPE DllRegisterServer() + //{ + // HRESULT hr = S_OK; + // ETW_FN_HRESULT(hr); + + // return hr; + //} + + + ///// + ///// Unregisters the EAP method. + ///// + //HRESULT STDAPICALLTYPE DllUnregisterServer() + //{ + // HRESULT hr = S_OK; + // ETW_FN_HRESULT(hr); + + // return hr; + //} + + + /// + /// Releases all memory associated with an opaque user interface context data buffer. + /// + /// \sa [EapPeerFreeMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363606.aspx) + /// + __declspec(dllexport) VOID WINAPI EapPeerFreeMemory(_In_ void *pUIContextData) + { + ETW_FN_VOID; + + if (pUIContextData) { + // Since we do security here and some of the BLOBs contain credentials, sanitize every memory block before freeing. + HANDLE hHeap = GetProcessHeap(); + SecureZeroMemory(pUIContextData, HeapSize(hHeap, 0, pUIContextData)); + HeapFree(hHeap, 0, pUIContextData); + } + } + + + /// + /// Releases error-specific memory allocated by the EAP peer method. + /// + /// \sa [EapPeerFreeErrorMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363605.aspx) + /// + __declspec(dllexport) VOID WINAPI EapPeerFreeErrorMemory(_In_ EAP_ERROR *ppEapError) + { + ETW_FN_VOID; + + if (ppEapError) { + // pRootCauseString and pRepairString always trail the ppEapError to reduce number of (de)allocations. + HeapFree(GetProcessHeap(), 0, ppEapError); + } + } +}; diff --git a/lib/WinStd b/lib/WinStd index f6029b2..9be7e7e 160000 --- a/lib/WinStd +++ b/lib/WinStd @@ -1 +1 @@ -Subproject commit f6029b2f04f52ae2b692980cb4d5734031568d91 +Subproject commit 9be7e7eafb62f815155a9635764ce7770a5daa25