Skip to content
Smart contracts Audit finding Intermediate

Attackers can call UToken.redeem() and drain the funds in assetManager

The redeem function in UToken does not check whether the calculated uTokenAmount is zero, allowing an attacker to redeem underlying tokens without burning any uTokens. An attacker could repeatedly call redeem with a small amountOut to drain funds from assetManager.

chaduke Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-union-judging/issues/12

Found by

chaduke

Summary

Attackers can call UToken.redeem() and drain the funds in assetManager, taking advantage of the following vulnerability: due to round error, it is possible that uTokenAmount = 0; the redeem()function does not check whether uTokenAmount = 0 and will redeem the amount of underlyingAmount even when zero uTokens are burned.

Vulnerability Detail

Consider the following attack scenario:

  1. Suppose exchangeRate = 1000 WAD, that is each utoken exchanges for 1000 underlying tokens.

  2. Attacker B calls redeem(0, 999), then the else-part of the following code will get executed:

JavaScript
 if (amountIn > 0) {
            // We calculate the exchange rate and the amount of underlying to be redeemed:
            // uTokenAmount = amountIn
            // underlyingAmount = amountIn x exchangeRateCurrent
            uTokenAmount = amountIn;
            underlyingAmount = (amountIn * exchangeRate) / WAD;
        } else {
            // We get the current exchange rate and calculate the amount to be redeemed:
            // uTokenAmount = amountOut / exchangeRate
            // underlyingAmount = amountOut
            uTokenAmount = (amountOut * WAD) / exchangeRate;
            underlyingAmount = amountOut;
        }
  1. we have uTokenAmount = 999*WAD/1000WAD = 0, and underlyingAmount = 999.

  2. Since redeeem() does not check whether uTokenAmount = 0 and the function will proceed. When finished, the attacker will get 999 underlying tokens, but burned no utokens. He stole 999 underlying tokens.

  3. The attacker can accomplish draining the assetManger by writing a malicious contract/function using a loop to run redeem(0, exchangeRate/WAD-1) multiple times (as long as not running of gas) and will be able to steal more funds in one SINGLE transaction. Running this transaction a few times will drain assetManager easily. This attack will be successful when exchangeRate/WAD-1 > 0. Here we need to consider that exchangeRate might change due to the decreasing of totalReeemable. So in each iteration, when we call redeem(0, exchangeRate/WAD-1), the second argument is recalculated.

Impact

An attacker can keep calling redeem() and drain the funds in assetManager.

Code Snippet

https://github.com/sherlock-audit/2023-02-union/blob/main/union-v2-contracts/contracts/market/UToken.sol#L737-L771

Tool used

Remix

Manual Review

Recommendation

Revise redeem() so that it will revert when uTokenAmount = 0.

Discussion

kingjacob

points valid, but not easy to increase exchangeRate exponentially. becasue exchangeRate starts from 1e18, 10x of exchangeRate means UToken value, by accruing interests, has increased 10x comparing to the underlying DAI. and the attack can profit 9 DAI in that case)

hrishibhat

Agree with the Sponsor on the severity of the issue as exchangeRate cannot be increase exponentially. Considering this a valid medium.

kingjacob

https://github.com/unioncredit/union-v2-contracts/pull/113

dmitriia

Looks ok

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

Impact

An attacker could drain the funds in assetManager, but the sponsor notes that increasing the exchange rate exponentially is not easy, limiting the profit.

What the attacker needed

ExchangeRate/WAD-1 > 0; attacker can call redeem repeatedly

The fix

The redeem function should be revised to revert when uTokenAmount is zero.

How it was found

  1. Assume the exchange rate is 1000 WAD (each uToken equals 1000 underlying tokens).
  2. Attacker calls redeem with amountIn=0 and amountOut=999.
  3. The contract calculates uTokenAmount = (999 * WAD) / 1000 WAD = 0 and underlyingAmount = 999.
  4. Since uTokenAmount is zero, no uTokens are burned, but the attacker receives 999 underlying tokens.
  5. The attacker can repeat this in a loop, recalculating amountOut each time, to drain assetManager.

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 redeem tokens, the contract calculates how many uTokens you must burn to get the underlying tokens. Due to rounding, this calculation can result in zero uTokens needed, but the contract still sends you the underlying tokens. An attacker can exploit this to get free tokens, potentially draining the contract's funds.

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 chaduke

View all

21 more disclosed reports, best first.

More from Union Finance Update

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.