isUnderwater returns opposite boolean for short positions
The `isUnderwater` function in Position.sol incorrectly flips the inequality for short positions, so it returns the opposite boolean. An attacker could potentially exploit this if the function were used in critical control flow, but currently it is not.
Originally disclosed on Code4rena
Submitted by harleythedog
Impact
The function isUnderwater should return true if the position value is < 0. In the case of a short position, this is when oi * (2 - priceFrame) - debt < 0 (based on the logic given in the _value function). Rearranging this equation, a short position is underwater if oi * 2 < oi * priceFrame + debt. However, in the function \_isUnderwater in Position.sol, the left and right side of this equation is flipped, meaning that the function will return the opposite of what it should when called on short positions.
Fortunately, the V1 implementation of OverlayOVLCollateral does not directly use the isUnderwater function in major control flow changes. However, line 304 of OverlayV1OVLCollateral.sol is a comment that says:
// TODO: think through edge case of underwater position … and fee adjustments …
which hints that this function is going to be used to deal with underwater positions. As a result, this issue would have a huge impact if not properly dealt with.
Proof of Concept
See code for \_isUnderwater here: https://github.com/code-423n4/2021-11-overlay/blob/1833b792caf3eb8756b1ba5f50f9c2ce085e54d0/contracts/libraries/Position.sol#L70
Notice that for short positions the inequality is flipped from what it should be (indeed, when self.debt is higher it is more likely that isUnder will be false, which is obviously incorrect).
Also, see the TODO comment here that shows isUnderwater is important: https://github.com/code-423n4/2021-11-overlay/blob/1833b792caf3eb8756b1ba5f50f9c2ce085e54d0/contracts/collateral/OverlayV1OVLCollateral.sol#L304
Tools Used
Inspection
Recommended Mitigation Steps
Flip the left and right side of the inequality for short positions in \_isUnderwater.
mikeyrf (Overlay) disagreed with severity:
disagree with severity -
isUnderwater()isn’t used anywhere in the collateral manager and markets. Is more for information purposes, so would rate this at a severity of 2 - Medium in the event we had actually used this function for something more important
I agree with the sponsor here. This represents a severe, but hypothetical issue.
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 inequality in the `_isUnderwater` function for short positions has its left and right sides swapped.
Impact
The function returns the wrong result for short positions, but since it is not used in major control flow, the impact is currently hypothetical; if it were used to handle underwater positions, the impact could be huge.
What the attacker needed
None
The fix
The inequality for short positions should be flipped so that the left and right sides are in the correct order.
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 is a smart contract bug where a function that checks if a trading position is losing money gives the wrong answer for short positions. A short position is a bet that a price will go down. The function is supposed to say 'yes' when the position is losing, but for shorts it says 'no' when it should say 'yes', and vice versa. Right now the function is not used to make important decisions, so the bug is not currently exploitable.
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.