Skip to content
Business logic Audit finding Intermediate

Fragmentation fee is not taken if user compensates with newly created position

The fragmentation fee is not charged when a user compensates part of their debt by creating a new credit position, because the code calculates the remaining credit as zero. An attacker could split their credit position without paying the intended fee.

SpicyMeatball Follow with 0xRobocop, 0xAlix2 and 26 others

Originally disclosed on Code4rena

Submitted by SpicyMeatball, also found by 0xRobocop, 0xAlix2, samuraii77, DanielArmstrong, Rhaydden, ubl4nk, alix40, 0xStalin, asui, zanderbyte, carlos__alegre, Honour, lanrebayode77, Infect3d, elhaj, Nihavent, Bigsam, trachev, 0xRstStn, Jorgect, hyh, serial-coder, KupiaSec, shaflow2, pkqs90, Sentryx, said, and zzebra83

https://github.com/code-423n4/2024-06-size/blob/main/src/libraries/actions/Compensate.sol#L119-L125 https://github.com/code-423n4/2024-06-size/blob/main/src/libraries/actions/Compensate.sol#L136

Impact

User can split his debt credit using the compensate function without paying the fragmentation fee.

Proof of Concept

The fragmentation fee normally applies when the initial credit amount is split, creating a new credit position with the fractionalized amount. However, under specific conditions when the compensate function is invoked, this fee is not charged.

Consider the following scenario, the user compensates half of his debt (amountToCompensate = creditPositionWithDebtToRepay.credit / 2) with a new credit position (params.creditPositionToCompensateId == RESERVED_ID) to the lender:

    function executeCompensate(State storage state, CompensateParams calldata params) external {
        emit Events.Compensate(
            params.creditPositionWithDebtToRepayId, params.creditPositionToCompensateId, params.amount
        );

        CreditPosition storage creditPositionWithDebtToRepay =
            state.getCreditPosition(params.creditPositionWithDebtToRepayId);
        DebtPosition storage debtPositionToRepay =
            state.getDebtPositionByCreditPositionId(params.creditPositionWithDebtToRepayId);

>>      uint256 amountToCompensate = Math.min(params.amount, creditPositionWithDebtToRepay.credit);

        CreditPosition memory creditPositionToCompensate;
        if (params.creditPositionToCompensateId == RESERVED_ID) {
>>          creditPositionToCompensate = state.createDebtAndCreditPositions({
                lender: msg.sender,
                borrower: msg.sender,
                futureValue: amountToCompensate,
                dueDate: debtPositionToRepay.dueDate
            });

        ---SNIP---

In the above code snippet, a new credit position is created with credit = futureValue = amountToCompensate. This results in creditPositionToCompensate.credit always equating to amountToCompensate, leading exiterCreditRemaining to consistently be zero:

        ---SNIP---

>>      uint256 exiterCreditRemaining = creditPositionToCompensate.credit - amountToCompensate;

        // credit emission
        state.createCreditPosition({
            exitCreditPositionId: params.creditPositionToCompensateId == RESERVED_ID
                ? state.data.nextCreditPositionId - 1
                : params.creditPositionToCompensateId,
            lender: creditPositionWithDebtToRepay.lender,
            credit: amountToCompensate
        });
>>      if (exiterCreditRemaining > 0) {
            // charge the fragmentation fee in collateral tokens, capped by the user balance
            uint256 fragmentationFeeInCollateral = Math.min(
                state.debtTokenAmountToCollateralTokenAmount(state.feeConfig.fragmentationFee),
                state.data.collateralToken.balanceOf(msg.sender)
            );
            state.data.collateralToken.transferFrom(
                msg.sender, state.feeConfig.feeRecipient, fragmentationFeeInCollateral
            );

As a result, despite splitting creditPositionWithDebtToRepay.credit in half and creating a new credit position, the fragmentation fee is not deducted as expected.

Recommended Mitigation Steps

+  uint256 initialCredit = params.creditPositionToCompensateId == RESERVED_ID
     ?  creditPositionWithDebtToRepay.credit
     :  creditPositionToCompensate.credit;
+  uint256 exiterCreditRemaining = initialCredit - amountToCompensate;

aviggiano (Size) confirmed and commented:

This report correctly identifies the root cause and correctly provides a mitigation. This could be the primary issue.

Fixed in https://github.com/SizeCredit/size-solidity/pull/120.


The original report as published on Code4rena, by SpicyMeatball. 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

Likely the code calculates the remaining credit using the newly created position's credit amount, which always equals the amount being compensated, instead of using the original credit position's credit amount.

Impact

A user could avoid the fragmentation fee when splitting their credit position, potentially reducing fees collected by the protocol.

What the attacker needed

A credit position with debt to repay; use the compensate function; set creditPositionToCompensateId to RESERVED_ID

The fix

The code was fixed by calculating the initial credit from the original credit position when the reserved ID is used, so the remaining credit is correct and the fee is charged.

How it was found

  1. Call the compensate function with a credit position that has debt to repay.
  2. Set the credit position to compensate to the reserved ID, which creates a new credit position.
  3. Set the amount to compensate to half of the original credit.
  4. The new credit position is created with credit equal to the amount to compensate.
  5. The remaining credit is calculated as zero, so the fragmentation fee is not charged.

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 lending system, splitting a credit position normally incurs a small fee called the fragmentation fee. The report shows that if you use the compensate function and create a new position at the same time, the code mistakenly thinks no credit is left over, so it skips the fee. This lets someone avoid a fee they should have paid.

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 SpicyMeatball

View all

18 more disclosed reports, best first.

More from Size

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.