[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [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 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 5 | #include <memory> |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 6 | #include <string> |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 7 | #include <utility> |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 8 | |
blundell | 6e85b7c | 2015-09-29 12:33:35 | [diff] [blame] | 9 | #include "base/base_switches.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 13 | #include "base/json/json_file_value_serializer.h" |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 14 | #include "base/json/json_reader.h" |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 16 | #include "base/metrics/histogram_base.h" |
| 17 | #include "base/metrics/histogram_samples.h" |
| 18 | #include "base/metrics/statistics_recorder.h" |
| 19 | #include "base/path_service.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
| 21 | #include "base/strings/string_util.h" |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 22 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 23 | #include "base/values.h" |
| 24 | #include "build/build_config.h" |
| 25 | #include "chrome/browser/extensions/extension_browsertest.h" |
| 26 | #include "chrome/browser/extensions/extension_service.h" |
| 27 | #include "chrome/browser/prefs/chrome_pref_service_factory.h" |
| 28 | #include "chrome/browser/prefs/profile_pref_store_manager.h" |
| 29 | #include "chrome/browser/prefs/session_startup_pref.h" |
| 30 | #include "chrome/browser/profiles/profile.h" |
| 31 | #include "chrome/browser/ui/browser.h" |
| 32 | #include "chrome/common/chrome_constants.h" |
| 33 | #include "chrome/common/chrome_paths.h" |
| 34 | #include "chrome/common/pref_names.h" |
| 35 | #include "chrome/test/base/testing_profile.h" |
| 36 | #include "components/search_engines/default_search_manager.h" |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 37 | #include "components/search_engines/template_url_data.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 38 | #include "extensions/browser/pref_names.h" |
| 39 | #include "extensions/common/extension.h" |
sammc | fb473a87 | 2017-04-03 01:56:59 | [diff] [blame] | 40 | #include "services/preferences/public/cpp/tracked/tracked_preference_histogram_names.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 41 | |
| 42 | #if defined(OS_CHROMEOS) |
| 43 | #include "chromeos/chromeos_switches.h" |
| 44 | #endif |
| 45 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 46 | #if defined(OS_WIN) |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 47 | #include "base/win/registry.h" |
| 48 | #include "chrome/install_static/install_util.h" |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 49 | #endif |
| 50 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 51 | namespace { |
| 52 | |
| 53 | // Extension ID of chrome/test/data/extensions/good.crx |
| 54 | const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; |
| 55 | |
| 56 | // Explicit expectations from the caller of GetTrackedPrefHistogramCount(). This |
| 57 | // enables detailed reporting of the culprit on failure. |
| 58 | enum AllowedBuckets { |
| 59 | // Allow no samples in any buckets. |
| 60 | ALLOW_NONE = -1, |
| 61 | // Any integer between BEGIN_ALLOW_SINGLE_BUCKET and END_ALLOW_SINGLE_BUCKET |
| 62 | // indicates that only this specific bucket is allowed to have a sample. |
| 63 | BEGIN_ALLOW_SINGLE_BUCKET = 0, |
| 64 | END_ALLOW_SINGLE_BUCKET = 100, |
| 65 | // Allow any buckets (no extra verifications performed). |
| 66 | ALLOW_ANY |
| 67 | }; |
| 68 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 69 | #if defined(OS_WIN) |
| 70 | base::string16 GetRegistryPathForTestProfile() { |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 71 | // Cleanup follow-up to http://crbug.com/721245 for the previous location of |
| 72 | // this test key which had similar problems (to a lesser extent). It's |
| 73 | // redundant but harmless to have multiple callers hit this on the same |
| 74 | // machine. TODO(gab): remove this mid-june 2017. |
| 75 | base::win::RegKey key; |
| 76 | if (key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\Chromium\\PrefHashBrowserTest", |
| 77 | KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 78 | LONG result = key.DeleteKey(L""); |
| 79 | EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 80 | } |
| 81 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 82 | base::FilePath profile_dir; |
| 83 | EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir)); |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 84 | |
| 85 | // Use a location under the real PreferenceMACs path so that the backup |
| 86 | // cleanup logic in ChromeTestLauncherDelegate::PreSharding() for interrupted |
| 87 | // tests covers this test key as well. |
| 88 | return install_static::GetRegistryPath() + |
| 89 | L"\\PreferenceMACs\\PrefHashBrowserTest\\" + |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 90 | profile_dir.BaseName().value(); |
| 91 | } |
| 92 | #endif |
| 93 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 94 | // Returns the number of times |histogram_name| was reported so far; adding the |
| 95 | // results of the first 100 buckets (there are only ~19 reporting IDs as of this |
| 96 | // writing; varies depending on the platform). |allowed_buckets| hints at extra |
| 97 | // requirements verified in this method (see AllowedBuckets for details). |
| 98 | int GetTrackedPrefHistogramCount(const char* histogram_name, |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 99 | const char* histogram_suffix, |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 100 | int allowed_buckets) { |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 101 | std::string full_histogram_name(histogram_name); |
| 102 | if (*histogram_suffix) |
| 103 | full_histogram_name.append(".").append(histogram_suffix); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 104 | const base::HistogramBase* histogram = |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 105 | base::StatisticsRecorder::FindHistogram(full_histogram_name); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 106 | if (!histogram) |
| 107 | return 0; |
| 108 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 109 | std::unique_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 110 | int sum = 0; |
| 111 | for (int i = 0; i < 100; ++i) { |
| 112 | int count_for_id = samples->GetCount(i); |
| 113 | EXPECT_GE(count_for_id, 0); |
| 114 | sum += count_for_id; |
| 115 | |
| 116 | if (allowed_buckets == ALLOW_NONE || |
| 117 | (allowed_buckets != ALLOW_ANY && i != allowed_buckets)) { |
| 118 | EXPECT_EQ(0, count_for_id) << "Unexpected reporting_id: " << i; |
| 119 | } |
| 120 | } |
| 121 | return sum; |
| 122 | } |
| 123 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 124 | // Helper function to call GetTrackedPrefHistogramCount with no external |
| 125 | // validation suffix. |
| 126 | int GetTrackedPrefHistogramCount(const char* histogram_name, |
| 127 | int allowed_buckets) { |
| 128 | return GetTrackedPrefHistogramCount(histogram_name, "", allowed_buckets); |
| 129 | } |
| 130 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 131 | std::unique_ptr<base::DictionaryValue> ReadPrefsDictionary( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 132 | const base::FilePath& pref_file) { |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 133 | JSONFileValueDeserializer deserializer(pref_file); |
| 134 | int error_code = JSONFileValueDeserializer::JSON_NO_ERROR; |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 135 | std::string error_str; |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 136 | std::unique_ptr<base::Value> prefs = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 137 | deserializer.Deserialize(&error_code, &error_str); |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 138 | if (!prefs || error_code != JSONFileValueDeserializer::JSON_NO_ERROR) { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 139 | ADD_FAILURE() << "Error #" << error_code << ": " << error_str; |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 140 | return std::unique_ptr<base::DictionaryValue>(); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 141 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 142 | if (!prefs->IsType(base::Value::Type::DICTIONARY)) { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 143 | ADD_FAILURE(); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 144 | return std::unique_ptr<base::DictionaryValue>(); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 145 | } |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 146 | return std::unique_ptr<base::DictionaryValue>( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 147 | static_cast<base::DictionaryValue*>(prefs.release())); |
| 148 | } |
| 149 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 150 | // Returns whether external validation is supported on the platform through |
| 151 | // storing MACs in the registry. |
| 152 | bool SupportsRegistryValidation() { |
| 153 | #if defined(OS_WIN) |
| 154 | return true; |
| 155 | #else |
| 156 | return false; |
| 157 | #endif |
| 158 | } |
| 159 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 160 | #define PREF_HASH_BROWSER_TEST(fixture, test_name) \ |
| 161 | IN_PROC_BROWSER_TEST_P(fixture, PRE_##test_name) { \ |
| 162 | SetupPreferences(); \ |
| 163 | } \ |
| 164 | IN_PROC_BROWSER_TEST_P(fixture, test_name) { \ |
| 165 | VerifyReactionToPrefAttack(); \ |
| 166 | } \ |
| 167 | INSTANTIATE_TEST_CASE_P( \ |
| 168 | fixture##Instance, \ |
| 169 | fixture, \ |
| 170 | testing::Values( \ |
| 171 | chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement, \ |
| 172 | chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways, \ |
| 173 | chrome_prefs::internals:: \ |
| 174 | kSettingsEnforcementGroupEnforceAlwaysWithDSE, \ |
| 175 | chrome_prefs::internals:: \ |
| 176 | kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE)); |
| 177 | |
| 178 | // A base fixture designed such that implementations do two things: |
| 179 | // 1) Override all three pure-virtual methods below to setup, attack, and |
| 180 | // verify preferenes throughout the tests provided by this fixture. |
| 181 | // 2) Instantiate their test via the PREF_HASH_BROWSER_TEST macro above. |
| 182 | // Based on top of ExtensionBrowserTest to allow easy interaction with the |
| 183 | // ExtensionService. |
| 184 | class PrefHashBrowserTestBase |
| 185 | : public ExtensionBrowserTest, |
| 186 | public testing::WithParamInterface<std::string> { |
| 187 | public: |
| 188 | // List of potential protection levels for this test in strict increasing |
| 189 | // order of protection levels. |
| 190 | enum SettingsProtectionLevel { |
| 191 | PROTECTION_DISABLED_ON_PLATFORM, |
| 192 | PROTECTION_DISABLED_FOR_GROUP, |
| 193 | PROTECTION_ENABLED_BASIC, |
| 194 | PROTECTION_ENABLED_DSE, |
| 195 | PROTECTION_ENABLED_EXTENSIONS, |
| 196 | // Represents the strongest level (i.e. always equivalent to the last one in |
| 197 | // terms of protection), leave this one last when adding new levels. |
| 198 | PROTECTION_ENABLED_ALL |
| 199 | }; |
| 200 | |
| 201 | PrefHashBrowserTestBase() |
| 202 | : protection_level_(GetProtectionLevelFromTrialGroup(GetParam())) { |
| 203 | } |
| 204 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 205 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 206 | ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 207 | EXPECT_FALSE(command_line->HasSwitch(switches::kForceFieldTrials)); |
| 208 | command_line->AppendSwitchASCII( |
| 209 | switches::kForceFieldTrials, |
| 210 | std::string(chrome_prefs::internals::kSettingsEnforcementTrialName) + |
| 211 | "/" + GetParam() + "/"); |
| 212 | #if defined(OS_CHROMEOS) |
| 213 | command_line->AppendSwitch( |
| 214 | chromeos::switches::kIgnoreUserProfileMappingForTests); |
| 215 | #endif |
| 216 | } |
| 217 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 218 | bool SetUpUserDataDirectory() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 219 | // Do the normal setup in the PRE test and attack preferences in the main |
| 220 | // test. |
| 221 | if (IsPRETest()) |
| 222 | return ExtensionBrowserTest::SetUpUserDataDirectory(); |
| 223 | |
| 224 | #if defined(OS_CHROMEOS) |
| 225 | // For some reason, the Preferences file does not exist in the location |
| 226 | // below on Chrome OS. Since protection is disabled on Chrome OS, it's okay |
| 227 | // to simply not attack preferences at all (and still assert that no |
| 228 | // hardening related histogram kicked in in VerifyReactionToPrefAttack()). |
| 229 | // TODO(gab): Figure out why there is no Preferences file in this location |
| 230 | // on Chrome OS (and re-enable the section disabled for OS_CHROMEOS further |
| 231 | // below). |
| 232 | EXPECT_EQ(PROTECTION_DISABLED_ON_PLATFORM, protection_level_); |
| 233 | return true; |
| 234 | #endif |
| 235 | |
| 236 | base::FilePath profile_dir; |
| 237 | EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir)); |
| 238 | profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir); |
| 239 | |
| 240 | // Sanity check that old protected pref file is never present in modern |
| 241 | // Chromes. |
| 242 | EXPECT_FALSE(base::PathExists( |
sammc | f420c16e | 2017-03-16 02:11:13 | [diff] [blame] | 243 | profile_dir.Append(FILE_PATH_LITERAL("Protected Preferences")))); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 244 | |
| 245 | // Read the preferences from disk. |
| 246 | |
| 247 | const base::FilePath unprotected_pref_file = |
| 248 | profile_dir.Append(chrome::kPreferencesFilename); |
| 249 | EXPECT_TRUE(base::PathExists(unprotected_pref_file)); |
| 250 | |
| 251 | const base::FilePath protected_pref_file = |
| 252 | profile_dir.Append(chrome::kSecurePreferencesFilename); |
| 253 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, |
| 254 | base::PathExists(protected_pref_file)); |
| 255 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 256 | std::unique_ptr<base::DictionaryValue> unprotected_preferences( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 257 | ReadPrefsDictionary(unprotected_pref_file)); |
| 258 | if (!unprotected_preferences) |
| 259 | return false; |
| 260 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 261 | std::unique_ptr<base::DictionaryValue> protected_preferences; |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 262 | if (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM) { |
| 263 | protected_preferences = ReadPrefsDictionary(protected_pref_file); |
| 264 | if (!protected_preferences) |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | // Let the underlying test modify the preferences. |
| 269 | AttackPreferencesOnDisk(unprotected_preferences.get(), |
| 270 | protected_preferences.get()); |
| 271 | |
| 272 | // Write the modified preferences back to disk. |
| 273 | |
| 274 | JSONFileValueSerializer unprotected_prefs_serializer(unprotected_pref_file); |
| 275 | EXPECT_TRUE( |
| 276 | unprotected_prefs_serializer.Serialize(*unprotected_preferences)); |
| 277 | |
| 278 | if (protected_preferences) { |
| 279 | JSONFileValueSerializer protected_prefs_serializer(protected_pref_file); |
| 280 | EXPECT_TRUE(protected_prefs_serializer.Serialize(*protected_preferences)); |
| 281 | } |
| 282 | |
| 283 | return true; |
| 284 | } |
| 285 | |
gab | bcdefd0cc | 2015-02-17 18:12:40 | [diff] [blame] | 286 | void SetUpInProcessBrowserTestFixture() override { |
| 287 | ExtensionBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 288 | |
| 289 | // Bots are on a domain, turn off the domain check for settings hardening in |
| 290 | // order to be able to test all SettingsEnforcement groups. |
| 291 | chrome_prefs::DisableDomainCheckForTesting(); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 292 | |
| 293 | #if defined(OS_WIN) |
| 294 | // Avoid polluting prefs for the user and the bots by writing to a specific |
| 295 | // testing registry path. |
| 296 | registry_key_for_external_validation_ = GetRegistryPathForTestProfile(); |
| 297 | ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( |
| 298 | ®istry_key_for_external_validation_); |
| 299 | |
| 300 | // Keys should be unique, but to avoid flakes in the long run make sure an |
| 301 | // identical test key wasn't left behind by a previous test. |
| 302 | if (IsPRETest()) { |
| 303 | base::win::RegKey key; |
| 304 | if (key.Open(HKEY_CURRENT_USER, |
| 305 | registry_key_for_external_validation_.c_str(), |
| 306 | KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 307 | LONG result = key.DeleteKey(L""); |
| 308 | ASSERT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 309 | } |
| 310 | } |
| 311 | #endif |
| 312 | } |
| 313 | |
| 314 | void TearDown() override { |
| 315 | #if defined(OS_WIN) |
| 316 | // When done, delete the Registry key to avoid polluting the registry. |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 317 | if (!IsPRETest()) { |
| 318 | base::string16 registry_key = GetRegistryPathForTestProfile(); |
| 319 | base::win::RegKey key; |
| 320 | if (key.Open(HKEY_CURRENT_USER, registry_key.c_str(), |
| 321 | KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 322 | LONG result = key.DeleteKey(L""); |
| 323 | ASSERT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 324 | } |
| 325 | } |
| 326 | #endif |
| 327 | ExtensionBrowserTest::TearDown(); |
gab | bcdefd0cc | 2015-02-17 18:12:40 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 330 | // In the PRE_ test, find the number of tracked preferences that were |
| 331 | // initialized and save it to a file to be read back in the main test and used |
| 332 | // as the total number of tracked preferences. |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 333 | void SetUpOnMainThread() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 334 | ExtensionBrowserTest::SetUpOnMainThread(); |
| 335 | |
| 336 | // File in which the PRE_ test will save the number of tracked preferences |
| 337 | // on this platform. |
| 338 | const char kNumTrackedPrefFilename[] = "NumTrackedPrefs"; |
| 339 | |
| 340 | base::FilePath num_tracked_prefs_file; |
| 341 | ASSERT_TRUE( |
| 342 | PathService::Get(chrome::DIR_USER_DATA, &num_tracked_prefs_file)); |
| 343 | num_tracked_prefs_file = |
| 344 | num_tracked_prefs_file.AppendASCII(kNumTrackedPrefFilename); |
| 345 | |
| 346 | if (IsPRETest()) { |
| 347 | num_tracked_prefs_ = GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 348 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 349 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, |
| 350 | num_tracked_prefs_ > 0); |
| 351 | |
gab | 342aa618 | 2014-10-02 21:59:00 | [diff] [blame] | 352 | // Split tracked prefs are reported as Unchanged not as NullInitialized |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 353 | // when an empty dictionary is encountered on first run (this should only |
| 354 | // hit for pref #5 in the current design). |
| 355 | int num_split_tracked_prefs = GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 356 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 357 | BEGIN_ALLOW_SINGLE_BUCKET + 5); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 358 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 359 | num_split_tracked_prefs); |
| 360 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 361 | if (SupportsRegistryValidation()) { |
| 362 | // Same checks as above, but for the registry. |
| 363 | num_tracked_prefs_ = GetTrackedPrefHistogramCount( |
| 364 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 365 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 366 | ALLOW_ANY); |
| 367 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, |
| 368 | num_tracked_prefs_ > 0); |
| 369 | |
| 370 | int num_split_tracked_prefs = GetTrackedPrefHistogramCount( |
| 371 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 372 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 373 | BEGIN_ALLOW_SINGLE_BUCKET + 5); |
| 374 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 375 | num_split_tracked_prefs); |
| 376 | } |
| 377 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 378 | num_tracked_prefs_ += num_split_tracked_prefs; |
| 379 | |
| 380 | std::string num_tracked_prefs_str = base::IntToString(num_tracked_prefs_); |
| 381 | EXPECT_EQ(static_cast<int>(num_tracked_prefs_str.size()), |
| 382 | base::WriteFile(num_tracked_prefs_file, |
| 383 | num_tracked_prefs_str.c_str(), |
| 384 | num_tracked_prefs_str.size())); |
| 385 | } else { |
| 386 | std::string num_tracked_prefs_str; |
| 387 | EXPECT_TRUE(base::ReadFileToString(num_tracked_prefs_file, |
| 388 | &num_tracked_prefs_str)); |
| 389 | EXPECT_TRUE( |
| 390 | base::StringToInt(num_tracked_prefs_str, &num_tracked_prefs_)); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | protected: |
| 395 | // Called from the PRE_ test's body. Overrides should use it to setup |
| 396 | // preferences through Chrome. |
| 397 | virtual void SetupPreferences() = 0; |
| 398 | |
| 399 | // Called prior to the main test launching its browser. Overrides should use |
| 400 | // it to attack preferences. |(un)protected_preferences| represent the state |
| 401 | // on disk prior to launching the main test, they can be modified by this |
| 402 | // method and modifications will be flushed back to disk before launching the |
| 403 | // main test. |unprotected_preferences| is never NULL, |protected_preferences| |
| 404 | // may be NULL if in PROTECTION_DISABLED_ON_PLATFORM mode. |
| 405 | virtual void AttackPreferencesOnDisk( |
| 406 | base::DictionaryValue* unprotected_preferences, |
| 407 | base::DictionaryValue* protected_preferences) = 0; |
| 408 | |
| 409 | // Called from the body of the main test. Overrides should use it to verify |
| 410 | // that the browser had the desired reaction when faced when the attack |
| 411 | // orchestrated in AttackPreferencesOnDisk(). |
| 412 | virtual void VerifyReactionToPrefAttack() = 0; |
| 413 | |
| 414 | int num_tracked_prefs() const { return num_tracked_prefs_; } |
| 415 | |
| 416 | const SettingsProtectionLevel protection_level_; |
| 417 | |
| 418 | private: |
| 419 | // Returns true if this is the PRE_ phase of the test. |
| 420 | bool IsPRETest() { |
brettw | 66d1b81b | 2015-07-06 19:29:40 | [diff] [blame] | 421 | return base::StartsWith( |
brettw | 44ce0ec5 | 2015-06-12 01:57:57 | [diff] [blame] | 422 | testing::UnitTest::GetInstance()->current_test_info()->name(), "PRE_", |
brettw | 66d1b81b | 2015-07-06 19:29:40 | [diff] [blame] | 423 | base::CompareCase::SENSITIVE); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | SettingsProtectionLevel GetProtectionLevelFromTrialGroup( |
| 427 | const std::string& trial_group) { |
| 428 | if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking) |
| 429 | return PROTECTION_DISABLED_ON_PLATFORM; |
| 430 | |
| 431 | // Protection levels can't be adjusted via --force-fieldtrials in official |
| 432 | // builds. |
| 433 | #if defined(OFFICIAL_BUILD) |
| 434 | |
proberge | 2562e6f | 2017-04-10 17:46:57 | [diff] [blame] | 435 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 436 | // The strongest mode is enforced on Windows and MacOS in the absence of a |
| 437 | // field trial. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 438 | return PROTECTION_ENABLED_ALL; |
| 439 | #else |
| 440 | return PROTECTION_DISABLED_FOR_GROUP; |
| 441 | #endif |
| 442 | |
| 443 | #else // defined(OFFICIAL_BUILD) |
| 444 | |
| 445 | using namespace chrome_prefs::internals; |
| 446 | if (trial_group == kSettingsEnforcementGroupNoEnforcement) { |
| 447 | return PROTECTION_DISABLED_FOR_GROUP; |
| 448 | } else if (trial_group == kSettingsEnforcementGroupEnforceAlways) { |
| 449 | return PROTECTION_ENABLED_BASIC; |
| 450 | } else if (trial_group == kSettingsEnforcementGroupEnforceAlwaysWithDSE) { |
| 451 | return PROTECTION_ENABLED_DSE; |
| 452 | } else if (trial_group == |
| 453 | kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE) { |
| 454 | return PROTECTION_ENABLED_EXTENSIONS; |
| 455 | } else { |
| 456 | ADD_FAILURE(); |
| 457 | return static_cast<SettingsProtectionLevel>(-1); |
| 458 | } |
| 459 | |
| 460 | #endif // defined(OFFICIAL_BUILD) |
| 461 | |
| 462 | } |
| 463 | |
| 464 | int num_tracked_prefs_; |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 465 | |
| 466 | #if defined(OS_WIN) |
| 467 | base::string16 registry_key_for_external_validation_; |
| 468 | #endif |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 469 | }; |
| 470 | |
| 471 | } // namespace |
| 472 | |
| 473 | // Verifies that nothing is reset when nothing is tampered with. |
| 474 | // Also sanity checks that the expected preferences files are in place. |
| 475 | class PrefHashBrowserTestUnchangedDefault : public PrefHashBrowserTestBase { |
| 476 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 477 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 478 | // Default Chrome setup. |
| 479 | } |
| 480 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 481 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 482 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 483 | base::DictionaryValue* protected_preferences) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 484 | // No attack. |
| 485 | } |
| 486 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 487 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 488 | // Expect all prefs to be reported as Unchanged with no resets. |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 489 | EXPECT_EQ( |
| 490 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 491 | ? num_tracked_prefs() |
| 492 | : 0, |
| 493 | GetTrackedPrefHistogramCount( |
| 494 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 495 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 496 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 497 | ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 498 | EXPECT_EQ(0, |
| 499 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 500 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 501 | |
| 502 | // Nothing else should have triggered. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 503 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 504 | 0, GetTrackedPrefHistogramCount( |
| 505 | user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE)); |
| 506 | EXPECT_EQ( |
| 507 | 0, GetTrackedPrefHistogramCount( |
| 508 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 509 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 510 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 511 | ALLOW_NONE)); |
| 512 | EXPECT_EQ(0, |
| 513 | GetTrackedPrefHistogramCount( |
| 514 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 515 | ALLOW_NONE)); |
| 516 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 517 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 518 | ALLOW_NONE)); |
| 519 | EXPECT_EQ( |
| 520 | 0, GetTrackedPrefHistogramCount( |
| 521 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 522 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 523 | |
| 524 | if (SupportsRegistryValidation()) { |
| 525 | // Expect all prefs to be reported as Unchanged. |
| 526 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 527 | ? num_tracked_prefs() |
| 528 | : 0, |
| 529 | GetTrackedPrefHistogramCount( |
| 530 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 531 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 532 | ALLOW_ANY)); |
| 533 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 534 | } |
| 535 | }; |
| 536 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 537 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedDefault, UnchangedDefault); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 538 | |
| 539 | // Augments PrefHashBrowserTestUnchangedDefault to confirm that nothing is reset |
| 540 | // when nothing is tampered with, even if Chrome itself wrote custom prefs in |
| 541 | // its last run. |
| 542 | class PrefHashBrowserTestUnchangedCustom |
| 543 | : public PrefHashBrowserTestUnchangedDefault { |
| 544 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 545 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 546 | profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); |
| 547 | |
| 548 | InstallExtensionWithUIAutoConfirm( |
| 549 | test_data_dir_.AppendASCII("good.crx"), 1, browser()); |
| 550 | } |
| 551 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 552 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 553 | // Make sure the settings written in the last run stuck. |
| 554 | EXPECT_EQ("http://example.com", |
| 555 | profile()->GetPrefs()->GetString(prefs::kHomePage)); |
| 556 | |
| 557 | EXPECT_TRUE(extension_service()->GetExtensionById(kGoodCrxId, false)); |
| 558 | |
| 559 | // Reaction should be identical to unattacked default prefs. |
| 560 | PrefHashBrowserTestUnchangedDefault::VerifyReactionToPrefAttack(); |
| 561 | } |
| 562 | }; |
| 563 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 564 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedCustom, UnchangedCustom); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 565 | |
| 566 | // Verifies that cleared prefs are reported. |
| 567 | class PrefHashBrowserTestClearedAtomic : public PrefHashBrowserTestBase { |
| 568 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 569 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 570 | profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); |
| 571 | } |
| 572 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 573 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 574 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 575 | base::DictionaryValue* protected_preferences) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 576 | base::DictionaryValue* selected_prefs = |
| 577 | protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences |
| 578 | : unprotected_preferences; |
| 579 | // |selected_prefs| should never be NULL under the protection level picking |
| 580 | // it. |
| 581 | EXPECT_TRUE(selected_prefs); |
| 582 | EXPECT_TRUE(selected_prefs->Remove(prefs::kHomePage, NULL)); |
| 583 | } |
| 584 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 585 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 586 | // The clearance of homepage should have been noticed (as pref #2 being |
| 587 | // cleared), but shouldn't have triggered a reset (as there is nothing we |
| 588 | // can do when the pref is already gone). |
| 589 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 590 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 591 | user_prefs::tracked::kTrackedPrefHistogramCleared, |
| 592 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 593 | EXPECT_EQ( |
| 594 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 595 | ? num_tracked_prefs() - 1 |
| 596 | : 0, |
| 597 | GetTrackedPrefHistogramCount( |
| 598 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 599 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 600 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 601 | ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 602 | EXPECT_EQ(0, |
| 603 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 604 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 605 | |
| 606 | // Nothing else should have triggered. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 607 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 608 | 0, GetTrackedPrefHistogramCount( |
| 609 | user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE)); |
| 610 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 611 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 612 | ALLOW_NONE)); |
| 613 | EXPECT_EQ(0, |
| 614 | GetTrackedPrefHistogramCount( |
| 615 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 616 | ALLOW_NONE)); |
| 617 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 618 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 619 | ALLOW_NONE)); |
| 620 | EXPECT_EQ( |
| 621 | 0, GetTrackedPrefHistogramCount( |
| 622 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 623 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 624 | |
| 625 | if (SupportsRegistryValidation()) { |
| 626 | // Expect homepage clearance to have been noticed by registry validation. |
| 627 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 628 | GetTrackedPrefHistogramCount( |
| 629 | user_prefs::tracked::kTrackedPrefHistogramCleared, |
| 630 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 631 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 632 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 633 | } |
| 634 | }; |
| 635 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 636 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestClearedAtomic, ClearedAtomic); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 637 | |
| 638 | // Verifies that clearing the MACs results in untrusted Initialized pings for |
| 639 | // non-null protected prefs. |
| 640 | class PrefHashBrowserTestUntrustedInitialized : public PrefHashBrowserTestBase { |
| 641 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 642 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 643 | // Explicitly set the DSE (it's otherwise NULL by default, preventing |
| 644 | // thorough testing of the PROTECTION_ENABLED_DSE level). |
| 645 | DefaultSearchManager default_search_manager( |
| 646 | profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); |
| 647 | DefaultSearchManager::Source dse_source = |
| 648 | static_cast<DefaultSearchManager::Source>(-1); |
| 649 | |
| 650 | const TemplateURLData* default_template_url_data = |
| 651 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 652 | EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, dse_source); |
| 653 | |
| 654 | default_search_manager.SetUserSelectedDefaultSearchEngine( |
| 655 | *default_template_url_data); |
| 656 | |
| 657 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 658 | EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source); |
| 659 | |
| 660 | // Also explicitly set an atomic pref that falls under |
| 661 | // PROTECTION_ENABLED_BASIC. |
| 662 | profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup, |
| 663 | SessionStartupPref::URLS); |
| 664 | } |
| 665 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 666 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 667 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 668 | base::DictionaryValue* protected_preferences) override { |
proberge | 574d7d9 | 2016-08-01 20:14:11 | [diff] [blame] | 669 | unprotected_preferences->Remove("protection.macs", NULL); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 670 | if (protected_preferences) |
proberge | 574d7d9 | 2016-08-01 20:14:11 | [diff] [blame] | 671 | protected_preferences->Remove("protection.macs", NULL); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 672 | } |
| 673 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 674 | void VerifyReactionToPrefAttack() override { |
gab | 342aa618 | 2014-10-02 21:59:00 | [diff] [blame] | 675 | // Preferences that are NULL by default will be NullInitialized. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 676 | int num_null_values = GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 677 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 678 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, |
| 679 | num_null_values > 0); |
| 680 | if (num_null_values > 0) { |
| 681 | // This test requires that at least 3 prefs be non-null (extensions, DSE, |
| 682 | // and 1 atomic pref explictly set for this test above). |
gab | 342aa618 | 2014-10-02 21:59:00 | [diff] [blame] | 683 | EXPECT_GE(num_tracked_prefs() - num_null_values, 3); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // Expect all non-null prefs to be reported as Initialized (with |
| 687 | // accompanying resets or wanted resets based on the current protection |
| 688 | // level). |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 689 | EXPECT_EQ( |
| 690 | num_tracked_prefs() - num_null_values, |
| 691 | GetTrackedPrefHistogramCount( |
| 692 | user_prefs::tracked::kTrackedPrefHistogramInitialized, ALLOW_ANY)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 693 | |
| 694 | int num_protected_prefs = 0; |
| 695 | // A switch statement falling through each protection level in decreasing |
| 696 | // levels of protection to add expectations for each level which augments |
| 697 | // the previous one. |
| 698 | switch (protection_level_) { |
| 699 | case PROTECTION_ENABLED_ALL: |
| 700 | // Falls through. |
| 701 | case PROTECTION_ENABLED_EXTENSIONS: |
| 702 | ++num_protected_prefs; |
| 703 | // Falls through. |
| 704 | case PROTECTION_ENABLED_DSE: |
| 705 | ++num_protected_prefs; |
| 706 | // Falls through. |
| 707 | case PROTECTION_ENABLED_BASIC: |
| 708 | num_protected_prefs += num_tracked_prefs() - num_null_values - 2; |
| 709 | // Falls through. |
| 710 | case PROTECTION_DISABLED_FOR_GROUP: |
| 711 | // No protection. Falls through. |
| 712 | case PROTECTION_DISABLED_ON_PLATFORM: |
| 713 | // No protection. |
| 714 | break; |
| 715 | } |
| 716 | |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 717 | EXPECT_EQ( |
| 718 | num_tracked_prefs() - num_null_values - num_protected_prefs, |
| 719 | GetTrackedPrefHistogramCount( |
| 720 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, ALLOW_ANY)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 721 | EXPECT_EQ(num_protected_prefs, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 722 | GetTrackedPrefHistogramCount( |
| 723 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_ANY)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 724 | |
| 725 | // Explicitly verify the result of reported resets. |
| 726 | |
| 727 | DefaultSearchManager default_search_manager( |
| 728 | profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); |
| 729 | DefaultSearchManager::Source dse_source = |
| 730 | static_cast<DefaultSearchManager::Source>(-1); |
| 731 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 732 | EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_DSE |
| 733 | ? DefaultSearchManager::FROM_USER |
| 734 | : DefaultSearchManager::FROM_FALLBACK, |
| 735 | dse_source); |
| 736 | |
| 737 | EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_BASIC, |
| 738 | profile()->GetPrefs()->GetInteger(prefs::kRestoreOnStartup) == |
| 739 | SessionStartupPref::URLS); |
| 740 | |
| 741 | // Nothing else should have triggered. |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 742 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 743 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 744 | ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 745 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 746 | 0, GetTrackedPrefHistogramCount( |
| 747 | user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE)); |
| 748 | EXPECT_EQ( |
| 749 | 0, GetTrackedPrefHistogramCount( |
| 750 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 751 | EXPECT_EQ( |
| 752 | 0, GetTrackedPrefHistogramCount( |
| 753 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 754 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 755 | |
| 756 | if (SupportsRegistryValidation()) { |
| 757 | // The MACs have been cleared but the preferences have not been tampered. |
| 758 | // The registry should report all prefs as unchanged. |
| 759 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 760 | ? num_tracked_prefs() |
| 761 | : 0, |
| 762 | GetTrackedPrefHistogramCount( |
| 763 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 764 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 765 | ALLOW_ANY)); |
| 766 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 767 | } |
| 768 | }; |
| 769 | |
| 770 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedInitialized, |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 771 | UntrustedInitialized); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 772 | |
| 773 | // Verifies that changing an atomic pref results in it being reported (and reset |
| 774 | // if the protection level allows it). |
| 775 | class PrefHashBrowserTestChangedAtomic : public PrefHashBrowserTestBase { |
| 776 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 777 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 778 | profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup, |
| 779 | SessionStartupPref::URLS); |
| 780 | |
| 781 | ListPrefUpdate update(profile()->GetPrefs(), |
| 782 | prefs::kURLsToRestoreOnStartup); |
| 783 | update->AppendString("http://example.com"); |
| 784 | } |
| 785 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 786 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 787 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 788 | base::DictionaryValue* protected_preferences) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 789 | base::DictionaryValue* selected_prefs = |
| 790 | protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences |
| 791 | : unprotected_preferences; |
| 792 | // |selected_prefs| should never be NULL under the protection level picking |
| 793 | // it. |
| 794 | EXPECT_TRUE(selected_prefs); |
| 795 | base::ListValue* startup_urls; |
| 796 | EXPECT_TRUE( |
| 797 | selected_prefs->GetList(prefs::kURLsToRestoreOnStartup, &startup_urls)); |
| 798 | EXPECT_TRUE(startup_urls); |
| 799 | EXPECT_EQ(1U, startup_urls->GetSize()); |
| 800 | startup_urls->AppendString("http://example.org"); |
| 801 | } |
| 802 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 803 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 804 | // Expect a single Changed event for tracked pref #4 (startup URLs). |
| 805 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 806 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 807 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 808 | BEGIN_ALLOW_SINGLE_BUCKET + 4)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 809 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 810 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 811 | ? num_tracked_prefs() - 1 |
| 812 | : 0, |
| 813 | GetTrackedPrefHistogramCount( |
| 814 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 815 | |
| 816 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 817 | protection_level_ < PROTECTION_ENABLED_BASIC) |
| 818 | ? 1 |
| 819 | : 0, |
| 820 | GetTrackedPrefHistogramCount( |
| 821 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 822 | BEGIN_ALLOW_SINGLE_BUCKET + 4)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 823 | EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 1 : 0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 824 | GetTrackedPrefHistogramCount( |
| 825 | user_prefs::tracked::kTrackedPrefHistogramReset, |
| 826 | BEGIN_ALLOW_SINGLE_BUCKET + 4)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 827 | |
| 828 | // TODO(gab): This doesn't work on OS_CHROMEOS because we fail to attack |
| 829 | // Preferences. |
| 830 | #if !defined(OS_CHROMEOS) |
| 831 | // Explicitly verify the result of reported resets. |
| 832 | EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 0U : 2U, |
| 833 | profile() |
| 834 | ->GetPrefs() |
| 835 | ->GetList(prefs::kURLsToRestoreOnStartup) |
| 836 | ->GetSize()); |
| 837 | #endif |
| 838 | |
| 839 | // Nothing else should have triggered. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 840 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 841 | 0, GetTrackedPrefHistogramCount( |
| 842 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 843 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 844 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 845 | ALLOW_NONE)); |
| 846 | EXPECT_EQ(0, |
| 847 | GetTrackedPrefHistogramCount( |
| 848 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 849 | ALLOW_NONE)); |
| 850 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 851 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 852 | ALLOW_NONE)); |
| 853 | EXPECT_EQ( |
| 854 | 0, GetTrackedPrefHistogramCount( |
| 855 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 856 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 857 | |
| 858 | if (SupportsRegistryValidation()) { |
| 859 | // Expect a single Changed event for tracked pref #4 (startup URLs). |
| 860 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 861 | GetTrackedPrefHistogramCount( |
| 862 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 863 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 864 | BEGIN_ALLOW_SINGLE_BUCKET + 4)); |
| 865 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 866 | } |
| 867 | }; |
| 868 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 869 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedAtomic, ChangedAtomic); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 870 | |
| 871 | // Verifies that changing or adding an entry in a split pref results in both |
| 872 | // items being reported (and remove if the protection level allows it). |
| 873 | class PrefHashBrowserTestChangedSplitPref : public PrefHashBrowserTestBase { |
| 874 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 875 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 876 | InstallExtensionWithUIAutoConfirm( |
| 877 | test_data_dir_.AppendASCII("good.crx"), 1, browser()); |
| 878 | } |
| 879 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 880 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 881 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 882 | base::DictionaryValue* protected_preferences) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 883 | base::DictionaryValue* selected_prefs = |
| 884 | protection_level_ >= PROTECTION_ENABLED_EXTENSIONS |
| 885 | ? protected_preferences |
| 886 | : unprotected_preferences; |
| 887 | // |selected_prefs| should never be NULL under the protection level picking |
| 888 | // it. |
| 889 | EXPECT_TRUE(selected_prefs); |
| 890 | base::DictionaryValue* extensions_dict; |
| 891 | EXPECT_TRUE(selected_prefs->GetDictionary( |
| 892 | extensions::pref_names::kExtensions, &extensions_dict)); |
| 893 | EXPECT_TRUE(extensions_dict); |
| 894 | |
| 895 | // Tamper with any installed setting for good.crx |
| 896 | base::DictionaryValue* good_crx_dict; |
| 897 | EXPECT_TRUE(extensions_dict->GetDictionary(kGoodCrxId, &good_crx_dict)); |
| 898 | int good_crx_state; |
| 899 | EXPECT_TRUE(good_crx_dict->GetInteger("state", &good_crx_state)); |
| 900 | EXPECT_EQ(extensions::Extension::ENABLED, good_crx_state); |
| 901 | good_crx_dict->SetInteger("state", extensions::Extension::DISABLED); |
| 902 | |
| 903 | // Drop a fake extension (for the purpose of this test, dropped settings |
| 904 | // don't need to be valid extension settings). |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 905 | auto fake_extension = base::MakeUnique<base::DictionaryValue>(); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 906 | fake_extension->SetString("name", "foo"); |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 907 | extensions_dict->Set(std::string(32, 'a'), std::move(fake_extension)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 908 | } |
| 909 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 910 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 911 | // Expect a single split pref changed report with a count of 2 for tracked |
| 912 | // pref #5 (extensions). |
| 913 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 914 | GetTrackedPrefHistogramCount( |
| 915 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 916 | BEGIN_ALLOW_SINGLE_BUCKET + 5)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 917 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 918 | GetTrackedPrefHistogramCount( |
| 919 | "Settings.TrackedSplitPreferenceChanged.extensions.settings", |
| 920 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 921 | |
| 922 | // Everything else should have remained unchanged. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 923 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 924 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 925 | ? num_tracked_prefs() - 1 |
| 926 | : 0, |
| 927 | GetTrackedPrefHistogramCount( |
| 928 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 929 | |
| 930 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 931 | protection_level_ < PROTECTION_ENABLED_EXTENSIONS) |
| 932 | ? 1 |
| 933 | : 0, |
| 934 | GetTrackedPrefHistogramCount( |
| 935 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 936 | BEGIN_ALLOW_SINGLE_BUCKET + 5)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 937 | EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_EXTENSIONS ? 1 : 0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 938 | GetTrackedPrefHistogramCount( |
| 939 | user_prefs::tracked::kTrackedPrefHistogramReset, |
| 940 | BEGIN_ALLOW_SINGLE_BUCKET + 5)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 941 | |
| 942 | EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_EXTENSIONS, |
| 943 | extension_service()->GetExtensionById(kGoodCrxId, true) != NULL); |
| 944 | |
| 945 | // Nothing else should have triggered. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 946 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 947 | 0, GetTrackedPrefHistogramCount( |
| 948 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 949 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 950 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 951 | ALLOW_NONE)); |
| 952 | EXPECT_EQ(0, |
| 953 | GetTrackedPrefHistogramCount( |
| 954 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 955 | ALLOW_NONE)); |
| 956 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 957 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 958 | ALLOW_NONE)); |
| 959 | EXPECT_EQ( |
| 960 | 0, GetTrackedPrefHistogramCount( |
| 961 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 962 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 963 | |
| 964 | if (SupportsRegistryValidation()) { |
| 965 | // Expect that the registry validation caught the invalid MAC in split |
| 966 | // pref #5 (extensions). |
| 967 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 968 | GetTrackedPrefHistogramCount( |
| 969 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 970 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 971 | BEGIN_ALLOW_SINGLE_BUCKET + 5)); |
| 972 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 973 | } |
| 974 | }; |
| 975 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 976 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedSplitPref, ChangedSplitPref); |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 977 | |
| 978 | // Verifies that adding a value to unprotected preferences for a key which is |
| 979 | // still using the default (i.e. has no value stored in protected preferences) |
| 980 | // doesn't allow that value to slip in with no valid MAC (regression test for |
| 981 | // http://crbug.com/414554) |
| 982 | class PrefHashBrowserTestUntrustedAdditionToPrefs |
| 983 | : public PrefHashBrowserTestBase { |
| 984 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 985 | void SetupPreferences() override { |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 986 | // Ensure there is no user-selected value for kRestoreOnStartup. |
| 987 | EXPECT_FALSE( |
| 988 | profile()->GetPrefs()->GetUserPrefValue(prefs::kRestoreOnStartup)); |
| 989 | } |
| 990 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 991 | void AttackPreferencesOnDisk( |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 992 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 993 | base::DictionaryValue* protected_preferences) override { |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 994 | unprotected_preferences->SetInteger(prefs::kRestoreOnStartup, |
| 995 | SessionStartupPref::LAST); |
| 996 | } |
| 997 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 998 | void VerifyReactionToPrefAttack() override { |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 999 | // Expect a single Changed event for tracked pref #3 (kRestoreOnStartup) if |
| 1000 | // not protecting; if protection is enabled the change should be a no-op. |
| 1001 | int changed_expected = |
| 1002 | protection_level_ == PROTECTION_DISABLED_FOR_GROUP ? 1 : 0; |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1003 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 1004 | protection_level_ < PROTECTION_ENABLED_BASIC) |
| 1005 | ? changed_expected |
| 1006 | : 0, |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1007 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1008 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 1009 | BEGIN_ALLOW_SINGLE_BUCKET + 3)); |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1010 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1011 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 1012 | ? num_tracked_prefs() - changed_expected |
| 1013 | : 0, |
| 1014 | GetTrackedPrefHistogramCount( |
| 1015 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 1016 | |
| 1017 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 1018 | protection_level_ < PROTECTION_ENABLED_BASIC) |
| 1019 | ? 1 |
| 1020 | : 0, |
| 1021 | GetTrackedPrefHistogramCount( |
| 1022 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 1023 | BEGIN_ALLOW_SINGLE_BUCKET + 3)); |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1024 | EXPECT_EQ(0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1025 | GetTrackedPrefHistogramCount( |
| 1026 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE)); |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1027 | |
| 1028 | // Nothing else should have triggered. |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1029 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1030 | 0, GetTrackedPrefHistogramCount( |
| 1031 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 1032 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 1033 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 1034 | ALLOW_NONE)); |
| 1035 | EXPECT_EQ(0, |
| 1036 | GetTrackedPrefHistogramCount( |
| 1037 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 1038 | ALLOW_NONE)); |
| 1039 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 1040 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 1041 | ALLOW_NONE)); |
| 1042 | EXPECT_EQ( |
| 1043 | 0, GetTrackedPrefHistogramCount( |
| 1044 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 1045 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 1046 | |
| 1047 | if (SupportsRegistryValidation()) { |
| 1048 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 1049 | protection_level_ < PROTECTION_ENABLED_BASIC) |
| 1050 | ? changed_expected |
| 1051 | : 0, |
| 1052 | GetTrackedPrefHistogramCount( |
| 1053 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 1054 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 1055 | BEGIN_ALLOW_SINGLE_BUCKET + 3)); |
| 1056 | } |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1057 | } |
| 1058 | }; |
| 1059 | |
| 1060 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefs, |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 1061 | UntrustedAdditionToPrefs); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1062 | |
| 1063 | // Verifies that adding a value to unprotected preferences while wiping a |
| 1064 | // user-selected value from protected preferences doesn't allow that value to |
| 1065 | // slip in with no valid MAC (regression test for http://crbug.com/414554). |
| 1066 | class PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe |
| 1067 | : public PrefHashBrowserTestBase { |
| 1068 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 1069 | void SetupPreferences() override { |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1070 | profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); |
| 1071 | } |
| 1072 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 1073 | void AttackPreferencesOnDisk( |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1074 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 1075 | base::DictionaryValue* protected_preferences) override { |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1076 | // Set or change the value in Preferences to the attacker's choice. |
| 1077 | unprotected_preferences->SetString(prefs::kHomePage, "http://example.net"); |
| 1078 | // Clear the value in Secure Preferences, if any. |
| 1079 | if (protected_preferences) |
| 1080 | protected_preferences->Remove(prefs::kHomePage, NULL); |
| 1081 | } |
| 1082 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 1083 | void VerifyReactionToPrefAttack() override { |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1084 | // Expect a single Changed event for tracked pref #2 (kHomePage) if |
| 1085 | // not protecting; if protection is enabled the change should be a Cleared. |
| 1086 | int changed_expected = |
| 1087 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 1088 | protection_level_ < PROTECTION_ENABLED_BASIC |
| 1089 | ? 1 : 0; |
| 1090 | int cleared_expected = |
| 1091 | protection_level_ >= PROTECTION_ENABLED_BASIC |
| 1092 | ? 1 : 0; |
| 1093 | EXPECT_EQ(changed_expected, |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1094 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1095 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 1096 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 1097 | EXPECT_EQ(cleared_expected, |
| 1098 | GetTrackedPrefHistogramCount( |
| 1099 | user_prefs::tracked::kTrackedPrefHistogramCleared, |
| 1100 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1101 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1102 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 1103 | ? num_tracked_prefs() - changed_expected - cleared_expected |
| 1104 | : 0, |
| 1105 | GetTrackedPrefHistogramCount( |
| 1106 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 1107 | |
| 1108 | EXPECT_EQ(changed_expected, |
| 1109 | GetTrackedPrefHistogramCount( |
| 1110 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 1111 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1112 | EXPECT_EQ(0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1113 | GetTrackedPrefHistogramCount( |
| 1114 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1115 | |
| 1116 | // Nothing else should have triggered. |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1117 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 1118 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 1119 | ALLOW_NONE)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1120 | EXPECT_EQ(0, |
| 1121 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1122 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 1123 | ALLOW_NONE)); |
| 1124 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 1125 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 1126 | ALLOW_NONE)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1127 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1128 | 0, GetTrackedPrefHistogramCount( |
| 1129 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 1130 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 1131 | |
| 1132 | if (SupportsRegistryValidation()) { |
| 1133 | EXPECT_EQ(changed_expected, |
| 1134 | GetTrackedPrefHistogramCount( |
| 1135 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 1136 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 1137 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 1138 | EXPECT_EQ(cleared_expected, |
| 1139 | GetTrackedPrefHistogramCount( |
| 1140 | user_prefs::tracked::kTrackedPrefHistogramCleared, |
| 1141 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 1142 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 1143 | } |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1144 | } |
| 1145 | }; |
| 1146 | |
| 1147 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe, |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 1148 | UntrustedAdditionToPrefsAfterWipe); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 1149 | |
| 1150 | #if defined(OS_WIN) |
| 1151 | class PrefHashBrowserTestRegistryValidationFailure |
| 1152 | : public PrefHashBrowserTestBase { |
| 1153 | public: |
| 1154 | void SetupPreferences() override { |
| 1155 | profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); |
| 1156 | } |
| 1157 | |
| 1158 | void AttackPreferencesOnDisk( |
| 1159 | base::DictionaryValue* unprotected_preferences, |
| 1160 | base::DictionaryValue* protected_preferences) override { |
| 1161 | base::string16 registry_key = |
| 1162 | GetRegistryPathForTestProfile() + L"\\PreferenceMACs\\Default"; |
| 1163 | base::win::RegKey key; |
| 1164 | ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, registry_key.c_str(), |
| 1165 | KEY_SET_VALUE | KEY_WOW64_32KEY)); |
| 1166 | // An incorrect hash should still have the correct size. |
| 1167 | ASSERT_EQ(ERROR_SUCCESS, |
| 1168 | key.WriteValue(L"homepage", base::string16(64, 'A').c_str())); |
| 1169 | } |
| 1170 | |
| 1171 | void VerifyReactionToPrefAttack() override { |
| 1172 | EXPECT_EQ( |
| 1173 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 1174 | ? num_tracked_prefs() |
| 1175 | : 0, |
| 1176 | GetTrackedPrefHistogramCount( |
| 1177 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 1178 | |
| 1179 | if (SupportsRegistryValidation()) { |
| 1180 | // Expect that the registry validation caught the invalid MAC for pref #2 |
| 1181 | // (homepage). |
| 1182 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 1183 | GetTrackedPrefHistogramCount( |
| 1184 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 1185 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 1186 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 1187 | } |
| 1188 | } |
| 1189 | }; |
| 1190 | |
| 1191 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestRegistryValidationFailure, |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 1192 | RegistryValidationFailure); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 1193 | #endif |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1194 | |
| 1195 | // Verifies that all preferences related to choice of default search engine are |
| 1196 | // protected. |
| 1197 | class PrefHashBrowserTestDefaultSearch : public PrefHashBrowserTestBase { |
| 1198 | public: |
| 1199 | void SetupPreferences() override { |
| 1200 | // Set user selected default search engine. |
| 1201 | DefaultSearchManager default_search_manager( |
| 1202 | profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); |
| 1203 | DefaultSearchManager::Source dse_source = |
| 1204 | static_cast<DefaultSearchManager::Source>(-1); |
| 1205 | |
| 1206 | TemplateURLData user_dse; |
| 1207 | user_dse.SetKeyword(base::UTF8ToUTF16("userkeyword")); |
| 1208 | user_dse.SetShortName(base::UTF8ToUTF16("username")); |
| 1209 | user_dse.SetURL("http://user_default_engine/search?q=good_user_query"); |
| 1210 | default_search_manager.SetUserSelectedDefaultSearchEngine(user_dse); |
| 1211 | |
| 1212 | const TemplateURLData* current_dse = |
| 1213 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 1214 | EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source); |
| 1215 | EXPECT_EQ(current_dse->keyword(), base::UTF8ToUTF16("userkeyword")); |
| 1216 | EXPECT_EQ(current_dse->short_name(), base::UTF8ToUTF16("username")); |
| 1217 | EXPECT_EQ(current_dse->url(), |
| 1218 | "http://user_default_engine/search?q=good_user_query"); |
| 1219 | } |
| 1220 | |
| 1221 | void AttackPreferencesOnDisk( |
| 1222 | base::DictionaryValue* unprotected_preferences, |
| 1223 | base::DictionaryValue* protected_preferences) override { |
| 1224 | static constexpr char default_search_provider_data[] = R"( |
| 1225 | { |
| 1226 | "default_search_provider_data" : { |
| 1227 | "template_url_data" : { |
| 1228 | "keyword" : "badkeyword", |
| 1229 | "short_name" : "badname", |
| 1230 | "url" : "http://bad_default_engine/search?q=dirty_user_query" |
| 1231 | } |
| 1232 | } |
| 1233 | })"; |
| 1234 | static constexpr char search_provider_overrides[] = R"( |
| 1235 | { |
| 1236 | "search_provider_overrides" : [ |
| 1237 | { |
| 1238 | "keyword" : "badkeyword", |
| 1239 | "name" : "badname", |
| 1240 | "search_url" : "http://bad_default_engine/search?q=dirty_user_query", |
| 1241 | "encoding" : "utf-8", |
| 1242 | "id" : 1 |
| 1243 | }, { |
| 1244 | "keyword" : "badkeyword2", |
| 1245 | "name" : "badname2", |
| 1246 | "search_url" : "http://bad_default_engine2/search?q=dirty_user_query", |
| 1247 | "encoding" : "utf-8", |
| 1248 | "id" : 2 |
| 1249 | } |
| 1250 | ] |
| 1251 | })"; |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1252 | |
| 1253 | // Try to override default search in all three of available preferences. |
| 1254 | auto attack1 = base::DictionaryValue::From( |
| 1255 | base::JSONReader::Read(default_search_provider_data)); |
| 1256 | auto attack2 = base::DictionaryValue::From( |
| 1257 | base::JSONReader::Read(search_provider_overrides)); |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1258 | unprotected_preferences->MergeDictionary(attack1.get()); |
| 1259 | unprotected_preferences->MergeDictionary(attack2.get()); |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1260 | if (protected_preferences) { |
| 1261 | // Override here, too. |
| 1262 | protected_preferences->MergeDictionary(attack1.get()); |
| 1263 | protected_preferences->MergeDictionary(attack2.get()); |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | void VerifyReactionToPrefAttack() override { |
| 1268 | DefaultSearchManager default_search_manager( |
| 1269 | profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); |
| 1270 | DefaultSearchManager::Source dse_source = |
| 1271 | static_cast<DefaultSearchManager::Source>(-1); |
| 1272 | |
| 1273 | const TemplateURLData* current_dse = |
| 1274 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 1275 | |
| 1276 | if (protection_level_ < PROTECTION_ENABLED_DSE) { |
| 1277 | // This doesn't work on OS_CHROMEOS because we fail to attack Preferences. |
| 1278 | #if !defined(OS_CHROMEOS) |
| 1279 | // Attack is successful. |
| 1280 | EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source); |
| 1281 | EXPECT_EQ(current_dse->keyword(), base::UTF8ToUTF16("badkeyword")); |
| 1282 | EXPECT_EQ(current_dse->short_name(), base::UTF8ToUTF16("badname")); |
| 1283 | EXPECT_EQ(current_dse->url(), |
| 1284 | "http://bad_default_engine/search?q=dirty_user_query"); |
| 1285 | #endif |
| 1286 | } else { |
| 1287 | // Attack fails. |
| 1288 | EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, dse_source); |
| 1289 | EXPECT_NE(current_dse->keyword(), base::UTF8ToUTF16("badkeyword")); |
| 1290 | EXPECT_NE(current_dse->short_name(), base::UTF8ToUTF16("badname")); |
| 1291 | EXPECT_NE(current_dse->url(), |
| 1292 | "http://bad_default_engine/search?q=dirty_user_query"); |
| 1293 | } |
| 1294 | } |
| 1295 | }; |
| 1296 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame^] | 1297 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestDefaultSearch, SearchProtected); |