Understatement of poolTotalPeUSDCirculation amounts due to incorrect accounting after function _repay is called
The _repay function reduces poolTotalPeUSDCirculation by the full repayment amount, even though part of that amount is transferred as fees rather than burned. This understates the pool's circulation figure, affecting any logic that relies on it.
hl_ Follow with mahdikarimi, OMEN and 25 others
Originally disclosed on Code4rena
Submitted by hl_, also found by mahdikarimi, OMEN, DedOhWale, Toshii, Kenshin, RedOneN, kenta, Iurii3, mahdikarimi, cccz, gs8nrv, 0xRobocop, hl_, pep7siup, lanrebayode77, bytes032, Co0nan, SpicyMeatball, CrypticShepherd, Musaka, Vagner, Vagner, peanuts, 0xRobocop, peanuts, peanuts, and max10afternoon
Incorrect accounting of poolTotalPeUSDCirculation, results in an understatement of poolTotalPeUSDCirculation amounts. This causes inaccurate bookkeeping and in turn affects any other functions dependent on the use of poolTotalPeUSDCirculation.
Proof of Concept
We look at function _repay of LybraPeUSDVaultBase.sol as follows:
function _repay(address _provider, address _onBehalfOf, uint256 _amount) internal virtual {
try configurator.refreshMintReward(_onBehalfOf) {} catch {}
_updateFee(_onBehalfOf);
uint256 totalFee = feeStored[_onBehalfOf];
uint256 amount = borrowed[_onBehalfOf] + totalFee >= _amount ? _amount : borrowed[_onBehalfOf] + totalFee;
if(amount >= totalFee) {
feeStored[_onBehalfOf] = 0;
PeUSD.transferFrom(_provider, address(configurator), totalFee);
PeUSD.burn(_provider, amount - totalFee);
} else {
feeStored[_onBehalfOf] = totalFee - amount;
PeUSD.transferFrom(_provider, address(configurator), amount);
}
try configurator.distributeRewards() {} catch {}
borrowed[_onBehalfOf] -= amount;
poolTotalPeUSDCirculation -= amount;
emit Burn(_provider, _onBehalfOf, amount, block.timestamp);
}
In particular, note the accounting of poolTotalPeUSDCirculation after repayment as follows:
poolTotalPeUSDCirculation -= amount;
Consider a scenario per below for user Alice, where:
- Amount borrowed = 200
- TotalFee = 2
Repay Scenario (PeUSD) | |
_amount input | 100 |
totalFee | 2 |
amount (repay) | 100 |
Fees left | 0 |
PeUSD transfer to config addr | 2 |
PeUSD burnt | 98 |
borrowed[Alice] | 100 |
poolTotalPeUSDCirculation (X) | X - 100 |
Based on the accounting flow of the function, the fees incurred are transferred to address(configurator). The amount burned is amount - totalFee. However, we see that poolTotalPeUSDCirculation reduces the entire amount where it should be amount - totalFee reduced.
This results in an understatement of poolTotalPeUSDCirculation amounts.
Tools Used
Manual review
Recommended Mitigation Steps
Correct the accounting as follows:
- poolTotalPeUSDCirculation -= amount;
+ poolTotalPeUSDCirculation -= (amount - totalFee);
Assessed type
Error
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 repayment function subtracts the full repayment amount from poolTotalPeUSDCirculation instead of subtracting only the burned portion (amount minus totalFee).
Impact
The recorded poolTotalPeUSDCirculation is lower than it should be, causing inaccurate bookkeeping and affecting other functions that depend on it.
What the attacker needed
A borrower repays a loan with fees
The fix
The accounting should be corrected so that poolTotalPeUSDCirculation is reduced by amount minus totalFee rather than by the full amount.
How it was found
- Alice borrows 200 with a total fee of 2.
- Alice repays 100, of which 2 is transferred as fees and 98 is burned.
- The function subtracts the full 100 from poolTotalPeUSDCirculation instead of 98.
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.
A smart contract keeps a running total of how much of its token is in circulation. When someone repays a loan, part of the money goes to fees and part is destroyed, but the contract subtracts the whole repayment from the total. This makes the recorded total too low, so other parts of the system that trust that number may behave incorrectly.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.