Skip to content
DoS Audit finding Intermediate

Unbounded loop in withdraw() may cause rewards to be locked in the contract

The withdraw function contains an unbounded loop that makes external token transfer calls, so if gas costs change or the number of rewards grows, the function may revert due to exceeding the block gas limit, locking rewards in the contract.

IllIllI Follow

Originally disclosed on Code4rena

Submitted by IllIllI

The withdraw() has an unbounded loop with external calls. If the gas costs of functions change between when deposits are made and when rewards are withdrawn, or if the gas cost of the deposit (transferFrom()) is less than the gas cost of the withdrawal (transfer()), then the withdraw() function may revert due to exceeding the block size gas limit.

Proof of Concept

transfer() is an external call, and rewards.length has no maximum size:

File: contracts/PermissionlessBasicPoolFactory.sol   #1

224           for (uint i = 0; i < rewards.length; i++) {
225               pool.rewardsWeiClaimed[i] += rewards[i];
226               pool.rewardFunding[i] -= rewards[i];
227               uint tax = (pool.taxPerCapita * rewards[i]) / 1000;
228               uint transferAmount = rewards[i] - tax;
229               taxes[poolId][i] += tax;
230               success = success && IERC20(pool.rewardTokens[i]).transfer(receipt.owner, transferAmount);
231           }

https://github.com/code-423n4/2022-05-factorydao/blob/db415804c06143d8af6880bc4cda7222e5463c0e/contracts/PermissionlessBasicPoolFactory.sol#L224-L231

Recommended Mitigation Steps

Allow the specification of an offset and length to the withdraw() function, so that withdrawals can be broken up into smaller batches if required

illuzen (FactoryDAO) confirmed


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

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 withdraw function iterates over an array with no maximum length and performs an external call in each iteration, so the total gas required can exceed the block gas limit.

Impact

Rewards may become locked in the contract because withdraw could revert due to exceeding the block gas limit, especially if gas costs change or the number of rewards is large.

What the attacker needed

Multiple rewards in the pool; gas costs increase or many rewards

The fix

The withdraw function should be modified to allow specifying an offset and length so that withdrawals can be processed in smaller batches.

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 function that pays out rewards uses a loop that can run for an unlimited number of times. Each iteration makes an external call to transfer tokens, which costs gas. If there are too many rewards or gas costs increase, the transaction may run out of gas and fail, leaving the rewards stuck.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by IllIllI

View all

119 more disclosed reports, best first.

More from FactoryDAO contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.