BalancerFlashLender#receiveFlashLoan does not validate the originalCallData
The BalancerFlashLender contract's receiveFlashLoan function did not validate the originalCallData, allowing an attacker to pass arbitrary parameters and trigger any Strategy instruction such as _supplyBorrow, _repayAndWithdraw, or _payDeb. The judge noted that exploitation was unlikely because the contract typically holds no funds, but the behaviour was unintended and should be fixed.
zhaojie Follow with rvierdiiev
Originally disclosed on Code4rena
Submitted by zhaojie, also found by rvierdiiev
Impact
receiveFlashLoan does not validate the originalCallData. The attacker can pass any parameters into the receiveFlashLoan function and execute any Strategy instruction :_supplyBorrow _repayAndWithdraw _payDeb.
Proof of Concept
The StrategyLeverage#receiveFlashLoan function only validates whether the msg.sender is _balancerVault, but does not validate the originalCallData:
function receiveFlashLoan(address[] memory tokens,
uint256[] memory amounts, uint256[] memory feeAmounts, bytes memory userData
) external {
@> if (msg.sender != address(_balancerVault)) revert InvalidFlashLoadLender();
if (tokens.length != 1) revert InvalidTokenList();
if (amounts.length != 1) revert InvalidAmountList();
if (feeAmounts.length != 1) revert InvalidFeesAmount();
//@audit originalCallData is not verified
(address borrower, bytes memory originalCallData) = abi.decode(userData, (address, bytes));
address asset = tokens[0];
uint256 amount = amounts[0];
uint256 fee = feeAmounts[0];
// Transfer the loan received to borrower
IERC20(asset).safeTransfer(borrower, amount);
@> if (IERC3156FlashBorrowerUpgradeable(borrower).onFlashLoan(borrower,
tokens[0], amounts[0], feeAmounts[0], originalCallData
) != CALLBACK_SUCCESS
) {
revert BorrowerCallbackFailed();
}
....
}
_balancerVault.flashLoan can specify the recipient:
_balancerVault.flashLoan(address(this), tokens, amounts, abi.encode(borrower, data));
An attacker can initiate flashLoan from another contract and specify the recipient as BalancerFlashLender. _balancerVault will call the balancerFlashlender#receiveFlashLoan function. Since the caller of the receiveFlashLoan function is _balancerVault, this can be verified against msg.sender.
The StrategyLeverage#onFlashLoan function parses the instructions to be executed from originalCallData(FlashLoanData) and executes them.
function onFlashLoan(address initiator,address token,uint256 amount, uint256 fee, bytes memory callData
) external returns (bytes32) {
if (msg.sender != flashLenderA()) revert InvalidFlashLoanSender();
if (initiator != address(this)) revert InvalidLoanInitiator();
// Only Allow WETH Flash Loans
if (token != wETHA()) revert InvalidFlashLoanAsset();
//loanAmount = leverage - msg.value;
@> FlashLoanData memory data = abi.decode(callData, (FlashLoanData));
if (data.action == FlashLoanAction.SUPPLY_BOORROW) {
_supplyBorrow(data.originalAmount, amount, fee);
// Use the Borrowed to pay ETH and deleverage
} else if (data.action == FlashLoanAction.PAY_DEBT_WITHDRAW) {
// originalAmount = deltaCollateralInETH
_repayAndWithdraw(data.originalAmount, amount, fee, payable(data.receiver));
} else if (data.action == FlashLoanAction.PAY_DEBT) {
_payDebt(amount, fee);
}
return _SUCCESS_MESSAGE;
}
So the attacker, by calling the _balancerVault flashLoan function, designated recipient for BalancerFlashLender, borrower for StrategyLeverage. An attacker can invoke the _supplyBorrow _repayAndWithdraw _payDeb function in StrategyLeverage with any FlashLoanData parameter.
Tools Used
VSCode, Manual
Recommended Mitigation Steps
BalancerFlashLender#flashLoanfunction to record the parameters called via hash.- Verify the hash value in the
receiveFlashLoanfunction.
hvasconcelos (BakerFi) confirmed
0xleastwood (judge) decreased severity to Medium and commented:
After further discussion, I agree that
_repayAndWithdraw()would fail when a zero amount repayment is made, so the only action that is really possible is_supplyBorrow()which would require some funds to already be in the contract. Unlikely for this to ever be the case because the contract doesn’t normally hold funds that aren’t being put to use in some way, but do correct me if this assumption is incorrect.So I’m not sure how this issue can be exploited even if I do agree that it is an issue. For the time being, I will downgrade this to
mediumseverity because it is obvious this is not intended behavour even if it may not lead to funds being lost.
0xleastwood (judge) commented:
This is clearly unintended behaviour and should be fixed even if it is not currently exploitable. I believe
mediumseverity is still justified.
Fixed → https://github.com/baker-fi/bakerfi-contracts/pull/50
Note: for full discussion, please see the warden’s original submission.
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 receiveFlashLoan function trusted the userData supplied by the Balancer vault without verifying that it matched a request previously made through the contract's own flashLoan function.
Impact
The judge downgraded this to medium severity because _repayAndWithdraw would fail on a zero repayment and _supplyBorrow would require funds already sitting in the contract, which is unlikely, so no funds were shown to be at risk.
What the attacker needed
Attacker can call the Balancer vault; contract holds spare funds (unlikely)
The fix
The issue was fixed by recording a hash of the parameters in flashLoan and verifying that hash in receiveFlashLoan.
How it was found
- An attacker calls the Balancer vault's flashLoan function from another contract, setting the recipient to BalancerFlashLender and the borrower to StrategyLeverage.
- The Balancer vault calls BalancerFlashLender's receiveFlashLoan, which passes the msg.sender check because the caller is the vault.
- receiveFlashLoan decodes the attacker-supplied userData and calls onFlashLoan on the chosen borrower with the attacker's originalCallData.
- StrategyLeverage's onFlashLoan decodes the FlashLoanData and executes the chosen action such as _supplyBorrow, _repayAndWithdraw, or _payDeb.
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 borrowed and repaid within the same transaction. This contract acted as a middleman for such loans but did not check that the instructions it received actually came from its own flashLoan function. An attacker could therefore ask it to run any instruction, though in practice the contract usually held no spare funds to steal.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.