# Borrower can obtain principle tokens without paying collateral tokens

DebitaV3Aggregator.matchOffersV3 rounds collateral to zero for small lend amounts, letting a borrower take principal tokens without paying collateral.

- Payout: Audit contest
- Severity: medium
- Program: Debita
- Found by: xiaoming90
- Disclosed: 2024-10-31
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-10-debita-judging/issues/558
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-10-debita%3AM-17
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:evm tech:solidity vuln:business-logic vuln:precision-loss

**Target:** DebitaV3Aggregator.matchOffersV3 lending match function
**Root cause:** userUsedCollateral = (lendAmountPerOrder * 10**decimalsCollateral) / ratio truncates to zero when lendAmountPerOrder < ratio/10**decimalsCollateral (e.g. <1e12 for 6-decimal collateral, 1e18 ratio), and no zero-check exists.
**Steps:**
1. Set ratio=1e18, collateral ABC 6 decimals, principal XYZ 18 decimals.
2. Call `matchOffersV3` with lendAmountPerOrder[i] = 1e12 - 1 per offer.
3. userUsedCollateral = ((1e12-1)*1e6)/1e18 = 0, so no ABC collateral is deducted.
4. Borrower receives 1e12-1 XYZ per offer; repeat across up to 100 offers per tx on cheap L2s.
**How found:** Code-reading of the collateral formula in matchOffersV3; recognized integer division truncation with mismatched token decimals (18 vs 6) yields zero, then computed the lendAmountPerOrder range that rounds down.
**Exploitation:** Malicious borrower submits borrow offers with lendAmountPerOrder just below the rounding threshold, matching lender offers via permissionless matchOffersV3; collateral computation truncates to zero while principal is still disbursed, extracting principal tokens for free at scale across 100 offers per tx.
**Escalation:** Batching up to 100 lend orders per transaction and deploying on low-gas L2s (Sonic, Base, Arbitrum, OP) makes repeated zero-collateral borrows profitable.
**Insight:** When collateral/principal conversion divides by a ratio scaled by 1e18, check decimal mismatch between tokens: a 6-decimal collateral token makes the truncation-to-zero threshold 1e12, large enough to be a profitable borrow amount.

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

**Rounding-to-zero collateral calculation in matchOffersV3**

```
userUsedCollateral = (lendAmountPerOrder[i] * (10 ** decimalsCollateral)) / ratio;
userUsedCollateral = (lendAmountPerOrder[i] * 1e6) / 1e18;

https://github.com/sherlock-audit/2024-11-debita-finance-v3/blob/main/Debita-V3-Contracts/contracts/DebitaV3Aggregator.sol#L467

File: DebitaV3Aggregator.sol
274:     function matchOffersV3(
..SNIP..
466:             // calculate the amount of collateral used by the lender
467:             uint userUsedCollateral = (lendAmountPerOrder[i] *
468:                 (10 ** decimalsCollateral)) / ratio;


For `lendAmountPerOrder`, he uses a value that is small enough to trigger a rounding to zero error. The range of `lendAmountPerOrder` that will cause `userUsedCollateral` to round down to zero is:

$0≤lendAmountPerOrder<10^{12}$

Thus, for each offer, Bob will specify the `lendAmountPerOrder[i]` to be `1e12 - 1`. Thus, for each offer, he will be able to obtain `1e12 - 1` XYZ tokens without paying a single ABC tokens as collateral.

This attack is profitable because each `matchOffersV3` transaction can execute up to 100 offers, and the protocol is intended to be deployed on L2 chains where gas fees are extremely cheap or even negligible.

https://github.com/sherlock-audit/2024-11-debita-finance-v3/blob/main/Debita-V3-Contracts/contracts/DebitaV3Aggregator.sol#L290

File: DebitaV3Aggregator.sol
274:     function matchOffersV3(
..SNIP..
289:         // check lendOrder length is less than 100
290:         require(lendOrders.length <= 100, "Too many lend orders");

Following is the extract from [Contest's README](https://github.com/sherlock-audit/2024-11-debita-finance-v3-xiaoming9090?tab=readme-ov-file#q-on-what-chains-are-the-smart-contracts-going-to-be-deployed) showing that the protocol will be deployed to following L2 chains.

> Q: On what chains are the smart contracts going to be deployed?
>
> Sonic (Prev. Fantom), Base, Arbitrum & OP

### Impact

High. Loss of assets.

### PoC

_No response_

### Mitigation

This issue can be easily mitigated by implementing the following changes to prevent the above attack.

// calculate the amount of collateral used by the lender
uint userUsedCollateral = (lendAmountPerOrder[i] * (10 ** decimalsCollateral)) / ratio;
```

