Skip to content
Race condition Audit finding Advanced

addGlobalToken() localAdress could be overwritten

The addGlobalToken function allows multiple concurrent requests to create local tokens for the same global token on a destination chain. Because the final check uses the newly generated local address instead of the global token, a later request can overwrite the earlier local token mapping, potentially causing users to lose deposited tokens if they bridge during the window between the two confirmations.

bin2chen Follow with Koolex and Udsen

Originally disclosed on Code4rena

Submitted by bin2chen, also found by Koolex and Udsen

CoreBranchRouter.addGlobalToken() is used to set the local token of chains.

When CoreBranchRouter.addGlobalToken(_dstChainId = ftm), it will execute follow step:

  1. [root]CoreRootRouter.\_addGlobalToken()

    • 1.1 check isGlobalAddress(\_globalAddress)
    • 1.2 check not isGlobalToken(\_globalAddress, \_dstChainId)
  2. [branch]CoreBranchRouter.\_receiveAddGlobalToken()

    • 2.1 [remote:ftm] CoreBranchRouter.\_receiveAddGlobalToken()
    • 2.1.1 New Local Token address
  3. [root]CoreRootRouter.\_setLocalToken()

    • 3.1 check not isLocalToken (new Local token)
    • 3.2 rootPort.setLocalAddress (globalGlobal, new Local token, fmtChainId)

Call sequence [root]->[branch]->[root], with asynchronous calls via layerzero. Since it is asynchronous, in the case of concurrency, the check in step 1.2 is invalid because step 3.2 is executed after a certain amount of time.

Consider the following scenarios:

  1. Alice executes addGlobalToken(ftm) through Steps 1 and 2, and generates alice_LocalTokenAddress = 0x01.
  2. Bob executes addGlobalToken(ftm) through Steps 1 and 1, and generates bob_LocalTokenAddress = 0x02 at the same time.
  3. After a while, layerzero executes Alice’s request and will pass step 3.1, because alice_LocalTokenAddress is new.
  4. After a while, layerzero executes Bob’s request and will pass step 3.1, because bob_LocalTokenAddress is new.

So bob_LocalTokenAddress will override alice_LocalTokenAddress.

The main problem here, is that the check in step 3.1 is wrong because the local token is a regenerated address, so isLocalToken() is always false. It should be checking isGlobalToken(_globalAddress, _dstChainId)).

    function _setLocalToken(address _globalAddress, address _localAddress, uint16 _dstChainId) internal {
        // Verify if the token already added
@>      if (IPort(rootPortAddress).isLocalToken(_localAddress, _dstChainId)) revert TokenAlreadyAdded();

        // Set the global token's new branch chain address
        IPort(rootPortAddress).setLocalAddress(_globalAddress, _localAddress, _dstChainId);
    }

Impact

In the case of concurrency, the second local token will overwrite the first local token. Before overwriting, the first local token is still valid. If someone exchanges the first local token and waits until after overwriting, then the first local token will be invalidated and the user will lose the corresponding token.

Proof of Concept

The following code demonstrates that with layerzero asynchronous and due to _setLocalToken() error checking, it may cause the second localToken to overwrite the first localToken; the layerzero call is simplified.

