Skip to content
Business logic Audit finding Intermediate

Locked funds due to underflow in withdrawal

In the withdraw function of the Leverager contract, when the borrowed asset is token1, the code mistakenly uses amountOut0 instead of amountOut1 in a conditional subtraction. This causes an arithmetic underflow if amountOut1 is less than the owed amount, reverting the transaction and blocking withdrawals for token1 borrowers.

000000 Follow with 00xJi, 0x15 and 46 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-02-yieldoor-judging/issues/407

Found by

000000, 00xJi, 0x15, 0x73696d616f, 0xaxaxa, 0xnegan, CL001, Chain-sentry, DSbeX, Drynooo, Elawdie, Harsh, JasonBPMIASN, Jumcee, KiteWeb3, MaslarovK, Mrmatrixxx, Ryonen, Spomaria, Uddercover, Victor_TheOracle, Z-Bra, bigbear1229, bladeee, brakeless, destiny_rs, devAnas, elolpuer, elvin.a.block, eta, fspah, godwinudo, gogolevds, hackcat, iamephraim, ilyadruzh, ivanalexandur, kazan, m3dython, oct0pwn, princekay, stuart_the_minion, surenyan-oks, v_2110, verbotenviking, whitehair0330, xKeywordx, yuza101, zraxx

Summary

In the withdraw function of the Leverager contract, when the borrowed asset is token1, the code uses the wrong variable in its conditional subtraction. Instead of using the available amount of token1 (amountOut1), it mistakenly uses amountOut0 (the token0 balance) to determine how much to subtract from the owed amount. This error causes an arithmetic underflow if amountOut1 (the correct balance) is less than the owed amount, because the ternary operator selects amountOut0 even though it might be much larger. In Solidity 0.8.x, arithmetic underflow reverts the transaction, blocking the withdrawal.

Root Cause

The bug is a simple copy‑paste error. In the branch where borrowed == up.token1, the code should reference amountOut1 in the ternary operator. Instead, it mistakenly uses amountOut0, leading to an underflow when token1’s balance is insufficient.

Internal Pre-conditions

None.

External Pre-conditions

None.

Attack Path

https://github.com/sherlock-audit/2025-02-yieldoor/blob/main/yieldoor/src/Leverager.sol#L209

In the withdraw function, after obtaining withdrawal amounts from the vault:

Solidity
uint256 bIndex = ILendingPool(lendingPool).getCurrentBorrowingIndex(borrowed);
uint256 totalOwedAmount = up.borrowedAmount * bIndex / up.borrowedIndex;
uint256 owedAmount = totalOwedAmount * wp.pctWithdraw / 1e18;
// ...
if (borrowed == up.token0) {
    uint256 repayFromWithdraw = amountOut0 < owedAmount ? amountOut0 : owedAmount;
    owedAmount -= repayFromWithdraw;
    amountOut0 -= repayFromWithdraw;
} else if (borrowed == up.token1) {
    // BUG: Should use amountOut1 below instead of amountOut0.
    uint256 repayFromWithdraw = amountOut1 < owedAmount ? amountOut0 : owedAmount;
    owedAmount -= repayFromWithdraw;
    amountOut1 -= repayFromWithdraw;
}
  1. Issue Trigger:

When borrowed equals up.token1, the condition if (amountOut1 < owedAmount) is met.

Instead of using amountOut1 (the correct available balance of token1), the code mistakenly uses amountOut0 to compute repayFromWithdraw.

For example, assume:

owedAmount = 150

amountOut1 = 100 (available token1 is less than owed)

amountOut0 = 300 (available token0 is much higher)

The ternary operator incorrectly selects amountOut0 (300) because amountOut1 < owedAmount is true.

Then, the code performs:

owedAmount -= repayFromWithdraw; // 150 - 300, which underflows.

Solidity 0.8 enforces checked arithmetic, so this underflow causes a revert.

  1. Consequences:

Denial of Withdrawal: Users with positions borrowing token1 cannot withdraw if the token1 liquidity is insufficient compared to token0’s balance.

Funds Locked: Repeated failures due to underflow may lock user funds, preventing any withdrawal until the bug is fixed.

Attack Vector: Although likely unintentional, an attacker could manipulate the withdrawal scenario (e.g., by altering input amounts) to trigger the bug repeatedly, effectively causing a denial-of-service on withdrawals for token1 borrowers.

Impact

Denial of Withdrawal: Users with positions borrowing token1 cannot withdraw if the token1 liquidity is insufficient compared to token0’s balance.

Funds Locked: Repeated failures due to underflow may lock user funds, preventing any withdrawal until the bug is fixed.

PoC

Foundry test demonstrating that the withdrawal call reverts due to the arithmetic underflow when the borrowed asset is token1:

Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";

// Minimal interfaces and contract fragments required for the test.
interface IVault {
    function withdraw(uint256 shares, uint256 min0, uint256 min1) external returns (uint256, uint256);
}

interface ILendingPool {
    function getCurrentBorrowingIndex(address asset) external view returns (uint256);
    function repay(address asset, uint256 amount) external;
}

interface IERC20 {
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}

