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.
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:
- The call to
(bool OK, ) = payable(_feeTo).call{value: _feeAmount}("")is made. Lets assume the_totalAmount= 1ETH and_feeAmount= 0.1ETH initially. _feeTocontract has therecieve()function implemented in it.- It reenters the
FeeWrappercontract by calling therubicallPayable()function by sending 0.01ETH asmsg.value. - It sets the
_feeParams.totalAmount= 0.01ETH and_feeParams.feeAmount= 0.1ETH in the call torubicallPayable(). FeeWrapper.chargeFeePayable()will be called again by therubicallPayable()function.(bool OK, ) = payable(_feeTo).call{value: _feeAmount}(""); will be called andTx will passsince there is enough ETH in the contract from the previous deposit.- Again
_feeParams.feeAmount= 0.1ETH will be transferred to the_feeToaddress. - This can be continued till there is enough ETH in the contract.
- This happens as there is
no checkto make suremsg.value > _feeAmountbefore the low levelcallto the_feeToaddress.
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:
- Add a reentrancy guard.
OpenzeppelinReentrancyGuard.solcan be used, andnonReentrantmodifier can be added to bothFeeWrapper._chargeFeePayable()andFeeWrapper._rubicallPayable()functions. - Check that the
msg.value > _feeAmountbefore the low levelcallto the_feeToaddress is made. In that case, there is no gain to the_feeTomalicious contract, since the value it’s sending viamsg.valueis always greater than the_feeAmountwhich can be stolen viareentrancy attack.
daoio (Rubicon) confirmed via duplicate issue #495
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
- The FeeWrapper contract calls the _feeTo address with the fee amount using a low-level call.
- The _feeTo contract has a receive function that re-enters FeeWrapper by calling rubicallPayable with a small msg.value.
- The re-entrant call sets totalAmount to the small msg.value and feeAmount to the original fee amount.
- FeeWrapper calls _chargeFeePayable again, which sends the fee amount to _feeTo again.
- 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.