Skip to content
Business logic Audit finding Intermediate

Missing prevAgentId update in promptMulti() function may cause token loss by transferring to address(0)

The promptMulti() function fails to update prevAgentId inside the loop, so the cached agentTba address can be uninitialised or stale. This can cause tokens to be sent to address(0) or to the wrong agent.

Vemus Follow with 4ny0n3, bareli and 8 others

Originally disclosed on Code4rena

Submitted by Vemus, also found by 4ny0n3, bareli, djshan_eden, dustykid, harsh123, Heyu, ks__xxxxx, odessos42, Raihan, and sergei2340

https://github.com/code-423n4/2025-04-virtuals-protocol/blob/main/contracts/AgentInference.sol#L80-L87

Finding description

The promptMulti() function attempts to optimize token transfers by caching agentTba when the agentId remains unchanged. However, it fails to update prevAgentId inside the loop, which causes agentTba to remain outdated or uninitialized.

As a result, if the first agentId equals the default prevAgentId (0), the contract never sets agentTba before the first transfer, causing a safeTransferFrom(sender, address(0), amount), losing the user’s tokens.

Additionally, when the same agentId reoccurs after a different one, agentTba may point to the wrong address, resulting in unexpected transfers.

Recommended mitigation steps

Consider implementing the following version of the code:

// Initialize prevAgentId to a value that no valid agentId will ever match,
// ensuring the first iteration always enters the if-block.
uint256 prevAgentId = type(uint256).max;

// Initialize agentTba to a placeholder; it will be set properly on the first iteration.
address agentTba = address(0);

for (uint256 i = 0; i < len; i++) {
    uint256 agentId = agentIds[I];
    require(amounts[i] > 0, "Amount must be > 0"); // Validate if amounts = 0

    // If the agentId changes, fetch the corresponding agent TBA (target address)
    if (prevAgentId != agentId) {
        agentTba = agentNft.virtualInfo(agentId).tba;
        require(agentTba != address(0), "Invalid agent TBA");  // Ensure the target address is valid
        prevAgentId = agentId;  // Update the cache to reflect the current agentId
    }

    token.safeTransferFrom(sender, agentTba, amounts[i]);

    inferenceCount[agentId]++;
    emit Prompt(sender, promptHashes[i], agentId, amounts[i], coreIds[i]);
}

Proof of Concept

Assume the following scenario. The loop processes the following values, controlled by user:

agentIds = [0, 1, 0]
amounts  = [10, 20, 30]

Initialization:

prevAgentId = 0
agentTba = address(0)

Iteration 0:

agentId = 0
  1. Check: if (prevAgentId != agentId) → if (0 != 0) → FALSE
  2. No update to agentTba (remains address(0))
  3. Transfer:
token.safeTransferFrom(sender, address(0), 10)

→ User lost 10 tokens unintentionally.

Iteration 1:

agentId = 1
  1. Check: if (prevAgentId != agentId) → if (0 != 1) → TRUE
  2. agentTba is updated: agentTba = agentNft.virtualInfo(1).tba

→ BUT: prevAgentId is not updated, so it still holds 0

  1. Transfer:
token.safeTransferFrom(sender, agent1Tba, 20)

→ So that’s correct.

Iteration 2:

agentId = 0
  1. Check: if (prevAgentId != agentId) → if (0 != 0) → FALSE
  2. No update to agentTba, which still holds the address for agent 1.
  3. Transfer:
token.safeTransferFrom(sender, agent1Tba, 30)

→ Tokens are sent to the wrong agent (agent 1 instead of agent 0).

Root Cause

The condition to fetch a new agentTba relies on prevAgentId being updated after each successful fetch.

  • Because prevAgentId is never updated inside the loop, the caching logic becomes broken:

    • The first transfer may lose tokens (if agentId == 0 and no update is triggered),
    • Subsequent transfers may send tokens to incorrect agents if agentId switches back and forth.

Virtuals marked as informative


The original report as published on Code4rena, by Vemus. 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 caching logic relies on prevAgentId being updated after each fetch, but the code never updates it inside the loop.

Impact

Users could lose tokens sent to address(0) or have tokens transferred to the wrong agent.

What the attacker needed

User calls promptMulti() with crafted agentIds

The fix

The code should be fixed by initialising prevAgentId to a value no valid agentId can match and updating prevAgentId whenever agentTba is refreshed.

How it was found

  1. Call promptMulti() with agentIds = [0, 1, 0] and amounts = [10, 20, 30].
  2. First iteration: agentId 0 equals prevAgentId 0, so agentTba stays address(0) and tokens are sent to address(0).
  3. Second iteration: agentId 1 differs, so agentTba is updated to agent 1's address, but prevAgentId remains 0.
  4. Third iteration: agentId 0 equals prevAgentId 0, so agentTba is not updated and tokens go to agent 1's address instead of agent 0's.

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.

This is a smart contract bug where a function tries to save time by remembering the last agent's address. Because it forgets to update that memory, it can send tokens to the zero address (burning them) or to the wrong agent.

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 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.