Mis-configured ticks are allowed in positions due to insufficient checks, all subsequent swaps can be incorrect #18
Labels
3 (High Risk)
Assets can be stolen/lost/compromised directly
bug
Something isn't working
duplicate-149
edited-by-warden
🤖_02_group
AI based duplicate group recommendation
satisfactory
satisfies C4 submission criteria; eligible for awards
sufficient quality report
This report is of sufficient quality
upgraded by judge
Original issue severity upgraded from QA/Gas by judge
Lines of code
https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/pool.rs#L83-L84
Vulnerability details
Impact
Mis-configured ticks are allowed in positions due to insufficient checks, all subsequent swaps can be incorrect.
Proof of Concept
Positions are created through mint_position() -> create_position(). The problem is in
create_position()
there are insufficient checks to ensure lower tick < upper tick.(https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/pool.rs#L83-L84)
Suppose a user minted a position with a flipped lower and upper tick. e.g. (low: 20, up: 10).
When the user adds liquidity to the position, incorrect
liquidity_net
will be updated in the tick info. Inpool::update_position()
->ticks::update()
, when the tick is an upper tick, added liquidity(liquidity_delta
) should be subtracted from current tick info'sliquidity_net
because liquidity is removed when the upper tick is crossed from left to right. However, when lower/upper tick flag is flipped,liquidity_net
for the upper tick will be added withliquidity_delta
, which is incorrect.(https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/tick.rs#L106-L116)
When a tick info's
liquidity_net
is incorrect, any swap that crosses the tick will be using incorrect liquidity, resulting in incorrect swap amount calculation. Inpool::swap
, when reaching the next initialized tick, the swap liquidity (state.liquidity) will be modified with based on the crossed tick'sliquidity_net
. Incorrect cross tick'sliquidity_net
=> incorrectstate.liquidity
=> incorrectswap_math::compute_swap_step
in the next step swap.(https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/pool.rs#L473)
As seen above, state.liquidity is accumulated incorrectly and used for all subsequent swaps. So if one position has flipped lower/upper tick flags, all swaps can be impacted - incorrect token delta amount calculation when a swap crosses the positions' tick boundary.
See add unit test
test_minting_positions_with_flipped_ticks
:Test results:
Tools Used
Manual, vscode
Recommended Mitigation Steps
In
create_position()
, adding a check to ensure lower < upper.Assessed type
Other
The text was updated successfully, but these errors were encountered: