A liquidated position possibly cannot be closed
The closePosition function attempts to close a margin position using the full original amount, ignoring any liquidation that reduced the position. An attacker could liquidate a position, after which the owner cannot close it, forcing manual recovery and risking mismanagement of other positions.
Submitted by thekmj, also found by immeas, bin2chen and ast3ros.
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L210-L223 https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L382-L397
The Compound V2 protocol is a lending protocol that allows users to supply collaterals in order to borrow other assets (possibly the collateral itself). If a user’s position gets undercollateralized, the user will get liquidated (i.e. a liquidator can seize some of their collateral, while repaying also part of their debt.)
The problem is that, if a user’s created margin position gets liquidated, it is not possible to close said position, at least not without disrupting other positions alongside that.
This is because the function closePosition() will attempt to close the position using the full amount it has been provided when opening. In other words, the contract never got the info that any liquidations happened, and will attempt to explicitly undo the position in addition to closing it.
Proof of concept
We will use test/hardhat-tests/leverage-wrapper.ts for the POC.
- Firstly, we want more data returned from the fixture. Modify the following lines from the fixture as follows:
return {
rubiconMarket,
testCoin,
cToken,
owner,
otherAccount,
testStableCoin,
cTokenStable,
Position,
priceOracle, // add this
comptroller, // add this
};
- Then, paste the following test:
describe("Liquidated position bug", function () {
it("POC", async function () {
const { owner, testCoin, testStableCoin, cToken, cTokenStable, Position, otherAccount, priceOracle, comptroller } = await loadFixture(
deployPoolsUtilityFixture
); // I really went all out on this didn't I?
// assert that, initially, the Position contract holds no cTokens
expect(await cToken.balanceOf(Position.address)).to.be.eq(0);
// let's first open a long position with 2x margin
await Position.connect(owner).buyAllAmountWithLeverage(
testCoin.address,
testStableCoin.address,
TEST_AMOUNT,
x2_332
);
// now drop the price to half
await priceOracle.addCtoken(cToken.address, parseUnits("0.5", 18));
// caveat: gotta set the close factor and liq incentive first
await comptroller.connect(owner)._setCloseFactor(parseUnits("0.5"));
await comptroller.connect(owner)._setLiquidationIncentive(parseUnits("0.1"));
// now fund otherAccount with some testStableCoin, and liquidate
await testStableCoin.connect(otherAccount).faucet();
await testStableCoin.connect(otherAccount).approve(cTokenStable.address, parseUnits("100000")); // excess approval is fine
await cTokenStable.connect(otherAccount).liquidateBorrow(Position.address, parseUnits("12", 6), cToken.address); // liquidated!
// now attempt to close the position, but fail
await expect(Position.connect(owner).closePosition(1)).to.be.reverted;
// however, the position contract still have cToken, but also positive debt
expect(await cToken.balanceOf(Position.address)).to.be.gt(0);
expect(await cTokenStable.borrowBalanceStored(Position.address)).to.be.gt(0);
});
});
It is worth noting that the funds can still be recovered. This is because Compound exposes a function, repayBehalf, allowing the user to repay on behalf of Position, and Position itself exposes a function, withdraw, allowing users to withdraw their CTokens directly and redeem.
However, this defeats the purpose of Position contract, when the users are forced to manage their margin positions for the Position contract, as opposed to the contract doing so for them.
The most serious impact here, we believe, is not the difficult recover of funds, but in affecting/misleading user decisions when managing their margin portfolio. During a liquidation, an asset is repaid to seize another asset. If two assets that are seized belongs to two different positions, a “technically liquidated” position can be withdrawn, but leaving other positions at risk. In other words, the impossibility of closing a position will cause mismanagement, which is the opposite of what Position is trying to achieve.
Tools used
Manual review + Hardhat for POC/testing
Recommended mitigation
This is a very difficult problem to address due to its nature as a business logic problem. At the very least, it is not possible to tell whether “a position was liquidated”, due to the way cross-margin positionings work.
We suggest exposing the following extra functionalities to address the immediate problem:
-
A “close position given margin” function, quite literally the opposite counterpart of “open position” function: The user is able to supply an amount of borrow to repay, an amount of collateral to withdraw, and an initial margin. The function calculates the loopings needed to repay/withdraw the required amount, and possibly performs them.
- The point here is, the ability to close positions that have not been opened. If a position gets liquidated, there is no way for the
Positioncontract to tell and manage its own portfolio accordingly.
- The point here is, the ability to close positions that have not been opened. If a position gets liquidated, there is no way for the
-
A function to close a position without un-looping the position. This allows users to freely close positions that they recognize are no longer applicable.
While similar, not a dup of #702 because their conditions are different. The fixes may have some overlap, though (but also potentially different since this issue leans toward management of partially liquidated positions).
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 uses the original full position amount without accounting for liquidations that reduced the position.
Impact
The position cannot be closed normally, forcing users to manually manage funds and potentially causing mismanagement of other positions.
What the attacker needed
A leveraged position exists; the position becomes undercollateralized; a liquidator liquidates the position
The fix
The contract should be modified to allow closing a position given a margin or without un-looping, so that liquidated positions can be managed.
How it was found
- Open a leveraged position with 2x margin.
- Drop the price of the collateral to half.
- Set close factor and liquidation incentive.
- Liquidate the position from another account.
- Attempt to close the position, which 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.
A lending protocol lets people borrow using collateral. If the collateral value drops, a liquidator can repay part of the debt and take some collateral. This report shows that after such a liquidation, the contract's function to close the position fails because it still tries to undo the full original amount, not the reduced amount.
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.