Closed code423n4 closed 2 years ago
All mentioned functions have a minimumReturn value, it is true that when it is set to zero or to low the trades can be front run. However the minimumReturn should not be set to a to low value similar to how uniswaps minimumReturn should not be set to zero.
While I appreciate the detailed submission, the sponsor is correct, all instances of swapExactTokensForTokens
, the output amount is actually checked indirectly through the struct provided in the initial function call.
So for that reason, I must mark this invalid
.
The only occurrence I see where this might be an issue is SingleTokenJoin#_joinTokenSingle
. However, in order to sandwich attack this, an attacker cannot use a flash loan as they cannot initiate the call joinTokenSingle
. So they must artificially inflate the price using their own funds, which is possible. But this is only the first trade. There are potentially more trades afterwards which are subsequently checked against _joinTokenStruct.outputAmount
.
So the output amount is always validated to be equal to the expected output amount, hence, there is no concern of sandwich attacks.
Handle
thank_you
Vulnerability details
Impact
Amun utilizes both Pangolin and Uniswap's Routers to swap tokens within a given pair. One of the router functions used by Amun is
swapExactTokensForTokens
. Amun provides this function several arguments that the Router contract then utilizes to commence a trade between two select tokens. Documentation on Uniswap's RouterswapExactTokensForTokens
function can be found here. Pangolin lacks documentation on theswapExactTokensForTokens
function but it is possible to look at their source code to find this same function. That can be found here.Regardless if the Router is Uniswap or Pangolin, the
swapExactTokensForTokens
requires a parameter calledamountOutMin
. This parameter is used by the Router to calculate what the minimum amount of tokens received is acceptable in the swap. In cases whereamountOutMin
is set to zero, then Amun is willing to accept any amount of tokens in the trade.For frontrunners, this is an incredible win. A front-run attack consists of a user/bot watching the Eth mempool for favorable transactions. For example, a favorable transaction for a frontrunner can occur when
swapExactTokensForTokens
is called and theamountOutMin
argument is set to zero or an arbitrary number not based on a spot price. Let's see how this would look to the frontrunner watching for Amun contract transactions:swapExactTokensForTokens
with anamountOutMin
set to zero. The Amun transaction is about to swap Token A for Token B. The Amun transaction is willing to accept any amount of Token B in the trade.This attack impacts users of Amun and Amun itself. In the former case, users will be using these vulnerable contract functions to invest in Amun's basket. In the latter case, Amun's rebalance manager will utilize the
RebalanceManagerV3#rebalance
to rebalance the basket. Any time either the user or Amun calls these functions, a frontrunner can execute the sandwich attack and gain profit at the victim's expense through inflating prices and receiving a profit from the inflation after the victim's trade.Proof of Concept
Amun utilizes the
swapExactTokensForTokens
in multiple contracts. Each of these contracts set theamountOutMin
to zero or utilizes thegetAmountsOut
to determine the price of a token, which is equally exploitable. Because of this, a frontrunner will watch for any of these contract functions to be called, analyze the tokens that are to be traded, and commit a sandwich attack mentioned in the Impact section. Below are a list of functions vulnerable to this attack:amountOutMin
to zero.SingleNativeTokenExitV2#exit
andSingleNativeTokenExitV2#exitEth
.amountOutMin
is set to zero.amountOutMin
based on the Router'sgetAmountsOut
function. This function is equally vulnerable to the frontrun attack asgetAmountsOut
is calculated AFTER the frontrunner has artifically raised the price of the token Amun is receiving.getAmountsOut
utilizes a spot price that is not weighted by time or ticks.amountOutMin
to zero._swapUniswapV2
and passes as the third argument the value 0, which then acts as theamountOutMin
argument when callingswapExactTokensForTokens
inside_swapUniswapV2
.Tools Used
N/A
Recommended Mitigation Steps
When utilizing
swapExactTokensForTokens
, Amun should utilize a price oracle to determine the accurate price before a swap can take place. Otherwise a frontrunner can frontrun the trade.Special consideration must be taken into where that price oracle comes from. Oracles such as Chainlink can provide more secure price reporting. However, any protocol utilizing price oracles should always spend extra effort to ensure that a hacker can manipulate a price oracle in their favor.