Skip to content
DoS Audit finding Intermediate

VE3DLocker.sol Wrong implementation of inversely traverse for loops always reverts

The contract's inversely traversing loops, ported from a version using Solidity 0.6.12, underflow in Solidity 0.8.7 and always revert. This makes several view functions (totalSupplyAtEpoch, balanceAtEpochOf, pendingLockAtEpochOf, totalSupply) unusable, potentially breaking governance and integrations.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by Dravee, gzeon, and TerrierLover

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/VE3DLocker.sol#L305-L329

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/VE3DLocker.sol#L349-L373

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/VE3DLocker.sol#L376-L396

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/VE3DLocker.sol#L399-L415

Vulnerability Details

function totalSupplyAtEpoch(uint256 _epoch) external view returns (uint256 supply) {
    uint256 epochStart = uint256(epochs[_epoch].date).div(rewardsDuration).mul(
        rewardsDuration
    );
    uint256 cutoffEpoch = epochStart.sub(lockDuration);

    //traverse inversely to make more current queries more gas efficient
    for (uint256 i = _epoch; i + 1 != 0; i--) {
        Epoch storage e = epochs[i];
        if (uint256(e.date) <= cutoffEpoch) {
            break;
        }
        supply = supply.add(epochs[i].supply);
    }

    return supply;
}

In VE3DLocker.sol, there are multiple instances in which an inversely traverse for loop is used “to make more current queries more gas efficient”.

For example:

  • totalSupplyAtEpoch()
  • balanceAtEpochOf()
  • pendingLockAtEpochOf()
  • totalSupply()

The implementation of the inversely traverse for loop is inherited from Convex’s original version: https://github.com/convex-eth/platform/blob/main/contracts/contracts/CvxLockerV2.sol#L333-L334

However, Convex’s locker contract is using Solidity 0.6.12, in which the arithmetic operations will overflow/underflow without revert.

As the solidity version used in the current implementation of VE3DLocker.sol is 0.8.7, and there are some breaking changes in Solidity v0.8.0, including:

Arithmetic operations revert on underflow and overflow.

Ref: https://docs.soliditylang.org/en/v0.8.7/080-breaking-changes.html#silent-changes-of-the-semantics

Which makes the current implementation of inversely traverse for loops always reverts.

More specifically:

  1. for (uint i = locks.length - 1; i + 1 != 0; i--) { will revert when locks.length == 0 at locks.length - 1 due to underflow;
  2. for (uint256 i = _epoch; i + 1 != 0; i--) { will loop until i == 0 and reverts at i-- due to underflow.

As a result, all these functions will be malfunctioning and all the internal and external usage of these function will always revert.

Recommended Mitigation Steps

Change VE3DLocker.sol#L315 to:

for (uint256 i = locks.length; i > 0; i--) {
    uint256 lockEpoch = uint256(locks[i - 1].unlockTime).sub(lockDuration);
    //lock epoch must be less or equal to the epoch we're basing from.
    if (lockEpoch <= epochTime) {
        if (lockEpoch > cutoffEpoch) {
            amount = amount.add(locks[i - 1].amount);

Change VE3DLocker.sol#L360 to:

for (uint256 i = locks.length; i > 0; i--) {
    uint256 lockEpoch = uint256(locks[i - 1].unlockTime).sub(lockDuration);

    //return the next epoch balance
    if (lockEpoch == nextEpoch) {
        return locks[i - 1].amount;
    } else if (lockEpoch < nextEpoch) {
        //no need to check anymore
        break;
    }

Change VE3DLocker.sol#L387 to:

for (uint256 i = epochindex; i > 0; i--) {
    Epoch storage e = epochs[i - 1];

Change VE3DLocker.sol#L406 to:

for (uint256 i = _epoch + 1; i > 0; i--) {
    Epoch storage e = epochs[i - 1];
    if (uint256(e.date) <= cutoffEpoch) {
        break;
    }
    supply = supply.add(e.supply);
}

solvetony (veToken Finance) confirmed and commented:

Looks valid.

Alex the Entreprenerd (judge) commented:

The warden has shed light into a underflow that will cause a few functions which logic was ported over from 0.6.12, to revert due to new checks introduced in a more recent version of solidity.

While the bug is easily fixable, the functions are broken.

Normally I would be conflicted between a Low and Medium severity as these functions are mostly view and seem to have no particular impact.

However, due to my familiarity with other Lockers, and the CVX Voting Strategy used I believe the finding implications are that:

  • Integrating strategies (tokenizedLocks of the VE3DLocker) would be bricked
  • Snapshot wouldn’t be usable as it would need to know the balance of a user at a specific epoch

So am confident in a Medium Severity and have contemplated raising to High, as impact is pretty dramatic.

Personally am very confident governance couldn’t work without some of the view functions impacted, however, considering that the Warden (and other dups) did not bring in any mention of governance, I think Medium Severity is appropriate.

I highly recommend the sponsor ensures their governance-related function don’t revert as that would be very problematic.


The original report as published on Code4rena, by WatchPug. 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 loops were written for Solidity 0.6.12 where underflow wraps silently, but in 0.8.7 underflow reverts, so the loop condition causes a revert.

Impact

All internal and external calls to these functions always revert, which could brick integrating strategies and make Snapshot unusable for governance.

What the attacker needed

None

The fix

The loops should be rewritten to count down from length to 1 and index with i-1, avoiding underflow.

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.

Solidity is a programming language for smart contracts. Older versions allowed numbers to wrap around when they went below zero (underflow), but newer versions stop with an error. The code here was copied from an old contract and still uses a loop that counts down past zero, so it always fails.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by WatchPug

View all

197 more disclosed reports, best first.

More from veToken Finance contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.