Incorrect calculations can occur when calling Position._marketBuy and Position._marketSell functions that do not include maker fee in _fee
The Position contract's internal market buy and sell helpers calculate a fee that omits the maker fee, while the underlying market charges that fee on execution. A user calling the leveraged buy function therefore receives more asset tokens than they should, taking value from other users in the protocol.
Submitted by rbserver, also found by adriro, zhuXKET, immeas, bin2chen, cccz, rvierdiiev, said, anodaram, T1MOH, RaymondFam, MalfurionWhitehat, ladboy233 and J4de.
When the following Position._marketBuy and Position._marketSell functions calculate _fee, the maker fee is not included.
function _marketBuy(
address _asset,
address _quote,
uint256 _maxFill
) internal {
uint256 _fee = _maxFill.mul(rubiconMarket.getFeeBPS()).div(10000);
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 {
uint256 _feeBPS = rubiconMarket.getFeeBPS();
uint256 _fee = _minFill.mul(_feeBPS).div(10000);
uint256 _payAmount = rubiconMarket.getPayAmount(
ERC20(_asset),
ERC20(_quote),
_minFill.add(_fee)
);
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)
);
}
IERC20(_asset).approve(
address(rubiconMarket),
IERC20(_asset).balanceOf(address(this))
);
rubiconMarket.sellAllAmount(
ERC20(_asset),
_payAmount,
ERC20(_quote),
_minFill
);
}
When the Position._marketBuy and Position._marketSell functions eventually call the following RubiconMarket.buy function, the maker fee can be considered and paid when makerFee() > 0 is true.
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/RubiconMarket.sol#L314-L448
function buy(
uint256 id,
uint256 quantity
) public virtual can_buy(id) synchronized returns (bool) {
OfferInfo memory _offer = offers[id];
uint256 spend = mul(quantity, _offer.buy_amt) / _offer.pay_amt;
require(uint128(spend) == spend, "spend is not an int");
require(uint128(quantity) == quantity, "quantity is not an int");
///@dev For backwards semantic compatibility.
if (
quantity == 0 ||
spend == 0 ||
quantity > _offer.pay_amt ||
spend > _offer.buy_amt
) {
return false;
}
offers[id].pay_amt = sub(_offer.pay_amt, quantity);
offers[id].buy_amt = sub(_offer.buy_amt, spend);
/// @dev Fee logic added on taker trades
uint256 fee = mul(spend, feeBPS) / 100_000;
require(
_offer.buy_gem.transferFrom(msg.sender, feeTo, fee),
"Insufficient funds to cover fee"
);
// taker pay maker 0_0
if (makerFee() > 0) {
uint256 mFee = mul(spend, makerFee()) / 100_000;
/// @dev Handle the v1 -> v2 migration case where if owner == address(0) we transfer this fee to _offer.recipient
if (_offer.owner == address(0) && getRecipient(id) != address(0)) {
require(
_offer.buy_gem.transferFrom(
msg.sender,
_offer.recipient,
mFee
),
"Insufficient funds to cover fee"
);
} else {
require(
_offer.buy_gem.transferFrom(msg.sender, _offer.owner, mFee),
"Insufficient funds to cover fee"
);
}
...
}
...
}
Since the Position._marketBuy and Position._marketSell functions do not consider the maker fee but the RubiconMarket.buy function does, _fee calculated by the Position._marketBuy and Position._marketSell functions and the fees, including the maker fee, that are paid by the user when calling the RubiconMarket.buy function are different. As a result, calling the Position contract’s functions that eventually call the Position._marketBuy or Position._marketSell function can cause incorrect calculations and user funds to be lost. For instance, when calling the Position.buyAllAmountWithLeverage function, the user should receive less asset tokens when the Position._marketBuy function includes the maker fee in _fee than when the Position._marketBuy function does not include the maker fee in _fee. Since the Position._marketBuy function currently does not include the maker fee in _fee, the user receives more asset tokens than she or he is entitled to, and other users’ asset tokens in the protocol are lost.
Proof of Concept
First, please update the Position._marketBuy function as follows to divide by 100_000 for the POC purpose. This is for fixing the bug mentioned in the report titled “Calling Position._marketBuy and Position._marketSell functions that calculate _fee by dividing by 10000 can cause incorrect calculations”.
function _marketBuy(
address _asset,
address _quote,
uint256 _maxFill
) internal {
/** @audit divide by 100_000 for POC purpose */
// uint256 _fee = _maxFill.mul(rubiconMarket.getFeeBPS()).div(10000);
uint256 _fee = _maxFill.mul(rubiconMarket.getFeeBPS()).div(100_000);
/** */
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
);
}
Second, please add the following changes of quote and asset tokens when Position._marketBuy function calculates _fee through dividing by 100_000 but does not consider maker fee test in the Long positions describe block in test\hardhat-tests\leverage-wrapper.ts. This test will pass to show the number of asset tokens received by the user who calls the Position.buyAllAmountWithLeverage function when the Position._marketBuy function does not include the maker fee in _fee.
it.only("changes of quote and asset tokens when Position._marketBuy function calculates _fee through dividing by 100_000 but does not consider maker fee", async function () {
const { owner, testCoin, testStableCoin, Position, rubiconMarket} =
await loadFixture(deployPoolsUtilityFixture);
// testCoin is quote token when calling Position.buyAllAmountWithLeverage function
const ownerTestCoinBalanceBefore = await testCoin.balanceOf(owner.address);
// testStableCoin is asset token when calling Position.buyAllAmountWithLeverage function
const ownerTestStableCoinBefore = await testStableCoin.balanceOf(owner.address);
// there is a maker fee
const fee = 50;
await rubiconMarket.setMakerFee(fee);
// owner calls Position.buyAllAmountWithLeverage function
await Position.connect(owner).buyAllAmountWithLeverage(
testCoin.address,
testStableCoin.address,
TEST_AMOUNT,
x1_25
);
// owner sends 25000000000000000000 quote tokens
const ownerTestCoinBalanceAfter = await testCoin.balanceOf(owner.address);
expect(ownerTestCoinBalanceAfter.sub(ownerTestCoinBalanceBefore)).to.equal(-25000000000000000000n);
// owner receives 5624942 asset tokens
const ownerTestStableCoinAfter = await testStableCoin.balanceOf(owner.address);
expect(ownerTestStableCoinAfter.sub(ownerTestStableCoinBefore)).to.equal(5624942);
});
Third, please update the Position._marketBuy function again as follows to include the maker fee in _fee for the POC purpose.
function _marketBuy(
address _asset,
address _quote,
uint256 _maxFill
) internal {
/** @audit divide by 100_000 for POC purpose */
// uint256 _fee = _maxFill.mul(rubiconMarket.getFeeBPS()).div(10000);
uint256 _fee = _maxFill.mul(rubiconMarket.getFeeBPS()).div(100_000);
/** */
/** @audit consider maker fee for POC purpose */
if (rubiconMarket.makerFee() > 0) {
_fee += mul(_maxFill, rubiconMarket.makerFee()) / 100_000;
}
/** */
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
);
}
Fourth, please add the following changes of quote and asset tokens when Position._marketBuy function calculates _fee through dividing by 100_000 and also considers maker fee test in the Long positions describe block in test\hardhat-tests\leverage-wrapper.ts. This test will pass to show the number of asset tokens received by the user who calls the Position.buyAllAmountWithLeverage function when the Position._marketBuy function includes the maker fee in _fee. This test also demonstrates that the user should receive less asset tokens when the Position._marketBuy function includes the maker fee in _fee than when the Position._marketBuy function does not include the maker fee in _fee.
it.only("changes of quote and asset tokens when Position._marketBuy function calculates _fee through dividing by 100_000 and also considers maker fee", async function () {
const { owner, testCoin, testStableCoin, Position, rubiconMarket} =
await loadFixture(deployPoolsUtilityFixture);
// testCoin is quote token when calling Position.buyAllAmountWithLeverage function
const ownerTestCoinBalanceBefore = await testCoin.balanceOf(owner.address);
// testStableCoin is asset token when calling Position.buyAllAmountWithLeverage function
const ownerTestStableCoinBefore = await testStableCoin.balanceOf(owner.address);
// there is a maker fee
const fee = 50;
await rubiconMarket.setMakerFee(fee);
// owner calls Position.buyAllAmountWithLeverage function
await Position.connect(owner).buyAllAmountWithLeverage(
testCoin.address,
testStableCoin.address,
TEST_AMOUNT,
x1_25
);
// owner still sends 25000000000000000000 quote tokens
const ownerTestCoinBalanceAfter = await testCoin.balanceOf(owner.address);
expect(ownerTestCoinBalanceAfter.sub(ownerTestCoinBalanceBefore)).to.equal(-25000000000000000000n);
// yet, owner receives 5622129 asset tokens
const ownerTestStableCoinAfter = await testStableCoin.balanceOf(owner.address);
expect(ownerTestStableCoinAfter.sub(ownerTestStableCoinBefore)).to.equal(5622129);
// owner receives less asset tokens in this situation than when Position._marketBuy function does not consider maker fee
expect(5622129).to.be.lt(5624942);
});
Tools Used
VSCode
Recommended Mitigation Steps
The Position._marketBuy and Position._marketSell functions can be updated to include the maker fee in _fee when rubiconMarket.makerFee() > 0 is true.
HickupHH3 (judge) decreased severity to Medium and commented:
As mentioned in #282, issues with the market’s implementation make it difficult for me to claim that
_marketBuy()and_marketSell()shouldn’t be accounting for fees at all.
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 calculation in the Position helpers only uses the taker fee rate and never adds the maker fee that the market applies when makerFee() is greater than zero.
Impact
Users calling the leveraged buy path could receive more asset tokens than they are entitled to, with the excess taken from other users' funds in the protocol; the judge rated this Medium.
What the attacker needed
Maker fee set above zero on the market; user calls a leveraged Position function
The fix
The helpers should be updated to add the maker fee into the fee calculation whenever the market's maker fee is greater than zero.
How it was found
- Set a non-zero maker fee on the market contract.
- Call the leveraged buy function on the Position contract as a user.
- Observe the quote tokens spent and the asset tokens received.
- Repeat with the helper changed to include the maker fee in its fee calculation.
- Compare the two runs: the user receives fewer asset tokens when the maker fee is included.
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 trading contract works out in advance how much of a fee a trade will cost, so it can ask the user for the right amount. Here it forgot one part of the fee (the part paid to the person who placed the order being filled). Because the real trade still charges that part, the user ends up getting slightly more tokens than they paid for, and the extra comes out of the shared pool.
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.