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:
singularity_.removeAsset(data.user, removeAssetTo, share);
Where share is computed in this way:
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:
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.
PR here.
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.