`TradingUtils._executeTrade()` doesn't check `preTradeBalance` properly
The trade execution function does not correctly record the contract's starting ETH or WETH balance when the sold token is neither WETH nor ETH. As a result, the contract may treat its entire existing ETH or WETH balance as proceeds from the trade and wrap or unwrap all of it, losing those funds.
0x52 Follow with lemonmon and hansfriese
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2022-09-notional-judging/issues/110
Found by
0x52, lemonmon, hansfriese
Summary
TradingUtils._executeTrade() doesn't check preTradeBalance properly.
Vulnerability Detail
TradingUtils._executeTrade() doesn't check preTradeBalance properly.
function _executeTrade(
address target,
uint256 msgValue,
bytes memory params,
address spender,
Trade memory trade
) private {
uint256 preTradeBalance;
if (trade.sellToken == address(Deployments.WETH) && spender == Deployments.ETH_ADDRESS) {
preTradeBalance = address(this).balance;
// Curve doesn't support Deployments.WETH (spender == address(0))
uint256 withdrawAmount = _isExactIn(trade) ? trade.amount : trade.limit;
Deployments.WETH.withdraw(withdrawAmount);
} else if (trade.sellToken == Deployments.ETH_ADDRESS && spender != Deployments.ETH_ADDRESS) {
preTradeBalance = IERC20(address(Deployments.WETH)).balanceOf(address(this));
// UniswapV3 doesn't support ETH (spender != address(0))
uint256 depositAmount = _isExactIn(trade) ? trade.amount : trade.limit;
Deployments.WETH.deposit{value: depositAmount }();
}
(bool success, bytes memory returnData) = target.call{value: msgValue}(params);
if (!success) revert TradeExecution(returnData);
if (trade.buyToken == address(Deployments.WETH)) {
if (address(this).balance > preTradeBalance) {
// If the caller specifies that they want to receive Deployments.WETH but we have received ETH,
// wrap the ETH to Deployments.WETH.
uint256 depositAmount;
unchecked { depositAmount = address(this).balance - preTradeBalance; }
Deployments.WETH.deposit{value: depositAmount}();
}
} else if (trade.buyToken == Deployments.ETH_ADDRESS) {
uint256 postTradeBalance = IERC20(address(Deployments.WETH)).balanceOf(address(this));
if (postTradeBalance > preTradeBalance) {
// If the caller specifies that they want to receive ETH but we have received Deployments.WETH,
// unwrap the Deployments.WETH to ETH.
uint256 withdrawAmount;
unchecked { withdrawAmount = postTradeBalance - preTradeBalance; }
Deployments.WETH.withdraw(withdrawAmount);
}
}
}
It uses preTradeBalance to manage the WETH/ETH deposits and withdrawals.
But it doesn't save the correct preTradeBalance for some cases.
- Let's assume
trade.sellToken = some ERC20 token(not WETH/ETH), trade.buyToken = WETH - Before executing the trade,
preTradeBalancewill be 0 as bothifconditions are false. - Then all ETH inside the contract will be converted to WETH and considered as a
amountBoughthere and here. - After all, all ETH of the contract will be lost.
- All WETH of the contract will be lost also when
trade.sellToken = some ERC20 token(not WETH/ETH), trade.buyToken = ETHhere.
Impact
All of ETH/WETH balance of the contract might be lost in some cases.
Code Snippet
Tool used
Manual Review
Recommendation
We should check preTradeBalance properly. We can remove the current code for preTradeBalance and insert the below code before executing the trade.
if (trade.buyToken == address(Deployments.WETH)) {
preTradeBalance = address(this).balance;
} else if (trade.buyToken == Deployments.ETH_ADDRESS) {
preTradeBalance = IERC20(address(Deployments.WETH)).balanceOf(address(this));
}
Discussion
jeffywu
@weitianjie2000
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
Likely the pre-trade balance is only recorded when the sold token is WETH or ETH, so for other sold tokens it stays zero and the post-trade difference includes the contract's whole pre-existing balance.
Impact
All of the contract's ETH or WETH balance might be lost in some trade configurations.
What the attacker needed
Contract holds ETH or WETH; trade sells a non-WETH/ETH token; trade buys WETH or ETH
The fix
The pre-trade balance should be recorded based on the bought token before the trade executes, rather than only when the sold token is WETH or ETH.
How it was found
- Set the sold token to an ERC20 that is neither WETH nor ETH and the bought token to WETH.
- Observe that the pre-trade balance remains zero because neither condition is met.
- Execute the trade, after which all ETH in the contract is wrapped into WETH and counted as bought amount.
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.
A smart contract can hold both ether (ETH) and a wrapped version of ether called WETH. This function tries to track how much the contract had before a trade so it only converts the new amount afterwards, but in one situation it starts the count at zero, so it converts everything the contract already held.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.