Skip to content
Business logic Audit finding Intermediate

When sellCreditMarket() is called to sell credit for a specific cash amount, the protocol might receive a lower swapping fee than expected

The protocol's fee calculation for selling credit for a specific cash amount was incorrect, resulting in lower swap fees collected than intended. An attacker could exploit this to pay less in fees when selling credit.

0xpiken Follow with 0xAlix2, Honour and 19 others

Originally disclosed on Code4rena

Submitted by 0xpiken, also found by 3n0ch (1, 2), 0xAlix2, Honour, gd, asui, DanielArmstrong, carlos__alegre, zarkk01, Shield (1, 2), mt030d, Brenzee, 0xStalin, dhank, KupiaSec, Kalogerone, 0xJoyBoy03, bin2chen, pkqs90, ether_sky, trachev, samuraii77, m4k2, and LinKenji

https://github.com/code-423n4/2024-06-size/blob/main/src/libraries/AccountingLibrary.sol#L249 https://github.com/code-423n4/2024-06-size/blob/main/src/libraries/AccountingLibrary.sol#L256

Impact

The protocol might receive a lower fee than expected when sellCreditMarket() is called to sell credit for a specific cash amount.

Proof of Concept

The protocol allows a user to sell their credit for szaUSDC, which can be used to redeem USDC thereafter.

  • If a specific amount of credit is sold for szaUSDC, sellCreditMarket() will calculate the amount of szaUSDC received and fees paid to feeReceipient.
  • If credit is sold for a specific amount of szaUSDC, sellCreditMarket() will calculate the amount of credit sold and fees paid to feeReceipient.

The calculation should follow the below rules:

Note: please see the calculation scenarios in warden’s original submission.

We can verify the fee samples with the above formulas.

Example 1: Bob owned 120 credit and sell 120 credit to Candy for szaUSDC. All results of the calculation are same as Example 1.

Example 2 : Bob owned 120 credit and sell credit to Candy for 50 szaUSDC. However, the swap fee stated in Example 2 is 0.5, which is different with the result calculated from in the formulas.

The sold credit and swap fee are calculated in getCreditAmountIn():

253:            creditAmountIn = Math.mulDivUp(
254:                cashAmountOut + state.feeConfig.fragmentationFee, PERCENT + ratePerTenor, PERCENT - swapFeePercent
255:            );
256:            fees = Math.mulDivUp(cashAmountOut, swapFeePercent, PERCENT) + state.feeConfig.fragmentationFee;

As we can see, the calculation of creditAmountIn is same as the calculation of credit_{sold} (4); however, the swap fee is different. This leaves the protocol suffering a loss on swap fees when sellCreditMarket() is called to sell credit for a specific cash amount.

Copy the below codes to SellCreditMarketTest.t.sol and run forge test --match-test test_SellCreditMarket_sellCreditMarket_incorrectFee:

    function test_SellCreditMarket_sellCreditMarket_incorrectFee() public {
        _deposit(bob, weth, 100e18);
        _deposit(alice, usdc, 100e6);
        _buyCreditLimit(alice, block.timestamp + 365 days, YieldCurveHelper.pointCurve(365 days, 0.2e18));
        uint256 tenor = 365 days;
        vm.startPrank(bob);
        uint256 apr = size.getLoanOfferAPR(alice, tenor);
        //@audit-info alice has 100e6 szaUSDC
        assertEq(size.getUserView(alice).borrowATokenBalance, 100e6);
        uint256 snapshot = vm.snapshot();
        //@audit-info bob sell 120e6 credit to alice for szaUSDC
        size.sellCreditMarket(
            SellCreditMarketParams({
                lender: alice,
                creditPositionId: RESERVED_ID,
                amount: 120e6,
                tenor: tenor,
                deadline: block.timestamp,
                maxAPR: apr,
                exactAmountIn: true
            })
        );
        //@audit-info alice has 0 szaUSDC left
        assertEq(size.getUserView(alice).borrowATokenBalance, 0);
        //@audit-info bob received 99.5e6 szaUSDC
        assertEq(size.getUserView(bob).borrowATokenBalance, 99.5e6);
        //@audit-info bob owed 120e6 debt
        assertEq(size.getUserView(bob).debtBalance, 120e6);
        //@audit-info feeRecipient received 0.5e6 szaUSDC as fee
        assertEq(size.getUserView(feeRecipient).borrowATokenBalance, 500000);
        //@audit-info restore to the snapshot before sellCreditMarket
        vm.revertTo(snapshot);
        //@audit-info bob sell credit to alice for 99.5e6 szaUSDC
        size.sellCreditMarket(
            SellCreditMarketParams({
                lender: alice,
                creditPositionId: RESERVED_ID,
                amount: 99.5e6,
                tenor: tenor,
                deadline: block.timestamp,
                maxAPR: apr,
                exactAmountIn: false
            })
        );
        //@audit-info alice has 2500 szaUSDC left
        assertEq(size.getUserView(alice).borrowATokenBalance, 2500);
        //@audit-info bob received 99.5e6 szaUSDC
        assertEq(size.getUserView(bob).borrowATokenBalance, 99.5e6);
        //@audit-info bob owed 120e6 debt
        assertEq(size.getUserView(bob).debtBalance, 120e6);
        //@audit-info feeRecipient received 497500 szaUSDC as fee
        assertEq(size.getUserView(feeRecipient).borrowATokenBalance, 497500);
    }

