user can profit by reentrancy if erc777 is used as base token #221
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/main/src/Pair.sol#L154
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L398-L400
Vulnerability details
Impact
This is a well known attack, openzepellin talks about it here https://blog.openzeppelin.com/exploiting-uniswap-from-reentrancy-to-actual-profit/.
When a erc777 token is used, user can reenter
buy
before the transfer of base token has taken place, allowing user to buy fractional token at a cheaper price because baseTokenBalance will stay the same.Proof of Concept
User buys some fractional token.
buyQuote
is used to calculate the amount of baseToken needed for the amount of fractional token user want to buy.In
buyQuote
, baseTokenReserves is divided by fractionalTokenReserves to get the price of fractional token.However, user have the opportunity to reenter
buy
at safeTransferFrom if baseToken is a erc777 token. According to erc777 docs, the hook is called before the transfer of tokens. Hence, baseTokenReserves above will remain the same. This allows the user to buy fractional token at a cheaper price than usual by reentering thebuy
function repeatedly.Tools Used
Manual Review
Recommended Mitigation Steps
Recommend using ReentrancyGuard from OpenZepellin.
The text was updated successfully, but these errors were encountered: