Rounding-down of flashFee can result in calls to flash loan to revert
The flashFee function rounds down the fee, while flash loan providers round up, so the approval given to the provider can be less than required and the flash loan call reverts.
Submitted by t0x1c, also found by bin2chen
The BalancerFlashLender::flashFee() function returns a rounded-down fee. This fee is then later used across the protocol while providing spend approval to the flash loan provider. This can result in an approval less than that expected by the provider and hence cause the call to flash loan to revert. This is because flash loan providers calculate their fee by rounding up in their favour, instead of rounding down:
File: contracts/core/flashloan/BalancerFlashLender.sol
function flashFee(address, uint256 amount) external view override returns (uint256) {
uint256 perc = _balancerVault.getProtocolFeesCollector().getFlashLoanFeePercentage();
if (perc == 0 || amount == 0) {
return 0;
}
@---> return (amount * perc) / _BALANCER_MAX_FEE_PERCENTAGE;
}
and
File: contracts/core/strategies/StrategyLeverage.sol
function deploy() external payable onlyOwner nonReentrant returns (uint256 deployedAmount) {
if (msg.value == 0) revert InvalidDeployAmount();
// 1. Wrap Ethereum
address(wETHA()).functionCallWithValue(abi.encodeWithSignature("deposit()"), msg.value);
// 2. Initiate a WETH Flash Loan
uint256 leverage = calculateLeverageRatio(
msg.value,
getLoanToValue(),
getNrLoops()
);
uint256 loanAmount = leverage - msg.value;
@---> uint256 fee = flashLender().flashFee(wETHA(), loanAmount);
//§uint256 allowance = wETH().allowance(address(this), flashLenderA());
@---> if(!wETH().approve(flashLenderA(), loanAmount + fee)) revert FailedToApproveAllowance();
if (
!flashLender().flashLoan(
IERC3156FlashBorrowerUpgradeable(this),
wETHA(),
loanAmount,
abi.encode(msg.value, msg.sender, FlashLoanAction.SUPPLY_BOORROW)
)
) {
revert FailedToRunFlashLoan();
}
deployedAmount = _pendingAmount;
_deployedAmount = _deployedAmount + deployedAmount;
emit StrategyAmountUpdate(_deployedAmount);
// Pending amount is not cleared to save gas
// _pendingAmount = 0;
}
Impact
Flash loan call reverts for many amount and fee percentage combinations.
Recommended Mitigation Steps
Round up in favour of the protocol. A library like solmate can be used which has mulDivUp:
function flashFee(address, uint256 amount) external view override returns (uint256) {
uint256 perc = _balancerVault.getProtocolFeesCollector().getFlashLoanFeePercentage();
if (perc == 0 || amount == 0) {
return 0;
}
- return (amount * perc) / _BALANCER_MAX_FEE_PERCENTAGE;
+ return amount.mulDivUp(perc, _BALANCER_MAX_FEE_PERCENTAGE);
}
hvasconcelos (BakerFi) confirmed
Fixed → https://github.com/baker-fi/bakerfi-contracts/pull/47
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
Impact
Flash loan calls revert for many amount and fee percentage combinations.
What the attacker needed
Flash loan amount and fee percentage cause rounding down
The fix
The fee calculation was changed to round up in favour of the protocol, and the project confirmed a fix was merged.
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 flash loan is a loan that must be repaid in the same transaction. The protocol calculates a fee for the loan and approves the lender to take that amount. Because the protocol rounds the fee down but the lender rounds it up, the approved amount can be slightly too small, so the loan fails.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.