An attacker can steal the entire borrow and lending incentive of an epoch with FLASHLOAN in a single transaction
The report describes a flaw in a lending protocol where an attacker can use a flash loan to create a huge lend offer and a borrow offer with dust collateral, self-match them, repay in the same block, and then claim nearly all epoch incentives. The attacker profits by stealing a larger share of the borrow and lending incentives.
BengalCatBalu Follow with CL001, Feder and 10 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-10-debita-judging/issues/708
Found by
BengalCatBalu, CL001, Feder, KaplanLabs, KlosMitSoss, Pablo, bbl4de, dimulski, lanrebayode77, mike-watson, pashap9990, tjonair, xiaoming90
Summary
An attacker, with the aid of flash-loan can steal the entire borrow and lending incentives.
This involves the attacker creating a huge lend offer with funds from flash-loan, creating a borrow offer with dust amount as collateral, self-matching the offer, paying back the loan in the same block(with zero interest) and having extra principle to payback flash-loan fees, then claim the entire incentive after the epoch has ended.
Root Cause
- Same address can be the borrower, lender and connector, there is no check against this.
DebitaV3Loan.payDebt()allows repayment of loan in the same block it was taken. (Allows the use of flash-loan to access huge funds!)- Lender can set ratio high enough to allow borrower take huge loan with dust collateral(1wei).(this reduced flash-loan needed as 1wei can be used as collateral to get unlimited principle amount reduce fees(flash-loan) and make attack more feasible/profitable).
Internal pre-conditions
Incentive is huge enough to cover attack expenses(flash-loan fees, loan disbursement fee and off-cus gasFee!)
External pre-conditions
- Attacker has extra principle to cover flash-loan fees(0.05% in Aave V3)
- Attack capital becomes lower when the borrow/lend inn the current epoch is low
Attack Path
- Attacker takes in account the amount of incentives and total borrow/lent of the current epoch to determine profitability and also to know if there is capital(flash-loan fee).
- Attacker takes flash-loan, in the flash loan call-back,
- A block to the end of an epoch, creates a lend offer with HUGE ratio!(100e24 for instance) allowing borrowing huge amount with 1wei, no check/limit for this
- creates a borrow offer using 1wei as collateral
- calls
matchOfferV3(), matching the offers, min fee of 0.2% is deducted in which 15% of it goes back to attacker, so only 0.17% net paid as fees - pays back by calling
payDebt(), offcus no fee on interest since Apr is set to zero - pays back flash-loan and fees
- epoch ends, and attacker claims almost all incentives(borrow + lend) in the next block(after the end of the epoch), since lent and borrow will be almost 100%, thanks to FLASH-LOAN!
Impact
Attacker steals larger share of incentives
PoC
Repayment is possible in the same block! the only tiime check is for deadline
// check next deadline
require(
nextDeadline() >= block.timestamp,
"Deadline passed to pay Debt"
);
} else {
maxRatio = lendInfo.maxRatio[collateralIndex]; //@audit attacker set this to be large to allow the use of dust collateral
}
// calculate ratio based on porcentage of the lend order
uint ratio = (maxRatio * porcentageOfRatioPerLendOrder[i]) / 10000;
uint m_amountCollateralPerPrinciple = amountCollateralPerPrinciple[
principleIndex
];
// calculate the amount of collateral used by the lender
uint userUsedCollateral = (lendAmountPerOrder[i] *
(10 ** decimalsCollateral)) / ratio; //@audit collateral required for large loan becomes dust!
// check ratio for each principle and check if the ratios are within the limits of the borrower
for (uint i = 0; i < principles.length; i++) {
require(
weightedAverageRatio[i] >= //@audit attacker set both, so this aligns!
((ratiosForBorrower[i] * 9800) / 10000) &&
weightedAverageRatio[i] <=
(ratiosForBorrower[i] * 10200) / 10000,
"Invalid ratio"
);
// check if the apr is within the limits of the borrower
require(weightedAverageAPR[i] <= borrowInfo.maxApr, "Invalid APR"); //@audit Apr is set to zero, so no fee on interest!
Mitigation
- Prevent repayment in the same block
- It might be helpful to prevent lender == borrower == connector
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/DebitaFinance/Debita-V3-Contracts/commit/bc889a3d624b8376c9be43f5421a78448bdaca20
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 lack of checks preventing the same address from acting as lender, borrower, and connector, combined with allowing repayment in the same block and no limit on the collateral ratio, enables the attack.
Impact
The attacker could steal a larger share of the incentives, potentially the entire epoch's borrow and lending incentives, as the text claims.
What the attacker needed
Incentives large enough to cover costs; Attacker has extra principal for flash loan fees; Flash loan available (e.g., Aave V3)
The fix
The protocol team fixed this issue by preventing repayment in the same block and possibly preventing the same address from being lender, borrower, and connector.
How it was found
- Attacker calculates profitability based on current epoch incentives and total borrow/lend amounts.
- Attacker takes a flash loan and in the callback creates a lend offer with a huge ratio.
- Attacker creates a borrow offer using 1 wei as collateral.
- Attacker calls matchOfferV3() to match the offers, paying a small fee.
- Attacker repays the loan immediately by calling payDebt(), with zero interest.
- Attacker repays the flash loan and after the epoch ends claims almost all incentives.
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.
This is about a lending platform that rewards people for lending and borrowing. An attacker can use a temporary large loan (flash loan) to pretend to lend and borrow a lot, then quickly repay and collect almost all the rewards meant for everyone. The platform did not stop someone from being both the lender and borrower, or from repaying immediately.
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.