# Credit can be sold forcibly as forSale setting can be ignored via Compensate

Compensate with RESERVED_ID creates a new credit position with forSale=true, letting a borrower force-sell a lender's credit despite forSale=false.

- Payout: Audit contest
- Severity: medium
- Program: Size
- Found by: hyh
- Disclosed: 2024-09-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-06-size-findings/issues/179
- Library page: https://bugbountylibrary.com/r/c4%3A2024-06-size%3AM-07
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:medium tech:solidity technique:source-review vuln:access-control vuln:business-logic

**Target:** Solidity lending protocol Compensate/BuyCreditMarket actions
**Root cause:** createDebtAndCreditPositions always sets forSale=true; Compensate with creditPositionToCompensateId==RESERVED_ID creates a fresh credit position and assigns it to the existing lender, discarding the original position's forSale=false setting.
**Steps:**
1. Lender holds a credit position with forSale=false and no free collateral.
2. Borrower of the corresponding debt position calls `Compensate` with `params.creditPositionToCompensateId == RESERVED_ID`, which creates a new credit position with `forSale == true` and substitutes the existing lender position.
3. Compensate calls `createDebtAndCreditPositions` producing a new credit with `forSale: true`, then `createCreditPosition` assigns it to the lender instead of the existing position with `forSale == false`.
4. Attacker buys the newly for-sale credit via `BuyCreditMarket` at the lender's borrow offer.
**How found:** Code reading of Compensate's RESERVED_ID branch noticing createDebtAndCreditPositions hardcodes forSale=true and the new position replaces the lender's existing forSale=false position.
**Exploitation:** Borrower triggers Compensate with RESERVED_ID to mint a forSale=true credit position for the lender, then purchases it through BuyCreditMarket when the lender's curve is above market, capturing the rate spread the lender intended to block.
**Insight:** When a protocol mints a replacement position to substitute an existing one, check whether per-position flags (forSale, permissions, config) are copied from the original or reset to constructor defaults.

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

**Compensate with RESERVED_ID creates forSale credit**

```
CreditPosition memory creditPositionToCompensate;
        if (params.creditPositionToCompensateId == RESERVED_ID) {
>>          creditPositionToCompensate = state.createDebtAndCreditPositions({  // @audit create new credit with `forSale == true`
                lender: msg.sender,
                borrower: msg.sender,
                futureValue: amountToCompensate,
                dueDate: debtPositionToRepay.dueDate
            });
        } else {
            creditPositionToCompensate = state.getCreditPosition(params.creditPositionToCompensateId);
            amountToCompensate = Math.min(amountToCompensate, creditPositionToCompensate.credit);
        }

        // debt and credit reduction
        state.reduceDebtAndCredit(
            creditPositionWithDebtToRepay.debtPositionId, params.creditPositionWithDebtToRepayId, amountToCompensate
        );

        uint256 exiterCreditRemaining = creditPositionToCompensate.credit - amountToCompensate;

        // credit emission
>>      state.createCreditPosition({
            exitCreditPositionId: params.creditPositionToCompensateId == RESERVED_ID  // @audit give it to the lender instead of the existing position with `forSale == false`
                ? state.data.nextCreditPositionId - 1
                : params.creditPositionToCompensateId,
            lender: creditPositionWithDebtToRepay.lender,
            credit: amountToCompensate
        });
```

**createDebtAndCreditPositions sets forSale true**

```
creditPosition = CreditPosition({
            lender: lender,
            credit: debtPosition.futureValue,
            debtPositionId: debtPositionId,
>>          forSale: true
        });
```

