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

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: Add tracking for MAC migrations from local state. 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/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 return;
96
97 // The store must be initialized and have a valid super MAC to be trusted.
98
99 const base::DictionaryValue* store_contents = contents()->GetContents();
100 if (!store_contents)
101 return;
102
103 std::string super_mac = contents()->GetSuperMac();
104 if (super_mac.empty())
105 return;
106
107 super_mac_valid_ =
108 outer_->pref_hash_calculator_.Validate(
109 contents()->hash_store_id(), store_contents, super_mac) ==
110 PrefHashCalculator::VALID;
97 } 111 }
98 112
99 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: 113 PrefHashStoreImpl::PrefHashStoreTransactionImpl::
100 ~PrefHashStoreTransactionImpl() { 114 ~PrefHashStoreTransactionImpl() {
101 // Update the super MAC if and only if the hashes dictionary has been 115 if (super_mac_dirty_ && outer_->use_super_mac_) {
102 // modified in this transaction. 116 // Get the dictionary of hashes (or NULL if it doesn't exist).
103 if (has_changed_) { 117 const base::DictionaryValue* hashes_dict = contents()->GetContents();
104 if (outer_->use_super_mac_) { 118 contents()->SetSuperMac(outer_->pref_hash_calculator_.Calculate(
105 // Get the dictionary of hashes (or NULL if it doesn't exist). 119 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 } 120 }
113
114 } 121 }
115 122
116 PrefHashStoreTransaction::ValueState 123 PrefHashStoreTransaction::ValueState
117 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( 124 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue(
118 const std::string& path, const base::Value* initial_value) const { 125 const std::string& path,
119 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); 126 const base::Value* initial_value) const {
127 const base::DictionaryValue* hashes_dict = contents()->GetContents();
120 128
121 std::string last_hash; 129 std::string last_hash;
122 if (hashed_prefs) 130 if (hashes_dict)
123 hashed_prefs->GetString(path, &last_hash); 131 hashes_dict->GetString(path, &last_hash);
124 132
125 if (last_hash.empty()) { 133 if (last_hash.empty()) {
126 // In the absence of a hash for this pref, always trust a NULL value, but 134 // 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. 135 // only trust an existing value if the initial hashes dictionary is trusted.
128 return (!initial_value || outer_->initial_hashes_dictionary_trusted_) ? 136 return (!initial_value || super_mac_valid_) ? TRUSTED_UNKNOWN_VALUE
129 TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE; 137 : UNTRUSTED_UNKNOWN_VALUE;
130 } 138 }
131 139
132 PrefHashCalculator::ValidationResult validation_result = 140 PrefHashCalculator::ValidationResult validation_result =
133 outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash); 141 outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash);
134 switch (validation_result) { 142 switch (validation_result) {
135 case PrefHashCalculator::VALID: 143 case PrefHashCalculator::VALID:
136 return UNCHANGED; 144 return UNCHANGED;
137 case PrefHashCalculator::VALID_WEAK_LEGACY: 145 case PrefHashCalculator::VALID_WEAK_LEGACY:
138 return WEAK_LEGACY; 146 return WEAK_LEGACY;
139 case PrefHashCalculator::VALID_SECURE_LEGACY: 147 case PrefHashCalculator::VALID_SECURE_LEGACY:
140 return SECURE_LEGACY; 148 return SECURE_LEGACY;
141 case PrefHashCalculator::INVALID: 149 case PrefHashCalculator::INVALID:
142 return initial_value ? CHANGED : CLEARED; 150 return initial_value ? CHANGED : CLEARED;
143 } 151 }
144 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " 152 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: "
145 << validation_result; 153 << validation_result;
146 return UNTRUSTED_UNKNOWN_VALUE; 154 return UNTRUSTED_UNKNOWN_VALUE;
147 } 155 }
148 156
149 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( 157 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash(
150 const std::string& path, const base::Value* new_value) { 158 const std::string& path,
159 const base::Value* new_value) {
151 const std::string mac = 160 const std::string mac =
152 outer_->pref_hash_calculator_.Calculate(path, new_value); 161 outer_->pref_hash_calculator_.Calculate(path, new_value);
153 (*outer_->contents_->GetMutableContents())->SetString(path, mac); 162 (*contents()->GetMutableContents())->SetString(path, mac);
154 has_changed_ = true; 163 super_mac_dirty_ = true;
155 } 164 }
156 165
157 PrefHashStoreTransaction::ValueState 166 PrefHashStoreTransaction::ValueState
158 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( 167 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue(
159 const std::string& path, 168 const std::string& path,
160 const base::DictionaryValue* initial_split_value, 169 const base::DictionaryValue* initial_split_value,
161 std::vector<std::string>* invalid_keys) const { 170 std::vector<std::string>* invalid_keys) const {
162 DCHECK(invalid_keys && invalid_keys->empty()); 171 DCHECK(invalid_keys && invalid_keys->empty());
163 172
164 std::map<std::string, std::string> split_macs; 173 std::map<std::string, std::string> split_macs;
165 const bool has_hashes = GetSplitMacs(path, &split_macs); 174 const bool has_hashes = GetSplitMacs(path, &split_macs);
166 175
167 // Treat NULL and empty the same; otherwise we would need to store a hash 176 // 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 177 // for the entire dictionary (or some other special beacon) to
169 // differentiate these two cases which are really the same for 178 // differentiate these two cases which are really the same for
170 // dictionaries. 179 // dictionaries.
171 if (!initial_split_value || initial_split_value->empty()) 180 if (!initial_split_value || initial_split_value->empty())
172 return has_hashes ? CLEARED : UNCHANGED; 181 return has_hashes ? CLEARED : UNCHANGED;
173 182
174 if (!has_hashes) { 183 if (!has_hashes)
175 return outer_->initial_hashes_dictionary_trusted_ ? 184 return super_mac_valid_ ? TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE;
176 TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE;
177 }
178 185
179 bool has_secure_legacy_id_hashes = false; 186 bool has_secure_legacy_id_hashes = false;
180 std::string keyed_path(path); 187 std::string keyed_path(path);
181 keyed_path.push_back('.'); 188 keyed_path.push_back('.');
182 const size_t common_part_length = keyed_path.length(); 189 const size_t common_part_length = keyed_path.length();
183 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); 190 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd();
184 it.Advance()) { 191 it.Advance()) {
185 std::map<std::string, std::string>::iterator entry = 192 std::map<std::string, std::string>::iterator entry =
186 split_macs.find(it.key()); 193 split_macs.find(it.key());
187 if (entry == split_macs.end()) { 194 if (entry == split_macs.end()) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 226
220 // Anything left in the map is missing from the data. 227 // Anything left in the map is missing from the data.
221 for (std::map<std::string, std::string>::const_iterator it = 228 for (std::map<std::string, std::string>::const_iterator it =
222 split_macs.begin(); 229 split_macs.begin();
223 it != split_macs.end(); 230 it != split_macs.end();
224 ++it) { 231 ++it) {
225 invalid_keys->push_back(it->first); 232 invalid_keys->push_back(it->first);
226 } 233 }
227 234
228 return invalid_keys->empty() 235 return invalid_keys->empty()
229 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) 236 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED)
230 : CHANGED; 237 : CHANGED;
231 } 238 }
232 239
233 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( 240 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash(
234 const std::string& path, 241 const std::string& path,
235 const base::DictionaryValue* split_value) { 242 const base::DictionaryValue* split_value) {
236 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary = 243 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary =
237 outer_->contents_->GetMutableContents(); 244 contents()->GetMutableContents();
238 (*mutable_dictionary)->Remove(path, NULL); 245 (*mutable_dictionary)->Remove(path, NULL);
239 246
240 if (split_value) { 247 if (split_value) {
241 std::string keyed_path(path); 248 std::string keyed_path(path);
242 keyed_path.push_back('.'); 249 keyed_path.push_back('.');
243 const size_t common_part_length = keyed_path.length(); 250 const size_t common_part_length = keyed_path.length();
244 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); 251 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd();
245 it.Advance()) { 252 it.Advance()) {
246 // Keep the common part from the old |keyed_path| and replace the key to 253 // Keep the common part from the old |keyed_path| and replace the key to
247 // get the new |keyed_path|. 254 // get the new |keyed_path|.
248 keyed_path.replace(common_part_length, std::string::npos, it.key()); 255 keyed_path.replace(common_part_length, std::string::npos, it.key());
249 (*mutable_dictionary)->SetString( 256 (*mutable_dictionary)->SetString(
250 keyed_path, 257 keyed_path,
251 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); 258 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value()));
252 } 259 }
253 } 260 }
254 has_changed_ = true; 261 super_mac_dirty_ = true;
255 } 262 }
256 263
257 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs( 264 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs(
258 const std::string& key, 265 const std::string& key,
259 std::map<std::string, std::string>* split_macs) const { 266 std::map<std::string, std::string>* split_macs) const {
260 DCHECK(split_macs); 267 DCHECK(split_macs);
261 DCHECK(split_macs->empty()); 268 DCHECK(split_macs->empty());
262 269
263 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); 270 const base::DictionaryValue* hashes_dict = contents()->GetContents();
264 const base::DictionaryValue* split_mac_dictionary = NULL; 271 const base::DictionaryValue* split_mac_dictionary = NULL;
265 if (!hashed_prefs || !hashed_prefs->GetDictionary(key, &split_mac_dictionary)) 272 if (!hashes_dict || !hashes_dict->GetDictionary(key, &split_mac_dictionary))
266 return false; 273 return false;
267 for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd(); 274 for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd();
268 it.Advance()) { 275 it.Advance()) {
269 std::string mac_string; 276 std::string mac_string;
270 if (!it.value().GetAsString(&mac_string)) { 277 if (!it.value().GetAsString(&mac_string)) {
271 NOTREACHED(); 278 NOTREACHED();
272 continue; 279 continue;
273 } 280 }
274 split_macs->insert(make_pair(it.key(), mac_string)); 281 split_macs->insert(make_pair(it.key(), mac_string));
275 } 282 }
276 return true; 283 return true;
277 } 284 }
285
286 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash(
287 const std::string& path) const {
288 const base::DictionaryValue* hashes_dict = contents()->GetContents();
289 return hashes_dict && hashes_dict->Get(path, NULL);
290 }
291
292 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ImportHash(
293 const std::string& path,
294 const base::Value* hash) {
295 DCHECK(hash);
296
297 (*contents()->GetMutableContents())->Set(path, hash->DeepCopy());
298
299 if (super_mac_valid_)
300 super_mac_dirty_ = true;
301 }
302
303 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ClearHash(
304 const std::string& path) {
305 if ((*contents()->GetMutableContents())->RemovePath(path, NULL) &&
306 super_mac_valid_) {
307 super_mac_dirty_ = true;
308 }
309 }
310
311 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const {
312 return super_mac_valid_;
313 }
314
315 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() {
316 if (!outer_->use_super_mac_ || super_mac_valid_)
317 return false;
318 super_mac_dirty_ = true;
319 return true;
320 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698