Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: chrome/browser/prefs/pref_hash_store_impl.cc

Issue 324493002: Move preference MACs to the protected preference stores. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: New approach. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/prefs/pref_hash_store_impl.h" 5 #include "chrome/browser/prefs/pref_hash_store_impl.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/prefs/pref_hash_store_transaction.h" 10 #include "chrome/browser/prefs/pref_hash_store_transaction.h"
11 #include "chrome/browser/prefs/tracked/hash_store_contents.h" 11 #include "chrome/browser/prefs/tracked/dictionary_hash_store_contents.h"
12
13 namespace {
14
15 // Returns true if the dictionary of hashes stored in |contents| is trusted
16 // (which implies unknown values can be trusted as newly tracked values).
17 bool IsHashDictionaryTrusted(const PrefHashCalculator& calculator,
18 const HashStoreContents& contents) {
19 const base::DictionaryValue* store_contents = contents.GetContents();
20 std::string super_mac = contents.GetSuperMac();
21 // The store must be initialized and have a valid super MAC to be trusted.
22 return store_contents && !super_mac.empty() &&
23 calculator.Validate(contents.hash_store_id(),
24 store_contents,
25 super_mac) == PrefHashCalculator::VALID;
26 }
27
28 } // namespace
29 12
30 class PrefHashStoreImpl::PrefHashStoreTransactionImpl 13 class PrefHashStoreImpl::PrefHashStoreTransactionImpl
31 : public PrefHashStoreTransaction { 14 : public PrefHashStoreTransaction {
32 public: 15 public:
33 // Constructs a PrefHashStoreTransactionImpl which can use the private 16 // Constructs a PrefHashStoreTransactionImpl which can use the private
34 // members of its |outer| PrefHashStoreImpl. 17 // members of its |outer| PrefHashStoreImpl.
35 explicit PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer); 18 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer,
19 base::DictionaryValue* storage);
36 virtual ~PrefHashStoreTransactionImpl(); 20 virtual ~PrefHashStoreTransactionImpl();
37 21
38 // PrefHashStoreTransaction implementation. 22 // PrefHashStoreTransaction implementation.
39 virtual ValueState CheckValue(const std::string& path, 23 virtual ValueState CheckValue(const std::string& path,
40 const base::Value* value) const OVERRIDE; 24 const base::Value* value) const OVERRIDE;
41 virtual void StoreHash(const std::string& path, 25 virtual void StoreHash(const std::string& path,
42 const base::Value* value) OVERRIDE; 26 const base::Value* value) OVERRIDE;
43 virtual bool StampSuperMac() OVERRIDE; 27 virtual bool StampSuperMac() OVERRIDE;
44 virtual ValueState CheckSplitValue( 28 virtual ValueState CheckSplitValue(
45 const std::string& path, 29 const std::string& path,
46 const base::DictionaryValue* initial_split_value, 30 const base::DictionaryValue* initial_split_value,
47 std::vector<std::string>* invalid_keys) const OVERRIDE; 31 std::vector<std::string>* invalid_keys) const OVERRIDE;
48 virtual void StoreSplitHash( 32 virtual void StoreSplitHash(
49 const std::string& path, 33 const std::string& path,
50 const base::DictionaryValue* split_value) OVERRIDE; 34 const base::DictionaryValue* split_value) OVERRIDE;
35 virtual bool HasHash(const std::string& path) const OVERRIDE;
36 virtual void ImportHash(const std::string& path,
37 const base::Value* hash) OVERRIDE;
38 virtual void ClearHash(const std::string& path) OVERRIDE;
51 39
52 private: 40 private:
53 bool GetSplitMacs(const std::string& path, 41 bool GetSplitMacs(const std::string& path,
54 std::map<std::string, std::string>* split_macs) const; 42 std::map<std::string, std::string>* split_macs) const;
43
55 PrefHashStoreImpl* outer_; 44 PrefHashStoreImpl* outer_;
56 bool has_changed_; 45 DictionaryHashStoreContents contents_;
46
47 bool super_mac_valid_;
48 bool super_mac_dirty_;
57 49
58 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); 50 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl);
59 }; 51 };
60 52
61 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, 53 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed,
62 const std::string& device_id, 54 const std::string& device_id,
63 scoped_ptr<HashStoreContents> contents,
64 bool use_super_mac) 55 bool use_super_mac)
65 : pref_hash_calculator_(seed, device_id), 56 : pref_hash_calculator_(seed, device_id),
66 contents_(contents.Pass()), 57 use_super_mac_(use_super_mac) {
67 initial_hashes_dictionary_trusted_(
68 use_super_mac
69 ? IsHashDictionaryTrusted(pref_hash_calculator_, *contents_)
70 : false),
71 use_super_mac_(use_super_mac),
72 has_pending_write_(false) {
73 DCHECK(contents_);
74 UMA_HISTOGRAM_BOOLEAN("Settings.HashesDictionaryTrusted",
erikwright (departed) 2014/06/12 20:49:41 We need to rethink where this goes. May need an ex
75 initial_hashes_dictionary_trusted_);
76 } 58 }
77 59
78 PrefHashStoreImpl::~PrefHashStoreImpl() {} 60 PrefHashStoreImpl::~PrefHashStoreImpl() {
79
80 void PrefHashStoreImpl::Reset() {
81 contents_->Reset();
82 } 61 }
83 62
84 scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction() { 63 scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction(
64 base::DictionaryValue* storage) {
85 return scoped_ptr<PrefHashStoreTransaction>( 65 return scoped_ptr<PrefHashStoreTransaction>(
86 new PrefHashStoreTransactionImpl(this)); 66 new PrefHashStoreTransactionImpl(this, storage));
87 }
88
89 void PrefHashStoreImpl::CommitPendingWrite() {
90 if (has_pending_write_) {
91 contents_->CommitPendingWrite();
92 has_pending_write_ = false;
93 }
94 } 67 }
95 68
96 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( 69 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl(
97 PrefHashStoreImpl* outer) : outer_(outer), has_changed_(false) { 70 PrefHashStoreImpl* outer,
71 base::DictionaryValue* storage)
72 : outer_(outer),
73 contents_(storage),
74 super_mac_valid_(false),
75 super_mac_dirty_(false) {
76 if (outer_->use_super_mac_) {
77 const base::DictionaryValue* store_contents = contents_.GetContents();
78 std::string super_mac = contents_.GetSuperMac();
79 // The store must be initialized and have a valid super MAC to be trusted.
80 super_mac_valid_ =
81 store_contents && !super_mac.empty() &&
82 outer_->pref_hash_calculator_.Validate(
83 contents_.hash_store_id(), store_contents, super_mac) ==
84 PrefHashCalculator::VALID;
85 }
98 } 86 }
99 87
100 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: 88 PrefHashStoreImpl::PrefHashStoreTransactionImpl::
101 ~PrefHashStoreTransactionImpl() { 89 ~PrefHashStoreTransactionImpl() {
102 // Update the super MAC if and only if the hashes dictionary has been 90 // Update the super MAC if and only if the hashes dictionary has been
103 // modified in this transaction. 91 // modified in this transaction.
104 if (has_changed_) { 92 if (super_mac_dirty_ && outer_->use_super_mac_) {
105 if (outer_->use_super_mac_) { 93 // Get the dictionary of hashes (or NULL if it doesn't exist).
106 // Get the dictionary of hashes (or NULL if it doesn't exist). 94 const base::DictionaryValue* hashes_dict = contents_.GetContents();
107 const base::DictionaryValue* hashes_dict = 95 contents_.SetSuperMac(outer_->pref_hash_calculator_.Calculate(
108 outer_->contents_->GetContents(); 96 contents_.hash_store_id(), hashes_dict));
109 outer_->contents_->SetSuperMac(outer_->pref_hash_calculator_.Calculate(
110 outer_->contents_->hash_store_id(), hashes_dict));
111 }
112 outer_->has_pending_write_ = true;
113 } 97 }
114
115 } 98 }
116 99
117 PrefHashStoreTransaction::ValueState 100 PrefHashStoreTransaction::ValueState
118 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( 101 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue(
119 const std::string& path, const base::Value* initial_value) const { 102 const std::string& path,
120 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); 103 const base::Value* initial_value) const {
104 const base::DictionaryValue* hashed_prefs = contents_.GetContents();
121 105
122 std::string last_hash; 106 std::string last_hash;
123 if (hashed_prefs) 107 if (hashed_prefs)
124 hashed_prefs->GetString(path, &last_hash); 108 hashed_prefs->GetString(path, &last_hash);
125 109
126 if (last_hash.empty()) { 110 if (last_hash.empty()) {
127 // In the absence of a hash for this pref, always trust a NULL value, but 111 // In the absence of a hash for this pref, always trust a NULL value, but
128 // only trust an existing value if the initial hashes dictionary is trusted. 112 // only trust an existing value if the initial hashes dictionary is trusted.
129 return (!initial_value || outer_->initial_hashes_dictionary_trusted_) ? 113 return (!initial_value || super_mac_valid_) ? TRUSTED_UNKNOWN_VALUE
130 TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE; 114 : UNTRUSTED_UNKNOWN_VALUE;
131 } 115 }
132 116
133 PrefHashCalculator::ValidationResult validation_result = 117 PrefHashCalculator::ValidationResult validation_result =
134 outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash); 118 outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash);
135 switch (validation_result) { 119 switch (validation_result) {
136 case PrefHashCalculator::VALID: 120 case PrefHashCalculator::VALID:
137 return UNCHANGED; 121 return UNCHANGED;
138 case PrefHashCalculator::VALID_WEAK_LEGACY: 122 case PrefHashCalculator::VALID_WEAK_LEGACY:
139 return WEAK_LEGACY; 123 return WEAK_LEGACY;
140 case PrefHashCalculator::VALID_SECURE_LEGACY: 124 case PrefHashCalculator::VALID_SECURE_LEGACY:
141 return SECURE_LEGACY; 125 return SECURE_LEGACY;
142 case PrefHashCalculator::INVALID: 126 case PrefHashCalculator::INVALID:
143 return initial_value ? CHANGED : CLEARED; 127 return initial_value ? CHANGED : CLEARED;
144 } 128 }
145 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " 129 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: "
146 << validation_result; 130 << validation_result;
147 return UNTRUSTED_UNKNOWN_VALUE; 131 return UNTRUSTED_UNKNOWN_VALUE;
148 } 132 }
149 133
150 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( 134 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash(
151 const std::string& path, const base::Value* new_value) { 135 const std::string& path,
136 const base::Value* new_value) {
152 const std::string mac = 137 const std::string mac =
153 outer_->pref_hash_calculator_.Calculate(path, new_value); 138 outer_->pref_hash_calculator_.Calculate(path, new_value);
154 (*outer_->contents_->GetMutableContents())->SetString(path, mac); 139 (*contents_.GetMutableContents())->SetString(path, mac);
155 has_changed_ = true; 140 super_mac_dirty_ = true;
141 }
142
143 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash(
144 const std::string& path) const {
145 const base::DictionaryValue* hashed_prefs = contents_.GetContents();
146 return hashed_prefs && hashed_prefs->Get(path, NULL);
147 }
148
149 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ImportHash(
150 const std::string& path,
151 const base::Value* hash) {
152 if (!hash)
153 (*contents_.GetMutableContents())->RemovePath(path, NULL);
154 else
155 (*contents_.GetMutableContents())->Set(path, hash->DeepCopy());
156 super_mac_dirty_ = super_mac_dirty_ || super_mac_valid_;
157 }
158
159 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ClearHash(
160 const std::string& path) {
161 if ((*contents_.GetMutableContents())->RemovePath(path, NULL) &&
162 super_mac_valid_) {
163 super_mac_dirty_ = true;
164 }
156 } 165 }
157 166
158 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { 167 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() {
159 if (!outer_->use_super_mac_ || outer_->initial_hashes_dictionary_trusted_) 168 if (!outer_->use_super_mac_ || super_mac_valid_)
160 return false; 169 return false;
161 has_changed_ = true; 170 super_mac_dirty_ = true;
162 return true; 171 return true;
163 } 172 }
164 173
165 PrefHashStoreTransaction::ValueState 174 PrefHashStoreTransaction::ValueState
166 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( 175 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue(
167 const std::string& path, 176 const std::string& path,
168 const base::DictionaryValue* initial_split_value, 177 const base::DictionaryValue* initial_split_value,
169 std::vector<std::string>* invalid_keys) const { 178 std::vector<std::string>* invalid_keys) const {
170 DCHECK(invalid_keys && invalid_keys->empty()); 179 DCHECK(invalid_keys && invalid_keys->empty());
171 180
172 std::map<std::string, std::string> split_macs; 181 std::map<std::string, std::string> split_macs;
173 const bool has_hashes = GetSplitMacs(path, &split_macs); 182 const bool has_hashes = GetSplitMacs(path, &split_macs);
174 183
175 // Treat NULL and empty the same; otherwise we would need to store a hash 184 // Treat NULL and empty the same; otherwise we would need to store a hash
176 // for the entire dictionary (or some other special beacon) to 185 // for the entire dictionary (or some other special beacon) to
177 // differentiate these two cases which are really the same for 186 // differentiate these two cases which are really the same for
178 // dictionaries. 187 // dictionaries.
179 if (!initial_split_value || initial_split_value->empty()) 188 if (!initial_split_value || initial_split_value->empty())
180 return has_hashes ? CLEARED : UNCHANGED; 189 return has_hashes ? CLEARED : UNCHANGED;
181 190
182 if (!has_hashes) { 191 if (!has_hashes)
183 return outer_->initial_hashes_dictionary_trusted_ ? 192 return super_mac_valid_ ? TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE;
184 TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE;
185 }
186 193
187 bool has_secure_legacy_id_hashes = false; 194 bool has_secure_legacy_id_hashes = false;
188 std::string keyed_path(path); 195 std::string keyed_path(path);
189 keyed_path.push_back('.'); 196 keyed_path.push_back('.');
190 const size_t common_part_length = keyed_path.length(); 197 const size_t common_part_length = keyed_path.length();
191 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); 198 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd();
192 it.Advance()) { 199 it.Advance()) {
193 std::map<std::string, std::string>::iterator entry = 200 std::map<std::string, std::string>::iterator entry =
194 split_macs.find(it.key()); 201 split_macs.find(it.key());
195 if (entry == split_macs.end()) { 202 if (entry == split_macs.end()) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 234
228 // Anything left in the map is missing from the data. 235 // Anything left in the map is missing from the data.
229 for (std::map<std::string, std::string>::const_iterator it = 236 for (std::map<std::string, std::string>::const_iterator it =
230 split_macs.begin(); 237 split_macs.begin();
231 it != split_macs.end(); 238 it != split_macs.end();
232 ++it) { 239 ++it) {
233 invalid_keys->push_back(it->first); 240 invalid_keys->push_back(it->first);
234 } 241 }
235 242
236 return invalid_keys->empty() 243 return invalid_keys->empty()
237 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) 244 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED)
238 : CHANGED; 245 : CHANGED;
239 } 246 }
240 247
241 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( 248 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash(
242 const std::string& path, 249 const std::string& path,
243 const base::DictionaryValue* split_value) { 250 const base::DictionaryValue* split_value) {
244 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary = 251 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary =
245 outer_->contents_->GetMutableContents(); 252 contents_.GetMutableContents();
246 (*mutable_dictionary)->Remove(path, NULL); 253 (*mutable_dictionary)->Remove(path, NULL);
247 254
248 if (split_value) { 255 if (split_value) {
249 std::string keyed_path(path); 256 std::string keyed_path(path);
250 keyed_path.push_back('.'); 257 keyed_path.push_back('.');
251 const size_t common_part_length = keyed_path.length(); 258 const size_t common_part_length = keyed_path.length();
252 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); 259 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd();
253 it.Advance()) { 260 it.Advance()) {
254 // Keep the common part from the old |keyed_path| and replace the key to 261 // Keep the common part from the old |keyed_path| and replace the key to
255 // get the new |keyed_path|. 262 // get the new |keyed_path|.
256 keyed_path.replace(common_part_length, std::string::npos, it.key()); 263 keyed_path.replace(common_part_length, std::string::npos, it.key());
257 (*mutable_dictionary)->SetString( 264 (*mutable_dictionary)->SetString(
258 keyed_path, 265 keyed_path,
259 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); 266 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value()));
260 } 267 }
261 } 268 }
262 has_changed_ = true; 269 super_mac_dirty_ = true;
263 } 270 }
264 271
265 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs( 272 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs(
266 const std::string& key, 273 const std::string& key,
267 std::map<std::string, std::string>* split_macs) const { 274 std::map<std::string, std::string>* split_macs) const {
268 DCHECK(split_macs); 275 DCHECK(split_macs);
269 DCHECK(split_macs->empty()); 276 DCHECK(split_macs->empty());
270 277
271 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); 278 const base::DictionaryValue* hashed_prefs = contents_.GetContents();
272 const base::DictionaryValue* split_mac_dictionary = NULL; 279 const base::DictionaryValue* split_mac_dictionary = NULL;
273 if (!hashed_prefs || !hashed_prefs->GetDictionary(key, &split_mac_dictionary)) 280 if (!hashed_prefs || !hashed_prefs->GetDictionary(key, &split_mac_dictionary))
274 return false; 281 return false;
275 for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd(); 282 for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd();
276 it.Advance()) { 283 it.Advance()) {
277 std::string mac_string; 284 std::string mac_string;
278 if (!it.value().GetAsString(&mac_string)) { 285 if (!it.value().GetAsString(&mac_string)) {
279 NOTREACHED(); 286 NOTREACHED();
280 continue; 287 continue;
281 } 288 }
282 split_macs->insert(make_pair(it.key(), mac_string)); 289 split_macs->insert(make_pair(it.key(), mac_string));
283 } 290 }
284 return true; 291 return true;
285 } 292 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_hash_store_impl.h ('k') | chrome/browser/prefs/pref_hash_store_transaction.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698