Unspent allowance may break functionality in AMO
The AMO contract uses safeApprove to grant the Curve pool an allowance, but safeApprove reverts if the current allowance is not zero. Any leftover allowance causes addLiquidity and addLiquidityOnlyStETH to revert, potentially bricking the contract.
Submitted by adriro
https://github.com/code-423n4/2023-05-xeth/blob/main/src/AMO2.sol#L545 https://github.com/code-423n4/2023-05-xeth/blob/main/src/AMO2.sol#L546 https://github.com/code-423n4/2023-05-xeth/blob/main/src/AMO2.sol#L571
An unspent allowance may cause a denial of service during the calls to safeApprove() in the AMO contract.
The AMO contract uses the safeApprove() function to grant the Curve pool permission to spend funds while adding liquidity. When adding liquidity into the Curve pool, the AMO contract needs to approve allowance so the AMM can pull tokens from the caller.
The safeApprove() function is a wrapper provided by the SafeERC20 library present in the OpenZeppelin contracts package, its implementation is the following:
function safeApprove(IERC20 token, address spender, uint256 value) internal {
// 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'
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
As the comment warns, this should only be used when setting an initial balance or resetting it to zero. In the AMO contract the use of safeApprove() is included in the functions that are in charge of adding liquidity to the Curve pool (addLiquidity() and addLiquidityOnlyStETH()), implying a repeatedly use whenever the allowance needs to be set so that the pool can pull the funds. As we can see in the implementation, if the current allowance is not zero the function will revert.
This means that any unspent allowance of xETH or stETH (i.e. allowance(AMO, curvePool) > 0) will cause a denial of service in the addLiquidity() and addLiquidityOnlyStETH() functions, potentially bricking the contract.
Proof of Concept
- Suppose there is an unspent allowance of stETH in the AMO contract,
stETH.allowance(AMO, curvePool) > 0. - Admin calls
addLiquidityOnlyStETH(stETHAmount, minLpOut)withstETHAmount > 0to provide liquidity to the pool. - Transaction will be reverted in the call to
safeApprove()as(value == 0) || (token.allowance(address(this), spender) == 0)will be false.
Recommendation
Simply use approve(), or first reset the allowance to zero using safeApprove(spender, 0), or use safeIncreaseAllowance().
Note from warden
Even though the deprecated usage of safeApprove() is mentioned in the automated findings, this report demonstrates how this function can cause a serious vulnerability that may end up bricking the contract.
Assessed type
DoS
Change
safeApproveto approve.PR: https://github.com/code-423n4/2023-05-xeth/commit/793dade5217bd5751856f7cf0bccd4936286aeab
Status: Mitigation confirmed. Full details in reports from ronnyx2017, bin2chen and d3e4.
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 use of safeApprove, which requires the current allowance to be zero, in functions that may be called when a non-zero allowance already exists.
What the attacker needed
Unspent allowance > 0; admin calls addLiquidity
The fix
The fix was to change safeApprove to approve.
How it was found
- Suppose there is an unspent allowance of stETH in the AMO contract.
- Admin calls addLiquidityOnlyStETH with a positive amount.
- The transaction reverts in the call to safeApprove because the 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.
This is a smart contract bug. The contract tried to give a spending permission (an allowance) to another contract, but used a function that only works if no permission was already given. If any permission was left over from before, the function fails and the contract can no longer add liquidity.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.