swap_2 implementation will randomly revert due to improper check, root cause for failed test ethers_suite_uniswap_orchestrated_uniswap_two #30
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
M-09
primary issue
Highest quality submission among a set of duplicates
🤖_primary
AI based primary recommendation
🤖_07_group
AI based duplicate group recommendation
satisfactory
satisfies C4 submission criteria; eligible for awards
selected for report
This submission will be included/highlighted in the audit report
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
sufficient quality report
This report is of sufficient quality
Lines of code
https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/lib.rs#L241
Vulnerability details
Impact
swap_2
implementation will randomly revert due to improper check, root cause for failed test ethers_suite_uniswap_orchestrated_uniswap_two.Proof of Concept
The contest readme listed an unsolved failed test
ethers_suite_uniswap_orchestrated_uniswap_two()
. The failed test is due to an improper check inswap_2_internal()
, which sometimes causes a valid swap_2 to revert.We see that in
swap_2_internal
,interim_usdc_out
is required to equalinterim_usdc_in
.interim_usdc_out
is the fusdc swapped out in 1st swap.interim_usdc_in
is the actual fusdc swapped in 2nd swap. In any swap, not all user input amount has to be used to achieve a desirable output. Especially in uniswapV3 swap logic, amount_remaining can be greater than 0, which means not all input tokens are used.(https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/lib.rs#L241)
The equality check will cause revert when 2nd swap runs out of liquidity but still satisfies the user's
min_out
. A valid swap can be reverted.Test
ethers_suite_uniswap_orchestrated_uniswap_two()
analysis:When running the test, the output error byte array
[73, 110, 116, 101, 114, 110, 97, 108, 32, 115, 119, 97, 112, 32, 97, 109, 111, 117, 110, 116, 115, 32, 110, 111, 116, 32, 109, 97, 116, 99, 104, 101, 100]
decodes(ASII) intoInternal swap amounts not matched
, corresponding toError::InterimSwapNotEq
. This custom error is only used inswap_2_internal
.I added a unit test to break down the failed reason
ethers_suite_orchestrated_uniswap_two_breakdown
.Test result:
As seen above, due to in the 2nd swap (fusdc → token1) , the token1 pool ran out of liquidity, only a portion of fusdc_out_0 is used to perform the swap.
fusdc_out_0 ≠ fusdc_out_1
reverted the originalethers_suite_uniswap_orchestrated_uniswap_two
test.Tools Used
Manual, vscode
Recommended Mitigation Steps
Consider removing/relaxing the strict equality check.
Assessed type
Other
The text was updated successfully, but these errors were encountered: