1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/win/conflicts/module_blocklist_cache_updater.h"
#include <windows.h>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/hash/sha1.h"
#include "base/i18n/case_conversion.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_path_override.h"
#include "base/test/task_environment.h"
#include "base/test/test_reg_util_win.h"
#include "base/win/pe_image.h"
#include "base/win/registry.h"
#include "chrome/browser/win/conflicts/module_blocklist_cache_util.h"
#include "chrome/browser/win/conflicts/module_info.h"
#include "chrome/browser/win/conflicts/module_list_filter.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/install_static/install_util.h"
#include "content/public/common/process_type.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr base::FilePath::CharType kCertificatePath[] =
FILE_PATH_LITERAL("CertificatePath");
constexpr char16_t kCertificateSubject[] = u"CertificateSubject";
constexpr base::FilePath::CharType kDllPath1[] =
FILE_PATH_LITERAL("c:\\path\\to\\module.dll");
constexpr base::FilePath::CharType kDllPath2[] =
FILE_PATH_LITERAL("c:\\some\\shellextension.dll");
// Returns a new ModuleInfoData marked as loaded into the browser process but
// otherwise empty.
ModuleInfoData CreateLoadedModuleInfoData() {
ModuleInfoData module_data;
module_data.module_properties |= ModuleInfoData::kPropertyLoadedModule;
module_data.process_types |= ProcessTypeToBit(content::PROCESS_TYPE_BROWSER);
module_data.inspection_result = std::make_optional<ModuleInspectionResult>();
return module_data;
}
// Returns a new ModuleInfoData marked as loaded into the process with a
// CertificateInfo that matches kCertificateSubject.
ModuleInfoData CreateSignedLoadedModuleInfoData() {
ModuleInfoData module_data = CreateLoadedModuleInfoData();
module_data.inspection_result->certificate_info.type =
CertificateInfo::Type::CERTIFICATE_IN_FILE;
module_data.inspection_result->certificate_info.path =
base::FilePath(kCertificatePath);
module_data.inspection_result->certificate_info.subject = kCertificateSubject;
return module_data;
}
void GetModulePath(HMODULE module_handle, base::FilePath* module_path) {
base::FilePath result;
wchar_t buffer[MAX_PATH];
DWORD length = ::GetModuleFileName(module_handle, buffer, MAX_PATH);
ASSERT_NE(length, 0U);
ASSERT_LT(length, static_cast<DWORD>(MAX_PATH));
*module_path = base::FilePath(buffer);
}
// Returns true if the cache path registry key value exists.
bool RegistryKeyExists() {
base::win::RegKey reg_key(HKEY_CURRENT_USER,
install_static::GetRegistryPath()
.append(third_party_dlls::kThirdPartyRegKeyName)
.c_str(),
KEY_READ);
return reg_key.HasValue(third_party_dlls::kBlFilePathRegValue);
}
} // namespace
class ModuleBlocklistCacheUpdaterTest : public testing::Test,
public ModuleDatabaseEventSource {
public:
ModuleBlocklistCacheUpdaterTest(const ModuleBlocklistCacheUpdaterTest&) =
delete;
ModuleBlocklistCacheUpdaterTest& operator=(
const ModuleBlocklistCacheUpdaterTest&) = delete;
protected:
ModuleBlocklistCacheUpdaterTest()
: dll1_(kDllPath1),
dll2_(kDllPath2),
task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME),
user_data_dir_override_(chrome::DIR_USER_DATA),
module_list_filter_(CreateModuleListFilter()),
module_blocklist_cache_path_(
ModuleBlocklistCacheUpdater::GetModuleBlocklistCachePath()) {
exe_certificate_info_.type = CertificateInfo::Type::CERTIFICATE_IN_FILE;
exe_certificate_info_.path = base::FilePath(kCertificatePath);
exe_certificate_info_.subject = kCertificateSubject;
}
void SetUp() override {
ASSERT_TRUE(base::CreateDirectory(module_blocklist_cache_path().DirName()));
ASSERT_NO_FATAL_FAILURE(
registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER));
}
std::unique_ptr<ModuleBlocklistCacheUpdater>
CreateModuleBlocklistCacheUpdater() {
return std::make_unique<ModuleBlocklistCacheUpdater>(
this, exe_certificate_info_, module_list_filter_,
initial_blocklisted_modules_,
base::BindRepeating(
&ModuleBlocklistCacheUpdaterTest::OnModuleBlocklistCacheUpdated,
base::Unretained(this)),
false);
}
void RunUntilIdle() { task_environment_.RunUntilIdle(); }
void FastForwardBy(base::TimeDelta delta) {
task_environment_.FastForwardBy(delta);
// The expired timer callback posts a task to update the cache. Wait for it
// to finish.
task_environment_.RunUntilIdle();
}
base::FilePath& module_blocklist_cache_path() {
return module_blocklist_cache_path_;
}
bool on_cache_updated_callback_invoked() {
return on_cache_updated_callback_invoked_;
}
// ModuleDatabaseEventSource:
void AddObserver(ModuleDatabaseObserver* observer) override {}
void RemoveObserver(ModuleDatabaseObserver* observer) override {}
const base::FilePath dll1_;
const base::FilePath dll2_;
private:
scoped_refptr<ModuleListFilter> CreateModuleListFilter() {
chrome::conflicts::ModuleList module_list;
// Include an empty blocklist and allowlist.
module_list.mutable_blocklist();
module_list.mutable_allowlist();
// Serialize the module list to the user data directory.
base::FilePath module_list_path;
if (!base::PathService::Get(chrome::DIR_USER_DATA, &module_list_path))
return nullptr;
module_list_path =
module_list_path.Append(FILE_PATH_LITERAL("ModuleList.bin"));
std::string contents;
if (!module_list.SerializeToString(&contents) ||
!base::WriteFile(module_list_path, contents)) {
return nullptr;
}
auto module_list_filter = base::MakeRefCounted<ModuleListFilter>();
if (!module_list_filter->Initialize(module_list_path))
return nullptr;
return module_list_filter;
}
void OnModuleBlocklistCacheUpdated(
const ModuleBlocklistCacheUpdater::CacheUpdateResult& result) {
on_cache_updated_callback_invoked_ = true;
}
base::test::TaskEnvironment task_environment_;
registry_util::RegistryOverrideManager registry_override_manager_;
base::ScopedPathOverride user_data_dir_override_;
CertificateInfo exe_certificate_info_;
scoped_refptr<ModuleListFilter> module_list_filter_;
std::vector<third_party_dlls::PackedListModule> initial_blocklisted_modules_;
base::FilePath module_blocklist_cache_path_;
bool on_cache_updated_callback_invoked_ = false;
};
TEST_F(ModuleBlocklistCacheUpdaterTest, OneThirdPartyModule) {
EXPECT_FALSE(base::PathExists(module_blocklist_cache_path()));
auto module_blocklist_cache_updater = CreateModuleBlocklistCacheUpdater();
// Simulate some arbitrary module loading into the process.
ModuleInfoKey module_key(dll1_, 0, 0);
module_blocklist_cache_updater->OnNewModuleFound(
module_key, CreateLoadedModuleInfoData());
module_blocklist_cache_updater->OnModuleDatabaseIdle();
RunUntilIdle();
EXPECT_TRUE(base::PathExists(module_blocklist_cache_path()));
EXPECT_TRUE(on_cache_updated_callback_invoked());
EXPECT_TRUE(RegistryKeyExists());
// Check the cache.
third_party_dlls::PackedListMetadata metadata;
std::vector<third_party_dlls::PackedListModule> blocklisted_modules;
base::MD5Digest md5_digest;
EXPECT_EQ(ReadResult::kSuccess,
ReadModuleBlocklistCache(module_blocklist_cache_path(), &metadata,
&blocklisted_modules, &md5_digest));
EXPECT_EQ(1u, blocklisted_modules.size());
ASSERT_EQ(
ModuleBlocklistCacheUpdater::ModuleBlockingDecision::kDisallowedImplicit,
module_blocklist_cache_updater->GetModuleBlockingState(module_key)
.blocking_decision);
}
TEST_F(ModuleBlocklistCacheUpdaterTest, IgnoreMicrosoftModules) {
EXPECT_FALSE(base::PathExists(module_blocklist_cache_path()));
// base::RunLoop run_loop;
auto module_blocklist_cache_updater = CreateModuleBlocklistCacheUpdater();
// Simulate a Microsoft module loading into the process.
base::win::PEImage kernel32_image(::GetModuleHandle(L"kernel32.dll"));
ASSERT_TRUE(kernel32_image.module());
base::FilePath module_path;
ASSERT_NO_FATAL_FAILURE(GetModulePath(kernel32_image.module(), &module_path));
ASSERT_FALSE(module_path.empty());
uint32_t module_size =
kernel32_image.GetNTHeaders()->OptionalHeader.SizeOfImage;
uint32_t time_date_stamp =
kernel32_image.GetNTHeaders()->FileHeader.TimeDateStamp;
ModuleInfoKey module_key(module_path, module_size, time_date_stamp);
ModuleInfoData module_data = CreateLoadedModuleInfoData();
module_data.inspection_result = InspectModule(module_key.module_path);
module_blocklist_cache_updater->OnNewModuleFound(module_key, module_data);
module_blocklist_cache_updater->OnModuleDatabaseIdle();
RunUntilIdle();
EXPECT_TRUE(base::PathExists(module_blocklist_cache_path()));
EXPECT_TRUE(on_cache_updated_callback_invoked());
EXPECT_TRUE(RegistryKeyExists());
// Check the cache.
third_party_dlls::PackedListMetadata metadata;
std::vector<third_party_dlls::PackedListModule> blocklisted_modules;
base::MD5Digest md5_digest;
EXPECT_EQ(ReadResult::kSuccess,
ReadModuleBlocklistCache(module_blocklist_cache_path(), &metadata,
&blocklisted_modules, &md5_digest));
EXPECT_EQ(0u, blocklisted_modules.size());
ASSERT_EQ(
ModuleBlocklistCacheUpdater::ModuleBlockingDecision::kAllowedMicrosoft,
module_blocklist_cache_updater->GetModuleBlockingState(module_key)
.blocking_decision);
}
// Tests that modules with a matching certificate subject are allowlisted.
TEST_F(ModuleBlocklistCacheUpdaterTest, allowlistMatchingCertificateSubject) {
EXPECT_FALSE(base::PathExists(module_blocklist_cache_path()));
auto module_blocklist_cache_updater = CreateModuleBlocklistCacheUpdater();
// Simulate the module loading into the process.
ModuleInfoKey module_key(dll1_, 0, 0);
module_blocklist_cache_updater->OnNewModuleFound(
module_key, CreateSignedLoadedModuleInfoData());
module_blocklist_cache_updater->OnModuleDatabaseIdle();
RunUntilIdle();
EXPECT_TRUE(base::PathExists(module_blocklist_cache_path()));
EXPECT_TRUE(on_cache_updated_callback_invoked());
EXPECT_TRUE(RegistryKeyExists());
// Check the cache.
third_party_dlls::PackedListMetadata metadata;
std::vector<third_party_dlls::PackedListModule> blocklisted_modules;
base::MD5Digest md5_digest;
EXPECT_EQ(ReadResult::kSuccess,
ReadModuleBlocklistCache(module_blocklist_cache_path(), &metadata,
&blocklisted_modules, &md5_digest));
EXPECT_EQ(0u, blocklisted_modules.size());
ASSERT_EQ(ModuleBlocklistCacheUpdater::ModuleBlockingDecision::
kAllowedSameCertificate,
module_blocklist_cache_updater->GetModuleBlockingState(module_key)
.blocking_decision);
}
// Make sure IMEs are allowed while shell extensions are blocklisted.
TEST_F(ModuleBlocklistCacheUpdaterTest, RegisteredModules) {
EXPECT_FALSE(base::PathExists(module_blocklist_cache_path()));
auto module_blocklist_cache_updater = CreateModuleBlocklistCacheUpdater();
// Set the respective bit for registered modules.
ModuleInfoKey module_key1(dll1_, 123u, 456u);
ModuleInfoData module_data1 = CreateLoadedModuleInfoData();
module_data1.module_properties |= ModuleInfoData::kPropertyIme;
ModuleInfoKey module_key2(dll2_, 456u, 789u);
ModuleInfoData module_data2 = CreateLoadedModuleInfoData();
module_data2.module_properties |= ModuleInfoData::kPropertyShellExtension;
// Simulate the modules loading into the process.
module_blocklist_cache_updater->OnNewModuleFound(module_key1, module_data1);
module_blocklist_cache_updater->OnNewModuleFound(module_key2, module_data2);
module_blocklist_cache_updater->OnModuleDatabaseIdle();
RunUntilIdle();
EXPECT_TRUE(base::PathExists(module_blocklist_cache_path()));
EXPECT_TRUE(on_cache_updated_callback_invoked());
EXPECT_TRUE(RegistryKeyExists());
// Check the cache.
third_party_dlls::PackedListMetadata metadata;
std::vector<third_party_dlls::PackedListModule> blocklisted_modules;
base::MD5Digest md5_digest;
EXPECT_EQ(ReadResult::kSuccess,
ReadModuleBlocklistCache(module_blocklist_cache_path(), &metadata,
&blocklisted_modules, &md5_digest));
// Make sure the only blocklisted module is the shell extension.
ASSERT_EQ(1u, blocklisted_modules.size());
ASSERT_EQ(ModuleBlocklistCacheUpdater::ModuleBlockingDecision::kAllowedIME,
module_blocklist_cache_updater->GetModuleBlockingState(module_key1)
.blocking_decision);
ASSERT_EQ(
ModuleBlocklistCacheUpdater::ModuleBlockingDecision::kDisallowedImplicit,
module_blocklist_cache_updater->GetModuleBlockingState(module_key2)
.blocking_decision);
third_party_dlls::PackedListModule expected;
const std::string module_basename = base::UTF16ToUTF8(
base::i18n::ToLower(module_key2.module_path.BaseName().AsUTF16Unsafe()));
base::span(expected.basename_hash)
.copy_from(base::SHA1Hash(base::as_byte_span(module_basename)));
const std::string module_code_id = GenerateCodeId(module_key2);
base::span(expected.code_id_hash)
.copy_from(base::SHA1Hash(base::as_byte_span(module_code_id)));
EXPECT_TRUE(internal::ModuleEqual()(expected, blocklisted_modules[0]));
}
TEST_F(ModuleBlocklistCacheUpdaterTest, DisableModuleAnalysis) {
EXPECT_FALSE(base::PathExists(module_blocklist_cache_path()));
auto module_blocklist_cache_updater = CreateModuleBlocklistCacheUpdater();
module_blocklist_cache_updater->DisableModuleAnalysis();
// Simulate some arbitrary module loading into the process.
ModuleInfoKey module_key(dll1_, 0, 0);
module_blocklist_cache_updater->OnNewModuleFound(
module_key, CreateLoadedModuleInfoData());
module_blocklist_cache_updater->OnModuleDatabaseIdle();
RunUntilIdle();
EXPECT_TRUE(base::PathExists(module_blocklist_cache_path()));
EXPECT_TRUE(on_cache_updated_callback_invoked());
EXPECT_TRUE(RegistryKeyExists());
// Check the cache.
third_party_dlls::PackedListMetadata metadata;
std::vector<third_party_dlls::PackedListModule> blocklisted_modules;
base::MD5Digest md5_digest;
EXPECT_EQ(ReadResult::kSuccess,
ReadModuleBlocklistCache(module_blocklist_cache_path(), &metadata,
&blocklisted_modules, &md5_digest));
// The module is not added to the blocklist.
EXPECT_EQ(0u, blocklisted_modules.size());
ASSERT_EQ(ModuleBlocklistCacheUpdater::ModuleBlockingDecision::kNotAnalyzed,
module_blocklist_cache_updater->GetModuleBlockingState(module_key)
.blocking_decision);
}
|