maxSellAllAmount and maxBuyAllAmount functions can be unintentionally paused (always revert).
The maxSellAllAmount and maxBuyAllAmount functions in RubiconRouter pass the user's entire balance to the market's buyAll/sellAll functions without accounting for the trading fee, so when the order can be fully matched, the subsequent fee transfer fails and the transaction reverts. An attacker could not exploit this for gain, but the functions become unusable for users whose full balance can be matched.
Submitted by PP1004, also found by hansfriese
Impact
The two functions maxSellAllAmount and maxBuyAllAmount will always revert in case at least (100-fee)% of user’s balance can be matched with orders.
Proof of Concept
Let’s say Bob placed an order selling 100 USDC with a low USDT price of 1:0.95.
Alice currently has 50 USDT and they want to maxSellAllAmount into USDC.
The function will pass 50 as amount into RubiconMarket’s buyAll function where it fully matches with Bob’s order. Here, the buy() function will first transfer alice’s 50 USDT in and later 50 * feeBPS / BPS as fee. In this case, alice can not afford to pay.
Therefore, the two functions maxSellAllAmount and maxBuyAllAmount are useless in case user’s request can be fully matched.
Recommended Mitigation Steps
Add the fee calculating before passing the amount to the RubiconMarket’s buyAll, sellAll function.
/// @dev this function takes a user's entire balance for the trade in case they want to do a max trade so there's no leftover dust
function maxBuyAllAmount(
ERC20 buy_gem,
ERC20 pay_gem,
uint256 max_fill_amount
) external returns (uint256 fill) {
//swaps msg.sender's entire balance in the trade
uint256 maxAmount = _calcAmountAfterFee(ERC20(buy_gem).balanceOf(msg.sender));
fill = RubiconMarket(RubiconMarketAddress).buyAllAmount(
buy_gem,
maxAmount,
pay_gem,
max_fill_amount
);
ERC20(buy_gem).transfer(msg.sender, fill);
}
/// @dev this function takes a user's entire balance for the trade in case they want to do a max trade so there's no leftover dust
function maxSellAllAmount(
ERC20 pay_gem,
ERC20 buy_gem,
uint256 min_fill_amount
) external returns (uint256 fill) {
//swaps msg.sender entire balance in the trade
uint256 maxAmount = _calcAmountAfterFee(ERC20(buy_gem).balanceOf(msg.sender));
fill = RubiconMarket(RubiconMarketAddress).sellAllAmount(
pay_gem,
maxAmount,
buy_gem,
min_fill_amount
);
ERC20(buy_gem).transfer(msg.sender, fill);
}
function _calcAmountAfterFee(uint256 amount) internal view returns (uint256) {
uint256 feeBPS = RubiconMarket(RubiconMarketAddress).getFeeBPS();
return amount.sub(amount.mul(feeBPS).div(10000));
}
Realised this is a separate issue from not transferring funds into the router. #376 will be the primary issue for that issue.
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
The functions pass the user's full balance as the amount to the market without first subtracting the trading fee, so the fee transfer fails when the full amount is matched.
Impact
The maxSellAllAmount and maxBuyAllAmount functions become unusable (always revert) when the user's entire balance can be fully matched with existing orders.
What the attacker needed
User has a balance to trade; existing orders can fully match the trade
The fix
The functions should calculate the amount after subtracting the fee before passing it to the market's buyAll/sellAll functions.
How it was found
- Bob places a sell order for 100 USDC at a low price of 1:0.95 USDT per USDC.
- Alice has 50 USDT and calls maxSellAllAmount to trade her entire balance into USDC.
- The function passes 50 USDT as the amount to RubiconMarket's buyAll function.
- The order is fully matched, so the market first transfers Alice's 50 USDT and then tries to charge a fee of 50 * feeBPS / BPS.
- Alice's balance is already zero, so the fee transfer fails and the transaction reverts.
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.
These two functions are meant to let you trade your entire balance of a token in one go. But they forget that the exchange takes a small fee, so they try to spend your whole balance and then also pay the fee, which fails because there is nothing left. As a result, the functions stop working whenever your whole balance can be traded.
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.