<!-- canonical: https://0xsimao.com/findings/autonomint-deposited-modified-depositor-stuck -->

# Total cds deposited amount is incorrectly modified when cds depositor is at a loss, leading to stuck USDa

Crit/High · Sherlock · Hedged stablecoin · 4th December 2024

Finding H-12 of the Autonomint competition.

- Protocol: https://audits.sherlock.xyz/contests/569
- Report: /reports/autonomint
- Codebase: https://github.com/0xsimao/2024-11-autonomint/tree/0d324e04d4c0ca306e1ae4d4c65f0cb9d681751b
- Source: https://github.com/sherlock-audit/2024-11-autonomint-judging/issues/738

---

### Summary

The total cds deposited amount is [decreased](https://github.com/sherlock-audit/2024-11-autonomint/blob/main/Blockchain/Blockchian/contracts/lib/CDSLib.sol#L713) by the [returned amount](https://github.com/sherlock-audit/2024-11-autonomint/blob/main/Blockchain/Blockchian/contracts/Core_logic/CDS.sol#L343) when the cds depositor withdraws, which will include any loss that the cds depositor has taken. After the withdrawal, the following cumulative values will be [calculated](https://github.com/sherlock-audit/2024-11-autonomint/blob/main/Blockchain/Blockchian/contracts/Core_logic/CDS.sol#L312) based on this faulty total cds deposited amount and lead to stuck USDa.

### Root Cause

In `CDSLib.sol:713`, `params.omniChainData.totalCdsDepositedAmount` is decreased by `params.cdsDepositDetails.depositedAmount`, which includes the loss.

### Internal pre-conditions

None.

### External pre-conditions

None.

### Attack Path

1. Cds depositors deposit 400 and 600 USDa, respectively, at a price of 1000 USD / ETH.
2. Borrower deposits 1 ETH at the same 1000 USD / ETH price.
3. Price drops to 900 USD / ETH.
4. 400 cds depositor withdraws, calculating a lossy cumulative value of `1 * (1000 - 900) / 1000 = 0.1`. The return amount is `400 - 400 * 0.1 = 360`. The final total cds deposited amount is 1000 - 360 = 640.
5. All the cumulative values will be calculated on a total cds deposited amount of 640, but the cds depositor left has 600 deposit amount, so all values will be off. For example, consider that the price goes up to 1000 USD / ETH again, and the cds depositor withdraws. The cumulative value will be increased by `1 * (1000 - 900) / 640 = 0.15625`, netting `-0.1 + 0.15625 = 0.05625`, which becomes `600 + 600 * 0.05625 = 633.75`, so 6.25 USDa will be stuck.
6. Alternatively, if the price drops to 850 USD / ETH, and the borrower withdraws, the final total cds deposited amount will underflow, as the downside protected value is 150, so it becomes `640 - 150 - (600 - 600 * (0.1 + 1 * (900 - 850) / 640)) = -3.125`. Note that if it was divided by 600, instead of 640, it would not underflow and everything would add up, for example.

### Impact

Stuck USDa, loss of USDa on withdrawal or DoSed withdrawal, depending on use case.

### PoC

See attack path above.

### Mitigation

Divide by the correct total cds deposited, such that the sum of individual cds deposited amounts equals the total cds deposited amount.

---

Related findings:

- [RsaVerifyOptimized::pkcs1Sha256() modified the original code incorrectly in one instance](https://0xsimao.com/findings/keyring-ii-pkcs1-sha256-modified-original): Keyring Credentials
- [Mismatch between yield amount deposited in shares calculation and getAccountYieldBalance()](https://0xsimao.com/findings/benddao-mismatch-yield-deposited-shares): BendDAO
- [MToken's total principal invariant doesn't hold without MinterGateway, leading to potential principal loss](https://0xsimao.com/findings/m-0-invariant-hold-minter-gateway): M^0 Minter Gateway
- [Stuck ETH in Curve exchanges due to sending msg.value to the exchange instead of amountIn](https://0xsimao.com/findings/singularity-stuck-eth-curve-exchanges): Singularity
