Reentrancy issue #2: Functions remove()
and sell()
are vulnerable to reentrancy attack if baseToken implement a call to receiver in _beforeTokenTransfer()
.
#446
Labels
3 (High Risk)
Assets can be stolen/lost/compromised directly
bug
Something isn't working
nullified
Issue is high quality, but not accepted
Lines of code
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L137
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L203
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 an ERC20 token that implemented_beforeTokenTransfer()
and this function has a call hook to receiver, it could be exploited because this function is called before state changes.Proof of Concept
Check out a similar issue from VTVL contest.
code-423n4/2022-09-vtvl-findings#362
Consider the scenario:
1000 XXX - 1000 FracToken
and total supply of LP token is1000
.sell()
to swap from100 FracToken
toXXX
. She will receiveAfter minting, before
XXX
is transferred out, liquidity has1000 XXX - 1100 FracToken
.During
safeTransferFrom()
calls,_beforeTokenTransfer()
does a call to receiver (Alice) before balance update. Alice used it to callsell()
again to swap100 FracToken
. At this moment, theoutputAmount
is calculated asAfter all, Alice swapped
200 FracToken
and get90 + 83 = 173 XXX
. And now, liquidity pool has827 XXX - 1200 FracToken
.Using xyk variant to check, we can notice that at first
xy = 1000000
. After Alice swaps,xy = 992400 < 1000000
.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: