Arbitrary contract call within UniV3LpVault._swap with controllable swapPath
The vault's swap function accepts a user-supplied path and only validates the first and last tokens, not intermediate ones. An attacker could create a malicious token and pools to execute arbitrary code during the swap, though no concrete exploit was demonstrated.
Submitted by rayn
UniV3LpVault.sol#L621 UniV3LpVault.sol#L379 UniV3LpVault.sol#L520 UniV3LpVault.sol#L521
UniV3LpVault._swap utilizes swapRouter.exactInput to perform swaps between two tokens. During swaps, transfer function of each token along the path will be called to propagate the assets.
Since anyone can create a uniswap pair of arbitrary assets, it is possible to include intermediate hop with malicious tokens within the path. Thus UniV3LpVault._swap effectively grants users the ability to perform arbitrary contract calls during the swap process if swapPath is not validated properly.
Usage of invalidated swapPath can be found in UniV3LpVault.flashFocusCall and UniV3LpVault.repayDebt.
Proof of Concept
The security of Comptroller and UniV3LpVault relies on validating all used tokens thoroughly. This is done by a whitelist mechanism where admin decides a predefined set of usable tokens, and users can only perform actions within the allowed range. This whitelist approach eliminates most of the attack surface regarding directly passing in malicious tokens as arguments.
Apart from passing malicious tokens directly, there are a few other potential weaknesses, the most obvious one is leveraging flash loans for collaterals. However, due to the adoption of AAVE LendingPool, the external validation within flash loan pool blocks this approach.
Unfortunately, a more obscure path exists. Looking at the swapping mechanism, it is not hard to realize it is backed by uniswapV3. An interesting characteristic of uniswap pools is that anyone can create pools for any token pairs, thus if we don’t fully validate each and every pool we are using, chances are there will be malicious entries hidden within them.
This is partially the case which we see here, the user gets to supply a path, where the source and target are validated against benign tokens, the intermediate ones are not. An example of utilizing path for arbitrary function call is illustrated below
- Create malicious token tokenM
- Create pools tokenS<->tokenM and tokenM<->tokenT where tokenS and tokenT are benign tokens
- Supply path (tokenS, tokenM, tokenT) for swapping
In the above case, when transferring tokenM while doing swap, we have full control over code executed and can insert arbitrary contract calls within.
Noticeably, while gaining arbitrary contract calls sounds dangerous, it does not necessarily mean the contract is exploitable. It still depends on the scenario in which an arbitrary call happens.
In the case of duality, the two locations where arbitrary swapPath can be provided is in flashFocusCall and repayDebt, both in which holds a local lock over UniV3LpVault. No global are applied to Comptroller or Ctokens while performing swaps.
function flashFocusCall(FlashFocusParams calldata params) external override {
...
{
...
if (!tokenOfPool && params.swapPath.length > 0) amountIn0 = _swap(params.swapPath, params.amount);
...
}
...
}
function flashFocus(FlashFocusParams calldata params)
external
override
nonReentrant(true)
isAuthorizedForToken(params.tokenId)
avoidsShortfall
{
...
flashLoan.LENDING_POOL().flashLoan(
receiverAddress,
assets,
amounts,
modes,
onBehalfOf,
newParams,
referralCode
);
}
function repayDebt(RepayDebtParams calldata params)
external
override
nonReentrant(true)
isAuthorizedForToken(params.tokenId)
avoidsShortfall
returns (uint256 amountReturned)
{
...
{
...
if (amountOutFrom0 == 0 && params.swapPath0.length > 0) amountOutFrom0 = _swap(params.swapPath0, amount0);
if (amountOutFrom1 == 0 && params.swapPath1.length > 0) amountOutFrom1 = _swap(params.swapPath1, amount1);
...
}
...
}
The lack of global locks here had us doubting whether an attack is possible. While we spent a considerable amount of time and failed to come up with any possible attack vectors, the complexity of the system held us back from concluding that an attack is impossible.
Thus we report this finding here in hope of inspiring developers either to prove the attack impossible or mitigate the attack surface.
Tools Used
vim, ganache-cli
Recommended Mitigation Steps
The easiest way to mitigate this is to validate the entire path against a predefined whitelist while in _checkSwapPath. This approach is far from optimal and also limits the flexibility of swapping between tokens. However, before security is proved, this is the best approach we can come up with.
0xdramaone (Duality Focus) confirmed and commented:
Great issue raised!
Agreed that it is safest to validate all tokens to avoid arbitrary transfer functionality, and we will also enforce global reentrancy lock wherever possible. Agree with severity.
Jack the Pug (judge) commented:
After some quick research on this issue, I also failed to find a valid attack vector. I agree that this is a noteworthy issue. A
medseverity seems fair given the detailed and honest issue description by the warden.
0xdramaone (Duality Focus) resolved
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 swap path validation only checks the first and last tokens, allowing malicious intermediate tokens to be included.
Impact
An attacker could execute arbitrary contract calls during a swap, but no concrete exploit was found and the report only suggests a potential risk.
What the attacker needed
Ability to create tokens and pools; user can supply swap path
The fix
The fix is to validate the entire swap path against a whitelist of allowed tokens.
How it was found
- Create a malicious token.
- Create pools pairing the malicious token with two benign tokens.
- Supply a swap path that includes the malicious token as an intermediate hop.
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 vault lets users swap tokens along a path. It checks the first and last token but not the ones in between. Since anyone can create a fake token, an attacker could insert one and run their own code during the swap, but the report does not show a full attack.
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.