|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 [Ribose Inc](https://www.ribose.com). |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, |
| 6 | + * are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 19 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <windows.h> |
| 28 | +#include <wintrust.h> |
| 29 | +#include <softpub.h> |
| 30 | +#include <tlhelp32.h> |
| 31 | + |
| 32 | +#include <stdio.h> |
| 33 | +#include <string> |
| 34 | + |
| 35 | +#include "dll-verify.h" |
| 36 | + |
| 37 | +namespace { |
| 38 | + |
| 39 | +/* Checks whether the file has a valid embedded Authenticode signature which |
| 40 | + * chains to a trusted root. Revocation is not checked so the verification |
| 41 | + * also works on offline systems. */ |
| 42 | +bool |
| 43 | +file_has_valid_signature(const std::wstring &path) |
| 44 | +{ |
| 45 | + WINTRUST_FILE_INFOW file = {}; |
| 46 | + file.cbStruct = sizeof(file); |
| 47 | + file.pcwszFilePath = path.c_str(); |
| 48 | + |
| 49 | + GUID policy = WINTRUST_ACTION_GENERIC_VERIFY_V2; |
| 50 | + WINTRUST_DATAW data = {}; |
| 51 | + data.cbStruct = sizeof(data); |
| 52 | + data.dwUIChoice = WTD_UI_NONE; |
| 53 | + data.fdwRevocationChecks = WTD_REVOCATION_CHECK_NONE; |
| 54 | + data.dwUnionChoice = WTD_CHOICE_FILE; |
| 55 | + data.pFile = &file; |
| 56 | + data.dwStateAction = WTD_STATEACTION_VERIFY; |
| 57 | + |
| 58 | + HWND dummy = static_cast<HWND>(INVALID_HANDLE_VALUE); |
| 59 | + LONG res = WinVerifyTrustW(dummy, &policy, &data); |
| 60 | + data.dwStateAction = WTD_STATEACTION_CLOSE; |
| 61 | + WinVerifyTrustW(dummy, &policy, &data); |
| 62 | + return res == ERROR_SUCCESS; |
| 63 | +} |
| 64 | + |
| 65 | +std::wstring |
| 66 | +directory_of(const std::wstring &path) |
| 67 | +{ |
| 68 | + size_t pos = path.find_last_of(L"\\/"); |
| 69 | + return (pos == std::wstring::npos) ? std::wstring() : path.substr(0, pos); |
| 70 | +} |
| 71 | + |
| 72 | +bool |
| 73 | +path_in_directory(const std::wstring &path, const std::wstring &dir) |
| 74 | +{ |
| 75 | + if (dir.empty() || path.size() <= dir.size() + 1) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + if (_wcsnicmp(path.c_str(), dir.c_str(), dir.size()) != 0) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + return path[dir.size()] == L'\\' || path[dir.size()] == L'/'; |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace |
| 85 | + |
| 86 | +bool |
| 87 | +rnp_dll_verify_modules() |
| 88 | +{ |
| 89 | + wchar_t exepath[32768] = {0}; |
| 90 | + if (!GetModuleFileNameW(NULL, exepath, 32768)) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + std::wstring exe(exepath); |
| 94 | + /* Unsigned executable is a development build: a swapped DLL does not gain |
| 95 | + * any trusted-process status, so there is nothing to enforce. */ |
| 96 | + if (!file_has_valid_signature(exe)) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); |
| 101 | + if (snap == INVALID_HANDLE_VALUE) { |
| 102 | + fwprintf(stderr, L"warning: could not enumerate loaded modules.\n"); |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + std::wstring exedir = directory_of(exe); |
| 107 | + bool trusted = true; |
| 108 | + MODULEENTRY32W entry = {}; |
| 109 | + entry.dwSize = sizeof(entry); |
| 110 | + if (Module32FirstW(snap, &entry)) { |
| 111 | + do { |
| 112 | + std::wstring path(entry.szExePath); |
| 113 | + if (path == exe || !path_in_directory(path, exedir)) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + if (!file_has_valid_signature(path)) { |
| 117 | + fwprintf(stderr, |
| 118 | + L"error: module '%s' is loaded from the application directory " |
| 119 | + L"but has no valid digital signature - possible DLL " |
| 120 | + L"side-loading. Refusing to continue.\n", |
| 121 | + path.c_str()); |
| 122 | + trusted = false; |
| 123 | + } |
| 124 | + } while (Module32NextW(snap, &entry)); |
| 125 | + } |
| 126 | + CloseHandle(snap); |
| 127 | + return trusted; |
| 128 | +} |
0 commit comments