Infinite approval to an arbitrary address can be used to steal all the funds from the contract
The contract granted infinite token approval to a router address supplied by the caller, which could be a fake contract. An attacker could use this to steal any tokens left in the contract.
Submitted by WatchPug, also found by catchup, csanuragjain, rayn, and VAD37
function _startBridge(AnyswapData memory _anyswapData) internal {
// Check chain id
require(block.chainid != _anyswapData.toChainId, "Cannot bridge to the same network.");
address underlyingToken = IAnyswapToken(_anyswapData.token).underlying();
if (underlyingToken == IAnyswapRouter(_anyswapData.router).wNATIVE()) {
IAnyswapRouter(_anyswapData.router).anySwapOutNative{ value: _anyswapData.amount }(
_anyswapData.token,
_anyswapData.recipient,
_anyswapData.toChainId
);
return;
}
if (_anyswapData.token != address(0)) {
// Has underlying token?
if (underlyingToken != address(0)) {
// Give Anyswap approval to bridge tokens
LibAsset.approveERC20(IERC20(underlyingToken), _anyswapData.router, _anyswapData.amount);
IAnyswapRouter(_anyswapData.router).anySwapOutUnderlying(
_anyswapData.token,
_anyswapData.recipient,
_anyswapData.amount,
_anyswapData.toChainId
);
} else {
function approveERC20(
IERC20 assetId,
address spender,
uint256 amount
) internal {
if (isNativeAsset(address(assetId))) return;
uint256 allowance = assetId.allowance(address(this), spender);
if (allowance < amount) {
if (allowance > 0) SafeERC20.safeApprove(IERC20(assetId), spender, 0);
SafeERC20.safeApprove(IERC20(assetId), spender, MAX_INT);
}
}
In the AnyswapFacet.sol, _anyswapData.router is from the caller’s calldata, which can really be any contract, including a fake Anyswap router contract, as long as it complies to the interfaces used.
And in _startBridge, it will grant infinite approval for the _anyswapData.token to the _anyswapData.router.
This makes it possible for a attacker to steal all the funds from the contract.
Which we explained in #159, the diamond contract may be holding some funds for various of reasons.
Proof of Concept
Given:
There are 100 USDC tokens in the contract.
- The attacker can submit a
startBridgeTokensViaAnyswap()with a FAKE_anyswapData.router. - Once the FAKE router contract deployed by the attacker got the infinite approval from the diamond contract, the attacker can call
transferFrom()and take all the funds, including the 100 USDC in the contract anytime.
Recommended Mitigation Steps
- Whitelisting the
_anyswapData.routerrather than trusting user’s inputs; - Or, only
approve()for the amount that required for the current transaction instead of infinite approval.
H3xept (Li.Fi) acknowledged, but disagreed with High severity and commented:
We are aware that the contract allows users to use latent funds, although we disagree on it being an issue as no funds (ERC20 or native) should ever lay in the contract. To make sure that no value is ever kept by the diamond, we now provide refunds for outstanding user value (after bridges/swaps).
gzeon (judge) decreased severity to Medium and commented:
Warden highlighted the vulnerability in
AnyswapFacetwhich would allow attacker to grant approval to arbitrary contract.There can be fund leftover in the contract under normal operation, for example this tx. In fact, ~$300 worth of token is left in the LI.Fi smart contract on ETH mainnet 0x5a9fd7c39a6c488e715437d7b1f3c823d5596ed1 as of block 14597316. I don’t think this is High Risk because the max amount lost is no more than allowed slippage, which can be loss to MEV too.
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 trusted a user-supplied router address and granted it infinite token approval without validation.
Impact
An attacker could steal any tokens left in the contract, though the judge noted the maximum loss is limited to leftover funds and slippage.
What the attacker needed
Contract holds leftover tokens; attacker can deploy a fake router contract
The fix
The router address should be whitelisted or only the exact amount needed should be approved, not an infinite amount.
How it was found
- The attacker submits a bridge transaction with a fake router address.
- The fake router contract receives infinite approval from the diamond contract.
- The attacker calls transferFrom to take all funds from the contract.
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.
A smart contract can let another address spend its tokens, called an approval. Here the contract gave unlimited spending permission to an address the user provided, which could be a fake contract. That fake contract could then take all tokens the main contract held.
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.