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