Skip to content

[scudo] Double frees result in chunk state error #110345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions compiler-rt/lib/scudo/standalone/combined.h
Original file line number Diff line number Diff line change
Expand Up @@ -1252,22 +1252,26 @@ class Allocator {
else
Header->State = Chunk::State::Quarantined;

void *BlockBegin;
if (LIKELY(!useMemoryTagging<AllocatorConfig>(Options))) {
if (LIKELY(!useMemoryTagging<AllocatorConfig>(Options)))
Header->OriginOrWasZeroed = 0U;
if (BypassQuarantine && allocatorSupportsMemoryTagging<AllocatorConfig>())
Ptr = untagPointer(Ptr);
BlockBegin = getBlockBegin(Ptr, Header);
} else {
else {
Header->OriginOrWasZeroed =
Header->ClassId && !TSDRegistry.getDisableMemInit();
BlockBegin =
retagBlock(Options, TaggedPtr, Ptr, Header, Size, BypassQuarantine);
}

Chunk::storeHeader(Cookie, Ptr, Header);

if (BypassQuarantine) {
void *BlockBegin;
if (LIKELY(!useMemoryTagging<AllocatorConfig>(Options))) {
// Must do this after storeHeader because loadHeader uses a tagged ptr.
if (allocatorSupportsMemoryTagging<AllocatorConfig>())
Ptr = untagPointer(Ptr);
BlockBegin = getBlockBegin(Ptr, Header);
} else {
BlockBegin = retagBlock(Options, TaggedPtr, Ptr, Header, Size, true);
}

const uptr ClassId = Header->ClassId;
if (LIKELY(ClassId)) {
bool CacheDrained;
Expand All @@ -1285,6 +1289,8 @@ class Allocator {
Secondary.deallocate(Options, BlockBegin);
}
} else {
if (UNLIKELY(useMemoryTagging<AllocatorConfig>(Options)))
retagBlock(Options, TaggedPtr, Ptr, Header, Size, false);
typename TSDRegistryT::ScopedTSD TSD(TSDRegistry);
Quarantine.put(&TSD->getQuarantineCache(),
QuarantineCallback(*this, TSD->getCache()), Ptr, Size);
Expand Down
21 changes: 21 additions & 0 deletions compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,27 @@ SCUDO_TYPED_TEST(ScudoCombinedDeathTest, UseAfterFree) {
}
}

SCUDO_TYPED_TEST(ScudoCombinedDeathTest, DoubleFreeFromPrimary) {
auto *Allocator = this->Allocator.get();

for (scudo::uptr SizeLog = 0U; SizeLog <= 20U; SizeLog++) {
const scudo::uptr Size = 1U << SizeLog;
if (!isPrimaryAllocation<TestAllocator<TypeParam>>(Size, 0))
break;

// Verify that a double free results in a chunk state error.
EXPECT_DEATH(
{
// Allocate from primary
void *P = Allocator->allocate(Size, Origin);
ASSERT_TRUE(P != nullptr);
Allocator->deallocate(P, Origin);
Allocator->deallocate(P, Origin);
},
"invalid chunk state");
}
}

SCUDO_TYPED_TEST(ScudoCombinedDeathTest, DisableMemoryTagging) {
auto *Allocator = this->Allocator.get();

Expand Down
Loading