Attacker can prevent user from executing application registered through initFromToken() in AgentFactoryV4 .
The contract's check for an existing Uniswap pair only verifies that the pair address is zero, but anyone can create an empty pair without adding liquidity. An attacker can front-run the victim's executeApplication() call by creating the pair first, causing the victim's transaction to revert and preventing them from executing their application.
0x04bytes Follow with 0xShitgem, anchabadze and 8 others
Originally disclosed on Code4rena
Submitted by 0x04bytes, also found by 0xShitgem, anchabadze, levi_104, newspacexyz, oakcobalt, Olugbenga, patitonar, RaOne, shui, and YouCrossTheLineAlfie
Finding description
AgentFactoryV4 allowed user to register agent with existing/custom agent token. The flow is divided in two steps:
initFromToken(), where the user populates registration with required metadata.executeApplication(), where the application is executed, setting up agent token, dao and provision liquidity on uniswap v2.
When an application is executed in executeApplication(), the contract will check whether the agent token is custom or not. When the agent token is custom, the contract will skip agent token creation step and instead directly create a new pair on uniswap v2 for liquidity provisioning. To prevent the user from provisioning liquidity before execution, the _createPair() internal function will check the existence of the pair. But this check is not enough because any user can permissionlessly create a new pair without provisioning liquidity. This condition can be used by attacker to prevent user from executing application by simply creating a transaction that calls uniswapV2Factory.createPair(agentToken, assetToken) before the victim calls executeApplication(). The execution will fail because the check require(factory.getPair(tokenAddr, assetToken) == address(0), "pool already exists"); will revert since the pair is already created by the attacker in the previous transaction.
Impact
User that register through initFromToken() unable to execute the application.
Proof of Concept
- The attacker monitor the transaction that calls
agentFactoryV4.initFromToken() - Back-run that transaction with a transaction that calls
uniswapV2Factory.createPair(agentToken, assetToken). - The
executeApplication()now will revert with pool exists error for the given application id.
Recommended Mitigation Steps
Simply verifying uniswapV2Factory.getPair(tokenAddr, assetToken) == address(0)) is not enough. When the token pair exists, it is crucial to check if the totalSupply of LP token is equal to 0. By doing that, we can make sure that there is no liquidity provisioned before the execution.
function _createPair(address tokenAddr) internal returns (address uniswapV2Pair_) {
IUniswapV2Factory factory = IUniswapV2Factory(IUniswapV2Router02(_uniswapRouter).factory());
+ uniswapV2Pair_ = uniswapV2Factory.getPair(tokenAddr, assetToken);
+ // valid only if the pair doesn't exist or the total supply of the pair is 0
+ require((uniswapV2Pair_ == address(0)) || (IUniswapV2Pair(uniswapV2Pair_).totalSupply() == 0), "pool already exists");
- require(factory.getPair(tokenAddr, assetToken) == address(0), "pool already exists");
+ if(uniswapV2Pair_ == address(0)) {
+ uniswapV2Pair_ = factory.createPair(tokenAddr, assetToken);
+ }
return (uniswapV2Pair_);
}
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 assumes that a non-zero pair address means liquidity has been provisioned, but Uniswap allows anyone to create a pair with zero liquidity.
Impact
A user who registered through initFromToken() is unable to execute their application.
What the attacker needed
Attacker can monitor pending transactions; attacker can submit a transaction before the victim's
The fix
The check should also allow the pair to exist if its LP token total supply is zero, and only create the pair when it does not exist.
How it was found
- The attacker monitors the transaction that calls agentFactoryV4.initFromToken().
- The attacker back-runs that transaction with a transaction that calls uniswapV2Factory.createPair(agentToken, assetToken).
- The victim's executeApplication() now reverts with a pool exists error for the given application id.
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 you register an agent using an existing token, the contract later sets up a trading pair on Uniswap. The contract checks that no pair exists yet, but an attacker can create an empty pair themselves without adding any money. This makes the victim's setup fail because the contract sees a pair already exists.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.