Improper Approval Mechanism of Clearing House
The ClearingHouse contract sets an approval for the transfer proxy using the contract's full token balance, but the debt payment may not consume the entire approval. For non-standard tokens like USDT that require zero allowance before setting a new one, subsequent transfers fail because a non-zero allowance remains.
0xsomeone Follow with tsvetanovv and ayeslick
Originally disclosed on Code4rena
Submitted by 0xsomeone, also found by tsvetanovv and ayeslick
https://github.com/code-423n4/2023-01-astaria/blob/main/src/ClearingHouse.sol#L148-L151 https://github.com/code-423n4/2023-01-astaria/blob/main/src/ClearingHouse.sol#L160-L165 https://github.com/code-423n4/2023-01-astaria/blob/main/src/LienToken.sol#L637-L641 https://github.com/code-423n4/2023-01-astaria/blob/main/src/CollateralToken.sol#L566-L577
The ClearingHouse implementation performs a @solmate-based safeApprove instruction ([1]) with the remaining balanceOf(address(this)) but contains code handling any remainder of funds that may remain in the contract [2]. Investigation of the payDebtViaClearingHouse function will indicate that the function may not consume the maximum approval that was set to the TRANSFER_PROXY [3].
As a result, any consequent invocation of _execute via safeTransferFrom from OpenSea with a paymentToken (i.e. identifier) such as USDT would fail. Given that the ClearingHouse of a particular collateralId is created only once, this vulnerability will impact consequent listings and cause them to fatally fail for a token that has been used in the past and is part of the non-compliant EIP-20 tokens, with USDT being the prime and most popular example.
The code in USDT that causes this complication is as follows:
require(!((_value != 0) && (allowed[msg.sender][_spender] != 0)));
Proof of Concept
The vulnerability is clearly defined above, however, for testing purposes, the ClearingHouse::safeTransferFrom function of a particular clearing house instance can be invoked twice with the same arguments. The second invocation will fail provided that the first invocation provided a token balance that exceeds the number of funds necessary for the debt payment which should be denoted in USDT.
On an important note, if the code used safeApprove from @openzeppelin this issue would affect any token, however, it is limited to non-standard tokens due to the usage of the @solmate implementation of safeApprove.
Tools Used
Manual review of the codebase. Historically, findings pertaining to incorrect approval mechanisms that do not support USDT have been marked as “medium” in severity in the past in the following cases:
Recommended Mitigation Steps
We advise approvals to be properly handled by evaluating whether a non-zero approval already exists and in such an instance nullifying it before re-setting it to a non-zero value. Example below:
// Optimizing lookup
address transferProxy = address(ASTARIA_ROUTER.TRANSFER_PROXY());
// If existing approval is non-zero -> set it to zero
if (ERC20(paymentToken).allowance(address(this), transferProxy) != 0) {
ERC20(paymentToken).safeApprove(transferProxy, 0);
}
// Set non-zero approval
ERC20(paymentToken).safeApprove(transferProxy, payment - liquidatorPayment);
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 contract uses solmate's safeApprove to set a non-zero allowance without first resetting any existing non-zero allowance to zero, which non-standard tokens like USDT require.
Impact
Consequent listings for a token that has been used before will fatally fail for non-compliant EIP-20 tokens such as USDT.
What the attacker needed
Token is USDT or similar non-standard EIP-20; ClearingHouse already used for that token
The fix
The approval should be handled by checking if a non-zero allowance exists and setting it to zero before setting a new non-zero allowance.
How it was found
- Invoke the ClearingHouse safeTransferFrom function with a token balance exceeding the debt payment amount.
- Invoke the same function again with the same arguments; the second call fails because a non-zero allowance remains.
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, have a rule: you cannot change an allowance from one non-zero amount to another non-zero amount; you must first set it to zero. The contract sets a large allowance but only uses part of it, leaving a leftover non-zero allowance. The next time it tries to set a new allowance, the token rejects the transaction, so future transfers fail.
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.