Skip to content

Commit ebb70fe

Browse files
committed
M72: service worker: Make LevelDB database outlive its iterator to fix crash.
As explained in the bug by pwnall: ServiceWorkerDatabase::GetRegistrationsForOrigin() obtains a LevelDB iterator and calls ServiceWorkerDatabase::ReadResourceRecords() in a loop. ReadResourceRecords() in turn calls ServiceWorkerDatabase::HandleReadResult(), which may call ServiceWorkerDatabase::Disable() if the status is a failure. Disable() contains a "db_.reset()" which deletes the leveldb::DB instance. So, ReadResourceRecords() may end up deleting the database before the iterator used by GetRegistrationsForOrigin() is deleted. The contract for leveldb::DB::NewIterator() [1] states that the iterator must be deleted before the DB instance is deleted. [1] https://cs.chromium.org/chromium/src/third_party/leveldatabase/src/include/leveldb/db.h?l=92&rcl=73d5834eceee8efa9a8ccfec77dc096a9e8ba18a Bug: 909024 Change-Id: Ifee9aa0f7e1db9168d61b6407a11e249b2001986 Reviewed-on: https://chromium-review.googlesource.com/c/1354730 Commit-Queue: Matt Falkenhagen <falken@chromium.org> Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org> Reviewed-by: Victor Costan <pwnall@chromium.org> Cr-Original-Commit-Position: refs/heads/master@{#612599}(cherry picked from commit dd5d624) Reviewed-on: https://chromium-review.googlesource.com/c/1362673 Reviewed-by: Matt Falkenhagen <falken@chromium.org> Cr-Commit-Position: refs/branch-heads/3626@{#65} Cr-Branched-From: d897fb1-refs/heads/master@{#612437}
1 parent 8cecdfb commit ebb70fe

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-10
lines changed

content/browser/service_worker/service_worker_database.cc

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin(
397397
return status;
398398

399399
std::string prefix = CreateRegistrationKeyPrefix(origin);
400+
401+
// Read all registrations.
400402
{
401403
std::unique_ptr<leveldb::Iterator> itr(
402404
db_->NewIterator(leveldb::ReadOptions()));
@@ -421,21 +423,34 @@ ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin(
421423
break;
422424
}
423425
registrations->push_back(registration);
426+
}
427+
}
424428

425-
if (opt_resources_list) {
426-
std::vector<ResourceRecord> resources;
427-
status = ReadResourceRecords(registration, &resources);
428-
if (status != STATUS_OK) {
429-
registrations->clear();
430-
opt_resources_list->clear();
431-
break;
432-
}
433-
opt_resources_list->push_back(resources);
429+
// Count reading all registrations as one "read operation" for UMA
430+
// purposes.
431+
HandleReadResult(FROM_HERE, status);
432+
if (status != STATUS_OK)
433+
return status;
434+
435+
// Read the resources if requested. This must be done after the loop with
436+
// leveldb::Iterator above, because it calls ReadResouceRecords() which
437+
// deletes |db_| on failure, and iterators must be destroyed before the
438+
// database.
439+
if (opt_resources_list) {
440+
for (const auto& registration : *registrations) {
441+
std::vector<ResourceRecord> resources;
442+
// NOTE: ReadResourceRecords already calls HandleReadResult() on its own,
443+
// so to avoid double-counting the UMA, don't call it again after this.
444+
status = ReadResourceRecords(registration, &resources);
445+
if (status != STATUS_OK) {
446+
registrations->clear();
447+
opt_resources_list->clear();
448+
break;
434449
}
450+
opt_resources_list->push_back(resources);
435451
}
436452
}
437453

438-
HandleReadResult(FROM_HERE, status);
439454
return status;
440455
}
441456

content/browser/service_worker/service_worker_database_unittest.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "base/macros.h"
1515
#include "base/stl_util.h"
1616
#include "base/strings/string_number_conversions.h"
17+
#include "base/test/metrics/histogram_tester.h"
1718
#include "content/browser/service_worker/service_worker_database.pb.h"
1819
#include "content/common/service_worker/service_worker_types.h"
1920
#include "testing/gmock/include/gmock/gmock.h"
@@ -2042,4 +2043,64 @@ TEST(ServiceWorkerDatabaseTest, Corruption_NoMainResource) {
20422043
EXPECT_TRUE(resources_out.empty());
20432044
}
20442045

2046+
// Tests that GetRegistrationsForOrigin() detects corruption without crashing.
2047+
// It must delete the database after freeing the iterator it uses to read all
2048+
// registrations. Regression test for https://crbug.com/909024.
2049+
TEST(ServiceWorkerDatabaseTest, Corruption_GetRegistrationsForOrigin) {
2050+
std::unique_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory());
2051+
ServiceWorkerDatabase::RegistrationData deleted_version;
2052+
std::vector<int64_t> newly_purgeable_resources;
2053+
std::vector<Resource> resources;
2054+
GURL origin("https://example.com");
2055+
2056+
// Write a normal registration.
2057+
RegistrationData data1;
2058+
data1.registration_id = 1;
2059+
data1.scope = URL(origin, "/foo");
2060+
data1.script = URL(origin, "/resource1");
2061+
data1.version_id = 1;
2062+
data1.resources_total_size_bytes = 2016;
2063+
resources = {CreateResource(1, URL(origin, "/resource1"), 2016)};
2064+
ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
2065+
database->WriteRegistration(data1, resources, &deleted_version,
2066+
&newly_purgeable_resources));
2067+
2068+
// Write a corrupt registration.
2069+
RegistrationData data2;
2070+
data2.registration_id = 2;
2071+
data2.scope = URL(origin, "/foo");
2072+
data2.script = URL(origin, "/resource2");
2073+
data2.version_id = 2;
2074+
data2.resources_total_size_bytes = 2016;
2075+
// Simulate that "/resource2" wasn't correctly written in the database by
2076+
// not adding it.
2077+
resources = {CreateResource(3, URL(origin, "/resource3"), 2016)};
2078+
ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
2079+
database->WriteRegistration(data2, resources, &deleted_version,
2080+
&newly_purgeable_resources));
2081+
2082+
// Call GetRegistrationsForOrigin(). It should detect corruption, and not
2083+
// crash.
2084+
base::HistogramTester histogram_tester;
2085+
std::vector<RegistrationData> registrations;
2086+
std::vector<std::vector<ServiceWorkerDatabase::ResourceRecord>>
2087+
resources_list;
2088+
EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED,
2089+
database->GetRegistrationsForOrigin(origin, &registrations,
2090+
&resources_list));
2091+
EXPECT_TRUE(registrations.empty());
2092+
EXPECT_TRUE(resources_list.empty());
2093+
2094+
// There should be three "read" operations logged:
2095+
// 1. Reading all registration data.
2096+
// 2. Reading the resources of the first registration.
2097+
// 3. Reading the resources of the second registration. This one fails.
2098+
histogram_tester.ExpectTotalCount("ServiceWorker.Database.ReadResult", 3);
2099+
histogram_tester.ExpectBucketCount("ServiceWorker.Database.ReadResult",
2100+
ServiceWorkerDatabase::STATUS_OK, 2);
2101+
histogram_tester.ExpectBucketCount(
2102+
"ServiceWorker.Database.ReadResult",
2103+
ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED, 1);
2104+
}
2105+
20452106
} // namespace content

0 commit comments

Comments
 (0)