Skip to content
Audit finding Intermediate

execute_claim() possible loss of accuracy or even inability to retrieve funds

The claim_batch function in a vesting contract calculates the amount to send by first dividing and then multiplying, which can cause precision loss and even result in zero amount being claimable. An attacker or user could be unable to retrieve funds due to this rounding error.

bin2chen Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-05-andromeda-ado-judging/issues/49

Found by

bin2chen

Summary

claim_batch() dividing and then multiplying may result in loss of precision, and in the worst case may not retrieve funds

Vulnerability Detail

claim_batch() is used to calculate the amount of money that can be retrieved.

Rust
fn claim_batch(
    querier: &QuerierWrapper,
    env: &Env,
    batch: &mut Batch,
    config: &Config,
    number_of_claims: Option<u64>,
) -> Result<Uint128, ContractError> {
    let current_time = env.block.time.seconds();
    ensure!(
        batch.lockup_end <= current_time,
        ContractError::FundsAreLocked {}
    );
@>  let amount_per_claim = batch.release_amount.get_amount(batch.amount)?;

    let total_amount = AssetInfo::native(config.denom.to_owned())
        .query_balance(querier, env.contract.address.to_owned())?;

    let elapsed_time = current_time - batch.last_claimed_release_time;
    let num_available_claims = elapsed_time / batch.release_unit;

    let number_of_claims = cmp::min(
        number_of_claims.unwrap_or(num_available_claims),
        num_available_claims,
    );

@>  let amount_to_send = amount_per_claim * Uint128::from(number_of_claims);
    let amount_available = cmp::min(batch.amount - batch.amount_claimed, total_amount);

    let amount_to_send = cmp::min(amount_to_send, amount_available);

    // We dont want to update the last_claim_time when there are no funds to claim.
    if !amount_to_send.is_zero() {
        batch.amount_claimed += amount_to_send;
        batch.last_claimed_release_time += number_of_claims * batch.release_unit;
    }

    Ok(amount_to_send)
}

From the code above we know that the calculation is

  1. amount_per_claim = batch.amount * release_amount (release_amount is Decimal, Precision = 1e18)
  2. number_of_claims = elapsed_time / batch.release_unit
  3. amount_to_send = amount_per_claim * number_of_claims

i.e.: amount_to_send = (batch.amount * release_amount / 1e18) * number_of_claims

Since it is dividing and then multiplying, it may lead to loss of precision, even amount_per_claim = 0 Assumption: it takes 5 years to claim 1 btc, batch.amount = 1e8 btc release_unit = 1 second release_amount = 1e8 * 1e18 / 157680000(seconds) / 1e8 = 6341958396 (6341958396 percent per second, precision 1e18)

Following the existing formula, divide and multiply.

amount_to_send = (1e8 * 6341958396 / 1e18) * 157680000(seconds) = 0

If modified to multiply before dividing:

amount_to_send = (1e8 * 6341958396 * 157680000(seconds) / 1e18 = 99999999

Impact

claim_batch() dividing and then multiplying may result in a loss of precision, and in the worst case it may not be possible to retrieve the funds

Code Snippet

https://github.com/sherlock-audit/2024-05-andromeda-ado/blob/main/andromeda-core/contracts/finance/andromeda-vesting/src/contract.rs#L462

Tool used

Manual Review

Recommendation

In case of WithdrawalType::Percentage, multiply then divide Example: `batch.amount * number_of_claims * release_amount / 1e18

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/andromedaprotocol/andromeda-core/pull/555

bin2chen66

https://github.com/andromedaprotocol/andromeda-core/pull/555 This PR modifies get_amount() to return Decimal, which improves precision and solves the problem of precision loss. At the same time, when creating execute_create_batch(), it checks !release_amount.get_amount(funds.amount)? .is_zero() to avoid the value being too low Fixed this issue

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

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 order of operations in the calculation: dividing before multiplying causes truncation and precision loss.

Impact

Users may lose precision in their claimable amount, and in the worst case, they may be unable to retrieve any funds.

What the attacker needed

Vesting contract with percentage withdrawal

The fix

The issue was fixed by modifying get_amount() to return Decimal and adding a check to prevent zero amounts.

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.

Smart contracts often use fixed-point arithmetic where numbers are represented as integers scaled by a large factor (like 1e18). If you divide before multiplying, you can lose precision because the division result is rounded down. In this case, the amount per claim could become zero, meaning the user gets nothing even though they should receive funds.

More by bin2chen

View all

139 more disclosed reports, best first.

More from Andromeda – Validator Staking ADO and Vesting ADO

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.