Trading will not work on Ethereum if USDT is used
The Trading contract calls approve with type(uint).max on the margin asset every time a deposit is handled. On Ethereum, USDT requires the allowance to be reset to zero before setting a new non-zero value, so the second and later calls revert, preventing traders from initiating market orders, adding margin, adding to positions, or initiating limit orders when USDT is the margin asset.
0xdeadbeef0x Follow with rbserver, Rolezn and 6 others
Originally disclosed on Code4rena
Submitted by 0xdeadbeef0x, also found by rbserver, Rolezn, Ruhum, Faith, mookimgo, 0x52, 8olidity, and KingNFT
Traders will not be able to:
- Initiate a market order
- Add margin
- Add to position
- initiate limit order
If USDT is set as the margin asset and protocol is deployed on Ethereum.
(Note: this issue was submitted after consulting with the sponsor even though currently there are no plans to deploy the platform on Ethereum).
Proof of Concept
USDT has a race condition protection mechanism on ethereum chain:
It does not allow users to change the allowance without first changing the allowance to 0.
approve function in USDT on Ethereum:
https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7#code#L205
function approve(address _spender, uint _value) public onlyPayloadSize(2 * 32) {
// To change the approve amount you first have to reduce the addresses`
// allowance to zero by calling `approve(_spender, 0)` if it is not
// already 0 to mitigate the race condition described here:
// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
require(!((_value != 0) && (allowed[msg.sender][_spender] != 0)));
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
}
In Trading, if users use USDT as margin to:
- Initiate a market order
- Add margin
- Add to position
- initiate limit order
The transaction will revert.
This is due to the the _handleDeposit which is called in all of the above uses.
_handleDeposit calls the USDT margin asset approve function with type(uint).max.
From the second time approve will be called, the transaction will revert.
_handleDeposit in Trading:
https://github.com/code-423n4/2022-12-tigris/blob/588c84b7bb354d20cbca6034544c4faa46e6a80e/contracts/Trading.sol#L652
function _handleDeposit(address _tigAsset, address _marginAsset, uint256 _margin, address _stableVault
, ERC20PermitData calldata _permitData, address _trader) internal {
------
IERC20(_marginAsset).transferFrom(_trader, address(this), _margin/_marginDecMultiplier);
IERC20(_marginAsset).approve(_stableVault, type(uint).max);
IStableVault(_stableVault).deposit(_marginAsset, _margin/_marginDecMultiplier);
------
}
Tools Used
VS Code
Recommended Mitigation Steps
No need to to approve USDT every time. The protocol could:
- Keep a record if allowance was already set on an address
- Create an external function that can be called by the owner to approve the a token address
TriHaz (Tigris Trade) confirmed
Alex the Entreprenerd (judge) commented:
In contrast to unsafeERC20 functions (OOS), this report shows an issue with USDT or similar tokens that require a zero to non-zero allowance.
Not resetting to zero and instead calling to set max multiple times will cause reverts in those cases.
For this reason I agree with Medium Severity.
GainsGoblin (Tigris Trade) resolved:
Mitigation: https://github.com/code-423n4/2022-12-tigris/pull/2#issuecomment-1419174789
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 unconditionally calls approve with type(uint).max on the margin asset in _handleDeposit, without checking whether an allowance already exists or resetting it to zero first, which violates USDT's requirement that non-zero allowances be reduced to zero before being changed.
Impact
Traders using USDT as margin on Ethereum cannot initiate market orders, add margin, add to positions, or initiate limit orders after the first approval, effectively blocking those trading functions for USDT.
What the attacker needed
Protocol deployed on Ethereum; USDT set as the margin asset; Trader uses USDT as margin
The fix
The protocol should avoid approving USDT every time, for example by recording whether an allowance was already set or by providing an owner-callable function to approve a token address once.
How it was found
- Deploy the protocol on Ethereum with USDT as the margin asset.
- A trader initiates a market order, adds margin, adds to a position, or initiates a limit order using USDT.
- The first such action succeeds because the allowance is initially zero.
- On the second and subsequent actions, _handleDeposit calls approve with type(uint).max again, and USDT reverts because the existing allowance is not zero.
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.
Some tokens, like USDT on Ethereum, have a safety rule: you cannot change an existing spending permission directly to a new non-zero amount; you must first set it to zero. The contract here tries to set the maximum permission every time, so after the first time the token rejects the request and the whole transaction fails. This means users cannot trade or add margin using USDT on Ethereum.
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.