`SafeERC20.safeApprove` reverts for changing existing approvals
The `CrosschainDistributor._setTotal` function uses `SafeERC20.safeApprove` to change an existing non-zero approval to another non-zero value, which causes a revert. This prevents the owner from updating the total allocation, breaking a core feature of the airdrop protocol.
AkshaySrivastav Follow with Juntao, Musaka and 6 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-tokensoft-judging/issues/141
Found by
AkshaySrivastav, Juntao, Musaka, auditsea, blackhole, circlelooper, jah, kutugu, ni8mare
Summary
SafeERC20.safeApprove reverts when a non-zero approval is changed to a non-zero approval. The CrosschainDistributor._setTotal function tries to change an existing approval to a non-zero value which will revert.
Vulnerability Detail
The safeApprove function has explicit warning:
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
But still the _setTotal use it to change approval amount:
function _allowConnext(uint256 amount) internal {
token.safeApprove(address(connext), amount);
}
/** Reset Connext allowance when total is updated */
function _setTotal(uint256 _total) internal virtual override onlyOwner {
super._setTotal(_total);
_allowConnext(total - claimed);
}
Impact
Due to this bug all calls to setTotal function of CrosschainContinuousVestingMerkle and CrosschainTrancheVestingMerkle will get reverted.
Tokensoft airdrop protocol is meant to be used by other protocols and the ability to change total parameter is an intended offering. This feature will be important for those external protocols due to the different nature & requirement of every airdrop. But this feature will not be usable by airdrop owners due to the incorrect code implementation.
Code Snippet
Tool used
Manual Review
Recommendation
Consider using 'safeIncreaseAllowance' and 'safeDecreaseAllowance' instead of safeApprove in _setTotal.
Discussion
cr-walker
Good catch, updating the code to simply set approval to zero first and then reset approval. I don't think any of the reentrancy attacks that safeApprove() is worried about are relevant here (neither the owner nor the connect protocol are going to use this to rug the contract, they both have much more direct ways to take tokens if malicious!)
cr-walker
Fixed by https://github.com/SoftDAO/contracts/pull/12
maarcweiss
Fixed by approving to 0 before approving again by implementing the following line: token.safeApprove(address(connext), 0);
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 code calls `safeApprove` to change an existing non-zero allowance to another non-zero value, which the SafeERC20 library explicitly disallows.
Impact
All calls to `setTotal` on the affected contracts will revert, making the intended feature unusable for airdrop owners.
What the attacker needed
Owner calls setTotal; existing non-zero approval
The fix
The code was fixed by setting the approval to zero before setting the new approval 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.
In Ethereum tokens, an approval lets someone else spend your tokens. The `safeApprove` function is meant only for setting an initial approval or resetting to zero; changing directly from one non-zero amount to another is blocked because it can be unsafe. Here, the code tries to update an existing approval, so the transaction always fails, and the owner cannot change the total amount.
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.