Add to CoreRootBridgeAgentTest.t.sol

    function testAddGlobalTokenOverride() public {
        //1. new global token
        GasParams memory gasParams = GasParams(0, 0);
        arbitrumCoreRouter.addLocalToken(address(arbAssetToken), gasParams);
        newGlobalAddress = RootPort(rootPort).getLocalTokenFromUnderlying(address(arbAssetToken), rootChainId);
        console2.log("New Global Token Address: ", newGlobalAddress);

        //2.1 new first local token
        gasParams = GasParams(1 ether, 1 ether);
        GasParams[] memory remoteGas = new GasParams[](2);
        remoteGas[0] = GasParams(0.0001 ether, 0.00005 ether);
        remoteGas[1] = GasParams(0.0001 ether, 0.00005 ether);
        bytes memory data = abi.encode(ftmCoreBridgeAgentAddress, newGlobalAddress, ftmChainId,remoteGas);
        bytes memory packedData = abi.encodePacked(bytes1(0x01), data);
        encodeCallNoDeposit(
            payable(ftmCoreBridgeAgentAddress),
            payable(address(coreBridgeAgent)),
            chainNonce[ftmChainId]++,
            packedData,
            gasParams,
            ftmChainId
        );
        //2.2 new second local token

        data = abi.encode(ftmCoreBridgeAgentAddress, newGlobalAddress, ftmChainId,remoteGas);
        packedData = abi.encodePacked(bytes1(0x01), data);
        encodeCallNoDeposit(
            payable(ftmCoreBridgeAgentAddress),
            payable(address(coreBridgeAgent)),
            chainNonce[ftmChainId]++,
            packedData,
            gasParams,
            ftmChainId
        );
        //3.1 wait some time ,lz execute first local token
        address newLocalToken = address(0xFA111111);
        data = abi.encode(newGlobalAddress, newLocalToken, "UnderLocal Coin", "UL");
        packedData = abi.encodePacked(bytes1(0x03), data);
        encodeSystemCall(
            payable(ftmCoreBridgeAgentAddress),
            payable(address(coreBridgeAgent)),
            chainNonce[ftmChainId]++,
            packedData,
            gasParams,
            ftmChainId
        );
        //3.1.1 show first local token address
        address ftmLocalToken = RootPort(rootPort).getLocalTokenFromGlobal(newGlobalAddress,ftmChainId);
        console2.log("loal Token Address(first): ", ftmLocalToken);

        //3.2 wait some time ,lz execute second local token
        gasParams = GasParams(0.0001 ether, 0.00005 ether);
        newLocalToken = address(0xFA222222);
        data = abi.encode(newGlobalAddress, newLocalToken, "UnderLocal Coin", "UL");
        packedData = abi.encodePacked(bytes1(0x03), data);
        encodeSystemCall(
            payable(ftmCoreBridgeAgentAddress),
            payable(address(coreBridgeAgent)),
            chainNonce[ftmChainId]++,
            packedData,
            gasParams,
            ftmChainId
        );
        //3.2.1 show second local token address
        ftmLocalToken = RootPort(rootPort).getLocalTokenFromGlobal(newGlobalAddress,ftmChainId);
        console2.log("loal Token Address(override): ", ftmLocalToken);
    }
$ forge test -vvv --match-contract CoreRootBridgeAgentTest  --match-test testAddGlobalTokenOverride

[PASS] testAddGlobalTokenOverride() (gas: 1846078)
Logs:
  New Global Token Address:  0x12Cd3f6571aF11e01137d294866C5fFd1369Bbe9
  loal Token Address(first):  0x00000000000000000000000000000000Fa111111
  loal Token Address(override):  0x00000000000000000000000000000000fA222222

Recommended Mitigation

Check isGlobalToken() should replace check isLocalToken().

    function _setLocalToken(address _globalAddress, address _localAddress, uint16 _dstChainId) internal {
        // Verify if the token already added
-      if (IPort(rootPortAddress).isLocalToken(_localAddress, _dstChainId)) revert TokenAlreadyAdded();
+       if (IPort(rootPortAddress).isGlobalToken(_globalAddress, _dstChainId)) {
+            revert TokenAlreadyAdded();
+       }
        // Set the global token's new branch chain address
        IPort(rootPortAddress).setLocalAddress(_globalAddress, _localAddress, _dstChainId);
    }

Assessed type

Context

0xBugsy (Maia) disputed and commented via duplicate issue #822:

This has already been checked in the _addGlobalToken step of this execution flow.

alcueca (judge) commented via duplicate issue #822:

Many issues around addGlobalToken due to lack of input validation when linking a global token to local token - #860.

Would you please point out where? I can’t find where the _localAddress is obtained.

0xBugsy (Maia) commented via duplicate issue #822:

After calling addGlobalToken - requesting a new local token representation in a new chain for an existing global token - we perform these steps:

  1. https://github.com/code-423n4/2023-09-maia/blob/f5ba4de628836b2a29f9b5fff59499690008c463/src/CoreRootRouter.sol#L406 (where we check if the global token exists and if it has already been added to the destination chain).
  2. https://github.com/code-423n4/2023-09-maia/blob/f5ba4de628836b2a29f9b5fff59499690008c463/src/CoreBranchRouter.sol#L166 (creation of the new branch hToken for the existing root/global hToken) _localAddress is deployed in this step.
  3. https://github.com/code-423n4/2023-09-maia/blob/f5ba4de628836b2a29f9b5fff59499690008c463/src/CoreRootRouter.sol#L481 (check if we have not added the new local token t.o system already and request update to root port)
  4. Ends here: https://github.com/code-423n4/2023-09-maia/blob/f5ba4de628836b2a29f9b5fff59499690008c463/src/RootPort.sol#L259 (updating the root port token mapping namely getLocalTokenFromGlobal).

