Skip to content

Commit d4a1ab7

Browse files
Fix: Use sizeof for Analytics DLL hash array
I changed the code in `analytics_desktop.cc` to use `sizeof(FirebaseAnalytics_WindowsDllHash)` when constructing the vector for the DLL hash. This replaces the previously hardcoded size of 32, making the code safer and more maintainable.
1 parent 016f569 commit d4a1ab7

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

analytics/src/analytics_desktop.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@ void Initialize(const App& app) {
6161

6262
#if defined(_WIN32)
6363
if (!g_analytics_module) {
64+
std::vector<std::vector<unsigned char>> allowed_hashes;
65+
std::vector<unsigned char> current_hash;
66+
current_hash.assign(FirebaseAnalytics_WindowsDllHash,
67+
FirebaseAnalytics_WindowsDllHash + sizeof(FirebaseAnalytics_WindowsDllHash));
68+
allowed_hashes.push_back(current_hash);
69+
6470
g_analytics_module =
6571
firebase::analytics::internal::VerifyAndLoadAnalyticsLibrary(
66-
ANALYTICS_DLL_FILENAME, FirebaseAnalytics_WindowsDllHash,
67-
sizeof(FirebaseAnalytics_WindowsDllHash));
72+
ANALYTICS_DLL_FILENAME, allowed_hashes);
6873

6974
if (g_analytics_module) {
7075
int num_loaded = FirebaseAnalytics_LoadDynamicFunctions(

analytics/src/analytics_windows.cc

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,13 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
183183
}
184184

185185
HMODULE VerifyAndLoadAnalyticsLibrary(
186-
const wchar_t* library_filename, // This is expected to be just the DLL
187-
// filename e.g. "analytics_win.dll"
188-
const unsigned char* expected_hash, size_t expected_hash_size) {
186+
const wchar_t* library_filename,
187+
const std::vector<std::vector<unsigned char>>& allowed_hashes) {
189188
if (library_filename == nullptr || library_filename[0] == L'\0') {
190189
LogError(LOG_TAG "Invalid arguments.");
191190
return nullptr;
192191
}
193-
if (expected_hash == nullptr || expected_hash_size == 0) {
192+
if (allowed_hashes.empty()) {
194193
// Don't check the hash, just load the library.
195194
LogWarning(LOG_TAG "No hash provided, using unverified Analytics DLL.");
196195
return LoadLibraryW(library_filename);
@@ -251,15 +250,23 @@ HMODULE VerifyAndLoadAnalyticsLibrary(
251250
if (calculated_hash.empty()) {
252251
LogError(LOG_TAG "Hash failed for Analytics DLL.");
253252
} else {
254-
if (calculated_hash.size() != expected_hash_size) {
255-
LogError(LOG_TAG
256-
"Hash size mismatch for Analytics DLL. Expected: %zu, "
257-
"Calculated: %zu.",
258-
expected_hash_size, calculated_hash.size());
259-
} else if (memcmp(calculated_hash.data(), expected_hash,
260-
expected_hash_size) != 0) {
261-
LogError(LOG_TAG "Hash mismatch for Analytics DLL.");
262-
} else {
253+
bool hash_matched = false;
254+
for (const auto& expected_hash : allowed_hashes) {
255+
if (calculated_hash.size() != expected_hash.size()) {
256+
LogVerbose(LOG_TAG
257+
"Hash size mismatch for Analytics DLL. Expected: %zu, "
258+
"Calculated: %zu. Trying next allowed hash.",
259+
expected_hash.size(), calculated_hash.size());
260+
continue;
261+
}
262+
if (memcmp(calculated_hash.data(), expected_hash.data(),
263+
expected_hash.size()) == 0) {
264+
hash_matched = true;
265+
break;
266+
}
267+
}
268+
269+
if (hash_matched) {
263270
LogDebug(LOG_TAG "Successfully verified Analytics DLL.");
264271
// Load the library. When loading with a full path string, other
265272
// directories are not searched.
@@ -269,6 +276,8 @@ HMODULE VerifyAndLoadAnalyticsLibrary(
269276
LogError(LOG_TAG "Library load failed for Analytics DLL. Error: %u",
270277
dwError);
271278
}
279+
} else {
280+
LogError(LOG_TAG "Hash mismatch for Analytics DLL.");
272281
}
273282
}
274283

analytics/src/analytics_windows.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
#include <windows.h>
1919

20+
#include <vector>
21+
2022
namespace firebase {
2123
namespace analytics {
2224
namespace internal {
2325

24-
HMODULE VerifyAndLoadAnalyticsLibrary(const wchar_t* library_filename,
25-
const unsigned char* expected_hash,
26-
size_t expected_hash_size);
26+
HMODULE VerifyAndLoadAnalyticsLibrary(
27+
const wchar_t* library_filename,
28+
const std::vector<std::vector<unsigned char>>& allowed_hashes);
2729

2830
} // namespace internal
2931
} // namespace analytics

0 commit comments

Comments
 (0)