Ineffective ReserveRatio Enforcement
The reserve ratio check in the BathPair contract was performed only before placing market-making orders, not after, allowing strategists to withdraw more than the allowed percentage of pool liquidity. An attacker could overutilise the pool, leaving it illiquid and preventing users from withdrawing funds.
Originally disclosed on Code4rena
Submitted by xiaoming90, also found by shenwilly and pedroais
Background
Per whitepaper, ReserveRatio ensures that some amount of pool liquidity is present in the contract at all times. This protects the pools from overutilization by strategists and ensures that a portion of the underlying pool assets are liquid so LPs can withdraw. If the ReserveRatio is set to 50, meaning 50% of a liquidity pool’s assets must remain in the pool at all times.
However, it was possible for the strategists to bypass the Reserve Ratio restriction and utilize all the funds in the pools, causing the pools to be illiquid.
Proof of Concept
Strategists place their market making trades via the BathPair.placeMarketMakingTrades function. This function would first check if the pool’s reserveRatio is maintained before proceeding. If true, strategists will be allowed to place their market making trades with orders with arbitrary pay amount. Strategists could place ask and bid orders with large pay amount causing large amount of funds to be withdrawn from the pools. The root cause is that at the end of the transaction, there is no additional validation to ensure the pools are not overutilized by strategists and the reserve ratio of the pools is maintained.
function placeMarketMakingTrades(
address[2] memory tokenPair, // ASSET, Then Quote
uint256 askNumerator, // Quote / Asset
uint256 askDenominator, // Asset / Quote
uint256 bidNumerator, // size in ASSET
uint256 bidDenominator // size in QUOTES
) public onlyApprovedStrategist(msg.sender) returns (uint256 id) {
// Require at least one order is non-zero
require(
(askNumerator > 0 && askDenominator > 0) ||
(bidNumerator > 0 && bidDenominator > 0),
"one order must be non-zero"
);
address _underlyingAsset = tokenPair[0];
address _underlyingQuote = tokenPair[1];
(
address bathAssetAddress,
address bathQuoteAddress
) = enforceReserveRatio(_underlyingAsset, _underlyingQuote);
require(
bathAssetAddress != address(0) && bathQuoteAddress != address(0),
"tokenToBathToken error"
);
.. SNIP..
// Place new bid and/or ask
// Note: placeOffer returns a zero if an incomplete order
uint256 newAskID = IBathToken(bathAssetAddress).placeOffer(
ask.pay_amt,
ask.pay_gem,
ask.buy_amt,
ask.buy_gem
);
uint256 newBidID = IBathToken(bathQuoteAddress).placeOffer(
bid.pay_amt,
bid.pay_gem,
bid.buy_amt,
bid.buy_gem
);
.. SNIP..
}
/// @notice This function enforces that the Bath House reserveRatio (a % of underlying pool liquidity) is enforced across all pools
/// @dev This function should ensure that reserveRatio % of the underlying liquidity always remains on the Bath Token. Utilization should be 1 - reserveRatio in practice assuming strategists use all available liquidity.
function enforceReserveRatio(
address underlyingAsset,
address underlyingQuote
)
internal
view
returns (address bathAssetAddress, address bathQuoteAddress)
{
bathAssetAddress = IBathHouse(bathHouse).tokenToBathToken(
underlyingAsset
);
bathQuoteAddress = IBathHouse(bathHouse).tokenToBathToken(
underlyingQuote
);
require(
(
IBathToken(bathAssetAddress).underlyingBalance().mul(
IBathHouse(bathHouse).reserveRatio()
)
).div(100) <= IERC20(underlyingAsset).balanceOf(bathAssetAddress),
"Failed to meet asset pool reserve ratio"
);
require(
(
IBathToken(bathQuoteAddress).underlyingBalance().mul(
IBathHouse(bathHouse).reserveRatio()
)
).div(100) <= IERC20(underlyingQuote).balanceOf(bathQuoteAddress),
"Failed to meet quote pool reserve ratio"
);
}
Test Cases
The following is the snippet of the test case result. Reserve Ratio is initialized to 80% in this example, which means only 20% of the funds could be utilized by strategists. The BathWETH and BathDAI pools contained 1 WETH and 100 DAI respectively after users deposited their funds into the pools. At the bottom half of the output, it shows that it was possible for the strategists to utilise 90% of the funds in the pools to place an ask and bid order, which exceeded the 20% limit.
The last two lines of the output show that 90% of the funds in the pools are outstanding.
..SNIP..
--------------- Order Book ---------------
[-] asks index 0: ask_pay_amt = 0, ask_buy_amt = 0
[-] asks index 1: ask_pay_amt = 0, ask_buy_amt = 0
[-] asks index 2: ask_pay_amt = 0, ask_buy_amt = 0
[-] asks index 3: ask_pay_amt = 0, ask_buy_amt = 0
[+] bids index 0: bid_pay_amt = 0, bid_buy_amt = 0
[+] bids index 1: bid_pay_amt = 0, bid_buy_amt = 0
[+] bids index 2: bid_pay_amt = 0, bid_buy_amt = 0
[+] bids index 3: bid_pay_amt = 0, bid_buy_amt = 0
bathAssetInstance: underlyingBalance() = 1 WETH, balanceOf = 1 WETH, Outstanding Amount = 0 WETH
bathQuoteInstance: underlyingBalance() = 100 DAI, balanceOf = 100 DAI, Outstanding Amount = 0 DAI
After Placing Order
--------------- Order Book ---------------
[-] asks index 0: ask_pay_amt = 0.9, ask_buy_amt = 180
[-] asks index 1: ask_pay_amt = 0, ask_buy_amt = 0
[-] asks index 2: ask_pay_amt = 0, ask_buy_amt = 0
[-] asks index 3: ask_pay_amt = 0, ask_buy_amt = 0
[+] bids index 0: bid_pay_amt = 90, bid_buy_amt = 0.9
[+] bids index 1: bid_pay_amt = 0, bid_buy_amt = 0
[+] bids index 2: bid_pay_amt = 0, bid_buy_amt = 0
[+] bids index 3: bid_pay_amt = 0, bid_buy_amt = 0
bathAssetInstance: underlyingBalance() = 1 WETH, balanceOf = 0.1 WETH, Outstanding Amount = 0.9 WETH
bathQuoteInstance: underlyingBalance() = 100 DAI, balanceOf = 10 DAI, Outstanding Amount = 90 DAI
..SNIP..
Test Script can be found at https://gist.github.com/xiaoming9090/c4fcd4e967bd7d6940429e5d8e39004d
Impact
Following are the impacts of this issue:
- Underlying pool assets are overutilized by strategists, causing the pools to be illiquid. Users might not be able to withdraw their funds from the pools as the pools might not have sufficient underlying assets remained as their assets have been deployed to the Rubicon Market.
- Reserve Ratio is one of the key security parameters to safeguard LP’s funds so that the amount of losses the pools could potentially incur is limited. Without effective reserve ratio enforcement, strategists could deploy (“invest”) all the user capital on the Rubicon Market. If the strategist makes a loss from all their orders, the LP would incur significant loss.
Recommended Mitigation Steps
Check that the reserveRatio for each of the underlying liquidity pools (asset and quote bathTokens) is observed before and after function execution.
function placeMarketMakingTrades(
address[2] memory tokenPair, // ASSET, Then Quote
uint256 askNumerator, // Quote / Asset
uint256 askDenominator, // Asset / Quote
uint256 bidNumerator, // size in ASSET
uint256 bidDenominator // size in QUOTES
) public onlyApprovedStrategist(msg.sender) returns (uint256 id) {
// Require at least one order is non-zero
require(
(askNumerator > 0 && askDenominator > 0) ||
(bidNumerator > 0 && bidDenominator > 0),
"one order must be non-zero"
);
address _underlyingAsset = tokenPair[0];
address _underlyingQuote = tokenPair[1];
(
address bathAssetAddress,
address bathQuoteAddress
) = enforceReserveRatio(_underlyingAsset, _underlyingQuote);
require(
bathAssetAddress != address(0) && bathQuoteAddress != address(0),
"tokenToBathToken error"
);
.. SNIP..
// Place new bid and/or ask
// Note: placeOffer returns a zero if an incomplete order
uint256 newAskID = IBathToken(bathAssetAddress).placeOffer(
ask.pay_amt,
ask.pay_gem,
ask.buy_amt,
ask.buy_gem
);
uint256 newBidID = IBathToken(bathQuoteAddress).placeOffer(
bid.pay_amt,
bid.pay_gem,
bid.buy_amt,
bid.buy_gem
);
.. SNIP..
// Ensure that the strategist does not overutilize
enforceReserveRatio(_underlyingAsset, _underlyingQuote);
}
bghughes (Rubicon) confirmed and commented:
Good issue! I believe it needs to just be moved to the end of the function. Nice catch and already implemented in practice.
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
Impact
The pool becomes illiquid, so users might not be able to withdraw their funds, and the strategist could deploy all user capital, leading to significant losses for liquidity providers if the strategist's orders lose money.
What the attacker needed
Approved strategist role
The fix
The reserve ratio check should be performed both before and after placing orders to ensure the ratio is maintained.
How it was found
- A strategist calls placeMarketMakingTrades with large ask and bid amounts.
- The function checks the reserve ratio before placing orders, which passes.
- The orders are placed, withdrawing a large amount of funds from the pools.
- No further check is done, so the pool ends up with less than the required reserve.
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 reserve ratio is a rule that keeps a minimum portion of funds in a pool so users can always withdraw. The contract checked this rule only before a strategist placed orders, but not after, so the strategist could take out almost all the funds. This left the pool empty and users unable to get their money back.
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.