1
- // ===-- dpctl_dynamic_lib_helper.cpp - Dynamic library helper -*-C++-*- ===//
1
+ // ===-- dpctl_dynamic_lib_helper.cpp - Dynamic library helper -*-C++-*- ===//
2
2
//
3
3
// Data Parallel Control (dpctl)
4
4
//
26
26
27
27
#if defined(_WIN32) || defined(_WIN64)
28
28
29
- #include " Support/DllExport.h"
30
- #include < windows.h>
29
+ #include " Support/DllExport.h"
30
+ #include < windows.h>
31
31
32
- #if !defined(DPCTL_CHECK_DLL_SIG)
33
- #define DPCTL_LOAD_DLL (name ) LoadLibrary(name)
34
- #else
35
- #define DPCTL_LOAD_DLL (name ) _DPCTLLoadLibrary(name)
32
+ #if !defined(DPCTL_CHECK_DLL_SIG)
33
+ #define DPCTL_LOAD_DLL (name ) LoadLibrary(name)
34
+ #else
35
+ #define DPCTL_LOAD_DLL (name ) _DPCTLLoadLibrary(name)
36
36
37
- #include < stdio .h>
38
- #include < Softpub .h>
39
- #include < wincrypt.h>
40
- #include < wintrust.h>
37
+ #include < Softpub .h>
38
+ #include < stdio .h>
39
+ #include < wincrypt.h>
40
+ #include < wintrust.h>
41
41
42
- #pragma comment(lib, "Wintrust.lib")
42
+ #pragma comment(lib, "Wintrust.lib")
43
43
44
44
static HMODULE WINAPI _DPCTLLoadLibrary (LPCTSTR filename)
45
45
{
@@ -49,40 +49,46 @@ static HMODULE WINAPI _DPCTLLoadLibrary(LPCTSTR filename)
49
49
HMODULE rv2 = NULL ;
50
50
51
51
// References:
52
- // "Dynamic-Link Library Security" - https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-security
53
- // "Dynamic-Link Library Search Order" - https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order?redirectedfrom=MSDN
54
- // "SearchPath function" - https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-searchpathw
55
- // "SetSearchPathMode function" - https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setsearchpathmode
56
- // "SetDllDirectory function" - https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setdlldirectorya
57
- // "LoadLibraryExA function" - https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
52
+ // "Dynamic-Link Library Security" -
53
+ // https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-security
54
+ // "Dynamic-Link Library Search Order" -
55
+ // https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order?redirectedfrom=MSDN
56
+ // "SearchPath function" -
57
+ // https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-searchpathw
58
+ // "SetSearchPathMode function" -
59
+ // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setsearchpathmode
60
+ // "SetDllDirectory function" -
61
+ // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setdlldirectorya
62
+ // "LoadLibraryExA function" -
63
+ // https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
58
64
59
65
// Exclude current directory from the serch path
60
66
rv1 = SetDllDirectoryA (" " );
61
- if (0 == rv1)
62
- {
63
- printf ( " Intel dpctl FATAL ERROR: Cannot exclude current directory from serch path.\n " );
67
+ if (0 == rv1) {
68
+ printf ( " Intel dpctl FATAL ERROR: Cannot exclude current directory from "
69
+ " serch path.\n " );
64
70
return NULL ;
65
71
}
66
72
67
73
rv2 = LoadLibraryExA (filename, NULL , DONT_RESOLVE_DLL_REFERENCES);
68
- if (NULL == rv2)
69
- {
70
- printf ( " Intel dpctl FATAL ERROR: Cannot find/load library %s. \n " , filename);
74
+ if (NULL == rv2) {
75
+ printf ( " Intel dpctl FATAL ERROR: Cannot find/load library %s. \n " ,
76
+ filename);
71
77
return NULL ;
72
78
}
73
79
74
80
rv = GetModuleFileNameA (rv2, PathBuf, MAX_PATH);
75
- if (0 == rv)
76
- {
77
- printf ( " Intel dpctl FATAL ERROR: Cannot find module %s in memory. \n " , filename);
81
+ if (0 == rv) {
82
+ printf ( " Intel dpctl FATAL ERROR: Cannot find module %s in memory. \n " ,
83
+ filename);
78
84
return NULL ;
79
85
}
80
86
81
87
FreeLibrary (rv2);
82
88
rv2 = NULL ;
83
89
84
- size_t strLength = strnlen (PathBuf, MAX_PATH) + 1 ;
85
- wchar_t * wPathBuf = new wchar_t [strLength];
90
+ size_t strLength = strnlen (PathBuf, MAX_PATH) + 1 ;
91
+ wchar_t *wPathBuf = new wchar_t [strLength];
86
92
size_t convertedChars = 0 ;
87
93
mbstowcs_s (&convertedChars, wPathBuf, strLength, PathBuf, _TRUNCATE);
88
94
@@ -92,64 +98,77 @@ static HMODULE WINAPI _DPCTLLoadLibrary(LPCTSTR filename)
92
98
GUID pgActionID;
93
99
WINTRUST_DATA pWVTData;
94
100
95
- fdata.cbStruct = sizeof (WINTRUST_FILE_INFO);
96
- fdata.pcwszFilePath = wPathBuf;
97
- fdata.hFile = NULL ;
101
+ fdata.cbStruct = sizeof (WINTRUST_FILE_INFO);
102
+ fdata.pcwszFilePath = wPathBuf;
103
+ fdata.hFile = NULL ;
98
104
fdata.pgKnownSubject = NULL ;
99
105
100
106
pgActionID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
101
107
102
- pWVTData.cbStruct = sizeof (WINTRUST_DATA);
108
+ pWVTData.cbStruct = sizeof (WINTRUST_DATA);
103
109
pWVTData.pPolicyCallbackData = NULL ;
104
- pWVTData.pSIPClientData = NULL ;
105
- pWVTData.dwUIChoice = WTD_UI_NONE;
110
+ pWVTData.pSIPClientData = NULL ;
111
+ pWVTData.dwUIChoice = WTD_UI_NONE;
106
112
pWVTData.fdwRevocationChecks = WTD_REVOKE_NONE;
107
- pWVTData.dwUnionChoice = WTD_CHOICE_FILE;
108
- pWVTData.pFile = &fdata;
109
- pWVTData.dwStateAction = WTD_STATEACTION_VERIFY;
110
- pWVTData.hWVTStateData = NULL ;
111
- pWVTData.pwszURLReference = NULL ;
112
- pWVTData.dwProvFlags = 0 ;
113
- pWVTData.dwUIContext = WTD_UICONTEXT_EXECUTE;
114
- pWVTData.pSignatureSettings = NULL ;
113
+ pWVTData.dwUnionChoice = WTD_CHOICE_FILE;
114
+ pWVTData.pFile = &fdata;
115
+ pWVTData.dwStateAction = WTD_STATEACTION_VERIFY;
116
+ pWVTData.hWVTStateData = NULL ;
117
+ pWVTData.pwszURLReference = NULL ;
118
+ pWVTData.dwProvFlags = 0 ;
119
+ pWVTData.dwUIContext = WTD_UICONTEXT_EXECUTE;
120
+ pWVTData.pSignatureSettings = NULL ;
115
121
116
122
sverif = WinVerifyTrust ((HWND)INVALID_HANDLE_VALUE, &pgActionID, &pWVTData);
117
123
118
- switch (sverif)
119
- {
124
+ switch (sverif) {
120
125
case TRUST_E_NOSIGNATURE:
121
126
lerr = GetLastError ();
122
- if (TRUST_E_NOSIGNATURE == lerr || TRUST_E_SUBJECT_FORM_UNKNOWN == lerr || TRUST_E_PROVIDER_UNKNOWN == lerr)
127
+ if (TRUST_E_NOSIGNATURE == lerr ||
128
+ TRUST_E_SUBJECT_FORM_UNKNOWN == lerr ||
129
+ TRUST_E_PROVIDER_UNKNOWN == lerr)
123
130
{
124
131
printf (" Intel dpctl FATAL ERROR: %s is not signed.\n " , filename);
125
132
}
126
- else
127
- {
128
- printf (" Intel dpctl FATAL ERROR: An unknown error occurred trying to verify the signature of the %s.\n " , filename);
133
+ else {
134
+ printf (" Intel dpctl FATAL ERROR: An unknown error occurred trying "
135
+ " to verify the signature of the %s.\n " ,
136
+ filename);
129
137
}
130
138
break ;
131
139
132
- case TRUST_E_EXPLICIT_DISTRUST: printf (" Intel dpctl FATAL ERROR: The signature/publisher of %s is disallowed.\n " , filename); break ;
140
+ case TRUST_E_EXPLICIT_DISTRUST:
141
+ printf (" Intel dpctl FATAL ERROR: The signature/publisher of %s is "
142
+ " disallowed.\n " ,
143
+ filename);
144
+ break ;
133
145
134
- case ERROR_SUCCESS: break ;
146
+ case ERROR_SUCCESS:
147
+ break ;
135
148
136
- case TRUST_E_SUBJECT_NOT_TRUSTED: printf (" Intel dpctl FATAL ERROR: The signature of %s in not trusted.\n " , filename); break ;
149
+ case TRUST_E_SUBJECT_NOT_TRUSTED:
150
+ printf (" Intel dpctl FATAL ERROR: The signature of %s in not trusted.\n " ,
151
+ filename);
152
+ break ;
137
153
138
154
case CRYPT_E_SECURITY_SETTINGS:
139
- printf (" Intel dpctl FATAL ERROR: %s. The subject hash or publisher was not explicitly trusted and user trust was not allowed "
155
+ printf (" Intel dpctl FATAL ERROR: %s. The subject hash or publisher was "
156
+ " not explicitly trusted and user trust was not allowed "
140
157
" (CRYPT_E_SECURITY_SETTINGS).\n " ,
141
158
filename);
142
159
break ;
143
160
144
- default : printf (" Intel dpctl FATAL ERROR: %s. Error code is 0x%x.\n " , filename, (unsigned int )sverif); break ;
161
+ default :
162
+ printf (" Intel dpctl FATAL ERROR: %s. Error code is 0x%x.\n " , filename,
163
+ (unsigned int )sverif);
164
+ break ;
145
165
}
146
166
147
167
pWVTData.dwStateAction = WTD_STATEACTION_CLOSE;
148
168
WinVerifyTrust (NULL , &pgActionID, &pWVTData);
149
169
delete[] wPathBuf;
150
170
151
- if (ERROR_SUCCESS != sverif)
152
- {
171
+ if (ERROR_SUCCESS != sverif) {
153
172
return NULL ;
154
173
}
155
174
@@ -160,7 +179,7 @@ static HMODULE WINAPI _DPCTLLoadLibrary(LPCTSTR filename)
160
179
161
180
return rv2;
162
181
}
163
- #endif
182
+ #endif
164
183
165
184
DPCTL_API HMODULE _dpctl_load_win_dynamic_lib (LPCTSTR filename)
166
185
{
0 commit comments