Both buyAllAmountWithLeverage and sellAllAmountWithLeverage always revert
The functions buyAllAmountWithLeverage and sellAllAmountWithLeverage always revert because the internal _maxBorrow function requires positive liquidity, but a newly created Position contract has zero liquidity. An attacker can transfer a tiny amount of bathToken to the Position contract to trigger the revert, preventing users from opening leveraged positions.
nobody2018 Follow with Bauer
Originally disclosed on Code4rena
Submitted by nobody2018, also found by Bauer.
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L545 https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L306-L319
Both buyAllAmountWithLeverage and sellAllAmountWithLeverage always revert. So all users cannot open a leveraged position.
Proof of Concept
openPosition is the entry point for users to open long/short positions, internally calling the _borrowLimit function to calculate an amount of iterations needed to reach desired amount with leverage. The logic of this function is: if the collateral has been provided, then the _maxBorrow function will be called to calculate the maximum amount available to borrow from _bathToken market, and then the returned result will be added to the _loopBorrowed variable.
// check if collateral was already supplied
uint256 _minted = IERC20(_bathToken).balanceOf(address(this));
// how much is borrowed on a current loop
uint256 _loopBorrowed;
while (_assetAmount <= _desiredAmount) {
if (_limit == 0) {
// if collateral already provided
if (_minted != 0) {
uint256 _max = _maxBorrow(_bathToken);
// take into account previous collateral
_loopBorrowed = wmul(_assetAmount, _collateralFactor).add(
_max
);
Next, look at the code of _maxBorrow:
function _maxBorrow(
address _bathToken
) internal view returns (uint256 _max) {
(uint256 _err, uint256 _liq, uint256 _shortfall) = comptroller
.getAccountLiquidity(address(this)); //if address(this) is new user of comptroller, _liq always equal to 0
require(_err == 0, "_maxBorrow: ERROR");
require(_liq > 0, "_maxBorrow: LIQUIDITY == 0"); //if _liq equals to 0, tx will revert.
require(_shortfall == 0, "_maxBorrow: SHORTFALL != 0");
uint256 _price = oracle.getUnderlyingPrice(CToken(_bathToken));
_max = (_liq.mul(10 ** 18)).div(_price);
require(_max > 0, "_maxBorrow: can't borrow 0");
}
To make the _liq returned by comptroller.getAccountLiquidity(address(this)) be 0, then address(this) must not provide any collateral. In other words, this contract is a new user to the comptroller. Obviously, the Position contract created by the user meets this condition.
The condition for calling _maxBorrow is that IERC20(_bathToken).balanceOf(address(this)) returns a non-zero value. A newly created Position contract does not have any tokens. However, we can transfer 1wei _bathToken to it. This allows the code to execute to _maxBorrow and revert.
There are two ways to transfer _bathToken to the newly created Position:
- Front-run
buyAllAmountWithLeverageorsellAllAmountWithLeverage. - Once the Position is created, transfer all
_bathTokenssupported byBathHouseV2to it. The amount transferred is 1wei.
In fact, _maxBorrow will also revert in normal scenario. Position borrows other tokens resulting in _liq being 0.
Recommended Mitigation Steps
We should use comptroller.getAccountLiquidity instead of the balance of _bathToken as the condition for calling _maxBorrow.
--- a/contracts/utilities/poolsUtility/Position.sol
+++ b/contracts/utilities/poolsUtility/Position.sol
@@ -534,7 +534,7 @@ contract Position is Ownable, DSMath {
uint256 _desiredAmount = wmul(_assetAmount, _leverage);
// check if collateral was already supplied
- uint256 _minted = IERC20(_bathToken).balanceOf(address(this));
+ (,uint256 _minted,) = comptroller.getAccountLiquidity(address(this));
// how much is borrowed on a current loop
uint256 _loopBorrowed;
Medium: loss of functionality due to griefing from external party.
Marking as best because it mentions an additional attack path compared to counterpart.
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 code uses the bathToken balance as a condition to call _maxBorrow, but _maxBorrow requires positive liquidity, which a new Position contract does not have.
Impact
Users cannot open leveraged positions, and an attacker can grief the system by transferring a tiny amount of bathToken to a Position contract.
What the attacker needed
Attacker can front-run or transfer tokens; Position contract is newly created
The fix
The code should use comptroller.getAccountLiquidity instead of the bathToken balance as the condition for calling _maxBorrow.
How it was found
- Attacker front-runs the buyAllAmountWithLeverage or sellAllAmountWithLeverage transaction.
- Attacker transfers 1 wei of bathToken to the newly created Position contract.
- The Position contract's balance becomes non-zero, so it calls _maxBorrow.
- _maxBorrow calls comptroller.getAccountLiquidity, which returns 0 liquidity for the new contract.
- _maxBorrow reverts because liquidity is 0, causing the entire transaction to revert.
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.
This is a smart contract bug where a function that opens leveraged positions always fails. The contract checks if it has any tokens and then tries to borrow more, but the borrowing check fails because the contract has no collateral yet. An attacker can send a tiny amount of a token to the contract to make it fail even earlier, blocking all users from using the feature.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.