| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | static const HRESULT E_SUSPECTED_TAMPERING = MAKE_HRESULT(SEVERITY_ERROR, 500/*FACILITY_WIX*/, 2001); |
| 6 | |
| 7 | static void AvoidLocalDllRedirection(LPCWSTR wzPath); |
| 8 | |
| 9 | |
| 10 | int WINAPI wWinMain( |
| 11 | __in HINSTANCE hInstance, |
| 12 | __in_opt HINSTANCE /* hPrevInstance */, |
| 13 | __in_z_opt LPWSTR lpCmdLine, |
| 14 | __in int nCmdShow |
| 15 | ) |
| 16 | { |
| 17 | HRESULT hr = S_OK; |
| 18 | DWORD dwExitCode = 0; |
| 19 | LPWSTR sczPath = NULL; |
| 20 | HANDLE hEngineFile = INVALID_HANDLE_VALUE; |
| 21 | |
| 22 | LPCWSTR rgsczSafelyLoadSystemDlls[] = |
| 23 | { |
| 24 | L"cabinet.dll", // required by Burn. |
| 25 | L"msi.dll", // required by Burn. |
| 26 | L"version.dll", // required by Burn. |
| 27 | L"wininet.dll", // required by Burn. |
| 28 | |
| 29 | L"comres.dll", // required by CLSIDFromProgID() when loading clbcatq.dll. |
| 30 | L"clbcatq.dll", // required by CLSIDFromProgID() when loading msxml?.dll. |
| 31 | |
| 32 | L"msasn1.dll", // required by DecryptFile() when loading crypt32.dll. |
| 33 | L"crypt32.dll", // required by DecryptFile() when loading feclient.dll. |
| 34 | L"feclient.dll", // unsafely loaded by DecryptFile(). |
| 35 | }; |
| 36 | |
| 37 | // Best effort attempt to get our file handle as soon as possible. |
| 38 | hr = PathForCurrentProcess(&sczPath, NULL); |
| 39 | if (SUCCEEDED(hr)) |
| 40 | { |
| 41 | hEngineFile = ::CreateFileW(sczPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 42 | } |
| 43 | |
| 44 | AppInitialize(rgsczSafelyLoadSystemDlls, countof(rgsczSafelyLoadSystemDlls)); |
| 45 | |
| 46 | AvoidLocalDllRedirection(sczPath); |
| 47 | |
| 48 | // call run |
| 49 | hr = EngineRun(hInstance, hEngineFile, lpCmdLine, nCmdShow, &dwExitCode); |
| 50 | ExitOnFailure(hr, "Failed to run application."); |
| 51 | |
| 52 | LExit: |
| 53 | ReleaseFileHandle(hEngineFile); |
| 54 | ReleaseStr(sczPath); |
| 55 | |
| 56 | |
| 57 | return FAILED(hr) ? (int)hr : (int)dwExitCode; |
| 58 | } |
| 59 | |
| 60 | static void AvoidLocalDllRedirection(LPCWSTR wzPath) |
| 61 | { |
| 62 | LPWSTR sczLocalPath = NULL; |
| 63 | HMODULE hmodComCtl = NULL; |
| 64 | |
| 65 | // Bail if there's a <bundle>.exe.local directory, as it's a feature of |
| 66 | // DLL redirection that has no real use for a bundle and is a hole for |
| 67 | // DLL hijacking attacks. |
| 68 | |
| 69 | if (FAILED(StrAllocFormatted(&sczLocalPath, L"%ls.local", wzPath)) |
| 70 | || DirExists(sczLocalPath, NULL) |
| 71 | || FileExistsEx(sczLocalPath, NULL) |
| 72 | || FAILED(LoadSystemLibrary(L"Comctl32.dll", &hmodComCtl))) |
| 73 | { |
| 74 | ::ExitProcess((UINT)E_SUSPECTED_TAMPERING); |
| 75 | } |
| 76 | |
| 77 | ReleaseStr(sczLocalPath); |
| 78 | } |