contract Leverager {
    // Simplified Position struct used in the withdraw function.
    struct Position {
        address token0;
        address token1;
        address vault;
        uint256 borrowedAmount;
        uint256 borrowedIndex;
        address denomination; // borrowed token
        uint256 shares;
    }
    mapping(uint256 => Position) public positions;
    address public lendingPool;

    // Vulnerable withdraw function fragment.
    function withdraw(uint256 wp_pctWithdraw, uint256 shares, uint256 totalSupply, address msgSender) external {
        Position memory up = positions[1]; // using position ID 1 for testing
        // For testing, assume borrowed token equals up.token1.
        address borrowed = up.denomination;
        
        uint256 bIndex = ILendingPool(lendingPool).getCurrentBorrowingIndex(borrowed);
        uint256 totalOwedAmount = up.borrowedAmount * bIndex / up.borrowedIndex;
        uint256 owedAmount = totalOwedAmount * wp_pctWithdraw / 1e18;
        
        // Simulated values from vault.withdraw.
        // In our test, we set:
        // amountOut0 (token0 balance from vault) = 300
        // amountOut1 (token1 balance from vault) = 100, which is less than owedAmount = 150.
        uint256 amountOut0 = 300;
        uint256 amountOut1 = 100;
        
        if (borrowed == up.token0) {
            uint256 repayFromWithdraw = amountOut0 < owedAmount ? amountOut0 : owedAmount;
            owedAmount -= repayFromWithdraw;
            amountOut0 -= repayFromWithdraw;
        } else if (borrowed == up.token1) {
            // BUG: incorrectly uses amountOut0 instead of amountOut1.
            uint256 repayFromWithdraw = amountOut1 < owedAmount ? amountOut0 : owedAmount;
            // This line causes underflow: 150 - 300 underflows.
            owedAmount -= repayFromWithdraw;
            amountOut1 -= repayFromWithdraw;
        }
        
        // For testing, we simply require that owedAmount must be zero at end.
        require(owedAmount == 0, "Underflow detected: insufficient token1 funds");
    }
    
    // For testing: allow setting a position directly.
    function testSetPosition(Position calldata pos) external {
        positions[1] = pos;
    }
}

contract LeveragerWithdrawTest is Test {
    Leverager leverager;
    
    // Dummy lending pool to return a fixed borrowing index.
    contract DummyLendingPool {
        function getCurrentBorrowingIndex(address) external pure returns (uint256) {
            return 1e27;
        }
    }
    DummyLendingPool dummyLendingPool;
    
    function setUp() public {
        leverager = new Leverager();
        dummyLendingPool = new DummyLendingPool();
        // Set the dummy lending pool.
        // (In practice, this would be set via constructor or a setter.)
        // Here we cheat by writing directly to the storage slot for demonstration.
        // For our test, assume lendingPool is address(dummyLendingPool).
        (bool success, ) = address(leverager).call(abi.encodeWithSignature("setLendingPool(address)", address(dummyLendingPool)));
        // If the above fails (due to simplified contract), we assume lendingPool is dummyLendingPool.
        
        // Setup a position where:
        // - borrowedAmount = 150, borrowedIndex = 1e27, so totalOwedAmount = 150.
        // - Borrowed token (denomination) is token1.
        Leverager.Position memory pos = Leverager.Position({
            token0: address(0x1),
            token1: address(0x2),
            vault: address(0x3),
            borrowedAmount: 150,
            borrowedIndex: 1e27,
            denomination: address(0x2), // token1
            shares: 1000
        });
        leverager.testSetPosition(pos);
    }
    
    function testWithdrawRevertsDueToBug() public {
        // Expect the withdrawal to revert due to underflow caused by the wrong variable reference.
        vm.expectRevert();
        // Call withdraw with 100% withdrawal (pctWithdraw = 1e18) and dummy shares/totalSupply.
        leverager.withdraw(1e18, 1000, 1000, address(this));
    }
}

Mitigation

Correct the code by replacing:

uint256 repayFromWithdraw = amountOut1 < owedAmount ? amountOut0 : owedAmount;

with

uint256 repayFromWithdraw = amountOut1 < owedAmount ? amountOut1 : owedAmount;

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

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 a copy-paste error: in the branch for borrowed token1, the ternary operator uses amountOut0 instead of amountOut1, leading to an underflow when amountOut1 is less than owedAmount.

Impact

Users with positions borrowing token1 cannot withdraw if the token1 liquidity is insufficient compared to token0’s balance, potentially locking funds until the bug is fixed.

What the attacker needed

Position borrowing token1; token1 balance less than owed amount

The fix

The code should be fixed by replacing amountOut0 with amountOut1 in the ternary operator for the token1 branch.

How it was found

  1. Set up a position where the borrowed token is token1 and the owed amount exceeds the available token1 balance from the vault.
  2. Call the withdraw function with a percentage that triggers the conditional subtraction.
  3. The code incorrectly uses amountOut0 (token0 balance) instead of amountOut1, causing an underflow and revert.

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 smart contract has a function to withdraw funds. When a user borrows a particular token, the code checks the balance of the wrong token to decide how much to repay. If the correct token's balance is too low, the calculation goes negative, which the programming language does not allow, so the withdrawal fails and the user cannot get their funds out.

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.

More by 000000

View all

32 more disclosed reports, best first.

More from Yieldoor

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.