Residual Allowance Might Allow Tokens In SetToken To Be Stolen
The NotionalTradeModule contract approves the fCash wrapper to spend the SetToken's payment tokens using the maximum send amount, leaving a residual allowance after minting. If the upgradeable fCash wrapper contract is compromised, an attacker could exploit this allowance to withdraw funds from the SetToken contract.
Originally disclosed on Code4rena
Submitted by xiaoming90
Proof of Concept
Whenever _mintFCashPosition function is called to mint new fCash position, the contract will call the _approve function to set the allowance to _maxSendAmount so that the fCash Wrapper contact can pull the payment tokens from the SetToken contract during minting.
function _mintFCashPosition(
ISetToken _setToken,
IWrappedfCashComplete _fCashPosition,
IERC20 _sendToken,
uint256 _fCashAmount,
uint256 _maxSendAmount
)
internal
returns(uint256 sentAmount)
{
if(_fCashAmount == 0) return 0;
bool fromUnderlying = _isUnderlying(_fCashPosition, _sendToken);
_approve(_setToken, _fCashPosition, _sendToken, _maxSendAmount);
uint256 preTradeSendTokenBalance = _sendToken.balanceOf(address(_setToken));
uint256 preTradeReceiveTokenBalance = _fCashPosition.balanceOf(address(_setToken));
_mint(_setToken, _fCashPosition, _maxSendAmount, _fCashAmount, fromUnderlying)
..SNIP..
}
Note that _maxSendAmount is the maximum amount of payment tokens that is allowed to be consumed during minting. This is not the actual amount of payment tokens consumed during the minting process. Thus, after the minting, there will definitely be some residual allowance since it is unlikely that the fCash wrapper contract will consume the exact maximum amount during minting.
The following piece of code shows that having some residual allowance is expected. The _approve function will not set the allowance unless there is insufficient allowance.
/**
* @dev Approve the given wrappedFCash instance to spend the setToken's sendToken
*/
function _approve(
ISetToken _setToken,
IWrappedfCashComplete _fCashPosition,
IERC20 _sendToken,
uint256 _maxAssetAmount
)
internal
{
if(IERC20(_sendToken).allowance(address(_setToken), address(_fCashPosition)) < _maxAssetAmount) {
bytes memory approveCallData = abi.encodeWithSelector(_sendToken.approve.selector, address(_fCashPosition), _maxAssetAmount);
_setToken.invoke(address(_sendToken), 0, approveCallData);
}
}
Impact
Having residual allowance increases the risk of the asset tokens being stolen from the SetToken contract. SetToken contract is where all the tokens/assets are held. If the Notional’s fCash wrapper contract is compromised, it will allow the compromised fCash wrapper contract to withdraw funds from the SetToken contract due to the residual allowance.
Note that Notional’s fCash wrapper contract is not totally immutable, as it is a upgradeable contract. This is an additional risk factor to be considered. If the Notional’s deployer account is compromised, the attacker could upgrade the Notional’s fCash wrapper contract to a malicious one to withdraw funds from the Index Coop’s SetToken contract due to the residual allowance.
Index Coop and Notional are two separate protocols and teams. Thus, it is a good security practice not to place any trust on external party wherever possible to ensure that if one party is compromised, it won’t affect the other party. Thus, there should not be any residual allowance that allows Notional’s contract to withdraw funds from Index Coop’s contract in any circumstance.
In the worst case scenario, a “lazy” manager might simply set the _maxAssetAmount to type(uint256).max. Thus, this will result in large amount of residual allowance left, and expose the SetToken contract to significant risk.
Recommended Mitigation Steps
Approve the allowance on-demand whenever _mintFCashPosition is called, and reset the allowance back to zero after each minting process to eliminate any residual allowance.
function _mintFCashPosition(
ISetToken _setToken,
IWrappedfCashComplete _fCashPosition,
IERC20 _sendToken,
uint256 _fCashAmount,
uint256 _maxSendAmount
)
internal
returns(uint256 sentAmount)
{
if(_fCashAmount == 0) return 0;
bool fromUnderlying = _isUnderlying(_fCashPosition, _sendToken);
_approve(_setToken, _fCashPosition, _sendToken, _maxSendAmount);
uint256 preTradeSendTokenBalance = _sendToken.balanceOf(address(_setToken));
uint256 preTradeReceiveTokenBalance = _fCashPosition.balanceOf(address(_setToken));
_mint(_setToken, _fCashPosition, _maxSendAmount, _fCashAmount, fromUnderlying)
..SNIP..
+ // Reset the allowance back to zero after minting
+ _approve(_setToken, _fCashPosition, _sendToken, 0);
}
Update the _approve accordingly to remove the if-statement related to residual allowance.
function _approve(
ISetToken _setToken,
IWrappedfCashComplete _fCashPosition,
IERC20 _sendToken,
uint256 _maxAssetAmount
)
internal
{
- if(IERC20(_sendToken).allowance(address(_setToken), address(_fCashPosition)) < _maxAssetAmount) {
bytes memory approveCallData = abi.encodeWithSelector(_sendToken.approve.selector, address(_fCashPosition), _maxAssetAmount);
_setToken.invoke(address(_sendToken), 0, approveCallData);
- }
}
ckoopmann (Index Coop) confirmed and commented:
This looks like a prudent suggestion. Will review and potentially adopt the suggested mitigation.
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 approves the maximum send amount and does not reset the allowance after minting, leaving a residual allowance that the external contract can use.
Impact
If the upgradeable fCash wrapper contract is compromised, an attacker could withdraw funds from the SetToken contract due to the residual allowance.
What the attacker needed
Compromise of the upgradeable fCash wrapper contract
The fix
The allowance should be approved on-demand and reset to zero after each minting to eliminate any residual allowance.
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.
When a smart contract lets another contract spend its tokens, it sets an allowance. This report found that the allowance was set higher than needed and not reset, so the other contract could still take tokens later. If that other contract were hacked, the attacker could steal the tokens.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.