In LeverageModule.executeOpen/executeAdjust, vault.checkSkewMax should be called after updating the global position data
The check that limits how skewed the system is towards long positions was performed before updating the global position data, which changes the stable collateral total. An attacker could open a position that pushes the system beyond the allowed skew limit, making it more exposed to long-side risk.
ge6a Follow with jennifer37, nobody2018 and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-12-flatmoney-judging/issues/143
Found by
ge6a, jennifer37, nobody2018, santipu_
Summary
[checkSkewMax](https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/FlatcoinVault.sol#L296) is used to assert that the system will not be too skewed towards longs after additional skew is added. However, the stableCollateralTotal used by this function is a variable that will [be updated by updateGlobalPositionData](https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/FlatcoinVault.sol#L205). Therefore, checkSkewMax should be executed after updateGlobalPositionData. Otherwise, there is no guarantee whether newly opened positions will make the system more skew towards long side.
Vulnerability Detail
File: flatcoin-v1\src\LeverageModule.sol
080: function executeOpen(
081: address _account,
082: address _keeper,
083: FlatcoinStructs.Order calldata _order
084: ) external whenNotPaused onlyAuthorizedModule returns (uint256 _newTokenId) {
......
101:-> vault.checkSkewMax({additionalSkew: announcedOpen.additionalSize});
102:
103: {
104: // The margin change is equal to funding fees accrued to longs and the margin deposited by the trader.
105:-> vault.updateGlobalPositionData({
106: price: entryPrice,
107: marginDelta: int256(announcedOpen.margin),
108: additionalSizeDelta: int256(announcedOpen.additionalSize)
109: });
......
140: }
L101, [vault.checkSkewMax](https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/FlatcoinVault.sol#L296-L307) internally calculates longSkewFraction by the formula ((_globalPositions.sizeOpenedTotal + _additionalSkew) * 1e18) / stableCollateralTotal. This function guarantees that longSkewFraction will not exceed skewFractionMax ([120%](https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/scripts/deployment/configs/FlatcoinVault.config.js#L9)).
However, stableCollateralTotal will [be updated in updateGlobalPositionData](https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/FlatcoinVault.sol#L205).
- When
profitLossTotalis positive value, thenstableCollateralTotalwill decrease. - When
profitLossTotalis negative value, thenstableCollateralTotalwill increase.
Assume the following:
stableCollateralTotal = 90e18
_globalPositions = {
sizeOpenedTotal: 100e18,
lastPrice: 1800e18,
}
A new position is to be opened with additionalSize = 5e18.
fresh price=2000e18
We explain it in two situations:
checkSkewMaxis called beforeupdateGlobalPositionData.
longSkewFraction = (_globalPositions.sizeOpenedTotal + additionalSize) * 1e18 / stableCollateralTotal
= (100e18 + 5e18) * 1e18 / 90e18
= 1.16667e18 < skewFractionMax(1.2e18)
so checkSkewMax will be passed.
checkSkewMaxis called afterupdateGlobalPositionData.
In updateGlobalPositionData:
PerpMath._profitLossTotal calculates
profitLossTotal = _globalPositions.sizeOpenedTotal * (int256(price) - int256(globalPosition.lastPrice)) / int256(price)
= 100e18 * (2000e18 - 1800e18) / 2000e18 = 100e18 * 200e18 /2000e18
= 10e18
_updateStableCollateralTotal(-profitLossTotal) will deduct 10e18 from stableCollateralTotal.
so stableCollateralTotal = 90e18 - 10e18 = 80e18.
Now, checkSkewMax is called:
longSkewFraction = (_globalPositions.sizeOpenedTotal + additionalSize) * 1e18 / stableCollateralTotal
= (100e18 + 5e18) * 1e18 / 80e18
= 1.3125e18 > skewFractionMax(1.2e18)
Therefore, this new position should not be allowed to open, as this will only make the system more skewed towards the long side.
Impact
The stableCollateralTotal used by checkSkewMax is the value of the total profit that has not yet been settled, which is old value. In this way, when the price of collateral rises, it will cause the system to be more skewed towards the long side.
Code Snippet
Tool used
Manual Review
Recommendation
File: flatcoin-v1\src\LeverageModule.sol
080: function executeOpen(
081: address _account,
082: address _keeper,
083: FlatcoinStructs.Order calldata _order
084: ) external whenNotPaused onlyAuthorizedModule returns (uint256 _newTokenId) {
......
101:--- vault.checkSkewMax({additionalSkew: announcedOpen.additionalSize});
102:
103: {
104: // The margin change is equal to funding fees accrued to longs and the margin deposited by the trader.
105: vault.updateGlobalPositionData({
106: price: entryPrice,
107: marginDelta: int256(announcedOpen.margin),
108: additionalSizeDelta: int256(announcedOpen.additionalSize)
109: });
+++ vault.checkSkewMax(0); //0 means that vault.updateGlobalPositionData has added announcedOpen.additionalSize.
......
140: }
Also, if announcedAdjust.additionalSizeAdjustment is greater than 0 in [executeAdjust](https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/LeverageModule.sol#L166), similar fix is required.
Discussion
sherlock-admin
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid: checkSkewMax should be adjusted; medium(6)
sherlock-admin
The protocol team fixed this issue in PR/commit https://github.com/dhedge/flatcoin-v1/pull/266.
itsermin
Resolved here: https://github.com/dhedge/flatcoin-v1/pull/266
Because collateral is no longer settled in updateGlobalPositionData
sherlock-admin4
The Lead Senior Watson signed off on the fix.
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
Impact
When the collateral price rises, the system can become more skewed towards the long side than the intended limit, potentially increasing risk for the protocol.
What the attacker needed
A position can be opened; collateral price changes
The fix
The issue was fixed by changing the order so that checkSkewMax is called after updateGlobalPositionData, and collateral is no longer settled in that function.
How it was found
- Assume initial stableCollateralTotal is 90e18 and global positions sizeOpenedTotal is 100e18 with lastPrice 1800e18.
- A new position is to be opened with additionalSize 5e18 at a fresh price of 2000e18.
- checkSkewMax is called before updateGlobalPositionData, using the old stableCollateralTotal of 90e18, and passes because the skew fraction is below the maximum.
- updateGlobalPositionData then calculates a profit of 10e18, reducing stableCollateralTotal to 80e18.
- If checkSkewMax were called after the update, the skew fraction would exceed the maximum, so the position should not be allowed.
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 leveraged trading system, there is a limit on how much the total long positions can exceed the stable collateral. The code checked this limit before recalculating the collateral after a price change, so the check used outdated numbers. This could let someone open a position that actually breaks the limit, increasing the risk for the system.
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.