User can abuse tight stop losses and high leverage to make risk free trades
Stop-loss orders were executed at the user-specified stop-loss price rather than the current market price, allowing a trader to open a high-leverage position with a stop-loss just below the entry price. If the price dropped, the position would close at the stop-loss price, capping losses to fees; if the price rose, the trader could make a large gain, with the loss borne by liquidity providers.
0x52 Follow with hansfriese and noot
Originally disclosed on Code4rena
Submitted by 0x52, also found by hansfriese and noot
User can abuse how stop losses are priced to open high leverage trades with huge upside and very little downside.
Proof of Concept
function limitClose(
uint _id,
bool _tp,
PriceData calldata _priceData,
bytes calldata _signature
)
external
{
_checkDelay(_id, false);
(uint _limitPrice, address _tigAsset) = tradingExtension._limitClose(_id, _tp, _priceData, _signature);
_closePosition(_id, DIVISION_CONSTANT, _limitPrice, address(0), _tigAsset, true);
}
function _limitClose(
uint _id,
bool _tp,
PriceData calldata _priceData,
bytes calldata _signature
) external view returns(uint _limitPrice, address _tigAsset) {
_checkGas();
IPosition.Trade memory _trade = position.trades(_id);
_tigAsset = _trade.tigAsset;
getVerifiedPrice(_trade.asset, _priceData, _signature, 0);
uint256 _price = _priceData.price;
if (_trade.orderType != 0) revert("4"); //IsLimit
if (_tp) {
if (_trade.tpPrice == 0) revert("7"); //LimitNotSet
if (_trade.direction) {
if (_trade.tpPrice > _price) revert("6"); //LimitNotMet
} else {
if (_trade.tpPrice < _price) revert("6"); //LimitNotMet
}
_limitPrice = _trade.tpPrice;
} else {
if (_trade.slPrice == 0) revert("7"); //LimitNotSet
if (_trade.direction) {
if (_trade.slPrice < _price) revert("6"); //LimitNotMet
} else {
if (_trade.slPrice > _price) revert("6"); //LimitNotMet
}
//@audit stop loss is closed at user specified price NOT market price
_limitPrice = _trade.slPrice;
}
}
When closing a position with a stop loss the user is closed at their SL price rather than the current price of the asset. A user could abuse this in directional markets with high leverage to make nearly risk free trades. A user could open a long with a stop loss that in $0.01 below the current price. If the price tanks immediately on the next update then they will be closed out at their entrance price, only out the fees to open and close their position. If the price goes up then they can make a large gain.
Recommended Mitigation Steps
Take profit and stop loss trades should be executed at the current price rather than the price specified by the user:
if (_trade.tpPrice == 0) revert("7"); //LimitNotSet
if (_trade.direction) {
if (_trade.tpPrice > _price) revert("6"); //LimitNotMet
} else {
if (_trade.tpPrice < _price) revert("6"); //LimitNotMet
}
- _limitPrice = _trade.tpPrice;
+ _limitPrice = _price;
} else {
if (_trade.slPrice == 0) revert("7"); //LimitNotSet
if (_trade.direction) {
if (_trade.slPrice < _price) revert("6"); //LimitNotMet
} else {
if (_trade.slPrice > _price) revert("6"); //LimitNotMet
}
- _limitPrice = _trade.slPrice;
+ _limitPrice = _price;
TriHaz (Tigris Trade) disputed and commented:
Because of open fees, close fees and spread, that wouldn’t be profitable.
We also have a cooldown after a trade is opened so there will be enough time for price to move freely past the sl.
Alex the Entreprenerd (judge) commented:
The warden has shown a flaw in how the protocol offers Stop Losses.
By using the originally stored value for Stop Loss, instead of just using it as a trigger, an attacker can perform a highly profitable strategy on the system as they know that their max risk is capped by the value of the Stop Loss, instead of the current asset price.
This will happen at the detriment of LPs.
Because the attack breaks an important invariant, causing a loss to other users, I agree with High Severity.
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
The stop-loss price was used as the execution price instead of only as a trigger, so the position closed at the user-chosen level rather than the current market price.
Impact
A trader could make nearly risk-free high-leverage trades, with losses capped at fees and gains potentially large, at the expense of liquidity providers; the protocol disputed profitability due to fees and a cooldown, but the judge rated it high severity.
What the attacker needed
Ability to open a leveraged trade; stop-loss set near entry price; price moves favourably or unfavourably
The fix
Stop-loss and take-profit orders should execute at the current verified price rather than the user-specified price.
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 stop-loss is an instruction to close a trade if the price falls to a certain level, to limit losses. Normally it should close at the best available market price at that moment. Here, the system closed the trade at the exact stop-loss price the user chose, even if the market had already fallen much further. That let a trader set a stop-loss very close to the entry price, so a big drop would only cost fees, while a rise would give a big profit.
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.