Wrong direction of rounding in redeem may lead to drain if exchange rate grows large
The redeem function rounds the number of shares to burn down instead of up, so a user redeeming a fixed amount of underlying pays slightly fewer shares than they should. A first depositor can exploit this rounding repeatedly to inflate the exchange rate to an extreme value, after which later depositors' funds can be redeemed for almost no shares.
Source: https://github.com/sherlock-audit/2025-07-malda-judging/issues/81
Found by
cergyk
Description
In the mToken contract, forked from compound v2, the same wrong direction rounding is introduced in the __redeem function:
if (redeemTokensIn > 0) {
/*
* We calculate the exchange rate and the amount of underlying to be redeemed:
* redeemTokens = redeemTokensIn
* redeemAmount = redeemTokensIn x exchangeRateCurrent
*/
redeemTokens = redeemTokensIn;
redeemAmount = mul_ScalarTruncate(exchangeRate, redeemTokensIn);
} else {
/*
* We get the current exchange rate and calculate the amount to be redeemed:
* redeemTokens = redeemAmountIn / exchangeRate
* redeemAmount = redeemAmountIn
*/
>> redeemTokens = div_(redeemAmountIn, exchangeRate); //@audit should round up, as this is the amount of tokens pulled from user
redeemAmount = redeemAmountIn;
}
This wrong direction of rounding leads to pull less shares from the user while the amount sent out is the same as requested. In most cases the losses should be negligible (1 wei of shares per operation), because the division operates at 1e18 precision, and the exchange rate starts as a small value initially (0.02).
Inflating exchange rate
We will show that the first depositor is able to increase the exchange rate by an arbitrary factor, by using the rounding against him during mint and redeem(shares), when supply is initially zero.
As can be seen in the mint function:
uint256 mintTokens = div_(actualMintAmount, exchangeRate);
require(mintTokens >= minAmountOut, mt_MinAmountNotValid());
// avoid exchangeRate manipulation
if (totalSupply == 0) {
//@audit 1000 of shares are minted to the zero address
totalSupply = 1000;
accountTokens[address(0)] = 1000;
//@audit underflow if initial mint is below 1000
mintTokens -= 1000;
}
When the totalSupply is zero, at least 1000 shares have to be minted initially, which means that 20 wei of token has to be supplied (due to the 0.02 initial exchange rate).
Reach exchange rate of exp
Then the first depositor repeats the steps:
- Mint by providing 1 wei of underlying (mints 50 shares to the depositor)
- Redeem 25 shares *2 -> sends 0 underlying to the depositor
each time these steps are repeated, the totalUnderlying of the market is increased by 1 while totalSupply is unchanged. This means the exchangeRate is increasing. We need to repeat this 980 times to reach echangeRate == exp
As a side note, the host chain here is
Linea, and although repeating these steps many times costs a lot of gas (around 50M), the gas would be very cheap; Such a step of 1000 iterations only costs around 0.20$ at current ethereum price. Also please note that the gas limit by block is very large (2B).
Increase exchange rate exponentially
When exchangeRate > exp, the process is simpler we only need to provide 1 wei and 0 shares are minted. From now on, we double the exchange rate at each step of 1000 iterations:
Indeed when exchange rate is X*exp, we can call mint with X wei, which will mint 0 shares.
With this in mind and taking the example of USDC, reaching an exchange rate value of 1e6*exp, would take approx 20 iterations and cost ~5$ of gas, plus the cost of donating 1000$ of token to the market.
Target exchange rate of 1e6*exp reached
The attacker just has to wait for new depositors to supply into the market, and then he can call redeemUnderlying 1e6 by 1e6, which will burn zero shares.
Recommendation
Consider fixing the rounding in redeem, make the division by exchange rate round up: mToken.sol#L614-L630:
} else {
/*
* We get the current exchange rate and calculate the amount to be redeemed:
* redeemTokens = redeemAmountIn / exchangeRate
* redeemAmount = redeemAmountIn
*/
- redeemTokens = div_(redeemAmountIn, exchangeRate);
+ redeemTokens = divUp_(redeemAmountIn, exchangeRate);
redeemAmount = redeemAmountIn;
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/malda-protocol/malda-lending/pull/117
CergyK
Fix looks good, redeem underlying now rounds up, in favor of the protocol
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 redeem-by-amount branch divides the requested underlying amount by the exchange rate using truncating division, so it burns fewer shares than the value being paid out.
Impact
An attacker could inflate the exchange rate and then redeem underlying while burning zero shares, draining funds supplied by later depositors; the text notes the attack costs significant gas but is cheap on the host chain.
What the attacker needed
Attacker is first depositor; enough gas for many iterations; later victims deposit into market
The fix
The division by the exchange rate in redeem should round up; the protocol team fixed this and the reviewer confirmed redeem underlying now rounds up in favour of the protocol.
How it was found
- Mint initially so total supply is non-zero, meeting the 1000-share minimum.
- Repeatedly mint 1 wei of underlying and redeem half the resulting shares, which sends 0 underlying back but raises totalUnderlying while supply stays the same.
- Repeat until the exchange rate exceeds exp, then mint with an amount that yields zero shares to double the rate each round.
- Wait for other depositors to supply into the market.
- Call redeemUnderlying for the inflated amount, which burns zero shares.
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.
In a lending pool, shares represent a claim on the pool's underlying tokens, and the exchange rate says how many tokens one share is worth. When someone redeems a fixed amount of tokens, the contract works out how many shares to take back, and it should round that number up so the pool never gives away a tiny bit extra. Here it rounded down, and by repeating tiny deposits and redemptions an attacker could push the exchange rate to a huge value, so later users' deposits could be taken for almost nothing.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.