Description buyOrder
gets called by the seller address and index of seller orders. SaleOrder struct looks like this:
So when a good offer is listed by TokenDivider::sellErc20
, there can be more than one user interested in it. So the one sending the transaction faster and ready to spend more on gas will buy the order faster. This is fair, but it creates a vulnerability for other users because when their transaction comes, the order of sell orders from mapping(address user => SellOrder[] orders) s_userToSellOrders
is now changed.
But protocol can't know if there is a problem with that, so check if there is an order at that index from that seller, and that's it. However, a user will get a wrong offer, maybe more expensive, maybe less ERC20s, and maybe a wrong ERC20 that you don't need.
Impact User will buy the wrong offer, with the wrong ERC20 tokens, or the offer can be more expensive. Also, he can pay the same amount of money for less ERC20 tokens. When more users arrive in the protocol the impact will be a lot bigger, because will happen on a daily base.
Proof of Concepts
Seller creates an orders to sell for example:
Index 0 = SellOrder1 {seller, erc20Address, price: 2e18, amount: 2e18}
Index 1 = SellOrder2 {seller, erc20Address, price: 2e18, amount: 1e18}
At this point every user will prefer the order at index zero, so they will try to buy it
We have two users who want to buy the same offer, and they send their transaction at around the same time, but one of the guys set higher prices for gas
So the guy who spent more gas bought the order
Now orders look like this:
Index 0 = SellOrder2 {seller, erc20Address, price: 2e18, amount: 1e18}
The expected behavior for other user's transaction is to revert, but instead, we buy the other offer, that we didn't want and spent the same price for fewer tokens
Change this modifier from test file TokenDividerTest.t.sol
Place the following code in the test file TokenDividerTest.t.sol
And run the following command in the terminal forge test --mt testBuyOrderFrontrunning -vvvv
Recommended mitigation Consider adding some checks of what a user expects to retrieve at least what is the ERC20 address of tokens he wants, maybe even what is the price and amount. The user will be sure that he gets the offer he wants.
Example:
!Note! After making changes of functions make sure to check you scripts and update it!
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.