blob: f77616a6b84ab5faa574b096939d1e39677681e3 [file] [log] [blame]
[email protected]ae7bf342014-08-06 18:03:471// 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
dcheng4af48582016-04-19 00:29:355#include <memory>
[email protected]ae7bf342014-08-06 18:03:476#include <string>
7
blundell6e85b7c2015-09-29 12:33:358#include "base/base_switches.h"
[email protected]ae7bf342014-08-06 18:03:479#include "base/command_line.h"
[email protected]ae7bf342014-08-06 18:03:4710#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0411#include "base/files/file_util.h"
[email protected]ae7bf342014-08-06 18:03:4712#include "base/json/json_file_value_serializer.h"
[email protected]ae7bf342014-08-06 18:03:4713#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]ae7bf342014-08-06 18:03:4717#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.m113481c82015-12-16 05:21:4333#include "components/user_prefs/tracked/tracked_preference_histogram_names.h"
[email protected]ae7bf342014-08-06 18:03:4734#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
proberge269fd092016-10-04 22:13:4141#if defined(OS_WIN)
42#include "base/test/test_reg_util_win.h"
43#endif
44
[email protected]ae7bf342014-08-06 18:03:4745namespace {
46
47// Extension ID of chrome/test/data/extensions/good.crx
48const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
49
50// Explicit expectations from the caller of GetTrackedPrefHistogramCount(). This
51// enables detailed reporting of the culprit on failure.
52enum 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
proberge269fd092016-10-04 22:13:4163#if defined(OS_WIN)
64base::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]ae7bf342014-08-06 18:03:4772// 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).
76int GetTrackedPrefHistogramCount(const char* histogram_name,
proberge269fd092016-10-04 22:13:4177 const char* histogram_suffix,
[email protected]ae7bf342014-08-06 18:03:4778 int allowed_buckets) {
proberge269fd092016-10-04 22:13:4179 std::string full_histogram_name(histogram_name);
80 if (*histogram_suffix)
81 full_histogram_name.append(".").append(histogram_suffix);
[email protected]ae7bf342014-08-06 18:03:4782 const base::HistogramBase* histogram =
proberge269fd092016-10-04 22:13:4183 base::StatisticsRecorder::FindHistogram(full_histogram_name);
[email protected]ae7bf342014-08-06 18:03:4784 if (!histogram)
85 return 0;
86
dcheng4af48582016-04-19 00:29:3587 std::unique_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
[email protected]ae7bf342014-08-06 18:03:4788 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
proberge269fd092016-10-04 22:13:41102// Helper function to call GetTrackedPrefHistogramCount with no external
103// validation suffix.
104int GetTrackedPrefHistogramCount(const char* histogram_name,
105 int allowed_buckets) {
106 return GetTrackedPrefHistogramCount(histogram_name, "", allowed_buckets);
107}
108
dcheng4af48582016-04-19 00:29:35109std::unique_ptr<base::DictionaryValue> ReadPrefsDictionary(
[email protected]ae7bf342014-08-06 18:03:47110 const base::FilePath& pref_file) {
prashhir54a994502015-03-05 09:30:57111 JSONFileValueDeserializer deserializer(pref_file);
112 int error_code = JSONFileValueDeserializer::JSON_NO_ERROR;
[email protected]ae7bf342014-08-06 18:03:47113 std::string error_str;
dcheng4af48582016-04-19 00:29:35114 std::unique_ptr<base::Value> prefs =
olli.raulaba045252015-10-16 06:16:40115 deserializer.Deserialize(&error_code, &error_str);
prashhir54a994502015-03-05 09:30:57116 if (!prefs || error_code != JSONFileValueDeserializer::JSON_NO_ERROR) {
[email protected]ae7bf342014-08-06 18:03:47117 ADD_FAILURE() << "Error #" << error_code << ": " << error_str;
dcheng4af48582016-04-19 00:29:35118 return std::unique_ptr<base::DictionaryValue>();
[email protected]ae7bf342014-08-06 18:03:47119 }
120 if (!prefs->IsType(base::Value::TYPE_DICTIONARY)) {
121 ADD_FAILURE();
dcheng4af48582016-04-19 00:29:35122 return std::unique_ptr<base::DictionaryValue>();
[email protected]ae7bf342014-08-06 18:03:47123 }
dcheng4af48582016-04-19 00:29:35124 return std::unique_ptr<base::DictionaryValue>(
[email protected]ae7bf342014-08-06 18:03:47125 static_cast<base::DictionaryValue*>(prefs.release()));
126}
127
proberge269fd092016-10-04 22:13:41128// Returns whether external validation is supported on the platform through
129// storing MACs in the registry.
130bool SupportsRegistryValidation() {
131#if defined(OS_WIN)
132 return true;
133#else
134 return false;
135#endif
136}
137
[email protected]ae7bf342014-08-06 18:03:47138#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.
162class 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
avi556c05022014-12-22 23:31:43183 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]ae7bf342014-08-06 18:03:47184 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
dcheng8f4b8622014-10-23 16:37:48196 bool SetUpUserDataDirectory() override {
[email protected]ae7bf342014-08-06 18:03:47197 // 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
dcheng4af48582016-04-19 00:29:35234 std::unique_ptr<base::DictionaryValue> unprotected_preferences(
[email protected]ae7bf342014-08-06 18:03:47235 ReadPrefsDictionary(unprotected_pref_file));
236 if (!unprotected_preferences)
237 return false;
238
dcheng4af48582016-04-19 00:29:35239 std::unique_ptr<base::DictionaryValue> protected_preferences;
[email protected]ae7bf342014-08-06 18:03:47240 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
gabbcdefd0cc2015-02-17 18:12:40264 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();
proberge269fd092016-10-04 22:13:41270
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 &registry_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();
gabbcdefd0cc2015-02-17 18:12:40308 }
309
[email protected]ae7bf342014-08-06 18:03:47310 // 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.
dcheng8f4b8622014-10-23 16:37:48313 void SetUpOnMainThread() override {
[email protected]ae7bf342014-08-06 18:03:47314 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.m113481c82015-12-16 05:21:43328 user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY);
[email protected]ae7bf342014-08-06 18:03:47329 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
330 num_tracked_prefs_ > 0);
331
gab342aa6182014-10-02 21:59:00332 // Split tracked prefs are reported as Unchanged not as NullInitialized
[email protected]ae7bf342014-08-06 18:03:47333 // 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.m113481c82015-12-16 05:21:43336 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
337 BEGIN_ALLOW_SINGLE_BUCKET + 5);
[email protected]ae7bf342014-08-06 18:03:47338 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
339 num_split_tracked_prefs);
340
proberge269fd092016-10-04 22:13:41341 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]ae7bf342014-08-06 18:03:47358 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() {
brettw66d1b81b2015-07-06 19:29:40401 return base::StartsWith(
brettw44ce0ec52015-06-12 01:57:57402 testing::UnitTest::GetInstance()->current_test_info()->name(), "PRE_",
brettw66d1b81b2015-07-06 19:29:40403 base::CompareCase::SENSITIVE);
[email protected]ae7bf342014-08-06 18:03:47404 }
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_;
proberge269fd092016-10-04 22:13:41445
446#if defined(OS_WIN)
447 base::string16 registry_key_for_external_validation_;
448#endif
[email protected]ae7bf342014-08-06 18:03:47449};
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.
455class PrefHashBrowserTestUnchangedDefault : public PrefHashBrowserTestBase {
456 public:
dcheng8f4b8622014-10-23 16:37:48457 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47458 // Default Chrome setup.
459 }
460
dcheng8f4b8622014-10-23 16:37:48461 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47462 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17463 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47464 // No attack.
465 }
466
dcheng8f4b8622014-10-23 16:37:48467 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47468 // Expect all prefs to be reported as Unchanged with no resets.
deepak.m113481c82015-12-16 05:21:43469 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]ae7bf342014-08-06 18:03:47478 EXPECT_EQ(0,
479 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43480 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47481
482 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47483 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43484 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));
proberge269fd092016-10-04 22:13:41503
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]ae7bf342014-08-06 18:03:47514 }
515};
516
517PREF_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.
522class PrefHashBrowserTestUnchangedCustom
523 : public PrefHashBrowserTestUnchangedDefault {
524 public:
dcheng8f4b8622014-10-23 16:37:48525 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47526 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
527
528 InstallExtensionWithUIAutoConfirm(
529 test_data_dir_.AppendASCII("good.crx"), 1, browser());
530 }
531
dcheng8f4b8622014-10-23 16:37:48532 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47533 // 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
544PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedCustom, UnchangedCustom);
545
546// Verifies that cleared prefs are reported.
547class PrefHashBrowserTestClearedAtomic : public PrefHashBrowserTestBase {
548 public:
dcheng8f4b8622014-10-23 16:37:48549 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47550 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
551 }
552
dcheng8f4b8622014-10-23 16:37:48553 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47554 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17555 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47556 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
dcheng8f4b8622014-10-23 16:37:48565 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47566 // 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]ae7bf342014-08-06 18:03:47570 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43571 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]ae7bf342014-08-06 18:03:47582 EXPECT_EQ(0,
583 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43584 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47585
586 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47587 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43588 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));
proberge269fd092016-10-04 22:13:41604
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]ae7bf342014-08-06 18:03:47613 }
614};
615
616PREF_HASH_BROWSER_TEST(PrefHashBrowserTestClearedAtomic, ClearedAtomic);
617
618// Verifies that clearing the MACs results in untrusted Initialized pings for
619// non-null protected prefs.
620class PrefHashBrowserTestUntrustedInitialized : public PrefHashBrowserTestBase {
621 public:
dcheng8f4b8622014-10-23 16:37:48622 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47623 // 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
dcheng8f4b8622014-10-23 16:37:48646 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47647 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17648 base::DictionaryValue* protected_preferences) override {
proberge574d7d92016-08-01 20:14:11649 unprotected_preferences->Remove("protection.macs", NULL);
[email protected]ae7bf342014-08-06 18:03:47650 if (protected_preferences)
proberge574d7d92016-08-01 20:14:11651 protected_preferences->Remove("protection.macs", NULL);
[email protected]ae7bf342014-08-06 18:03:47652 }
653
dcheng8f4b8622014-10-23 16:37:48654 void VerifyReactionToPrefAttack() override {
gab342aa6182014-10-02 21:59:00655 // Preferences that are NULL by default will be NullInitialized.
[email protected]ae7bf342014-08-06 18:03:47656 int num_null_values = GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43657 user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY);
[email protected]ae7bf342014-08-06 18:03:47658 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).
gab342aa6182014-10-02 21:59:00663 EXPECT_GE(num_tracked_prefs() - num_null_values, 3);
[email protected]ae7bf342014-08-06 18:03:47664 }
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.m113481c82015-12-16 05:21:43669 EXPECT_EQ(
670 num_tracked_prefs() - num_null_values,
671 GetTrackedPrefHistogramCount(
672 user_prefs::tracked::kTrackedPrefHistogramInitialized, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47673
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.m113481c82015-12-16 05:21:43697 EXPECT_EQ(
698 num_tracked_prefs() - num_null_values - num_protected_prefs,
699 GetTrackedPrefHistogramCount(
700 user_prefs::tracked::kTrackedPrefHistogramWantedReset, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47701 EXPECT_EQ(num_protected_prefs,
deepak.m113481c82015-12-16 05:21:43702 GetTrackedPrefHistogramCount(
703 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47704
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.m113481c82015-12-16 05:21:43722 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
723 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
724 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47725 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43726 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));
proberge269fd092016-10-04 22:13:41735
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]ae7bf342014-08-06 18:03:47747 }
748};
749
750PREF_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).
755class PrefHashBrowserTestChangedAtomic : public PrefHashBrowserTestBase {
756 public:
dcheng8f4b8622014-10-23 16:37:48757 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47758 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
dcheng8f4b8622014-10-23 16:37:48766 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47767 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17768 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47769 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
dcheng8f4b8622014-10-23 16:37:48783 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47784 // Expect a single Changed event for tracked pref #4 (startup URLs).
785 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
[email protected]ae7bf342014-08-06 18:03:47786 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43787 user_prefs::tracked::kTrackedPrefHistogramChanged,
788 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47789 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43790 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]ae7bf342014-08-06 18:03:47803 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43804 GetTrackedPrefHistogramCount(
805 user_prefs::tracked::kTrackedPrefHistogramReset,
806 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47807
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]ae7bf342014-08-06 18:03:47820 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43821 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));
proberge269fd092016-10-04 22:13:41837
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]ae7bf342014-08-06 18:03:47846 }
847};
848
849PREF_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).
853class PrefHashBrowserTestChangedSplitPref : public PrefHashBrowserTestBase {
854 public:
dcheng8f4b8622014-10-23 16:37:48855 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47856 InstallExtensionWithUIAutoConfirm(
857 test_data_dir_.AppendASCII("good.crx"), 1, browser());
858 }
859
dcheng8f4b8622014-10-23 16:37:48860 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47861 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17862 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47863 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
dcheng8f4b8622014-10-23 16:37:48890 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47891 // 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.m113481c82015-12-16 05:21:43894 GetTrackedPrefHistogramCount(
895 user_prefs::tracked::kTrackedPrefHistogramChanged,
896 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47897 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]ae7bf342014-08-06 18:03:47903 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43904 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]ae7bf342014-08-06 18:03:47917 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_EXTENSIONS ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43918 GetTrackedPrefHistogramCount(
919 user_prefs::tracked::kTrackedPrefHistogramReset,
920 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47921
922 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_EXTENSIONS,
923 extension_service()->GetExtensionById(kGoodCrxId, true) != NULL);
924
925 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47926 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43927 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));
proberge269fd092016-10-04 22:13:41943
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]ae7bf342014-08-06 18:03:47953 }
954};
955
956PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedSplitPref, ChangedSplitPref);
gabb004a562014-09-16 12:45:22957
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)
962class PrefHashBrowserTestUntrustedAdditionToPrefs
963 : public PrefHashBrowserTestBase {
964 public:
dcheng8f4b8622014-10-23 16:37:48965 void SetupPreferences() override {
gabb004a562014-09-16 12:45:22966 // Ensure there is no user-selected value for kRestoreOnStartup.
967 EXPECT_FALSE(
968 profile()->GetPrefs()->GetUserPrefValue(prefs::kRestoreOnStartup));
969 }
970
dcheng8f4b8622014-10-23 16:37:48971 void AttackPreferencesOnDisk(
gabb004a562014-09-16 12:45:22972 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17973 base::DictionaryValue* protected_preferences) override {
gabb004a562014-09-16 12:45:22974 unprotected_preferences->SetInteger(prefs::kRestoreOnStartup,
975 SessionStartupPref::LAST);
976 }
977
dcheng8f4b8622014-10-23 16:37:48978 void VerifyReactionToPrefAttack() override {
gabb004a562014-09-16 12:45:22979 // 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.m113481c82015-12-16 05:21:43983 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
984 protection_level_ < PROTECTION_ENABLED_BASIC)
985 ? changed_expected
986 : 0,
gabb004a562014-09-16 12:45:22987 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43988 user_prefs::tracked::kTrackedPrefHistogramChanged,
989 BEGIN_ALLOW_SINGLE_BUCKET + 3));
gabb004a562014-09-16 12:45:22990 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43991 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));
gabb004a562014-09-16 12:45:221004 EXPECT_EQ(0,
deepak.m113481c82015-12-16 05:21:431005 GetTrackedPrefHistogramCount(
1006 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
gabb004a562014-09-16 12:45:221007
1008 // Nothing else should have triggered.
gabb004a562014-09-16 12:45:221009 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431010 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));
proberge269fd092016-10-04 22:13:411026
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 }
gabb004a562014-09-16 12:45:221037 }
1038};
1039
1040PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefs,
1041 UntrustedAdditionToPrefs);
erikwrightf4e02b72014-09-17 20:25:451042
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).
1046class PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe
1047 : public PrefHashBrowserTestBase {
1048 public:
dcheng8f4b8622014-10-23 16:37:481049 void SetupPreferences() override {
erikwrightf4e02b72014-09-17 20:25:451050 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
1051 }
1052
dcheng8f4b8622014-10-23 16:37:481053 void AttackPreferencesOnDisk(
erikwrightf4e02b72014-09-17 20:25:451054 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:171055 base::DictionaryValue* protected_preferences) override {
erikwrightf4e02b72014-09-17 20:25:451056 // 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
dcheng8f4b8622014-10-23 16:37:481063 void VerifyReactionToPrefAttack() override {
erikwrightf4e02b72014-09-17 20:25:451064 // 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,
erikwrightf4e02b72014-09-17 20:25:451074 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:431075 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));
erikwrightf4e02b72014-09-17 20:25:451081 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431082 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));
erikwrightf4e02b72014-09-17 20:25:451092 EXPECT_EQ(0,
deepak.m113481c82015-12-16 05:21:431093 GetTrackedPrefHistogramCount(
1094 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:451095
1096 // Nothing else should have triggered.
deepak.m113481c82015-12-16 05:21:431097 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
1098 user_prefs::tracked::kTrackedPrefHistogramInitialized,
1099 ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:451100 EXPECT_EQ(0,
1101 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:431102 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
1103 ALLOW_NONE));
1104 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
1105 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
1106 ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:451107 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431108 0, GetTrackedPrefHistogramCount(
1109 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
1110 ALLOW_NONE));
proberge269fd092016-10-04 22:13:411111
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 }
erikwrightf4e02b72014-09-17 20:25:451124 }
1125};
1126
1127PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe,
1128 UntrustedAdditionToPrefsAfterWipe);
proberge269fd092016-10-04 22:13:411129
1130#if defined(OS_WIN)
1131class 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
1171PREF_HASH_BROWSER_TEST(PrefHashBrowserTestRegistryValidationFailure,
1172 RegistryValidationFailure);
1173#endif