Skip to content
Business logic Audit finding Intermediate

voters cannot disable max lock

The disable_max_lock function incorrectly updates the maxLockIdToIndex mapping when removing the last element from the max_locked_nfts array, leaving a stale index. This prevents voters from disabling maxLock, which in turn prevents them from withdrawing their assets because their lock end time decreases on each withdrawal attempt.

0xpiken Follow with 1nc0gn170, Chinmay and 8 others

Originally disclosed on Sherlock

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

Found by

0xpiken, 1nc0gn170, Chinmay, HackTrace, Kirkeelee, bin2chen, bughuntoor, coffiasd, eeshenggoh, jovi, pashap9990

Summary

Voters can enable maxLock and this causes their voting power wouldn't decrease but they cannot disable maxLock

Vulnerability Detail

Textual PoC: Let's assume three voters lock their assets in ve,hence three nfts will be minted[1,2,3] and after that they enable maxLock

Initial values max_locked_nfts corresponding values:

index 0 index 1 index 2
1 2 3

maxLockIdToIndex corresponding values:

index 1 index 2 index 3
1 2 3

when owner of nft 3 want to disable maxLock he has to call VotingEscrow::disable_max_lock in result : variable's values from line 897 til 901:

  • index = 2
  • maxLockIdToIndex[3] = 0
  • max_locked_nfts[2] = 3

max_locked_nfts corresponding values:

index 0 index 1 index 2
1 2 3

maxLockIdToIndex corresponding values:

index 1 index 2 index 3
1 2 0

finally

  • maxLockIdToIndex[max_locked_nfts[2]] => maxLockIdToIndex[3] = 2 + 1
  • last element of max_locked_nfts will be deleted

Coded PoC:

Solidity
    function testEnableAndDisableMaxLock() external {
        flowDaiPair.approve(address(escrow), TOKEN_1);
        uint256 lockDuration = 7 * 24 * 3600; // 1 week
        escrow.create_lock(400, lockDuration);
        escrow.create_lock(400, lockDuration);
        escrow.create_lock(400, lockDuration);

        assertEq(escrow.currentTokenId(), 3);
        escrow.enable_max_lock(1);
        escrow.enable_max_lock(2);
        escrow.enable_max_lock(3);


        assertEq(escrow.maxLockIdToIndex(1), 1);
        assertEq(escrow.maxLockIdToIndex(2), 2);
        assertEq(escrow.maxLockIdToIndex(3), 3);

        assertEq(escrow.max_locked_nfts(0), 1);
        assertEq(escrow.max_locked_nfts(1), 2);
        assertEq(escrow.max_locked_nfts(2), 3);

        escrow.disable_max_lock(3);

        assertEq(escrow.maxLockIdToIndex(1), 1);
        assertEq(escrow.maxLockIdToIndex(2), 2);
        assertEq(escrow.maxLockIdToIndex(3), 3);//mockLockIdToIndex has to be zero 

        assertEq(escrow.max_locked_nfts(0), 1);
        assertEq(escrow.max_locked_nfts(1), 2);
    }

Impact

Voters cannot withdraw their assets from ve because every time they call VotingEscrow::withdraw their lockEnd will be decrease

Code Snippet

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

Tool used

Manual Review

Recommendation

Diff
    function disable_max_lock(uint _tokenId) external {
        assert(_isApprovedOrOwner(msg.sender, _tokenId));
        require(maxLockIdToIndex[_tokenId] != 0,"disabled");

        uint index =  maxLockIdToIndex[_tokenId] - 1;
        maxLockIdToIndex[_tokenId] = 0;

         // Move the last element into the place to delete
        max_locked_nfts[index] = max_locked_nfts[max_locked_nfts.length - 1];

+         if (index != max_locked_nfts.length - 1) {
+             uint lastTokenId = max_locked_nfts[max_locked_nfts.length - 1];
+             max_locked_nfts[index] = lastTokenId;
+             maxLockIdToIndex[lastTokenId] = index + 1;
+         }
        
+         maxLockIdToIndex[max_locked_nfts[index]] = 0;
        

-       maxLockIdToIndex[max_locked_nfts[index]] = index + 1;//@audit maxLockIdToIndex computes wrongly when lps want to disable last element in array
        
        // Remove the last element
        max_locked_nfts.pop();
    }

Discussion

sherlock-admin2

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

spacegliderrrr

Fix looks good. disable_max_lock now works properly.

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 disable_max_lock function fails to properly update the maxLockIdToIndex mapping when the element being removed is the last in the array, leaving a stale index that prevents future disabling.

What the attacker needed

User has locked assets and enabled maxLock

The fix

The protocol team fixed this issue in PR #12, and the fix was verified by the Lead Senior Watson.

How it was found

  1. Three voters lock their assets, creating NFTs with IDs 1, 2, and 3.
  2. Each voter enables maxLock, populating max_locked_nfts and maxLockIdToIndex.
  3. The owner of NFT 3 calls disable_max_lock(3).
  4. The function sets maxLockIdToIndex[3] to 0 and attempts to update the mapping for the last element, but incorrectly sets maxLockIdToIndex[3] to 3 instead of 0.
  5. As a result, maxLockIdToIndex[3] remains non-zero, so the require check in disable_max_lock fails on subsequent calls, preventing disabling.

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.

In a voting system, users lock tokens to get voting power. A feature called maxLock lets them keep full voting power, but they cannot turn it off due to a bug in how the list of max-locked tokens is updated. This means they cannot withdraw their tokens because the lock time keeps decreasing.

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.