ProfitManager’s creditMultiplier calculation does not count undistributed rewards; this can cause value losses to users
The ProfitManager contract calculates the creditMultiplier using totalSupply() instead of targetTotalSupply(), so when a loss is reported, the multiplier is slashed more than it should be. This causes credit token holders to lose value, with some collateral left locked in the SimplePSM contract.
3docSec Follow with aslanbek, rvierdiiev and 2 others
Originally disclosed on Code4rena
Submitted by 3docSec, also found by aslanbek, rvierdiiev, btk, and ether_sky
The function notifyPnL in ProfitManager is called by LendingTerms when a loan is closed to report a profit or loss.
In case there is a loss, the amount in excess of the protocol’s buffer is attributed to the credit token holders. This is done by slashing the creditMultiplier with the effect of depreciating the credit token relatively to the collateral.
The math for slashing the creditMultiplier is the following:
File: ProfitManager.sol
292: function notifyPnL(
293: address gauge,
294: int256 amount
295: ) external onlyCoreRole(CoreRoles.GAUGE_PNL_NOTIFIER) {
---
330: // update the CREDIT multiplier
331: uint256 creditTotalSupply = CreditToken(_credit).totalSupply();
332: uint256 newCreditMultiplier = (creditMultiplier *
333: (creditTotalSupply - loss)) / creditTotalSupply;
334: creditMultiplier = newCreditMultiplier;
It is particularly interesting that totalSupply() is used, since in a similar high-level accounting calculation, totalBorrowedCredit(), targetTotalSupply() is used instead:
File: ProfitManager.sol
172: function totalBorrowedCredit() external view returns (uint256) {
173: return
174: CreditToken(credit).targetTotalSupply() -
175: SimplePSM(psm).redeemableCredit();
176: }
The usage of totalSupply() here can be problematic when a significant portion of the supply is in undistributed rewards. In this case, the creditMultiplier slashing will be higher than it should because totalSupply() will return a much lower value than targetTotalSupply().
Impact
creditMultiplier slashing will always be higher than it should for correct accounting, penalizing credit token holders, and finally locking value in the protocol. The negative effects will be proportional to the relative supply of credit tokens in undistributed rewards versus the interpolated supply of the credit token.
Proof of Concept
The following PoC in Foundry (full setup here) shows how an overshot creditMultiplier slashing locks collateral value in SimplePSM, with a net loss for protocol users, in a real-wold scenario:
function testH2() external {
uint ts = block.timestamp;
// Set ProfitManager to 100% rewards for rebasing users
pm.setProfitSharingConfig(
0, // surplusBufferSplit,
1e18, // creditSplit,
0, // guildSplit,
0, // otherSplit,
address(0) // otherRecipient
);
// User 1 deposit 3000 USDC in PSM, gets 3000 gUSDC, enters rebase
address user1 = address(1);
vm.startPrank(user1);
coll.mint(user1, 3_000e18);
coll.approve(address(sPsm), 3_000e18);
sPsm.mintAndEnterRebase(3_000e18);
// User 2 open Loan A, 1000 gUSDC, redeems for 1000 USDC
address user2 = address(2);
vm.startPrank(user2);
coll.mint(user2, 1_000e18);
coll.approve(address(lt), 1_000e18);
bytes32 loanA = lt.borrow(1_000e18, 1_000e18);
ct.approve(address(sPsm), 1_000e18);
sPsm.redeem(user2, 1_000e18);
// User 3 open Loan B, 1000 gUSDC, redeems for 1000 USDC
address user3 = address(3);
vm.startPrank(user3);
coll.mint(user3, 1_000e18);
coll.approve(address(lt), 1_000e18);
bytes32 loanB = lt.borrow(1_000e18, 1_000e18);
ct.approve(address(sPsm), 1_000e18);
sPsm.redeem(user3, 1_000e18);
// User 4 open Loan C, 1000 gUSDC, redeems for 1000 USDC
address user4 = address(4);
vm.startPrank(user4);
coll.mint(user4, 1_000e18);
coll.approve(address(lt), 1_000e18);
bytes32 loanC = lt.borrow(1_000e18, 1_000e18);
ct.approve(address(sPsm), 1_000e18);
sPsm.redeem(user4, 1_000e18);
// Time passes, all loans accrue 50% interest, loan B gets called
ts += lt.YEAR() - 3 weeks;
vm.warp(ts);
lt.call(loanB);
ts += 3 weeks;
vm.warp(ts);
// User 2 deposit 1500 USDC in PSM, gets 1500 gUSDC, and repay Loan A (500 profit) -> 1500 USDC in PSM
vm.startPrank(user2);
coll.mint(user2, 500e18);
coll.approve(address(sPsm), 1500e18);
sPsm.mint(user2, 1500e18);
ct.approve(address(lt), 1500e18);
lt.repay(loanA);
// Now User 1's 3000 gUSDC balance is interpolating towards 3500 gUSDC
assertEq(3_000e18, ct.totalSupply());
assertEq(ct.totalSupply(), ct.balanceOf(user1));
assertEq(3_500e18, ct.targetTotalSupply());
// --- Everything good till here; now we get to the bug:
// User 3 completely defaults on Loan B, 1000 gUSDC loss is reported,
// creditMultiplier becomes 1e18 * (3000 - 1000) / 3000 = 0.6667e18
// 🚨 if targetTotalSupply was used, this would be 1e18 * (3500 - 1000) / 3500 = 0.714285e18
ah.forgive(loanB);
assertApproxEqRel(pm.creditMultiplier(), 0.6667e18, 0.0001e18 /* 0.01% */);
// User 4's Loan C now owes 1500 / 0.66667 = 2250 gUSDC
uint loanCdebt = lt.getLoanDebt(loanC);
assertApproxEqRel(loanCdebt, 2250e18, 0.0001e18 /* 0.01% */);
// User 4 deposit 1500 USDC in PSM, gets 2250 gUSDC, and repay Loan C (750 profit) -> 3000 USDC in PSM
vm.startPrank(user4);
coll.mint(user4, 500e18);
coll.approve(address(sPsm), 1500e18);
sPsm.mint(user4, 1500e18);
ct.approve(address(lt), loanCdebt);
lt.repay(loanC);
// Now User 1's 3000 gUSDC balance is interpolating towards 4250
assertEq(3_000e18, ct.totalSupply());
assertEq(ct.totalSupply(), ct.balanceOf(user1));
assertApproxEqRel(4_250e18, ct.targetTotalSupply(), 0.0001e18 /* 0.01% */);
// User 1 waits for the interpolation to end
ts += ct.DISTRIBUTION_PERIOD();
vm.warp(ts);
// User 1 redeems 4250 gUSDC for 4250 * 0.66667 = 2833 USDC -> 167 USDC in PSM (🚨 there should be no leftover)
vm.startPrank(user1);
ct.approve(address(sPsm), ct.balanceOf(user1));
sPsm.redeem(user1, ct.balanceOf(user1));
assertApproxEqRel(2833.3e18, coll.balanceOf(user1), 0.0001e18 /* 0.01% */);
// 🚨 this value remains locked in the SimplePSM contract as a result of the incorrect accounting
assertApproxEqRel(166.66e18, coll.balanceOf(address(sPsm)), 0.0001e18 /* 0.01% */);
// ℹ️ if ProfitManager used targetTotalSupply, the value locked would be ~2e4 lost to rounding
}
Tools Used
Foundry
Recommended Mitigation Steps
Consider using targetTotalSupply() instead of totalSupply() in the creditMultiplier slashing calculation:
// ProfitManager:330
// update the CREDIT multiplier
- uint256 creditTotalSupply = CreditToken(_credit).totalSupply();
+ uint256 creditTotalSupply = CreditToken(_credit).targetTotalSupply();
uint256 newCreditMultiplier = (creditMultiplier *
(creditTotalSupply - loss)) / creditTotalSupply;
creditMultiplier = newCreditMultiplier;
emit CreditMultiplierUpdate(
block.timestamp,
newCreditMultiplier
);
Assessed type
Math
eswak (Ethereum Credit Guild) confirmed, but disagreed with severity and commented:
Confirming this issue, and thanks a lot for the quality of the report!
I wonder if this should be qualified as “Medium”, as duplicates of this issue are qualified as Medium. Even though the protocol could end up with funds left in the PSM, all users are treated the same way, and there is always the possibility to recover the funds and organize and airdrop / multisend.
TrungOre (judge) decreased severity to Medium
@TrungOre - I believe that this issue should be a high because the
creditMultiplierdirectly influences the value of CREDIT in the system. Using the incorrect method will lead to an inaccurate representation of the CREDIT value. Note thatcreditMultipliercan only decrease, and due to the current logical error, it’s diminishing too quickly, resulting in direct losses for borrowers and holders.
I agree with @btk - this issue should be a high.
@eswak - Could you take a second look at this? Doesn’t the
CreditToken.totalSupply()return the total number of the tokens in existence? If a slashing event occurs, why would thecreditMultiplierneed to consider the future rewards (__unmintedRebaseRewards.targetValue)? Aren’t those tokens locked until a date in the future? Why would the slashing consider those future rewards in the present?Suppose a slashing occurs on day 2 of the reward’s distribution, why would the rewards from day 3 to day 30 need to be considered as part of the existing supply? Is it not the purpose of the distribution of rewards to lock rewards and as time passes, slowly unlock them to be part of the existing supply?
I think the value of the
creditMultiplieris correctly calculated by using the amount of existing supply at the moment when the slashing event occurs. I reiterate again, why is a supply that is meant to be unlocked in the future would need to be considered in the present?I just want to emphasize, the
unmintedRewardsare indeed considered when reading fromCreditToken.totalSupply(). Understanding thatunmintedRewardsare those rewards that have been unlocked based on the total time that has passed since they have been added to the distribution period. On the other hand, the future unminted rewards (__unmintedRebaseRewards.targetValue) are the ones that I believe should not be considered part of the supply in the present, because those rewards are yet to be slowly added to the supply in the future, not in the present.
I’ve taken a second look at the report and the problem seems to be a legit issue, if the undistributed rewards are not included as part of the existing supply at the moment of the slashing, that would bring down the
creditMultipliermore than what it should. If all the undistributed rewards are included, this would cause that the PSM module has more PeggedTokens than what they could be claimed for using the totalCreditTokensin existence.As for the severity, I think @eswak has a point in his comment (severity should be medium). All the funds that would’ve been left in the PSM could’ve been recovered and distributed to the users so they ended up equal as if the
creditMultiplierwould’ve been updated considering all the supply. The users would not lose their assets permanently, right? It would be a temporary loss while the extra funds are correctly distributed. It may take some extra work, but in the end, all users could get the exact amount of PeggedTokens they could’ve claimed if thecreditMultiplierhad been updated correctly. No PeggedTokens would be lost in reality and there would only be more PeggedTokens in the PSM module than the total PeggedTokens that could be claimed for. Thus, the extra tokens can be claimed by governance and distributed accordingly, making whole to all the users.TL:DR; no funds are lost, the extra tokens are left in the PSM module, and they can be claimed by governance and distributed accordingly.
Plus, all new minting and borrowing, and basically everything, from that point onwards will use the new value of the
creditMultiplier, even though it was brought down more than what it should. All the accounting from that point and beyond it will use the new value of thecreditMultiplier.
@0xStalin - the point of the credit multiplier update, and the goal of the math behind it, is to depreciate the synthetic versus the underlying so that:
- All synthetics are redeemable (solvency).
- When all synthetics are redeemed, the protocol does not hold value locked (correctness to users).
This report proves how the second point is not respected, so value can remain locked in the protocol at the user’s expense. This should be clear in the PoC but you are very right that I should have point it out in the report.
@TrungOre - A few wardens suggested this finding could be high and I tend to agree with that because the remediation of a governance action suggested by the sponsor may in practice not always be feasible in terms of:
- Time to figure out a potentially large number of affected users and the exact extent of the loss for each of them.
- Preparing and having governance token holders review and vote a large proposal that includes an emergency action for each and every one of the affected users.
- The cost in terms of gas of executing such proposal and the “always lost” choice of whether refunding users whose loss is lower than the cost of gas to refund them.
@3docSec - For the points that you’ve just mentioned is exactly why this fits as a medium severity. Yes, it may take some extra work to governance for preparing the distribution of the extra PeggedTokens left in the PSM module. As for the second point, how much gas does it take to make an ERC20 transfer? Include it inside a batch of transfers, and how much would it cost to make full the user? Unless the user’s difference was less than 1-2 usd then it might be more expensive to credit the difference, but even though, that is doable, that’s the reason why this is fine as a medium severity. For a report to be classified as a high severity, there should be a permanent loss of funds, a way that it is impossible to recover them.
@0xStalin, not all borrowers will redeem their credit in the PSM. Those holding credit during a loss will incur more losses than they should. Any thoughts on how governance can refund them?
@btk - Those incurred losses for the holders are exactly what it should be distributed by governance. As for the question of how could governance refund them, The SimplePSM contract inherits from the CoreRef contract, the CoreRef has the
emergencyAction(), governance can simply craft a calldata to transfer the extra PeggedTokens that needs to be distributed to the holders who were impacted by thecreditMultipliergoing down more than what it should. So, governance transfers those PeggedTokens to an account of their own, they collect the information of how many PeggedTokens need to be sent to who to cover the difference and then they do those transfers in batch. That’s it, governance recovered the extra tokens and distributed them.That is because I consider this issue not to be a high severity. The tokens are not lost and they are not stuck forever in the PSM.
I consider this issue as medium because it doesn’t cause a direct loss to the protocol or users. As the sponsor stated, the protocol could end up with funds left in the PSM, but all users are treated the same way, and Governor can still recover the funds by
emergencyActions.
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
The creditMultiplier is slashed more than it should be, causing credit token holders to lose value and leaving collateral locked in the SimplePSM contract; the locked funds could potentially be recovered by governance, but users suffer a loss.
What the attacker needed
A loan defaults causing a loss; undistributed rewards exist; loss exceeds the protocol buffer
The fix
The creditMultiplier slashing calculation should use targetTotalSupply() instead of totalSupply().
How it was found
- Set ProfitManager to give 100% of rewards to rebasing users.
- User 1 deposits 3000 USDC into the PSM and enters rebase, receiving 3000 gUSDC.
- Users 2, 3, and 4 each open a loan of 1000 gUSDC and redeem it for 1000 USDC.
- Time passes, loans accrue interest, and loan B is called.
- User 2 repays loan A with a 500 USDC profit, increasing the target total supply to 3500 gUSDC while totalSupply remains 3000.
- User 3 defaults on loan B, causing a 1000 gUSDC loss; the creditMultiplier is slashed to about 0.6667e18 instead of the correct 0.714285e18.
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 this system, a credit token's value is adjusted by a multiplier when losses occur. The multiplier is meant to spread the loss fairly across all tokens, including rewards that are earned but not yet claimed. The bug is that the calculation ignores those pending rewards, so the multiplier drops too much, making everyone's tokens worth less than they should be and leaving extra collateral stuck in the contract.
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.