Incorrect Write Position in Block Device Trim Operation
In the block device trim code, when a trim boundary fell inside a disk page, the preserved partial page was written back to the advanced offset rather than the original one. This overwrote the contents of the following disk page, corrupting stored data.
Submitted by inh3l
monad/category/async/storage_pool.cpp #L258-L320
The chunk::try_trim_contents() method for block devices contains a critical bug in handling partial page preservation during trim operations. When a trim boundary falls within a disk page (i.e., remainder > 0), the code:
- Reads the partial page from the original offset (
range[0]) - Advances
range[0]byDISK_PAGE_SIZEto skip the partial page in the trim operation - But then incorrectly writes the modified buffer back to the advanced
range[0]instead of the original offset
Current Buggy Implementation
if (remainder > 0) {
range[0] += DISK_PAGE_SIZE; // Advance for trim
range[1] -= DISK_PAGE_SIZE;
}
// ... trim operation happens ...
if (remainder > 0) {
// @audit Writing to advanced range[0] instead of original position
MONAD_ASSERT_PRINTF(
-1 != ::pwrite(
write_fd_,
buffer,
DISK_PAGE_SIZE,
static_cast<off_t>(range[0])), // Wrong offset!
"failed due to %s",
strerror(errno));
}
This writes the preserved data fragment to the next disk page, corrupting whatever data was stored there.
Recommended mitigation steps
Store the original offset before modification and use it for the write operation.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The code advanced the start offset before the trim and then reused that advanced offset for the write-back, instead of keeping the original offset.
Impact
Data in the following disk page could be overwritten, corrupting stored contents.
What the attacker needed
Trim boundary inside a disk page
The fix
The original offset should be saved before it is modified and used for the write-back.
How it was found
- A trim boundary falls inside a disk page, so a remainder is present.
- The partial page is read from the original offset.
- The start offset is advanced by one page size for the trim.
- The trim runs, then the saved buffer is written back to the advanced offset.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A block device stores data in fixed-size chunks called pages. When only part of a page needed clearing, the code saved that part and wrote it back afterwards, but it wrote it to the wrong place, so it landed on top of the next page and destroyed data there.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.