Skip to content
Business logic Audit finding Intermediate

User is unable to claim their reward for the expanded epochs if farm is expanded

A user who claims rewards during the preliminary end epoch cannot later claim rewards for that same epoch if the farm owner expands the farm afterwards, because the user's last claimed epoch is set to the preliminary end epoch. This can cause the user to miss rewards for up to two epochs.

0xRajkumar Follow with 0xlookman and carrotsmuggler

Originally disclosed on Code4rena

Submitted by 0xRajkumar, also found by 0xlookman and carrotsmuggler

/contracts/farm-manager/src/manager/commands.rs#L240-L243

Finding description and Impact

We have a claim function in the Farm Manager, which is used to claim rewards if the user has any open positions. This function updates the LAST_CLAIMED_EPOCH to the user’s last claimed epoch.

Additionally, we have the expand_farm function, which is used to expand farms. Technically, the farm creator can expand the farm even after the farm’s last reward epoch has been completed.

Let’s explore why below:

pub(crate) fn is_farm_expired(
    farm: &Farm,
  deps: Deps,
  env: &Env,
  config: &Config,
) -> Result<bool, ContractError> {
    let epoch_response: EpochResponse = deps
  .querier
  // query preliminary_end_epoch + 1 because the farm is preliminary ending at that epoch, including it.
  .query_wasm_smart(
            config.epoch_manager_addr.to_string(),
  &QueryMsg::Epoch {
                id: farm.preliminary_end_epoch + 1u64,
  },
  )?;

  let farm_ending_at = epoch_response.epoch.start_time;

  Ok(
        farm.farm_asset.amount.saturating_sub(farm.claimed_amount) == Uint128::zero()
            || farm_ending_at.plus_seconds(config.farm_expiration_time) < env.block.time,
  )
}

Let’s say our starting epoch was 1 and the preliminary_end_epoch was 2, meaning the farm was intended only for epoch 1. However, due to the condition farm_ending_at.plus_seconds(config.farm_expiration_time) < env.block.time, the farm owner can expand the farm during epoch 2 + farm_expiration_time as well.

Now let’s say even if farm expanding the farm for 2 epoch, then users who have an open position for that epoch should be able to claim reward, but this is not the case. Let’s see how.

Let’s say a user has an opened position claims rewards during epoch 2, then he will be able to claim reward from farm for epoch 1 only because preliminary_end_epoch is 2 as you can see this in this function.

fn compute_farm_emissions(
    farm: &Farm,
  start_from_epoch: &EpochId,
  current_epoch_id: &EpochId,
) -> Result<(HashMap<EpochId, Uint128>, EpochId), ContractError> {
    let mut farm_emissions = HashMap::new();

  let until_epoch = if farm.preliminary_end_epoch <= *current_epoch_id {
        // the preliminary_end_epoch is not inclusive, so we subtract 1
  farm.preliminary_end_epoch - 1u64
  } else {
        *current_epoch_id
  };

  for epoch in *start_from_epoch..=until_epoch {
  farm_emissions.insert(epoch, farm.emission_rate);
  }

    Ok((farm_emissions, until_epoch))
}

Whenever a user claims a reward, we maintain the LAST_CLAIMED_EPOCH. If the user tries to claim again, they will only be able to claim rewards starting from LAST_CLAIMED_EPOCH + 1.

There is a possibility that if a user claims rewards during the preliminary_end_epoch, and immediately after, the farm owner expands the farm, the user will not be able to claim rewards for the expanded epoch. This issue can occur for up to a maximum of two epochs.

Let’s consider a scenario: the farm’s start_epoch is 1, and the preliminary_end_epoch is 2, meaning the user can currently claim rewards only for epoch 1.

Now, the user claims their reward during epoch 2. However, since the preliminary_end_epoch is still 2, the user can only claim up to epoch 1. Immediately after the user’s claim transaction, the farm owner expands the farm during the same epoch (epoch 2) for 1 additional epoch. This expansion updates the preliminary_end_epoch to 3.

Even though the user has an open position for epoch 2, they will not be able to claim the reward for epoch 2. This is because their LAST_CLAIMED_EPOCH is now set to 2, and they can only claim rewards starting from epoch 3. However, the user should still be able to claim rewards for epoch 2 as they had an active position during that time.

In the example above, we observed that the user is unable to claim rewards for one epoch, even though they had an open position for that epoch. This issue can extend to an additional epoch if the user claims rewards during the period between farm_ending_at and farm_ending_at.plus_seconds(config.farm_expiration_time), and the farm owner expands the farm within the same time frame but after the user’s claim transaction.

Impact

The impact is High because the user will not be able to claim their full reward.

Proof of Concept

For the proof of concept, let’s consider the following scenario:

  1. The farm’s start_epoch is 1, preliminary_epoch is 2, farm_expiration_time is 1 hour, and each epoch lasts 1 day.
  2. A user have a position for epoch 1 with some weight.
  3. During epoch 2, the user claims their reward. Due to the preliminary_epoch being 2, they can only claim rewards for epoch 1. After this, their last_claimed_epoch is updated to 2.
  4. In the same epoch (epoch 2), the farm owner submits a transaction after the user’s claim and expands the farm for 1 additional epoch, updating the preliminary_epoch to 3.
  5. The user comes back in epoch 3 to claim their rewards but will not be able to claim for epoch 2, as their last_claimed_epoch is still 2. Consequently, they miss the reward for epoch 2.

This illustrates how the issue prevents the user from receiving rewards for epoch 2.

Recommended mitigation steps

We can mitigate this issue by only allowing farm expansion before preliminary_epoch - 1 only.

jvr0x (MANTRA) confirmed

3docSec (judge) commented:

S-387 proposes an alternative solution.


The original report as published on Code4rena, by 0xRajkumar. 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 claim function sets the last claimed epoch to the current preliminary end epoch, and expanding the farm later does not adjust this value, so rewards for the newly added epoch are skipped.

Impact

The user could lose rewards for up to two epochs, meaning they cannot claim their full reward.

What the attacker needed

User has an open position; user claims during preliminary end epoch; farm owner expands farm after claim

The fix

The farm should only be allowed to expand before the preliminary end epoch minus one, as recommended.

How it was found

  1. A user has a position for epoch 1 and the farm's preliminary end epoch is 2.
  2. During epoch 2, the user claims rewards, which sets their last claimed epoch to 2.
  3. In the same epoch, the farm owner expands the farm for one more epoch, updating the preliminary end epoch to 3.
  4. The user returns in epoch 3 to claim but cannot claim rewards for epoch 2 because their last claimed epoch is still 2.

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 farm gives out rewards over several time periods called epochs. When you claim, the system records the last epoch you claimed up to. If the farm is extended after you claim, the system thinks you already claimed for the new epoch, so you cannot get those rewards. This means you lose rewards you should have earned.

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 0xRajkumar

View all

6 more disclosed reports, best first.

More from MANTRA DEX

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.