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

Side by Side Diff: components/safe_browsing_db/v4_store.h

Issue 2384893002: PVer4: Test checksum on startup outside the hotpath of DB load (Closed)
Patch Set: Verify that the checksum check happens async Created 4 years, 2 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
7 7
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/sequenced_task_runner.h" 10 #include "base/sequenced_task_runner.h"
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 // If a hash prefix in this store matches |full_hash|, returns that hash 173 // If a hash prefix in this store matches |full_hash|, returns that hash
174 // prefix; otherwise returns an empty hash prefix. 174 // prefix; otherwise returns an empty hash prefix.
175 virtual HashPrefix GetMatchingHashPrefix(const FullHash& full_hash); 175 virtual HashPrefix GetMatchingHashPrefix(const FullHash& full_hash);
176 176
177 std::string DebugString() const; 177 std::string DebugString() const;
178 178
179 // Reads the store file from disk and populates the in-memory representation 179 // Reads the store file from disk and populates the in-memory representation
180 // of the hash prefixes. 180 // of the hash prefixes.
181 void Initialize(); 181 void Initialize();
182 182
183 // Reset internal state and delete the backing file. 183 // Reset internal state.
184 virtual bool Reset(); 184 void Reset();
185
186 // Scheduled after reading the store file from disk on startup. When run, it
187 // ensures that the checksum of the hash prefixes in lexicographical sorted
188 // order matches the expected value in |expected_checksum_|. Returns true if
189 // it matches; false otherwise. Checksum verification can take a long time,
190 // so it is performed outside of the hotpath of loading SafeBrowsing database,
191 // which blocks resource loads.
192 bool VerifyChecksum();
185 193
186 private: 194 private:
187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromEmptyFile); 195 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromEmptyFile);
188 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); 196 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile);
189 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); 197 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile);
190 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); 198 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile);
191 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); 199 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile);
192 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); 200 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile);
193 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); 201 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile);
194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); 202 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 TestHashPrefixExistsInMapWithSingleSize); 247 TestHashPrefixExistsInMapWithSingleSize);
240 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 248 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
241 TestHashPrefixExistsInMapWithDifferentSizes); 249 TestHashPrefixExistsInMapWithDifferentSizes);
242 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 250 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
243 TestHashPrefixDoesNotExistInMapWithDifferentSizes); 251 TestHashPrefixDoesNotExistInMapWithDifferentSizes);
244 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 252 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
245 TestAdditionsWithRiceEncodingFailsWithInvalidInput); 253 TestAdditionsWithRiceEncodingFailsWithInvalidInput);
246 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAdditionsWithRiceEncodingSucceeds); 254 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAdditionsWithRiceEncodingSucceeds);
247 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestRemovalsWithRiceEncodingSucceeds); 255 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestRemovalsWithRiceEncodingSucceeds);
248 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesFailsChecksum); 256 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesFailsChecksum);
257 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestChecksumErrorOnStartup);
249 friend class V4StoreTest; 258 friend class V4StoreTest;
250 259
251 // If |prefix_size| is within expected range, and |raw_hashes_length| is a 260 // If |prefix_size| is within expected range, and |raw_hashes_length| is a
252 // multiple of prefix_size, then it sets the string of length 261 // multiple of prefix_size, then it sets the string of length
253 // |raw_hashes_length| starting at |raw_hashes_begin| as the value at key 262 // |raw_hashes_length| starting at |raw_hashes_begin| as the value at key
254 // |prefix_size| in |additions_map| 263 // |prefix_size| in |additions_map|
255 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, 264 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size,
256 const char* raw_hashes_begin, 265 const char* raw_hashes_begin,
257 const size_t raw_hashes_length, 266 const size_t raw_hashes_length,
258 HashPrefixMap* additions_map); 267 HashPrefixMap* additions_map);
(...skipping 27 matching lines...) Expand all
286 // list is exact. This ignores the space that would otherwise be released by 295 // list is exact. This ignores the space that would otherwise be released by
287 // deletions specified in the update because it is non-trivial to calculate 296 // deletions specified in the update because it is non-trivial to calculate
288 // those deletions upfront. This isn't so bad since deletions are supposed to 297 // those deletions upfront. This isn't so bad since deletions are supposed to
289 // be small and infrequent. 298 // be small and infrequent.
290 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, 299 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map,
291 HashPrefixMap* prefix_map_to_update); 300 HashPrefixMap* prefix_map_to_update);
292 301
293 // Merges the prefix map from the old store (|old_hash_prefix_map|) and the 302 // Merges the prefix map from the old store (|old_hash_prefix_map|) and the
294 // update (additions_map) to populate the prefix map for the current store. 303 // update (additions_map) to populate the prefix map for the current store.
295 // The indices in the |raw_removals| list, which may be NULL, are not merged. 304 // The indices in the |raw_removals| list, which may be NULL, are not merged.
296 // The SHA256 checksum of the final list of hash prefixes, in lexographically 305 // The SHA256 checksum of the final list of hash prefixes, in
297 // sorted order, must match |expected_checksum| (if it's not empty). 306 // lexicographically sorted order, must match |expected_checksum| (if it's not
307 // empty).
298 ApplyUpdateResult MergeUpdate(const HashPrefixMap& old_hash_prefix_map, 308 ApplyUpdateResult MergeUpdate(const HashPrefixMap& old_hash_prefix_map,
299 const HashPrefixMap& additions_map, 309 const HashPrefixMap& additions_map,
300 const ::google::protobuf::RepeatedField< 310 const ::google::protobuf::RepeatedField<
301 ::google::protobuf::int32>* raw_removals, 311 ::google::protobuf::int32>* raw_removals,
302 const std::string& expected_checksum); 312 const std::string& expected_checksum);
303 313
304 // Processes the FULL_UPDATE |response| from the server, and writes the 314 // Processes the FULL_UPDATE |response| from the server, and writes the
305 // merged V4Store to disk. If processing the |response| succeeds, it returns 315 // merged V4Store to disk. If processing the |response| succeeds, it returns
306 // APPLY_UPDATE_SUCCESS. 316 // APPLY_UPDATE_SUCCESS.
307 // This method is only called when we receive a FULL_UPDATE from the server. 317 // This method is only called when we receive a FULL_UPDATE from the server.
(...skipping 30 matching lines...) Expand all
338 // Updates the |additions_map| with the additions received in the partial 348 // Updates the |additions_map| with the additions received in the partial
339 // update from the server. 349 // update from the server.
340 ApplyUpdateResult UpdateHashPrefixMapFromAdditions( 350 ApplyUpdateResult UpdateHashPrefixMapFromAdditions(
341 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions, 351 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions,
342 HashPrefixMap* additions_map); 352 HashPrefixMap* additions_map);
343 353
344 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. 354 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto.
345 StoreWriteResult WriteToDisk( 355 StoreWriteResult WriteToDisk(
346 std::unique_ptr<ListUpdateResponse> response) const; 356 std::unique_ptr<ListUpdateResponse> response) const;
347 357
358 // The checksum value as read from the disk, until it is verified. Once
359 // verified, it is cleared.
360 std::string expected_checksum_;
361
348 // The state of the store as returned by the PVer4 server in the last applied 362 // The state of the store as returned by the PVer4 server in the last applied
349 // update response. 363 // update response.
350 std::string state_; 364 std::string state_;
351 const base::FilePath store_path_; 365 const base::FilePath store_path_;
352 HashPrefixMap hash_prefix_map_; 366 HashPrefixMap hash_prefix_map_;
353 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 367 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
354 }; 368 };
355 369
356 std::ostream& operator<<(std::ostream& os, const V4Store& store); 370 std::ostream& operator<<(std::ostream& os, const V4Store& store);
357 371
358 } // namespace safe_browsing 372 } // namespace safe_browsing
359 373
360 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 374 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_local_database_manager_unittest.cc ('k') | components/safe_browsing_db/v4_store.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698