Skip to content
Business logic Audit finding Intermediate

Incorrect math means data.removeAndRepayData.removeAssetFromSGL will never work once SGL has accrued interest

The MagnetarOptionModule contract incorrectly passes a YieldBox share amount to Singularity's removeAsset function, which expects a fraction of total assets. Once Singularity accrues interest, the share and fraction values diverge, causing the removal to fail or behave incorrectly.

GalloDaSballo Follow with KIntern_NA and bin2chen

Originally disclosed on Code4rena

Submitted by GalloDaSballo, also found by KIntern_NA and bin2chen

The code to remove shares from Singularity is as follows:

https://github.com/Tapioca-DAO/tapioca-periph/blob/2ddbcb1cde03b548e13421b2dba66435d2ac8eb5/contracts/Magnetar/modules/MagnetarOptionModule.sol#L158-L159

            singularity_.removeAsset(data.user, removeAssetTo, share);

Where share is computed in this way:

https://github.com/Tapioca-DAO/tapioca-periph/blob/2ddbcb1cde03b548e13421b2dba66435d2ac8eb5/contracts/Magnetar/modules/MagnetarOptionModule.sol#L153

uint256 share = yieldBox_.toShare(_assetId, _removeAmount, false);

The line is calculating: The (incorrectly rounded down) amount of shares of Yieldbox to burn in order to withdraw from Yieldbox the _removeAmount.

But the code is calling:

singularity_.removeAsset(data.user, removeAssetTo, share);

This is asking Singularity to remove a % (part) of the total assets in Singularity. Due to this, the line will stop working as soon as singularity has had any operation that generated interest.

Proof of Concept

Please see the formula used by Singularity for pricing asset:

https://github.com/Tapioca-DAO/Tapioca-bar/blob/c2031ac2e2667ac8f9ac48eaedae3dd52abef559/contracts/markets/singularity/SGLCommon.sol#L199-L216

    function _removeAsset(address from, address to, uint256 fraction) internal returns (uint256 share) {
        if (totalAsset.base == 0) {
            return 0;
        }
        Rebase memory _totalAsset = totalAsset;
        uint256 allShare = _totalAsset.elastic + yieldBox.toShare(assetId, totalBorrow.elastic, false);
        share = (fraction * allShare) / _totalAsset.base;
    }

As you can see, the fraction will be computed against _totalAsset.elastic + yieldBox.toShare(assetId, totalBorrow.elastic, false);. Meaning that the math will be incorrect as soon as any operation is done in Singularity

Coded POC

This Poc is built on the public repo: https://github.com/GalloDaSballo/yieldbox-foundry

We show how a change in interest will change fraction. In my local testing, fraction and shares are already out of sync. However, due to decimals it may be possible for them to be the same value, until some interest will make borrowElastic grow.

Logs

[PASS] testSingularityRebasedMathIsNotYieldbox() (gas: 34810)
Logs:
fraction 999999990000000099999
share 100000000000000000000000000000

[PASS] testSingularityRebasedMathIsNotYieldboxAfterInterest() (gas: 34756)
Logs:
fraction 666666662222222251851
share 100000000000000000000000000000

Code

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import {Test} from "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";
import {TargetFunctions} from "./TargetFunctions.sol";
import {FoundryAsserts} from "@chimera/FoundryAsserts.sol";

contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts {
    function setUp() public {
        setup();
    }

    function testSingularityRebasedMathIsNotYieldbox() public {
        uint256 amountToRepay = 1000e18;

        uint256 totalAssetShares = 2000e18;
        uint256 totalAssetBase = 2000e18;
        uint256 totalBorrowElastic = 2000e18;

        uint256 share = yieldBox.toShare(assetId, amountToRepay, false);
        uint256 allShare = totalAssetShares + yieldBox.toShare(assetId, totalBorrowElastic, true);

        uint256 fraction = allShare == 0 ? share : (share * totalAssetBase) / allShare;
        console2.log("fraction", fraction);
        console2.log("share", share);
    }

    function testSingularityRebasedMathIsNotYieldboxAfterInterest() public {
        uint256 amountToRepay = 1000e18;

        uint256 totalAssetShares = 2000e18;
        uint256 totalAssetBase = 2000e18;
        uint256 totalBorrowElastic = 3000e18; // NOTE: Higher cause of interest

        uint256 share = yieldBox.toShare(assetId, amountToRepay, false);
        uint256 allShare = totalAssetShares + yieldBox.toShare(assetId, totalBorrowElastic, true);

        uint256 fraction = allShare == 0 ? share : (share * totalAssetBase) / allShare;
        console2.log("fraction", fraction);
        console2.log("share", share);
    }
}

Mitigation

The unused function getFractionForAmount should help, minus some possible rounding considerations.

cryptotechmaker (Tapioca) confirmed, but disagreed with severity and commented via duplicate Issue #159:

PR here.


The original report as published on Code4rena, by GalloDaSballo. 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 code passes a YieldBox share amount to Singularity's removeAsset function, which expects a fraction of total assets, without converting between the two representations.

Impact

Once Singularity has accrued interest, the removeAsset call will use an incorrect fraction, causing the removal to fail or produce wrong results.

What the attacker needed

Singularity has accrued interest

The fix

The code should use the existing getFractionForAmount function to convert the share amount to a fraction before calling removeAsset.

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.

This report is about a smart contract bug where one part of the system uses a different way of counting than another part expects. The code sends a number that means 'how many shares to burn' to a function that thinks it is receiving 'what fraction of the total to remove'. When interest is added, the two numbers no longer match, so the operation breaks.

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 GalloDaSballo

View all

30 more disclosed reports, best first.

More from Tapioca Invitational

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.