Block gas limit can be hit due to loop depth
The claim function in a smart contract farm manager uses three nested loops over a user's positions, farms per liquidity pool, and epochs since the first deposit, giving roughly cubic complexity. A user with many old positions across many farms could exceed the block gas limit, making claim and close_position transactions fail and forcing an emergency withdrawal that loses deposits and rewards.
carrotsmuggler Follow with 0xAlix2, Evo and 1 other
Originally disclosed on Code4rena
Submitted by carrotsmuggler, also found by 0xAlix2, Evo, and Lambda
/contracts/farm-manager/src/farm/commands.rs#L43-L94
Finding description and impact
The claim function iterates over the user positions and calculates the rewards in nested loops. The issue is that every blockchain, to combat against gas attacks of infinite loops, has a block gas limit. If this limit is exceeded, that transaction cannot be included in the chain. The implementation of the claim function here is of the order of N^3 and is thus highly susceptible to an out of gas error.
The claim function iterates over all the user’s positions.
let lp_denoms = get_unique_lp_asset_denoms_from_positions(open_positions);
for lp_denom in &lp_denoms {
// calculate the rewards for the lp denom
let rewards_response = calculate_rewards(
deps.as_ref(),
&env,
lp_denom,
&info.sender,
current_epoch.id,
true,
)?;
//...
}
Lets say the user has P positions, all of different lp_deonm values. Thus this loop is of the order of P. The calculate_rewards function then loops over all the farms of each lp_denom.
let farms = get_farms_by_lp_denom(
deps.storage,
lp_denom,
None,
Some(config.max_concurrent_farms),
)?;
//...
for farm in farms {
// skip farms that have not started
if farm.start_epoch > current_epoch_id {
continue;
}
// compute where the user can start claiming rewards for the farm
let start_from_epoch = compute_start_from_epoch_for_address(
deps.storage,
&farm.lp_denom,
last_claimed_epoch_for_user,
receiver,
)?;
//...
}
Say there are F farms, then this inner loop is of the order of F. Then for each farm, the reward is calculated by iterating over all the epochs from start_from_epoch up to the current_epoch.
for epoch_id in start_from_epoch..=until_epoch {
if farm.start_epoch > epoch_id {
continue;
}
//...
}
The start_from_epoch can be the very first deposit of the user, far back in time, if this is the first time the user is claiming rewards. Thus, this loop can run very long if the position is years old. Say the epoch loop is of the order of E.
Since these 3 loops are nested, the claim function is of the order of P*F*E. P and F are restricted by the config can can have maximum values of the order of 10. But E can be very large, and is actually the order of epoch number. So if epochs are only a few days long, the E can be of the order of 500 over a couple of years.
Thus the claim function can be of the order of 50_000. This is an issue since it requires a loop running 50_000 times along with reward calculations and even token transfers. This can be above the block gas limit and thus the transaction will fail.
There is no functionality to skip positions/farms/epochs. Thus users cannot claim rewards of only a few particular farms or epochs. This part of the code is also executed during the close_position function, which checks if rewards are 0. Thus, the close_position function can also fail due to the same issue, and users are thus forced to emergency withdraw and lose deposits as well as their rewards.
Thus users who join a bunch of different farms and keep their positions for a long time can hit the block gsa limit during the time of claiming rewards or closing positions.
The OOG issue due to large nesting depth is present in multiple instances in the code, this is only one example.
Proof of Concept
P, the number of open positions of a user, is restricted by limit of 100 stored in MAX_ITEMS_LIMIT. F is restricted by the max concurrent no of farms per lp_denom, which we can assume to be 10. E is of the order of epochs between the first deposit and the current epoch, which can be in the 100s if epochs are single days, or 100s if epochs are weeks.
Thus, P*F*E is of the order of 100*10*100 = 100_000; 100_000 iterations are required for the claim function on top of token transfers and math calculations. This can easily exceed the block gas limit.
Recommended mitigation steps
The order of the nested loops need to be decreased. This can be done in multiple ways.
- Implement sushi-masterchef style reward accounting. This way the entire
Enumber of epochs dont need to be looped over. - Implement a way to only process a given number of positions. This way
Pcan also be restricted and users can claim in batches.
jvr0x (MANTRA) confirmed
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 claim logic recomputes rewards by iterating every epoch from the user's first deposit to the current one inside loops over positions and farms, giving P*F*E complexity with no batching or checkpoint accounting.
Impact
A user with many positions held over many epochs could exceed the block gas limit, so claim and close_position fail and the user is forced into an emergency withdrawal, losing deposits and rewards.
What the attacker needed
Many open positions; positions held across many epochs; many farms per liquidity pool
The fix
The nested loops should be reduced, for example with masterchef-style reward accounting that avoids looping over every epoch, or by letting users process a limited number of positions per call.
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.
Every blockchain caps how much computing work one transaction may do, called the block gas limit, so a transaction that loops too many times is rejected. Here the reward-claiming code loops over each position, then each farm, then each time period (epoch) since the user first deposited, and those loops multiply together. A long-standing user with many positions can therefore build a transaction too heavy to ever succeed, and the same code runs when closing a position.
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.