# distribute() uses the wrong end time to break maxSeniorRepayment's expectations

LiquidationDistributor.distribute() computes pending interest from block.timestamp instead of loan end time, so senior tranches get more repayment than maxSeniorRepayment allowed.

- Payout: Audit contest
- Severity: medium
- Program: Gondi
- Found by: bin2chen
- Disclosed: 2024-07-25
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-04-gondi-findings/issues/16
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-gondi%3AM-16
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** LiquidationDistributor.distribute() / MultiSourceLoan tranche repayment
**Root cause:** distribute() accrues pending interest over block.timestamp - startTime rather than loan end time (startTime + duration), inflating interest owed to earlier tranches beyond the maxSeniorRepayment bound checked at emitLoan().
**Steps:**
1. Borrower emits loan with multiple tranches; _checkOffer enforces totalAmountWithMaxInterest <= maxSeniorRepayment using loan._duration.
2. Bidding takes ~3-7 days, so tranche.startTime is set later than loan start.
3. On liquidation, distribute() computes pendingInterest = principalAmount.getInterest(aprBps, block.timestamp - thisTranche.startTime).
4. Elapsed time exceeds duration, inflating owedPerTranche for front tranches.
5. With insufficient proceeds, front lenders are repaid more than expected, breaking back lenders' maxSeniorRepayment guarantee.
**How found:** Code reading: compared interest accrual in _checkOffer (uses loan duration) against distribute() (uses block.timestamp - startTime), spotting the mismatch caused by bidding delay.
**Exploitation:** Triggered on liquidation with insufficient proceeds: inflated interest for senior tranches consumes more of the proceeds, reducing repayment to later tranches below their maxSeniorRepayment expectation.
**Insight:** When a bound is enforced at loan creation using a fixed duration, audit every later interest/repayment computation for use of block.timestamp instead of the loan's scheduled end time; bidding delays make the two diverge.

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

**distribute() computes pending interest with block.timestamp**

```
uint256 pendingInterest =
@>              thisTranche.principalAmount.getInterest(thisTranche.aprBps, block.timestamp - thisTranche.startTime);
            totalPrincipalAndPaidInterestOwed += thisTranche.accruedInterest;
            totalPendingInterestOwed += pendingInterest;
            owedPerTranche[i] += thisTranche.principalAmount + thisTranche.accruedInterest + pendingInterest;
```

**maxSeniorRepayment check uses loan duration**

```
totalAmountWithMaxInterest += amount + amount.getInterest(offer.aprBps, _duration);
...

    function _checkOffer(
        LoanOffer calldata _offer,
        address _principalAddress,
        address _nftCollateralAddress,
        uint256 _amountWithInterestAhead
    ) private pure {
        if (_offer.principalAddress != _principalAddress || _offer.nftCollateralAddress != _nftCollateralAddress) {
            revert InvalidAddressesError();
        }
@>      if (_amountWithInterestAhead > _offer.maxSeniorRepayment) {
            revert InvalidTrancheError();
```

**Mitigation diff using loan end time**

```
+      uint256 loanExpireTime = _loan.startTime + _loan.duration;
        for (uint256 i = 0; i < _loan.tranche.length;) {
            IMultiSourceLoan.Tranche calldata thisTranche = _loan.tranche[i];
            uint256 pendingInterest =
-               thisTranche.principalAmount.getInterest(thisTranche.aprBps, block.timestamp - thisTranche.startTime);
+               thisTranche.principalAmount.getInterest(thisTranche.aprBps, loanExpireTime - thisTranche.startTime);
```

