Skip to content
Memory corruption Audit finding Intermediate

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.

inh3l Follow

Originally disclosed on Code4rena

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:

  1. Reads the partial page from the original offset (range[0])
  2. Advances range[0] by DISK_PAGE_SIZE to skip the partial page in the trim operation
  3. 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.


The original report as published on Code4rena, by inh3l. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. A trim boundary falls inside a disk page, so a remainder is present.
  2. The partial page is read from the original offset.
  3. The start offset is advanced by one page size for the trim.
  4. 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.

More by inh3l

View all

2 more disclosed reports, best first.

More from Monad

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.