Skip to content
Smart contracts Audit finding Advanced

BathToken.sol#_deposit() attacker can mint more shares with re-entrancy from hookable tokens

The deposit function in BathToken.sol calculates the deposited amount by comparing the contract's token balance before and after a transferFrom call, but lacks a reentrancy guard. An attacker using a hookable token like ERC777 could reenter the deposit function during the transfer callback and mint more shares than the actual deposit.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

BathToken.sol#_deposit() calculates the actual transferred amount by comparing the before and after balance, however, since there is no reentrancy guard on this function, there is a risk of re-entrancy attack to mint more shares.

Some token standards, such as ERC777, allow a callback to the source of the funds (the from address) before the balances are updated in transferFrom(). This callback could be used to re-enter the function and inflate the amount.

https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathToken.sol#L557-L568

function _deposit(uint256 assets, address receiver)
    internal
    returns (uint256 shares)
{
    uint256 _pool = underlyingBalance();
    uint256 _before = underlyingToken.balanceOf(address(this));

    // **Assume caller is depositor**
    underlyingToken.transferFrom(msg.sender, address(this), assets);
    uint256 _after = underlyingToken.balanceOf(address(this));
    assets = _after.sub(_before); // Additional check for deflationary tokens
    ...

Proof of Concept

With a ERC777 token by using the ERC777TokensSender tokensToSend hook to re-enter the deposit() function.

Given:

  • underlyingBalance(): 100_000e18 XYZ.
  • totalSupply: 1e18

The attacker can create a contract with tokensToSend() function, then:

  1. deposit(1)
-   preBalance  = `100_000e18`;
-   `underlyingToken.transferFrom(msg.sender, address(this), 1)`
  1. reenter using tokensToSend hook for the 2nd call: deposit(1_000e18)

    • preBalance = 100_000e18;
    • underlyingToken.transferFrom(msg.sender, address(this), 1_000e18)
    • postBalance = 101_000e18;
    • assets (actualDepositAmount) = 101_000e18 - 100_000e18 = 1_000e18;
    • mint 1000 shares;
  2. continue with the first deposit() call:

    • underlyingToken.transferFrom(msg.sender, address(this), 1)
    • postBalance = 101_000e18 + 1;
    • assets (actualDepositAmount) = (101_000e18 + 1) - 100_000e18 = 1_000e18 + 1;
    • mint 1000 shares;

As a result, with only 1 + 1_000e18 transferred to the contract, the attacker minted 2_000e18 XYZ worth of shares.

Recommendation

Consider adding nonReentrant modifier from OZ’s ReentrancyGuard.

bghughes (Rubicon) marked as duplicate and commented:

Duplicate of #283 #410. Note that no ERC777 tokens will be created and this will be patched, making it a non-issue in practice.

HickupHH3 (judge) commented:

Not sure what is meant by “no ERC777 tokens will be created”, since it’s transferring the underlying token which is an arbitrary ERC20, and by extension, ERC777.

The best practice is to break the CEI pattern for deposits and perform the interaction first. Or simply add reentrancy guards.

bghughes (Rubicon) confirmed


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

Impact

An attacker could mint more shares than the actual deposit, diluting other depositors, but the program owner noted that no ERC777 tokens will be created and this will be patched, making it a non-issue in practice.

What the attacker needed

Underlying token is ERC777 or hookable; attacker can deploy a contract

The fix

The recommendation is to add a nonReentrant modifier from OpenZeppelin's ReentrancyGuard, or follow the checks-effects-interactions pattern by performing the interaction first.

How it was found

  1. Attacker deploys a contract with a tokensToSend hook.
  2. Attacker calls deposit(1) on the BathToken contract.
  3. During the transferFrom, the tokensToSend hook reenters and calls deposit(1_000e18).
  4. The reentrant call completes, minting shares based on the inflated balance difference.
  5. The original deposit call resumes and also mints shares based on the now-larger balance difference.
  6. Attacker ends up with more shares than the total tokens actually transferred.

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 deposit tokens into a contract, it checks how many tokens arrived by looking at its balance before and after the transfer. If the token allows the sender to run code during the transfer (like ERC777 does), an attacker can call the deposit function again before the first deposit finishes. This lets them trick the contract into thinking they deposited more than they actually did, so they get extra shares.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Rubicon v2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.