Skip to content
Memory corruption Audit finding Advanced

Missing `offset` check will cause integer overflow violating ELF specification

The ELF parser in the Pico VM adds three 32-bit values without checking for overflow, so a crafted ELF file with a very large segment offset can wrap the computed offset to a small value. This lets an attacker submit a malformed ELF that is accepted despite violating the ELF specification, potentially causing out-of-bounds reads or panics in the prover node.

poetyellow Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-08-pico-judging/issues/75

Found by

poetyellow

Summary

There is an integer overflow in offset calculation in Elf::new() function.

Rust
for j in 0..len {
    let offset = (offset + i + j) as usize;  // integer overflow
    let byte = source_code
        .get(offset)
        .ok_or_else(|| eyre::eyre!("failed to read segment offset"))?;
    word |= u32::from(*byte) << (j * 8);
}

https://github.com/brevis-network/pico/blob/3046e89b1c7bf065b7932d859a06a24920d57531/vm/src/compiler/riscv/disassembler/elf.rs#L133

Root Cause

there is an unchecked integer addition let offset = (offset + i + j) as usize where the sum of three u32 values can overflow before being cast to usize, leading to a wrapped-around smaller offset value ,and this small value will not cause panic or return error,but it will violate ELF specification.

Internal Pre-conditions

  1. ELF file needs to pass basic validity checks (32-bit, RISC-V architecture, executable type)
  2. Attacker needs to craft segment with p_offset to be close to u32::MAX (4GB-1)
  3. Segment needs to have mem_size > 0 to enter the processing loop
  4. The sum (p_offset + i + j) needs to exceed u32::MAX to trigger overflow

External Pre-conditions

No special external conditions required

Attack Path

  1. Attacker crafts malicious ELF file with PT_LOAD segment
  2. Sets segment p_offset to a large value (e.g., u32::MAX - 1000)
  3. Sets segment mem_size to a value that causes loop iteration where i + j > 1000
  4. Attacker submits malicious ELF file to Prover node for processing
  5. Code enters segment processing loop at line 113
  6. When loop reaches large i values, the calculation offset + i + j at line 133 overflows
  7. The overflowed result wraps around to a small value after casting to usize
  8. Line 135 attempts to access source_code at the wrapped-around offset
  9. This causes either out-of-bounds access (panic) or reading from wrong memory location

Impact

Elf::new() should succeed if and only if the binary is a well formed Risc-V ELF. Violating this specification can have unexpected behavior for downstream users.

PoC

No response

Mitigation

Use checked arithmetic:

Rust
let offset = offset.checked_add(i)
    .and_then(|sum| sum.checked_add(j))
    .ok_or_else(|| eyre::eyre!("offset calculation overflow"))?
    as usize;

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/brevis-network/pico/pull/61

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

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 performs unchecked addition of three u32 values before casting to usize, so the sum can overflow and wrap to a small offset.

Impact

A malformed ELF that violates the specification may be accepted, leading to unexpected behaviour such as out-of-bounds access or a panic for downstream users; the text does not claim remote code execution.

What the attacker needed

Attacker can submit an ELF file to a Prover node; ELF passes basic validity checks; Segment p_offset near u32::MAX; Segment mem_size > 0

The fix

The issue was fixed by using checked arithmetic that returns an error when the offset calculation overflows.

How it was found

  1. Craft an ELF file with a PT_LOAD segment whose p_offset is close to the maximum 32-bit value.
  2. Set the segment's mem_size so that the loop reaches values where i + j exceeds the remaining headroom.
  3. Submit the malicious ELF file to a Prover node for processing.
  4. The segment processing loop computes offset + i + j, which overflows and wraps to a small value.
  5. The code then reads source_code at the wrapped offset, causing an out-of-bounds access or a read from the wrong location.

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.

An ELF file is a common format for executable programs. The code that reads it calculates where to look inside the file by adding numbers together, but it does not check whether the addition goes past the maximum value the number type can hold. If an attacker supplies a file with a huge starting offset, the sum wraps around to a small number, so the program reads the wrong part of the file instead of rejecting it.

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 from Brevis Pico ZKVM

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.