Skip to content
Business logic Audit finding Intermediate

twTAP.sol : Reward tokens stored in index 0 can be stolen

The twTAP contract's reward claiming function uses a user-supplied list of reward token addresses to look up indices, but unknown addresses default to index 0, causing the token at index 0 to be transferred multiple times. An attacker could steal an unfair amount of the reward token stored at index 0, potentially making the contract insolvent.

carrotsmuggler Follow with KIntern_NA

Originally disclosed on Code4rena

Submitted by carrotsmuggler, also found by KIntern_NA

The function claimAndSendRewards can be called to collect rewards accrued by the twTAP position. This function can only be called by the TapOFT.sol contract during a crosschain operation. Thus a user on chain A can call claimRewards and on chain B, the function _claimRewards will be called and a bunch of parameters will be passed in that message.

(
,
,
address to,
uint256 tokenID,
IERC20[] memory rewardTokens,
IRewardClaimSendFromParams[] memory rewardClaimSendParams
) = abi.decode(
    _payload,
    (
        uint16,
        address,
        address,
        uint256,
        IERC20[],
        IRewardClaimSendFromParams[]
    )
);

All these parameters passed here comes from the original lz payload sent by the user from chain A. Of note is the array rewardTokens which is a user inputted value. This function then calls the twtap.sol contract as shown below.

try twTap.claimAndSendRewards(tokenID, rewardTokens) {

In the twTAP contract, the function claimAndSendRewards eventually calls _claimRewardsOn, the functionality of which is shown below.

function _claimRewardsOn(
    uint256 _tokenId,
    address _to,
    IERC20[] memory _rewardTokens
) internal {
    uint256[] memory amounts = claimable(_tokenId);
    unchecked {
        uint256 len = _rewardTokens.length;
        for (uint256 i = 0; i < len; ) {
            uint256 claimableIndex = rewardTokenIndex[_rewardTokens[i]];
            uint256 amount = amounts[i];

            if (amount > 0) {
                // Math is safe: `amount` calculated safely in `claimable()`
                claimed[_tokenId][claimableIndex] += amount;
                rewardTokens[claimableIndex].safeTransfer(_to, amount);
            }
            ++i;
        }
    }
}

Here we want to investigate a case where a user sends some random address in the array rewardTokens. We already showed that this value is set by the user, and the above quoted snippet receives the same value in the _rewardTokens variable.

In the for loop, the indexes are found. But if rewardTokens[i] is a garbage address, the mapping rewardTokenIndex will return the default value of 0 which will be stored in claimableIndex. The array amounts stores the amounts of the different tokens that can be claimed by the user. But since claimableIndex is now 0, the safeTransfer function in the end is always called on the token rewardTokens[0]. Thus a user can withdraw the rewardtoken in index 0 multiple times and in amounts based on the values stored in the amounts array.

Thus we have shown that a user can steal an unfair amount of tokens stored in the 0 index of the rewardTokens array variable in the twTAP.sol contract. This will mess up the reward distribution for all users, and can lead to an insolvent contract. Thus this is deemed a high severity issue.

Proof of Concept

The attack can be done in the following steps:

  1. Attacker calls claimRewards on chain A. The attacker is assumed to have a valid position on chain B with pending rewards.
  2. The attacker passes an array of garbage addresses in the rewardTokens parameter.
  3. The contract sends forth the message to the destination chain, where the twTAP contract is called to collect the rewards.
  4. As shown above, the reward token stored in index 0 is sent multiple times to the caller, which is the TapOFT contract.
  5. In the TapOFT contract, the contract then sends all the collected rewards in the contract cross chain back to the attacker. Thus the tokens in index 0 were claimed and collected.

Thus the attacker can claim an unfair number of tokens present in the 0th index. If this token is more valuable than the other rewward tokens, the attacker can profit from this exploit.

Recommended Mitigation Steps

Mitigation can be done by not using the 0th index. The zero index of the rewardTokens array in twTAP.sol, if left empty, will point to the zero address, and if an unknown address is encountered, the contract will try to claim the tokens from the zero address which will revert.

This can be enforced by using the statement rewardTokens.push(address(0)) in the constructor. However changes will need to be made on other operations in the contract which loops over this array to skip operations on this zero address now present in the array.

0xRektora (Tapioca) confirmed via duplicate issue 1093


The original report as published on Code4rena, by carrotsmuggler. 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 a mapping that returns 0 for unknown token addresses without validating that the address is a known reward token, so the index 0 token is used for any garbage address.

Impact

An attacker could steal an unfair amount of the reward token at index 0, potentially making the contract insolvent and disrupting reward distribution for all users.

What the attacker needed

A valid twTAP position with pending rewards; ability to send cross-chain messages

The fix

The zero index of the rewardTokens array should be left empty (e.g., by pushing address(0) in the constructor) so that unknown addresses cause a revert, and other loops should skip this zero address.

How it was found

  1. Attacker calls claimRewards on chain A, assuming a valid position on chain B with pending rewards.
  2. Attacker passes an array of garbage addresses in the rewardTokens parameter.
  3. The contract sends a cross-chain message to the destination chain where twTAP is called.
  4. The reward token at index 0 is sent multiple times to the TapOFT contract.
  5. TapOFT sends the collected rewards cross-chain back to the attacker.

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 contract lets users claim rewards by sending a list of token addresses. If a user sends a fake address, the contract mistakenly thinks it means the first token in its list and pays out that token again and again. This lets an attacker take more rewards than they should, draining the contract.

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 carrotsmuggler

View all

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

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.