Credit can be sold forcibly as forSale setting can be ignored via Compensate
A borrower can force the sale of a lender's credit position even when the lender has set it as not for sale, by using the Compensate function with a reserved ID to create a new credit position that is for sale. This allows the borrower to sell the lender's credit at a profit if market conditions are favourable, though the lender does not suffer a direct loss.
hyh Follow with 0xAlix2, almurhasan and 3 others
Originally disclosed on Code4rena
Submitted by hyh, also found by 0xAlix2, almurhasan, BenRai, 0xStalin, and bin2chen
Any credit position can be forcibly sold with the help of its borrower. This will be executed only when have expected profit; for example, when lender’s curve is not null and is above the market it is profitable to buy credit from them (lend to them at above market rates), but they might block it with the lack of free collateral and the forSale = false flag, which can be overridden by the corresponding borrower of this credit position. The impact of this forced sale is proportional to interest rate volatility and can be substantial. There are no additional prerequisites for the setup.
Proof of Concept
createDebtAndCreditPositions creates credit positions with forSale == true:
function createDebtAndCreditPositions(
...
) external returns (CreditPosition memory creditPosition) {
DebtPosition memory debtPosition =
DebtPosition({borrower: borrower, futureValue: futureValue, dueDate: dueDate, liquidityIndexAtRepayment: 0});
uint256 debtPositionId = state.data.nextDebtPositionId++;
state.data.debtPositions[debtPositionId] = debtPosition;
emit Events.CreateDebtPosition(debtPositionId, borrower, lender, futureValue, dueDate);
creditPosition = CreditPosition({
lender: lender,
credit: debtPosition.futureValue,
debtPositionId: debtPositionId,
>> forSale: true
});
This can be done on demand for any credit position by the borrower of the corresponding debt position, by running Compensate with params.creditPositionToCompensateId == RESERVED_ID, which will create new position with forSale == true and substitute the existing lender position with it:
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
});
Then, it can be then bought with BuyCreditMarket:
borrower = creditPosition.lender;
tenor = debtPosition.dueDate - block.timestamp; // positive since the credit position is transferrable, so the loan must be ACTIVE
}
BorrowOffer memory borrowOffer = state.data.users[borrower].borrowOffer;
Recommended Mitigation Steps
Consider passing the flag to createDebtAndCreditPositions() indicating forSale flag to be set, which be passed from the existing credit in Compensate.sol#L139, so it won’t be changed.
aviggiano (Size) confirmed and commented:
Fixed in https://github.com/SizeCredit/size-solidity/pull/130.
hansfriese (judge) decreased severity to Medium and commented:
Valid finding.
Medium is more appropriate due to the below reasons.
- Buying the lender’s credit position doesn’t mean any loss to him as it’s sold with his borrow offer. (It’s just an unintended behavior.)
- The lender can prevent this by setting
allCreditPositionsForSaleDisabled = false.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The Compensate function creates a new credit position with forSale set to true when the reserved ID is used, ignoring the original credit position's forSale flag.
Impact
The lender's credit position can be forcibly sold, but the lender does not suffer a direct loss because the sale occurs at their own borrow offer; the impact is proportional to interest rate volatility and can be substantial.
What the attacker needed
Borrower of the debt position; profitable market conditions
The fix
The fix was to pass the forSale flag from the existing credit position when creating a new one in Compensate, so it won't be changed.
How it was found
- The borrower calls Compensate with params.creditPositionToCompensateId set to RESERVED_ID.
- This creates a new credit position with forSale set to true and assigns it to the lender.
- The new credit position can then be bought via BuyCreditMarket.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
In this lending system, a lender can mark their credit position as not for sale to prevent others from buying it. However, a borrower can use a function called Compensate to create a new credit position that is automatically for sale, effectively overriding the lender's setting. This means the borrower can force the sale of the lender's credit, but the lender doesn't lose money because the sale happens at the lender's own offered terms.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.