Skip to content
Smart contracts Audit finding Advanced

Rounding error in WUSDA can result in loss of user funds, especially when manipulated by an attacker

A rounding error in the WUSDA contract's conversion from USDA to WUSDA can cause depositors to receive zero or fewer WUSDA tokens than expected. An attacker could exploit this by using a flash loan to inflate the total USDA supply, front-running a victim's deposit so it mints little or no WUSDA, and then withdrawing the inflated supply to profit at the victim's expense.

giovannidisiena Follow with giovannidisiena, mert_eren and 10 others

Originally disclosed on Code4rena

Submitted by giovannidisiena, also found by giovannidisiena, mert_eren, pep7siup, Bauchibred, qpzm (1, 2, 3), said (1, 2), Giorgio (1, 2), ljmanini, Musaka, SpicyMeatball (1, 2), 0xbranded (1, 2), 0xComfyCat, 0xWaitress (1, 2), erebus, kutugu (1, 2), josephdara, adeolu, ak1, and SanketKogekar

Due to floor rounding in Solidity, it is possible for rounding errors to affect functions in the WUSDA contract.

Specifically, when WUSDA::_usdaToWUSDA is invoked internally, if _usdaAmount is sufficiently small compared with MAX_wUSDA_SUPPLY (10M) and the total USDA supply then it is possible for the _wusdaAmount return value to round down to 0.

function _usdaToWUSDA(uint256 _usdaAmount, uint256 _totalUsdaSupply) private pure returns (uint256 _wusdaAmount) {
    _wusdaAmount = (_usdaAmount * MAX_wUSDA_SUPPLY) / _totalUsdaSupply;
  }

When calling the WUSDA::deposit/depositTo functions, which call the internal WUSDA::_deposit function with this rounded _wusdaAmount value, the implication is that depositors could receive little/no WUSDA for their USDA. Additionally, calls to the WUSDA::withdraw/withdrawTo functions could be process without actually burning any WUSDA. This is clearly not desirable as USDA can be subsequently redeemed for sUSD (at the expense of the protocol reserves and its other depositors).

function deposit(uint256 _usdaAmount) external override returns (uint256 _wusdaAmount) {
    _wusdaAmount = _usdaToWUSDA(_usdaAmount, _usdaSupply());
    _deposit(_msgSender(), _msgSender(), _usdaAmount, _wusdaAmount);
  }

function withdraw(uint256 _usdaAmount) external override returns (uint256 _wusdaAmount) {
    _wusdaAmount = _usdaToWUSDA(_usdaAmount, _usdaSupply());
    _withdraw(_msgSender(), _msgSender(), _usdaAmount, _wusdaAmount);
  }

Even when the USDA supply is sufficiently small to avoid this issue, an attacker could still utilize a sUSD flash loan to artificially inflate the USDA total supply which causes the down rounding, potentially front-running a victim such that their deposit is processed without minting any WUSDA and then withdrawing the inflated USDA supply to repay the flash loan.

Proof of Concept

normal case:

  • Total USDA supply is 10B.
  • Alice calls WUSDA::deposit with _usdaAmount of 1e23 (10_000 tokens).
  • _wusdaAmount is rounded down to 1e20/100 tokens (1e23 * 1e25 / 1e28 = 1e20).
  • Alice receives 100 WUSDA rather than 10_000.
  • In reality, the total supply of sUSD is around 40M, so a more realistic scenario assuming 10M total USDA supply may be that Alice deposits 10e18 and receives 1 WUSDA or 1/10th the expected amount (10e18 * 1e25 / 1e26 = 1e18).

flash loan & attacker front-running case:

  • Total USDA supply is 1M.
  • Alice calls WUSDA::deposit with _usdaAmount of 9.9e22 (99_000 tokens).
  • Attacker front-runs this transaction and calls WUSDA::deposit with _usdaAmount of 3.3e22 (33_000 tokens).
  • Attacker receives 33_000 WUSDA.
  • Attacker takes out a sUSD flash loan of 39M.
  • Attacker deposits 39M sUSD into USDA.
  • Alice’s transaction is included here, but _wusdaAmount is rounded down to 24_750 tokens (9.9e22 * 1e25 / 4e25 = 2.475e22).
  • Alice receives 24_750 WUSDA rather than 99_000 (only 1/4 of the expected amount).
  • Attacker backruns Alice’s transaction and calls WUSDA::withdraw with _usdaAmount of 132e21 (132_000 tokens).
  • Using the same rounding logic, _wusdaAmount is rounded down to 33_000 tokens (132e21 * 1e25 / 4e25 = 33e22).
  • Attacker withdraws 132_000 WUSDA and receives the 99_000 USDA deposited by Alice in addition to their original 33_000.
  • Attacker redeems USDA, repays the flash loan and profits 90_000 USDA/sUSD at the expense of Alice.

Recommended Mitigation Steps

Consider reverting if either WUSDA::_usdaToWUSDA or WUSDA::_wUSDAToUSDA return a zero value. Also consider multiplication by some scalar precision amount. Protections against manipulation of USDA total supply within a single block would also be desirable, perhaps achievable by implementing some multi-block delay.

0xShaito (Amphora) confirmed


The original report as published on Code4rena, by giovannidisiena. 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 integer division in the conversion function rounds down to zero when the deposit amount is small relative to the total supply, and the code does not check for or prevent this.

Impact

Depositors could receive little or no WUSDA for their USDA, and an attacker could profit at their expense, potentially draining protocol reserves.

What the attacker needed

Attacker can take a flash loan; attacker can front-run transactions; victim makes a deposit

The fix

The code should revert if the conversion returns zero and should use a scalar precision multiplier; protections against single-block manipulation of the total supply, such as a multi-block delay, should also be considered.

How it was found

  1. Attacker front-runs the victim's deposit transaction with their own deposit.
  2. Attacker takes out a large flash loan of sUSD.
  3. Attacker deposits the borrowed sUSD into USDA, inflating the total USDA supply.
  4. Victim's deposit transaction is processed, but the WUSDA amount is rounded down significantly.
  5. Attacker back-runs the victim's transaction and withdraws their USDA, receiving more than they deposited.
  6. Attacker redeems the USDA, repays the flash loan, and keeps the profit.

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.

When you deposit one token into a contract, the contract often calculates how many 'shares' you should get in return. If that calculation rounds down to zero because of a small deposit or a large total supply, you get nothing for your tokens. An attacker can make the total supply very large temporarily (using a flash loan, a loan that must be repaid in the same transaction) so that a victim's deposit also rounds down, then take the victim's tokens.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by giovannidisiena

View all

1 more disclosed report, best first.

More from Amphora Protocol

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.