PoolV3#repayCreditAccount() use incorrect share converting function to calculate profit and loss
The repayCreditAccount function used convertToShares instead of _convertToShares to calculate profit and loss shares, which applies the wrong exchange rate. This could cause the treasury to receive or burn an incorrect number of lpETH shares.
Submitted by 0xpiken, also found by Afriauditor (1, 2), Agontuk, VAD37, monrel, and Trooper
https://github.com/code-423n4/2024-07-loopfi/blob/main/src/PoolV3.sol#L549
https://github.com/code-423n4/2024-07-loopfi/blob/main/src/PoolV3.sol#L553
Impact
Either the profit or the loss is calculated incorrectly, resulting in the treasury owns incorrect profit balance.
Proof of Concept
Anyone can deposit WETH for lpETH by calling PoolV3#deposit() or PoolV3#mint(). The exchange rate of WETH:lpETH is 1:1. The eligible credit manager can borrow WETH by calling PoolV3#lendCreditAccount(), and repay the debt and profit lately by calling PoolV3#repayCreditAccount(). The corresponding amount of lpETH will be minted to treasury if there is profit, and the corresponding amount of lpETH should be burned from treasury if there is any loss:
if (profit > 0) {
@> _mint(treasury, convertToShares(profit)); // U:[LP-14B]
} else if (loss > 0) {
address treasury_ = treasury;
uint256 sharesInTreasury = balanceOf(treasury_);
@> uint256 sharesToBurn = convertToShares(loss);
if (sharesToBurn > sharesInTreasury) {
unchecked {
emit IncurUncoveredLoss({
creditManager: msg.sender,
loss: convertToAssets(sharesToBurn - sharesInTreasury)
}); // U:[LP-14D]
}
sharesToBurn = sharesInTreasury;
}
_burn(treasury_, sharesToBurn); // U:[LP-14C,14D]
}
However, convertToShares() is used to calculate shares for profit and loss, while _convertToShares() is used to calculate shares in PoolV3#deposit(). convertToShares() uses the exchange rate of E4626 to calculate shares instead of 1:1 exchange rate defined in PoolV3.
The incorrect convertToShares() call could highly return less shares than expected, resulting in the treasury owning incorrect balance.
Recommended Mitigation Steps
Use _convertToShares() for share calculation:
if (profit > 0) {
- _mint(treasury, convertToShares(profit)); // U:[LP-14B]
+ _mint(treasury, _convertToShares(profit));
} else if (loss > 0) {
address treasury_ = treasury;
uint256 sharesInTreasury = balanceOf(treasury_);
- uint256 sharesToBurn = convertToShares(loss);
+ uint256 sharesToBurn = _convertToShares(loss);
if (sharesToBurn > sharesInTreasury) {
unchecked {
emit IncurUncoveredLoss({
creditManager: msg.sender,
loss: convertToAssets(sharesToBurn - sharesInTreasury)
}); // U:[LP-14D]
}
sharesToBurn = sharesInTreasury;
}
_burn(treasury_, sharesToBurn); // U:[LP-14C,14D]
}
Assessed type
Math
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 code called the public convertToShares function, which uses the E4626 exchange rate, instead of the internal _convertToShares function that uses the PoolV3 1:1 rate.
Impact
The treasury could own an incorrect profit balance because the share calculation may return fewer shares than expected.
What the attacker needed
A credit manager repays a loan; profit or loss occurs
The fix
The code should use _convertToShares() instead of convertToShares() for both profit minting and loss burning.
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 a borrower repays a loan, the pool calculates how many shares of profit or loss the treasury should get or lose. The code used a conversion function that applies a different exchange rate than the one used for deposits, so the treasury's share balance could be wrong.
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.