Attackathon _ Fuel Network 32465 - [Blockchain_DLT - High] Abuse of CCP instruction to do cheap memo

Submitted on Sat Jun 22 2024 14:11:33 GMT-0400 (Atlantic Standard Time) by @NinetyNineCrits for Attackathon | Fuel Network

Report ID: #32465

Report type: Blockchain/DLT

Report severity: High

Target: https://github.com/FuelLabs/fuel-vm/tree/0e46d324da460f2db8bcef51920fb9246ac2143b

Impacts:

  • Modification of transaction fees outside of design parameters

Description

Brief/Intro

The CCP: code copy instruction charges variable cost dependent on the size of the contract to copy from, but not for the actual amount of bytes to be copied. This can be abused to do cheap memory clears for large memory areas

Vulnerability Details

The function code_copy that is invoked by the CCP instruction, loads the target contracts bytecode, charges for its length and then does a copy with zero-fill with a different length parameter:

pub(crate) fn code_copy(...){

    ...

    dependent_gas_charge_without_base(
        self.cgas,
        self.ggas,
        profiler,
        self.gas_cost,
        contract_len as u64, // <-- charged based on this 
    )?;

    copy_from_slice_zero_fill(
        self.memory,
        self.owner,
        contract.as_ref().as_ref(),
        dst_addr,
        offset,
        length, // <-- amount copied, excess zero-filled
    )?;

Any excess amount for the length of bytes gets zero-filled:

The slice data can be turned into an empty slice (default value) by choosing a large offset. If data is an empty slice the copy_from_slice will do nothing. instead the full slice will be zero-filled (identical to a memory clear MCL)

Impact Details

Users can perform an otherwise expensive instruction for almost no cost

References

not applicable

Proof of concept

Proof of Concept

This POC shows the comparison in gas costs of a full memory clear between MCL and CCP

Add the following test to fuel-vm/src/tests/flow.rs:

The logs emitted by this are:

The gas costs for the MCL block are: 9686444 - 9666291 = 20153

The gas costs for the CCP block are 9666291 - 9666255 = 36

The overhead for the other operations in each block are: 1+1+9+1+9 = 21

Last updated

Was this helpful?