MagnetarHelper.getFractionForAmount uses the wrong rounding and will yield the wrong result
The helper function MagnetarHelper.getFractionForAmount rounds up when converting the borrowed amount to shares, while the actual withdrawal logic in Singularity rounds down. This mismatch means the helper returns an incorrect fraction, so a user withdrawing based on that fraction could receive a different amount than intended.
GalloDaSballo Follow with GalloDaSballo
Originally disclosed on Code4rena
Submitted by GalloDaSballo, also found by GalloDaSballo
MagnetarHelper.getFractionForAmount is used to determine the fraction (shares) that will be received when an amount is deposited. The code needs to compute the totalShares and then determine what the fraction is going to be.
The logic is used in Singularity._removeAsset and since it’s tied to a withdrawal, the rounding will be down. However, in MagnetarHelper the round is up.
function getFractionForAmount(ISingularity singularity, uint256 amount) external view returns (uint256 fraction) {
(uint128 totalAssetShare, uint128 totalAssetBase) = singularity.totalAsset();
(uint128 totalBorrowElastic,) = singularity.totalBorrow();
uint256 assetId = singularity.assetId();
IYieldBox yieldBox = IYieldBox(singularity.yieldBox());
uint256 share = yieldBox.toShare(assetId, amount, false);
uint256 allShare = totalAssetShare + yieldBox.toShare(assetId, totalBorrowElastic, true); /// @audit Round UP for Debt
fraction = allShare == 0 ? share : (share * totalAssetBase) / allShare;
}
See the actual implementation in Singularity:
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;
}
This will cause issues when calculating how much fraction to withdraw based on the amount required by the withdrawer.
Mitigation
Change the rounding direction:
uint256 allShare = totalAssetShare + yieldBox.toShare(assetId, totalBorrowElastic, down);
Assessed type
Math
cryptotechmaker (Tapioca) confirmed and commented:
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 helper function uses round-up when converting the borrowed amount to shares, but the actual withdrawal logic uses round-down, causing inconsistent fraction calculations.
Impact
The helper may return an incorrect fraction, so a withdrawal based on it could yield a different amount than intended; the text does not quantify the loss.
What the attacker needed
None
The fix
The rounding direction should be changed to round down, matching the withdrawal logic.
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 you withdraw from a savings pool, the system calculates how many 'shares' you get for your amount. This report says one helper function does that calculation with a different rounding rule than the real withdrawal code, so the helper's answer can be slightly wrong. That could make a user withdraw a bit more or less than they expected.