Liquidators can be tricked to operate with LiquidationPairs that were deployed using the LiquidationPairFactory but they configured the LiquidationSource as a fake malicious contract
The LiquidationPairFactory allowed anyone to create a LiquidationPair with an arbitrary malicious source contract. An attacker could trick users into swapping through such a pair, causing their input tokens to be sent to the attacker instead of the legitimate vault.
0xStalin Follow with MohammedRizwan
Originally disclosed on Code4rena
Submitted by 0xStalin, also found by MohammedRizwan
Users and Bots can be tricked into operating with LiquidationPairs that were deployed using the LiquidationPairFactory but they configured the LiquidationSource as a fake malicious contract that will allow the Liquidator’s creator to steal all the POOL tokens that were meant to be used to liquidate the Vault’s Yield.
Proof of Concept
- The current implementation of the
LiquidationPairFactory::createPair()allows the callers to set the LiquidationSource as any arbitrary address with no restrictions. - The problem is that the address of the
_sourceparameter may not necessarily be a real vault contract, and even though the_sourceaddress is set as a fake malicious contract, the LiquidationPair will be created and added to thedeployedPairsmapping, and as stated in the protocol’s documentation, any LiquidationPair created by the factory determines if a pair is legitimate or not
-
So, if a LiquidationPair is created by the LiquidationPairFactory may allow malicious users to trick users who want to liquidate the Vault’s Yield to operate with a LiquidationPair who’ll end up stealing their POOL tokens (tokenIn) when swapping tokens.
-
Practical Example of how the LiquidationPair would steal the user’s assets, (Keep in mind that
sourceis not the address of a Vault, but an arbitrary contract)- The fake
sourcecontract would look like this:
- The fake
contract FakeSource {
function targetOf(address _token) external view returns (address) {
return <ContractCreatorAddress>;
}
function liquidate(
address _account,
address _tokenIn,
uint256 _amountIn,
address _tokenOut,
uint256 _amountOut
) public virtual override returns (bool) {
return true;
}
}
- Calling
LiquidationPair.target()will returnsource.targetOf(tokenIn), and thesourcecontract can return any address when thetargetOf()is called, so, let’s say that will return the address of its creator. - So,
LiquidationPair.target()will return the address of its creator, instead of returning the expected address of thePrizePoolcontract - Calling
LiquidationPair::swapExactAmount()will do some computations prior to callsource.liquidate(), and thesource.liquidate()can just return true not to cause the tx to be reverted. - So,
LiquidationPair::swapExactAmount()will basically do nothing. - With the above points in mind, let’s see what would be the result of swapping using the LiquidationRouter contract
function swapExactAmountOut(
LiquidationPair _liquidationPair,
address _receiver,
uint256 _amountOut,
uint256 _amountInMax
) external onlyTrustedLiquidationPair(_liquidationPair) returns (uint256) {
//@audit-issue => The `tokenIn` will be transferred to the address of the LiquidationPair's creator instead of the PrizePool contract (Point 1) <====== Point 1 ======>
IERC20(_liquidationPair.tokenIn()).safeTransferFrom(
msg.sender,
_liquidationPair.target(),
_liquidationPair.computeExactAmountIn(_amountOut)
);
//@audit-issue => This call will basically do nothing, just return a true not to cause the tx to be reverted (Point 2) <====== Point 2 ======>
uint256 amountIn = _liquidationPair.swapExactAmountOut(_receiver, _amountOut, _amountInMax);
emit SwappedExactAmountOut(_liquidationPair, _receiver, _amountOut, _amountInMax, amountIn);
return amountIn;
}
Recommended Mitigation Steps
-
Use the
deployedVaultsmapping of theVaultFactorycontract to validate if the inputted address of the_sourceparameter is a valid vault supported by the Protocol.- Additionally, it could be a good idea to set the
_tokenInand_tokenOutby pulling the values that are already set up in the vault.
- Additionally, it could be a good idea to set the
function createPair(
ILiquidationSource _source,
- address _tokenIn,
- address _tokenOut,
uint32 _periodLength,
uint32 _periodOffset,
uint32 _targetFirstSaleTime,
SD59x18 _decayConstant,
uint112 _initialAmountIn,
uint112 _initialAmountOut,
uint256 _minimumAuctionAmount
) external returns (LiquidationPair) {
+ require(VaultFactory.deployedVaults(address(_source)) == true, "_source address is not a supported Vault");
+ address _prizePool = _source.prizePool();
+ address _tokenIn = _prizePool.prizeToken();
+ address _tokenOut = address(_source);
LiquidationPair _liquidationPair = new LiquidationPair(
_source,
_tokenIn,
_tokenOut,
_periodLength,
_periodOffset,
_targetFirstSaleTime,
_decayConstant,
_initialAmountIn,
_initialAmountOut,
_minimumAuctionAmount
);
allPairs.push(_liquidationPair);
deployedPairs[_liquidationPair] = true;
emit PairCreated(
_liquidationPair,
_source,
_tokenIn,
_tokenOut,
_periodLength,
_periodOffset,
_targetFirstSaleTime,
_decayConstant,
_initialAmountIn,
_initialAmountOut,
_minimumAuctionAmount
);
return _liquidationPair;
}
Assessed type
Invalid Validation
asselstine (PoolTogether) confirmed and commented:
This is an interesting report.
There are two ways we expect liquidations to occur:
- EOAs that interact with the LiquidationRouter. (users, off-chain bots, etc)
- Smart Contracts that interact with the LiquidationPair directly
Smart contracts that interact with the Pair can easily assert that they received the expected tokens; so a malicious LiquidationSource would not be a problem.
However, EOAs that interact with a Pair via the LiquidationRouter would be vulnerable to this kind of attack.
This makes me think that the fix is actually to alter the LiquidationRouter so that:
- In
swapExactAmountOutthe router makes itself the recipient of the tokens.- The router asserts that it has received the expected amount of tokens
- The router transfers to the tokens to the caller.
It costs more gas, but this change ensures that anyone who calls the router would receive the expected tokens.
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
Impact
Users and bots that interact with the malicious pair via the LiquidationRouter could lose their input tokens to the attacker; smart contracts that check received tokens are not affected.
What the attacker needed
Attacker can deploy a malicious contract; victim uses LiquidationRouter with the malicious pair
The fix
The LiquidationRouter should be altered so that it receives the tokens itself, asserts it received the expected amount, and then forwards them to the caller; alternatively, the factory should validate the source against deployed vaults.
How it was found
- Attacker deploys a fake source contract that returns the attacker's address from targetOf and returns true from liquidate.
- Attacker calls LiquidationPairFactory.createPair with the fake source, creating a pair that is added to deployedPairs.
- A user or bot is tricked into calling LiquidationRouter.swapExactAmountOut with this malicious pair.
- The router transfers the user's tokenIn to the address returned by target(), which is the attacker.
- The subsequent call to swapExactAmountOut does nothing but return true, so the transaction succeeds and the user loses funds.
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 factory contract lets anyone create a trading pair for liquidating yield. The factory did not check that the source of the pair was a real vault, so an attacker could make a fake source that redirects payments to themselves. When a user swaps through this fake pair, their tokens go to the attacker instead of the real vault.
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.