Skip to content
Business logic Audit finding Intermediate

_ownedTokensIndex is SHARED by different owners, as a result, _removeTokenFromAllTokensEnumeration might remove the wrong tokenId.

The contract's token index mapping was shared across all owners, so adding a token for one owner could overwrite another owner's index. Removing a token could then delete the wrong token from an owner's list.

chaduke Follow with adriro

Originally disclosed on Code4rena

Submitted by chaduke, also found by adriro

The data structure _ownedTokensIndex is SHARED by different owners, as a result, _removeTokenFromAllTokensEnumeration() might remove the wrong tokenId.

Proof of Concept

_ownedTokensIndex is used to map from token ID to index of the owner tokens list, unfortunately, all owners share the same data structure at the same time (non-fungible tokens). So, the mapping for one owner might be overwritten by another owner when _addTokenToOwnerEnumeration is called: https://github.com/code-423n4/2023-01-timeswap/blob/ef4c84fb8535aad8abd6b67cc45d994337ec4514/packages/v2-token/src/base/ERC1155Enumerable.sol#L116-L121. As a result, _removeTokenFromOwnerEnumeration() might remove the wrong tokenID.

Removing the wrong tokenID can happen like the following:

  1. Suppose Alice owns three tokens A, B, C with indices 1 -> A, 2->B, 3->C

  2. Suppose Bob owns token D, 1->D, and will add A to his list via _addTokenToOwnerEnumeration(). As a result, we have 1->D, and 2-A, since _ownedTokensIndex is shared, we have A->2 in _ownedTokensIndex.

  3. Next, _removeTokenFromOwnerEnumeration() is called to remove A from Alice. However, tokenIndex will be 2, which points to B, as a result, instead of deleting A, B is deleted from _ownedTokens. Wrong token delete!

    function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private { uint256 lastTokenIndex = _currentIndex[from] - 1; uint256 tokenIndex = _ownedTokensIndex[tokenId];

        if (tokenIndex != lastTokenIndex) {
            uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
    
            _ownedTokens[from][tokenIndex] = lastTokenId;
            _ownedTokensIndex[lastTokenId] = tokenIndex;
        }
    
        delete _ownedTokensIndex[tokenId];
        delete _ownedTokens[from][lastTokenIndex];
    }
    

Tools Used

Remix

Recommended Mitigation Steps

Redefine ownedTokensIndex so that is is not shared:

mapping(address => mapping(uint256 => uint256)) private _ownedTokensIndex;

vhawk19 (Timeswap) confirmed and commented:

Updated the ERC1155Enumerable.sol implementation, which should resolve these issues.


The original report as published on Code4rena, by chaduke. 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 `_ownedTokensIndex` mapping was not scoped per owner, so different owners' index entries overwrote each other.

Impact

Removing a token could delete the wrong token from an owner's list, corrupting ownership tracking.

What the attacker needed

Multiple owners hold tokens; token add and remove operations occur

The fix

The index mapping should be redefined to be per-owner, and the project confirmed it updated the implementation to resolve the issue.

How it was found

  1. Alice owns tokens A, B, C with indices 1, 2, 3.
  2. Bob adds token A to his list, overwriting the shared index so A maps to 2.
  3. Removing A from Alice uses index 2, which points to B, so B is deleted instead.

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 smart contract bug in a token tracker. The contract keeps a list of which tokens each person owns, but it used one shared index for everyone. When one person's list changed, it could mess up another person's list, so removing a token might delete the wrong one.

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 chaduke

View all

21 more disclosed reports, best first.

More from Timeswap contest

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.