Skip to content
Smart contracts Audit finding Intermediate

Underflown variable in borrowGivenDebtETHCollateral function

The borrowGivenDebtETHCollateral function initialises a uint256 excess variable to zero and then subtracts dueOut.collateral from it inside an unchecked block, causing an underflow. This means the function never correctly transfers excess ETH to the user and always reverts when maxCollateral exceeds dueOut.collateral.

TerrierLover Follow

Originally disclosed on Code4rena

Submitted by TerrierLover

borrowGivenDebtETHCollateral function does never properly call ETH.transfer due to underflow. If borrowGivenDebtETHCollateral function is not deprecated, it would cause unexpected behaviors for users.

Proof of Concept

Here are codes which contain a potential issue.

Borrow.sol#L121-L127

if (maxCollateral > dueOut.collateral) {
    uint256 excess;
    unchecked {
        excess -= dueOut.collateral;
    }
    ETH.transfer(payable(msg.sender), excess);
}

excess variable is uint256, and dueOut.collateral variable is uint112 as shown below. Hence, both variables will never be less than 0.

IPair.sol#L22-L26

struct Due {
    uint112 debt;
    uint112 collateral;
    uint32 startBlock;
}

uint256 excess is initialized to 0. However, subtracting dueOut.collateral variable which is more than or equal to 0 from excess variable which is 0 will be less than 0. Hence, excess -= dueOut.collateral will be less than 0, and excess will be underflown.

Recommended Mitigation Steps

The code should properly initialize excess variable.

borrowGivenPercentETHCollateral function uses uint256 excess = maxCollateral at similar functionality. Borrow.sol#L347

Hence, just initializing excess variable with maxCollateral can be a potential workaround to prevent the underflown.

if (maxCollateral > dueOut.collateral) {
    uint256 excess = maxCollateral;
    unchecked {
        excess -= dueOut.collateral;
    }
    ETH.transfer(payable(msg.sender), excess);
}

amateur-dev (Timeswap) confirmed

Mathepreneur (Timeswap) resolved and commented:

Timeswap-Labs/Timeswap-V1-Convenience@a6f0e74

0xleastwood (judge) commented:

Awesome find! I believe medium severity is the appropriate risk as this function will always revert when maxCollateral > dueOut.collateral.


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

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 excess variable was initialised to zero instead of maxCollateral before subtracting dueOut.collateral inside an unchecked block.

Impact

The function always reverts when maxCollateral > dueOut.collateral, so users cannot receive their excess ETH; the judge rated this medium severity.

What the attacker needed

Calling borrowGivenDebtETHCollateral; maxCollateral > dueOut.collateral

The fix

The code was fixed by initialising excess with maxCollateral before the unchecked subtraction.

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 variable meant to hold the extra collateral was set to zero instead of the total collateral. When the code subtracted the actual collateral from zero, the number wrapped around to a huge value because the check for underflow was turned off. As a result, the function could not send the correct amount of ETH back to the user and simply failed.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by TerrierLover

View all

2 more disclosed reports, best first.

More from Timeswap contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.