Submitted by nonseodion, also found by 0xbepresent
When a Short Order is being created it tries to fill its Short Record. If it fills the Short Record, the Short Record is given a filled status (SR.FullyFilled) and the Short Order isnt added to the market. But if it doesnt fill the Short Record, it is given a partially filled status (SR.PartiallyFilled) and the remaining part of the Short Order is added to the market.
The issue is the current implementation doesnt add the Short Order to the market every time the Short Record is partially filled. It does this in the sellMatchAlgo() function loop when it tries to match bids.
LibOrders.sol#L591-L597
When the Short Order is being matched in the sellMatchAlgo() loop, it encounters the check in the if statement above. If the value of the erc remaining in the short is less than minAskEth it is not added to the market. The Short Record is already given the SR.PartiallyFilled status before the check.
When this happens, the Short Record is created with no associated Short Order. This prevents the user from exiting the Short Record and a liquidator from liquidating the position if it ever becomes liquidatable. These actions revert with InvalidShortOrder() error in the following portion of the code.
When a user tries to exit using any of the exit functions, he has to pass a Short Order id.
ExitShortFacet.sol#L41
ExitShortFacet.sol#L87
ExitShortFacet.sol#L142
Since there is no valid Short Order Id, if he passes any value it reverts when the user tries to exit. Because the id needs to be associated with the shortRecord and still be owned by him to pass the checks. exitShort() function calls checkCancelShortOrder() which will revert in the check below.
LibSRUtil.sol#L57
For exitShortWallet() and exitShortErcEscrowed(), they revert in the check below when they call checkShortMinErc().
LibSRUtil.sol#L84
The primary and secondary liquidation calls require a Short Order Id.
Primary Liquidation call:
PrimaryLiquidationFacet.sol#L47
Secondary Liquidation call:
SecondaryLiquidationFacet.sol#L39
BatchLiquidation struct:
The liquidate() function reverts in its call to checkCancelShortOrder(). The check below causes the revert, because the id passed by the liquidator needs to be associated with the Short Record and still be owned by the user being liquidated to pass the check.
LibSRUtil.sol#L57
The liquidateSecondary() function uses a loop to complete batch liquidation. In the loop, it first does the check below on each batch element.
SecondaryLiquidationFacet.sol#L69-L80
The loop skips liquidating if the Short Records debt is below the minimum i.e. shortUnderMin is true for the passed shortOrder and shortOrder.shortRecordId != m.short.id || shortOrder.addr != m.shorter evaluates to true since the short order isnt attached to the Short Record.
It reverts in the check below. liquidateAmount is the amount the liquidator wants to liquidate and liquidateAmountLeft is the amount not liquidated. If only the bad Short Record is in the batch, it reverts. If other Short Records in the batch get liquidated it doesnt revert.
SecondaryLiquidationFacet.sol#L124
Note: Secondary Liquidation can still be done in some scenarios check the POC section for more details.
Apart from the DOS effects above, the issue also lets a user create a Short Record with an erc amount below the minShortErc. Check the POC.
The issue above has the following effects:
The tests can be run in the Shorts.t.sol file.
The POC below consists of 5 tests:
It also contains a utility function for setting up the Short Record, createPartiallyFilledSR.
Add this import statement to the top of the Shorts.t.sol file:
import {STypes, MTypes, O, SR} from "contracts/libraries/DataTypes.sol";
Consider setting ercAmount of the incomingAsk to zero in the sellMatchAlgo() function. This will allow the matchIncomingSell() call to set the Short Record to a Fully Filled state.
LibOrders.sol#L590-L598
DoS
ditto-eth (DittoETH) confirmed and commented:
