Skip to content
Business logic Audit finding Intermediate

Wrong timing of check allows users to withdraw collateral without paying for the debt

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by IllIllI

TimeswapPair.sol#L459-L490

function pay(PayParam calldata param)
    external
    override
    lock
    returns (
        uint128 assetIn,
        uint128 collateralOut
    )
{
    require(block.timestamp < param.maturity, 'E202');
    require(param.owner != address(0), 'E201');
    require(param.to != address(0), 'E201');
    require(param.to != address(this), 'E204');
    require(param.ids.length == param.assetsIn.length, 'E205');
    require(param.ids.length == param.collateralsOut.length, 'E205');

    Pool storage pool = pools[param.maturity];

    Due[] storage dues = pool.dues[param.owner];
    require(dues.length >= param.ids.length, 'E205');

    for (uint256 i; i < param.ids.length;) {
        Due storage due = dues[param.ids[i]];
        require(due.startBlock != BlockNumber.get(), 'E207');
        if (param.owner != msg.sender) require(param.collateralsOut[i] == 0, 'E213');
        require(uint256(assetIn) * due.collateral >= uint256(collateralOut) * due.debt, 'E303');
        due.debt -= param.assetsIn[i];
        due.collateral -= param.collateralsOut[i];
        assetIn += param.assetsIn[i];
        collateralOut += param.collateralsOut[i];
        unchecked { ++i; }
    }
    ...

At L484, if there is only one id, and for the first and only time of the for loop, assetIn and collateralOut will be 0, therefore require(uint256(assetIn) * due.collateral >= uint256(collateralOut) * due.debt, 'E303'); will pass.

A attacker can call pay() with param.assetsIn[0] == 0 and param.collateralsOut[i] == due.collateral.

Proof of Concept

The attacker can:

  1. borrow() 10,000 USDC with 1 BTC as collateral;
  2. pay() with 0 USDC as assetsIn and 1 BTC as collateralsOut.

As a result, the attacker effectively stole 10,000 USDC.

Recommended Mitigation Steps

Change to:

for (uint256 i; i < param.ids.length;) {
    Due storage due = dues[param.ids[i]];
    require(due.startBlock != BlockNumber.get(), 'E207');
    if (param.owner != msg.sender) require(param.collateralsOut[i] == 0, 'E213');
    due.debt -= param.assetsIn[i];
    due.collateral -= param.collateralsOut[i];
    assetIn += param.assetsIn[i];
    collateralOut += param.collateralsOut[i];
    unchecked { ++i; }
}

require(uint256(assetIn) * due.collateral >= uint256(collateralOut) * due.debt, 'E303');
...

Mathepreneur (Timeswap) resolved and commented:

Timeswap-Labs/Timeswap-V1-Core@b23b44a

0xleastwood (judge) commented:

This is an interesting find. It appears that assetIn and collateralOut are not checked properly during the first iteration of the for loop. As a result, this functionality of this function is inherently broken as the require statement will always be satisfied. Nice job!


The original report as published on Code4rena, by WatchPug. 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.

What is known

A high-severity business logic bug in Timeswap contest. Code4rena made it public on 17 May 2022. Found in the 2022-03-timeswap audit contest (finding H-01).

About Business logic

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

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

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 WatchPug

View all

197 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.

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.