Skip to content
Business logic Audit finding Intermediate

Penalty fees can be shared among future farms or expired farms, risks of exploits

When a position is emergency withdrawn, the penalty fee is split equally among all farm owners sharing the same LP denom, including owners of future or expired farms. A malicious farm creator could register a future farm to collect a share of penalty fees without contributing rewards, then close the farm and recover the deposited assets.

oakcobalt Follow with 0xAlix2, 0xlookman and 6 others

Originally disclosed on Code4rena

Submitted by oakcobalt, also found by 0xAlix2, 0xlookman, Bauchibred, Egis_Security, gegul, jasonxiale, Lambda, and Tigerfrake

The penalty fee is a percentage of the value of existing positions that are emergency withdrawn (withdraw_position). It is shared equally among farm owners whose farms have the same lp_denom as the position.

The vulnerability is that the penalty fee is divided amongst all farms regardless of whether the farms are current, in the future, or already expired, which allows malicious farm owners to exploit.

Proof of Concept

We see in withdraw_position, all farms with the same lp_denom are fetched to get farm owners to share penalty fees. And get_farms_by_lp_denom fetches all farms indexed by lp_denom regardless whether the farm is current, in the future or already expired.

//contracts/farm-manager/src/farm/commands.rs

pub(crate) fn withdraw_position(
...
) -> Result<Response, ContractError> {
...
        //@audit This gets all farms including future farms or expired farms
|>      let farms = get_farms_by_lp_denom(
            deps.storage,
            &position.lp_asset.denom,
            None,
            Some(MAX_ITEMS_LIMIT),
        )?;
            // get unique farm owners for this lp denom
                let unique_farm_owners: Vec<Addr> = farms
            .iter()
            .map(|farm| farm.owner.clone())
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();
        ...
                //@audit penalty fees can be divided among future farms or expired farms.
                    let penalty_fee_share_per_farm_owner = Decimal::from_ratio(
                owner_penalty_fee_comission,
|>              unique_farm_owners.len() as u128,
            )
            .to_uint_floor();

/contracts/farm-manager/src/position/commands.rs#L366-L368

//contracts/farm-manager/src/state.rs
 pub fn get_farms_by_lp_denom(
    storage: &dyn Storage,
    lp_denom: &str,
    start_after: Option<String>,
    limit: Option<u32>,
) -> StdResult<Vec<Farm>> {
    let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_ITEMS_LIMIT) as usize;
    let start = cw_utils::calc_range_start_string(start_after).map(Bound::ExclusiveRaw);
    FARMS
        .idx
        .lp_denom
        .prefix(lp_denom.to_owned())
        .range(storage, start, None, Order::Ascending)
        .take(limit)
        .map(|item| {
            let (_, farm) = item?;

            Ok(farm)
        })
        .collect()
}

/contracts/farm-manager/src/state.rs#L124

Suppose a malicious farm creator creates a future farm with valid assets and a popular lp_denom with one existing farm active. The new farm is valid in the eyes of admin due to the farm has sufficient assets and correct epoch settings.

  1. Any penalty fees shares incurred from withdraw_position’s emergency feature before the farm start time will be sent to the malicious farm creator.
  2. In addition, the malicious farm creator can also target existing farms with more frequent emergency withdraws either due to bugs or market conditions.
  3. Before the new farm epoch starts, the malicious farm creator made a profit (1/2 * total owner penalty fees - a flat pool creation fee >0). The malicious farm creator close_farm. All deposited farm assets are transferred back to the malicious creator.

Impact

Active farm owners get less penalty fee shares due to fee shared among expired farms and future farms. A malicious farm creator could also take penalty fee shares without having to contribute to rewarding.

Recommended mitigation steps

In withdraw_position, before dividing the owner_penalty_fee_comission, filter out farms that starts in a future epoch or have expired.

jvr0x (MANTRA) confirmed and commented:

The farm creation fee is going to be set high enough to prevent unserious players to create farms. Additionally, the contract owner can at any point close farms deemed as spam, malicious or dishonest.

However, the recommendation is valid, will likely adopt it.

3docSec (judge) commented:

Shares in penalty fees can, in my understanding, still compensate for farm creation fees, even though with low likelihood; so Medium seems appropriate.


The original report as published on Code4rena, by oakcobalt. 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

The withdrawal logic selects every farm with the matching LP denom without filtering by whether the farm is currently active, so future and expired farms are included in the penalty fee split.

Impact

Active farm owners receive smaller penalty fee shares, and a malicious farm creator could take a share of fees without contributing rewards, though the farm creation fee and the owner's ability to close spam farms may limit this.

What the attacker needed

Ability to create a farm; pay the farm creation fee; matching LP denom with an active farm; emergency withdrawals occur

The fix

The withdrawal logic should filter out farms that start in a future epoch or have already expired before dividing the penalty fee; the project said it would likely adopt this.

How it was found

  1. Create a future farm with valid assets and a popular LP denom that already has an active farm.
  2. Wait for emergency withdrawals on positions with that LP denom before the new farm's start time.
  3. Receive a share of the penalty fees as a farm owner.
  4. Close the farm before its epoch starts to recover the deposited assets.

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 is a staking pool that pays rewards. When someone pulls their money out early, they pay a penalty fee that is shared among the people who run farms for that same asset. The bug is that farms which have not started yet, or have already ended, also get a cut. So someone could create a farm that is not yet active, collect part of those fees, and then shut the farm down to get their deposit back.

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 oakcobalt

View all

50 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.