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.
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.
Next, look at the code of _maxBorrow:
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:
In fact, _maxBorrow will also revert in normal scenario. Position borrows other tokens resulting in _liq being 0.
We should use comptroller.getAccountLiquidity instead of the balance of _bathToken as the condition for calling _maxBorrow.
daoio (Rubicon) confirmed
HickupHH3 (judge) commented:
