# BalancerFlashLender#receiveFlashLoan does not validate the originalCallData

BalancerFlashLender.receiveFlashLoan trusts attacker-supplied userData, letting anyone route a Balancer flash loan through StrategyLeverage and trigger arbitrary _supplyBorrow/_repayAndWithdraw/_payDebt actions.

- Payout: Audit contest
- Severity: medium
- Program: BakerFi
- Found by: zhaojie
- Disclosed: 2024-06-24
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-bakerfi-findings/issues/2
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-bakerfi%3AM-08
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:access-control vuln:business-logic vuln:flash-loan

**Target:** BalancerFlashLender / StrategyLeverage flash loan callback
**Root cause:** receiveFlashLoan only checks msg.sender == _balancerVault but never binds userData to a prior flashLoan call, so any caller of the Balancer Vault can supply arbitrary borrower and originalCallData.
**Steps:**
1. Attacker contract calls `_balancerVault.flashLoan(address(this), tokens, amounts, abi.encode(borrower=StrategyLeverage, data))` with crafted FlashLoanData.
2. Balancer Vault invokes `BalancerFlashLender.receiveFlashLoan`, which passes the msg.sender check.
3. Lender decodes userData, transfers loan to borrower, and calls `StrategyLeverage.onFlashLoan(..., originalCallData)`.
4. StrategyLeverage decodes FlashLoanData and executes `_supplyBorrow`, `_repayAndWithdraw`, or `_payDeb` with attacker-chosen parameters.
**How found:** Code review of the flash loan callback: noticed receiveFlashLoan validates only msg.sender and token/amount array lengths, then decodes userData without checking it matches a legitimate flashLoan invocation, and traced how Balancer Vault lets the caller pick the recipient.
**Exploitation:** Attacker initiates a Balancer flash loan naming BalancerFlashLender as recipient and StrategyLeverage as borrower, forcing StrategyLeverage to run arbitrary strategy actions; judge noted _repayAndWithdraw fails on zero repayment and _supplyBorrow needs pre-existing contract funds, limiting practical fund loss.
**Insight:** For flash-loan callback contracts, check whether userData is bound to a prior call (hash/nonce); if the vault lets the caller choose the recipient, any contract implementing the callback can be driven with attacker-controlled calldata.

## Proof of concept (copied exactly from the original report)

**receiveFlashLoan decodes userData without validating 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();
        }
        ....
    }
```

**onFlashLoan decodes callData and dispatches actions**

```
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;
    }
```

