Incorrect fee handling in Position.sol's Market Buy / Sell functions
The market buy and sell functions in Position.sol incorrectly account for fees, which can lead to improper fee deductions and wrong trade execution. An attacker could potentially exploit this to cause incorrect trade amounts or fee losses.
Submitted by bytes032, also found by zhuXKET, zhuXKET, zhuXKET, qpzm, immeas, immeas, joestakey, kutugu, rbserver, sinarette, R2, carrotsmuggler, cccz, peakbolt, caventa, rvierdiiev, rvierdiiev, anodaram, top1st, RaymondFam, T1MOH, T1MOH and volodya.
The incorrect fee handling in the Position.sol market buy and sell functions can lead to improper fee deductions and wrong trade execution.
Proof of Concept
In Position.t.sol, _rubiconSwap_ executes the order in the RubiconMarket when opening/closing a position.
rubiconSwap can call either marketBuy or marketSell, depending on the circumstances.
The issue is that both marketBuy and marketSell account for fees, where they shouldn’t.
function _marketBuy(
address _asset,
address _quote,
uint256 _maxFill
) internal {
uint256 _fee = _maxFill.mul(rubiconMarket.getFeeBPS()).div(10000);
// @audit fee is applied twice?
uint256 _buyAmount = rubiconMarket.getBuyAmount(
ERC20(_asset),
ERC20(_quote),
_maxFill.sub(_fee)
);
IERC20(_quote).approve(address(rubiconMarket), _maxFill);
rubiconMarket.buyAllAmount(
ERC20(_asset),
_buyAmount,
ERC20(_quote),
_maxFill
);
}
function _marketSell(
address _asset,
address _quote,
uint256 _minFill
) internal {
// @audit fee?
uint256 _feeBPS = rubiconMarket.getFeeBPS();
uint256 _fee = _minFill.mul(_feeBPS).div(10000);
uint256 _payAmount = rubiconMarket.getPayAmount(
ERC20(_asset),
ERC20(_quote),
_minFill.add(_fee)
);
// @audit doesnt account for maker fee
// Add the BASE fee to the amount to be paid
uint256 _assetBalance = IERC20(_asset).balanceOf(address(this));
/// @dev recalculate fee in _asset form
_fee = _payAmount.mul(_feeBPS).div(10000);
if (_assetBalance < _payAmount) {
IERC20(_asset).transferFrom(
msg.sender,
address(this),
_payAmount.sub(_assetBalance).add(_fee) // transfer fee
);
}
As discussed with the sponsor, the proper approach for v2 trades is do not account for the fee. Instead, it should be taken from the input amount, unlike in v1 (where it was needed for the input amount to have some addition to paying the fee).
So basically, this fee calculation in Position is useless for v2, and that’s an issue.
Recommended Mitigation Steps
Remove all fee calculations from the _marketBuy and _marketSell functions in Position.sol. As per the discussion with the sponsor, fees should not be accounted for in these functions for v2 trades.
daoio (Rubicon) confirmed via duplicate issue #1149
HickupHH3 (judge) decreased severity to Medium commented:
As discussed with the sponsor, the proper approach for v2 trades is do not account for the fee. Instead, it should be taken from the input amount, unlike in v1 (where it was needed for the input amount to have some addition to paying the fee).
I agree that this should be the approach, but because the fee accounting in
RubiconMarkethas so many errors, I’m unsure what the expected behaviour looks like (should input amounts,min_fill_amtbe inclusive or exclusive of fees?). This also affects how I should be de duping the issues as there is some overlap here and there.#1149 and #1206 fall under this purview of accounting for fees where it shouldn’t be happening (double counting), and that’s why I grouped them together.
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 fee is accounted for in the Position contract's market buy and sell functions when it should not be, because the underlying market already handles fees.
Impact
This could lead to improper fee deductions and wrong trade execution, potentially causing financial loss to users.
What the attacker needed
None
The fix
Remove all fee calculations from the `_marketBuy` and `_marketSell` functions in `Position.sol`.
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.
In a trading contract, fees are usually taken from the amount you put in. Here, the code adds or subtracts fees in the wrong place, so the trade might use the wrong amount of money. This could make trades fail or cost users more than they should.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.