Reentrancy issue #1: Functions buy()
and add()
are vulnerable to reentrancy attack through tokensToSend()
hook of ERC777
#445
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#L95
Vulnerability details
It is important to be aware that I have reported two reentrancy bugs. Each of these have different ways of being activated and can be found in separate functions.
Impact
All calculations done in Caviar Pair are using token balance directly. For example, when users add liquidity, it will use
baseToken.balanceOf(address(this))
to calculate the amount of LP token should be minted.However, if
baseToken
is ERC777 that implementedtokensToSend()
, it could be exploited because thetokensToSend()
hook is called before balance update.Proof of Concept
Assuming
baseToken
isXXX
- an ERC777 that implemntedtokensToSend()
. This hook is called before balance update (Implementation Requirement from EIP-777)Consider the scenario:
1000 XXX - 1000 FracToken
and total supply of LP token is1000
.add()
to add100 XXX
and100 FracToken
. The calculate and mintlpTokenAmount = 100
to Alice.At this moment, liquidity pool has
1000 XXX
(because baseToken is not transferred in yet) and1100 FracToken
and total supply of LP token is1100
.safeTransferFrom()
calls, Alice received atokensToSend()
hook. She used this to calladd()
again with100 XXX
and110 FracToken
. NowlpTokenAmount
is calculated asSo totally, Alice added
200 XXX
and210 FracToken
and she received210
LP token while she is expected to only receive200
LP token normally.Tools Used
Manual Review
Recommended Mitigation Steps
Consider adding
nonReentrant
modifier to these functions to protect them from reentrancy issue.The text was updated successfully, but these errors were encountered: