Skip to content
Business logic Audit finding Intermediate

Using AgentFactory::setAssetToken will lead to loss of funds

The contract stores a single global asset token address, but applications record only amounts, not which token they were funded with. If an admin changes the asset token, withdrawals and application execution can fail or use the wrong token, potentially locking or misdirecting funds.

YouCrossTheLineAlfie Follow

Originally disclosed on Code4rena

Submitted by YouCrossTheLineAlfie

The AgentFactory::assetToken is initially set upon early initialize call:

    // AgentFactory.sol
    function initialize(
        // . . . Rest of the code . . .
    ) public initializer {
        __Pausable_init();

        tokenImplementation = tokenImplementation_;
        veTokenImplementation = veTokenImplementation_;
        daoImplementation = daoImplementation_;
        assetToken = assetToken_;                       <<@ -- // Sets assetToken initially
        tbaRegistry = tbaRegistry_;
        nft = nft_;
        applicationThreshold = applicationThreshold_;
        _nextId = 1;
        _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
        _vault = vault_;
    }

This assetToken is being used inside AgentFactory::executeApplication, AgentFactory::_createNewAgentToken and AgentFactory::withdraw.

However, changing this assetToken via AgentFactory::setAssetToken has serious complications;

    function setAssetToken(address newToken) public onlyRole(DEFAULT_ADMIN_ROLE) {
        assetToken = newToken;          <<@ -- // Sets `assetToken`
    }

These issues are described below:

  1. AgentFactory::withdraw function would fail as the new assetToken will be different from the funds held by the contract during proposeAgent call.
    function withdraw(uint256 id) public noReentrant {
        Application storage application = _applications[id];

        require(msg.sender == application.proposer || hasRole(WITHDRAW_ROLE, msg.sender), "Not proposer");

        require(application.status == ApplicationStatus.Active, "Application is not active");

        require(block.number > application.proposalEndBlock, "Application is not matured yet");

        uint256 withdrawableAmount = application.withdrawableAmount;

        application.withdrawableAmount = 0;
        application.status = ApplicationStatus.Withdrawn;

        IERC20(assetToken).safeTransfer(application.proposer, withdrawableAmount);      <<@ -- // Would break due to different assetToken than upon proposal
    }
  1. AgentFactory::executeApplication would fail as there might be no new assetToken available inside the contract:
    function executeApplication(uint256 id, bool canStake) public noReentrant {
        // . . . Rest of the code . . .
        // C2
        address lp = IAgentToken(token).liquidityPools()[0];
        IERC20(assetToken).transfer(token, initialAmount);          <<@ -- // Would revert
        IAgentToken(token).addInitialLiquidity(address(this));
  1. There is no way available to recover old assetToken balance as the contract lacks a recoverERC20 kind of function.
  2. If there were a case where assetToken were available inside the contract, the _createNewAgentToken would use unintended assetToken than what it was actually proposed to:
    function _createNewAgentToken(string memory name, string memory symbol) internal returns (address instance) {
        instance = Clones.clone(tokenImplementation);
        IAgentToken(instance).initialize(
            [_tokenAdmin, _uniswapRouter, assetToken],          <<@ -- // Initialising agent token
            abi.encode(name, symbol),
            _tokenSupplyParams,
            _tokenTaxParams
        );

        allTradingTokens.push(instance);
        return instance;
    }

This cannot be mitigated with current implementation as Base has a private mempool, the protocol in no capacity can guarantee that before setAssetToken all tokens are withdrawn by the WITHDRAW_ROLE.

Also, these issues are commonly found inside the AgentFactoryV3 and AgentFactoryV4 contracts.

Recommended mitigation steps

It is recommended that instead of using a public variable of assetToken in function calls other than proposeAgent, add the asset token’s address directly inside the Application struct and fetch it, as these calls anyways use the Application struct’s mapping:

    struct Application {
        string name;
        string symbol;
        string tokenURI;
+        address tokenAddress;
        ApplicationStatus status;
        uint256 withdrawableAmount;
        address proposer;
        uint8[] cores;
        uint256 proposalEndBlock;
        uint256 virtualId;
        bytes32 tbaSalt;
        address tbaImplementation;
        uint32 daoVotingPeriod;
        uint256 daoThreshold;
    }

Updated withdraw:

    function withdraw(uint256 id) public noReentrant {
        Application storage application = _applications[id];

        require(msg.sender == application.proposer || hasRole(WITHDRAW_ROLE, msg.sender), "Not proposer");

        require(application.status == ApplicationStatus.Active, "Application is not active");

        require(block.number > application.proposalEndBlock, "Application is not matured yet");

        uint256 withdrawableAmount = application.withdrawableAmount;

        application.withdrawableAmount = 0;
        application.status = ApplicationStatus.Withdrawn;

-        IERC20(assetToken).safeTransfer(application.proposer, withdrawableAmount);
+        IERC20(application.tokenAddress).safeTransfer(application.proposer, withdrawableAmount);
    }

Updated executeApplication:

    function executeApplication(uint256 id, bool canStake) public noReentrant {
        // . . . Rest of the code . . .
        // C2
        address lp = IAgentToken(token).liquidityPools()[0];
-        IERC20(assetToken).transfer(token, initialAmount);
+        IERC20(application.tokenAddress).transfer(token, initialAmount);
        IAgentToken(token).addInitialLiquidity(address(this));

Updated _createNewAgentToken:

- function _createNewAgentToken(string memory name, string memory symbol) internal returns (address instance) {
+ function _createNewAgentToken(string memory name, string memory symbol, address memory applicationAssetToken) internal returns (address instance) {
        instance = Clones.clone(tokenImplementation);
        IAgentToken(instance).initialize(
-            [_tokenAdmin, _uniswapRouter, assetToken],
+            [_tokenAdmin, _uniswapRouter, applicationAssetToken],
            abi.encode(name, symbol),
            _tokenSupplyParams,
            _tokenTaxParams
        );

        allTradingTokens.push(instance);
        return instance;
    }

This would ensure that changing assetToken does not break any flow.


The original report as published on Code4rena, by YouCrossTheLineAlfie. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

Likely the application record does not store the asset token used at proposal time, so later operations rely on a mutable global address.

Impact

Withdrawals and application execution could revert or use an unintended token, and there is no recovery function for the old token balance, so funds could be stuck or misdirected.

What the attacker needed

Admin calls setAssetToken; existing applications funded with old token

The fix

The report recommends storing the asset token address inside each Application struct and using that stored address in withdraw, executeApplication and _createNewAgentToken.

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 keeps one shared 'asset token' address that it uses for payments. Applications only remember how much was paid, not which token. If an admin swaps that shared address, old applications point at the wrong token, so refunds and setup can fail or send the wrong currency.

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.

More by YouCrossTheLineAlfie

View all

10 more disclosed reports, best first.

More from Virtuals Protocol

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.