123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- // Copyright 2013 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "services/preferences/tracked/pref_hash_store_impl.h"
- #include <stddef.h>
- #include <utility>
- #include "base/check.h"
- #include "base/memory/raw_ptr.h"
- #include "base/metrics/histogram_macros.h"
- #include "base/no_destructor.h"
- #include "base/notreached.h"
- #include "services/preferences/tracked/device_id.h"
- #include "services/preferences/tracked/hash_store_contents.h"
- namespace {
- using ValueState =
- prefs::mojom::TrackedPreferenceValidationDelegate::ValueState;
- // Returns a deterministic ID for this machine.
- std::string GenerateDeviceId() {
- static base::NoDestructor<std::string> cached_device_id;
- if (!cached_device_id->empty())
- return *cached_device_id;
- std::string device_id;
- MachineIdStatus status = GetDeterministicMachineSpecificId(&device_id);
- DCHECK(status == MachineIdStatus::NOT_IMPLEMENTED ||
- status == MachineIdStatus::SUCCESS);
- if (status == MachineIdStatus::SUCCESS) {
- *cached_device_id = device_id;
- return device_id;
- }
- return std::string();
- }
- } // namespace
- class PrefHashStoreImpl::PrefHashStoreTransactionImpl
- : public PrefHashStoreTransaction {
- public:
- // Constructs a PrefHashStoreTransactionImpl which can use the private
- // members of its |outer| PrefHashStoreImpl.
- PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer,
- HashStoreContents* storage);
- PrefHashStoreTransactionImpl(const PrefHashStoreTransactionImpl&) = delete;
- PrefHashStoreTransactionImpl& operator=(const PrefHashStoreTransactionImpl&) =
- delete;
- ~PrefHashStoreTransactionImpl() override;
- // PrefHashStoreTransaction implementation.
- base::StringPiece GetStoreUMASuffix() const override;
- ValueState CheckValue(const std::string& path,
- const base::Value* value) const override;
- void StoreHash(const std::string& path, const base::Value* value) override;
- ValueState CheckSplitValue(
- const std::string& path,
- const base::DictionaryValue* initial_split_value,
- std::vector<std::string>* invalid_keys) const override;
- void StoreSplitHash(const std::string& path,
- const base::DictionaryValue* split_value) override;
- bool HasHash(const std::string& path) const override;
- void ImportHash(const std::string& path, const base::Value* hash) override;
- void ClearHash(const std::string& path) override;
- bool IsSuperMACValid() const override;
- bool StampSuperMac() override;
- private:
- raw_ptr<PrefHashStoreImpl> outer_;
- raw_ptr<HashStoreContents> contents_;
- bool super_mac_valid_;
- bool super_mac_dirty_;
- };
- PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed,
- const std::string& legacy_device_id,
- bool use_super_mac)
- : pref_hash_calculator_(seed, GenerateDeviceId(), legacy_device_id),
- use_super_mac_(use_super_mac) {}
- PrefHashStoreImpl::~PrefHashStoreImpl() {}
- std::unique_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction(
- HashStoreContents* storage) {
- return std::unique_ptr<PrefHashStoreTransaction>(
- new PrefHashStoreTransactionImpl(this, storage));
- }
- std::string PrefHashStoreImpl::ComputeMac(const std::string& path,
- const base::Value* value) {
- return pref_hash_calculator_.Calculate(path, value);
- }
- std::unique_ptr<base::DictionaryValue> PrefHashStoreImpl::ComputeSplitMacs(
- const std::string& path,
- const base::DictionaryValue* split_values) {
- if (!split_values)
- return std::make_unique<base::DictionaryValue>();
- std::string keyed_path(path);
- keyed_path.push_back('.');
- const size_t common_part_length = keyed_path.length();
- std::unique_ptr<base::DictionaryValue> split_macs(new base::DictionaryValue);
- for (base::DictionaryValue::Iterator it(*split_values); !it.IsAtEnd();
- it.Advance()) {
- // Keep the common part from the old |keyed_path| and replace the key to
- // get the new |keyed_path|.
- keyed_path.replace(common_part_length, std::string::npos, it.key());
- split_macs->SetKey(it.key(),
- base::Value(ComputeMac(keyed_path, &it.value())));
- }
- return split_macs;
- }
- PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl(
- PrefHashStoreImpl* outer,
- HashStoreContents* storage)
- : outer_(outer),
- contents_(storage),
- super_mac_valid_(false),
- super_mac_dirty_(false) {
- if (!outer_->use_super_mac_)
- return;
- // The store must have a valid super MAC to be trusted.
- std::string super_mac = contents_->GetSuperMac();
- if (super_mac.empty())
- return;
- super_mac_valid_ =
- outer_->pref_hash_calculator_.Validate(
- "", contents_->GetContents(), super_mac) == PrefHashCalculator::VALID;
- }
- PrefHashStoreImpl::PrefHashStoreTransactionImpl::
- ~PrefHashStoreTransactionImpl() {
- if (super_mac_dirty_ && outer_->use_super_mac_) {
- // Get the dictionary of hashes (or NULL if it doesn't exist).
- const base::DictionaryValue* hashes_dict = contents_->GetContents();
- contents_->SetSuperMac(outer_->ComputeMac("", hashes_dict));
- }
- }
- base::StringPiece
- PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetStoreUMASuffix() const {
- return contents_->GetUMASuffix();
- }
- ValueState PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue(
- const std::string& path,
- const base::Value* initial_value) const {
- std::string last_hash;
- contents_->GetMac(path, &last_hash);
- if (last_hash.empty()) {
- // In the absence of a hash for this pref, always trust a NULL value, but
- // only trust an existing value if the initial hashes dictionary is trusted.
- if (!initial_value)
- return ValueState::TRUSTED_NULL_VALUE;
- else if (super_mac_valid_)
- return ValueState::TRUSTED_UNKNOWN_VALUE;
- else
- return ValueState::UNTRUSTED_UNKNOWN_VALUE;
- }
- PrefHashCalculator::ValidationResult validation_result =
- outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash);
- switch (validation_result) {
- case PrefHashCalculator::VALID:
- return ValueState::UNCHANGED;
- case PrefHashCalculator::VALID_SECURE_LEGACY:
- return ValueState::SECURE_LEGACY;
- case PrefHashCalculator::INVALID:
- return initial_value ? ValueState::CHANGED : ValueState::CLEARED;
- }
- NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: "
- << validation_result;
- return ValueState::UNTRUSTED_UNKNOWN_VALUE;
- }
- void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash(
- const std::string& path,
- const base::Value* new_value) {
- const std::string mac = outer_->ComputeMac(path, new_value);
- contents_->SetMac(path, mac);
- super_mac_dirty_ = true;
- }
- ValueState PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue(
- const std::string& path,
- const base::DictionaryValue* initial_split_value,
- std::vector<std::string>* invalid_keys) const {
- DCHECK(invalid_keys && invalid_keys->empty());
- std::map<std::string, std::string> split_macs;
- const bool has_hashes = contents_->GetSplitMacs(path, &split_macs);
- // Treat NULL and empty the same; otherwise we would need to store a hash for
- // the entire dictionary (or some other special beacon) to differentiate these
- // two cases which are really the same for dictionaries.
- if (!initial_split_value || initial_split_value->DictEmpty())
- return has_hashes ? ValueState::CLEARED : ValueState::UNCHANGED;
- if (!has_hashes)
- return super_mac_valid_ ? ValueState::TRUSTED_UNKNOWN_VALUE
- : ValueState::UNTRUSTED_UNKNOWN_VALUE;
- bool has_secure_legacy_id_hashes = false;
- std::string keyed_path(path);
- keyed_path.push_back('.');
- const size_t common_part_length = keyed_path.length();
- for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd();
- it.Advance()) {
- std::map<std::string, std::string>::iterator entry =
- split_macs.find(it.key());
- if (entry == split_macs.end()) {
- invalid_keys->push_back(it.key());
- } else {
- // Keep the common part from the old |keyed_path| and replace the key to
- // get the new |keyed_path|.
- keyed_path.replace(common_part_length, std::string::npos, it.key());
- switch (outer_->pref_hash_calculator_.Validate(keyed_path, &it.value(),
- entry->second)) {
- case PrefHashCalculator::VALID:
- break;
- case PrefHashCalculator::VALID_SECURE_LEGACY:
- // Secure legacy device IDs based hashes are still accepted, but we
- // should make sure to notify the caller for them to update the legacy
- // hashes.
- has_secure_legacy_id_hashes = true;
- break;
- case PrefHashCalculator::INVALID:
- invalid_keys->push_back(it.key());
- break;
- }
- // Remove processed MACs, remaining MACs at the end will also be
- // considered invalid.
- split_macs.erase(entry);
- }
- }
- // Anything left in the map is missing from the data.
- for (std::map<std::string, std::string>::const_iterator it =
- split_macs.begin();
- it != split_macs.end(); ++it) {
- invalid_keys->push_back(it->first);
- }
- return invalid_keys->empty()
- ? (has_secure_legacy_id_hashes ? ValueState::SECURE_LEGACY
- : ValueState::UNCHANGED)
- : ValueState::CHANGED;
- }
- void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash(
- const std::string& path,
- const base::DictionaryValue* split_value) {
- contents_->RemoveEntry(path);
- if (split_value) {
- std::unique_ptr<base::DictionaryValue> split_macs =
- outer_->ComputeSplitMacs(path, split_value);
- for (base::DictionaryValue::Iterator it(*split_macs); !it.IsAtEnd();
- it.Advance()) {
- DCHECK(it.value().is_string());
- contents_->SetSplitMac(path, it.key(), it.value().GetString());
- }
- }
- super_mac_dirty_ = true;
- }
- bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash(
- const std::string& path) const {
- std::string out_value;
- std::map<std::string, std::string> out_values;
- return contents_->GetMac(path, &out_value) ||
- contents_->GetSplitMacs(path, &out_values);
- }
- void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ImportHash(
- const std::string& path,
- const base::Value* hash) {
- DCHECK(hash);
- contents_->ImportEntry(path, hash);
- if (super_mac_valid_)
- super_mac_dirty_ = true;
- }
- void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ClearHash(
- const std::string& path) {
- if (contents_->RemoveEntry(path) && super_mac_valid_) {
- super_mac_dirty_ = true;
- }
- }
- bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const {
- return super_mac_valid_;
- }
- bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() {
- if (!outer_->use_super_mac_ || super_mac_valid_)
- return false;
- super_mac_dirty_ = true;
- return true;
- }
|