[email protected] | 5a54207 | 2014-02-24 02:12:09 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 5a54207 | 2014-02-24 02:12:09 | [diff] [blame] | 5 | #include <tuple> |
pennymac | 84fd669 | 2016-07-13 22:35:34 | [diff] [blame] | 6 | #include <windows.h> |
| 7 | #include <versionhelpers.h> // windows.h must be before. |
[email protected] | 5a54207 | 2014-02-24 02:12:09 | [diff] [blame] | 8 | |
| 9 | #include "base/test/test_reg_util_win.h" |
| 10 | #include "base/win/registry.h" |
grt | b4cab81 | 2017-03-31 06:58:43 | [diff] [blame] | 11 | #include "chrome/install_static/install_util.h" |
pennymac | 84fd669 | 2016-07-13 22:35:34 | [diff] [blame] | 12 | #include "chrome_elf/chrome_elf_constants.h" |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 13 | #include "chrome_elf/chrome_elf_security.h" |
pennymac | 84fd669 | 2016-07-13 22:35:34 | [diff] [blame] | 14 | #include "chrome_elf/nt_registry/nt_registry.h" |
[email protected] | 5a54207 | 2014-02-24 02:12:09 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
ananta | 69086d7 | 2016-05-12 23:29:04 | [diff] [blame] | 16 | |
[email protected] | 5a54207 | 2014-02-24 02:12:09 | [diff] [blame] | 17 | namespace { |
| 18 | |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 19 | bool SetSecurityFinchFlag(bool creation) { |
| 20 | bool success = true; |
grt | b4cab81 | 2017-03-31 06:58:43 | [diff] [blame] | 21 | const base::string16 finch_path(install_static::GetRegistryPath().append( |
| 22 | elf_sec::kRegSecurityFinchKeyName)); |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 23 | base::win::RegKey security_key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); |
| 24 | |
| 25 | if (creation) { |
| 26 | if (ERROR_SUCCESS != |
grt | b4cab81 | 2017-03-31 06:58:43 | [diff] [blame] | 27 | security_key.CreateKey(finch_path.c_str(), KEY_QUERY_VALUE)) |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 28 | success = false; |
| 29 | } else { |
grt | b4cab81 | 2017-03-31 06:58:43 | [diff] [blame] | 30 | if (ERROR_SUCCESS != security_key.DeleteKey(finch_path.c_str())) |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 31 | success = false; |
| 32 | } |
| 33 | |
| 34 | security_key.Close(); |
| 35 | return success; |
| 36 | } |
| 37 | |
| 38 | bool IsSecuritySet() { |
| 39 | typedef decltype(GetProcessMitigationPolicy)* GetProcessMitigationPolicyFunc; |
| 40 | |
| 41 | // Check the settings from EarlyBrowserSecurity(). |
| 42 | if (::IsWindows8OrGreater()) { |
| 43 | GetProcessMitigationPolicyFunc get_process_mitigation_policy = |
| 44 | reinterpret_cast<GetProcessMitigationPolicyFunc>(::GetProcAddress( |
| 45 | ::GetModuleHandleW(L"kernel32.dll"), "GetProcessMitigationPolicy")); |
| 46 | if (!get_process_mitigation_policy) |
| 47 | return false; |
| 48 | |
| 49 | // Check that extension points are disabled. |
| 50 | // (Legacy hooking.) |
| 51 | PROCESS_MITIGATION_EXTENSION_POINT_DISABLE_POLICY policy = {}; |
| 52 | if (!get_process_mitigation_policy(::GetCurrentProcess(), |
| 53 | ProcessExtensionPointDisablePolicy, |
| 54 | &policy, sizeof(policy))) |
| 55 | return false; |
| 56 | |
| 57 | return policy.DisableExtensionPoints; |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | void RegRedirect(nt::ROOT_KEY key, |
pennymac | 5379f17 | 2016-10-04 20:43:58 | [diff] [blame] | 64 | registry_util::RegistryOverrideManager* rom) { |
| 65 | ASSERT_NE(key, nt::AUTO); |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 66 | base::string16 temp; |
| 67 | |
| 68 | if (key == nt::HKCU) { |
grt | f6d7da2 | 2017-02-14 07:14:47 | [diff] [blame] | 69 | ASSERT_NO_FATAL_FAILURE(rom->OverrideRegistry(HKEY_CURRENT_USER, &temp)); |
pennymac | 5379f17 | 2016-10-04 20:43:58 | [diff] [blame] | 70 | ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
| 71 | } else { |
grt | f6d7da2 | 2017-02-14 07:14:47 | [diff] [blame] | 72 | ASSERT_NO_FATAL_FAILURE(rom->OverrideRegistry(HKEY_LOCAL_MACHINE, &temp)); |
pennymac | 5379f17 | 2016-10-04 20:43:58 | [diff] [blame] | 73 | ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp)); |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 74 | } |
pennymac | 5379f17 | 2016-10-04 20:43:58 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void CancelRegRedirect(nt::ROOT_KEY key) { |
| 78 | ASSERT_NE(key, nt::AUTO); |
| 79 | if (key == nt::HKCU) |
| 80 | ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, base::string16())); |
| 81 | else |
| 82 | ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, base::string16())); |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 83 | } |
| 84 | |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 85 | TEST(ChromeElfUtilTest, BrowserProcessSecurityTest) { |
| 86 | if (!::IsWindows8OrGreater()) |
| 87 | return; |
| 88 | |
| 89 | // Set up registry override for this test. |
| 90 | registry_util::RegistryOverrideManager override_manager; |
grt | f6d7da2 | 2017-02-14 07:14:47 | [diff] [blame] | 91 | ASSERT_NO_FATAL_FAILURE(RegRedirect(nt::HKCU, &override_manager)); |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 92 | |
| 93 | // First, ensure that the emergency-off finch signal works. |
| 94 | EXPECT_TRUE(SetSecurityFinchFlag(true)); |
pennymac | 5446d89 | 2016-08-27 10:45:12 | [diff] [blame] | 95 | elf_security::EarlyBrowserSecurity(); |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 96 | EXPECT_FALSE(IsSecuritySet()); |
| 97 | EXPECT_TRUE(SetSecurityFinchFlag(false)); |
| 98 | |
| 99 | // Second, test that the process mitigation is set when no finch signal. |
pennymac | 5446d89 | 2016-08-27 10:45:12 | [diff] [blame] | 100 | elf_security::EarlyBrowserSecurity(); |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 101 | EXPECT_TRUE(IsSecuritySet()); |
pennymac | 4e0b5f2 | 2016-07-19 19:15:45 | [diff] [blame] | 102 | |
grt | f6d7da2 | 2017-02-14 07:14:47 | [diff] [blame] | 103 | ASSERT_NO_FATAL_FAILURE(CancelRegRedirect(nt::HKCU)); |
pennymac | 84fd669 | 2016-07-13 22:35:34 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 5a54207 | 2014-02-24 02:12:09 | [diff] [blame] | 106 | } // namespace |