alcueca (judge) commented via duplicate issue #822:

As a rule, you should validate the arguments in internal functions, that when those arguments change state in the same function, some values of those arguments could break the logic represented by that state.

alcueca (judge) decreased severity to Medium and commented:

From the sponsor:

[What the submission] is saying has no impact on users in any way, since there is no way to acquire the asset on the origin branch before token addition execution is confirmed on root; it would simply revert and the user would be able to redeem the deposited assets. In addition, the issue seems to revolve around the misuse of this function IPort(rootPortAddress).isLocalToken(_localAddress, _dstChainId) in _setLocalToken - but this is intended so as to prevent overwriting this mapping after it already being added.

Furthermore, the suggested funciton IPort(rootPortAddress).isGlobalToken(_globalAddress, _dstChainId) is always updated in tandem with the one currently being used, so it’s the same thing as doing the checks we currently have in place.

bin2chen (warden) commented:

@alcueca -

since there is no way to acquire the asset on the origin branch before token addition execution is confirmed on root

Is it possible to look again? Both will be confirmed at root. Please see poc’s ”3.1.1 show first local token address”.

The address has been printed out from root:

Logs:
  New Global Token Address:  0x12Cd3f6571aF11e01137d294866C5fFd1369Bbe9
  loal Token Address(first):  0x00000000000000000000000000000000Fa111111
  loal Token Address(override):  0x00000000000000000000000000000000fA222222

The problem describes that both addGlobalToken()all work. The second addGlobalToken() overrides the first.

Since these operations are asynchronous (cross-chain), the first step check can be skipped. Because the third step incorrectly uses isLocalToken() to check for legitimacy, the override becomes possible.

alcueca (judge) commented:

I explained the issue more clearly to the sponsor, and is now confirmed:

Really apologize for my misunderstanding, I was not interpreting the issue correctly, seems that during the time between _addGlobalToken and _setLocalToken, multiple tokens can be requested; although, this is to be expected, only the last one created from these requests will be retained in the system instead of the first one.

Due to this, during the time period between the first _setLocalToken execution and the last, the submission of bridgeOut requests would lead to the loss of the deposited tokens. Although, this is a very specific time frame to be possible (the 2 layer zero hops in question), it is a very important situation to be addressed as someone could loose access to their tokens under these circumstances.

0xBugsy (Maia) commented:

Addressed here.

Note: For full discussion, see here.


The original report as published on Code4rena, by bin2chen. 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 final check in _setLocalToken uses isLocalToken on the newly generated local address, which is always false, instead of checking whether the global token already has a local token on that chain.

Impact

During the time between the first and last _setLocalToken execution, a user who bridges out using the first local token could lose their deposited tokens because the token becomes invalid after being overwritten.

What the attacker needed

Two concurrent addGlobalToken requests for the same global…; a user bridges out using the first local token during the…

The fix

The check in _setLocalToken should use isGlobalToken on the global address and destination chain instead of isLocalToken on the local address; the sponsor addressed this in a remediation commit.

How it was found

  1. Alice calls addGlobalToken for a destination chain, which after cross-chain messages creates a new local token address.
  2. Bob calls addGlobalToken for the same destination chain at nearly the same time, creating a different local token address.
  3. When Alice's request is finally processed on the root chain, the check passes because her local token address is new.
  4. When Bob's request is processed later, the check also passes because his local token address is new, and his address overwrites Alice's.

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.

This is a bug in a cross-chain token system. When you ask to add a token on another chain, the system creates a new local token address and later records it. Because the recording step checks whether that new address already exists (which it never does), two simultaneous requests can both succeed, and the second one replaces the first. If someone uses the first token before it is replaced, their tokens become invalid and they lose them.

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 bin2chen

View all

139 more disclosed reports, best first.

More from Maia DAO - Ulysses

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.