Recommended Mitigation Steps

Correct the swap fee calculation:

    function getCreditAmountIn(
        State storage state,
        uint256 cashAmountOut,
        uint256 maxCashAmountOut,
        uint256 maxCredit,
        uint256 ratePerTenor,
        uint256 tenor
    ) internal view returns (uint256 creditAmountIn, uint256 fees) {
        uint256 swapFeePercent = getSwapFeePercent(state, tenor);

        uint256 maxCashAmountOutFragmentation = 0;

        if (maxCashAmountOut >= state.feeConfig.fragmentationFee) {
            maxCashAmountOutFragmentation = maxCashAmountOut - state.feeConfig.fragmentationFee;
        }

        // slither-disable-next-line incorrect-equality
        if (cashAmountOut == maxCashAmountOut) {
            // no credit fractionalization

            creditAmountIn = maxCredit;
-           fees = Math.mulDivUp(cashAmountOut, swapFeePercent, PERCENT);
+           fees = Math.mulDivUp(cashAmountOut, swapFeePercent, PERCENT - swapFeePercent);
        } else if (cashAmountOut < maxCashAmountOutFragmentation) {
            // credit fractionalization

            creditAmountIn = Math.mulDivUp(
                cashAmountOut + state.feeConfig.fragmentationFee, PERCENT + ratePerTenor, PERCENT - swapFeePercent
            );
-            fees = Math.mulDivUp(cashAmountOut, swapFeePercent, PERCENT) + state.feeConfig.fragmentationFee;
+            fees = Math.mulDivUp(cashAmountOut + state.feeConfig.fragmentationFee, swapFeePercent, PERCENT - swapFeePercent) + state.feeConfig.fragmentationFee;
        } else {
            // for maxCashAmountOutFragmentation < amountOut < maxCashAmountOut we are in an inconsistent situation
            //   where charging the swap fee would require to sell a credit that exceeds the max possible credit

            revert Errors.NOT_ENOUGH_CASH(maxCashAmountOutFragmentation, cashAmountOut);
        }
    }

Assessed type

Math

aviggiano (Size) confirmed and commented via duplicate Issue #213:

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

hansfriese (judge) increased severity to High

MotokoKusanagi-aka-Major (Size) commented:

This issue is an incorrect implementation of the swap fees formula described in the technical documentation, resulting in less fee collection in the credit swapping process.

In our opinion, this should not be categorized as a “High” issue since it only negatively affects the protocol owners, because in the current version, there is no mechanism for fees redistribution, so no user category (lenders / credit buyers, borrowers / credit sellers, and liquidators) is negatively affected. In fact, the user who is subject to fee payment would have benefited from this issue since he would have ended up paying lower fees than expected.


The original report as published on Code4rena, by 0xpiken. 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 swap fee was calculated as a percentage of the cash amount out, rather than as a percentage of the cash amount out divided by (1 minus the fee percentage), which is the correct formula to ensure the fee is a percentage of the total credit sold.

Impact

The protocol received a lower swap fee than expected, resulting in a loss of fee revenue.

What the attacker needed

User has credit to sell; User calls sellCreditMarket with exactAmountIn=false

The fix

The swap fee calculation was corrected to use the proper formula, and the fix was implemented in a pull request.

How it was found

  1. Bob sells 120 credit to Alice for szaUSDC using exactAmountIn=true, receiving 99.5 szaUSDC and paying 0.5 szaUSDC fee.
  2. Bob sells credit to Alice for 99.5 szaUSDC using exactAmountIn=false, receiving 99.5 szaUSDC but paying only 0.4975 szaUSDC fee.

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.

When you sell credit for a specific amount of cash, the protocol charges a fee. The code that calculates this fee used the wrong formula, so the fee was smaller than it should be. This meant users paid less and the protocol lost money.

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 0xpiken

View all

29 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.