Skip to content
Smart contracts Audit finding Intermediate

REENTRANCY ATTACK POSSIBLE IF THE _feeTo IS A MALICIOUS CONTRACT IN FeeWrapper._chargeFeePayable() FUNCTION

The FeeWrapper contract sends ETH fees to an arbitrary _feeTo address via a low-level call before updating state, allowing a malicious _feeTo contract to re-enter and repeatedly extract fees. An attacker could drain ETH from the contract if it holds sufficient balance.

Udsen Follow with 0xkazim, pipoca and 2 others

Originally disclosed on Code4rena

Submitted by Udsen, also found by 0xkazim, pipoca, 0xNineDec and 0xnacho.

Reentrancy attack could happen if _feeTo is a malicious contract and has recieve() function implemented in _feeTo contract. FeeWrapper._chargeFeePayable() function is used to send the _feeAmount of ETH to the _feeTo address.

Consider that FeeWrapper._chargeFeePayable() is called by the FeeWrapper._rubicallPayable() function with the necessary _feeParams input parameter, and the following scenario can occur:

  1. The call to (bool OK, ) = payable(_feeTo).call{value: _feeAmount}("") is made. Lets assume the _totalAmount = 1ETH and _feeAmount = 0.1ETH initially.
  2. _feeTo contract has the recieve() function implemented in it.
  3. It reenters the FeeWrapper contract by calling the rubicallPayable() function by sending 0.01ETH as msg.value.
  4. It sets the _feeParams.totalAmount = 0.01ETH and _feeParams.feeAmount = 0.1ETH in the call to rubicallPayable().
  5. FeeWrapper.chargeFeePayable() will be called again by the rubicallPayable() function.
  6. (bool OK, ) = payable(_feeTo).call{value: _feeAmount}(""); will be called and Tx will pass since there is enough ETH in the contract from the previous deposit.
  7. Again _feeParams.feeAmount = 0.1ETH will be transferred to the _feeTo address.
  8. This can be continued till there is enough ETH in the contract.
  9. This happens as there is no check to make sure msg.value > _feeAmount before the low level call to the _feeTo address.

Proof of Concept

function _chargeFeePayable(
    FeeParams memory _feeParams
) internal returns (uint256 _msgValue) {
    // _feeToken is ETH
    uint256 _totalAmount = _feeParams.totalAmount;
    uint256 _feeAmount = _feeParams.feeAmount;
    address _feeTo = _feeParams.feeTo;
    require(msg.value == _totalAmount, "FeeWrapper: not enough ETH sent");

    // transfer fee to the 3rd party protocol
    (bool OK, ) = payable(_feeTo).call{value: _feeAmount}("");
	require(OK, "ETH transfer failed");
    _msgValue = msg.value - _feeAmount;
}

https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/FeeWrapper.sol#L108-L121

function _rubicallPayable(
    CallParams memory _params
) internal returns (bytes memory) {
    // charge fee from feeParams
    uint256 _msgValue = _chargeFeePayable(_params.feeParams);

    (bool _OK, bytes memory _data) = _params.target.call{value: _msgValue}(
        bytes.concat(_params.selector, _params.args)
    );

    require(_OK, "low-level call to the router failed");

    return _data;
}

https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/FeeWrapper.sol#L76-L89

Tools Used

Manual Review and VSCode

Recommended Mitigation Steps

Two options can be recommended to mitigate this issue:

  1. Add a reentrancy guard. Openzeppelin ReentrancyGuard.sol can be used, and nonReentrant modifier can be added to both FeeWrapper._chargeFeePayable() and FeeWrapper._rubicallPayable() functions.
  2. Check that the msg.value > _feeAmount before the low level call to the _feeTo address is made. In that case, there is no gain to the _feeTo malicious contract, since the value it’s sending via msg.value is always greater than the _feeAmount which can be stolen via reentrancy attack.

daoio (Rubicon) confirmed via duplicate issue #495


The original report as published on Code4rena, by Udsen. 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 contract performs an external call to transfer fees before updating its internal state or checking that the sent value exceeds the fee amount, enabling reentrancy.

Impact

A malicious _feeTo contract could repeatedly extract fees and drain ETH from the FeeWrapper contract if it holds sufficient balance.

What the attacker needed

Malicious _feeTo contract; FeeWrapper holds ETH

The fix

Add a reentrancy guard to the relevant functions and/or check that msg.value is greater than _feeAmount before making the low-level call.

How it was found

  1. The FeeWrapper contract calls the _feeTo address with the fee amount using a low-level call.
  2. The _feeTo contract has a receive function that re-enters FeeWrapper by calling rubicallPayable with a small msg.value.
  3. The re-entrant call sets totalAmount to the small msg.value and feeAmount to the original fee amount.
  4. FeeWrapper calls _chargeFeePayable again, which sends the fee amount to _feeTo again.
  5. This repeats as long as the contract has enough ETH.

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 reentrancy attack happens when a contract sends money to another contract before finishing its own accounting. The receiving contract can call back into the sender and take advantage of the unfinished state. Here, a malicious fee recipient could repeatedly trigger the fee payment and drain ETH from the FeeWrapper contract.

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 Udsen

View all

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