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

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: Remove some now unnecessary changes. 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;
27 virtual bool StampSuperMac() OVERRIDE;
43 virtual ValueState CheckSplitValue( 28 virtual ValueState CheckSplitValue(
44 const std::string& path, 29 const std::string& path,
45 const base::DictionaryValue* initial_split_value, 30 const base::DictionaryValue* initial_split_value,
46 std::vector<std::string>* invalid_keys) const OVERRIDE; 31 std::vector<std::string>* invalid_keys) const OVERRIDE;
47 virtual void StoreSplitHash( 32 virtual void StoreSplitHash(
48 const std::string& path, 33 const std::string& path,
49 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;
50 39
51 private: 40 private:
52 bool GetSplitMacs(const std::string& path, 41 bool GetSplitMacs(const std::string& path,
53 std::map<std::string, std::string>* split_macs) const; 42 std::map<std::string, std::string>* split_macs) const;
43
54 PrefHashStoreImpl* outer_; 44 PrefHashStoreImpl* outer_;
55 bool has_changed_; 45 DictionaryHashStoreContents contents_;
46
47 bool super_mac_valid_;
48 bool super_mac_dirty_;
56 49
57 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); 50 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl);
58 }; 51 };
59 52
60 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, 53 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed,
61 const std::string& device_id, 54 const std::string& device_id,
62 scoped_ptr<HashStoreContents> contents,
63 bool use_super_mac) 55 bool use_super_mac)
64 : pref_hash_calculator_(seed, device_id), 56 : pref_hash_calculator_(seed, device_id),
65 contents_(contents.Pass()), 57 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",
gab 2014/06/13 01:57:43 Regarding what to do with this histogram: I think
74 initial_hashes_dictionary_trusted_);
75 } 58 }
76 59
77 PrefHashStoreImpl::~PrefHashStoreImpl() {} 60 PrefHashStoreImpl::~PrefHashStoreImpl() {
78
79 void PrefHashStoreImpl::Reset() {
80 contents_->Reset();
81 } 61 }
82 62
83 scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction() { 63 scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction(
64 base::DictionaryValue* storage) {
84 return scoped_ptr<PrefHashStoreTransaction>( 65 return scoped_ptr<PrefHashStoreTransaction>(
85 new PrefHashStoreTransactionImpl(this)); 66 new PrefHashStoreTransactionImpl(this, storage));
86 }
87
88 void PrefHashStoreImpl::CommitPendingWrite() {
89 if (has_pending_write_) {
90 contents_->CommitPendingWrite();
91 has_pending_write_ = false;
92 }
93 } 67 }
94 68
95 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( 69 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl(
96 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_ =
gab 2014/06/13 01:57:43 So if a previous transaction stamped a super MAC,
erikwright (departed) 2014/06/16 20:51:26 Yeah, there is kind of limited choice. I don't thi
gab 2014/06/17 02:00:05 How about adding a PrefHashFilter::filter_on_load_
erikwright (departed) 2014/06/17 17:54:05 Personally, I'm not worried about a single instanc
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 }
97 } 86 }
98 87
99 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: 88 PrefHashStoreImpl::PrefHashStoreTransactionImpl::
100 ~PrefHashStoreTransactionImpl() { 89 ~PrefHashStoreTransactionImpl() {
101 // 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
102 // modified in this transaction. 91 // modified in this transaction.
gab 2014/06/13 01:57:43 Remove the comment above, the new variable name ma
erikwright (departed) 2014/06/16 20:51:26 Done.
103 if (has_changed_) { 92 if (super_mac_dirty_ && outer_->use_super_mac_) {
104 if (outer_->use_super_mac_) { 93 // Get the dictionary of hashes (or NULL if it doesn't exist).
105 // Get the dictionary of hashes (or NULL if it doesn't exist). 94 const base::DictionaryValue* hashes_dict = contents_.GetContents();
106 const base::DictionaryValue* hashes_dict = 95 contents_.SetSuperMac(outer_->pref_hash_calculator_.Calculate(
107 outer_->contents_->GetContents(); 96 contents_.hash_store_id(), hashes_dict));
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 } 97 }
113
114 } 98 }
115 99
116 PrefHashStoreTransaction::ValueState 100 PrefHashStoreTransaction::ValueState
117 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( 101 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue(
118 const std::string& path, const base::Value* initial_value) const { 102 const std::string& path,
119 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); 103 const base::Value* initial_value) const {
104 const base::DictionaryValue* hashed_prefs = contents_.GetContents();
120 105
121 std::string last_hash; 106 std::string last_hash;
122 if (hashed_prefs) 107 if (hashed_prefs)
123 hashed_prefs->GetString(path, &last_hash); 108 hashed_prefs->GetString(path, &last_hash);
124 109
125 if (last_hash.empty()) { 110 if (last_hash.empty()) {
126 // 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
127 // 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.
128 return (!initial_value || outer_->initial_hashes_dictionary_trusted_) ? 113 return (!initial_value || super_mac_valid_) ? TRUSTED_UNKNOWN_VALUE
129 TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE; 114 : UNTRUSTED_UNKNOWN_VALUE;
130 } 115 }
131 116
132 PrefHashCalculator::ValidationResult validation_result = 117 PrefHashCalculator::ValidationResult validation_result =
133 outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash); 118 outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash);
134 switch (validation_result) { 119 switch (validation_result) {
135 case PrefHashCalculator::VALID: 120 case PrefHashCalculator::VALID:
136 return UNCHANGED; 121 return UNCHANGED;
137 case PrefHashCalculator::VALID_WEAK_LEGACY: 122 case PrefHashCalculator::VALID_WEAK_LEGACY:
138 return WEAK_LEGACY; 123 return WEAK_LEGACY;
139 case PrefHashCalculator::VALID_SECURE_LEGACY: 124 case PrefHashCalculator::VALID_SECURE_LEGACY:
140 return SECURE_LEGACY; 125 return SECURE_LEGACY;
141 case PrefHashCalculator::INVALID: 126 case PrefHashCalculator::INVALID:
142 return initial_value ? CHANGED : CLEARED; 127 return initial_value ? CHANGED : CLEARED;
143 } 128 }
144 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " 129 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: "
145 << validation_result; 130 << validation_result;
146 return UNTRUSTED_UNKNOWN_VALUE; 131 return UNTRUSTED_UNKNOWN_VALUE;
147 } 132 }
148 133
149 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( 134 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash(
150 const std::string& path, const base::Value* new_value) { 135 const std::string& path,
136 const base::Value* new_value) {
151 const std::string mac = 137 const std::string mac =
152 outer_->pref_hash_calculator_.Calculate(path, new_value); 138 outer_->pref_hash_calculator_.Calculate(path, new_value);
153 (*outer_->contents_->GetMutableContents())->SetString(path, mac); 139 (*contents_.GetMutableContents())->SetString(path, mac);
154 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)
gab 2014/06/13 01:57:43 Start if/else block with the positive condition wh
erikwright (departed) 2014/06/16 20:51:26 Done.
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_;
gab 2014/06/13 01:57:43 I find: if (super_mac_valid_) super_mac_dirty_
erikwright (departed) 2014/06/16 20:51:26 Done.
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 }
165 }
166
167 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() {
168 if (!outer_->use_super_mac_ || super_mac_valid_)
169 return false;
170 super_mac_dirty_ = true;
171 return true;
155 } 172 }
156 173
157 PrefHashStoreTransaction::ValueState 174 PrefHashStoreTransaction::ValueState
158 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( 175 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue(
159 const std::string& path, 176 const std::string& path,
160 const base::DictionaryValue* initial_split_value, 177 const base::DictionaryValue* initial_split_value,
161 std::vector<std::string>* invalid_keys) const { 178 std::vector<std::string>* invalid_keys) const {
162 DCHECK(invalid_keys && invalid_keys->empty()); 179 DCHECK(invalid_keys && invalid_keys->empty());
163 180
164 std::map<std::string, std::string> split_macs; 181 std::map<std::string, std::string> split_macs;
165 const bool has_hashes = GetSplitMacs(path, &split_macs); 182 const bool has_hashes = GetSplitMacs(path, &split_macs);
166 183
167 // 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
168 // for the entire dictionary (or some other special beacon) to 185 // for the entire dictionary (or some other special beacon) to
169 // differentiate these two cases which are really the same for 186 // differentiate these two cases which are really the same for
170 // dictionaries. 187 // dictionaries.
171 if (!initial_split_value || initial_split_value->empty()) 188 if (!initial_split_value || initial_split_value->empty())
172 return has_hashes ? CLEARED : UNCHANGED; 189 return has_hashes ? CLEARED : UNCHANGED;
173 190
174 if (!has_hashes) { 191 if (!has_hashes)
175 return outer_->initial_hashes_dictionary_trusted_ ? 192 return super_mac_valid_ ? TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE;
176 TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE;
177 }
178 193
179 bool has_secure_legacy_id_hashes = false; 194 bool has_secure_legacy_id_hashes = false;
180 std::string keyed_path(path); 195 std::string keyed_path(path);
181 keyed_path.push_back('.'); 196 keyed_path.push_back('.');
182 const size_t common_part_length = keyed_path.length(); 197 const size_t common_part_length = keyed_path.length();
183 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); 198 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd();
184 it.Advance()) { 199 it.Advance()) {
185 std::map<std::string, std::string>::iterator entry = 200 std::map<std::string, std::string>::iterator entry =
186 split_macs.find(it.key()); 201 split_macs.find(it.key());
187 if (entry == split_macs.end()) { 202 if (entry == split_macs.end()) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 234
220 // Anything left in the map is missing from the data. 235 // Anything left in the map is missing from the data.
221 for (std::map<std::string, std::string>::const_iterator it = 236 for (std::map<std::string, std::string>::const_iterator it =
222 split_macs.begin(); 237 split_macs.begin();
223 it != split_macs.end(); 238 it != split_macs.end();
224 ++it) { 239 ++it) {
225 invalid_keys->push_back(it->first); 240 invalid_keys->push_back(it->first);
226 } 241 }
227 242
228 return invalid_keys->empty() 243 return invalid_keys->empty()
229 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) 244 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED)
230 : CHANGED; 245 : CHANGED;
231 } 246 }
232 247
233 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( 248 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash(
234 const std::string& path, 249 const std::string& path,
235 const base::DictionaryValue* split_value) { 250 const base::DictionaryValue* split_value) {
236 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary = 251 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary =
237 outer_->contents_->GetMutableContents(); 252 contents_.GetMutableContents();
238 (*mutable_dictionary)->Remove(path, NULL); 253 (*mutable_dictionary)->Remove(path, NULL);
239 254
240 if (split_value) { 255 if (split_value) {
241 std::string keyed_path(path); 256 std::string keyed_path(path);
242 keyed_path.push_back('.'); 257 keyed_path.push_back('.');
243 const size_t common_part_length = keyed_path.length(); 258 const size_t common_part_length = keyed_path.length();
244 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); 259 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd();
245 it.Advance()) { 260 it.Advance()) {
246 // 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
247 // get the new |keyed_path|. 262 // get the new |keyed_path|.
248 keyed_path.replace(common_part_length, std::string::npos, it.key()); 263 keyed_path.replace(common_part_length, std::string::npos, it.key());
249 (*mutable_dictionary)->SetString( 264 (*mutable_dictionary)->SetString(
250 keyed_path, 265 keyed_path,
251 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); 266 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value()));
252 } 267 }
253 } 268 }
254 has_changed_ = true; 269 super_mac_dirty_ = true;
255 } 270 }
256 271
257 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs( 272 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs(
258 const std::string& key, 273 const std::string& key,
259 std::map<std::string, std::string>* split_macs) const { 274 std::map<std::string, std::string>* split_macs) const {
260 DCHECK(split_macs); 275 DCHECK(split_macs);
261 DCHECK(split_macs->empty()); 276 DCHECK(split_macs->empty());
262 277
263 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); 278 const base::DictionaryValue* hashed_prefs = contents_.GetContents();
264 const base::DictionaryValue* split_mac_dictionary = NULL; 279 const base::DictionaryValue* split_mac_dictionary = NULL;
265 if (!hashed_prefs || !hashed_prefs->GetDictionary(key, &split_mac_dictionary)) 280 if (!hashed_prefs || !hashed_prefs->GetDictionary(key, &split_mac_dictionary))
266 return false; 281 return false;
267 for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd(); 282 for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd();
268 it.Advance()) { 283 it.Advance()) {
269 std::string mac_string; 284 std::string mac_string;
270 if (!it.value().GetAsString(&mac_string)) { 285 if (!it.value().GetAsString(&mac_string)) {
271 NOTREACHED(); 286 NOTREACHED();
272 continue; 287 continue;
273 } 288 }
274 split_macs->insert(make_pair(it.key(), mac_string)); 289 split_macs->insert(make_pair(it.key(), mac_string));
275 } 290 }
276 return true; 291 return true;
277 } 292 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698