Closed sherlock-admin closed 8 months ago
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid: marginAdjustment should be reduced properly; high(12)
Please review the vuln detail. marginAdjustment
is not the same as announcedAdjust.marginAdjustment
. The latter is never modified and the former is used to update the global position data.
AuditorPraise
high
totalFee
(keeperFee + tradeFee) is not deducted from a user'smarginAdjustment
when the margin is being reduced but instead it is addedSummary
A reduction
marginAdjustment
will be a negative number, negative numbers are reduced differently.Negative numbers are reduced by (+)adding values to them.
Vulnerability Detail
lets say marginAdjustment of user is = -1000$ and total fee = 50$
Doing this (
announcedAdjust.marginAdjustment - int256(announcedAdjust.totalFee)
) will be = -1000$ -50$ == -1050$Doing the above ends up adding the fee to the negative marginAdjustment BUT the fee was supposed to be deducted from the negative marginAdjustment and not added.
Now the negative marginAdjustment that has totalFees added to it, is been sent out to account here
Hence the protocol will always lose totalFees on every reduction leverage adjustment, which is a loss as taking fees is usually one of the main ways protocols make profit in defi.
Impact
This flaw will make the protocol always lose out on total fees on every reduction Leverage adjust order (I.e whenever marginAdjustment is a negative value).
High severity because Trade fee(which is part of totalFees) is a yield source for LPs and the LPs will always miss out on yields via reduction leverage adjust orders
Code Snippet
https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/LeverageModule.sol#L177-L180
https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/LeverageModule.sol#L237-L245
Tool used
Manual Review
Recommendation
Here :
change this :
announcedAdjust.marginAdjustment - int256(announcedAdjust.totalFee)
to this :announcedAdjust.marginAdjustment + int256(announcedAdjust.totalFee)
sincemarginAdjustment
will be a negative numberJust number line math.