Skip to content
Business logic Audit finding Advanced

Unsafe implementation of fundLoan() allows attacker to steal collateral from an unfunded loan

The fundLoan function reads fee and recipient addresses from a user-supplied lender contract, so an attacker can set very high fees that reduce drawable funds to near zero while sending the fees back to themselves. The borrower then cannot draw funds or remove collateral, and after the grace period the attacker can repossess the collateral.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

MapleLoanInternals.sol L257-L273

uint256 treasuryFee = (fundsLent_ * ILenderLike(lender_).treasuryFee() * _paymentInterval * _paymentsRemaining) / uint256(365 days * 10_000);

// Transfer delegate fee, if any, to the pool delegate, and decrement drawable funds.
uint256 delegateFee = (fundsLent_ * ILenderLike(lender_).investorFee() * _paymentInterval * _paymentsRemaining) / uint256(365 days * 10_000);

// Drawable funds is the amount funded, minus any fees.
_drawableFunds = fundsLent_ - treasuryFee - delegateFee;

require(
    treasuryFee == uint256(0) || ERC20Helper.transfer(_fundsAsset, ILenderLike(lender_).mapleTreasury(), treasuryFee),
    "MLI:FL:T_TRANSFER_FAILED"
);

require(
    delegateFee == uint256(0) || ERC20Helper.transfer(_fundsAsset, ILenderLike(lender_).poolDelegate(), delegateFee),
    "MLI:FL:PD_TRANSFER_FAILED"
    );

In the current implementation, mapleTreasury, poolDelegate and treasuryFee are taken from user input lender_, which can be faked by setting up a contract with ILenderLike interfaces.

This allows the attacker to set very high fees, making _drawableFunds near 0.

Since mapleTreasury and poolDelegate are also read from lender_, treasuryFee and investorFee can be retrieved back to the attacker.

As a result, the borrower won’t get any _drawableFunds while also being unable to remove collateral.

PoC

Given:

  • _collateralRequired = 10 BTC
  • _principalRequested = 1,000,000 USDC
  • _paymentInterval = 1 day
  • _paymentsRemaining = 10
  • _gracePeriod = 1 day
  • Alice (borrower) calls postCollateral() and added 10 BTC as _collateralAsset;
  • The attacker calls fundLoan() by taking 1,000,000 USDC of flashloan and using a fake lendercontract;
  • Alice calls drawdownFunds() with any amount > 0 will fail;
  • Alice calls removeCollateral() with any amount > 0 will get “MLI:DF:INSUFFICIENT_COLLATERAL” error;
  • Unless Alice make payment (which is meaningless), after 2 day, the attacker can call repossess() and get 10 BTC.

Recommendation

Consider reading treasuryFee, investorFee, mapleTreasury, poolDelegate from an authoritative source instead.

lucas-manuel (Maple) confirmed and disagreed with severity:

We would consider this medium risk, since a borrower would never post collateral before a loan is funded. We can enforce this on the smart contracts level though by adding a require to the postCollateral function to assert that the principal amount is greater than zero.

pauliax (judge) commented:

Great find. As per the sponsor’s recommendation, this scenario is not very likely, so I am marking this issue as of medium severity.

The original report as published on Code4rena, by WatchPug. 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 fundLoan function trusts fee rates and fee recipient addresses from a caller-supplied lender contract instead of an authoritative source.

Impact

The borrower could lose their collateral while receiving no loan funds, though the sponsor argues this is unlikely because a borrower would not post collateral before a loan is funded.

What the attacker needed

Borrower posts collateral before funding; attacker can supply a fake lender contract; flash loan of the principal amount

The fix

The fee rates and recipient addresses should be read from an authoritative source rather than from the caller-supplied lender contract, and the sponsor also suggested requiring the principal amount to be greater than zero in postCollateral.

How it was found

  1. The borrower posts 10 BTC as collateral.
  2. The attacker takes a flash loan of 1,000,000 USDC and calls fundLoan using a fake lender contract.
  3. The borrower tries to draw funds but any amount above zero fails.
  4. The borrower tries to remove collateral but gets an insufficient collateral error.
  5. After two days the attacker calls repossess and takes the 10 BTC.

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 borrower puts up collateral (something valuable) to get a loan. The loan contract asks the lender contract for fee settings, but it trusts whatever lender address the caller provides. An attacker can pretend to be the lender, set huge fees that go back to themselves, and leave the borrower with no money while their collateral is stuck and later taken.

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 WatchPug

View all

197 more disclosed reports, best first.

More from Maple Finance contest

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.