PnL system can be broken by large users intentionally or unintentionally.
mediumLines of code
Vulnerability details
Impact
The function notifyPnL in ProfitManager.sol is responsible for accounting the profits gained by the system, and also the losses, slashing and recovery of bad loans. When a loss is encountered, gauge votes are slashed and if there is a loss to the system, the creditMultiplier is decreased to reflect the loss.
The creditMultiplier is updated in case of a loss in the following snippet.
solidity// update the CREDIT multiplier uint256 creditTotalSupply = CreditToken(_credit).totalSupply(); uint256 newCreditMultiplier = (creditMultiplier * (creditTotalSupply - loss)) / creditTotalSupply; creditMultiplier = newCreditMultiplier;
Here the term creditTotalSupply - loss is calculated to calculate the new credit multiplier. The issue is that the loss can be larger than the creditTotalSupply for large loans. This will lead to a revert, and block the function notifyPnL breaking the gauge slashing and voting systems as well.
This scenario can happen in the following manner:
- Alice deposits USDC and mints 1e18 gUSDC. Say she is the largest participator in the market.
- Alice goes to the SurplusGuildMinter contract and stakes the some of the minted gUSDC tokens (say 0.8e18) on her own term.
- Alice defaults due to some depeg event. System enters a loss.
Now, the code will enter the notifyPnL function with some large loss amount. Since Alice provided only a part of the tokens to the surplusbuffer in step 2, we can assume loss > _surplusBuffer. This executes the part of the code which updates the creditMultiplier.
solidityelse { // empty the surplus buffer loss -= _surplusBuffer; surplusBuffer = 0; CreditToken(_credit).burn(_surplusBuffer); emit SurplusBufferUpdate(block.timestamp, 0); // update the CREDIT multiplier uint256 creditTotalSupply = CreditToken(_credit).totalSupply(); uint256 newCreditMultiplier = (creditMultiplier * (creditTotalSupply - loss)) / creditTotalSupply; creditMultiplier = newCreditMultiplier; emit CreditMultiplierUpdate( block.timestamp, newCreditMultiplier );
As we can see above, the code first calls burn on the surplusbuffer. This burns up all the tokens supplied by alice in there, drastically reducing the totalSupply of the credit tokens. In this scenario, Alice's loss is 1e18, but the totalSupply just got nuked to 0.2e18. This will lead to a revert in the next line, as creditTotalSupply - loss will be negative. This will break the notifyPnL function, and the gauge slashing and voting systems as well.
This also pushes the system towards more bad debt, since the auction process cannot be completed since the onBid function also calls notifyPnL to update the credit multiplier.
The impact is high, and the likelihood is medium since most DeFi projects have whales who take out large loans and farm with them, making this a likely scenario. Thus the overall severity is high.
Proof of Concept
Attached is a POC loosely following the attack path described above. It is a modification of the testBidSuccessBadDebt test and should be added to the AuctionHouse.t.sol test file.
solidityfunction testAttackBid() public { bytes32 loanId = _setupAndCallLoan(); uint256 PHASE_1_DURATION = auctionHouse.midPoint(); uint256 PHASE_2_DURATION = auctionHouse.auctionDuration() - auctionHouse.midPoint(); vm.roll(block.number + 1); vm.warp(block.timestamp + PHASE_1_DURATION + (PHASE_2_DURATION * 2) / 3); // At this time, get full collateral, repay half debt (uint256 collateralReceived, uint256 creditAsked) = auctionHouse.getBidDetail(loanId); emit log_named_uint('collateralReceived', collateralReceived); emit log_named_uint('creditAsked', creditAsked); vm.startPrank(borrower); credit.burn(20_000e18); vm.stopPrank(); // bid credit.mint(bidder, creditAsked); vm.startPrank(bidder); credit.approve(address(term), creditAsked); vm.expectRevert(); auctionHouse.bid(loanId); vm.stopPrank(); }
In this POC, Alice mints tokens and then her loan gets sent to the auction house. Once Alice burns her tokens, Bob can no longer bid on her loan. Alice burning her tokens is the same as her supplying them to surplusguildminter since that contract will also burn her tokens.
Tools Used
Manual Review, Foundry
Recommended Mitigation Steps
If the loss is greater than the creditTotalSupply, the creditMultiplier should be set to 0. This will prevent the system from breaking.
Assessed type
Under/Overflow
