Skip to content
Business logic Audit finding Intermediate

bEth Rewards May Be Depleted By Flashloans or Whales

The reward distribution logic updates a global index based on the current total balance, so a user who suddenly increases their balance can capture a disproportionate share of rewards. An attacker could flashloan or buy a large amount of wormhole ETH, convert it to bEth, trigger a reward update, claim rewards, and then convert back, gaining rewards without contributing to the system.

BondiPestControl Follow

Originally disclosed on Code4rena

Submitted by BondiPestControl, also found by cmichel

https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bEth-contracts/contracts/anchor_beth_reward/src/user.rs#L187-L212

Rewards are dispersed to users as a percentage of the user’s balance vs total balance (of bEth). Rewards are accumulated each time a user calls execute_decrease_balance(), execute_increase_balance() or execute_claim_rewards() as these functions will in term call update_global_index() before any balance adjustments.

There is an attack vector where a user may arbitrarily increase their bEth balance either by flashloan of purchase of wormhole eth, then converting it bEth via anchor_beth_converter. The attacker may then have a significant portion of the total balance. This use can then call the contract which pushes rewards to anchor_beth_reward this will increase the reward_balance of the contract. Since the attacker controls a large portion of the bEth balance they will receive a higher portion of the rewards. They may then convert the bEth back to wormhole eth and close their position.

The result is the user has not contributed anything to the system but still gained a high portion of the rewards.

Proof of Concept

The logic for update_global_index() does not account for users suddenly increasing the balance.

fn update_global_index(state: &mut State, reward_balance: Uint128) -> StdResult<()> {
    // Zero staking balance check
    if state.total_balance.is_zero() {
        // nothing balance, skip update
        return Ok(());
    }

    // No change check
    if state.prev_reward_balance == reward_balance {
        // balance didnt change, skip update
        return Ok(());
    }

    // claimed_rewards = current_balance - prev_balance;
    let claimed_rewards = reward_balance.checked_sub(state.prev_reward_balance)?;

    // update state
    state.prev_reward_balance = reward_balance;
    // global_index += claimed_rewards / total_balance;
    state.global_index = decimal_summation_in_256(
        state.global_index,
        Decimal::from_ratio(claimed_rewards, state.total_balance),
    );

    Ok(())
}

Steps: a) Flashloan (or buy) a significant portion of wormhole Eth b) Convert to Anchor bEth via anchor_beth_converter c) Push rewards on anchor_beth_rewards d) anchor_beth_rewards::execute_claim_rewards() e) Convert Anchor bEth to wormhole Eth f) Repay flashloan (or sell wormhole eth)

Recommended Mitigation Steps

There are multiple possible mitigations to this issue.

First, option is to only allow the global_index to be updated once per block. In addition to this, cap the amount of rewards that may be paid per block (keeping the remaining rewards for the next block). This would reduce the effectiveness of the attack and limit the amount they may earn per block.

Another option is to induce a wait time before the user may begin earning rewards. However, this would require a second transaction from the user to begin collection their reward which may hurt UX.


The original report as published on Code4rena, by BondiPestControl. 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 reward index is updated using the instantaneous total balance, so a large balance added just before a reward update captures a disproportionate share of the rewards.

Impact

An attacker could gain a high portion of the rewards without contributing anything to the system, as the text states.

What the attacker needed

Ability to flashloan or buy large amount of wormhole ETH; access to anchor_beth_converter; ability to trigger reward update

The fix

The global index should be updated at most once per block and rewards paid per block should be capped, or a wait time should be introduced before rewards start accruing.

How it was found

  1. Flashloan (or buy) a significant portion of wormhole Eth
  2. Convert to Anchor bEth via `anchor_beth_converter`
  3. Push rewards on `anchor_beth_rewards`
  4. Call `anchor_beth_rewards::execute_claim_rewards()`
  5. Convert Anchor bEth to wormhole Eth
  6. Repay flashloan (or sell wormhole eth)

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.

Rewards are shared out based on how much of a token you hold compared to everyone else. The problem is that the system recalculates rewards whenever someone's balance changes, so if you suddenly get a huge amount of the token just before rewards are handed out, you get a huge share. You can then swap back and keep the rewards, even though you only held the tokens for a moment.

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 BondiPestControl

View all

2 more disclosed reports, best first.

More from Anchor contest

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.