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