Skip to content

Commit

Permalink
NTRegistry - added wow64 redirection support.
Browse files Browse the repository at this point in the history
TESTS=chrome_elf_unittests: NtRegistryTest*
BUG=641169
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.win:win10_chromium_x64_rel_ng

Review-Url: https://codereview.chromium.org/2345913003
Cr-Commit-Position: refs/heads/master@{#422919}
  • Loading branch information
pennymac authored and Commit bot committed Oct 4, 2016
1 parent 546e56f commit 5379f17
Show file tree
Hide file tree
Showing 12 changed files with 1,076 additions and 278 deletions.
2 changes: 1 addition & 1 deletion chrome/install_static/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ assert(is_win)
# Please don't add dependencies on other system libraries.
static_library("install_static_util") {
public_deps = [
"//chrome_elf/nt_registry:nt_registry",
"//chrome_elf:nt_registry",
]

sources = [
Expand Down
29 changes: 15 additions & 14 deletions chrome/install_static/install_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,18 @@ bool GetCollectStatsConsentImpl(const std::wstring& exe_path) {
std::wstring full_key_path(kRegPathClientStateMedium);
full_key_path.append(1, L'\\');
full_key_path.append(app_guid);
if (system_install && nt::QueryRegValueDWORD(nt::HKLM, full_key_path.c_str(),
kRegValueUsageStats, &out_value))
if (system_install &&
nt::QueryRegValueDWORD(nt::HKLM, nt::WOW6432, full_key_path.c_str(),
kRegValueUsageStats, &out_value))
return (out_value == 1);

// Second, try kRegPathClientState.
full_key_path = kRegPathClientState;
full_key_path.append(1, L'\\');
full_key_path.append(app_guid);
return (nt::QueryRegValueDWORD((system_install ? nt::HKLM : nt::HKCU),
full_key_path.c_str(), kRegValueUsageStats,
&out_value) &&
nt::WOW6432, full_key_path.c_str(),
kRegValueUsageStats, &out_value) &&
out_value == 1);
}

Expand Down Expand Up @@ -410,8 +411,8 @@ bool IsMultiInstall(bool is_system_install) {
full_key_path.append(1, L'\\');
full_key_path.append(kAppGuidGoogleChrome);
if (!nt::QueryRegValueSZ((is_system_install ? nt::HKLM : nt::HKCU),
full_key_path.c_str(), kUninstallArgumentsField,
&args))
nt::WOW6432, full_key_path.c_str(),
kUninstallArgumentsField, &args))
return false;

return (args.find(L"--multi-install") != std::wstring::npos);
Expand All @@ -429,7 +430,7 @@ bool GetCollectStatsInSample() {
std::wstring registry_path = GetChromeInstallRegistryPath();

DWORD out_value = 0;
if (!nt::QueryRegValueDWORD(nt::HKCU, registry_path.c_str(),
if (!nt::QueryRegValueDWORD(nt::HKCU, nt::WOW6432, registry_path.c_str(),
kRegValueChromeStatsSample, &out_value)) {
// If reading the value failed, treat it as though sampling isn't in effect,
// implicitly meaning this install is in the sample.
Expand All @@ -442,8 +443,8 @@ bool SetCollectStatsInSample(bool in_sample) {
std::wstring registry_path = GetChromeInstallRegistryPath();

HANDLE key_handle = INVALID_HANDLE_VALUE;
if (!nt::CreateRegKey(nt::HKCU, registry_path.c_str(), KEY_SET_VALUE,
&key_handle)) {
if (!nt::CreateRegKey(nt::HKCU, registry_path.c_str(),
KEY_SET_VALUE | KEY_WOW64_32KEY, &key_handle)) {
nt::CloseRegKey(key_handle);
return false;
}
Expand All @@ -456,14 +457,14 @@ bool ReportingIsEnforcedByPolicy(bool* crash_reporting_enabled) {
DWORD value = 0;

// First, try HKLM.
if (nt::QueryRegValueDWORD(nt::HKLM, kRegPathChromePolicy,
if (nt::QueryRegValueDWORD(nt::HKLM, nt::NONE, kRegPathChromePolicy,
kMetricsReportingEnabled, &value)) {
*crash_reporting_enabled = (value != 0);
return true;
}

// Second, try HKCU.
if (nt::QueryRegValueDWORD(nt::HKCU, kRegPathChromePolicy,
if (nt::QueryRegValueDWORD(nt::HKCU, nt::NONE, kRegPathChromePolicy,
kMetricsReportingEnabled, &value)) {
*crash_reporting_enabled = (value != 0);
return true;
Expand Down Expand Up @@ -643,13 +644,13 @@ void GetChromeChannelName(bool is_per_user_install,
std::wstring full_key_path(kRegPathClientState);
full_key_path.append(1, L'\\');
full_key_path.append(kAppGuidGoogleBinaries);
nt::QueryRegValueSZ(is_per_user_install ? nt::HKCU : nt::HKLM,
nt::QueryRegValueSZ(is_per_user_install ? nt::HKCU : nt::HKLM, nt::WOW6432,
full_key_path.c_str(), kRegApField, &value);
} else {
std::wstring full_key_path(kRegPathClientState);
full_key_path.append(1, L'\\');
full_key_path.append(kAppGuidGoogleChrome);
nt::QueryRegValueSZ(is_per_user_install ? nt::HKCU : nt::HKLM,
nt::QueryRegValueSZ(is_per_user_install ? nt::HKCU : nt::HKLM, nt::WOW6432,
full_key_path.c_str(), kRegApField, &value);
}

Expand Down Expand Up @@ -695,7 +696,7 @@ std::string GetGoogleUpdateVersion() {
// Consider whether Chromium should connect to Google update to manage
// updates. Should this be returning an empty string for Chromium builds?.
std::wstring update_version;
if (nt::QueryRegValueSZ(nt::AUTO, kRegPathGoogleUpdate,
if (nt::QueryRegValueSZ(nt::AUTO, nt::WOW6432, kRegPathGoogleUpdate,
kRegGoogleUpdateVersion, &update_version))
return UTF16ToUTF8(update_version);

Expand Down
29 changes: 24 additions & 5 deletions chrome_elf/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ shared_library("chrome_elf") {
":constants",
":crash",
":hook_util",
":nt_registry",
":security",
"//build/config/sanitizers:deps",
"//chrome/install_static:install_static_util",
"//chrome_elf/nt_registry:nt_registry",
]
configs += [ "//build/config/win:windowed" ]
configs -= [ "//build/config/win:console" ]
Expand Down Expand Up @@ -76,7 +76,7 @@ source_set("security") {
]
deps = [
":constants",
"//chrome_elf/nt_registry:nt_registry",
":nt_registry",
]
}

Expand Down Expand Up @@ -126,9 +126,9 @@ static_library("blacklist") {
":constants",
":crash",
":hook_util",
":nt_registry",
"//base:base_static", # pe_image
"//chrome/install_static:install_static_util",
"//chrome_elf/nt_registry:nt_registry",
]
}

Expand Down Expand Up @@ -159,11 +159,24 @@ static_library("hook_util") {
"hook_util/hook_util.h",
]
deps = [
":nt_registry", # utils
"//base:base_static", # pe_image
"//sandbox",
]
}

# This target contains utility functions which must only depend on
# kernel32. Please don't add dependencies on other system libraries.
static_library("nt_registry") {
sources = [
"../sandbox/win/src/nt_internals.h",
"nt_registry/nt_registry.cc",
"nt_registry/nt_registry.h",
]

libs = [ "kernel32.lib" ]
}

##------------------------------------------------------------------------------
## tests
##------------------------------------------------------------------------------
Expand All @@ -175,6 +188,7 @@ test("chrome_elf_unittests") {
"chrome_elf_util_unittest.cc",
"elf_imports_unittest.cc",
"hook_util/test/hook_util_test.cc",
"nt_registry/nt_registry_unittest.cc",
"run_all_unittests.cc",
]
include_dirs = [ "$target_gen_dir" ]
Expand All @@ -185,13 +199,13 @@ test("chrome_elf_unittests") {
":crash",
":hook_util",
":hook_util_test_dll",
":nt_registry",
":security",
"//base",
"//base/test:test_support",
"//chrome",
"//chrome/common:version_header",
"//chrome/install_static:install_static_util",
"//chrome_elf/nt_registry:nt_registry",
"//sandbox",
"//testing/gtest",
]
Expand Down Expand Up @@ -222,20 +236,22 @@ test("chrome_elf_unittests") {
}

shared_library("blacklist_test_main_dll") {
testonly = true
sources = [
"blacklist/test/blacklist_test_main_dll.cc",
"blacklist/test/blacklist_test_main_dll.def",
]
deps = [
":blacklist",
":nt_registry",
"//base",
"//build/config/sanitizers:deps",
"//chrome/install_static:install_static_util",
"//chrome_elf/nt_registry:nt_registry",
]
}

loadable_module("blacklist_test_dll_1") {
testonly = true
sources = [
"blacklist/test/blacklist_test_dll_1.cc",
"blacklist/test/blacklist_test_dll_1.def",
Expand All @@ -246,6 +262,7 @@ loadable_module("blacklist_test_dll_1") {
}

loadable_module("blacklist_test_dll_2") {
testonly = true
sources = [
"blacklist/test/blacklist_test_dll_2.cc",
"blacklist/test/blacklist_test_dll_2.def",
Expand All @@ -261,6 +278,7 @@ loadable_module("blacklist_test_dll_2") {
# which does), Ninja would get confused and always rebuild this target because
# it sees a declared output file but that file doesn't exist on disk.
loadable_module("blacklist_test_dll_3") {
testonly = true
sources = [
"blacklist/test/blacklist_test_dll_3.cc",
]
Expand All @@ -270,6 +288,7 @@ loadable_module("blacklist_test_dll_3") {
}

shared_library("hook_util_test_dll") {
testonly = true
sources = [
"hook_util/test/hook_util_test_dll.cc",
"hook_util/test/hook_util_test_dll.h",
Expand Down
21 changes: 9 additions & 12 deletions chrome_elf/blacklist/test/blacklist_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,18 @@ class BlacklistTest : public testing::Test {
// process-specific environment variables, for our test DLLs to access.
// This will only work as long as the IPC is within the same process.
void IpcOverrides() {
if (::wcslen(nt::HKCU_override) != 0) {
ASSERT_TRUE(
::SetEnvironmentVariableW(L"hkcu_override", nt::HKCU_override));
}
if (::wcslen(nt::HKLM_override) != 0) {
ASSERT_TRUE(
::SetEnvironmentVariableW(L"hklm_override", nt::HKLM_override));
}
base::string16 temp = nt::GetTestingOverride(nt::HKCU);
if (!temp.empty())
ASSERT_TRUE(::SetEnvironmentVariableW(L"hkcu_override", temp.c_str()));
temp = nt::GetTestingOverride(nt::HKLM);
if (!temp.empty())
ASSERT_TRUE(::SetEnvironmentVariableW(L"hklm_override", temp.c_str()));
}

void SetUp() override {
base::string16 temp;
override_manager_.OverrideRegistry(HKEY_CURRENT_USER, &temp);
::wcsncpy(nt::HKCU_override, temp.c_str(), nt::g_kRegMaxPathLen - 1);
ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp));

// Make the override path available to our test DLL.
IpcOverrides();
Expand Down Expand Up @@ -203,10 +201,9 @@ class BlacklistTest : public testing::Test {
TestDll_RemoveDllFromBlacklist(kTestDllName1);
TestDll_RemoveDllFromBlacklist(kTestDllName2);
TestDll_RemoveDllFromBlacklist(kTestDllName3);
}

// A scoped temporary directory to be destroyed with this test.
base::ScopedTempDir reg_override_dir_;
ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, base::string16()));
}
};

TEST_F(BlacklistTest, Beacon) {
Expand Down
14 changes: 6 additions & 8 deletions chrome_elf/blacklist/test/blacklist_test_main_dll.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ namespace {
void GetIpcOverrides() {
DWORD buffer_size = ::GetEnvironmentVariableW(L"hkcu_override", nullptr, 0);
if (buffer_size > 0) {
wchar_t* content = new wchar_t[buffer_size];
std::wstring content(buffer_size, L'\0');
buffer_size =
::GetEnvironmentVariableW(L"hkcu_override", content, buffer_size);
::GetEnvironmentVariableW(L"hkcu_override", &content[0], buffer_size);
if (buffer_size)
::wcsncpy(nt::HKCU_override, content, nt::g_kRegMaxPathLen - 1);
delete[] content;
nt::SetTestingOverride(nt::HKCU, content);
}

buffer_size = ::GetEnvironmentVariableW(L"hklm_override", nullptr, 0);
if (buffer_size > 0) {
wchar_t* content = new wchar_t[buffer_size];
std::wstring content(buffer_size, L'\0');
buffer_size =
::GetEnvironmentVariableW(L"hklm_override", content, buffer_size);
::GetEnvironmentVariableW(L"hklm_override", &content[0], buffer_size);
if (buffer_size)
::wcsncpy(nt::HKLM_override, content, nt::g_kRegMaxPathLen - 1);
delete[] content;
nt::SetTestingOverride(nt::HKLM, content);
}

return;
Expand Down
Loading

0 comments on commit 5379f17

Please sign in to comment.