resolveQueuedTrades() ERC777 re-enter to steal funds
The _openQueuedTrade function transfers a fee refund to the user before marking the queued trade as no longer queued. If the token is ERC777, an attacker can re-enter the cancelQueuedTrade function during the transfer to receive a second refund, stealing funds.
Source: https://github.com/sherlock-audit/2022-11-buffer-judging/issues/130
Found by
HonorLt, bin2chen, KingNFT
Summary
_openQueuedTrade() does not follow the “Checks Effects Interactions” principle and may lead to re-entry to steal the funds
https://fravoll.github.io/solidity-patterns/checks_effects_interactions.html
Vulnerability Detail
The prerequisite is that tokenX is ERC777 e.g. “sushi”
- resolveQueuedTrades() call _openQueuedTrade()
- in _openQueuedTrade() call "tokenX.transfer(queuedTrade.user)" if (revisedFee < queuedTrade.totalFee) before set queuedTrade.isQueued = false;
function _openQueuedTrade(uint256 queueId, uint256 price) internal {
...
if (revisedFee < queuedTrade.totalFee) {
tokenX.transfer( //***@audit call transfer , if ERC777 , can re-enter ***/
queuedTrade.user,
queuedTrade.totalFee - revisedFee
);
}
queuedTrade.isQueued = false; //****@audit change state****/
}
3.if ERC777 re-enter to #cancelQueuedTrade() to get tokenX back,it can close, because queuedTrade.isQueued still equal true 4. back to _openQueuedTrade() set queuedTrade.isQueued = false
5.so steal tokenX
Impact
if tokenX equal ERC777 can steal token
Code Snippet
Tool used
Manual Review
Recommendation
follow “Checks Effects Interactions”
function _openQueuedTrade(uint256 queueId, uint256 price) internal {
...
+ queuedTrade.isQueued = false;
// Transfer the fee to the target options contract
IERC20 tokenX = IERC20(optionsContract.tokenX());
tokenX.transfer(queuedTrade.targetContract, revisedFee);
- queuedTrade.isQueued = false;
emit OpenTrade(queuedTrade.user, queueId, optionId);
}
Discussion
0x00052
Fixed in PR#8
Changes look good. Trade is now removed from queue before sending user refund during option opening to avoid potential reetrancy. Canceling already removed trade before sending refund so no change needed there.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The contract updates the trade's queued status after making an external token transfer, violating the checks-effects-interactions pattern and allowing re-entrancy.
Impact
If tokenX is an ERC777 token, an attacker could steal tokens by re-entering the cancel function during the refund transfer.
What the attacker needed
TokenX is an ERC777 token; attacker can trigger a queued trade resolution
The fix
The contract was fixed by moving the state update (setting isQueued to false) before the external token transfer, following the checks-effects-interactions pattern.
How it was found
- resolveQueuedTrades() calls _openQueuedTrade()
- In _openQueuedTrade(), tokenX.transfer(queuedTrade.user) is called if revisedFee < queuedTrade.totalFee, before setting queuedTrade.isQueued = false
- If tokenX is ERC777, re-enter to cancelQueuedTrade() to get tokenX back, because queuedTrade.isQueued is still true
- Return to _openQueuedTrade() and set queuedTrade.isQueued = false
- Steal tokenX
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When a smart contract sends tokens to a user, the token contract can call back into the original contract before the transfer finishes. If the original contract hasn't yet updated its records to show the trade is closed, the attacker can call another function that also sends them tokens. This report shows that the order of operations allowed such a double refund.