Submitted by Lambda
LBRouter.sol#L291
LBRouter.removeLiquidity reorders tokens when the user did not pass them in the pair order (ascending order):
However, when returning amountX and amountY, it is ignored if the order was changed:
Therefore, when the order of the tokens is swapped by the function, the return value amountX (Amount of token X returned) in reality is the amount of the user-provided token Y that is returned and vice versa.
Because this is an exposed function that third-party protocols / contracts will use, this can cause them to malfunction. For instance, when integrating with Trader Joe, something natural to do is:
This snippet will only be correct when the token addresses are passed in the right order, which should not be the case. When they are not passed in the right order, the accounting of third-party contracts will be messed up, leading to vulnerabilities / lost funds there.
First consider the following diff, which shows a scenario when LBRouter does not switch tokenX and tokenY, resulting in correct return values:
This test passes (as it should). Now, consider the following diff, where LBRouter switches tokenX and tokenY:
This test should also pass (the order of the tokens was only switched), but it does not because the return values are mixed up.
Add the following statement in the end:
0x0Louis (Trader Joe) confirmed
Alex the Entreprenerd (judge) commented:
