Skip to content
DoS Audit finding Intermediate

initializeClone() price calculation should round up

The price calculation in initializeClone() rounds down, which can cause a revert in checkCollateral() when the division is not exact. An attacker could trigger this revert by providing parameters that cause precision loss, leading to a denial of service for legitimate users.

bin2chen Follow

Originally disclosed on Code4rena

Submitted by bin2chen

initializeClone() Price calculations use round down, which only works if divisible , If precision is lost, it will revert

Proof of Concept

When clone positon, _initialCollateral and _initialMint are used to calculate the price of the clone position

The code is as follows:

    function initializeClone(address owner, uint256 _price, uint256 _limit, uint256 _coll, uint256 _mint) external onlyHub {
        if(_coll < minimumCollateral) revert InsufficientCollateral();
        setOwner(owner);

        price = _mint * ONE_DEC18 / _coll;   //<----------use round down

        if (price > _price) revert InsufficientCollateral();
        limit = _limit;
        mintInternal(owner, _mint, _coll);

        emit PositionOpened(owner, original, address(zchf), address(collateral), _price);
    }
  1. The price calculation formula price = _mint * ONE_DEC18 / _coll, use round down
  2. In the next step mintInternal() will execute mint, and internally will call checkCollateral()
    function checkCollateral(uint256 collateralReserve, uint256 atPrice) internal view {
        if (collateralReserve * atPrice < minted * ONE_DEC18) revert InsufficientCollateral();
    }

checkCollateral() will check collateralReserve * atPrice < minted * ONE_DEC18

This has a problem, when calculating the price and there is a precision loss in price (round down), then checkCollateral() will definitely revert

Because if precision loss occurs, collateralReserve * atPrice will be 1 less than minted * ONE_DEC18

For example:

_initialCollateral = 101e18; _initialMint = 100e18;

Due to round down price = 0.99e18

Then checkCollateral () will revert because 101e18 * 0.99e18 < 100e18 * 1e18

Here is the demo code:

Will revert InsufficientCollateral in checkCollateral () Add to GeneralTest.t.sol

    function testCloneRevert() external {

        // 0.get 1000 for open bad position
        alice.obtainFrankencoins(swap, 1000 ether);
        col.mint(address(alice), 1001);

        // 1. open new position
        vm.startPrank(address(alice));
        col.approve(address(hub), 1001);
        uint256 oldPrice = 1 * (10 ** 36);
        Position pos = Position(hub.openPosition(address(col), 100, 1001, 1000000 ether, 100 days, 1 days, 25000, oldPrice, 200000));
        skip(7 * 86_400 + 60);

        console.log("0.pos price:",pos.price());

        // 2.pass _initialMint  _initialCollateral ,  will  round down
        uint256 _initialCollateral = 1001;
        uint256 _initialMint = 1000 * 10**18;

        uint256 newPrice = _initialMint * 10**18 / _initialCollateral;

        console.log("1.new price:",newPrice);
        console.log("1.new price Bigger than old ?:",newPrice > oldPrice);

        col.mint(address(alice), _initialCollateral);
        col.approve(address(hub), _initialCollateral);

        // 3. clonePosition will revert ,  Although the parameters are all legal
        hub.clonePosition(address(pos),_initialCollateral , _initialMint);
        vm.stopPrank();
    }

}
$ forge test --match testCloneRevert -vvv

Running 1 test for test/GeneralTest.t.sol:GeneralTest
[FAIL. Reason: InsufficientCollateral()] testCloneRevert() (gas: 2230875)
Logs:
  0.pos price: 1000000000000000000000000000000000000
  1.new price: 999000999000999000999000999000999000
  1.new price Bigger than old ?: false

Traces:
  [2131375] GeneralTest::testCloneRevert()

It is recommended to use round up.

Recommended Mitigation Steps

    function initializeClone(address owner, uint256 _price, uint256 _limit, uint256 _coll, uint256 _mint) external onlyHub {
...

        price = _mint * ONE_DEC18 / _coll;

+       if ( price * _coll != _mint * ONE_DEC18 ) price += 1; // round up, avoid mintInternal() revert InsufficientCollateral

0xA5DF (lookout) commented:

Impact seems insignificant Rounding down just means the price will decrease by a small percentage. Changing this to rounding up might cause a different edge case where an attacker can slightly increase the price by rounding.

hansfriese (judge) commented:

Seems correct. Sponsor review requested.

luziusmeisser (Frankencoin) commented:

Yes, I can confirm this issue. It doesn’t do much harm, but can cause some inconvenience when interacting with the protocol as it only works properly with cleanly divisible amounts for “mint” and “collateral”.


The original report as published on Code4rena, by bin2chen. 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 price calculation uses integer division that rounds down, causing a mismatch with the collateral check when the division is not exact.

Impact

The rounding down can cause clonePosition to revert for certain valid parameter combinations, leading to a denial of service for those cases.

What the attacker needed

Access to the clonePosition function; parameters causing rounding down

The fix

The price calculation should round up to avoid the revert in checkCollateral.

How it was found

  1. Obtain Frankencoins and collateral tokens.
  2. Open a position with specific parameters.
  3. Attempt to clone the position with collateral and mint amounts that cause rounding down.
  4. The transaction reverts with InsufficientCollateral.

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.

When a smart contract calculates a price by dividing two numbers, it sometimes rounds down and loses a tiny fraction. If that lost fraction makes the price slightly too low, a later check fails and the whole transaction is rejected. This means certain combinations of deposit and mint amounts cannot be used, even though they seem valid.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by bin2chen

View all

139 more disclosed reports, best first.

More from Frankencoin

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.