Skip to content
Business logic Audit finding Advanced

TimeswapPair.sol#borrow() Improper implementation allows attacker to increase pool.state.z to a large value

The borrow function allows the caller to specify an arbitrary zIncrease value, which is added to the pool's state.z without being tied to the actual collateral provided. An attacker can borrow a tiny amount (1 wei) with a huge zIncrease, then repay the loan and recover the collateral, artificially inflating state.z and later exploiting the inflated insurance token value to drain collateral from defaulted loans.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

In the current implementation, borrow() takes a user input value of zIncrease, while the actual collateral asset transferred in is calculated at L319, the state of pool.state.z still increased by the value of the user’s input at L332.

Even though a large number of zIncrease means that the user needs to add more collateral, the attacker can use a dust amount xDecrease (1 wei for example) so that the total collateral needed is rather small.

Plus, the attacker can always pay() the dust amount of loan to get back the rather large amount of collateral added.

https://github.com/code-423n4/2022-01-timeswap/blob/bf50d2a8bb93a5571f35f96bd74af54d9c92a210/Timeswap/Timeswap-V1-Core/contracts/TimeswapPair.sol#L299-L338

function borrow(
    uint256 maturity,
    address assetTo,
    address dueTo,
    uint112 xDecrease,
    uint112 yIncrease,
    uint112 zIncrease,
    bytes calldata data
) external override lock returns (uint256 id, Due memory dueOut) {
    require(block.timestamp < maturity, 'E202');
    require(assetTo != address(0) && dueTo != address(0), 'E201');
    require(assetTo != address(this) && dueTo != address(this), 'E204');
    require(xDecrease > 0, 'E205');

    Pool storage pool = pools[maturity];
    require(pool.state.totalLiquidity > 0, 'E206');

    BorrowMath.check(pool.state, xDecrease, yIncrease, zIncrease, fee);

    dueOut.debt = BorrowMath.getDebt(maturity, xDecrease, yIncrease);
    dueOut.collateral = BorrowMath.getCollateral(maturity, pool.state, xDecrease, zIncrease);
    dueOut.startBlock = BlockNumber.get();

    Callback.borrow(collateral, dueOut.collateral, data);

    id = pool.dues[dueTo].insert(dueOut);

    pool.state.reserves.asset -= xDecrease;
    pool.state.reserves.collateral += dueOut.collateral;
    pool.state.totalDebtCreated += dueOut.debt;

    pool.state.x -= xDecrease;
    pool.state.y += yIncrease;
    pool.state.z += zIncrease;

    asset.safeTransfer(assetTo, xDecrease);

    emit Sync(maturity, pool.state.x, pool.state.y, pool.state.z);
    emit Borrow(maturity, msg.sender, assetTo, dueTo, xDecrease, id, dueOut);
}

https://github.com/code-423n4/2022-01-timeswap/blob/bf50d2a8bb93a5571f35f96bd74af54d9c92a210/Timeswap/Timeswap-V1-Core/contracts/libraries/BorrowMath.sol#L62-L79

function getCollateral(
    uint256 maturity,
    IPair.State memory state,
    uint112 xDecrease,
    uint112 zIncrease
) internal view returns (uint112 collateralIn) {
    uint256 _collateralIn = maturity;
    _collateralIn -= block.timestamp;
    _collateralIn *= zIncrease;
    _collateralIn = _collateralIn.shiftRightUp(25);
    uint256 minimum = state.z;
    minimum *= xDecrease;
    uint256 denominator = state.x;
    denominator -= xDecrease;
    minimum = minimum.divUp(denominator);
    _collateralIn += minimum;
    collateralIn = _collateralIn.toUint112();
}

Proof of Concept

Near the maturity time, the attacker can do the following:

  1. borrow() a dust amount of assets (xDecrease = 1 wei) and increase pool.state.z to an extremely large value (20x of previous state.z in our tests);
  2. pay() the loan and get back the collateral;
  3. lend() a regular amount of state.x, get a large amount of insurance token;
  4. burn() the insurance token and get a large portion of the collateral assets from the defaulted loans.

Recommendation

Consider making pair.borrow() to be onlyConvenience, so that zIncrease will be a computed value (based on xDecrease and current state) rather than a user input value.

Mathepreneur (Timeswap) confirmed

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.

How it worked

Root cause

Likely, the borrow function trusts a user-supplied zIncrease value and adds it to pool.state.z without ensuring it is consistent with the actual collateral transferred in, allowing manipulation of the pool's internal accounting.

Impact

An attacker could manipulate the pool's state to extract a large portion of collateral assets from defaulted loans, potentially causing significant financial loss to the protocol and its users.

What the attacker needed

Near maturity time; attacker can call borrow, pay, lend, burn

The fix

The borrow function should be restricted to onlyConvenience so that zIncrease is computed internally from xDecrease and the current state, rather than being a user-supplied value.

How it was found

  1. Call borrow() with a dust xDecrease (1 wei) and an extremely large zIncrease, increasing pool.state.z to a very high value.
  2. Call pay() to repay the dust loan and recover the collateral.
  3. Call lend() with a regular amount of state.x to receive a large amount of insurance tokens.
  4. Call burn() on the insurance tokens to claim a large portion of collateral from defaulted loans.

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.

In a lending pool, borrowing usually requires putting up collateral. Here, the amount of collateral is calculated from a user-supplied number (zIncrease) that also directly increases a pool variable (state.z). By choosing a huge zIncrease but borrowing almost nothing, the attacker pays very little collateral yet makes state.z very large. Later, they can use this inflated state to get more insurance tokens than they should and then redeem those tokens for a large share of the pool's collateral.

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.