Quote manipulation via reentrant trading #125
Labels
3 (High Risk)
Assets can be stolen/lost/compromised directly
bug
Something isn't working
duplicate-343
satisfactory
satisfies C4 submission criteria; eligible for awards
Lines of code
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L172
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L182
Vulnerability details
Impact
When the
baseToken
of aPair
contract has pre-transfer hooks on the sender, such as any ERC777 token, then it's possible to execute a reentrancy attack to repeateadly manipulate quotes and obtain better rates. I'll explain using thebuy
function as an example.When the attacker calls the
buy
function to buy fractional tokens and pay base tokens in return, this would be the order of operations:transferFrom
function of the ERC777 base token is called, which callbacks the attacker'stokensToSend
hook, before updating balances. This means base token reserves remain unchanged.buy
again. This time, because the base token reserves haven't yet been increased (remain equal to step 1), the new calculated buy quote will be lower than expected, thus allowing the attacker to pay less base tokens.A similar attack can be carried out in other functions that would interact with the ERC777 base token and therefore call hooks, such as
sell
.Also, this attack vector is not uncommon in simple AMMs. It was described for Uniswap v1 here.
Tools Used
Manual review
Recommended Mitigation Steps
Implement reentrancy guards in all functions that can make unsafe external calls to attacker-controlled accounts.
The text was updated successfully, but these errors were encountered: