Skip to content
Business logic Audit finding Intermediate

PnL system can be broken by large users intentionally or unintentionally.

The profit and loss accounting function can revert when a large borrower's loss exceeds the remaining credit token supply after the surplus buffer is burned, because the calculation of the new credit multiplier subtracts the loss from the total supply and underflows. An attacker could intentionally burn their own credit tokens to trigger this, blocking loan auctions and breaking the gauge slashing and voting systems.

carrotsmuggler Follow with Soltho, y0ng0p3 and 3 others

Originally disclosed on Code4rena

Submitted by carrotsmuggler, also found by 0xPhantom (1, 2), Soltho, y0ng0p3, SECURITISE, CaeraDenoir, and pavankv

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.

// 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:

  1. Alice deposits USDC and mints 1e18 gUSDC. Say she is the largest participator in the market.
  2. Alice goes to the SurplusGuildMinter contract and stakes the some of the minted gUSDC tokens (say 0.8e18) on her own terms.
  3. 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.

else {
// 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:

function 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

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

eswak (Ethereum Credit Guild) commented:

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.

Alice burning her tokens is the same as her supplying them to SurplusGuildMinter since that contract will also burn her tokens.

This would not be the case, as the loss is reduced by the surplusBuffer when the tokens are burnt. So in the example, 0.8e18 credit staked get burnt, and the loss becomes 1e18 - 0.8e18 = 0.2e18, which is the balance that Alice has borrowed & still holds in her wallet, and worst case the creditMultiplier could go to 0.

I think the fact that borrowers can just burn their borrowed credit tokens is another problem which I need to think through before weighing in.

eswak (Ethereum Credit Guild) confirmed, but disagreed with severity and commented:

Confirming, I think we’re going to put access control on CreditToken.burn so that only lending terms, profit manager, and psm can do it.

I’d qualify as medium because it sticks the market into an unusable state, but the user funds are not lost/stolen. The governance can always recover peg tokens in the PSM + collateral tokens in lending terms to organize an orderly closing of the market where everyone recovers what they had put in the protocol + a share of the griefer’s collateral as a compensation.

TrungOre (judge) decreased severity to Medium and commented:

Agree with the sponsor that medium severity is appropriate for this issue. The scenario is very unlikely to happen, in which users would need to burn their own assets (more than the total balance of other Credit token holders).


The original report as published on Code4rena, by carrotsmuggler. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 code subtracts the loss from the total credit token supply without checking that the loss is not larger, causing an underflow and revert.

Impact

The notifyPnL function reverts, blocking loan auctions and breaking gauge slashing and voting, which could push the system towards more bad debt; the sponsor notes that user funds are not lost and governance can recover them.

What the attacker needed

Attacker is a large borrower; attacker can burn credit tokens; loss exceeds remaining credit supply

The fix

The credit multiplier should be set to zero if the loss exceeds the total credit supply, and access control should be added to the burn function so only authorised contracts can burn tokens.

How it was found

  1. Alice deposits USDC and mints a large amount of gUSDC, becoming the largest participant.
  2. Alice stakes most of her gUSDC in the SurplusGuildMinter contract.
  3. Alice defaults on her loan, causing a loss larger than the surplus buffer.
  4. The system burns the surplus buffer, reducing the total credit supply.
  5. The code then tries to subtract the remaining loss from the reduced total supply, causing a revert.

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 track of how much each token is worth using a multiplier. When someone defaults on a big loan, the system tries to reduce that multiplier, but if the loss is bigger than the total number of tokens left, the maths goes negative and the whole function stops working. This can freeze parts of the system like auctions and voting.

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.

More by carrotsmuggler

View all

42 more disclosed reports, best first.

More from Ethereum Credit Guild

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.