OLD | NEW |
---|---|
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/hash_store_contents.h" |
12 | 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 | |
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 scoped_ptr<HashStoreContents> 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 ValueState CheckSplitValue( | 27 virtual ValueState CheckSplitValue( |
44 const std::string& path, | 28 const std::string& path, |
45 const base::DictionaryValue* initial_split_value, | 29 const base::DictionaryValue* initial_split_value, |
46 std::vector<std::string>* invalid_keys) const OVERRIDE; | 30 std::vector<std::string>* invalid_keys) const OVERRIDE; |
47 virtual void StoreSplitHash( | 31 virtual void StoreSplitHash( |
48 const std::string& path, | 32 const std::string& path, |
49 const base::DictionaryValue* split_value) OVERRIDE; | 33 const base::DictionaryValue* split_value) OVERRIDE; |
34 virtual bool HasHash(const std::string& path) const OVERRIDE; | |
35 virtual void ImportHash(const std::string& path, | |
36 const base::Value* hash) OVERRIDE; | |
37 virtual void ClearHash(const std::string& path) OVERRIDE; | |
38 virtual bool IsSuperMACValid() const OVERRIDE; | |
39 virtual bool StampSuperMac() OVERRIDE; | |
50 | 40 |
51 private: | 41 private: |
52 bool GetSplitMacs(const std::string& path, | 42 bool GetSplitMacs(const std::string& path, |
53 std::map<std::string, std::string>* split_macs) const; | 43 std::map<std::string, std::string>* split_macs) const; |
44 | |
45 HashStoreContents* contents() { | |
46 return outer_->legacy_hash_store_contents_ | |
47 ? outer_->legacy_hash_store_contents_.get() | |
48 : contents_.get(); | |
49 } | |
50 | |
51 const HashStoreContents* contents() const { | |
52 return outer_->legacy_hash_store_contents_ | |
53 ? outer_->legacy_hash_store_contents_.get() | |
54 : contents_.get(); | |
55 } | |
56 | |
54 PrefHashStoreImpl* outer_; | 57 PrefHashStoreImpl* outer_; |
55 bool has_changed_; | 58 scoped_ptr<HashStoreContents> contents_; |
59 | |
60 bool super_mac_valid_; | |
61 bool super_mac_dirty_; | |
56 | 62 |
57 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); | 63 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); |
58 }; | 64 }; |
59 | 65 |
60 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, | 66 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, |
61 const std::string& device_id, | 67 const std::string& device_id, |
62 scoped_ptr<HashStoreContents> contents, | |
63 bool use_super_mac) | 68 bool use_super_mac) |
64 : pref_hash_calculator_(seed, device_id), | 69 : pref_hash_calculator_(seed, device_id), |
65 contents_(contents.Pass()), | 70 use_super_mac_(use_super_mac) { |
66 initial_hashes_dictionary_trusted_( | |
67 use_super_mac | |
68 ? IsHashDictionaryTrusted(pref_hash_calculator_, *contents_) | |
69 : false), | |
70 use_super_mac_(use_super_mac), | |
71 has_pending_write_(false) { | |
72 DCHECK(contents_); | |
73 UMA_HISTOGRAM_BOOLEAN("Settings.HashesDictionaryTrusted", | |
74 initial_hashes_dictionary_trusted_); | |
75 } | 71 } |
76 | 72 |
77 PrefHashStoreImpl::~PrefHashStoreImpl() {} | 73 PrefHashStoreImpl::~PrefHashStoreImpl() { |
78 | |
79 void PrefHashStoreImpl::Reset() { | |
80 contents_->Reset(); | |
81 } | 74 } |
82 | 75 |
83 scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction() { | 76 void PrefHashStoreImpl::set_legacy_hash_store_contents( |
84 return scoped_ptr<PrefHashStoreTransaction>( | 77 scoped_ptr<HashStoreContents> legacy_hash_store_contents) { |
85 new PrefHashStoreTransactionImpl(this)); | 78 legacy_hash_store_contents_ = legacy_hash_store_contents.Pass(); |
86 } | 79 } |
87 | 80 |
88 void PrefHashStoreImpl::CommitPendingWrite() { | 81 scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction( |
89 if (has_pending_write_) { | 82 scoped_ptr<HashStoreContents> storage) { |
90 contents_->CommitPendingWrite(); | 83 return scoped_ptr<PrefHashStoreTransaction>( |
91 has_pending_write_ = false; | 84 new PrefHashStoreTransactionImpl(this, storage.Pass())); |
92 } | |
93 } | 85 } |
94 | 86 |
95 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( | 87 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( |
96 PrefHashStoreImpl* outer) : outer_(outer), has_changed_(false) { | 88 PrefHashStoreImpl* outer, |
89 scoped_ptr<HashStoreContents> storage) | |
90 : outer_(outer), | |
91 contents_(storage.Pass()), | |
92 super_mac_valid_(false), | |
93 super_mac_dirty_(false) { | |
94 if (outer_->use_super_mac_) { | |
95 const base::DictionaryValue* store_contents = contents()->GetContents(); | |
96 std::string super_mac = contents()->GetSuperMac(); | |
97 // The store must be initialized and have a valid super MAC to be trusted. | |
98 super_mac_valid_ = | |
99 store_contents && !super_mac.empty() && | |
100 outer_->pref_hash_calculator_.Validate( | |
101 contents()->hash_store_id(), store_contents, super_mac) == | |
102 PrefHashCalculator::VALID; | |
gab
2014/06/17 02:00:05
I find this indent weird, I'd prefer wrapping "sup
erikwright (departed)
2014/06/17 19:07:23
Done.
| |
103 } | |
97 } | 104 } |
98 | 105 |
99 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: | 106 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: |
100 ~PrefHashStoreTransactionImpl() { | 107 ~PrefHashStoreTransactionImpl() { |
101 // Update the super MAC if and only if the hashes dictionary has been | 108 if (super_mac_dirty_ && outer_->use_super_mac_) { |
102 // modified in this transaction. | 109 // Get the dictionary of hashes (or NULL if it doesn't exist). |
103 if (has_changed_) { | 110 const base::DictionaryValue* hashes_dict = contents()->GetContents(); |
104 if (outer_->use_super_mac_) { | 111 contents()->SetSuperMac(outer_->pref_hash_calculator_.Calculate( |
105 // Get the dictionary of hashes (or NULL if it doesn't exist). | 112 contents()->hash_store_id(), hashes_dict)); |
106 const base::DictionaryValue* hashes_dict = | |
107 outer_->contents_->GetContents(); | |
108 outer_->contents_->SetSuperMac(outer_->pref_hash_calculator_.Calculate( | |
109 outer_->contents_->hash_store_id(), hashes_dict)); | |
110 } | |
111 outer_->has_pending_write_ = true; | |
112 } | 113 } |
113 | |
114 } | 114 } |
115 | 115 |
116 PrefHashStoreTransaction::ValueState | 116 PrefHashStoreTransaction::ValueState |
117 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( | 117 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( |
118 const std::string& path, const base::Value* initial_value) const { | 118 const std::string& path, |
119 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); | 119 const base::Value* initial_value) const { |
120 const base::DictionaryValue* hashed_prefs = contents()->GetContents(); | |
120 | 121 |
121 std::string last_hash; | 122 std::string last_hash; |
122 if (hashed_prefs) | 123 if (hashed_prefs) |
123 hashed_prefs->GetString(path, &last_hash); | 124 hashed_prefs->GetString(path, &last_hash); |
124 | 125 |
125 if (last_hash.empty()) { | 126 if (last_hash.empty()) { |
126 // In the absence of a hash for this pref, always trust a NULL value, but | 127 // In the absence of a hash for this pref, always trust a NULL value, but |
127 // only trust an existing value if the initial hashes dictionary is trusted. | 128 // only trust an existing value if the initial hashes dictionary is trusted. |
128 return (!initial_value || outer_->initial_hashes_dictionary_trusted_) ? | 129 return (!initial_value || super_mac_valid_) ? TRUSTED_UNKNOWN_VALUE |
129 TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE; | 130 : UNTRUSTED_UNKNOWN_VALUE; |
130 } | 131 } |
131 | 132 |
132 PrefHashCalculator::ValidationResult validation_result = | 133 PrefHashCalculator::ValidationResult validation_result = |
133 outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash); | 134 outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash); |
134 switch (validation_result) { | 135 switch (validation_result) { |
135 case PrefHashCalculator::VALID: | 136 case PrefHashCalculator::VALID: |
136 return UNCHANGED; | 137 return UNCHANGED; |
137 case PrefHashCalculator::VALID_WEAK_LEGACY: | 138 case PrefHashCalculator::VALID_WEAK_LEGACY: |
138 return WEAK_LEGACY; | 139 return WEAK_LEGACY; |
139 case PrefHashCalculator::VALID_SECURE_LEGACY: | 140 case PrefHashCalculator::VALID_SECURE_LEGACY: |
140 return SECURE_LEGACY; | 141 return SECURE_LEGACY; |
141 case PrefHashCalculator::INVALID: | 142 case PrefHashCalculator::INVALID: |
142 return initial_value ? CHANGED : CLEARED; | 143 return initial_value ? CHANGED : CLEARED; |
143 } | 144 } |
144 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " | 145 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " |
145 << validation_result; | 146 << validation_result; |
146 return UNTRUSTED_UNKNOWN_VALUE; | 147 return UNTRUSTED_UNKNOWN_VALUE; |
147 } | 148 } |
148 | 149 |
149 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( | 150 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( |
150 const std::string& path, const base::Value* new_value) { | 151 const std::string& path, |
152 const base::Value* new_value) { | |
151 const std::string mac = | 153 const std::string mac = |
152 outer_->pref_hash_calculator_.Calculate(path, new_value); | 154 outer_->pref_hash_calculator_.Calculate(path, new_value); |
153 (*outer_->contents_->GetMutableContents())->SetString(path, mac); | 155 (*contents()->GetMutableContents())->SetString(path, mac); |
154 has_changed_ = true; | 156 super_mac_dirty_ = true; |
155 } | 157 } |
156 | 158 |
157 PrefHashStoreTransaction::ValueState | 159 PrefHashStoreTransaction::ValueState |
158 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( | 160 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( |
159 const std::string& path, | 161 const std::string& path, |
160 const base::DictionaryValue* initial_split_value, | 162 const base::DictionaryValue* initial_split_value, |
161 std::vector<std::string>* invalid_keys) const { | 163 std::vector<std::string>* invalid_keys) const { |
162 DCHECK(invalid_keys && invalid_keys->empty()); | 164 DCHECK(invalid_keys && invalid_keys->empty()); |
163 | 165 |
164 std::map<std::string, std::string> split_macs; | 166 std::map<std::string, std::string> split_macs; |
165 const bool has_hashes = GetSplitMacs(path, &split_macs); | 167 const bool has_hashes = GetSplitMacs(path, &split_macs); |
166 | 168 |
167 // Treat NULL and empty the same; otherwise we would need to store a hash | 169 // Treat NULL and empty the same; otherwise we would need to store a hash |
168 // for the entire dictionary (or some other special beacon) to | 170 // for the entire dictionary (or some other special beacon) to |
169 // differentiate these two cases which are really the same for | 171 // differentiate these two cases which are really the same for |
170 // dictionaries. | 172 // dictionaries. |
171 if (!initial_split_value || initial_split_value->empty()) | 173 if (!initial_split_value || initial_split_value->empty()) |
172 return has_hashes ? CLEARED : UNCHANGED; | 174 return has_hashes ? CLEARED : UNCHANGED; |
173 | 175 |
174 if (!has_hashes) { | 176 if (!has_hashes) |
175 return outer_->initial_hashes_dictionary_trusted_ ? | 177 return super_mac_valid_ ? TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE; |
176 TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE; | |
177 } | |
178 | 178 |
179 bool has_secure_legacy_id_hashes = false; | 179 bool has_secure_legacy_id_hashes = false; |
180 std::string keyed_path(path); | 180 std::string keyed_path(path); |
181 keyed_path.push_back('.'); | 181 keyed_path.push_back('.'); |
182 const size_t common_part_length = keyed_path.length(); | 182 const size_t common_part_length = keyed_path.length(); |
183 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); | 183 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); |
184 it.Advance()) { | 184 it.Advance()) { |
185 std::map<std::string, std::string>::iterator entry = | 185 std::map<std::string, std::string>::iterator entry = |
186 split_macs.find(it.key()); | 186 split_macs.find(it.key()); |
187 if (entry == split_macs.end()) { | 187 if (entry == split_macs.end()) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 | 219 |
220 // Anything left in the map is missing from the data. | 220 // Anything left in the map is missing from the data. |
221 for (std::map<std::string, std::string>::const_iterator it = | 221 for (std::map<std::string, std::string>::const_iterator it = |
222 split_macs.begin(); | 222 split_macs.begin(); |
223 it != split_macs.end(); | 223 it != split_macs.end(); |
224 ++it) { | 224 ++it) { |
225 invalid_keys->push_back(it->first); | 225 invalid_keys->push_back(it->first); |
226 } | 226 } |
227 | 227 |
228 return invalid_keys->empty() | 228 return invalid_keys->empty() |
229 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) | 229 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) |
230 : CHANGED; | 230 : CHANGED; |
231 } | 231 } |
232 | 232 |
233 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( | 233 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( |
234 const std::string& path, | 234 const std::string& path, |
235 const base::DictionaryValue* split_value) { | 235 const base::DictionaryValue* split_value) { |
236 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary = | 236 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary = |
237 outer_->contents_->GetMutableContents(); | 237 contents()->GetMutableContents(); |
238 (*mutable_dictionary)->Remove(path, NULL); | 238 (*mutable_dictionary)->Remove(path, NULL); |
239 | 239 |
240 if (split_value) { | 240 if (split_value) { |
241 std::string keyed_path(path); | 241 std::string keyed_path(path); |
242 keyed_path.push_back('.'); | 242 keyed_path.push_back('.'); |
243 const size_t common_part_length = keyed_path.length(); | 243 const size_t common_part_length = keyed_path.length(); |
244 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); | 244 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); |
245 it.Advance()) { | 245 it.Advance()) { |
246 // Keep the common part from the old |keyed_path| and replace the key to | 246 // Keep the common part from the old |keyed_path| and replace the key to |
247 // get the new |keyed_path|. | 247 // get the new |keyed_path|. |
248 keyed_path.replace(common_part_length, std::string::npos, it.key()); | 248 keyed_path.replace(common_part_length, std::string::npos, it.key()); |
249 (*mutable_dictionary)->SetString( | 249 (*mutable_dictionary)->SetString( |
250 keyed_path, | 250 keyed_path, |
251 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); | 251 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); |
252 } | 252 } |
253 } | 253 } |
254 has_changed_ = true; | 254 super_mac_dirty_ = true; |
255 } | 255 } |
256 | 256 |
257 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs( | 257 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs( |
258 const std::string& key, | 258 const std::string& key, |
259 std::map<std::string, std::string>* split_macs) const { | 259 std::map<std::string, std::string>* split_macs) const { |
260 DCHECK(split_macs); | 260 DCHECK(split_macs); |
261 DCHECK(split_macs->empty()); | 261 DCHECK(split_macs->empty()); |
262 | 262 |
263 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); | 263 const base::DictionaryValue* hashed_prefs = contents()->GetContents(); |
264 const base::DictionaryValue* split_mac_dictionary = NULL; | 264 const base::DictionaryValue* split_mac_dictionary = NULL; |
265 if (!hashed_prefs || !hashed_prefs->GetDictionary(key, &split_mac_dictionary)) | 265 if (!hashed_prefs || !hashed_prefs->GetDictionary(key, &split_mac_dictionary)) |
266 return false; | 266 return false; |
267 for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd(); | 267 for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd(); |
268 it.Advance()) { | 268 it.Advance()) { |
269 std::string mac_string; | 269 std::string mac_string; |
270 if (!it.value().GetAsString(&mac_string)) { | 270 if (!it.value().GetAsString(&mac_string)) { |
271 NOTREACHED(); | 271 NOTREACHED(); |
272 continue; | 272 continue; |
273 } | 273 } |
274 split_macs->insert(make_pair(it.key(), mac_string)); | 274 split_macs->insert(make_pair(it.key(), mac_string)); |
275 } | 275 } |
276 return true; | 276 return true; |
277 } | 277 } |
278 | |
279 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash( | |
280 const std::string& path) const { | |
281 const base::DictionaryValue* hashed_prefs = contents()->GetContents(); | |
gab
2014/06/17 02:00:05
s/hashed_prefs/hashes_dict/
for consistency with
erikwright (departed)
2014/06/17 19:07:23
Done.
| |
282 return hashed_prefs && hashed_prefs->Get(path, NULL); | |
283 } | |
284 | |
285 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ImportHash( | |
286 const std::string& path, | |
287 const base::Value* hash) { | |
288 if (hash) | |
289 (*contents()->GetMutableContents())->Set(path, hash->DeepCopy()); | |
290 else | |
291 (*contents()->GetMutableContents())->RemovePath(path, NULL); | |
292 | |
293 if (super_mac_valid_) | |
294 super_mac_dirty_ = true; | |
295 } | |
296 | |
297 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ClearHash( | |
298 const std::string& path) { | |
299 if ((*contents()->GetMutableContents())->RemovePath(path, NULL) && | |
300 super_mac_valid_) { | |
301 super_mac_dirty_ = true; | |
302 } | |
303 } | |
304 | |
305 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { | |
306 return super_mac_valid_; | |
307 } | |
308 | |
309 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { | |
310 if (!outer_->use_super_mac_ || super_mac_valid_) | |
311 return false; | |
312 super_mac_dirty_ = true; | |
313 return true; | |
314 } | |
OLD | NEW |