Cannot close leveraged positions
The closePosition function attempts to repay the entire borrowed amount at once, which fails if the user lacks extra liquid assets because the leveraged position's collateral value is insufficient to cover the debt. An attacker could not exploit this, but users cannot close their leveraged positions without additional funds.
Submitted by sinarette, also found by adriro and Jigsaw.
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L280-L302 https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L210-L223
Opening a leveraged position goes through some cycles of supplying collateral and borrowing. Since the leverage is larger than 1, the borrowed amount is usually larger than the liquid asset. So in most cases, you would need to repeat the loop backwards (repay, redeem, swap…). However, closePosition tries to repay the whole borrowed amount at once, which would revert. As a result, the user would fail to close a position if he has no extra liquid assets.
Proof of Concept
closePosition first repays the borrowed asset, then redeems the collateral and closes the position:
/* Position.sol # closePosition */
_repay(asset, quote, posId);
_redeem(asset, bathTokenAmount);
_removePosition(posId);
_repay would always try to repay the whole borrowed quote tokens, so first it would sell the asset tokens for quote tokens. However, since this is a leveraged position, there won’t be enough asset tokens to sell here:
/* Position.sol # _repay */
address _bathTokenQuote = bathHouseV2.getBathTokenFromAsset(_quote);
uint256 _amountToRepay = borrowBalanceOfPos(_posId); // total borrowed quote tokens
// sell asset for quote
_rubiconSwap(_asset, _quote, _amountToRepay, false);
uint256 _quoteBalance = IERC20(_quote).balanceOf(address(this));
It would try to replenish the asset tokens from the owner, which would fail if there’s not enough balance:
/* Position.sol # _marketSell */
if (_assetBalance < _payAmount) {
IERC20(_asset).transferFrom(
msg.sender,
address(this),
_payAmount.sub(_assetBalance).add(_fee)
);
}
The below test shows that the position closing would revert if there’s not enough assets:
it.only("Cannot close position", async function () {
const { owner, testCoin, testStableCoin, Position, rubiconMarket } =
await loadFixture(deployPoolsUtilityFixture);
await Position.connect(owner).buyAllAmountWithLeverage(
testCoin.address,
testStableCoin.address,
TEST_AMOUNT,
parseUnits("2.97")
);
/* Revoke: as if there were no assets */
await testCoin.connect(owner).approve(Position.address, parseUnits("0"));
/* Cannot close position without enough assets */
await expect(Position.connect(owner).closePosition(1)).to.be.reverted;
});
Tools Used
Hardhat
Recommended Mitigation Steps
Add a repay loop (repay - redeem - swap…), or enable partially closing positions.
daoio (Rubicon) disagreed with severity, disputed and commented:
It’s mainly intended behavior and user needs to firstly repay the debt before redemption of his tokens; though, I like the idea of partially closing position.
HickupHH3 (judge) decreased severity to Medium and commented:
If I open a position, I should be able to close it in the same block without requiring further capital from my end because it should unwind correctly.
Considering this Medium severity because the debt can be repaid and have the position closed, so there isn’t exactly a loss of funds, but it’s bad UX arising from unsatisfactory unwinding behaviour.
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 closePosition function repays the full borrowed amount before redeeming collateral, without accounting for the fact that the collateral's value is less than the debt due to leverage.
Impact
Users cannot close leveraged positions without supplying additional liquid assets, leading to bad user experience but no direct loss of funds.
What the attacker needed
A leveraged position; no extra liquid assets
The fix
The contract should implement a repay loop (repay, redeem, swap) or allow partial closing of positions.
How it was found
- Open a leveraged position using buyAllAmountWithLeverage.
- Revoke approval for the asset token to simulate having no extra assets.
- Call closePosition and observe that it 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.
In leveraged trading, you borrow money to increase your position size. To close the position, you normally sell some assets to repay the loan. This report shows that the contract tries to repay the entire loan in one go, but the assets you have are not enough to cover it, so the closing fails unless you add more of your own money.
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.