Skip to content
Business logic Audit finding Beginner

The dispatch function of the VaultRouter , does not work as intended, with PULL_TOKEN action

The VaultRouter's dispatch function compares the full action value instead of the extracted action code for the PULL_TOKEN command, so supplying an input mapping causes a revert with InvalidCommand. An attacker cannot exploit this to steal funds; it only breaks intended functionality when an input mapping is provided.

pfapostol Follow with MrPotatoMagic

Originally disclosed on Code4rena

Submitted by pfapostol, also found by MrPotatoMagic

https://github.com/code-423n4/2024-12-bakerfi/blob/0daf8a0547b6245faed5b6cd3f5daf44d2ea7c9a/contracts/core/VaultRouter.sol#L99

Finding description and impact

The dispatch function of the VaultRouter contract handles the execution of actions from the MultiCommand.execute call. Actions are encoded as follows (Right-to-Left):

  1. Bits (0-32): The action.
  2. Bits (32-63): The input mapping.
  3. Bits (64-95): The output mapping.

While most commands are handled using the corresponding action stored in the actionToExecute variable:

        uint32 actionToExecute = uint32(action & Commands.THIRTY_TWO_BITS_MASK);

        // Extract input mapping from bits 32-63 by right shifting 32 bits and masking
        uint32 inputMapping = uint16((action >> 32) & Commands.THIRTY_TWO_BITS_MASK);

        // Extract output mapping from bits 64-95 by right shifting 64 bits and masking
        uint32 outputMapping = uint16(((action >> 64) & Commands.THIRTY_TWO_BITS_MASK));
...
        } else if (actionToExecute == Commands.PULL_TOKEN_FROM) {
            output = _handlePullTokenFrom(data, callStack, inputMapping);
        } else if (actionToExecute == Commands.PUSH_TOKEN) {
            output = _handlePushToken(data, callStack, inputMapping);
        } else if (actionToExecute == Commands.PUSH_TOKEN_FROM) {
            output = _handlePushTokenFrom(data, callStack, inputMapping);
        } else if (actionToExecute == Commands.SWEEP_TOKENS) {
...

There is one exception. Likely due to a typo, the action “tuple” is used instead of actionToExecute:

Impact

If an inputMapping is supplied with actionToExecute equal PULL_TOKEN, the execution will revert with InvalidCommand(uint256 action).

Proof of Concept

This PoC follows these steps:

  1. Deploy the VaultRouter.

  2. Transfer some WETH to the USER1 and approve the VaultRouter on the USER1’s behalf.

  3. Execute two commands:

    1. Demonstrate that the issue does not occur when inputMapping is not supplied.

      │   │   ├─ [29211] VaultRouter::dispatch(228, <unknown>, [0, 0, 0, 0, 0, 0, 0, 0])
      │   │   │   ├─ [28962] VaultRouter::_handlePullToken(<unknown>, [0, 0, 0, 0, 0, 0, 0, 0], 0)
      ...
      │   │   │   │   │   │   ├─ [26048] WETH::transferFrom(USER1: [0x856243F11eFbE357db89aeb2DC809768cC055b1B], VaultRouter: [0xF62849F9A0B5Bf2913b396098F7c7019b51A820a], 1000000000000000000 [1e18])
      ...
      │   │   │   └─ ← true, 0x
      
    2. Trigger the issue: when inputMapping is supplied, the execution reverts with:

      │   │   └─ ← [Revert] InvalidCommand(4294967298 [4.294e9])
      
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.24;

import {Test} from "lib/forge-std/src/Test.sol";
import {console} from "lib/forge-std/src/console.sol";
import {MockERC20} from "lib/forge-std/src/mocks/MockERC20.sol";

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {BakerFiProxy} from "contracts/proxy/BakerFiProxy.sol";
import {BakerFiProxyAdmin} from "contracts/proxy/BakerFiProxyAdmin.sol";
import {VaultRouter} from "contracts/core/VaultRouter.sol";
import {Command} from "contracts/core/MultiCommand.sol";
import {Commands} from "contracts/core/router/Commands.sol";

contract PoCs is Test {
    address immutable DEPLOYER = makeAddr("DEPLOYER");
    address immutable USER1 = makeAddr("USER1");

    address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address constant WstETH = 0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;

    function setUp() public {
        vm.label(WETH, "WETH");
        vm.label(WstETH, "WstETH");

        vm.createSelectFork("https://rpc.ankr.com/eth");
    }

    function test_dispatch_action_impossible() public {
        BakerFiProxyAdmin bakerFiProxyAdmin = new BakerFiProxyAdmin(DEPLOYER);
        BakerFiProxy bakerFiProxy = new BakerFiProxy(
            address(new VaultRouter()),
            address(bakerFiProxyAdmin),
            abi.encodeWithSelector(
                VaultRouter.initialize.selector,
                DEPLOYER,
                WETH
            )
        );
        VaultRouter vaultRouter = VaultRouter(payable(address(bakerFiProxy)));
        vm.label(address(vaultRouter), "VaultRouter");

        vm.startPrank(USER1);
        deal(WETH, USER1, 3 ether);
        vm.deal(USER1, 3 ether);
        IERC20(WETH).approve(address(vaultRouter), 3 ether);

        Command[] memory commands = new Command[](2);

        commands[0] = Command({
            action: Commands.PULL_TOKEN,
            data: abi.encode(WETH, 1 ether)
        });
        commands[1] = Command({
            action: ((uint256(uint8(1)) << 32) | uint256(Commands.PULL_TOKEN)),
            data: abi.encode(WETH, 1 ether)
        });

        vaultRouter.execute{value: 3 ether}(commands);
    }
}

Recommended mitigation steps

Use correct variable:

} else if (actionToExecute == Commands.PULL_TOKEN) {

chefkenji (BakerFi) confirmed

BakerFi mitigated:

PR-11

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


The original report as published on Code4rena, by pfapostol. 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 a typo: the code compares the full `action` value instead of the extracted `actionToExecute` when checking for `PULL_TOKEN`.

Impact

The PULL_TOKEN action cannot be used with an input mapping; execution reverts with InvalidCommand, so the intended functionality is broken but no funds are at risk.

What the attacker needed

None

The fix

The code was fixed by comparing `actionToExecute` instead of `action` for the PULL_TOKEN check.

How it was found

  1. Deploy the VaultRouter.
  2. Transfer WETH to USER1 and approve the VaultRouter.
  3. Execute a command with PULL_TOKEN and no input mapping (works).
  4. Execute a command with PULL_TOKEN and an input mapping (reverts).

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 router has a function that reads a packed number to decide which action to take. For one action, it accidentally checks the whole packed number instead of just the action part, so if extra settings are included the check fails and the transaction is rejected. This is a bug that stops a feature from working, not one that lets someone steal money.

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 pfapostol

View all

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

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.