Skip to content
Access control Audit finding Intermediate

A user with a TapiocaOFT allowance >0 could steal all the underlying ERC20 tokens of the owner

The TapiocaOFT contract's _wrap function checks the wrong allowance, allowing a spender with a small allowance to repeatedly wrap the owner's tokens without reducing that allowance. An attacker could steal all underlying ERC20 tokens from an owner who has approved the TapiocaOFT contract.

dirk_y Follow with bin2chen, carrotsmuggler and 2 others

Originally disclosed on Code4rena

Submitted by dirk_y, also found by bin2chen, carrotsmuggler, 0x73696d616f, and chaduke

The TapiocaOFT.sol contract allows users to wrap ERC20 tokens into an OFTV2 type contract to allow for seamless cross-chain use.

As with most ERC20 tokens, owners of tokens have the ability to give an allowance to another address to spend their tokens. This allowance should be decremented every time a user spends the owner’s tokens. However the TapiocaOFT.sol _wrap method contains a bug that allows a user with a non-zero allowance to keep using the same allowance to spend the owner’s tokens.

For example, if an owner had 100 tokens and gave an allowance of 10 to a spender, that spender would be able to spend all 100 tokens in 10 transactions.

Proof of Concept

When a user wants to wrap a non-native ERC20 token into a TapiocaOFT they call wrap which calls _wrap under the hood:

    function _wrap(
        address _fromAddress,
        address _toAddress,
        uint256 _amount
    ) internal virtual {
        if (_fromAddress != msg.sender) {
            require(
                allowance(_fromAddress, msg.sender) >= _amount,
                "TOFT_allowed"
            );
        }
        IERC20(erc20).safeTransferFrom(_fromAddress, address(this), _amount);
        _mint(_toAddress, _amount);
    }

If the sender isn’t the owner of the ERC20 tokens being wrapped, the allowance of the user is checked. However this isn’t checking the underlying ERC20 allowance, but the allowance of the current contract (the TapiocaOFT).

Next, the underlying ERC20 token is transferred from the owner to this address. This decrements the allowance of the sender, however the sender isn’t the original message sender, but this contract.

In order to use this contract as an owner (Alice) I would have to approve the TapiocaOFT contract to spend my ERC20 tokens, and it is common to approve this contract to spend all my tokens if I trust the contract. Now let’s say I approved another user (Bob) to spend some (let’s say 5) of my TapiocaOFT tokens. Bob can now call wrap(aliceAddress, bobAddress, 5) as many times as he wants to steal all of Alice’s tokens.

Recommended Mitigation Steps

In my opinion you shouldn’t be able to wrap another user’s ERC20 tokens into a different token, because this is a different action to spending. Also, there is no way to decrement the allowance of the user (of the TapiocaOFT token) in the same call as we aren’t actually transferring any tokens; there is no function selector in the ERC20 spec to decrease an allowance from another contract.

Therefore I would suggest the following change:

diff --git a/contracts/tOFT/BaseTOFT.sol b/contracts/tOFT/BaseTOFT.sol
index 5658a0a..e8b7f63 100644
--- a/contracts/tOFT/BaseTOFT.sol
+++ b/contracts/tOFT/BaseTOFT.sol
@@ -350,12 +350,7 @@ contract BaseTOFT is BaseTOFTStorage, ERC20Permit {
         address _toAddress,
         uint256 _amount
     ) internal virtual {
-        if (_fromAddress != msg.sender) {
-            require(
-                allowance(_fromAddress, msg.sender) >= _amount,
-                "TOFT_allowed"
-            );
-        }
+        require (_fromAddress == msg.sender, "TOFT_allowed");
         IERC20(erc20).safeTransferFrom(_fromAddress, address(this), _amount);
         _mint(_toAddress, _amount);
     }

0xRektora (Tapioca) confirmed


The original report as published on Code4rena, by dirk_y. 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

The _wrap function checks the TapiocaOFT contract's allowance instead of the underlying ERC20 allowance, and the subsequent transferFrom reduces the contract's allowance, not the spender's.

Impact

An attacker with a small allowance could steal all of the owner's underlying ERC20 tokens.

What the attacker needed

Owner approves TapiocaOFT to spend ERC20; owner gives attacker a TapiocaOFT allowance

The fix

The _wrap function should require that the caller is the owner of the tokens being wrapped.

How it was found

  1. Owner approves the TapiocaOFT contract to spend their ERC20 tokens.
  2. Owner gives a spender an allowance of TapiocaOFT tokens.
  3. Spender calls wrap with the owner's address as _fromAddress and their own address as _toAddress.
  4. The contract checks the spender's allowance and transfers the owner's ERC20 tokens, but the spender's allowance is not reduced.
  5. Spender repeats the wrap call to drain all of the owner's tokens.

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 you approve someone to spend your tokens, that approval should decrease as they spend. Here, the contract checked the wrong approval, so a spender could keep using the same small approval over and over to take all your tokens.

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 dirk_y

View all

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