Skip to content
Race condition Audit finding Intermediate

The implementation of pullTokensWithPermit poses a risk, allowing malicious actors to steal tokens

The router's pullTokensWithPermit function does not validate the caller, allowing an attacker to frontrun a user's permit signature and transfer the user's tokens to the router, then sweep them to the attacker. The user's original transaction fails because the permit is already used, resulting in token loss.

shaflow2 Follow with 0xlemon and MrPotatoMagic

Originally disclosed on Code4rena

Submitted by shaflow2, also found by 0xlemon and MrPotatoMagic

https://github.com/code-423n4/2024-12-bakerfi/blob/3873b82ae8b321473f3afaf08727e97be0635be9/contracts/core/hooks/UsePermitTransfers.sol#L31

Finding description and impact

In batch operations interacting with the router, users are allowed to input tokens into the router using the permit method. This approach may be vulnerable to frontrunning attacks, allowing malicious actors to steal the user’s tokens.

Proof of Concept

    function pullTokensWithPermit(
        IERC20Permit token,
        uint256 amount,
        address owner,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal virtual {
        // Permit the VaultRouter to spend tokens on behalf of the owner
        IERC20Permit(token).permit(owner, address(this), amount, deadline, v, r, s);

        // Transfer the tokens from the owner to this contract
        IERC20(address(token)).safeTransferFrom(owner, address(this), amount);
    }

Users can deposit tokens into the router via the pullTokensWithPermit function. However, the router contract does not validate the caller’s information, making it possible for a malicious actor to frontrun the user and exploit their permit signature to steal tokens.

Consider the following scenario:

  1. The user interacts with the router contract:

    • Step 1: Calls pullTokensWithPermit to transfer 1000 tokens to the router.
    • Step 2: Deposits the tokens into a designated vault.
  2. A malicious actor observes the user’s transaction in the mempool and constructs a malicious transaction to steal the user’s tokens:

    • Step 1: The attacker calls pullTokensWithPermit using the user’s permit signature, causing the user to transfer 1000 tokens to the router.
    • Step 2: The attacker immediately calls sweepTokens to transfer the tokens to their own account. When the user’s original transaction is executed:
  3. The permit signature has already been used, causing the user’s transaction to fail. As a result, the user loses 1000 tokens.

Additionally, an attacker could frontrun the permit function without using the router and then call pushTokenFrom directly to steal tokens.

Recommended mitigation

The current router is not suitable for integrating permit to handle token input.

chefkenji (BakerFi) confirmed

BakerFi mitigated:

PR-23

Status: Mitigation confirmed. Full details in reports from 0xlemon and shaflow2.


The original report as published on Code4rena, by shaflow2. 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 router's pullTokensWithPermit function does not verify that the caller is the owner or otherwise authorised, so anyone can use a valid permit signature to pull tokens.

Impact

An attacker could steal the user's tokens by frontrunning the permit, and the user's transaction would fail, causing loss of funds.

What the attacker needed

User signs a permit for the router; attacker can see the transaction in mempool

The fix

The router should not integrate permit in this way; the report states the current router is not suitable for integrating permit to handle token input, and the project mitigated it via a pull request.

How it was found

  1. User calls pullTokensWithPermit to transfer 1000 tokens to the router.
  2. User deposits the tokens into a designated vault.
  3. Attacker observes the user's transaction in the mempool.
  4. Attacker calls pullTokensWithPermit using the user's permit signature, causing the user to transfer 1000 tokens to the router.
  5. Attacker immediately calls sweepTokens to transfer the tokens to their own account.
  6. User's original transaction fails because the permit signature has already been used.

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 permit is a signed message that lets someone else move your tokens without you sending a transaction. Here, the router accepted any permit and did not check who was calling, so an attacker could copy the user's signed permit, use it first to move the tokens to the router, and then take them. The user's own transaction then failed because the permit was already used.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by shaflow2

View all

12 more disclosed reports, best first.

More from BakerFi Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.