blob: 5fc1fc5f8361893758d1c766637c01f02be6220e [file] [log] [blame]
[email protected]5a542072014-02-24 02:12:091// 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]5a542072014-02-24 02:12:095#include <tuple>
pennymac84fd6692016-07-13 22:35:346#include <windows.h>
7#include <versionhelpers.h> // windows.h must be before.
[email protected]5a542072014-02-24 02:12:098
9#include "base/test/test_reg_util_win.h"
10#include "base/win/registry.h"
grtb4cab812017-03-31 06:58:4311#include "chrome/install_static/install_util.h"
pennymac84fd6692016-07-13 22:35:3412#include "chrome_elf/chrome_elf_constants.h"
pennymac4e0b5f22016-07-19 19:15:4513#include "chrome_elf/chrome_elf_security.h"
pennymac84fd6692016-07-13 22:35:3414#include "chrome_elf/nt_registry/nt_registry.h"
[email protected]5a542072014-02-24 02:12:0915#include "testing/gtest/include/gtest/gtest.h"
ananta69086d72016-05-12 23:29:0416
[email protected]5a542072014-02-24 02:12:0917namespace {
18
pennymac4e0b5f22016-07-19 19:15:4519bool SetSecurityFinchFlag(bool creation) {
20 bool success = true;
grtb4cab812017-03-31 06:58:4321 const base::string16 finch_path(install_static::GetRegistryPath().append(
22 elf_sec::kRegSecurityFinchKeyName));
pennymac4e0b5f22016-07-19 19:15:4523 base::win::RegKey security_key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS);
24
25 if (creation) {
26 if (ERROR_SUCCESS !=
grtb4cab812017-03-31 06:58:4327 security_key.CreateKey(finch_path.c_str(), KEY_QUERY_VALUE))
pennymac4e0b5f22016-07-19 19:15:4528 success = false;
29 } else {
grtb4cab812017-03-31 06:58:4330 if (ERROR_SUCCESS != security_key.DeleteKey(finch_path.c_str()))
pennymac4e0b5f22016-07-19 19:15:4531 success = false;
32 }
33
34 security_key.Close();
35 return success;
36}
37
38bool 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
63void RegRedirect(nt::ROOT_KEY key,
pennymac5379f172016-10-04 20:43:5864 registry_util::RegistryOverrideManager* rom) {
65 ASSERT_NE(key, nt::AUTO);
pennymac4e0b5f22016-07-19 19:15:4566 base::string16 temp;
67
68 if (key == nt::HKCU) {
grtf6d7da22017-02-14 07:14:4769 ASSERT_NO_FATAL_FAILURE(rom->OverrideRegistry(HKEY_CURRENT_USER, &temp));
pennymac5379f172016-10-04 20:43:5870 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp));
71 } else {
grtf6d7da22017-02-14 07:14:4772 ASSERT_NO_FATAL_FAILURE(rom->OverrideRegistry(HKEY_LOCAL_MACHINE, &temp));
pennymac5379f172016-10-04 20:43:5873 ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp));
pennymac4e0b5f22016-07-19 19:15:4574 }
pennymac5379f172016-10-04 20:43:5875}
76
77void 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()));
pennymac4e0b5f22016-07-19 19:15:4583}
84
pennymac4e0b5f22016-07-19 19:15:4585TEST(ChromeElfUtilTest, BrowserProcessSecurityTest) {
86 if (!::IsWindows8OrGreater())
87 return;
88
89 // Set up registry override for this test.
90 registry_util::RegistryOverrideManager override_manager;
grtf6d7da22017-02-14 07:14:4791 ASSERT_NO_FATAL_FAILURE(RegRedirect(nt::HKCU, &override_manager));
pennymac4e0b5f22016-07-19 19:15:4592
93 // First, ensure that the emergency-off finch signal works.
94 EXPECT_TRUE(SetSecurityFinchFlag(true));
pennymac5446d892016-08-27 10:45:1295 elf_security::EarlyBrowserSecurity();
pennymac4e0b5f22016-07-19 19:15:4596 EXPECT_FALSE(IsSecuritySet());
97 EXPECT_TRUE(SetSecurityFinchFlag(false));
98
99 // Second, test that the process mitigation is set when no finch signal.
pennymac5446d892016-08-27 10:45:12100 elf_security::EarlyBrowserSecurity();
pennymac4e0b5f22016-07-19 19:15:45101 EXPECT_TRUE(IsSecuritySet());
pennymac4e0b5f22016-07-19 19:15:45102
grtf6d7da22017-02-14 07:14:47103 ASSERT_NO_FATAL_FAILURE(CancelRegRedirect(nt::HKCU));
pennymac84fd6692016-07-13 22:35:34104}
105
[email protected]5a542072014-02-24 02:12:09106} // namespace